Skip to content

Commit e4312b9

Browse files
junparsertstellar
authored andcommitted
[Coroutines] Fix PR45130
For now, when final suspend can be simplified by simplifySuspendPoint, handleFinalSuspend is executed as well to remove last case in switch instruction. This patch fixes it. Differential Revision: https://reviews.llvm.org/D76345 (cherry picked from commit 032251e)
1 parent 9cf9cf2 commit e4312b9

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,10 @@ static void simplifySuspendPoints(coro::Shape &Shape) {
11551155
if (N == 0)
11561156
return;
11571157
while (true) {
1158-
if (simplifySuspendPoint(cast<CoroSuspendInst>(S[I]), Shape.CoroBegin)) {
1158+
auto SI = cast<CoroSuspendInst>(S[I]);
1159+
// Leave final.suspend to handleFinalSuspend since it is undefined behavior
1160+
// to resume a coroutine suspended at the final suspend point.
1161+
if (!SI->isFinal() && simplifySuspendPoint(SI, Shape.CoroBegin)) {
11591162
if (--N == I)
11601163
break;
11611164
std::swap(S[I], S[N]);

llvm/test/Transforms/Coroutines/no-suspend.ll

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,58 @@ suspend:
361361
ret void
362362
}
363363

364+
; SimplifySuspendPoint should not simplify final suspend point
365+
;
366+
; CHECK-LABEL: define void @cannot_simplify_final_suspend(
367+
; CHECK-NEXT: entry:
368+
; CHECK-NEXT: llvm.coro.id
369+
;
370+
define void @cannot_simplify_final_suspend() "coroutine.presplit"="1" personality i32 0 {
371+
entry:
372+
%id = call token @llvm.coro.id(i32 0, i8* null, i8* null, i8* null)
373+
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
374+
br i1 %need.dyn.alloc, label %dyn.alloc, label %coro.begin
375+
dyn.alloc:
376+
%size = call i32 @llvm.coro.size.i32()
377+
%alloc = call i8* @malloc(i32 %size)
378+
br label %coro.begin
379+
coro.begin:
380+
%phi = phi i8* [ null, %entry ], [ %alloc, %dyn.alloc ]
381+
%hdl = call noalias i8* @llvm.coro.begin(token %id, i8* %phi)
382+
br label %body
383+
body:
384+
%save = call token @llvm.coro.save(i8* %hdl)
385+
%subfn = call i8* @llvm.coro.subfn.addr(i8* %hdl, i8 1)
386+
%bcast = bitcast i8* %subfn to void (i8*)*
387+
invoke fastcc void %bcast(i8* %hdl) to label %real_susp unwind label %lpad
388+
389+
real_susp:
390+
%0 = call i8 @llvm.coro.suspend(token %save, i1 1)
391+
switch i8 %0, label %suspend [i8 0, label %resume
392+
i8 1, label %pre.cleanup]
393+
resume:
394+
call void @print(i32 0)
395+
br label %cleanup
396+
397+
pre.cleanup:
398+
call void @print(i32 1)
399+
br label %cleanup
400+
401+
cleanup:
402+
%mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
403+
call void @free(i8* %mem)
404+
br label %suspend
405+
suspend:
406+
call i1 @llvm.coro.end(i8* %hdl, i1 false)
407+
ret void
408+
lpad:
409+
%lpval = landingpad { i8*, i32 }
410+
cleanup
411+
412+
call void @print(i32 2)
413+
resume { i8*, i32 } %lpval
414+
}
415+
364416
declare i8* @malloc(i32)
365417
declare void @free(i8*)
366418
declare void @print(i32)

0 commit comments

Comments
 (0)