Skip to content
Merged
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
61 changes: 61 additions & 0 deletions cl/caller_tracking_precompute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//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 TestCallerTrackingPrecomputeSupportsConcurrentReads(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{dep, root})
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
for range 32 {
wg.Add(1)
go func() {
defer wg.Done()
if !runtimeCallerBaseSet(tracking, dep)[dep.Func("Where")] ||
!runtimeCallerFuncSet(tracking, root)[root.Func("Logs")] {
t.Error("concurrent read lost precomputed caller tracking data")
}
}()
}
wg.Wait()
}
9 changes: 7 additions & 2 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type Options struct {
Trace bool
ExportRename bool
ShadowStack bool
// PreloadedSyntax means all Program-side source metadata was collected
// before lowering and is now shared read-only by backend Programs.
PreloadedSyntax bool
}

func legacyOptions() Options {
Expand Down Expand Up @@ -2231,8 +2234,10 @@ func newPackageEx(prog llssa.Program, ct *CallerTracking, patches Patches, rewri
pkg.Pkg = pkgTypes
patch.Alt.Pkg = pkgTypes
}
if err = ParsePkgSyntax(prog, pkgProg.Fset, pkgTypes, files); err != nil {
return nil, nil, err
if !options.PreloadedSyntax {
if err = ParsePkgSyntaxWithOptions(prog, pkgProg.Fset, pkgTypes, files, options); err != nil {
return nil, nil, err
}
}
if err = prog.ValidateLocalitiesFor(pkgTypes); err != nil {
return nil, nil, err
Expand Down
59 changes: 53 additions & 6 deletions cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func (p *context) importPkg(pkg *types.Package, i *pkgInfo) {
}
start:
i.kind = kind
if p.frontendOptions().PreloadedSyntax {
return
}
fset := p.fset
names := scope.Names()
syms := newPkgSymInfo()
Expand Down Expand Up @@ -177,11 +180,18 @@ start:
}

func (p *context) initFiles(pkgPath string, files []*ast.File, cPkg bool) {
preloaded := p.frontendOptions().PreloadedSyntax
for _, file := range files {
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
fullName, inPkgName := astFuncName(pkgPath, decl)
if preloaded {
if exportName, ok := p.prog.PackageExport(fullName); ok {
p.pkg.SetExport(fullName, exportName)
}
continue
}
p.processNoInterfaceByDoc(decl.Doc, fullName)
if !p.processLinknameByDoc(decl.Doc, fullName, inPkgName, false, true) && cPkg {
// package C (https://github.com/goplus/llgo/issues/1165)
Expand All @@ -197,7 +207,14 @@ func (p *context) initFiles(pkgPath string, files []*ast.File, cPkg bool) {
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 preloaded {
if exportName, ok := p.prog.PackageExport(fullName); ok {
p.pkg.SetExport(fullName, exportName)
}
} else {
p.processLinknameByDoc(decl.Doc, fullName, inPkgName, true, true)
}
}
}
case token.CONST:
Expand Down Expand Up @@ -279,6 +296,10 @@ 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) {
_, _ = collectDeclarationDirectivesWithOptions(prog, fset, doc, fullName, inPkgName, funcPos, legacyOptions())
}

func collectDeclarationDirectivesWithOptions(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 +315,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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equivalence check vs the old initLink export path holds for the common single-token //export name form (Args is the export name; matches SetLinkname+SetExport). One edge case to confirm: the legacy initLink treated //export args by splitting on the first space, whereas directive.Parse puts everything after //export into item.Args verbatim. A two-token //export foo bar directive would now flow through as item.Args == "foo bar" rather than name/link split. Unusual for //export, but worth a line of test or comment if that form is meant to be supported.

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 +334,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 +798,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 +825,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 := collectDeclarationDirectivesWithOptions(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 := collectDeclarationDirectivesWithOptions(prog, fset, decl.Doc, pkgPath+"."+inPkgName, inPkgName, token.NoPos, options); err != nil {
return err
}
}
}
vars, err := locality.ScanPackageVar(fset, decl)
Expand Down
24 changes: 21 additions & 3 deletions cl/instr.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,14 +980,32 @@ 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 before workers start and then share the plain maps read-only.
type CallerTracking struct {
base map[*ssa.Package]map[*ssa.Function]bool
extended map[*ssa.Package]map[*ssa.Function]bool
}

// Precompute resolves caller-tracking data before package backends start.
// Once it returns, callers may share c for concurrent read-only lookups as long
// as pkgs contains every package that can be passed to this compilation.
func (c *CallerTracking) Precompute(pkgs []*ssa.Package) {
if c == nil {
return
}
for _, pkg := range pkgs {
if pkg != nil {
runtimeCallerBaseSet(c, pkg)
}
}
for _, pkg := range pkgs {
if pkg != nil {
runtimeCallerFuncSet(c, pkg)
}
}
}

// NewCallerTracking creates the caller-tracking memoization for one
// compilation.
func NewCallerTracking() *CallerTracking {
Expand Down
82 changes: 82 additions & 0 deletions cl/preloaded_syntax_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//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 (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"testing"

"github.com/goplus/llgo/internal/goembed"
"github.com/goplus/llgo/ssa/ssatest"
"golang.org/x/tools/go/ssa"
)

func TestPreloadedSyntaxFeedsBackendWithoutLateDiscovery(t *testing.T) {
const source = `package C

//export callback
func Callback() {}

func XDefault() {}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "preloaded.go", source, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
files := []*ast.File{file}
info := newLocalityTypeInfo()
imp := importer.Default()
pkg, err := (&types.Config{Importer: imp}).Check("example.com/C", fset, files, info)
if err != nil {
t.Fatal(err)
}
coordinator := ssatest.NewProgramEx(t, nil, imp)
if err := ParsePkgSyntaxWithOptions(coordinator, fset, pkg, files, Options{ExportRename: true}); err != nil {
t.Fatal(err)
}
backend := coordinator.NewBackendProgram()
defer backend.Dispose()

goProg := ssa.NewProgram(fset, ssa.SanityCheckFunctions)
ssaPkg := goProg.CreatePackage(pkg, files, info, true)
ssaPkg.Build()
compiled, _, err := NewPackageExWithEmbedMetaOptions(
backend, nil, nil, nil, ssaPkg, files, goembed.VarMap{}, false,
Options{ExportRename: true, PreloadedSyntax: true},
)
if err != nil {
t.Fatal(err)
}
for fullName, want := range map[string]string{
"example.com/C.Callback": "callback",
"example.com/C.XDefault": "Default",
} {
if link, ok := backend.Linkname(fullName); !ok || link != want {
t.Errorf("Linkname(%q) = (%q, %v), want (%q, true)", fullName, link, ok, want)
}
if export, ok := compiled.ExportFuncs()[fullName]; !ok || export != want {
t.Errorf("ExportFuncs()[%q] = (%q, %v), want (%q, true)", fullName, export, ok, want)
}
}
}
48 changes: 48 additions & 0 deletions internal/build/backend_program_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 (
"runtime"
"testing"

llssa "github.com/goplus/llgo/ssa"
)

func TestNewBackendSessionCreatesIndependentLLVMState(t *testing.T) {
coordinator := llssa.NewProgram(&llssa.Target{GOOS: runtime.GOOS, GOARCH: runtime.GOARCH})
defer coordinator.Dispose()
ctx := &context{
prog: coordinator,
buildConf: &Config{
Goos: runtime.GOOS,
Goarch: runtime.GOARCH,
},
}
first := ctx.newBackendSession()
defer first.prog.Dispose()
second := ctx.newBackendSession()
defer second.prog.Dispose()
if first.transformer == nil || second.transformer == nil {
t.Fatal("backend session missing C ABI transformer")
}
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")
}
}
Loading
Loading