Skip to content

Commit 9ffd2e4

Browse files
authored
[SimplifyCFG] Fix SimplifyCFG pass to skip folding when both blocks contain convergence loop/entry intrinsics. (#166452)
Fixes a bug #165642. [Similar fix](#165643) is being made in `IndVarSimplify` pass to account for convergence tokens. [LLVM Spec](https://llvm.org/docs/ConvergentOperations.html#llvm-experimental-convergence-loop) states that only a single loop / entry convergence token can be included in a basic block. This PR fixes the issue in `SimplifyCFG` pass so that when a basic block and its predecessor both contain such convergence intrinsics, it skips merging the two blocks.
1 parent cd3192a commit 9ffd2e4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ emptyAndDetachBlock(BasicBlock *BB,
9292
"applying corresponding DTU updates.");
9393
}
9494

95+
static bool HasLoopOrEntryConvergenceToken(const BasicBlock *BB) {
96+
for (const Instruction &I : *BB) {
97+
const ConvergenceControlInst *CCI = dyn_cast<ConvergenceControlInst>(&I);
98+
if (CCI && (CCI->isLoop() || CCI->isEntry()))
99+
return true;
100+
}
101+
return false;
102+
}
103+
95104
void llvm::detachDeadBlocks(ArrayRef<BasicBlock *> BBs,
96105
SmallVectorImpl<DominatorTree::UpdateType> *Updates,
97106
bool KeepOneInputPHIs) {
@@ -259,6 +268,13 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
259268
if (llvm::is_contained(PN.incoming_values(), &PN))
260269
return false;
261270

271+
// Don't break if both the basic block and the predecessor contain loop or
272+
// entry convergent intrinsics, since there may only be one convergence token
273+
// per block.
274+
if (HasLoopOrEntryConvergenceToken(BB) &&
275+
HasLoopOrEntryConvergenceToken(PredBB))
276+
return false;
277+
262278
LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
263279
<< PredBB->getName() << "\n");
264280

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -S -passes=simplifycfg | FileCheck %s
3+
4+
declare token @llvm.experimental.convergence.entry() #0
5+
6+
define void @nested(i32 %tidx, i32 %tidy, ptr %array) #0 {
7+
; CHECK-LABEL: @nested(
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: [[TMP0:%.*]] = tail call token @llvm.experimental.convergence.entry()
10+
; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[TIDY:%.*]], [[TIDX:%.*]]
11+
; CHECK-NEXT: [[OR_COND_I:%.*]] = icmp eq i32 [[TMP1]], 0
12+
; CHECK-NEXT: br label [[FOR_COND_I:%.*]]
13+
; CHECK: for.cond.i:
14+
; CHECK-NEXT: [[TMP2:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[TMP0]]) ]
15+
; CHECK-NEXT: br label [[FOR_COND1_I:%.*]]
16+
; CHECK: for.cond1.i:
17+
; CHECK-NEXT: [[CMP2_I:%.*]] = phi i1 [ false, [[FOR_BODY4_I:%.*]] ], [ true, [[FOR_COND_I]] ]
18+
; CHECK-NEXT: [[TMP3:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[TMP2]]) ]
19+
; CHECK-NEXT: br i1 [[CMP2_I]], label [[FOR_BODY4_I]], label [[EXIT:%.*]]
20+
; CHECK: for.body4.i:
21+
; CHECK-NEXT: br i1 [[OR_COND_I]], label [[IF_THEN_I:%.*]], label [[FOR_COND1_I]]
22+
; CHECK: if.then.i:
23+
; CHECK-NEXT: [[TEST_VAL:%.*]] = call spir_func i32 @func_test(i32 0) [ "convergencectrl"(token [[TMP3]]) ]
24+
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[ARRAY:%.*]], i32 0
25+
; CHECK-NEXT: store i32 [[TEST_VAL]], ptr [[TMP4]], align 4
26+
; CHECK-NEXT: br label [[EXIT]]
27+
; CHECK: exit:
28+
; CHECK-NEXT: ret void
29+
;
30+
entry:
31+
%0 = tail call token @llvm.experimental.convergence.entry()
32+
%2 = or i32 %tidy, %tidx
33+
%or.cond.i = icmp eq i32 %2, 0
34+
br label %for.cond.i
35+
36+
for.cond.i:
37+
%3 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %0) ]
38+
br label %for.cond1.i
39+
40+
for.cond1.i:
41+
%cmp2.i = phi i1 [ false, %for.body4.i ], [ true, %for.cond.i ]
42+
%4 = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %3) ]
43+
br i1 %cmp2.i, label %for.body4.i, label %cleanup.i.loopexit
44+
45+
for.body4.i:
46+
br i1 %or.cond.i, label %if.then.i, label %for.cond1.i
47+
48+
if.then.i:
49+
%test.val = call spir_func i32 @func_test(i32 0) [ "convergencectrl"(token %4) ]
50+
%5 = getelementptr inbounds i32, ptr %array, i32 0
51+
store i32 %test.val, ptr %5, align 4
52+
br label %cleanup.i
53+
54+
cleanup.i.loopexit:
55+
br label %cleanup.i
56+
57+
cleanup.i:
58+
br label %exit
59+
60+
exit:
61+
ret void
62+
}
63+
64+
declare token @llvm.experimental.convergence.loop() #0
65+
66+
declare i32 @func_test(i32) #0
67+
68+
attributes #0 = { convergent }

0 commit comments

Comments
 (0)