Skip to content

Commit ce879a0

Browse files
committed
[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
1 parent 0387679 commit ce879a0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,7 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
27322732
Shape.ABI != coro::ABI::RetconOnce)
27332733
sinkLifetimeStartMarkers(F, Shape, Checker);
27342734

2735-
if (Shape.ABI != coro::ABI::Async || !Shape.CoroSuspends.empty())
2735+
if (Shape.ABI == coro::ABI::Switch || !Shape.CoroSuspends.empty())
27362736
collectFrameAllocas(F, Shape, Checker, FrameData.Allocas);
27372737
LLVM_DEBUG(dumpAllocas(FrameData.Allocas));
27382738

llvm/test/Transforms/Coroutines/coro-retcon.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,17 @@ cleanup:
130130
define i8* @nosuspend(i8* %buffer, i32 %n) {
131131
; CHECK-LABEL: @nosuspend(
132132
; CHECK-NEXT: entry:
133-
; CHECK-NEXT: unreachable
133+
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
134+
; CHECK-NEXT: store i32 [[N:%.*]], i32* [[A]], align 4
135+
; CHECK-NEXT: call void @use_var_ptr(i32* nonnull [[A]])
136+
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[A]], align 4
137+
; CHECK-NEXT: call void @use_var(i32 [[AL]])
138+
; CHECK-NEXT: ret i8* null
134139
;
135140
; CORO-LABEL: @nosuspend(
136141
; CORO-NEXT: entry:
137142
; CORO-NEXT: [[FRAMEPTR:%.*]] = bitcast i8* undef to %nosuspend.Frame*
138-
; CORO-NEXT: [[A:%.*]] = getelementptr inbounds [[NOSUSPEND_FRAME:%.*]], %nosuspend.Frame* [[FRAMEPTR]], i32 0, i32 0
143+
; CORO-NEXT: [[A:%.*]] = alloca i32, align 4
139144
; CORO-NEXT: store i32 [[N:%.*]], i32* [[A]], align 4
140145
; CORO-NEXT: call void @use_var_ptr(i32* [[A]])
141146
; CORO-NEXT: [[AL:%.*]] = load i32, i32* [[A]], align 4

0 commit comments

Comments
 (0)