[llvm] ce879a0 - [Coroutines] Do not add allocas for retcon coroutines

Sebastian Neubauer via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 14 01:47:03 PST 2022


Author: Sebastian Neubauer
Date: 2022-11-14T10:46:46+01:00
New Revision: ce879a03c9798aac0c3f246a250241087fb49e5c

URL: https://github.com/llvm/llvm-project/commit/ce879a03c9798aac0c3f246a250241087fb49e5c
DIFF: https://github.com/llvm/llvm-project/commit/ce879a03c9798aac0c3f246a250241087fb49e5c.diff

LOG: [Coroutines] Do not add allocas for retcon coroutines

Same as for async-style lowering, if there are no resume points in a
function, the coroutine frame pointer will be replaced by an undef,
making all accesses to the frame undefinde behavior.

Fix this by not adding allocas to the coroutine frame if there are no
resume points.

Differential Revision: https://reviews.llvm.org/D137866

Added: 
    

Modified: 
    llvm/lib/Transforms/Coroutines/CoroFrame.cpp
    llvm/test/Transforms/Coroutines/coro-retcon.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
index 7fc56e4f94694..b8f5c9db8f35b 100644
--- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
@@ -2732,7 +2732,7 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
       Shape.ABI != coro::ABI::RetconOnce)
     sinkLifetimeStartMarkers(F, Shape, Checker);
 
-  if (Shape.ABI != coro::ABI::Async || !Shape.CoroSuspends.empty())
+  if (Shape.ABI == coro::ABI::Switch || !Shape.CoroSuspends.empty())
     collectFrameAllocas(F, Shape, Checker, FrameData.Allocas);
   LLVM_DEBUG(dumpAllocas(FrameData.Allocas));
 

diff  --git a/llvm/test/Transforms/Coroutines/coro-retcon.ll b/llvm/test/Transforms/Coroutines/coro-retcon.ll
index c092709e0fef4..28b59e22b034e 100644
--- a/llvm/test/Transforms/Coroutines/coro-retcon.ll
+++ b/llvm/test/Transforms/Coroutines/coro-retcon.ll
@@ -130,12 +130,17 @@ cleanup:
 define i8* @nosuspend(i8* %buffer, i32 %n) {
 ; CHECK-LABEL: @nosuspend(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    unreachable
+; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    store i32 [[N:%.*]], i32* [[A]], align 4
+; CHECK-NEXT:    call void @use_var_ptr(i32* nonnull [[A]])
+; CHECK-NEXT:    [[AL:%.*]] = load i32, i32* [[A]], align 4
+; CHECK-NEXT:    call void @use_var(i32 [[AL]])
+; CHECK-NEXT:    ret i8* null
 ;
 ; CORO-LABEL: @nosuspend(
 ; CORO-NEXT:  entry:
 ; CORO-NEXT:    [[FRAMEPTR:%.*]] = bitcast i8* undef to %nosuspend.Frame*
-; CORO-NEXT:    [[A:%.*]] = getelementptr inbounds [[NOSUSPEND_FRAME:%.*]], %nosuspend.Frame* [[FRAMEPTR]], i32 0, i32 0
+; CORO-NEXT:    [[A:%.*]] = alloca i32, align 4
 ; CORO-NEXT:    store i32 [[N:%.*]], i32* [[A]], align 4
 ; CORO-NEXT:    call void @use_var_ptr(i32* [[A]])
 ; CORO-NEXT:    [[AL:%.*]] = load i32, i32* [[A]], align 4


        


More information about the llvm-commits mailing list