[clang] [clang][CodeGen] Promote point of GRO(CWG2563) (PR #151067)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 29 19:11:06 PDT 2025


================
@@ -642,6 +650,37 @@ struct CallCoroDelete final : public EHScopeStack::Cleanup {
     CGF.Builder.SetInsertPoint(AfterFreeBB);
   }
   explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
+
+private:
+  // [dcl.fct.def.coroutine]: The coroutine state is destroyed when control
+  // flows off the end of the coroutine or the destroy member function of a
+  // coroutine handle that refers to the coroutine is invoked.
+  void EmitPreCleanupBB(CodeGenFunction &CGF) {
+    auto &Builder = CGF.Builder;
+    auto &Data = *CGF.CurCoro.Data;
+    auto *BodyDone = Data.BodyDone;
+    BasicBlock *SaveInsertBlock = Builder.GetInsertBlock();
+    BasicBlock *PreConvBB = BodyDone->getParent();
+    BasicBlock *AfterConvBB =
+        cast<llvm::BranchInst>(PreConvBB->getTerminator())->getSuccessor(1);
+    BasicBlock *CleanupBB = AfterConvBB->getSingleSuccessor();
+    BasicBlock *PreCleanupBB = CleanupBB->splitBasicBlock(
+        CleanupBB->getFirstNonPHIIt(), "pre.coro.cleanup", true);
+    Builder.SetInsertPoint(PreCleanupBB);
+    PreCleanupBB->getTerminator()->eraseFromParent();
+
+    auto *NeedCleanup = Builder.CreatePHI(
+        Builder.getInt1Ty(), llvm::pred_size(PreCleanupBB), "is.completed");
+    for (auto *Pred : llvm::predecessors(PreCleanupBB)) {
----------------
ChuanqiXu9 wrote:

I didn't get this. The size of pred of PreCleanupBB shouldn't always be 1. And what's the intention of `EmitPreCleanupBB`. I didn't understand it.

https://github.com/llvm/llvm-project/pull/151067


More information about the cfe-commits mailing list