asyncio: merge promise and coro frame allocation

This commit is contained in:
Li Jie
2024-08-06 14:40:38 +08:00
parent 578bc165c4
commit 7b2747ce0c
4 changed files with 91 additions and 54 deletions

View File

@@ -383,9 +383,14 @@ func (b Builder) EndAsync() {
}
/*
pesudo code:
retPtr := malloc(sizeof(Promise))
id := @llvm.coro.id(0, null, null, null)
promiseSize := sizeof(Promise[T])
frameSize := @llvm.coro.size.i64()
allocSize := promiseSize + frameSize
promise := malloc(allocSize)
needAlloc := @llvm.coro.alloc(id)
; Allocate memory for return type and coroutine frame
frame := null
@@ -408,20 +413,23 @@ func (b Builder) BeginAsync(fn Function, entryBlk, allocBlk, cleanBlk, suspdBlk,
b.async = true
b.SetBlock(entryBlk)
promiseSize := b.Const(constant.MakeUint64(b.Prog.SizeOf(promiseTy)), b.Prog.Int64()).SetName("promise.size")
promise := b.AllocZ(promiseSize).SetName("promise")
promise.Type = b.Prog.Pointer(promiseTy)
b.promise = promise
align := b.Const(constant.MakeInt64(0), b.Prog.CInt()).SetName("align")
null := b.Const(nil, b.Prog.CIntPtr())
id := b.CoID(align, null, null, null).SetName("id")
b.asyncToken = id
promiseSize := b.Const(constant.MakeUint64(b.Prog.SizeOf(promiseTy)), b.Prog.Int64()).SetName("alloc.size")
frameSize := b.CoSizeI64().SetName("frame.size")
allocSize := b.BinOp(token.ADD, promiseSize, frameSize).SetName("alloc.size")
promise := b.AllocZ(allocSize).SetName("promise")
b.promise = promise
promise.Type = b.Prog.Pointer(promiseTy)
needAlloc := b.CoAlloc(id).SetName("need.dyn.alloc")
b.If(needAlloc, allocBlk, beginBlk)
b.SetBlock(allocBlk)
frameSize := b.CoSizeI64().SetName("frame.size")
frame := b.AllocZ(frameSize).SetName("frame")
frame := b.OffsetPtr(promise, promiseSize)
b.Jump(beginBlk)
b.SetBlock(beginBlk)
phi := b.Phi(b.Prog.VoidPtr())
phi.SetName("frame")

View File

@@ -594,6 +594,32 @@ _llgo_0:
`)
}
func TestPointerOffset(t *testing.T) {
prog := NewProgram(nil)
pkg := prog.NewPackage("bar", "foo/bar")
params := types.NewTuple(
types.NewVar(0, nil, "p", types.NewPointer(types.Typ[types.Int])),
types.NewVar(0, nil, "offset", types.Typ[types.Int]),
)
rets := types.NewTuple(types.NewVar(0, nil, "", types.NewPointer(types.Typ[types.Int])))
sig := types.NewSignatureType(nil, nil, nil, params, rets, false)
fn := pkg.NewFunc("fn", sig, InGo)
b := fn.MakeBody(1)
ptr := fn.Param(0)
offset := fn.Param(1)
result := b.OffsetPtr(ptr, offset)
b.Return(result)
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
source_filename = "foo/bar"
define ptr @fn(ptr %0, i64 %1) {
_llgo_0:
%2 = getelementptr ptr, ptr %0, i64 %1
ret ptr %2
}
`)
}
func TestLLVMTrap(t *testing.T) {
prog := NewProgram(nil)
pkg := prog.NewPackage("bar", "foo/bar")