Merge pull request #343 from xushiwei/q

cl: compileFuncDecl/funcName fix; patch library: sync; build: patch library fix (link dependencies)
This commit is contained in:
xushiwei
2024-06-17 19:39:44 +08:00
committed by GitHub
18 changed files with 212 additions and 145 deletions

View File

@@ -1,6 +1,7 @@
#include <pthread.h>
pthread_once_t llgoSyncOnceInitVal() {
pthread_once_t initVal = PTHREAD_ONCE_INIT;
return initVal;
}
// -----------------------------------------------------------------------------
pthread_once_t llgoSyncOnceInitVal = PTHREAD_ONCE_INIT;
// -----------------------------------------------------------------------------

View File

@@ -30,12 +30,15 @@ const (
LLGoPackage = "link"
)
// -----------------------------------------------------------------------------
// Once is an object that will perform exactly one action.
type Once C.pthread_once_t
//go:linkname onceInitVal C.llgoSyncOnceInitVal
func onceInitVal() Once
var OnceInit = onceInitVal()
//go:linkname OnceInit llgoSyncOnceInitVal
var OnceInit Once
// llgo:link (*Once).Do C.pthread_once
func (o *Once) Do(f func()) c.Int { return 0 }
// -----------------------------------------------------------------------------

View File

@@ -98,6 +98,22 @@ _llgo_0:
ret void
}
define i64 @"main.init#1$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define void @"main.init#1$2"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i64, ptr %2, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"main.init#2"() {
_llgo_0:
call void @main.assert(i1 true)
@@ -780,21 +796,9 @@ declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplu
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.init#1$2"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i64, ptr %2, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
define i64 @"main.init#1$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64)
@@ -815,7 +819,3 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.Interface"(%"github.com/go
declare void @"github.com/goplus/llgo/internal/runtime.InitNamed"(ptr, %"github.com/goplus/llgo/internal/runtime.String", %"github.com/goplus/llgo/internal/runtime.String", ptr, %"github.com/goplus/llgo/internal/runtime.Slice", %"github.com/goplus/llgo/internal/runtime.Slice")
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)

View File

@@ -72,10 +72,6 @@ _llgo_3: ; preds = %_llgo_1, %_llgo_0
br i1 %22, label %_llgo_2, label %_llgo_1
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.main$1"(ptr %0, %"github.com/goplus/llgo/internal/runtime.String" %1) {
_llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %1)
@@ -86,6 +82,10 @@ _llgo_0:
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare ptr @malloc(i64)

18
cl/_testlibgo/sync/in.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"sync"
)
var once sync.Once
func f(s string) {
once.Do(func() {
println(s)
})
}
func main() {
f("Do once")
f("Do twice")
}

View File

@@ -0,0 +1 @@
;

View File

@@ -613,6 +613,29 @@ _llgo_3: ; preds = %_llgo_1
ret i32 0
}
define void @"main.main$1"() {
_llgo_0:
%0 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 0
store ptr @6, ptr %1, align 8
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 1
store i64 2, ptr %2, align 4
%3 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %0, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"main.main$2"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i64, ptr %2, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.init"()
@@ -664,31 +687,8 @@ declare void @"github.com/goplus/llgo/internal/runtime.PrintPointer"(ptr)
declare i64 @"github.com/goplus/llgo/internal/runtime.SliceCopy"(%"github.com/goplus/llgo/internal/runtime.Slice", ptr, i64, i64)
define void @"main.main$2"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i64, ptr %2, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
define void @"main.main$1"() {
_llgo_0:
%0 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 0
store ptr @6, ptr %1, align 8
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 1
store i64 2, ptr %2, align 4
%3 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %0, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.NewStringIter"(%"github.com/goplus/llgo/internal/runtime.String")
declare { i1, i64, i32 } @"github.com/goplus/llgo/internal/runtime.StringIterNext"(ptr)

View File

@@ -50,28 +50,18 @@ _llgo_0:
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
define void @"main.main$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = call i32 (ptr, ...) @printf(ptr @0, i64 %0, i64 %1)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define void @"main.main$2"(i64 %0, i64 %1) {
_llgo_0:
%2 = call i32 (ptr, ...) @printf(ptr @1, i64 %0, i64 %1)
ret void
}
define linkonce void @"__llgo_stub.main.main$2"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
tail call void @"main.main$2"(i64 %1, i64 %2)
ret void
}
define void @"main.main$3"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
@@ -83,6 +73,16 @@ _llgo_0:
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define linkonce void @"__llgo_stub.main.main$2"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
tail call void @"main.main$2"(i64 %1, i64 %2)
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare i32 @printf(ptr, ...)

View File

@@ -463,6 +463,11 @@ _llgo_0:
ret i32 0
}
define void @"main.main$1"() {
_llgo_0:
ret void
}
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
@@ -886,11 +891,6 @@ declare ptr @"github.com/goplus/llgo/internal/runtime.ArrayOf"(i64, ptr)
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
define void @"main.main$1"() {
_llgo_0:
ret void
}
declare ptr @"github.com/goplus/llgo/internal/runtime.Func"(%"github.com/goplus/llgo/internal/runtime.Slice", %"github.com/goplus/llgo/internal/runtime.Slice", i1)
declare ptr @"github.com/goplus/llgo/internal/runtime.PointerTo"(ptr)

View File

@@ -170,6 +170,19 @@ _llgo_9: ; preds = %_llgo_7
ret i32 0
}
define i32 @"main.main$1"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i32, ptr %2, align 4
%4 = mul i32 %3, 2
%5 = extractvalue { ptr } %1, 0
store i32 %4, ptr %5, align 4
%6 = extractvalue { ptr } %1, 0
%7 = load i32, ptr %6, align 4
ret i32 %7
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
@@ -186,19 +199,6 @@ _llgo_0:
declare i32 @printf(ptr, ...)
define i32 @"main.main$1"(ptr %0) {
_llgo_0:
%1 = load { ptr }, ptr %0, align 8
%2 = extractvalue { ptr } %1, 0
%3 = load i32, ptr %2, align 4
%4 = mul i32 %3, 2
%5 = extractvalue { ptr } %1, 0
store i32 %4, ptr %5, align 4
%6 = extractvalue { ptr } %1, 0
%7 = load i32, ptr %6, align 4
ret i32 %7
}
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
define i32 @"main.next$bound"(ptr %0) {

View File

@@ -123,10 +123,6 @@ _llgo_0:
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
define i64 @"main.main$1"(ptr %0, i64 %1) {
_llgo_0:
%2 = load { ptr }, ptr %0, align 8
@@ -138,6 +134,10 @@ _llgo_0:
ret i64 %7
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
declare i32 @printf(ptr, ...)

View File

@@ -59,12 +59,6 @@ _llgo_3: ; preds = %_llgo_1
ret i32 0
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @qsort(ptr, i64, i64, ptr)
define i32 @"main.main$1"(ptr %0, ptr %1) {
_llgo_0:
%2 = load i64, ptr %0, align 4
@@ -74,6 +68,12 @@ _llgo_0:
ret i32 %5
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare void @qsort(ptr, i64, i64, ptr)
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
declare i32 @printf(ptr, ...)

View File

@@ -19,6 +19,12 @@ _llgo_0:
ret { ptr, ptr } %3
}
define i64 @"main.add$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define { { ptr, ptr }, i64 } @main.add2() {
_llgo_0:
%0 = alloca { ptr, ptr }, align 8
@@ -32,6 +38,12 @@ _llgo_0:
ret { { ptr, ptr }, i64 } %mrv1
}
define i64 @"main.add2$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
@@ -72,32 +84,6 @@ _llgo_0:
ret i32 0
}
define i64 @"main.add$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define linkonce i64 @"__llgo_stub.main.add$1"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
%3 = tail call i64 @"main.add$1"(i64 %1, i64 %2)
ret i64 %3
}
define i64 @"main.add2$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define linkonce i64 @"__llgo_stub.main.add2$1"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
%3 = tail call i64 @"main.add2$1"(i64 %1, i64 %2)
ret i64 %3
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
define { ptr, ptr } @"main.main$1"() {
_llgo_0:
%0 = alloca { ptr, ptr }, align 8
@@ -109,14 +95,28 @@ _llgo_0:
ret { ptr, ptr } %3
}
declare i32 @printf(ptr, ...)
define i64 @"main.main$1$1"(i64 %0, i64 %1) {
_llgo_0:
%2 = add i64 %0, %1
ret i64 %2
}
define linkonce i64 @"__llgo_stub.main.add$1"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
%3 = tail call i64 @"main.add$1"(i64 %1, i64 %2)
ret i64 %3
}
define linkonce i64 @"__llgo_stub.main.add2$1"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
%3 = tail call i64 @"main.add2$1"(i64 %1, i64 %2)
ret i64 %3
}
declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare i32 @printf(ptr, ...)
define linkonce i64 @"__llgo_stub.main.main$1$1"(ptr %0, i64 %1, i64 %2) {
_llgo_0:
%3 = tail call i64 @"main.main$1$1"(i64 %1, i64 %2)

View File

@@ -279,6 +279,9 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
}
b.EndBuild()
})
for _, af := range f.AnonFuncs {
p.compileFuncDecl(pkg, af)
}
}
return fn, nil, goFunc
}

View File

@@ -336,8 +336,14 @@ func typesFuncName(pkgPath string, fn *types.Func) (fullName, inPkgName string)
// - func: pkg.name
// - method: pkg.(T).name, pkg.(*T).name
func funcName(pkg *types.Package, fn *ssa.Function) string {
sig := fn.Signature
return llssa.FuncName(pkg, fn.Name(), sig.Recv())
var recv *types.Var
parent := fn.Parent()
if parent != nil { // closure in method
recv = parent.Signature.Recv()
} else {
recv = fn.Signature.Recv()
}
return llssa.FuncName(pkg, fn.Name(), recv)
}
func checkCgo(fnName string) bool {

View File

@@ -137,9 +137,9 @@ func Do(args []string, conf *Config) {
altPkgs, err := packages.LoadEx(dedup, sizes, cfg, altPkgPaths...)
check(err)
var needRt bool
noRt := 1
prog.SetRuntime(func() *types.Package {
needRt = true
noRt = 0
return altPkgs[0].Types
})
prog.SetPython(func() *types.Package {
@@ -153,25 +153,23 @@ func Do(args []string, conf *Config) {
ctx := &context{progSSA, prog, dedup, patches, make(map[string]none), mode, verbose}
pkgs := buildAllPkgs(ctx, initial)
var runtimeFiles []string
if needRt {
// TODO(xsw): maybe we need trace runtime sometimes
llssa.SetDebug(0)
cl.SetDebug(0)
// TODO(xsw): maybe we need trace runtime sometimes
llssa.SetDebug(0)
cl.SetDebug(0)
dpkg := buildAllPkgs(ctx, altPkgs[:1])
for _, pkg := range dpkg {
if !strings.HasSuffix(pkg.ExportFile, ".ll") {
continue
}
runtimeFiles = append(runtimeFiles, pkg.ExportFile)
var llFiles []string
dpkg := buildAllPkgs(ctx, altPkgs[noRt:])
for _, pkg := range dpkg {
if !strings.HasSuffix(pkg.ExportFile, ".ll") {
continue
}
llFiles = append(llFiles, pkg.ExportFile)
}
if mode != ModeBuild {
nErr := 0
for _, pkg := range initial {
if pkg.Name == "main" {
nErr += linkMainPkg(pkg, pkgs, runtimeFiles, conf, mode, verbose)
nErr += linkMainPkg(pkg, pkgs, llFiles, conf, mode, verbose)
}
}
if nErr > 0 {
@@ -285,7 +283,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package) (pkgs []*aPackage)
return
}
func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, runtimeFiles []string, conf *Config, mode Mode, verbose bool) (nErr int) {
func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf *Config, mode Mode, verbose bool) (nErr int) {
pkgPath := pkg.PkgPath
name := path.Base(pkgPath)
app := conf.OutFile
@@ -293,7 +291,7 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, runtimeFiles []string,
app = filepath.Join(conf.BinPath, name+conf.AppExt)
}
const N = 6
args := make([]string, N, len(pkg.Imports)+len(runtimeFiles)+(N+1))
args := make([]string, N, len(pkg.Imports)+len(llFiles)+(N+1))
args[0] = "-o"
args[1] = app
args[2] = "-Wno-override-module"
@@ -331,8 +329,8 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, runtimeFiles []string,
}
dirty := false
if needRuntime && runtimeFiles != nil {
for _, file := range runtimeFiles {
if needRuntime && llFiles != nil {
for _, file := range llFiles {
args = appendLinkFiles(args, file)
}
} else {
@@ -419,6 +417,9 @@ func altPkgs(initial []*packages.Package, alts ...string) []string {
func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package, verbose bool) {
packages.Visit(alts, nil, func(p *packages.Package) {
if p.Types != nil && !p.IllTyped {
if debugBuild || verbose {
log.Println("==> BuildSSA", p.PkgPath)
}
pkgSSA := prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true)
if strings.HasPrefix(p.PkgPath, altPkgPathPrefix) {
path := p.PkgPath[len(altPkgPathPrefix):]
@@ -446,7 +447,7 @@ func allPkgs(ctx *context, initial []*packages.Package) (all []*aPackage, errs [
packages.Visit(initial, nil, func(p *packages.Package) {
if p.Types != nil && !p.IllTyped {
pkgPath := p.PkgPath
if _, ok := built[pkgPath]; ok {
if _, ok := built[pkgPath]; ok || strings.HasPrefix(pkgPath, altPkgPathPrefix) {
return
}
var altPkg *packages.Cached

View File

@@ -18,5 +18,42 @@ package sync
// llgo:skipall
import (
_ "unsafe"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/pthread"
"github.com/goplus/llgo/c/pthread/sync"
)
const (
LLGoPackage = "link"
)
// -----------------------------------------------------------------------------
var onceParam pthread.Key
func init() {
onceParam.Create(nil)
}
type Once sync.Once
func (o *Once) Do(f func()) {
ptr := c.Malloc(unsafe.Sizeof(f))
*(*func())(ptr) = f
onceParam.Set(ptr)
if *(*c.Long)(unsafe.Pointer(o)) == 0 { // try init
*(*sync.Once)(o) = sync.OnceInit
}
onceDo(o, func() {
ptr := onceParam.Get()
(*(*func())(ptr))()
c.Free(ptr)
})
}
//go:linkname onceDo C.pthread_once
func onceDo(o *Once, f func()) c.Int
// -----------------------------------------------------------------------------

View File

@@ -121,9 +121,6 @@ func NewDeduper() Deduper {
func (p Deduper) Check(pkgPath string) *Cached {
if v, ok := p.cache.Load(pkgPath); ok {
if DebugPackagesLoad {
log.Println("==> dedup.check:", pkgPath)
}
return v.(*Cached)
}
return nil
@@ -131,7 +128,7 @@ func (p Deduper) Check(pkgPath string) *Cached {
func (p Deduper) set(pkgPath string, cp *Cached) {
if DebugPackagesLoad {
log.Println("==> dedup.set:", pkgPath)
log.Println("==> Import", pkgPath)
}
p.cache.Store(pkgPath, cp)
}