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
66 changes: 66 additions & 0 deletions cl/caller_tracking_precompute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//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) {
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})
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")
}
}
47 changes: 47 additions & 0 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 @@ -983,6 +987,7 @@ func runtimeCallerFuncSet(c *CallerTracking, pkg *ssa.Package) map[*ssa.Function
type CallerTracking struct {
base map[*ssa.Package]map[*ssa.Function]bool
extended map[*ssa.Package]map[*ssa.Function]bool
frozen bool
}

// NewCallerTracking creates the caller-tracking memoization for one
Expand All @@ -994,6 +999,45 @@ func NewCallerTracking() *CallerTracking {
}
}

// Precompute resolves caller-tracking state for every package in the supplied
// SSA programs, then freezes the memoization for concurrent reads. Drivers
// must call it after SSA Build and before compiling packages on independent
// LLVM contexts. A frozen tracker deliberately returns no data for an unknown
// package instead of mutating its maps from a worker.
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 {
return llssa.PathOf(ordered[i].Pkg) < llssa.PathOf(ordered[j].Pkg)
})
for _, pkg := range ordered {
runtimeCallerBaseSet(c, pkg)
}
for _, pkg := range ordered {
runtimeCallerFuncSet(c, pkg)
}
c.frozen = true
}

func isProgramUniqueFrame(pkg *ssa.Package, fn *ssa.Function) bool {
if fn == nil || fn.Parent() != nil {
return false
Expand All @@ -1012,6 +1056,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