cl: compile async functions
This commit is contained in:
37
cl/_testdata/async/in.go
Normal file
37
cl/_testdata/async/in.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package async_compile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/goplus/llgo/x/async"
|
||||
)
|
||||
|
||||
func GenInts() (co *async.Promise[int]) {
|
||||
co.Yield(1)
|
||||
co.Yield(2)
|
||||
co.Yield(3)
|
||||
return
|
||||
}
|
||||
|
||||
func WrapGenInts() *async.Promise[int] {
|
||||
return GenInts()
|
||||
}
|
||||
|
||||
func UseGenInts() int {
|
||||
co := WrapGenInts()
|
||||
r := 0
|
||||
for !co.Done() {
|
||||
r += co.Next()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func GenIntsWithDefer() (co *async.Promise[int]) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("panic:", r)
|
||||
}
|
||||
}()
|
||||
co.Yield(1)
|
||||
panic("GenIntsWithDefer")
|
||||
}
|
||||
372
cl/_testdata/async/out.ll
Normal file
372
cl/_testdata/async/out.ll
Normal file
@@ -0,0 +1,372 @@
|
||||
; ModuleID = 'github.com/goplus/llgo/cl/_testdata/async'
|
||||
source_filename = "github.com/goplus/llgo/cl/_testdata/async"
|
||||
|
||||
%"github.com/goplus/llgo/internal/runtime.Defer" = type { ptr, i64, ptr, ptr }
|
||||
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
|
||||
%"github.com/goplus/llgo/internal/runtime.eface" = type { ptr, ptr }
|
||||
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
|
||||
%"github.com/goplus/llgo/internal/runtime.iface" = type { ptr, ptr }
|
||||
%"github.com/goplus/llgo/x/async.Promise[int]" = type { ptr, i64 }
|
||||
|
||||
@"github.com/goplus/llgo/cl/_testdata/async.init$guard" = global i1 false, align 1
|
||||
@__llgo_defer = linkonce global i32 0, align 4
|
||||
@0 = private unnamed_addr constant [16 x i8] c"GenIntsWithDefer", align 1
|
||||
@_llgo_string = linkonce global ptr null, align 8
|
||||
@1 = private unnamed_addr constant [6 x i8] c"panic:", align 1
|
||||
|
||||
define ptr @"github.com/goplus/llgo/cl/_testdata/async.GenInts"() #0 {
|
||||
entry:
|
||||
%promise = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
|
||||
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
|
||||
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
|
||||
br i1 %need.dyn.alloc, label %alloc, label %_llgo_4
|
||||
|
||||
alloc: ; preds = %entry
|
||||
%frame.size = call i64 @llvm.coro.size.i64()
|
||||
%frame = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %frame.size)
|
||||
br label %_llgo_4
|
||||
|
||||
clean: ; preds = %_llgo_7, %_llgo_6, %_llgo_5, %_llgo_4
|
||||
%0 = call ptr @llvm.coro.free(token %id, ptr %hdl)
|
||||
br label %suspend
|
||||
|
||||
suspend: ; preds = %_llgo_6, %_llgo_5, %_llgo_4, %clean
|
||||
%1 = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
|
||||
ret ptr %promise
|
||||
|
||||
_llgo_4: ; preds = %alloc, %entry
|
||||
%frame1 = phi ptr [ null, %entry ], [ %frame, %alloc ]
|
||||
%hdl = call ptr @llvm.coro.begin(token %id, ptr %frame1)
|
||||
store ptr %hdl, ptr %promise, align 8
|
||||
call void @"github.com/goplus/llgo/x/async.(*Promise).setValue[int]"(ptr %promise, i64 1)
|
||||
%2 = call i8 @llvm.coro.suspend(token %id, i1 false)
|
||||
switch i8 %2, label %suspend [
|
||||
i8 0, label %_llgo_5
|
||||
i8 1, label %clean
|
||||
]
|
||||
|
||||
_llgo_5: ; preds = %_llgo_4
|
||||
call void @"github.com/goplus/llgo/x/async.(*Promise).setValue[int]"(ptr %promise, i64 2)
|
||||
%3 = call i8 @llvm.coro.suspend(token %id, i1 false)
|
||||
switch i8 %3, label %suspend [
|
||||
i8 0, label %_llgo_6
|
||||
i8 1, label %clean
|
||||
]
|
||||
|
||||
_llgo_6: ; preds = %_llgo_5
|
||||
call void @"github.com/goplus/llgo/x/async.(*Promise).setValue[int]"(ptr %promise, i64 3)
|
||||
%4 = call i8 @llvm.coro.suspend(token %id, i1 false)
|
||||
switch i8 %4, label %suspend [
|
||||
i8 0, label %_llgo_7
|
||||
i8 1, label %clean
|
||||
]
|
||||
|
||||
_llgo_7: ; preds = %_llgo_6
|
||||
br label %clean
|
||||
}
|
||||
|
||||
define ptr @"github.com/goplus/llgo/cl/_testdata/async.GenIntsWithDefer"() #0 {
|
||||
entry:
|
||||
%promise = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
|
||||
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
|
||||
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
|
||||
br i1 %need.dyn.alloc, label %alloc, label %_llgo_4
|
||||
|
||||
alloc: ; preds = %entry
|
||||
%frame.size = call i64 @llvm.coro.size.i64()
|
||||
%frame = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %frame.size)
|
||||
br label %_llgo_4
|
||||
|
||||
clean: ; preds = %_llgo_5, %_llgo_8
|
||||
%0 = call ptr @llvm.coro.free(token %id, ptr %hdl)
|
||||
br label %suspend
|
||||
|
||||
suspend: ; preds = %_llgo_8, %clean
|
||||
%1 = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
|
||||
ret ptr %promise
|
||||
|
||||
_llgo_4: ; preds = %alloc, %entry
|
||||
%frame1 = phi ptr [ null, %entry ], [ %frame, %alloc ]
|
||||
%hdl = call ptr @llvm.coro.begin(token %id, ptr %frame1)
|
||||
store ptr %hdl, ptr %promise, align 8
|
||||
%2 = alloca ptr, align 8
|
||||
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %2, i64 8)
|
||||
%4 = load i32, ptr @__llgo_defer, align 4
|
||||
%5 = call ptr @pthread_getspecific(i32 %4)
|
||||
%6 = alloca i8, i64 196, align 1
|
||||
%7 = alloca i8, i64 32, align 1
|
||||
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, i32 0, i32 0
|
||||
store ptr %6, ptr %8, align 8
|
||||
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, i32 0, i32 1
|
||||
store i64 0, ptr %9, align 4
|
||||
%10 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, i32 0, i32 2
|
||||
store ptr %5, ptr %10, align 8
|
||||
%11 = call i32 @pthread_setspecific(i32 %4, ptr %7)
|
||||
%12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, i32 0, i32 1
|
||||
%13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, i32 0, i32 3
|
||||
%14 = call i32 @sigsetjmp(ptr %6, i32 0)
|
||||
%15 = icmp eq i32 %14, 0
|
||||
br i1 %15, label %_llgo_8, label %_llgo_9
|
||||
|
||||
_llgo_5: ; preds = %_llgo_8, %_llgo_7
|
||||
%16 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||
%17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 0
|
||||
store ptr @0, ptr %17, align 8
|
||||
%18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1
|
||||
store i64 16, ptr %18, align 4
|
||||
%19 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8
|
||||
%20 = load ptr, ptr @_llgo_string, align 8
|
||||
%21 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
|
||||
store %"github.com/goplus/llgo/internal/runtime.String" %19, ptr %21, align 8
|
||||
%22 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
|
||||
%23 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %22, i32 0, i32 0
|
||||
store ptr %20, ptr %23, align 8
|
||||
%24 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %22, i32 0, i32 1
|
||||
store ptr %21, ptr %24, align 8
|
||||
%25 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %22, align 8
|
||||
call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %25)
|
||||
unreachable
|
||||
%26 = load ptr, ptr %3, align 8
|
||||
br label %clean
|
||||
|
||||
_llgo_6: ; preds = %_llgo_9
|
||||
%27 = load i64, ptr %12, align 4
|
||||
call void @"github.com/goplus/llgo/cl/_testdata/async.GenIntsWithDefer$1"()
|
||||
%28 = load %"github.com/goplus/llgo/internal/runtime.Defer", ptr %7, align 8
|
||||
%29 = extractvalue %"github.com/goplus/llgo/internal/runtime.Defer" %28, 2
|
||||
%30 = call i32 @pthread_setspecific(i32 %4, ptr %29)
|
||||
%31 = load ptr, ptr %13, align 8
|
||||
indirectbr ptr %31, [label %_llgo_7]
|
||||
|
||||
_llgo_7: ; preds = %_llgo_6
|
||||
call void @"github.com/goplus/llgo/internal/runtime.Rethrow"(ptr %5)
|
||||
br label %_llgo_5
|
||||
|
||||
_llgo_8: ; preds = %_llgo_4
|
||||
%32 = load ptr, ptr %3, align 8
|
||||
call void @"github.com/goplus/llgo/x/async.(*Promise).setValue[int]"(ptr %promise, i64 1)
|
||||
%33 = call i8 @llvm.coro.suspend(token %id, i1 false)
|
||||
switch i8 %33, label %suspend [
|
||||
i8 0, label %_llgo_5
|
||||
i8 1, label %clean
|
||||
]
|
||||
|
||||
_llgo_9: ; preds = %_llgo_4
|
||||
store ptr blockaddress(@"github.com/goplus/llgo/cl/_testdata/async.GenIntsWithDefer", %_llgo_7), ptr %13, align 8
|
||||
br label %_llgo_6
|
||||
|
||||
_llgo_10: ; No predecessors!
|
||||
}
|
||||
|
||||
define void @"github.com/goplus/llgo/cl/_testdata/async.GenIntsWithDefer$1"() {
|
||||
_llgo_0:
|
||||
%0 = call %"github.com/goplus/llgo/internal/runtime.eface" @"github.com/goplus/llgo/internal/runtime.Recover"()
|
||||
%1 = call i1 @"github.com/goplus/llgo/internal/runtime.EfaceEqual"(%"github.com/goplus/llgo/internal/runtime.eface" %0, %"github.com/goplus/llgo/internal/runtime.eface" zeroinitializer)
|
||||
%2 = xor i1 %1, true
|
||||
br i1 %2, label %_llgo_1, label %_llgo_2
|
||||
|
||||
_llgo_1: ; preds = %_llgo_0
|
||||
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
|
||||
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %3, i64 0
|
||||
%5 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
|
||||
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %5, i32 0, i32 0
|
||||
store ptr @1, ptr %6, align 8
|
||||
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %5, i32 0, i32 1
|
||||
store i64 6, ptr %7, align 4
|
||||
%8 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %5, align 8
|
||||
%9 = load ptr, ptr @_llgo_string, align 8
|
||||
%10 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
|
||||
store %"github.com/goplus/llgo/internal/runtime.String" %8, ptr %10, align 8
|
||||
%11 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
|
||||
%12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, i32 0, i32 0
|
||||
store ptr %9, ptr %12, align 8
|
||||
%13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, i32 0, i32 1
|
||||
store ptr %10, ptr %13, align 8
|
||||
%14 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, align 8
|
||||
store %"github.com/goplus/llgo/internal/runtime.eface" %14, ptr %4, align 8
|
||||
%15 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %3, i64 1
|
||||
store %"github.com/goplus/llgo/internal/runtime.eface" %0, ptr %15, align 8
|
||||
%16 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
||||
%17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %16, i32 0, i32 0
|
||||
store ptr %3, ptr %17, align 8
|
||||
%18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %16, i32 0, i32 1
|
||||
store i64 2, ptr %18, align 4
|
||||
%19 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %16, i32 0, i32 2
|
||||
store i64 2, ptr %19, align 4
|
||||
%20 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %16, align 8
|
||||
%21 = call { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice" %20)
|
||||
br label %_llgo_2
|
||||
|
||||
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @"github.com/goplus/llgo/cl/_testdata/async.UseGenInts"() {
|
||||
_llgo_0:
|
||||
%0 = call ptr @"github.com/goplus/llgo/cl/_testdata/async.WrapGenInts"()
|
||||
br label %_llgo_3
|
||||
|
||||
_llgo_1: ; preds = %_llgo_3
|
||||
%1 = call i64 @"github.com/goplus/llgo/x/async.(*Promise).Next[int]"(ptr %0)
|
||||
%2 = add i64 %3, %1
|
||||
br label %_llgo_3
|
||||
|
||||
_llgo_2: ; preds = %_llgo_3
|
||||
ret i64 %3
|
||||
|
||||
_llgo_3: ; preds = %_llgo_1, %_llgo_0
|
||||
%3 = phi i64 [ 0, %_llgo_0 ], [ %2, %_llgo_1 ]
|
||||
%4 = call i1 @"github.com/goplus/llgo/x/async.(*Promise).Done[int]"(ptr %0)
|
||||
br i1 %4, label %_llgo_2, label %_llgo_1
|
||||
}
|
||||
|
||||
define ptr @"github.com/goplus/llgo/cl/_testdata/async.WrapGenInts"() #0 {
|
||||
entry:
|
||||
%promise = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
|
||||
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
|
||||
%need.dyn.alloc = call i1 @llvm.coro.alloc(token %id)
|
||||
br i1 %need.dyn.alloc, label %alloc, label %_llgo_4
|
||||
|
||||
alloc: ; preds = %entry
|
||||
%frame.size = call i64 @llvm.coro.size.i64()
|
||||
%frame = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %frame.size)
|
||||
br label %_llgo_4
|
||||
|
||||
clean: ; preds = %_llgo_4
|
||||
%0 = call ptr @llvm.coro.free(token %id, ptr %hdl)
|
||||
br label %suspend
|
||||
|
||||
suspend: ; preds = %clean
|
||||
%1 = call i1 @llvm.coro.end(ptr %hdl, i1 false, token none)
|
||||
ret ptr %promise
|
||||
|
||||
_llgo_4: ; preds = %alloc, %entry
|
||||
%frame1 = phi ptr [ null, %entry ], [ %frame, %alloc ]
|
||||
%hdl = call ptr @llvm.coro.begin(token %id, ptr %frame1)
|
||||
store ptr %hdl, ptr %promise, align 8
|
||||
%2 = call ptr @"github.com/goplus/llgo/cl/_testdata/async.GenInts"()
|
||||
br label %clean
|
||||
}
|
||||
|
||||
define void @"github.com/goplus/llgo/cl/_testdata/async.init"() {
|
||||
_llgo_0:
|
||||
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testdata/async.init$guard", align 1
|
||||
br i1 %0, label %_llgo_2, label %_llgo_1
|
||||
|
||||
_llgo_1: ; preds = %_llgo_0
|
||||
store i1 true, ptr @"github.com/goplus/llgo/cl/_testdata/async.init$guard", align 1
|
||||
call void @fmt.init()
|
||||
call void @"github.com/goplus/llgo/cl/_testdata/async.init$after"()
|
||||
br label %_llgo_2
|
||||
|
||||
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||
ret void
|
||||
}
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
|
||||
|
||||
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
|
||||
declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr) #1
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i1 @llvm.coro.alloc(token) #2
|
||||
|
||||
; Function Attrs: nounwind memory(none)
|
||||
declare i64 @llvm.coro.size.i64() #3
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @llvm.coro.begin(token, ptr writeonly) #2
|
||||
|
||||
; Function Attrs: nounwind memory(argmem: read)
|
||||
declare ptr @llvm.coro.free(token, ptr nocapture readonly) #4
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i1 @llvm.coro.end(ptr, i1, token) #2
|
||||
|
||||
declare void @"github.com/goplus/llgo/x/async.(*Promise).setValue[int]"(ptr, i64)
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i8 @llvm.coro.suspend(token, i1) #2
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64)
|
||||
|
||||
declare ptr @pthread_getspecific(i32)
|
||||
|
||||
declare i32 @pthread_setspecific(i32, ptr)
|
||||
|
||||
declare i32 @sigsetjmp(ptr, i32)
|
||||
|
||||
declare void @"github.com/goplus/llgo/internal/runtime.Rethrow"(ptr)
|
||||
|
||||
define void @"github.com/goplus/llgo/cl/_testdata/async.init$after"() {
|
||||
_llgo_0:
|
||||
%0 = load ptr, ptr @_llgo_string, align 8
|
||||
%1 = icmp eq ptr %0, null
|
||||
br i1 %1, label %_llgo_1, label %_llgo_2
|
||||
|
||||
_llgo_1: ; preds = %_llgo_0
|
||||
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64 24)
|
||||
store ptr %2, ptr @_llgo_string, align 8
|
||||
br label %_llgo_2
|
||||
|
||||
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||
%3 = load i32, ptr @__llgo_defer, align 4
|
||||
%4 = icmp eq i32 %3, 0
|
||||
br i1 %4, label %_llgo_3, label %_llgo_4
|
||||
|
||||
_llgo_3: ; preds = %_llgo_2
|
||||
%5 = call i32 @pthread_key_create(ptr @__llgo_defer, ptr null)
|
||||
br label %_llgo_4
|
||||
|
||||
_llgo_4: ; preds = %_llgo_3, %_llgo_2
|
||||
ret void
|
||||
}
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.Basic"(i64)
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
|
||||
|
||||
declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface")
|
||||
|
||||
declare %"github.com/goplus/llgo/internal/runtime.eface" @"github.com/goplus/llgo/internal/runtime.Recover"()
|
||||
|
||||
declare i1 @"github.com/goplus/llgo/internal/runtime.EfaceEqual"(%"github.com/goplus/llgo/internal/runtime.eface", %"github.com/goplus/llgo/internal/runtime.eface")
|
||||
|
||||
declare { i64, %"github.com/goplus/llgo/internal/runtime.iface" } @fmt.Println(%"github.com/goplus/llgo/internal/runtime.Slice")
|
||||
|
||||
define i1 @"github.com/goplus/llgo/x/async.(*Promise).Done[int]"(ptr %0) {
|
||||
_llgo_0:
|
||||
%1 = getelementptr inbounds %"github.com/goplus/llgo/x/async.Promise[int]", ptr %0, i32 0, i32 0
|
||||
%2 = load ptr, ptr %1, align 8
|
||||
%3 = call i1 @llvm.coro.done(ptr %2)
|
||||
%4 = zext i1 %3 to i64
|
||||
%5 = trunc i64 %4 to i8
|
||||
%6 = icmp ne i8 %5, 0
|
||||
ret i1 %6
|
||||
}
|
||||
|
||||
define i64 @"github.com/goplus/llgo/x/async.(*Promise).Next[int]"(ptr %0) {
|
||||
_llgo_0:
|
||||
%1 = getelementptr inbounds %"github.com/goplus/llgo/x/async.Promise[int]", ptr %0, i32 0, i32 0
|
||||
%2 = load ptr, ptr %1, align 8
|
||||
call void @llvm.coro.resume(ptr %2)
|
||||
%3 = getelementptr inbounds %"github.com/goplus/llgo/x/async.Promise[int]", ptr %0, i32 0, i32 1
|
||||
%4 = load i64, ptr %3, align 4
|
||||
ret i64 %4
|
||||
}
|
||||
|
||||
declare void @fmt.init()
|
||||
|
||||
; Function Attrs: nounwind memory(argmem: readwrite)
|
||||
declare i1 @llvm.coro.done(ptr nocapture readonly) #5
|
||||
|
||||
declare void @llvm.coro.resume(ptr)
|
||||
|
||||
declare i32 @pthread_key_create(ptr, ptr)
|
||||
|
||||
attributes #0 = { "presplitcoroutine" }
|
||||
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
|
||||
attributes #2 = { nounwind }
|
||||
attributes #3 = { nounwind memory(none) }
|
||||
attributes #4 = { nounwind memory(argmem: read) }
|
||||
attributes #5 = { nounwind memory(argmem: readwrite) }
|
||||
119
cl/async.go
Normal file
119
cl/async.go
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cl
|
||||
|
||||
import (
|
||||
"go/types"
|
||||
"strings"
|
||||
|
||||
llssa "github.com/goplus/llgo/ssa"
|
||||
"golang.org/x/tools/go/ssa"
|
||||
)
|
||||
|
||||
// TODO(lijie): need more generics, shouldn't limit to async.Promise
|
||||
func promiseType(ty types.Type) (types.Type, bool) {
|
||||
// ty is a generic type, so we need to check the package path and type name
|
||||
if ptrTy, ok := ty.(*types.Pointer); ok {
|
||||
ty = ptrTy.Elem()
|
||||
if ty, ok := ty.(*types.Named); ok {
|
||||
if ty.Obj().Pkg() == nil {
|
||||
return nil, false
|
||||
}
|
||||
if ty.Obj().Pkg().Path() == "github.com/goplus/llgo/x/async" && ty.Obj().Name() == "Promise" {
|
||||
return ty, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// check function return async.Promise[T]
|
||||
// TODO(lijie): make it generic
|
||||
func isAsyncFunc(sig *types.Signature) bool {
|
||||
r := sig.Results()
|
||||
if r.Len() != 1 {
|
||||
return false
|
||||
}
|
||||
ty := r.At(0).Type()
|
||||
_, ok := promiseType(ty)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (p *context) coAwait(b llssa.Builder, args []ssa.Value) llssa.Expr {
|
||||
if !isAsyncFunc(b.Func.RawType().(*types.Signature)) {
|
||||
panic("coAwait(promise *T) T: invalid context")
|
||||
}
|
||||
if len(args) == 1 {
|
||||
// promise := p.compileValue(b, args[0])
|
||||
b.Unreachable()
|
||||
// return b.CoroutineAwait(promise)
|
||||
}
|
||||
panic("coAwait(promise *T) T: invalid arguments")
|
||||
}
|
||||
|
||||
func (p *context) coSuspend(b llssa.Builder, final llssa.Expr) {
|
||||
b.CoSuspend(b.AsyncToken(), final)
|
||||
}
|
||||
|
||||
func (p *context) coDone(b llssa.Builder, args []ssa.Value) llssa.Expr {
|
||||
if len(args) != 1 {
|
||||
panic("coDone(promise *T): invalid arguments")
|
||||
}
|
||||
hdl := p.compileValue(b, args[0])
|
||||
return b.CoDone(hdl)
|
||||
}
|
||||
|
||||
func (p *context) coResume(b llssa.Builder, args []ssa.Value) {
|
||||
if len(args) == 1 {
|
||||
hdl := p.compileValue(b, args[0])
|
||||
b.CoResume(hdl)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *context) coReturn(b llssa.Builder, args []ssa.Value) {
|
||||
cargs := make([]llssa.Expr, len(args))
|
||||
for i, arg := range args {
|
||||
cargs[i] = p.compileValue(b, arg)
|
||||
}
|
||||
b.CoReturn(cargs...)
|
||||
}
|
||||
|
||||
func (p *context) coYield(b llssa.Builder, fn *ssa.Function, args []ssa.Value) {
|
||||
typ := fn.Signature.Recv().Type()
|
||||
mthds := p.goProg.MethodSets.MethodSet(typ)
|
||||
// TODO(lijie): make llgo instruction callable (e.g. llgo.yield)
|
||||
var setValue *ssa.Function
|
||||
for i := 0; i < mthds.Len(); i++ {
|
||||
m := mthds.At(i)
|
||||
if ssaMthd := p.goProg.MethodValue(m); ssaMthd != nil {
|
||||
if ssaMthd.Name() == "setValue" || strings.HasPrefix(ssaMthd.Name(), "setValue[") {
|
||||
setValue = ssaMthd
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if setValue == nil {
|
||||
panic("coYield(): not found method setValue")
|
||||
}
|
||||
value := p.compileValue(b, args[1])
|
||||
setValueFn, _, _ := p.funcOf(setValue)
|
||||
b.CoYield(setValueFn, value)
|
||||
}
|
||||
|
||||
func (p *context) coRun(b llssa.Builder, args []ssa.Value) {
|
||||
panic("coRun(): not implemented")
|
||||
}
|
||||
@@ -214,6 +214,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
|
||||
log.Println("==> NewFunc", name, "type:", sig.Recv(), sig, "ftype:", ftype)
|
||||
}
|
||||
}
|
||||
async := isAsyncFunc(f.Signature)
|
||||
if fn == nil {
|
||||
if name == "main" {
|
||||
argc := types.NewParam(token.NoPos, pkgTypes, "", types.Typ[types.Int32])
|
||||
@@ -223,13 +224,20 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
|
||||
results := types.NewTuple(ret)
|
||||
sig = types.NewSignatureType(nil, nil, nil, params, results, false)
|
||||
}
|
||||
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx)
|
||||
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, async)
|
||||
}
|
||||
|
||||
nBlkOff := 0
|
||||
if nblk := len(f.Blocks); nblk > 0 {
|
||||
if async {
|
||||
nBlkOff = 4
|
||||
fn.MakeBlock("entry")
|
||||
fn.MakeBlock("alloc")
|
||||
fn.MakeBlock("clean")
|
||||
fn.MakeBlock("suspend")
|
||||
}
|
||||
fn.MakeBlocks(nblk) // to set fn.HasBody() = true
|
||||
if f.Recover != nil { // set recover block
|
||||
fn.SetRecover(fn.Block(f.Recover.Index))
|
||||
fn.SetRecover(fn.Block(f.Recover.Index + nBlkOff))
|
||||
}
|
||||
p.inits = append(p.inits, func() {
|
||||
p.fn = fn
|
||||
@@ -245,6 +253,10 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
|
||||
log.Println("==> FuncBody", name)
|
||||
}
|
||||
b := fn.NewBuilder()
|
||||
b.SetBlockOffset(nBlkOff)
|
||||
if async {
|
||||
b.BeginAsync(fn)
|
||||
}
|
||||
p.bvals = make(map[ssa.Value]llssa.Expr)
|
||||
off := make([]int, len(f.Blocks))
|
||||
for i, block := range f.Blocks {
|
||||
@@ -280,7 +292,7 @@ func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, n int, do
|
||||
var pkg = p.pkg
|
||||
var fn = p.fn
|
||||
var instrs = block.Instrs[n:]
|
||||
var ret = fn.Block(block.Index)
|
||||
var ret = fn.Block(block.Index + b.BlockOffset())
|
||||
b.SetBlock(ret)
|
||||
if doModInit {
|
||||
if pyModInit = p.pyMod != ""; pyModInit {
|
||||
@@ -650,7 +662,11 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
|
||||
results = make([]llssa.Expr, 1)
|
||||
results[0] = p.prog.IntVal(0, p.prog.CInt())
|
||||
}
|
||||
b.Return(results...)
|
||||
if b.Async() {
|
||||
b.EndAsync()
|
||||
} else {
|
||||
b.Return(results...)
|
||||
}
|
||||
case *ssa.If:
|
||||
fn := p.fn
|
||||
cond := p.compileValue(b, v.Cond)
|
||||
|
||||
11
cl/import.go
11
cl/import.go
@@ -412,7 +412,16 @@ const (
|
||||
llgoAtomicUMax = llgoAtomicOpBase + llssa.OpUMax
|
||||
llgoAtomicUMin = llgoAtomicOpBase + llssa.OpUMin
|
||||
|
||||
llgoAtomicOpLast = llgoAtomicOpBase + int(llssa.OpUMin)
|
||||
llgoCoBase = llgoInstrBase + 0x30
|
||||
llgoCoAwait = llgoCoBase + 0
|
||||
llgoCoSuspend = llgoCoBase + 1
|
||||
llgoCoDone = llgoCoBase + 2
|
||||
llgoCoResume = llgoCoBase + 3
|
||||
llgoCoReturn = llgoCoBase + 4
|
||||
llgoCoYield = llgoCoBase + 5
|
||||
llgoCoRun = llgoCoBase + 6
|
||||
|
||||
llgoAtomicOpLast = llgoCoRun
|
||||
)
|
||||
|
||||
func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, string, int) {
|
||||
|
||||
25
cl/instr.go
25
cl/instr.go
@@ -237,6 +237,14 @@ var llgoInstrs = map[string]int{
|
||||
"atomicMin": int(llgoAtomicMin),
|
||||
"atomicUMax": int(llgoAtomicUMax),
|
||||
"atomicUMin": int(llgoAtomicUMin),
|
||||
|
||||
"coAwait": int(llgoCoAwait),
|
||||
"coResume": int(llgoCoResume),
|
||||
"coSuspend": int(llgoCoSuspend),
|
||||
"coDone": int(llgoCoDone),
|
||||
"coReturn": int(llgoCoReturn),
|
||||
"coYield": int(llgoCoYield),
|
||||
"coRun": int(llgoCoRun),
|
||||
}
|
||||
|
||||
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
|
||||
@@ -265,7 +273,8 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj
|
||||
return nil, nil, ignoredFunc
|
||||
}
|
||||
sig := fn.Signature
|
||||
aFn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false)
|
||||
async := isAsyncFunc(sig)
|
||||
aFn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false, async)
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -390,6 +399,20 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
|
||||
ret = p.funcAddr(b, args)
|
||||
case llgoUnreachable: // func unreachable()
|
||||
b.Unreachable()
|
||||
case llgoCoAwait:
|
||||
ret = p.coAwait(b, args)
|
||||
case llgoCoSuspend:
|
||||
p.coSuspend(b, p.prog.BoolVal(false))
|
||||
case llgoCoDone:
|
||||
return p.coDone(b, args)
|
||||
case llgoCoResume:
|
||||
p.coResume(b, args)
|
||||
case llgoCoReturn:
|
||||
p.coReturn(b, args)
|
||||
case llgoCoYield:
|
||||
p.coYield(b, cv, args)
|
||||
case llgoCoRun:
|
||||
p.coRun(b, args)
|
||||
default:
|
||||
if ftype >= llgoAtomicOpBase && ftype <= llgoAtomicOpLast {
|
||||
ret = p.atomic(b, llssa.AtomicOp(ftype-llgoAtomicOpBase), args)
|
||||
|
||||
Reference in New Issue
Block a user