Skip to content

Commit df25fb1

Browse files
committed
Limit the check to LICM pass with coro.suspend function call detected.
1 parent 4ce3f0e commit df25fb1

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

llvm/include/llvm/Analysis/LoopInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ class LLVM_ABI Loop : public LoopBase<BasicBlock, Loop> {
5959
};
6060

6161
/// Return true if the specified value is loop invariant.
62-
bool isLoopInvariant(const Value *V) const;
62+
bool isLoopInvariant(const Value *V, bool HasCoroSuspendInst = false) const;
6363

6464
/// Return true if all the operands of the specified instruction are loop
6565
/// invariant.
66-
bool hasLoopInvariantOperands(const Instruction *I) const;
66+
bool hasLoopInvariantOperands(const Instruction *I,
67+
bool HasCoroSuspendInst = false) const;
6768

6869
/// If the given value is an instruction inside of the loop and it can be
6970
/// hoisted, do so to make it trivially loop-invariant.

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ LLVM_ABI bool hoistRegion(DomTreeNode *, AAResults *, LoopInfo *,
185185
TargetLibraryInfo *, Loop *, MemorySSAUpdater &,
186186
ScalarEvolution *, ICFLoopSafetyInfo *,
187187
SinkAndHoistLICMFlags &, OptimizationRemarkEmitter *,
188-
bool, bool AllowSpeculation);
188+
bool, bool AllowSpeculation,
189+
bool HasCoroSuspendInst = false);
189190

190191
/// Return true if the induction variable \p IV in a Loop whose latch is
191192
/// \p LatchBlock would become dead if the exit test \p Cond were removed.

llvm/lib/Analysis/LoopInfo.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,26 @@ static cl::opt<bool, true>
5858
// Loop implementation
5959
//
6060

61-
bool Loop::isLoopInvariant(const Value *V) const {
61+
bool Loop::isLoopInvariant(const Value *V, bool HasCoroSuspendInst) const {
6262
if (const Instruction *I = dyn_cast<Instruction>(V)) {
6363
// FIXME: this is semantically inconsistent. We're tracking a proper fix in
6464
// issue #149604.
65-
// If V is a pointer to stack object and F is a coroutine function, then V
66-
// may not be loop invariant because the ramp function and resume function
67-
// have different stack frames.
68-
if (isa<AllocaInst>(I) &&
69-
I->getParent()->getParent()->isPresplitCoroutine())
65+
// If V is a pointer to stack object and L contains a coro.suspend function
66+
// call, then V may not be loop invariant because the ramp function and
67+
// resume function have different stack frames.
68+
if (HasCoroSuspendInst && isa<AllocaInst>(I))
7069
return false;
7170
else
7271
return !contains(I);
7372
}
7473
return true; // All non-instructions are loop invariant
7574
}
7675

77-
bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
78-
return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
76+
bool Loop::hasLoopInvariantOperands(const Instruction *I,
77+
bool HasCoroSuspendInst) const {
78+
return all_of(I->operands(), [&](Value *V) {
79+
return isLoopInvariant(V, HasCoroSuspendInst);
80+
});
7981
}
8082

8183
bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
472472
if (Preheader)
473473
Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, AC, TLI, L,
474474
MSSAU, SE, &SafetyInfo, Flags, ORE, LoopNestMode,
475-
LicmAllowSpeculation);
475+
LicmAllowSpeculation, HasCoroSuspendInst);
476476

477477
// Now that all loop invariants have been removed from the loop, promote any
478478
// memory references to scalars that we can.
@@ -881,7 +881,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
881881
ICFLoopSafetyInfo *SafetyInfo,
882882
SinkAndHoistLICMFlags &Flags,
883883
OptimizationRemarkEmitter *ORE, bool LoopNestMode,
884-
bool AllowSpeculation) {
884+
bool AllowSpeculation, bool HasCoroSuspendInst) {
885885
// Verify inputs.
886886
assert(N != nullptr && AA != nullptr && LI != nullptr && DT != nullptr &&
887887
CurLoop != nullptr && SafetyInfo != nullptr &&
@@ -914,7 +914,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
914914
// TODO: It may be safe to hoist if we are hoisting to a conditional block
915915
// and we have accurately duplicated the control flow from the loop header
916916
// to that block.
917-
if (CurLoop->hasLoopInvariantOperands(&I) &&
917+
if (CurLoop->hasLoopInvariantOperands(&I, HasCoroSuspendInst) &&
918918
canSinkOrHoistInst(I, AA, DT, CurLoop, MSSAU, true, Flags, ORE) &&
919919
isSafeToExecuteUnconditionally(
920920
I, DT, TLI, CurLoop, SafetyInfo, ORE,
@@ -964,7 +964,7 @@ bool llvm::hoistRegion(DomTreeNode *N, AAResults *AA, LoopInfo *LI,
964964
SafetyInfo->doesNotWriteMemoryBefore(I, CurLoop);
965965
};
966966
if ((IsInvariantStart(I) || isGuard(&I)) &&
967-
CurLoop->hasLoopInvariantOperands(&I) &&
967+
CurLoop->hasLoopInvariantOperands(&I, HasCoroSuspendInst) &&
968968
MustExecuteWithoutWritesBefore(I)) {
969969
hoist(I, DT, CurLoop, CFH.getOrCreateHoistedBlock(BB), SafetyInfo,
970970
MSSAU, SE, ORE);

0 commit comments

Comments
 (0)