Skip to content

Commit 8906192

Browse files
niaowaykevl
authored andcommitted
fix goroutine lowering type errors
1 parent 20a55e7 commit 8906192

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

compiler/goroutine-lowering.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,6 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
450450
}
451451
coroDebugPrintln("scanning", f.Name())
452452

453-
var retAlloc llvm.Value
454-
455453
// Rewrite async calls
456454
for bb := f.EntryBasicBlock(); !bb.IsNil(); bb = llvm.NextBasicBlock(bb) {
457455
for inst := bb.FirstInstruction(); !inst.IsNil(); inst = llvm.NextInstruction(inst) {
@@ -511,9 +509,9 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
511509
// pass parent handle directly into function
512510
inst.SetOperand(inst.OperandsCount()-2, parentHandle)
513511

514-
if inst.Type().TypeKind() != llvm.VoidTypeKind {
512+
if callee.Type().ElementType().ReturnType().TypeKind() != llvm.VoidTypeKind {
515513
// delete return value
516-
uses[0].SetOperand(0, llvm.Undef(inst.Type()))
514+
uses[0].SetOperand(0, llvm.Undef(callee.Type().ElementType().ReturnType()))
517515
}
518516

519517
c.builder.SetInsertPointBefore(next)
@@ -534,15 +532,9 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
534532

535533
// Allocate space for the return value.
536534
var retvalAlloca llvm.Value
537-
if inst.Type().TypeKind() != llvm.VoidTypeKind {
538-
if retAlloc.IsNil() {
539-
// insert at start of function
540-
c.builder.SetInsertPointBefore(f.EntryBasicBlock().FirstInstruction())
541-
542-
// allocate return value buffer
543-
retAlloc = c.builder.CreateAlloca(inst.Type(), "coro.retvalAlloca")
544-
}
545-
retvalAlloca = retAlloc
535+
if callee.Type().ElementType().ReturnType().TypeKind() != llvm.VoidTypeKind {
536+
// allocate return value buffer
537+
retvalAlloca = c.createInstructionAlloca(callee.Type().ElementType().ReturnType(), inst, "coro.retvalAlloca")
546538

547539
// call before function
548540
c.builder.SetInsertPointBefore(inst)
@@ -672,7 +664,7 @@ func (c *Compiler) markAsyncFunctions() (needsScheduler bool, err error) {
672664
c.builder.CreateStore(inst.Operand(0), retPtr)
673665

674666
// delete return value
675-
inst.SetOperand(0, llvm.Undef(inst.Type()))
667+
inst.SetOperand(0, llvm.Undef(f.Type().ElementType().ReturnType()))
676668
}
677669

678670
// insert reactivation call

compiler/llvm.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ func (c *Compiler) createTemporaryAlloca(t llvm.Type, name string) (alloca, bitc
5454
return
5555
}
5656

57+
// createInstructionAlloca creates an alloca in the entry block, and places lifetime control intrinsics around the instruction
58+
func (c *Compiler) createInstructionAlloca(t llvm.Type, inst llvm.Value, name string) llvm.Value {
59+
alloca := c.createEntryBlockAlloca(t, name)
60+
c.builder.SetInsertPointBefore(inst)
61+
bitcast := c.builder.CreateBitCast(alloca, c.i8ptrType, name+".bitcast")
62+
size := llvm.ConstInt(c.ctx.Int64Type(), c.targetData.TypeAllocSize(t), false)
63+
c.builder.CreateCall(c.getLifetimeStartFunc(), []llvm.Value{size, bitcast}, "")
64+
if next := llvm.NextInstruction(inst); !next.IsNil() {
65+
c.builder.SetInsertPointBefore(next)
66+
} else {
67+
c.builder.SetInsertPointAtEnd(inst.InstructionParent())
68+
}
69+
c.builder.CreateCall(c.getLifetimeEndFunc(), []llvm.Value{size, bitcast}, "")
70+
return alloca
71+
}
72+
5773
// emitLifetimeEnd signals the end of an (alloca) lifetime by calling the
5874
// llvm.lifetime.end intrinsic. It is commonly used together with
5975
// createTemporaryAlloca.

0 commit comments

Comments
 (0)