[llvm] Reapply "[coro][CoroSplit] Use `llvm.lifetime.end` to compute putting objects on the frame vs the stack (#90265) (PR #91372)

Alan Zhao via llvm-commits llvm-commits at lists.llvm.org
Mon May 20 16:38:37 PDT 2024


https://github.com/alanzhao1 updated https://github.com/llvm/llvm-project/pull/91372

>From e4c029f1f906960416e285231fc3f023b4f1e55c Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 11:01:30 -0700
Subject: [PATCH 01/14] Reapply "[coro][CoroSplit] Use `llvm.lifetime.end` to
 compute putting objects on the frame vs the stack (#90265)"

This reverts commit 924384161ffceda08099536dd07a953299a69b53.

This reland addresses the performance regressions seen in #90265 by
retaining the original definition of
`isPotentiallyReachableFromMany(...)` instead of reimplementing it with
`isManyPotentiallyReachableFromMany(...)`.

Fixes #86580
---
 llvm/include/llvm/Analysis/CFG.h              |  12 ++
 llvm/lib/Analysis/CFG.cpp                     |  85 ++++++++++-
 llvm/lib/Transforms/Coroutines/CoroFrame.cpp  |  60 +++++---
 .../Coroutines/coro-lifetime-end.ll           | 142 ++++++++++++++++++
 4 files changed, 281 insertions(+), 18 deletions(-)
 create mode 100644 llvm/test/Transforms/Coroutines/coro-lifetime-end.ll

diff --git a/llvm/include/llvm/Analysis/CFG.h b/llvm/include/llvm/Analysis/CFG.h
index 86b01c13274fe..23bc10a4a9d1b 100644
--- a/llvm/include/llvm/Analysis/CFG.h
+++ b/llvm/include/llvm/Analysis/CFG.h
@@ -96,6 +96,18 @@ bool isPotentiallyReachableFromMany(
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
     const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
 
+/// Determine whether there is a potentially a path from at least one block in
+/// 'Worklist' to at least one block in 'StopSet' within a single function
+/// without passing through any of the blocks in 'ExclusionSet'. Returns false
+/// only if we can prove that once any block in 'Worklist' has been reached then
+/// no blocks in 'StopSet' can be executed without passing through any blocks in
+/// 'ExclusionSet'. Conservatively returns true.
+bool isManyPotentiallyReachableFromMany(
+    SmallVectorImpl<BasicBlock *> &Worklist,
+    const SmallPtrSetImpl<const BasicBlock *> &StopSet,
+    const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
+    const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr);
+
 /// Return true if the control flow in \p RPOTraversal is irreducible.
 ///
 /// This is a generic implementation to detect CFG irreducibility based on loop
diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 8528aa9f77e02..57fb529fada46 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -158,7 +158,7 @@ bool llvm::isPotentiallyReachableFromMany(
   const Loop *StopLoop = LI ? getOutermostLoop(LI, StopBB) : nullptr;
 
   unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock*, 32> Visited;
+  SmallPtrSet<const BasicBlock *, 32> Visited;
   do {
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)
@@ -204,6 +204,89 @@ bool llvm::isPotentiallyReachableFromMany(
   return false;
 }
 
+bool llvm::isManyPotentiallyReachableFromMany(
+    SmallVectorImpl<BasicBlock *> &Worklist,
+    const SmallPtrSetImpl<const BasicBlock *> &StopSet,
+    const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
+    const LoopInfo *LI) {
+  // When a stop block is unreachable, it's dominated from everywhere,
+  // regardless of whether there's a path between the two blocks.
+  llvm::DenseMap<const BasicBlock *, bool> StopBBReachable;
+  for (auto *BB : StopSet)
+    StopBBReachable[BB] = DT && DT->isReachableFromEntry(BB);
+
+  // We can't skip directly from a block that dominates the stop block if the
+  // exclusion block is potentially in between.
+  if (ExclusionSet && !ExclusionSet->empty())
+    DT = nullptr;
+
+  // Normally any block in a loop is reachable from any other block in a loop,
+  // however excluded blocks might partition the body of a loop to make that
+  // untrue.
+  SmallPtrSet<const Loop *, 8> LoopsWithHoles;
+  if (LI && ExclusionSet) {
+    for (auto *BB : *ExclusionSet) {
+      if (const Loop *L = getOutermostLoop(LI, BB))
+        LoopsWithHoles.insert(L);
+    }
+  }
+
+  llvm::DenseMap<const BasicBlock *, const Loop *> StopLoops;
+  for (auto *StopBB : StopSet)
+    StopLoops[StopBB] = LI ? getOutermostLoop(LI, StopBB) : nullptr;
+
+  unsigned Limit = DefaultMaxBBsToExplore;
+  SmallPtrSet<const BasicBlock*, 32> Visited;
+  do {
+    BasicBlock *BB = Worklist.pop_back_val();
+    if (!Visited.insert(BB).second)
+      continue;
+    if (StopSet.contains(BB))
+      return true;
+    if (ExclusionSet && ExclusionSet->count(BB))
+      continue;
+    if (DT && llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
+          return StopBBReachable[BB] && DT->dominates(BB, StopBB);
+        }))
+      return true;
+
+    const Loop *Outer = nullptr;
+    if (LI) {
+      Outer = getOutermostLoop(LI, BB);
+      // If we're in a loop with a hole, not all blocks in the loop are
+      // reachable from all other blocks. That implies we can't simply jump to
+      // the loop's exit blocks, as that exit might need to pass through an
+      // excluded block. Clear Outer so we process BB's successors.
+      if (LoopsWithHoles.count(Outer))
+        Outer = nullptr;
+      if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
+            const Loop *StopLoop = StopLoops[StopBB];
+            return StopLoop && StopLoop == Outer;
+          }))
+        return true;
+    }
+
+    if (!--Limit) {
+      // We haven't been able to prove it one way or the other. Conservatively
+      // answer true -- that there is potentially a path.
+      return true;
+    }
+
+    if (Outer) {
+      // All blocks in a single loop are reachable from all other blocks. From
+      // any of these blocks, we can skip directly to the exits of the loop,
+      // ignoring any other blocks inside the loop body.
+      Outer->getExitBlocks(Worklist);
+    } else {
+      Worklist.append(succ_begin(BB), succ_end(BB));
+    }
+  } while (!Worklist.empty());
+
+  // We have exhausted all possible paths and are certain that 'To' can not be
+  // reached from 'From'.
+  return false;
+}
+
 bool llvm::isPotentiallyReachable(
     const BasicBlock *A, const BasicBlock *B,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
index 08a4522e3fac6..dd9e77a855ef4 100644
--- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
@@ -19,6 +19,7 @@
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Analysis/CFG.h"
 #include "llvm/Analysis/PtrUseVisitor.h"
 #include "llvm/Analysis/StackLifetime.h"
 #include "llvm/Config/llvm-config.h"
@@ -1440,17 +1441,22 @@ namespace {
 struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
   using Base = PtrUseVisitor<AllocaUseVisitor>;
   AllocaUseVisitor(const DataLayout &DL, const DominatorTree &DT,
-                   const CoroBeginInst &CB, const SuspendCrossingInfo &Checker,
+                   const coro::Shape &CoroShape,
+                   const SuspendCrossingInfo &Checker,
                    bool ShouldUseLifetimeStartInfo)
-      : PtrUseVisitor(DL), DT(DT), CoroBegin(CB), Checker(Checker),
-        ShouldUseLifetimeStartInfo(ShouldUseLifetimeStartInfo) {}
+      : PtrUseVisitor(DL), DT(DT), CoroShape(CoroShape), Checker(Checker),
+        ShouldUseLifetimeStartInfo(ShouldUseLifetimeStartInfo) {
+    for (AnyCoroSuspendInst *SuspendInst : CoroShape.CoroSuspends)
+      CoroSuspendBBs.insert(SuspendInst->getParent());
+  }
 
   void visit(Instruction &I) {
     Users.insert(&I);
     Base::visit(I);
     // If the pointer is escaped prior to CoroBegin, we have to assume it would
     // be written into before CoroBegin as well.
-    if (PI.isEscaped() && !DT.dominates(&CoroBegin, PI.getEscapingInst())) {
+    if (PI.isEscaped() &&
+        !DT.dominates(CoroShape.CoroBegin, PI.getEscapingInst())) {
       MayWriteBeforeCoroBegin = true;
     }
   }
@@ -1553,10 +1559,19 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
     // When we found the lifetime markers refers to a
     // subrange of the original alloca, ignore the lifetime
     // markers to avoid misleading the analysis.
-    if (II.getIntrinsicID() != Intrinsic::lifetime_start || !IsOffsetKnown ||
-        !Offset.isZero())
+    if (!IsOffsetKnown || !Offset.isZero())
+      return Base::visitIntrinsicInst(II);
+    switch (II.getIntrinsicID()) {
+    default:
       return Base::visitIntrinsicInst(II);
-    LifetimeStarts.insert(&II);
+    case Intrinsic::lifetime_start:
+      LifetimeStarts.insert(&II);
+      LifetimeStartBBs.push_back(II.getParent());
+      break;
+    case Intrinsic::lifetime_end:
+      LifetimeEndBBs.insert(II.getParent());
+      break;
+    }
   }
 
   void visitCallBase(CallBase &CB) {
@@ -1586,7 +1601,7 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
 
 private:
   const DominatorTree &DT;
-  const CoroBeginInst &CoroBegin;
+  const coro::Shape &CoroShape;
   const SuspendCrossingInfo &Checker;
   // All alias to the original AllocaInst, created before CoroBegin and used
   // after CoroBegin. Each entry contains the instruction and the offset in the
@@ -1594,6 +1609,9 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
   DenseMap<Instruction *, std::optional<APInt>> AliasOffetMap{};
   SmallPtrSet<Instruction *, 4> Users{};
   SmallPtrSet<IntrinsicInst *, 2> LifetimeStarts{};
+  SmallVector<BasicBlock *> LifetimeStartBBs{};
+  SmallPtrSet<BasicBlock *, 2> LifetimeEndBBs{};
+  SmallPtrSet<const BasicBlock *, 2> CoroSuspendBBs{};
   bool MayWriteBeforeCoroBegin{false};
   bool ShouldUseLifetimeStartInfo{true};
 
@@ -1605,10 +1623,19 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
     // every basic block that uses the pointer to see if they cross suspension
     // points. The uses cover both direct uses as well as indirect uses.
     if (ShouldUseLifetimeStartInfo && !LifetimeStarts.empty()) {
-      for (auto *I : Users)
-        for (auto *S : LifetimeStarts)
-          if (Checker.isDefinitionAcrossSuspend(*S, I))
-            return true;
+      // If there is no explicit lifetime.end, then assume the address can
+      // cross suspension points.
+      if (LifetimeEndBBs.empty())
+        return true;
+
+      // If there is a path from a lifetime.start to a suspend without a
+      // corresponding lifetime.end, then the alloca's lifetime persists
+      // beyond that suspension point and the alloca must go on the frame.
+      llvm::SmallVector<BasicBlock *> Worklist(LifetimeStartBBs);
+      if (isManyPotentiallyReachableFromMany(Worklist, CoroSuspendBBs,
+                                             &LifetimeEndBBs, &DT))
+        return true;
+
       // Addresses are guaranteed to be identical after every lifetime.start so
       // we cannot use the local stack if the address escaped and there is a
       // suspend point between lifetime markers. This should also cover the
@@ -1646,13 +1673,13 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
   }
 
   void handleMayWrite(const Instruction &I) {
-    if (!DT.dominates(&CoroBegin, &I))
+    if (!DT.dominates(CoroShape.CoroBegin, &I))
       MayWriteBeforeCoroBegin = true;
   }
 
   bool usedAfterCoroBegin(Instruction &I) {
     for (auto &U : I.uses())
-      if (DT.dominates(&CoroBegin, U))
+      if (DT.dominates(CoroShape.CoroBegin, U))
         return true;
     return false;
   }
@@ -1661,7 +1688,7 @@ struct AllocaUseVisitor : PtrUseVisitor<AllocaUseVisitor> {
     // We track all aliases created prior to CoroBegin but used after.
     // These aliases may need to be recreated after CoroBegin if the alloca
     // need to live on the frame.
-    if (DT.dominates(&CoroBegin, &I) || !usedAfterCoroBegin(I))
+    if (DT.dominates(CoroShape.CoroBegin, &I) || !usedAfterCoroBegin(I))
       return;
 
     if (!IsOffsetKnown) {
@@ -2830,8 +2857,7 @@ static void collectFrameAlloca(AllocaInst *AI, coro::Shape &Shape,
   bool ShouldUseLifetimeStartInfo =
       (Shape.ABI != coro::ABI::Async && Shape.ABI != coro::ABI::Retcon &&
        Shape.ABI != coro::ABI::RetconOnce);
-  AllocaUseVisitor Visitor{AI->getModule()->getDataLayout(), DT,
-                           *Shape.CoroBegin, Checker,
+  AllocaUseVisitor Visitor{AI->getModule()->getDataLayout(), DT, Shape, Checker,
                            ShouldUseLifetimeStartInfo};
   Visitor.visitPtr(*AI);
   if (!Visitor.getShouldLiveOnFrame())
diff --git a/llvm/test/Transforms/Coroutines/coro-lifetime-end.ll b/llvm/test/Transforms/Coroutines/coro-lifetime-end.ll
new file mode 100644
index 0000000000000..330c61360e20a
--- /dev/null
+++ b/llvm/test/Transforms/Coroutines/coro-lifetime-end.ll
@@ -0,0 +1,142 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
+
+declare ptr @malloc(i64)
+
+%i8.array = type { [100 x i8] }
+declare void @consume.i8.array(ptr)
+
+ at testbool = external local_unnamed_addr global i8, align 1
+
+; testval does not contain an explicit lifetime end. We must assume that it may
+; live across suspension.
+define void @HasNoLifetimeEnd() presplitcoroutine {
+; CHECK-LABEL: define void @HasNoLifetimeEnd() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ID:%.*]] = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr @HasNoLifetimeEnd.resumers)
+; CHECK-NEXT:    [[ALLOC:%.*]] = call ptr @malloc(i64 16)
+; CHECK-NEXT:    [[VFRAME:%.*]] = call noalias nonnull ptr @llvm.coro.begin(token [[ID]], ptr [[ALLOC]])
+; CHECK-NEXT:    store ptr @HasNoLifetimeEnd.resume, ptr [[VFRAME]], align 8
+; CHECK-NEXT:    [[DESTROY_ADDR:%.*]] = getelementptr inbounds [[HASNOLIFETIMEEND_FRAME:%.*]], ptr [[VFRAME]], i32 0, i32 1
+; CHECK-NEXT:    store ptr @HasNoLifetimeEnd.destroy, ptr [[DESTROY_ADDR]], align 8
+; CHECK-NEXT:    [[INDEX_ADDR1:%.*]] = getelementptr inbounds [[HASNOLIFETIMEEND_FRAME]], ptr [[VFRAME]], i32 0, i32 2
+; CHECK-NEXT:    call void @consume.i8.array(ptr [[INDEX_ADDR1]])
+; CHECK-NEXT:    [[INDEX_ADDR2:%.*]] = getelementptr inbounds [[HASNOLIFETIMEEND_FRAME]], ptr [[VFRAME]], i32 0, i32 3
+; CHECK-NEXT:    store i1 false, ptr [[INDEX_ADDR2]], align 1
+; CHECK-NEXT:    ret void
+;
+entry:
+  %testval = alloca %i8.array
+  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
+  %alloc = call ptr @malloc(i64 16) #3
+  %vFrame = call noalias nonnull ptr @llvm.coro.begin(token %id, ptr %alloc)
+
+  call void @llvm.lifetime.start.p0(i64 100, ptr %testval)
+  call void @consume.i8.array(ptr %testval)
+
+  %save = call token @llvm.coro.save(ptr null)
+  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
+  switch i8 %suspend, label %exit [
+    i8 0, label %await.ready
+    i8 1, label %exit
+  ]
+await.ready:
+  br label %exit
+exit:
+  call i1 @llvm.coro.end(ptr null, i1 false, token none)
+  ret void
+}
+
+define void @LifetimeEndAfterCoroEnd() presplitcoroutine {
+; CHECK-LABEL: define void @LifetimeEndAfterCoroEnd() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ID:%.*]] = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr @LifetimeEndAfterCoroEnd.resumers)
+; CHECK-NEXT:    [[ALLOC:%.*]] = call ptr @malloc(i64 16)
+; CHECK-NEXT:    [[VFRAME:%.*]] = call noalias nonnull ptr @llvm.coro.begin(token [[ID]], ptr [[ALLOC]])
+; CHECK-NEXT:    store ptr @LifetimeEndAfterCoroEnd.resume, ptr [[VFRAME]], align 8
+; CHECK-NEXT:    [[DESTROY_ADDR:%.*]] = getelementptr inbounds [[LIFETIMEENDAFTERCOROEND_FRAME:%.*]], ptr [[VFRAME]], i32 0, i32 1
+; CHECK-NEXT:    store ptr @LifetimeEndAfterCoroEnd.destroy, ptr [[DESTROY_ADDR]], align 8
+; CHECK-NEXT:    [[INDEX_ADDR1:%.*]] = getelementptr inbounds [[LIFETIMEENDAFTERCOROEND_FRAME]], ptr [[VFRAME]], i32 0, i32 2
+; CHECK-NEXT:    call void @consume.i8.array(ptr [[INDEX_ADDR1]])
+; CHECK-NEXT:    [[INDEX_ADDR2:%.*]] = getelementptr inbounds [[LIFETIMEENDAFTERCOROEND_FRAME]], ptr [[VFRAME]], i32 0, i32 3
+; CHECK-NEXT:    store i1 false, ptr [[INDEX_ADDR2]], align 1
+; CHECK-NEXT:    ret void
+;
+entry:
+  %testval = alloca %i8.array
+  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
+  %alloc = call ptr @malloc(i64 16) #3
+  %vFrame = call noalias nonnull ptr @llvm.coro.begin(token %id, ptr %alloc)
+
+  call void @llvm.lifetime.start.p0(i64 100, ptr %testval)
+  call void @consume.i8.array(ptr %testval)
+
+  %save = call token @llvm.coro.save(ptr null)
+  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
+  switch i8 %suspend, label %exit [
+    i8 0, label %await.ready
+    i8 1, label %exit
+  ]
+await.ready:
+  br label %exit
+exit:
+  call i1 @llvm.coro.end(ptr null, i1 false, token none)
+  call void @llvm.lifetime.end.p0(i64 100, ptr  %testval)
+  ret void
+}
+
+define void @BranchWithoutLifetimeEnd() presplitcoroutine {
+; CHECK-LABEL: define void @BranchWithoutLifetimeEnd() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[ID:%.*]] = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr @BranchWithoutLifetimeEnd.resumers)
+; CHECK-NEXT:    [[ALLOC:%.*]] = call ptr @malloc(i64 16)
+; CHECK-NEXT:    [[VFRAME:%.*]] = call noalias nonnull ptr @llvm.coro.begin(token [[ID]], ptr [[ALLOC]])
+; CHECK-NEXT:    store ptr @BranchWithoutLifetimeEnd.resume, ptr [[VFRAME]], align 8
+; CHECK-NEXT:    [[DESTROY_ADDR:%.*]] = getelementptr inbounds [[BRANCHWITHOUTLIFETIMEEND_FRAME:%.*]], ptr [[VFRAME]], i32 0, i32 1
+; CHECK-NEXT:    store ptr @BranchWithoutLifetimeEnd.destroy, ptr [[DESTROY_ADDR]], align 8
+; CHECK-NEXT:    [[TESTVAL:%.*]] = getelementptr inbounds [[BRANCHWITHOUTLIFETIMEEND_FRAME]], ptr [[VFRAME]], i32 0, i32 2
+; CHECK-NEXT:    call void @consume.i8.array(ptr [[TESTVAL]])
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr @testbool, align 1
+; CHECK-NEXT:    [[INDEX_ADDR1:%.*]] = getelementptr inbounds [[BRANCHWITHOUTLIFETIMEEND_FRAME]], ptr [[VFRAME]], i32 0, i32 3
+; CHECK-NEXT:    store i1 false, ptr [[INDEX_ADDR1]], align 1
+; CHECK-NEXT:    ret void
+;
+entry:
+  %testval = alloca %i8.array
+  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
+  %alloc = call ptr @malloc(i64 16) #3
+  %vFrame = call noalias nonnull ptr @llvm.coro.begin(token %id, ptr %alloc)
+
+  call void @llvm.lifetime.start.p0(i64 100, ptr %testval)
+  call void @consume.i8.array(ptr %testval)
+
+  %0 = load i8, ptr @testbool, align 1
+  %tobool = trunc nuw i8 %0 to i1
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:
+  call void @llvm.lifetime.end.p0(i64 100, ptr  %testval)
+  br label %if.end
+
+if.end:
+  %save = call token @llvm.coro.save(ptr null)
+  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
+  switch i8 %suspend, label %exit [
+    i8 0, label %await.ready
+    i8 1, label %exit
+  ]
+await.ready:
+  br label %exit
+exit:
+  call i1 @llvm.coro.end(ptr null, i1 false, token none)
+  ret void
+}
+
+
+declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr)
+declare ptr @llvm.coro.begin(token, ptr writeonly) #3
+declare ptr @llvm.coro.frame() #5
+declare i8 @llvm.coro.suspend(token, i1) #3
+declare i1 @llvm.coro.end(ptr, i1, token) #3
+declare void @llvm.lifetime.start.p0(i64, ptr nocapture) #4
+declare void @llvm.lifetime.end.p0(i64, ptr nocapture) #4

>From 703081b9b7d8f92d281c616a830a3c2a6c927d42 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 11:30:50 -0700
Subject: [PATCH 02/14] fix clang format issue

---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 57fb529fada46..6c62e14ec9c85 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -236,7 +236,7 @@ bool llvm::isManyPotentiallyReachableFromMany(
     StopLoops[StopBB] = LI ? getOutermostLoop(LI, StopBB) : nullptr;
 
   unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock*, 32> Visited;
+  SmallPtrSet<const BasicBlock *, 32> Visited;
   do {
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)

>From 0bfa097c578cf731732873aaa52ce9cfaaa0bedd Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 15:04:49 -0700
Subject: [PATCH 03/14] Use SmallPtrSet instead of DenseMap

---
 llvm/lib/Analysis/CFG.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 6c62e14ec9c85..52709bcf8125d 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -211,9 +211,11 @@ bool llvm::isManyPotentiallyReachableFromMany(
     const LoopInfo *LI) {
   // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
-  llvm::DenseMap<const BasicBlock *, bool> StopBBReachable;
-  for (auto *BB : StopSet)
-    StopBBReachable[BB] = DT && DT->isReachableFromEntry(BB);
+  SmallPtrSet<const BasicBlock *, 32> StopBBReachable;
+  for (auto *BB : StopSet) {
+    if (DT && DT->isReachableFromEntry(BB))
+      StopBBReachable.insert(BB);
+  }
 
   // We can't skip directly from a block that dominates the stop block if the
   // exclusion block is potentially in between.
@@ -246,7 +248,7 @@ bool llvm::isManyPotentiallyReachableFromMany(
     if (ExclusionSet && ExclusionSet->count(BB))
       continue;
     if (DT && llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
-          return StopBBReachable[BB] && DT->dominates(BB, StopBB);
+          return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
         }))
       return true;
 

>From 025f2369684b928f682a1179161d58fbaa209c17 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 20:02:01 -0700
Subject: [PATCH 04/14] remove extra change

---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 52709bcf8125d..17def19c86447 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -158,7 +158,7 @@ bool llvm::isPotentiallyReachableFromMany(
   const Loop *StopLoop = LI ? getOutermostLoop(LI, StopBB) : nullptr;
 
   unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock *, 32> Visited;
+  SmallPtrSet<const BasicBlock*, 32> Visited;
   do {
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)

>From 7e9695fbb8d2316a0a83b7a10457743074948c11 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 20:20:57 -0700
Subject: [PATCH 05/14] use SmallPtrSet for loops

Also make curly braces more consistent
---
 llvm/lib/Analysis/CFG.cpp | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 17def19c86447..f943220f0ff51 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -212,10 +212,9 @@ bool llvm::isManyPotentiallyReachableFromMany(
   // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
   SmallPtrSet<const BasicBlock *, 32> StopBBReachable;
-  for (auto *BB : StopSet) {
+  for (auto *BB : StopSet)
     if (DT && DT->isReachableFromEntry(BB))
       StopBBReachable.insert(BB);
-  }
 
   // We can't skip directly from a block that dominates the stop block if the
   // exclusion block is potentially in between.
@@ -226,16 +225,16 @@ bool llvm::isManyPotentiallyReachableFromMany(
   // however excluded blocks might partition the body of a loop to make that
   // untrue.
   SmallPtrSet<const Loop *, 8> LoopsWithHoles;
-  if (LI && ExclusionSet) {
-    for (auto *BB : *ExclusionSet) {
+  if (LI && ExclusionSet)
+    for (auto *BB : *ExclusionSet)
       if (const Loop *L = getOutermostLoop(LI, BB))
         LoopsWithHoles.insert(L);
-    }
-  }
 
-  llvm::DenseMap<const BasicBlock *, const Loop *> StopLoops;
-  for (auto *StopBB : StopSet)
-    StopLoops[StopBB] = LI ? getOutermostLoop(LI, StopBB) : nullptr;
+  SmallPtrSet<const Loop *, 8> StopLoops;
+  if (LI)
+    for (auto *StopBB : StopSet)
+      if (const Loop *L = getOutermostLoop(LI, StopBB))
+        StopLoops.insert(L);
 
   unsigned Limit = DefaultMaxBBsToExplore;
   SmallPtrSet<const BasicBlock *, 32> Visited;
@@ -261,10 +260,7 @@ bool llvm::isManyPotentiallyReachableFromMany(
       // excluded block. Clear Outer so we process BB's successors.
       if (LoopsWithHoles.count(Outer))
         Outer = nullptr;
-      if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
-            const Loop *StopLoop = StopLoops[StopBB];
-            return StopLoop && StopLoop == Outer;
-          }))
+      if (StopLoops.contains(Outer))
         return true;
     }
 

>From edbcf9fd0020f4a63222baa441179d90420d8c0a Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 23:02:42 -0700
Subject: [PATCH 06/14] deduplicate code by using templates

---
 llvm/lib/Analysis/CFG.cpp | 167 ++++++++++++++++++--------------------
 1 file changed, 78 insertions(+), 89 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index f943220f0ff51..9f1ae9ce1ce3a 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -130,14 +130,35 @@ static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
   return L ? L->getOutermostLoop() : nullptr;
 }
 
-bool llvm::isPotentiallyReachableFromMany(
-    SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
-    const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
-    const LoopInfo *LI) {
-  // When the stop block is unreachable, it's dominated from everywhere,
+template <class T, bool IsMany>
+static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
+                            const T *StopBBOrSet,
+                            const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
+                            const DominatorTree *DT, const LoopInfo *LI) {
+  const BasicBlock *StopBB;
+  const SmallPtrSetImpl<const BasicBlock *> *StopSet;
+
+  // SmallPtrSetImpl is incompatible with LLVM's casting functions.
+  if constexpr (IsMany)
+    StopSet =
+        static_cast<const SmallPtrSetImpl<const BasicBlock *> *>(StopBBOrSet);
+  else
+    StopBB = static_cast<const BasicBlock *>(StopBBOrSet);
+
+  // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
-  if (DT && !DT->isReachableFromEntry(StopBB))
-    DT = nullptr;
+  SmallPtrSet<const BasicBlock *, 32> StopBBReachable;
+  if (DT) {
+    if constexpr (IsMany) {
+      for (auto *BB : *StopSet) {
+        if (DT->isReachableFromEntry(BB))
+          StopBBReachable.insert(BB);
+      }
+    } else {
+      if (!DT->isReachableFromEntry(StopBB))
+        DT = nullptr;
+    }
+  }
 
   // We can't skip directly from a block that dominates the stop block if the
   // exclusion block is potentially in between.
@@ -155,20 +176,47 @@ bool llvm::isPotentiallyReachableFromMany(
     }
   }
 
-  const Loop *StopLoop = LI ? getOutermostLoop(LI, StopBB) : nullptr;
+  const Loop *StopLoop = nullptr;
+  SmallPtrSet<const Loop *, 8> StopLoops;
+
+  if constexpr (IsMany) {
+    if (LI) {
+      for (auto *StopSetBB : *StopSet) {
+        if (const Loop *L = getOutermostLoop(LI, StopSetBB))
+          StopLoops.insert(L);
+      }
+    }
+  } else {
+    if (LI)
+      StopLoop = getOutermostLoop(LI, StopBB);
+  }
 
   unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock*, 32> Visited;
+  SmallPtrSet<const BasicBlock *, 32> Visited;
   do {
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)
       continue;
-    if (BB == StopBB)
-      return true;
+    if constexpr (IsMany) {
+      if (StopSet->contains(BB))
+        return true;
+    } else {
+      if (BB == StopBB)
+        return true;
+    }
     if (ExclusionSet && ExclusionSet->count(BB))
       continue;
-    if (DT && DT->dominates(BB, StopBB))
-      return true;
+    if (DT) {
+      if constexpr (IsMany) {
+        if (llvm::any_of(*StopSet, [&](const BasicBlock *StopBB) {
+              return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
+            }))
+          return true;
+      } else {
+        if (DT->dominates(BB, StopBB))
+          return true;
+      }
+    }
 
     const Loop *Outer = nullptr;
     if (LI) {
@@ -179,8 +227,13 @@ bool llvm::isPotentiallyReachableFromMany(
       // excluded block. Clear Outer so we process BB's successors.
       if (LoopsWithHoles.count(Outer))
         Outer = nullptr;
-      if (StopLoop && Outer == StopLoop)
-        return true;
+      if constexpr (IsMany) {
+        if (StopLoops.contains(Outer))
+          return true;
+      } else {
+        if (StopLoop && Outer == StopLoop)
+          return true;
+      }
     }
 
     if (!--Limit) {
@@ -204,85 +257,21 @@ bool llvm::isPotentiallyReachableFromMany(
   return false;
 }
 
+bool llvm::isPotentiallyReachableFromMany(
+    SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
+    const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
+    const LoopInfo *LI) {
+  return isReachableImpl<BasicBlock, false>(Worklist, StopBB, ExclusionSet, DT,
+                                            LI);
+}
+
 bool llvm::isManyPotentiallyReachableFromMany(
     SmallVectorImpl<BasicBlock *> &Worklist,
     const SmallPtrSetImpl<const BasicBlock *> &StopSet,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
     const LoopInfo *LI) {
-  // When a stop block is unreachable, it's dominated from everywhere,
-  // regardless of whether there's a path between the two blocks.
-  SmallPtrSet<const BasicBlock *, 32> StopBBReachable;
-  for (auto *BB : StopSet)
-    if (DT && DT->isReachableFromEntry(BB))
-      StopBBReachable.insert(BB);
-
-  // We can't skip directly from a block that dominates the stop block if the
-  // exclusion block is potentially in between.
-  if (ExclusionSet && !ExclusionSet->empty())
-    DT = nullptr;
-
-  // Normally any block in a loop is reachable from any other block in a loop,
-  // however excluded blocks might partition the body of a loop to make that
-  // untrue.
-  SmallPtrSet<const Loop *, 8> LoopsWithHoles;
-  if (LI && ExclusionSet)
-    for (auto *BB : *ExclusionSet)
-      if (const Loop *L = getOutermostLoop(LI, BB))
-        LoopsWithHoles.insert(L);
-
-  SmallPtrSet<const Loop *, 8> StopLoops;
-  if (LI)
-    for (auto *StopBB : StopSet)
-      if (const Loop *L = getOutermostLoop(LI, StopBB))
-        StopLoops.insert(L);
-
-  unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock *, 32> Visited;
-  do {
-    BasicBlock *BB = Worklist.pop_back_val();
-    if (!Visited.insert(BB).second)
-      continue;
-    if (StopSet.contains(BB))
-      return true;
-    if (ExclusionSet && ExclusionSet->count(BB))
-      continue;
-    if (DT && llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
-          return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
-        }))
-      return true;
-
-    const Loop *Outer = nullptr;
-    if (LI) {
-      Outer = getOutermostLoop(LI, BB);
-      // If we're in a loop with a hole, not all blocks in the loop are
-      // reachable from all other blocks. That implies we can't simply jump to
-      // the loop's exit blocks, as that exit might need to pass through an
-      // excluded block. Clear Outer so we process BB's successors.
-      if (LoopsWithHoles.count(Outer))
-        Outer = nullptr;
-      if (StopLoops.contains(Outer))
-        return true;
-    }
-
-    if (!--Limit) {
-      // We haven't been able to prove it one way or the other. Conservatively
-      // answer true -- that there is potentially a path.
-      return true;
-    }
-
-    if (Outer) {
-      // All blocks in a single loop are reachable from all other blocks. From
-      // any of these blocks, we can skip directly to the exits of the loop,
-      // ignoring any other blocks inside the loop body.
-      Outer->getExitBlocks(Worklist);
-    } else {
-      Worklist.append(succ_begin(BB), succ_end(BB));
-    }
-  } while (!Worklist.empty());
-
-  // We have exhausted all possible paths and are certain that 'To' can not be
-  // reached from 'From'.
-  return false;
+  return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>, true>(
+      Worklist, &StopSet, ExclusionSet, DT, LI);
 }
 
 bool llvm::isPotentiallyReachable(

>From 6dcb05ceb74a73f22edd4483e3bfc3c79ff3866a Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 23:07:23 -0700
Subject: [PATCH 07/14] remove unnecessary change..again

---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 9f1ae9ce1ce3a..e65de04612810 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -192,7 +192,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
   }
 
   unsigned Limit = DefaultMaxBBsToExplore;
-  SmallPtrSet<const BasicBlock *, 32> Visited;
+  SmallPtrSet<const BasicBlock*, 32> Visited;
   do {
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)

>From ca09dd020e6ad994223fba1b30baf50e101c3aa2 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 23:12:51 -0700
Subject: [PATCH 08/14] reduce size of StopBBReachable to align with size of
 CoroSuspendBBs

---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index e65de04612810..8fa9c42d2b748 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -147,7 +147,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
 
   // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
-  SmallPtrSet<const BasicBlock *, 32> StopBBReachable;
+  SmallPtrSet<const BasicBlock *, 2> StopBBReachable;
   if (DT) {
     if constexpr (IsMany) {
       for (auto *BB : *StopSet) {

>From cbb5c8bf3f712bce6ac9f1fedf3c31f378b92a82 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 7 May 2024 23:31:55 -0700
Subject: [PATCH 09/14] ditto with StopLoops

---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 8fa9c42d2b748..f6866bfe22499 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -177,7 +177,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
   }
 
   const Loop *StopLoop = nullptr;
-  SmallPtrSet<const Loop *, 8> StopLoops;
+  SmallPtrSet<const Loop *, 2> StopLoops;
 
   if constexpr (IsMany) {
     if (LI) {

>From 3f47dd417adffa45cfbf45c3a9d72a9f58112a36 Mon Sep 17 00:00:00 2001
From: Alan Zhao <azhao101 at gmail.com>
Date: Wed, 8 May 2024 19:28:22 -0700
Subject: [PATCH 10/14] Improve template parameter names

Co-authored-by: Chuanqi Xu <yedeng.yd at linux.alibaba.com>
---
 llvm/lib/Analysis/CFG.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index f6866bfe22499..f838b0c6dce8d 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -130,7 +130,7 @@ static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
   return L ? L->getOutermostLoop() : nullptr;
 }
 
-template <class T, bool IsMany>
+template <class StopT, bool IsManyStop>
 static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
                             const T *StopBBOrSet,
                             const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,

>From 6a8909cb6d38bb92bedb324fdb9526441f8e7558 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Wed, 8 May 2024 20:11:00 -0700
Subject: [PATCH 11/14] fix build failure

---
 llvm/lib/Analysis/CFG.cpp | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index f838b0c6dce8d..8568547a520ee 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -132,14 +132,14 @@ static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
 
 template <class StopT, bool IsManyStop>
 static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
-                            const T *StopBBOrSet,
+                            const StopT *StopBBOrSet,
                             const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
                             const DominatorTree *DT, const LoopInfo *LI) {
   const BasicBlock *StopBB;
   const SmallPtrSetImpl<const BasicBlock *> *StopSet;
 
   // SmallPtrSetImpl is incompatible with LLVM's casting functions.
-  if constexpr (IsMany)
+  if constexpr (IsManyStop)
     StopSet =
         static_cast<const SmallPtrSetImpl<const BasicBlock *> *>(StopBBOrSet);
   else
@@ -149,7 +149,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
   // regardless of whether there's a path between the two blocks.
   SmallPtrSet<const BasicBlock *, 2> StopBBReachable;
   if (DT) {
-    if constexpr (IsMany) {
+    if constexpr (IsManyStop) {
       for (auto *BB : *StopSet) {
         if (DT->isReachableFromEntry(BB))
           StopBBReachable.insert(BB);
@@ -179,16 +179,15 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
   const Loop *StopLoop = nullptr;
   SmallPtrSet<const Loop *, 2> StopLoops;
 
-  if constexpr (IsMany) {
-    if (LI) {
+  if (LI) {
+    if constexpr (IsManyStop) {
       for (auto *StopSetBB : *StopSet) {
         if (const Loop *L = getOutermostLoop(LI, StopSetBB))
           StopLoops.insert(L);
       }
-    }
-  } else {
-    if (LI)
+    } else {
       StopLoop = getOutermostLoop(LI, StopBB);
+    }
   }
 
   unsigned Limit = DefaultMaxBBsToExplore;
@@ -197,7 +196,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)
       continue;
-    if constexpr (IsMany) {
+    if constexpr (IsManyStop) {
       if (StopSet->contains(BB))
         return true;
     } else {
@@ -207,7 +206,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
     if (ExclusionSet && ExclusionSet->count(BB))
       continue;
     if (DT) {
-      if constexpr (IsMany) {
+      if constexpr (IsManyStop) {
         if (llvm::any_of(*StopSet, [&](const BasicBlock *StopBB) {
               return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
             }))
@@ -227,7 +226,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
       // excluded block. Clear Outer so we process BB's successors.
       if (LoopsWithHoles.count(Outer))
         Outer = nullptr;
-      if constexpr (IsMany) {
+      if constexpr (IsManyStop) {
         if (StopLoops.contains(Outer))
           return true;
       } else {

>From a487d326299929c14e3be6540658bfa0d92f7b73 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Mon, 13 May 2024 13:19:58 -0700
Subject: [PATCH 12/14] create and use SingleEntrySet

---
 llvm/lib/Analysis/CFG.cpp | 93 ++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 54 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 8568547a520ee..e5fe79a1ecb75 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -130,33 +130,18 @@ static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
   return L ? L->getOutermostLoop() : nullptr;
 }
 
-template <class StopT, bool IsManyStop>
+template <class StopSetT, bool IsManyStop>
 static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
-                            const StopT *StopBBOrSet,
+                            const StopSetT &StopSet,
                             const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
                             const DominatorTree *DT, const LoopInfo *LI) {
-  const BasicBlock *StopBB;
-  const SmallPtrSetImpl<const BasicBlock *> *StopSet;
-
-  // SmallPtrSetImpl is incompatible with LLVM's casting functions.
-  if constexpr (IsManyStop)
-    StopSet =
-        static_cast<const SmallPtrSetImpl<const BasicBlock *> *>(StopBBOrSet);
-  else
-    StopBB = static_cast<const BasicBlock *>(StopBBOrSet);
-
   // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
   SmallPtrSet<const BasicBlock *, 2> StopBBReachable;
   if (DT) {
-    if constexpr (IsManyStop) {
-      for (auto *BB : *StopSet) {
-        if (DT->isReachableFromEntry(BB))
-          StopBBReachable.insert(BB);
-      }
-    } else {
-      if (!DT->isReachableFromEntry(StopBB))
-        DT = nullptr;
+    for (auto *BB : StopSet) {
+      if (DT->isReachableFromEntry(BB))
+        StopBBReachable.insert(BB);
     }
   }
 
@@ -176,17 +161,12 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
     }
   }
 
-  const Loop *StopLoop = nullptr;
   SmallPtrSet<const Loop *, 2> StopLoops;
 
   if (LI) {
-    if constexpr (IsManyStop) {
-      for (auto *StopSetBB : *StopSet) {
-        if (const Loop *L = getOutermostLoop(LI, StopSetBB))
-          StopLoops.insert(L);
-      }
-    } else {
-      StopLoop = getOutermostLoop(LI, StopBB);
+    for (auto *StopSetBB : StopSet) {
+      if (const Loop *L = getOutermostLoop(LI, StopSetBB))
+        StopLoops.insert(L);
     }
   }
 
@@ -196,25 +176,15 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
     BasicBlock *BB = Worklist.pop_back_val();
     if (!Visited.insert(BB).second)
       continue;
-    if constexpr (IsManyStop) {
-      if (StopSet->contains(BB))
-        return true;
-    } else {
-      if (BB == StopBB)
-        return true;
-    }
+    if (StopSet.contains(BB))
+      return true;
     if (ExclusionSet && ExclusionSet->count(BB))
       continue;
     if (DT) {
-      if constexpr (IsManyStop) {
-        if (llvm::any_of(*StopSet, [&](const BasicBlock *StopBB) {
-              return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
-            }))
-          return true;
-      } else {
-        if (DT->dominates(BB, StopBB))
-          return true;
-      }
+      if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
+            return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
+          }))
+        return true;
     }
 
     const Loop *Outer = nullptr;
@@ -226,13 +196,8 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
       // excluded block. Clear Outer so we process BB's successors.
       if (LoopsWithHoles.count(Outer))
         Outer = nullptr;
-      if constexpr (IsManyStop) {
-        if (StopLoops.contains(Outer))
-          return true;
-      } else {
-        if (StopLoop && Outer == StopLoop)
-          return true;
-      }
+      if (StopLoops.contains(Outer))
+        return true;
     }
 
     if (!--Limit) {
@@ -256,12 +221,32 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
   return false;
 }
 
+template <class T> class SingleEntrySet {
+public:
+  using iterator = T *;
+  using const_iterator = const T *;
+
+  SingleEntrySet(T Elem) : Elem(Elem) {}
+
+  bool contains(T Other) const { return Elem == Other; }
+
+  iterator begin() { return &Elem; }
+  iterator end() { return &Elem + 1; }
+
+  const_iterator begin() const { return &Elem; }
+  const_iterator end() const { return &Elem + 1; }
+
+private:
+  T Elem;
+};
+
 bool llvm::isPotentiallyReachableFromMany(
     SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
     const LoopInfo *LI) {
-  return isReachableImpl<BasicBlock, false>(Worklist, StopBB, ExclusionSet, DT,
-                                            LI);
+  return isReachableImpl<SingleEntrySet<const BasicBlock *>, false>(
+      Worklist, SingleEntrySet<const BasicBlock *>(StopBB), ExclusionSet, DT,
+      LI);
 }
 
 bool llvm::isManyPotentiallyReachableFromMany(
@@ -270,7 +255,7 @@ bool llvm::isManyPotentiallyReachableFromMany(
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
     const LoopInfo *LI) {
   return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>, true>(
-      Worklist, &StopSet, ExclusionSet, DT, LI);
+      Worklist, StopSet, ExclusionSet, DT, LI);
 }
 
 bool llvm::isPotentiallyReachable(

>From 34617b3da2adae79354254bd09c53e28ece54b0a Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Tue, 14 May 2024 23:02:15 -0700
Subject: [PATCH 13/14] remove template bool param and non const iterator
 methods

---
 llvm/lib/Analysis/CFG.cpp | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index e5fe79a1ecb75..64a79741ca81a 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -130,7 +130,7 @@ static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
   return L ? L->getOutermostLoop() : nullptr;
 }
 
-template <class StopSetT, bool IsManyStop>
+template <class StopSetT>
 static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
                             const StopSetT &StopSet,
                             const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
@@ -223,16 +223,12 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
 
 template <class T> class SingleEntrySet {
 public:
-  using iterator = T *;
   using const_iterator = const T *;
 
   SingleEntrySet(T Elem) : Elem(Elem) {}
 
   bool contains(T Other) const { return Elem == Other; }
 
-  iterator begin() { return &Elem; }
-  iterator end() { return &Elem + 1; }
-
   const_iterator begin() const { return &Elem; }
   const_iterator end() const { return &Elem + 1; }
 
@@ -244,7 +240,7 @@ bool llvm::isPotentiallyReachableFromMany(
     SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
     const LoopInfo *LI) {
-  return isReachableImpl<SingleEntrySet<const BasicBlock *>, false>(
+  return isReachableImpl<SingleEntrySet<const BasicBlock *>>(
       Worklist, SingleEntrySet<const BasicBlock *>(StopBB), ExclusionSet, DT,
       LI);
 }
@@ -254,7 +250,7 @@ bool llvm::isManyPotentiallyReachableFromMany(
     const SmallPtrSetImpl<const BasicBlock *> &StopSet,
     const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
     const LoopInfo *LI) {
-  return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>, true>(
+  return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>>(
       Worklist, StopSet, ExclusionSet, DT, LI);
 }
 

>From 46bd7d9645b365da1c6f298002332c66eaf7a0cb Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Mon, 20 May 2024 16:38:07 -0700
Subject: [PATCH 14/14] Drop StopBBReachable set

---
 llvm/lib/Analysis/CFG.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 64a79741ca81a..48850ee255d35 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -137,11 +137,12 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
                             const DominatorTree *DT, const LoopInfo *LI) {
   // When a stop block is unreachable, it's dominated from everywhere,
   // regardless of whether there's a path between the two blocks.
-  SmallPtrSet<const BasicBlock *, 2> StopBBReachable;
   if (DT) {
     for (auto *BB : StopSet) {
-      if (DT->isReachableFromEntry(BB))
-        StopBBReachable.insert(BB);
+      if (DT->isReachableFromEntry(BB)) {
+        DT = nullptr;
+        break;
+      }
     }
   }
 
@@ -182,7 +183,7 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
       continue;
     if (DT) {
       if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
-            return StopBBReachable.contains(BB) && DT->dominates(BB, StopBB);
+            return DT->dominates(BB, StopBB);
           }))
         return true;
     }



More information about the llvm-commits mailing list