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
114 changes: 114 additions & 0 deletions cl/caller_tracking_precompute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//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)
}
}

func TestNewPackageCallerTrackingMatchesWholeProgramPrecompute(t *testing.T) {
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() }
`)

whole := NewCallerTracking()
whole.Precompute([]*gossa.Package{root})
local := NewPackageCallerTracking(
root,
SummarizeCallerTracking(dep),
SummarizeCallerTracking(root),
)
if !local.frozen {
t.Fatal("package caller tracking was not frozen")
}
if got, want := local.base[dep][dep.Func("Where")], whole.base[dep][dep.Func("Where")]; got != want {
t.Fatalf("dependency base tracking = %v, want %v", got, want)
}
if got, want := local.extended[root][root.Func("Logs")], whole.extended[root][root.Func("Logs")]; got != want {
t.Fatalf("root extended tracking = %v, want %v", got, want)
}
if len(local.extended) != 1 {
t.Fatalf("package snapshot computed %d extended package sets, want 1", len(local.extended))
}
if len(local.base) != 2 {
t.Fatalf("package snapshot retained %d base package sets, want 2", len(local.base))
}
}
24 changes: 22 additions & 2 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2084,11 +2084,27 @@ func (p *context) compileValues(b llssa.Builder, vals []ssa.Value, hasVArg int)
type Patch struct {
Alt *ssa.Package
Types *types.Package

skips map[string]none
skipall bool
prepared bool
}

// Patches is patches of some packages.
type Patches = map[string]Patch

// PreparePatch creates the immutable merged type view imported by packages
// compiled before the patched package's own LLVM backend runs.
func PreparePatch(patch Patch, original *types.Package, files []*ast.File) Patch {
if patch.prepared || patch.Types == nil || original == nil {
return patch
}
patch.skips, patch.skipall = collectPatchSkips(files)
typepatch.MergePrepared(patch.Types, original, patch.skips, patch.skipall)
patch.prepared = true
return patch
}

// NewPackage compiles a Go package to LLVM IR package.
// Deprecated: use NewPackageExWithEmbedMetaOptions with explicit Options.
func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, err error) {
Expand Down Expand Up @@ -2212,6 +2228,10 @@ func newPackageEx(prog llssa.Program, ct *CallerTracking, patches Patches, rewri

if hasPatch {
skips := ctx.skips
if patch.prepared {
skips = patch.skips
ctx.skipall = patch.skipall
}
typepatch.Merge(pkgTypes, oldTypes, skips, ctx.skipall)
ctx.skips = nil
ctx.state = pkgInPatch
Expand Down Expand Up @@ -2367,8 +2387,8 @@ func (p *context) _patchType(typ types.Type) (types.Type, bool) {
return t, true
}
o := typ.Obj()
if pkg := o.Pkg(); typepatch.IsPatched(pkg) {
if patch, ok := p.patches[pkg.Path()]; ok {
if pkg := o.Pkg(); pkg != nil {
if patch, ok := p.patches[pkg.Path()]; ok && patch.Types != nil {
if obj := patch.Types.Scope().Lookup(o.Name()); obj != nil {
raw := p.prog.Type(instantiate(obj.Type(), typ), llssa.InGo).RawType()
return raw, typ != raw
Expand Down
38 changes: 34 additions & 4 deletions cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ func (p *context) importPkg(pkg *types.Package, i *pkgInfo) {
kind, _ := pkgKindByScope(scope)
if kind == PkgNormal {
if patch, ok := p.patches[pkgPath]; ok {
pkg = patch.Alt.Pkg
switch {
case patch.Types != nil:
pkg = patch.Types
case patch.Alt != nil:
pkg = patch.Alt.Pkg
}
scope = pkg.Scope()
if kind, _ = pkgKindByScope(scope); kind != PkgNormal {
goto start
Expand Down Expand Up @@ -220,6 +225,27 @@ func (p *context) initFiles(pkgPath string, files []*ast.File, cPkg bool) {
}
}

func collectPatchSkips(files []*ast.File) (map[string]none, bool) {
ctx := &context{skips: make(map[string]none)}
for _, file := range files {
for _, decl := range file.Decls {
gen, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
switch gen.Tok {
case token.CONST, token.TYPE:
ctx.collectSkipNamesByDoc(gen.Doc)
case token.IMPORT:
if gen.Doc != nil && len(gen.Doc.List) != 0 {
ctx.collectSkipNames(gen.Doc.List[len(gen.Doc.List)-1].Text)
}
}
}
}
return ctx.skips, ctx.skipall
}

// Collect skip names and skip other annotations, such as go: and llgo:
// llgo:skip symbol1 symbol2 ...
// llgo:skipall
Expand Down Expand Up @@ -276,7 +302,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 +312,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 +800,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
Loading
Loading