Merge pull request #770 from visualfc/tpfunc

ssa: fix llgo:type c for typeparam named
This commit is contained in:
xushiwei
2024-09-06 06:51:30 +08:00
committed by GitHub
5 changed files with 120 additions and 6 deletions

26
cl/_testrt/tpfunc/in.go Normal file
View File

@@ -0,0 +1,26 @@
package main
import (
"unsafe"
)
type Func func(*int)
//llgo:type C
type CFunc func(*int)
//llgo:type C
type Callback[T any] func(*T)
func main() {
var fn1 Func = func(v *int) {
println(*v)
}
var fn2 CFunc = func(v *int) {
println(*v)
}
var fn3 Callback[int] = func(v *int) {
println(*v)
}
println(unsafe.Sizeof(fn1), unsafe.Sizeof(fn2), unsafe.Sizeof(fn3))
}

80
cl/_testrt/tpfunc/out.ll Normal file
View File

@@ -0,0 +1,80 @@
; ModuleID = 'main'
source_filename = "main"
%main.Func = type { ptr, ptr }
@"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = alloca %main.Func, align 8
%3 = getelementptr inbounds %main.Func, ptr %2, i32 0, i32 0
store ptr @"__llgo_stub.main.main$1", ptr %3, align 8
%4 = getelementptr inbounds %main.Func, ptr %2, i32 0, i32 1
store ptr null, ptr %4, align 8
%5 = load %main.Func, ptr %2, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 16)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 8)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 8)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret i32 0
}
define void @"main.main$1"(ptr %0) {
_llgo_0:
%1 = load i64, ptr %0, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"main.main$2"(ptr %0) {
_llgo_0:
%1 = load i64, ptr %0, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"main.main$3"(ptr %0) {
_llgo_0:
%1 = load i64, ptr %0, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
define linkonce void @"__llgo_stub.main.main$1"(ptr %0, ptr %1) {
_llgo_0:
tail call void @"main.main$1"(ptr %1)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)

View File

@@ -244,7 +244,7 @@ func (p Program) SetRuntime(runtime any) {
} }
func (p Program) SetTypeBackground(fullName string, bg Background) { func (p Program) SetTypeBackground(fullName string, bg Background) {
p.gocvt.typbg[fullName] = bg p.gocvt.typbg.Store(fullName, bg)
} }
func (p Program) SetLinkname(name, link string) { func (p Program) SetLinkname(name, link string) {

View File

@@ -125,7 +125,7 @@ func (p *goProgram) extraSize(typ types.Type, ptrSize int64) (ret int64) {
retry: retry:
switch t := typ.(type) { switch t := typ.(type) {
case *types.Named: case *types.Named:
if p.gocvt.typbg[t.String()] == InC { if v, ok := p.gocvt.typbg.Load(namedLinkname(t)); ok && v.(Background) == InC {
return 0 return 0
} }
typ = t.Underlying() typ = t.Underlying()

View File

@@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"go/token" "go/token"
"go/types" "go/types"
"sync"
"unsafe" "unsafe"
) )
@@ -27,13 +28,12 @@ import (
type goTypes struct { type goTypes struct {
typs map[unsafe.Pointer]unsafe.Pointer typs map[unsafe.Pointer]unsafe.Pointer
typbg map[string]Background typbg sync.Map
} }
func newGoTypes() goTypes { func newGoTypes() goTypes {
typs := make(map[unsafe.Pointer]unsafe.Pointer) typs := make(map[unsafe.Pointer]unsafe.Pointer)
typbk := make(map[string]Background) return goTypes{typs: typs}
return goTypes{typs, typbk}
} }
type Background int type Background int
@@ -95,7 +95,7 @@ func (p goTypes) cvtType(typ types.Type) (raw types.Type, cvt bool) {
case *types.Struct: case *types.Struct:
return p.cvtStruct(t) return p.cvtStruct(t)
case *types.Named: case *types.Named:
if p.typbg[t.String()] == InC { if v, ok := p.typbg.Load(namedLinkname(t)); ok && v.(Background) == InC {
break break
} }
return p.cvtNamed(t) return p.cvtNamed(t)
@@ -115,6 +115,14 @@ func (p goTypes) cvtType(typ types.Type) (raw types.Type, cvt bool) {
return typ, false return typ, false
} }
func namedLinkname(t *types.Named) string {
obj := t.Obj()
if obj.Pkg() != nil {
return obj.Pkg().Path() + "." + obj.Name()
}
return obj.Name()
}
func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) { func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) {
if v, ok := p.typs[unsafe.Pointer(t)]; ok { if v, ok := p.typs[unsafe.Pointer(t)]; ok {
raw = (*types.Named)(v) raw = (*types.Named)(v)