[Coroutine] Update promise object's final layout index

promise is a header field but it is not guaranteed that it would be the third
field of the frame due to `performOptimizedStructLayout`.

Reviewed By: lxfind

Differential Revision: https://reviews.llvm.org/D94137
This commit is contained in:
Yuanfang Chen 2021-01-12 17:42:10 -08:00
parent 055644cc45
commit 5c7dcd7aea
2 changed files with 66 additions and 7 deletions

View File

@ -758,6 +758,15 @@ static StructType *buildFrameType(Function &F, coro::Shape &Shape,
// Because multiple allocas may own the same field slot,
// we add allocas to field here.
B.addFieldForAllocas(F, FrameData, Shape);
// Add PromiseAlloca to Allocas list so that
// 1. updateLayoutIndex could update its index after
// `performOptimizedStructLayout`
// 2. it is processed in insertSpills.
if (Shape.ABI == coro::ABI::Switch && PromiseAlloca)
// We assume that the promise alloca won't be modified before
// CoroBegin and no alias will be create before CoroBegin.
FrameData.Allocas.emplace_back(
PromiseAlloca, DenseMap<Instruction *, llvm::Optional<APInt>>{}, false);
// Create an entry for every spilled value.
for (auto &S : FrameData.Spills) {
FieldIDType Id = B.addField(S.first->getType(), None);
@ -2288,13 +2297,6 @@ void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
Shape.ABI == coro::ABI::Async)
sinkSpillUsesAfterCoroBegin(F, FrameData, Shape.CoroBegin);
Shape.FrameTy = buildFrameType(F, Shape, FrameData);
// Add PromiseAlloca to Allocas list so that it is processed in insertSpills.
if (Shape.ABI == coro::ABI::Switch && Shape.SwitchLowering.PromiseAlloca)
// We assume that the promise alloca won't be modified before
// CoroBegin and no alias will be create before CoroBegin.
FrameData.Allocas.emplace_back(
Shape.SwitchLowering.PromiseAlloca,
DenseMap<Instruction *, llvm::Optional<APInt>>{}, false);
Shape.FramePtr = insertSpills(FrameData, Shape);
lowerLocalAllocas(LocalAllocas, DeadInstructions);

View File

@ -0,0 +1,57 @@
; Check that promise object is reloaded from the correct index of the coro frame.
; RUN: opt < %s -coro-split -S | FileCheck %s
; RUN: opt < %s -passes=coro-split -S | FileCheck %s
%"class.task::promise_type" = type { [64 x i8] }
declare void @consume(i32*)
declare void @consume2(%"class.task::promise_type"*)
define i8* @f() "coroutine.presplit"="1" {
entry:
%data = alloca i32, align 4
%__promise = alloca %"class.task::promise_type", align 64
%pv = bitcast %"class.task::promise_type"* %__promise to i8*
%id = call token @llvm.coro.id(i32 0, i8* %pv, i8* null, i8* null)
%size = call i32 @llvm.coro.size.i32()
%alloc = call i8* @malloc(i32 %size)
%hdl = call i8* @llvm.coro.begin(token %id, i8* %alloc)
call void @consume(i32* %data)
%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 @consume(i32* %data)
call void @consume2(%"class.task::promise_type"* %__promise)
br label %cleanup
cleanup:
%mem = call i8* @llvm.coro.free(token %id, i8* %hdl)
call void @free(i8* %mem)
br label %suspend
suspend:
call i1 @llvm.coro.end(i8* %hdl, i1 0)
ret i8* %hdl
}
; CHECK-LABEL: %f.Frame = type { void (%f.Frame*)*, void (%f.Frame*)*, i32, i1, [43 x i8], %"class.task::promise_type" }
; CHECK-LABEL: @f.resume(
; CHECK: %[[DATA:.+]] = getelementptr inbounds %f.Frame, %f.Frame* %FramePtr, i32 0, i32 5
; CHECK: call void @consume2(%"class.task::promise_type"* %[[DATA]])
; CHECK: ret void
declare i8* @llvm.coro.free(token, i8*)
declare i32 @llvm.coro.size.i32()
declare i8 @llvm.coro.suspend(token, i1)
declare void @llvm.coro.resume(i8*)
declare void @llvm.coro.destroy(i8*)
declare token @llvm.coro.id(i32, i8*, i8*, i8*)
declare i1 @llvm.coro.alloc(token)
declare i8* @llvm.coro.begin(token, i8*)
declare i1 @llvm.coro.end(i8*, i1)
declare noalias i8* @malloc(i32)
declare double @print(double)
declare void @free(i8*)