Skip to content

Commit 1573826

Browse files
niaowaykevl
authored andcommitted
transform (coroutines): move any misplaced entry-block allocas to the start of the entry block before lowering
1 parent ecd8c2d commit 1573826

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

transform/coroutines.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,27 @@ func (c *coroutineLoweringPass) lowerCallReturn(caller *asyncFunc, call llvm.Val
763763
// lowerFuncCoro transforms an async function into a coroutine by lowering async operations to `llvm.coro` intrinsics.
764764
// See https://llvm.org/docs/Coroutines.html for more information on these intrinsics.
765765
func (c *coroutineLoweringPass) lowerFuncCoro(fn *asyncFunc) {
766+
// Ensure that any alloca instructions in the entry block are at the start.
767+
// Otherwise, block splitting would result in unintended behavior.
768+
{
769+
// Skip alloca instructions at the start of the block.
770+
inst := fn.fn.FirstBasicBlock().FirstInstruction()
771+
for !inst.IsAAllocaInst().IsNil() {
772+
inst = llvm.NextInstruction(inst)
773+
}
774+
775+
// Find any other alloca instructions and move them after the other allocas.
776+
c.builder.SetInsertPointBefore(inst)
777+
for !inst.IsNil() {
778+
next := llvm.NextInstruction(inst)
779+
if !inst.IsAAllocaInst().IsNil() {
780+
inst.RemoveFromParentAsInstruction()
781+
c.builder.Insert(inst)
782+
}
783+
inst = next
784+
}
785+
}
786+
766787
returnType := fn.fn.Type().ElementType().ReturnType()
767788

768789
// Prepare coroutine state.

0 commit comments

Comments
 (0)