Skip to content
Closed
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)
}
}
57 changes: 40 additions & 17 deletions cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,25 @@ start:
syms.initLinknames(p)
}

func (p *context) initFiles(pkgPath string, files []*ast.File, cPkg bool) {
func (p *context) initFiles(pkgPath string, files []*ast.File, _ bool) {
for _, file := range files {
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
fullName, inPkgName := astFuncName(pkgPath, decl)
p.processNoInterfaceByDoc(decl.Doc, fullName)
if !p.processLinknameByDoc(decl.Doc, fullName, inPkgName, false, true) && cPkg {
// package C (https://github.com/goplus/llgo/issues/1165)
if decl.Recv == nil && token.IsExported(inPkgName) {
exportName := strings.TrimPrefix(inPkgName, "X")
p.prog.SetLinkname(fullName, exportName)
p.pkg.SetExport(fullName, exportName)
}
fullName, _ := astFuncName(pkgPath, decl)
if exportName, ok := p.prog.PackageExport(fullName); ok {
p.pkg.SetExport(fullName, exportName)
}
case *ast.GenDecl:
switch decl.Tok {
case token.VAR:
if len(decl.Specs) == 1 {
if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 {
inPkgName := names[0].Name
p.processLinknameByDoc(decl.Doc, pkgPath+"."+inPkgName, inPkgName, true, true)
fullName := pkgPath + "." + inPkgName
if exportName, ok := p.prog.PackageExport(fullName); ok {
p.pkg.SetExport(fullName, exportName)
}
}
}
case token.CONST:
Expand Down Expand Up @@ -278,7 +275,7 @@ func (p *context) collectSkip(line string, prefix int) {

// collectDeclarationDirectives caches source metadata needed after the syntax
// pass. funcPos is token.NoPos for non-function declarations.
func collectDeclarationDirectives(prog llssa.Program, fset *token.FileSet, doc *ast.CommentGroup, fullName, inPkgName string, funcPos token.Pos) {
func collectDeclarationDirectives(prog llssa.Program, fset *token.FileSet, doc *ast.CommentGroup, fullName, inPkgName string, funcPos token.Pos, options Options) (bool, error) {
directives := directive.ParseGroup(doc)
linkCollected := false
hasClosureEnv := false
Expand All @@ -294,6 +291,16 @@ func collectDeclarationDirectives(prog llssa.Program, fset *token.FileSet, doc *
prog.SetLinkname(fullName, strings.Join(fields[1:], " "))
linkCollected = true
}
case "export":
if linkCollected || item.Args == "" {
continue
}
if item.Args != inPkgName && !options.ExportRename {
return false, fmt.Errorf("export comment has wrong name %q", item.Args)
}
prog.SetLinkname(fullName, item.Args)
prog.SetPackageExport(fullName, item.Args)
linkCollected = true
case "llgo:env":
if funcPos.IsValid() {
hasClosureEnv = true
Expand All @@ -303,6 +310,7 @@ func collectDeclarationDirectives(prog llssa.Program, fset *token.FileSet, doc *
if hasClosureEnv {
prog.SetClosureEnvDirective(fset, fullName, funcPos)
}
return linkCollected, nil
}

func (p *context) processLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar, allowExport bool) bool {
Expand Down Expand Up @@ -766,16 +774,21 @@ func (p *context) initPyModule() {
}

// ParsePkgSyntax collects declaration directives in one syntax pass before SSA
// creation. Directives that need an LLVM package (such as //export) are applied
// later by initFiles.
// creation using the legacy frontend options.
func ParsePkgSyntax(prog llssa.Program, fset *token.FileSet, pkg *types.Package, files []*ast.File) error {
return ParsePkgSyntaxWithOptions(prog, fset, pkg, files, legacyOptions())
}

// ParsePkgSyntaxWithOptions collects all Program-side declaration metadata.
// LLVM Package effects such as preserving //export symbols are applied later.
func ParsePkgSyntaxWithOptions(prog llssa.Program, fset *token.FileSet, pkg *types.Package, files []*ast.File, options Options) error {
if pkg == nil {
return nil
}
if prog.PackageSyntaxParsed(pkg) {
return nil
}
ctx := &context{prog: prog}
ctx := &context{prog: prog, options: options, optionsSet: true}
pkgPath := llssa.PathOf(pkg)
for _, file := range files {
for _, decl := range file.Decls {
Expand All @@ -788,14 +801,24 @@ func ParsePkgSyntax(prog llssa.Program, fset *token.FileSet, pkg *types.Package,
return err
}
fullName, inPkgName := astFuncName(pkgPath, decl)
collectDeclarationDirectives(prog, fset, decl.Doc, fullName, inPkgName, decl.Pos())
hasLinkname, err := collectDeclarationDirectives(prog, fset, decl.Doc, fullName, inPkgName, decl.Pos(), options)
if err != nil {
return err
}
if !hasLinkname && pkg.Name() == "C" && decl.Recv == nil && token.IsExported(inPkgName) {
exportName := strings.TrimPrefix(inPkgName, "X")
prog.SetLinkname(fullName, exportName)
prog.SetPackageExport(fullName, exportName)
}
ctx.processNoInterfaceByDoc(decl.Doc, fullName)
case *ast.GenDecl:
if decl.Tok == token.VAR {
if len(decl.Specs) == 1 {
if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 {
inPkgName := names[0].Name
collectDeclarationDirectives(prog, fset, decl.Doc, pkgPath+"."+inPkgName, inPkgName, token.NoPos)
if _, err := collectDeclarationDirectives(prog, fset, decl.Doc, pkgPath+"."+inPkgName, inPkgName, token.NoPos, options); err != nil {
return err
}
}
}
vars, err := locality.ScanPackageVar(fset, decl)
Expand Down
52 changes: 50 additions & 2 deletions cl/import_coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,60 @@ func TestParsePkgSyntaxCollectsLinknames(t *testing.T) {
})
}
prog := llssa.NewProgram(nil)
collectDeclarationDirectives(prog, nil, &ast.CommentGroup{List: []*ast.Comment{{Text: "//go:linkname Other C.other"}}}, llssa.PkgRuntime+".Sigsetjmp", "Sigsetjmp", token.NoPos)
collectDeclarationDirectives(prog, nil, &ast.CommentGroup{List: []*ast.Comment{{Text: "//go:linkname Other C.other"}}}, llssa.PkgRuntime+".Sigsetjmp", "Sigsetjmp", token.NoPos, Options{})
if _, ok := prog.Linkname(llssa.PkgRuntime + ".Sigsetjmp"); ok {
t.Fatal("mismatched linkname was collected")
}
}

func TestParsePkgSyntaxCollectsExportsBeforeLowering(t *testing.T) {
tests := []struct {
name string
pkgName string
declaration string
exportRename bool
want string
wantErr string
}{
{name: "same name", pkgName: "p", declaration: "//export Entry\nfunc Entry() {}", want: "Entry"},
{name: "target rename", pkgName: "p", declaration: "//export irq_handler\nfunc Entry() {}", exportRename: true, want: "irq_handler"},
{name: "invalid rename", pkgName: "p", declaration: "//export irq_handler\nfunc Entry() {}", wantErr: "wrong name"},
{name: "C default", pkgName: "C", declaration: "func Xmalloc() {}", want: "malloc"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "p.go", "package "+test.pkgName+"\n"+test.declaration+"\n", parser.ParseComments)
if err != nil {
t.Fatal(err)
}
prog := llssa.NewProgram(nil)
defer prog.Dispose()
pkg := types.NewPackage("example.com/"+test.pkgName, test.pkgName)
err = ParsePkgSyntaxWithOptions(prog, fset, pkg, []*ast.File{file}, Options{ExportRename: test.exportRename})
if test.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
t.Fatalf("ParsePkgSyntaxWithOptions error = %v, want %q", err, test.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
fullName := pkg.Path() + ".Entry"
if test.pkgName == "C" {
fullName = pkg.Path() + ".Xmalloc"
}
if link, ok := prog.Linkname(fullName); !ok || link != test.want {
t.Fatalf("Linkname(%q) = (%q, %v), want (%q, true)", fullName, link, ok, test.want)
}
if export, ok := prog.PackageExport(fullName); !ok || export != test.want {
t.Fatalf("PackageExport(%q) = (%q, %v), want (%q, true)", fullName, export, ok, test.want)
}
})
}
}

func TestParsePkgSyntaxCollectsClosureEnvDirectives(t *testing.T) {
const src = `package p
//go:linkname env C.old
Expand Down Expand Up @@ -303,7 +351,7 @@ func TestCollectDeclarationDirectivesIgnoresOtherDirectives(t *testing.T) {
{Text: "//llgo:tls"},
}}
const fullName = "example.com/p.Value"
collectDeclarationDirectives(prog, nil, doc, fullName, "Value", token.NoPos)
collectDeclarationDirectives(prog, nil, doc, fullName, "Value", token.NoPos, Options{})
if _, ok := prog.Linkname(fullName); ok {
t.Fatal("non-link directives installed a linkname")
}
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 @@ -927,6 +928,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 @@ -980,12 +984,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 @@ -1015,6 +1062,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
Loading
Loading