From 98f3e45c0a998029d93d636d7a02293467b18d7d Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 17 Jun 2024 18:32:58 +0800 Subject: [PATCH 1/3] cl: compileFuncDecl/funcName fix; patch library: sync --- c/pthread/sync/_pthd/pthd.c | 9 +++++---- c/pthread/sync/sync.go | 11 +++++++---- cl/_testlibgo/sync/in.go | 18 ++++++++++++++++++ cl/compile.go | 3 +++ cl/import.go | 10 ++++++++-- internal/lib/sync/sync.go | 36 +++++++++++++++++++++++++++++++++++- 6 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 cl/_testlibgo/sync/in.go diff --git a/c/pthread/sync/_pthd/pthd.c b/c/pthread/sync/_pthd/pthd.c index 453bbe4d..e8275a54 100644 --- a/c/pthread/sync/_pthd/pthd.c +++ b/c/pthread/sync/_pthd/pthd.c @@ -1,6 +1,7 @@ #include -pthread_once_t llgoSyncOnceInitVal() { - pthread_once_t initVal = PTHREAD_ONCE_INIT; - return initVal; -} +// ----------------------------------------------------------------------------- + +pthread_once_t llgoSyncOnceInitVal = PTHREAD_ONCE_INIT; + +// ----------------------------------------------------------------------------- diff --git a/c/pthread/sync/sync.go b/c/pthread/sync/sync.go index 2ddf2ee9..9fd3dd03 100644 --- a/c/pthread/sync/sync.go +++ b/c/pthread/sync/sync.go @@ -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 } + +// ----------------------------------------------------------------------------- diff --git a/cl/_testlibgo/sync/in.go b/cl/_testlibgo/sync/in.go new file mode 100644 index 00000000..1442dbbd --- /dev/null +++ b/cl/_testlibgo/sync/in.go @@ -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") +} diff --git a/cl/compile.go b/cl/compile.go index ec18c06f..dbfe8fa1 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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 } diff --git a/cl/import.go b/cl/import.go index fbbf4408..fc1bc46d 100644 --- a/cl/import.go +++ b/cl/import.go @@ -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 { diff --git a/internal/lib/sync/sync.go b/internal/lib/sync/sync.go index 56822964..63f22d78 100644 --- a/internal/lib/sync/sync.go +++ b/internal/lib/sync/sync.go @@ -18,5 +18,39 @@ 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 + } + (*sync.Once)(o).Do(func() { + ptr := onceParam.Get() + (*(*func())(ptr))() + c.Free(ptr) + }) +} + +// ----------------------------------------------------------------------------- From 592500cb0c7377ac72b59d18a80ff14f5533e16c Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 17 Jun 2024 18:58:01 +0800 Subject: [PATCH 2/3] build: patch library fix (link dependencies) --- internal/build/build.go | 39 ++++++++++++++++++++------------------- internal/packages/load.go | 5 +---- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index e1ed2f73..9aa89734 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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 = 5 - 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" @@ -329,8 +327,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 { @@ -417,6 +415,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):] @@ -444,7 +445,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 diff --git a/internal/packages/load.go b/internal/packages/load.go index 788830ea..05aaf61b 100644 --- a/internal/packages/load.go +++ b/internal/packages/load.go @@ -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) } From 6442279a4461c8ea650fc73ebf1bacb4e293ca2f Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 17 Jun 2024 19:30:59 +0800 Subject: [PATCH 3/3] testlibgo: sync (to do) --- cl/_testgo/equal/out.ll | 36 ++++++++++++------------ cl/_testgo/goroutine/out.ll | 8 +++--- cl/_testlibgo/sync/out.ll | 1 + cl/_testrt/builtin/out.ll | 46 +++++++++++++++--------------- cl/_testrt/closure/out.ll | 20 ++++++------- cl/_testrt/eface/out.ll | 10 +++---- cl/_testrt/intgen/out.ll | 26 ++++++++--------- cl/_testrt/named/out.ll | 8 +++--- cl/_testrt/qsort/out.ll | 12 ++++---- cl/_testrt/result/out.ll | 56 ++++++++++++++++++------------------- internal/lib/sync/sync.go | 5 +++- 11 files changed, 116 insertions(+), 112 deletions(-) create mode 100644 cl/_testlibgo/sync/out.ll diff --git a/cl/_testgo/equal/out.ll b/cl/_testgo/equal/out.ll index fec88027..fdd5222f 100644 --- a/cl/_testgo/equal/out.ll +++ b/cl/_testgo/equal/out.ll @@ -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) diff --git a/cl/_testgo/goroutine/out.ll b/cl/_testgo/goroutine/out.ll index 87077df0..ef8f5b75 100644 --- a/cl/_testgo/goroutine/out.ll +++ b/cl/_testgo/goroutine/out.ll @@ -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) diff --git a/cl/_testlibgo/sync/out.ll b/cl/_testlibgo/sync/out.ll new file mode 100644 index 00000000..1c8a0e79 --- /dev/null +++ b/cl/_testlibgo/sync/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/cl/_testrt/builtin/out.ll b/cl/_testrt/builtin/out.ll index 858d32ef..74b754b6 100644 --- a/cl/_testrt/builtin/out.ll +++ b/cl/_testrt/builtin/out.ll @@ -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) diff --git a/cl/_testrt/closure/out.ll b/cl/_testrt/closure/out.ll index 326344f3..88e47a5c 100644 --- a/cl/_testrt/closure/out.ll +++ b/cl/_testrt/closure/out.ll @@ -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, ...) diff --git a/cl/_testrt/eface/out.ll b/cl/_testrt/eface/out.ll index d24c6978..2b0e6229 100644 --- a/cl/_testrt/eface/out.ll +++ b/cl/_testrt/eface/out.ll @@ -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) diff --git a/cl/_testrt/intgen/out.ll b/cl/_testrt/intgen/out.ll index 6f36fcc2..4eaa98d6 100644 --- a/cl/_testrt/intgen/out.ll +++ b/cl/_testrt/intgen/out.ll @@ -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) { diff --git a/cl/_testrt/named/out.ll b/cl/_testrt/named/out.ll index 337619ac..a9c450d2 100644 --- a/cl/_testrt/named/out.ll +++ b/cl/_testrt/named/out.ll @@ -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, ...) diff --git a/cl/_testrt/qsort/out.ll b/cl/_testrt/qsort/out.ll index 2d507d3a..03a2fe69 100644 --- a/cl/_testrt/qsort/out.ll +++ b/cl/_testrt/qsort/out.ll @@ -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, ...) diff --git a/cl/_testrt/result/out.ll b/cl/_testrt/result/out.ll index dde63bee..3866d8ec 100644 --- a/cl/_testrt/result/out.ll +++ b/cl/_testrt/result/out.ll @@ -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) diff --git a/internal/lib/sync/sync.go b/internal/lib/sync/sync.go index 63f22d78..35ad736b 100644 --- a/internal/lib/sync/sync.go +++ b/internal/lib/sync/sync.go @@ -46,11 +46,14 @@ func (o *Once) Do(f func()) { if *(*c.Long)(unsafe.Pointer(o)) == 0 { // try init *(*sync.Once)(o) = sync.OnceInit } - (*sync.Once)(o).Do(func() { + 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 + // -----------------------------------------------------------------------------