Skip to content

build: preserve main package identity for test builds - #2370

Merged
xushiwei merged 1 commit into
xgo-dev:mainfrom
visualfc:fortest
Aug 21, 2026
Merged

build: preserve main package identity for test builds#2370
xushiwei merged 1 commit into
xgo-dev:mainfrom
visualfc:fortest

Conversation

@visualfc

@visualfc visualfc commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Preserve the main package identity during test builds instead of renaming it to main.test.
  • Add an internal @fortest marker to distinguish test-main packages when resolving ABI paths.
  • Keep c-shared test exports pointed at main.init and main.main.
  • Add linkname coverage for same-package, external-test, and main-package references.
  • Update the stdlib build-mode test runner for test binaries that retain the main package identity.
  • Keep generated test library names package-specific, such as libtar.test.
  • Link runners against main.init and main.main rather than package-path-derived test symbols.

Testing

  • llgo test -v ./test/go/linkname
  • LLGO=./dev/llgo.sh dev/test_std_buildmodes.sh ./test/std/archive/tar

@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown

LLGo baseline benchmarks

c8bdb683fc11 | workflow run | long-term charts

Program measurements

Platform Workload File size vs base Build vs base Run vs base
Linux cprintf 19288 B 0 B / +0.0% 325.824 ms +1.377 ms / +0.4% (worse) 1.351 ms -13 us / -1.0% (better)
Linux fmtprintf 1860168 B -8 B / -0.0004301% (better) 2.696 s +12.67 ms / +0.5% (worse) 3.558 ms -739 ns / -0.02077% (better)
Linux println 68776 B 0 B / +0.0% 330.526 ms +2.662 ms / +0.8% (worse) 1.710 ms -33.78 us / -1.9% (better)
macOS cprintf 84672 B 0 B / +0.0% 686.106 ms +153.6 ms / +28.8% (worse) 6.716 ms +3.067 ms / +84.0% (worse)
macOS fmtprintf 1892720 B 0 B / +0.0% 3.180 s +209.9 ms / +7.1% (worse) 16.400 ms +2.121 ms / +14.9% (worse)
macOS println 121360 B 0 B / +0.0% 584.248 ms +184.3 ms / +46.1% (worse) 5.179 ms +1.626 ms / +45.8% (worse)
Core language and compiler benchmarks
Platform Benchmark ns/op vs base
Linux BenchmarkLookupPCRandom 12.260 ns/op -0.08 ns/op / -0.6% (better)
Linux BenchmarkMergeCompilerFlags 144.500 ns/op 0 ns/op / +0.0%
Linux BenchmarkMergeLinkerFlags 94.200 ns/op -0.05 ns/op / -0.1% (better)
Linux BenchmarkChannelBuffered 36.320 ns/op +0.02 ns/op / +0.1% (worse)
Linux BenchmarkChannelHandoff 23833 ns/op +823 ns/op / +3.6% (worse)
Linux BenchmarkDefer 44.390 ns/op +0.36 ns/op / +0.8% (worse)
Linux BenchmarkDirectCall 1.758 ns/op 0 ns/op / +0.0%
Linux BenchmarkGlobalRead 1.758 ns/op -0.001 ns/op / -0.1% (better)
Linux BenchmarkGlobalWrite 2.806 ns/op 0 ns/op / +0.0%
Linux BenchmarkGoroutine 29868 ns/op +482 ns/op / +1.6% (worse)
Linux BenchmarkInterfaceCall 8.651 ns/op +0.03 ns/op / +0.3% (worse)
Linux BenchmarkRuntimeGetG 2.112 ns/op 0 ns/op / +0.0%
macOS BenchmarkLookupPCRandom 16.560 ns/op -0.17 ns/op / -1.0% (better)
macOS BenchmarkMergeCompilerFlags 168.100 ns/op +11.9 ns/op / +7.6% (worse)
macOS BenchmarkMergeLinkerFlags 107.600 ns/op +5.3 ns/op / +5.2% (worse)
macOS BenchmarkChannelBuffered 27.470 ns/op +2.5 ns/op / +10.0% (worse)
macOS BenchmarkChannelHandoff 11267 ns/op +1347 ns/op / +13.6% (worse)
macOS BenchmarkDefer 34.580 ns/op +4.75 ns/op / +15.9% (worse)
macOS BenchmarkDirectCall 1.214 ns/op +0.006 ns/op / +0.5% (worse)
macOS BenchmarkGlobalRead 1.068 ns/op -0.155 ns/op / -12.7% (better)
macOS BenchmarkGlobalWrite 1.161 ns/op -0.211 ns/op / -15.4% (better)
macOS BenchmarkGoroutine 32112 ns/op -20614 ns/op / -39.1% (better)
macOS BenchmarkInterfaceCall 5.589 ns/op -0.323 ns/op / -5.5% (better)
macOS BenchmarkRuntimeGetG 2.529 ns/op -0.178 ns/op / -6.6% (better)

Compared with 0314faeba69d measured in the same runner job.

@fennoai fennoai Bot left a comment

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.

Review Summary

This PR reworks how //go:linkname resolves into test-compiled main packages. It replaces the previous approach (renaming the package to "main.test", which mutated the real package name) with a scope sentinel: an @ForTest marker constant is inserted into the package scope when a main package is built for test, and ssa/abi/abi.go PathOf() consults that marker to return the real import path instead of collapsing to "main".

The approach is sound and a genuine improvement over mutating Types.Name():

  • The @ prefix is deliberately un-spellable as a Go identifier, so the sentinel cannot collide with, or be spoofed by, user code. No security issue.
  • The added Scope().Lookup in PathOf is O(1) and guarded by Name() == "main" short-circuit, so it only runs for main packages. No performance concern.
  • Removing the obsolete main.test rename (and its test) is correct hygiene; the surviving .test suffix checks operate on pkg.ID/pkg.PkgPath, not the removed rename, so they are unaffected.

Findings below are maintainability/robustness improvements, not blockers. The new test/linkname/ regression tests are a good addition.

Comment thread internal/build/build.go Outdated
Comment thread ssa/abi/abi.go Outdated
Comment thread internal/build/build.go Outdated
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/build/build.go 85.71% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@visualfc
visualfc force-pushed the fortest branch 6 times, most recently from 2884feb to eaedad0 Compare August 20, 2026 13:57
@xushiwei
xushiwei merged commit 0212380 into xgo-dev:main Aug 21, 2026
41 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants