-
Notifications
You must be signed in to change notification settings - Fork 49
build: prepare read-only package backend state #2282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xushiwei
merged 3 commits into
xgo-dev:main
from
zhouguangyuan0718:agent/parallel-build-pr6-overlay
Aug 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
initLinkexport path holds for the common single-token//export nameform (Args is the export name; matchesSetLinkname+SetExport). One edge case to confirm: the legacyinitLinktreated//exportargs by splitting on the first space, whereasdirective.Parseputs everything after//exportintoitem.Argsverbatim. A two-token//export foo bardirective would now flow through asitem.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.