[Coroutines] Do not add allocas for retcon coroutines
authorSebastian Neubauer <Sebastian.Neubauer@amd.com>
Mon, 14 Nov 2022 09:46:46 +0000 (10:46 +0100)
committerSebastian Neubauer <Sebastian.Neubauer@amd.com>
Mon, 14 Nov 2022 09:46:46 +0000 (10:46 +0100)
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

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

index 7fc56e4..b8f5c9d 100644 (file)
@@ -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));
 
index c092709..28b59e2 100644 (file)
@@ -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