-
Notifications
You must be signed in to change notification settings - Fork 49
build: parallelize cache preflight by dependency level #2178
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
Closed
zhouguangyuan0718
wants to merge
12
commits into
xgo-dev:main
from
zhouguangyuan0718:agent/parallel-build-pr4
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f6650e6
build: prepare bounded parallel compilation
zhouguangyuan0718 e111d99
build: address parallelism review feedback
zhouguangyuan0718 4520af8
build: scope LLVM verification errors locally
zhouguangyuan0718 6b50b79
build: bypass cache for cyclic alternate dependencies
zhouguangyuan0718 d5bf760
build: model package build units explicitly
zhouguangyuan0718 225a82b
test: use the llgo runtime path in build spec coverage
zhouguangyuan0718 7126afd
build: split package execution stages
zhouguangyuan0718 90046ef
build: plan package dependency levels
zhouguangyuan0718 cca97bd
build: identify package dependency cycles
zhouguangyuan0718 697eb43
build: exclude alternate imports from scheduler graph
zhouguangyuan0718 50f92ed
build: parallelize cache preflight by dependency level
zhouguangyuan0718 4782448
build: fix parallel preflight synchronization
zhouguangyuan0718 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,74 @@ | ||
| /* | ||
| * 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 ( | ||
| "sort" | ||
|
|
||
| "github.com/goplus/llgo/internal/packages" | ||
| ) | ||
|
|
||
| // effectiveDependencies returns the package graph that contributes code to an | ||
| // aPackage. An alternate package can add imports beyond the original package, | ||
| // so using only Package.Imports would allow stale cache entries after an alt | ||
| // dependency changes. | ||
| func effectiveDependencies(pkg *aPackage) []*packages.Package { | ||
| if pkg == nil || pkg.Package == nil { | ||
| return nil | ||
| } | ||
| deps := make(map[string]*packages.Package) | ||
| add := func(imports map[string]*packages.Package) { | ||
| for _, dep := range imports { | ||
| if dep == nil || dep.ID == pkg.ID || (pkg.AltPkg != nil && dep.ID == pkg.AltPkg.ID) { | ||
| continue | ||
| } | ||
| deps[dep.ID] = dep | ||
| } | ||
| } | ||
| add(pkg.Imports) | ||
| if pkg.AltPkg != nil { | ||
| add(pkg.AltPkg.Imports) | ||
| } | ||
| ret := make([]*packages.Package, 0, len(deps)) | ||
| for _, dep := range deps { | ||
| ret = append(ret, dep) | ||
| } | ||
| sort.Slice(ret, func(i, j int) bool { return ret[i].ID < ret[j].ID }) | ||
| return ret | ||
| } | ||
|
|
||
| // packageBuildDependencies returns the true Go import edges for scheduler | ||
| // ordering. Alternate-package imports still participate in cache fingerprints, | ||
| // but may intentionally form cycles with the runtime replacement graph after | ||
| // all packages have already been built into SSA. | ||
| func packageBuildDependencies(pkg *aPackage) []*packages.Package { | ||
| if pkg == nil || pkg.Package == nil { | ||
| return nil | ||
| } | ||
| deps := make(map[string]*packages.Package, len(pkg.Imports)) | ||
| for _, dep := range pkg.Imports { | ||
| if dep != nil && dep.ID != pkg.ID { | ||
| deps[dep.ID] = dep | ||
| } | ||
| } | ||
| ret := make([]*packages.Package, 0, len(deps)) | ||
| for _, dep := range deps { | ||
| ret = append(ret, dep) | ||
| } | ||
| sort.Slice(ret, func(i, j int) bool { return ret[i].ID < ret[j].ID }) | ||
| return ret | ||
| } |
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,64 @@ | ||
| /* | ||
| * 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 ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/goplus/llgo/internal/packages" | ||
| ) | ||
|
|
||
| func TestEffectiveDependenciesIncludesAlternateImports(t *testing.T) { | ||
| base := &packages.Package{ID: "base"} | ||
| shared := &packages.Package{ID: "shared"} | ||
| altOnly := &packages.Package{ID: "alt-only"} | ||
| alt := &packages.Package{ | ||
| ID: "alt", | ||
| Imports: map[string]*packages.Package{"shared": shared, "alt-only": altOnly}, | ||
| } | ||
| pkg := &aPackage{ | ||
| Package: &packages.Package{ID: "pkg", Imports: map[string]*packages.Package{"base": base, "shared": shared}}, | ||
| AltPkg: &packages.Cached{Package: alt}, | ||
| } | ||
| deps := effectiveDependencies(pkg) | ||
| got := make([]string, len(deps)) | ||
| for i, dep := range deps { | ||
| got[i] = dep.ID | ||
| } | ||
| if want := []string{"alt-only", "base", "shared"}; !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("effectiveDependencies = %v, want %v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestPackageBuildDependenciesExcludeAlternateImports(t *testing.T) { | ||
| base := &packages.Package{ID: "base"} | ||
| altOnly := &packages.Package{ID: "alt-only"} | ||
| alt := &packages.Package{ID: "patch/pkg", Imports: map[string]*packages.Package{"alt-only": altOnly}} | ||
| pkg := &aPackage{ | ||
| Package: &packages.Package{ID: "pkg", Imports: map[string]*packages.Package{"base": base}}, | ||
| AltPkg: &packages.Cached{Package: alt}, | ||
| } | ||
| deps := packageBuildDependencies(pkg) | ||
| got := make([]string, len(deps)) | ||
| for i, dep := range deps { | ||
| got[i] = dep.ID | ||
| } | ||
| if want := []string{"base"}; !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("packageBuildDependencies = %v, want %v", got, 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
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
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.
Data race on shared
aPackagefingerprint fields. This lazy-fill branch writesaDep.Manifest/aDep.Fingerprint(lines 67-68) for a dependency whose fields are still empty. During parallel preflight, two packages in the same dependency level run concurrently; if both import a dependency that skippedcollectFingerprintin preflight (decl-only, or link-only-without-source —preflightPackageBuildreturnsskip=truebefore fingerprinting those), both workers reach this branch for the sameaDepand write its fields concurrently. There is no lock onaPackage.Fingerprint/Manifest, so this is a write/write + read/write race that can also produce a blank/partial fingerprint used as a cache key.The stated invariant ("every dependency's fingerprint is complete before a worker reads it") holds only for deps that were fingerprinted in an earlier level — it is violated precisely by the skip paths. Suggest either fingerprinting skipped packages too, or guarding these fields (e.g. per-package
sync.Once/ a mutex). Please verify withgo test -race.