Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aff2665
build: split package compilation stages
zhouguangyuan0718 Jul 30, 2026
36de60c
test: cover package build stage boundaries
zhouguangyuan0718 Jul 30, 2026
0be4014
test: cover empty package dependencies
zhouguangyuan0718 Jul 30, 2026
d84ca29
test: cover cgo command environment parsing
zhouguangyuan0718 Jul 30, 2026
6d29a37
test: cover cgo pkg-config invocation
zhouguangyuan0718 Jul 30, 2026
d24eb12
test: cover cgo pkg-config failures
zhouguangyuan0718 Jul 31, 2026
e67a384
build: archive LLVM package objects in memory
zhouguangyuan0718 Jul 31, 2026
dcf4cca
test: cover package stage and archive branches
zhouguangyuan0718 Aug 1, 2026
7b39688
test: cover package pipeline error paths
zhouguangyuan0718 Aug 2, 2026
39ffae9
build: update LLVM bindings to v0.9.6
zhouguangyuan0718 Aug 2, 2026
9a16a20
build: snapshot package linker state
zhouguangyuan0718 Jul 30, 2026
c72f759
test: cover package summary boundaries
zhouguangyuan0718 Aug 1, 2026
160c3d0
build: isolate package backend sessions
zhouguangyuan0718 Jul 30, 2026
98064d1
build: run package backends in parallel
zhouguangyuan0718 Jul 30, 2026
b4e99f7
test: cover parallel backend task setup
zhouguangyuan0718 Jul 30, 2026
cb9cd95
test: cover package scheduling boundaries
zhouguangyuan0718 Jul 30, 2026
7efcdd3
test: cover Plan9 asm package selection
zhouguangyuan0718 Jul 30, 2026
322a330
build: replay locality state in backend sessions
zhouguangyuan0718 Jul 31, 2026
32a5aa8
build: publish archives in backend workers
zhouguangyuan0718 Jul 31, 2026
aea47f1
test: cover parallel environment boundaries
zhouguangyuan0718 Aug 1, 2026
a0f73b1
build: retain isolated programs through method DCE
zhouguangyuan0718 Aug 2, 2026
0d38bb4
test: cover concurrent deadcode worker contexts
zhouguangyuan0718 Aug 2, 2026
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)
}
}
10 changes: 7 additions & 3 deletions cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (p *context) collectSkip(line string, prefix int) {
}
}

func collectLinknameByDoc(prog llssa.Program, doc *ast.CommentGroup, fullName, inPkgName string) {
func collectLinknameByDoc(prog llssa.Program, doc *ast.CommentGroup, fullName, inPkgName string) bool {
directives := directive.ParseGroup(doc)
for n := len(directives) - 1; n >= 0; n-- {
directive := directives[n]
Expand All @@ -286,9 +286,10 @@ func collectLinknameByDoc(prog llssa.Program, doc *ast.CommentGroup, fullName, i
fields := strings.Fields(directive.Args)
if len(fields) >= 2 && fields[0] == inPkgName {
prog.SetLinkname(fullName, strings.Join(fields[1:], " "))
return
return true
}
}
return false
}

func (p *context) processLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar, allowExport bool) bool {
Expand Down Expand Up @@ -773,7 +774,10 @@ func ParsePkgSyntax(prog llssa.Program, fset *token.FileSet, pkg *types.Package,
return err
}
fullName, inPkgName := astFuncName(pkgPath, decl)
collectLinknameByDoc(prog, decl.Doc, fullName, inPkgName)
hasLinkname := collectLinknameByDoc(prog, decl.Doc, fullName, inPkgName)
if !hasLinkname && pkg.Name() == "C" && decl.Recv == nil && token.IsExported(inPkgName) {
prog.SetLinkname(fullName, strings.TrimPrefix(inPkgName, "X"))
}
ctx.processNoInterfaceByDoc(decl.Doc, fullName)
case *ast.GenDecl:
if decl.Tok == token.VAR {
Expand Down
39 changes: 39 additions & 0 deletions cl/import_coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,45 @@ func TestParsePkgSyntaxCollectsLinknames(t *testing.T) {
}
}

func TestParsePkgSyntaxCollectsCPackageExports(t *testing.T) {
const src = `package C

func Xadd(a, b int) int { return a + b }
func Double(x float64) float64 { return 2 * x }
func hidden() {}

//go:linkname Xnamed explicit_name
func Xnamed()
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "c.go", src, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
prog := llssa.NewProgram(nil)
pkg := types.NewPackage("example.com/c", "C")
if err := ParsePkgSyntax(prog, fset, pkg, []*ast.File{file}); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
want string
ok bool
}{
{name: "Xadd", want: "add", ok: true},
{name: "Double", want: "Double", ok: true},
{name: "hidden"},
{name: "Xnamed", want: "explicit_name", ok: true},
}
for _, tt := range tests {
fullName := pkg.Path() + "." + tt.name
got, ok := prog.Linkname(fullName)
if got != tt.want || ok != tt.ok {
t.Errorf("Linkname(%q) = (%q, %v), want (%q, %v)", fullName, got, ok, tt.want, tt.ok)
}
}
}

func TestCollectLinknameByDocIgnoresOtherDirectives(t *testing.T) {
prog := llssa.NewProgram(nil)
doc := &ast.CommentGroup{List: []*ast.Comment{
Expand Down
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
Loading
Loading