Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions cl/caller_tracking_precompute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//go:build !llgo

/*
* Copyright (c) 2026 The XGo Authors (xgo.dev). 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 (
"sync"
"testing"

gossa "golang.org/x/tools/go/ssa"
)

func TestCallerTrackingPrecomputeFreezesConcurrentReads(t *testing.T) {
var nilTracking *CallerTracking
nilTracking.Precompute(nil)
dep, root := buildCallerFrameSSAProgram(t,
"example.com/dep", `package dep
import "runtime"
func Where() { runtime.Caller(0) }
`,
"example.com/root", `package root
import "example.com/dep"
func Logs() { dep.Where() }
`)
tracking := NewCallerTracking()
tracking.Precompute([]*gossa.Package{root})
tracking.Precompute(nil)
if !tracking.frozen {
t.Fatal("CallerTracking was not frozen after precomputation")
}
if !runtimeCallerBaseSet(tracking, dep)[dep.Func("Where")] {
t.Fatal("precomputed base set lost runtime caller function")
}
if !runtimeCallerFuncSet(tracking, root)[root.Func("Logs")] {
t.Fatal("precomputed extended set lost cross-package caller")
}

var wg sync.WaitGroup
errs := make(chan struct{}, 32)
for range 32 {
wg.Add(1)
go func() {
defer wg.Done()
if !runtimeCallerBaseSet(tracking, dep)[dep.Func("Where")] ||
!runtimeCallerFuncSet(tracking, root)[root.Func("Logs")] {
errs <- struct{}{}
}
}()
}
wg.Wait()
close(errs)
if len(errs) != 0 {
t.Fatal("concurrent read lost precomputed caller tracking data")
}

delete(tracking.base, dep)
if got := runtimeCallerBaseSet(tracking, dep); got != nil {
t.Fatalf("frozen base lookup for unknown package = %v, want nil", got)
}
delete(tracking.extended, root)
if got := runtimeCallerFuncSet(tracking, root); got != nil {
t.Fatalf("frozen extended lookup for unknown package = %v, want nil", got)
}
}
56 changes: 53 additions & 3 deletions cl/instr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"os"
"regexp"
"sort"
"strings"

"golang.org/x/tools/go/ssa"
Expand Down Expand Up @@ -924,6 +925,9 @@ func runtimeCallerFuncSet(c *CallerTracking, pkg *ssa.Package) map[*ssa.Function
if set, ok := c.extended[pkg]; ok {
return set
}
if c.frozen {
return nil
}
base := runtimeCallerBaseSet(c, pkg)
out := make(map[*ssa.Function]bool, len(base))
for fn := range base {
Expand Down Expand Up @@ -977,12 +981,55 @@ func runtimeCallerFuncSet(c *CallerTracking, pkg *ssa.Package) map[*ssa.Function
// queries (criterion 2 below) hit the memoization. It must not outlive
// the compilation — the maps are keyed by *ssa.Package with
// *ssa.Function values, so anything longer-lived would pin every
// compiled package's go/types and go/ssa graphs. Plain maps are enough:
// packages of one compilation are compiled sequentially (the LLVM
// context is not thread-safe).
// compiled package's go/types and go/ssa graphs. Concurrent drivers call
// Precompute and share only the resulting frozen, read-only maps.
type CallerTracking struct {
base map[*ssa.Package]map[*ssa.Function]bool
extended map[*ssa.Package]map[*ssa.Function]bool
frozen bool
}

// Precompute resolves every caller-tracking query before backend workers
// start, then freezes the maps for concurrent read-only access.
func (c *CallerTracking) Precompute(pkgs []*ssa.Package) {
if c == nil || c.frozen {
return
}
all := make(map[*ssa.Package]bool)
for _, pkg := range pkgs {
if pkg == nil {
continue
}
all[pkg] = true
if pkg.Prog != nil {
for _, programPkg := range pkg.Prog.AllPackages() {
if programPkg != nil {
all[programPkg] = true
}
}
}
}
ordered := make([]*ssa.Package, 0, len(all))
for pkg := range all {
ordered = append(ordered, pkg)
}
sort.Slice(ordered, func(i, j int) bool {
left, right := "", ""
if ordered[i].Pkg != nil {
left = ordered[i].Pkg.Path()
}
if ordered[j].Pkg != nil {
right = ordered[j].Pkg.Path()
}
return left < right
})
for _, pkg := range ordered {
runtimeCallerBaseSet(c, pkg)
}
for _, pkg := range ordered {
runtimeCallerFuncSet(c, pkg)
}
c.frozen = true
}

// NewCallerTracking creates the caller-tracking memoization for one
Expand Down Expand Up @@ -1012,6 +1059,9 @@ func runtimeCallerBaseSet(c *CallerTracking, pkg *ssa.Package) map[*ssa.Function
if set, ok := c.base[pkg]; ok {
return set
}
if c.frozen {
return nil
}
set := computeRuntimeCallerBaseSet(pkg)
c.base[pkg] = set
return set
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/goplus/mod v0.21.1
github.com/mattn/go-tty v0.0.8
github.com/qiniu/x v1.18.0
github.com/xgo-dev/llvm v0.9.5
github.com/xgo-dev/llvm v0.9.6
github.com/xgo-dev/plan9asm v0.3.5
go.bug.st/serial v1.6.4
go.yaml.in/yaml/v3 v3.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/qiniu/x v1.18.0 h1:iMfc7Gqy1au+akr+Tl5Z40px7TR8VBLLkJsIeajKIbc=
github.com/qiniu/x v1.18.0/go.mod h1:Sx3Wy+0GI9OsX4a53mYj6A0o7mHJ94PUvraqGYb4EIs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xgo-dev/llvm v0.9.5 h1:RijI/6vGu7DGw5ldlaqvpK2uFcg9OXY9OXB/RmywtEk=
github.com/xgo-dev/llvm v0.9.5/go.mod h1:42vav2/cI5BAIcL543DZSMO9do8/aCK2z7JERH+AE+M=
github.com/xgo-dev/llvm v0.9.6 h1:DtjcgENgDItbf7LyUukssMwnnWXDpERSzGQ9jRQ1CRM=
github.com/xgo-dev/llvm v0.9.6/go.mod h1:42vav2/cI5BAIcL543DZSMO9do8/aCK2z7JERH+AE+M=
github.com/xgo-dev/plan9asm v0.3.5 h1:886BmpjMK6JfJ03VWA3nPK01jkVZA1a7/mZia3BOsdg=
github.com/xgo-dev/plan9asm v0.3.5/go.mod h1:0yM4CCIp2PyT8h+Ro3Ukro3lHL8ji9mzHEv5yfhOckc=
go.bug.st/serial v1.6.4 h1:7FmqNPgVp3pu2Jz5PoPtbZ9jJO5gnEnZIvnI1lzve8A=
Expand Down
89 changes: 89 additions & 0 deletions internal/build/backend_program_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2026 The XGo Authors (xgo.dev). 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 build

import (
"go/types"
"testing"

"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/packages"
llssa "github.com/goplus/llgo/ssa"
)

func TestBackendProgramTemplateCreatesIsolatedSessions(t *testing.T) {
conf := &Config{Goos: "linux", Goarch: "amd64"}
template := newBackendProgramTemplate(
&llssa.Target{GOOS: conf.Goos, GOARCH: conf.Goarch},
conf,
true,
true,
)
first, err := template.newSession()
if err != nil {
t.Fatal(err)
}
defer first.prog.Dispose()
second, err := template.newSession()
if err != nil {
t.Fatal(err)
}
defer second.prog.Dispose()
if first.transformer == nil || second.transformer == nil {
t.Fatal("backend session missing C ABI transformer")
}
if !first.prog.FuncInfoMetadataEnabled() || !first.prog.FuncInfoSitesEnabled() {
t.Fatal("backend template did not preserve funcinfo configuration")
}
firstModule := first.prog.NewPackage("first", "example.com/first").Module()
secondModule := second.prog.NewPackage("second", "example.com/second").Module()
if firstModule.Context().C == secondModule.Context().C {
t.Fatal("backend sessions share an LLVM context")
}
}

func TestBackendProgramTemplateOptionalState(t *testing.T) {
conf := &Config{Goos: "linux", Goarch: "amd64", DisableBoundsChecks: true, PthreadStackSize: 4096}
template := newBackendProgramTemplate(nil, conf, false, false)
template.typeSizes = &types.StdSizes{WordSize: 8, MaxAlign: 8}
template.runtimePackage = types.NewPackage(llssa.PkgRuntime, "runtime")
template.pythonPackage = types.NewPackage(llssa.PkgPython, "python")
prog := template.newProgram()
defer prog.Dispose()
if prog.Target() == nil {
t.Fatal("backend program created without a default target")
}

validTypes := types.NewPackage("example.com/valid", "valid")
valid := &packages.Package{PkgPath: "example.com/valid", Types: validTypes}
duplicate := &packages.Package{PkgPath: "example.com/duplicate", Types: validTypes}
missingTypes := &packages.Package{PkgPath: "example.com/missing"}
illTyped := &packages.Package{PkgPath: "example.com/ill", Types: types.NewPackage("example.com/ill", "ill"), IllTyped: true}
inputs := collectBackendProgramInputs([]*packages.Package{missingTypes, illTyped, valid, duplicate})
if len(inputs) != 1 || inputs[0].pkg != validTypes {
t.Fatalf("backend inputs = %#v, want one deduplicated package", inputs)
}

patched := backendProgramTemplate{}
appendPatchedBackendInputs(&patched, cl.Patches{
"example.com/missing": {},
"example.com/other": {},
}, packages.NewDeduper())
if len(patched.inputs) != 0 {
t.Fatalf("missing patched packages produced inputs: %#v", patched.inputs)
}
}
Loading
Loading