Skip to content

Commit fcdb220

Browse files
authored
[AMDGPU][AtomicOptimizer] Fix DT update for divergent values with Iterative strategy (#87605)
We take the terminator from EntryBB and put it in ComputeEnd. Make sure we also move the DT edges, we previously only did it assuming a non-conditional branch. Fixes SWDEV-453943
1 parent da579ad commit fcdb220

File tree

2 files changed

+108
-9
lines changed

2 files changed

+108
-9
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAtomicOptimizer.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ void AMDGPUAtomicOptimizerImpl::optimizeAtomic(Instruction &I,
743743

744744
Function *F = I.getFunction();
745745
LLVMContext &C = F->getContext();
746-
746+
747747
// For atomic sub, perform scan with add operation and allow one lane to
748748
// subtract the reduced value later.
749749
AtomicRMWInst::BinOp ScanOp = Op;
@@ -855,7 +855,7 @@ void AMDGPUAtomicOptimizerImpl::optimizeAtomic(Instruction &I,
855855
Value *const Cond = B.CreateICmpEQ(Mbcnt, B.getInt32(0));
856856

857857
// Store I's original basic block before we split the block.
858-
BasicBlock *const EntryBB = I.getParent();
858+
BasicBlock *const OriginalBB = I.getParent();
859859

860860
// We need to introduce some new control flow to force a single lane to be
861861
// active. We do this by splitting I's basic block at I, and introducing the
@@ -876,25 +876,36 @@ void AMDGPUAtomicOptimizerImpl::optimizeAtomic(Instruction &I,
876876
BasicBlock *Predecessor = nullptr;
877877
if (ValDivergent && ScanImpl == ScanOptions::Iterative) {
878878
// Move terminator from I's block to ComputeEnd block.
879-
Instruction *Terminator = EntryBB->getTerminator();
879+
//
880+
// OriginalBB is known to have a branch as terminator because
881+
// SplitBlockAndInsertIfThen will have inserted one.
882+
BranchInst *Terminator = cast<BranchInst>(OriginalBB->getTerminator());
880883
B.SetInsertPoint(ComputeEnd);
881884
Terminator->removeFromParent();
882885
B.Insert(Terminator);
883886

884887
// Branch to ComputeLoop Block unconditionally from the I's block for
885888
// iterative approach.
886-
B.SetInsertPoint(EntryBB);
889+
B.SetInsertPoint(OriginalBB);
887890
B.CreateBr(ComputeLoop);
888891

889892
// Update the dominator tree for new control flow.
890-
DTU.applyUpdates(
891-
{{DominatorTree::Insert, EntryBB, ComputeLoop},
892-
{DominatorTree::Insert, ComputeLoop, ComputeEnd},
893-
{DominatorTree::Delete, EntryBB, SingleLaneTerminator->getParent()}});
893+
SmallVector<DominatorTree::UpdateType, 6> DomTreeUpdates(
894+
{{DominatorTree::Insert, OriginalBB, ComputeLoop},
895+
{DominatorTree::Insert, ComputeLoop, ComputeEnd}});
896+
897+
// We're moving the terminator from EntryBB to ComputeEnd, make sure we move
898+
// the DT edges as well.
899+
for (auto *Succ : Terminator->successors()) {
900+
DomTreeUpdates.push_back({DominatorTree::Insert, ComputeEnd, Succ});
901+
DomTreeUpdates.push_back({DominatorTree::Delete, OriginalBB, Succ});
902+
}
903+
904+
DTU.applyUpdates(DomTreeUpdates);
894905

895906
Predecessor = ComputeEnd;
896907
} else {
897-
Predecessor = EntryBB;
908+
Predecessor = OriginalBB;
898909
}
899910
// Move the IR builder into single_lane next.
900911
B.SetInsertPoint(SingleLaneTerminator);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -mtriple=amdgcn-- -mcpu=gfx908 -passes="amdgpu-atomic-optimizer,verify<domtree>" %s -S -o - | FileCheck %s
3+
4+
; Check we're properly adding an edge from ComputeEnd to the "End" block added by
5+
; SplitBlockAndInsertIfThen
6+
;
7+
; If the edge is not added, domtree verification will fail.
8+
9+
declare i32 @quux()
10+
11+
define amdgpu_kernel void @ham(ptr addrspace(4) %arg) {
12+
; CHECK-LABEL: define amdgpu_kernel void @ham(
13+
; CHECK-SAME: ptr addrspace(4) [[ARG:%.*]]) #[[ATTR0:[0-9]+]] {
14+
; CHECK-NEXT: bb:
15+
; CHECK-NEXT: [[CALL:%.*]] = tail call i32 @quux()
16+
; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i32 [[CALL]], 0
17+
; CHECK-NEXT: br i1 [[ICMP]], label [[BB1:%.*]], label [[BB3:%.*]]
18+
; CHECK: bb1:
19+
; CHECK-NEXT: [[CALL2:%.*]] = tail call i32 @quux()
20+
; CHECK-NEXT: br label [[BB3]]
21+
; CHECK: bb3:
22+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[CALL2]], [[BB1]] ], [ [[CALL]], [[BB:%.*]] ]
23+
; CHECK-NEXT: br label [[BB4:%.*]]
24+
; CHECK: bb4:
25+
; CHECK-NEXT: [[CALL5:%.*]] = tail call i32 @quux()
26+
; CHECK-NEXT: [[ICMP6:%.*]] = icmp eq i32 [[CALL5]], 0
27+
; CHECK-NEXT: br i1 [[ICMP6]], label [[BB8:%.*]], label [[BB7:%.*]]
28+
; CHECK: bb7:
29+
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr addrspace(4) [[ARG]], align 8
30+
; CHECK-NEXT: [[ADDRSPACECAST:%.*]] = addrspacecast ptr [[LOAD]] to ptr addrspace(1)
31+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.amdgcn.ballot.i64(i1 true)
32+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[TMP0]] to i32
33+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i64 [[TMP0]], 32
34+
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32
35+
; CHECK-NEXT: [[TMP4:%.*]] = call i32 @llvm.amdgcn.mbcnt.lo(i32 [[TMP1]], i32 0)
36+
; CHECK-NEXT: [[TMP5:%.*]] = call i32 @llvm.amdgcn.mbcnt.hi(i32 [[TMP3]], i32 [[TMP4]])
37+
; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.amdgcn.ballot.i64(i1 true)
38+
; CHECK-NEXT: br label [[COMPUTELOOP:%.*]]
39+
; CHECK: 7:
40+
; CHECK-NEXT: [[TMP8:%.*]] = atomicrmw add ptr addrspace(1) [[ADDRSPACECAST]], i32 [[TMP13:%.*]] syncscope("agent-one-as") monotonic, align 4
41+
; CHECK-NEXT: br label [[TMP9:%.*]]
42+
; CHECK: 9:
43+
; CHECK-NEXT: br label [[BB8]]
44+
; CHECK: bb8:
45+
; CHECK-NEXT: br label [[BB4]]
46+
; CHECK: ComputeLoop:
47+
; CHECK-NEXT: [[ACCUMULATOR:%.*]] = phi i32 [ 0, [[BB7]] ], [ [[TMP13]], [[COMPUTELOOP]] ]
48+
; CHECK-NEXT: [[ACTIVEBITS:%.*]] = phi i64 [ [[TMP6]], [[BB7]] ], [ [[TMP16:%.*]], [[COMPUTELOOP]] ]
49+
; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.cttz.i64(i64 [[ACTIVEBITS]], i1 true)
50+
; CHECK-NEXT: [[TMP11:%.*]] = trunc i64 [[TMP10]] to i32
51+
; CHECK-NEXT: [[TMP12:%.*]] = call i32 @llvm.amdgcn.readlane(i32 [[PHI]], i32 [[TMP11]])
52+
; CHECK-NEXT: [[TMP13]] = add i32 [[ACCUMULATOR]], [[TMP12]]
53+
; CHECK-NEXT: [[TMP14:%.*]] = shl i64 1, [[TMP10]]
54+
; CHECK-NEXT: [[TMP15:%.*]] = xor i64 [[TMP14]], -1
55+
; CHECK-NEXT: [[TMP16]] = and i64 [[ACTIVEBITS]], [[TMP15]]
56+
; CHECK-NEXT: [[TMP17:%.*]] = icmp eq i64 [[TMP16]], 0
57+
; CHECK-NEXT: br i1 [[TMP17]], label [[COMPUTEEND:%.*]], label [[COMPUTELOOP]]
58+
; CHECK: ComputeEnd:
59+
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i32 [[TMP5]], 0
60+
; CHECK-NEXT: br i1 [[TMP18]], label [[TMP7:%.*]], label [[TMP9]]
61+
;
62+
bb:
63+
%call = tail call i32 @quux()
64+
%icmp = icmp eq i32 %call, 0
65+
br i1 %icmp, label %bb1, label %bb3
66+
67+
bb1: ; preds = %bb
68+
%call2 = tail call i32 @quux()
69+
br label %bb3
70+
71+
bb3: ; preds = %bb1, %bb
72+
%phi = phi i32 [ %call2, %bb1 ], [ %call, %bb ]
73+
br label %bb4
74+
75+
bb4: ; preds = %bb8, %bb3
76+
%call5 = tail call i32 @quux()
77+
%icmp6 = icmp eq i32 %call5, 0
78+
br i1 %icmp6, label %bb8, label %bb7
79+
80+
bb7: ; preds = %bb4
81+
%load = load ptr, ptr addrspace(4) %arg, align 8
82+
%addrspacecast = addrspacecast ptr %load to ptr addrspace(1)
83+
%atomicrmw = atomicrmw add ptr addrspace(1) %addrspacecast, i32 %phi syncscope("agent-one-as") monotonic, align 4
84+
br label %bb8
85+
86+
bb8: ; preds = %bb7, %bb4
87+
br label %bb4
88+
}

0 commit comments

Comments
 (0)