[llvm] [Coroutines] Respect noinline attributes when eliding heap allocation (PR #115384)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 14:43:22 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-coroutines

Author: Yuxuan Chen (yuxuanchen1997)

<details>
<summary>Changes</summary>

Because the latest coroutine heap elision no longer depend on inlining. We can occasionally elide coroutines that are marked `__attribute__((noinline))`. Considering that we are force inlining the ramp function as of https://github.com/llvm/llvm-project/pull/114004, this is kind of important. 

---
Full diff: https://github.com/llvm/llvm-project/pull/115384.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Coroutines/CoroSplit.cpp (+3-3) 
- (added) llvm/test/Transforms/Coroutines/coro-split-noinline.ll (+62) 


``````````diff
diff --git a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
index efd622123bccde..25a962ddf1b0da 100644
--- a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp
@@ -2088,9 +2088,9 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,
 
   bool isNoSuspendCoroutine = Shape.CoroSuspends.empty();
 
-  bool shouldCreateNoAllocVariant = !isNoSuspendCoroutine &&
-                                    Shape.ABI == coro::ABI::Switch &&
-                                    hasSafeElideCaller(F);
+  bool shouldCreateNoAllocVariant =
+      !isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch &&
+      hasSafeElideCaller(F) && !F.hasFnAttribute(llvm::Attribute::NoInline);
 
   // If there are no suspend points, no split required, just remove
   // the allocation and deallocation blocks, they are not needed.
diff --git a/llvm/test/Transforms/Coroutines/coro-split-noinline.ll b/llvm/test/Transforms/Coroutines/coro-split-noinline.ll
new file mode 100644
index 00000000000000..c53771570a0795
--- /dev/null
+++ b/llvm/test/Transforms/Coroutines/coro-split-noinline.ll
@@ -0,0 +1,62 @@
+; Tests that coro-split does not generate noinline variant for noinline functions
+; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
+
+; CHECK-LABEL: @cannotinline()
+define ptr @cannotinline() presplitcoroutine noinline {
+entry:
+  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
+  %need.alloc = call i1 @llvm.coro.alloc(token %id)
+  br i1 %need.alloc, label %dyn.alloc, label %begin
+
+dyn.alloc:  
+  %size = call i32 @llvm.coro.size.i32()
+  %alloc = call ptr @malloc(i32 %size)
+  br label %begin
+
+begin:
+  %phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
+  %hdl = call ptr @llvm.coro.begin(token %id, ptr %phi)
+  call void @print(i32 0)
+  %0 = call i8 @llvm.coro.suspend(token none, i1 false)
+  switch i8 %0, label %suspend [i8 0, label %resume 
+                                i8 1, label %cleanup]
+resume:
+  call void @print(i32 1)
+  br label %cleanup
+
+cleanup:
+  %mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
+  call void @free(ptr %mem)
+  br label %suspend
+suspend:
+  call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)  
+  ret ptr %hdl
+}
+
+; CHECK-NOT-LABEL: @cannotinline.noalloc()
+
+
+; Make a safe_elide call to cannotinline
+define void @caller() presplitcoroutine {
+entry:
+  %ptr = call ptr @cannotinline() #1
+  ret void
+}
+
+
+declare ptr @llvm.coro.free(token, ptr)
+declare i32 @llvm.coro.size.i32()
+declare i8  @llvm.coro.suspend(token, i1)
+declare void @llvm.coro.resume(ptr)
+declare void @llvm.coro.destroy(ptr)
+
+declare token @llvm.coro.id(i32, ptr, ptr, ptr)
+declare i1 @llvm.coro.alloc(token)
+declare ptr @llvm.coro.begin(token, ptr)
+declare i1 @llvm.coro.end(ptr, i1, token) 
+
+declare noalias ptr @malloc(i32) allockind("alloc,uninitialized") "alloc-family"="malloc"
+declare void @print(i32)
+declare void @free(ptr) willreturn allockind("free") "alloc-family"="malloc"
+
+attributes #1 = { coro_elide_safe }

``````````

</details>


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


More information about the llvm-commits mailing list