From 0eb9b61c5cb5dcc25bfffc9e04293583bdf3dd0c Mon Sep 17 00:00:00 2001 From: thesyncim Date: Sat, 31 Oct 2026 22:26:30 +0000 Subject: [PATCH] cmd/loopvec-toolexec: give the same archive on every build The wrapper wrote the rewritten files to a new os.MkdirTemp directory under the go command's work directory. The compiler records file names in the export data, and the go command trims only the compile's own object directory, so every build of a rewritten package produced a different archive, with a random absolute path in it. That fails builds where the go command compiles a package twice with the same inputs. With PGO, go test can compile a dependency once for each test binary that needs it, then compile an importer against one copy and link the other. In github.com/thesyncim/goav1, whose cmd/qualitybench has a default.pgo, go test ./... fails with: link: fingerprint mismatch: github.com/thesyncim/goav1 has 9bcecb47ca806e38, import from github.com/thesyncim/goav1/cmd/qualitybench expecting 224b81cee36e8b4e The rewritten files now go in a loopvec directory inside the object directory, next to the importcfg the go command writes there. That is still a separate place for each compile, and the -trimpath the go command passes reduces it to loopvec/x.go. --- cmd/loopvec-toolexec/compile.go | 15 +++++++++++---- testdata/toolexec_reproducible.txt | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 testdata/toolexec_reproducible.txt diff --git a/cmd/loopvec-toolexec/compile.go b/cmd/loopvec-toolexec/compile.go index 13f0d91..adf3ea4 100644 --- a/cmd/loopvec-toolexec/compile.go +++ b/cmd/loopvec-toolexec/compile.go @@ -197,10 +197,17 @@ func tryRewrite(tool string, args []string) (out []string, ok bool) { return nil, false } - // A package and its test variant are compiled concurrently under the same - // import path, so every compile gets its own directory. - dir, err := os.MkdirTemp(b.dir, strings.NewReplacer("/", "_", ".", "_").Replace(c.pkg)+"-") - if err != nil { + // The rewritten files go in the compile's own object directory, where the + // go command writes the importcfg: a package and its test variant are + // compiled concurrently under the same import path, so each needs its own + // place, and the go command trims that directory from file names with + // -trimpath. The file names end up in the export data, so the archive must + // not depend on where the directory is: the go command can compile a + // package twice in one build (with PGO, once for each test binary) and use + // one archive to compile its importers and the other to link them, and the + // linker rejects two archives that differ. + dir := filepath.Join(filepath.Dir(args[c.importcfg]), "loopvec") + if err := os.MkdirAll(dir, 0o755); err != nil { return nil, false } newArgs := append([]string(nil), args...) diff --git a/testdata/toolexec_reproducible.txt b/testdata/toolexec_reproducible.txt new file mode 100644 index 0000000..ad9e082 --- /dev/null +++ b/testdata/toolexec_reproducible.txt @@ -0,0 +1,28 @@ +# The wrapper compiles rewritten files from a directory of its own, and their +# names end up in the export data. Building the same package twice must still +# give the same archive: the go command can compile a package twice in one build +# (with PGO, once for each test binary that needs it), compile its importers +# against one copy and link the other, and the linker rejects two copies that +# differ. + +env GOEXPERIMENT=simd +env LOOPVEC_TOOLEXEC_LOG=$WORK/log +env GOCACHE=$WORK/gocache1 +exec go build -buildmode=archive -toolexec=$LOOPVEC_TOOLEXEC -o a1.a . +env GOCACHE=$WORK/gocache2 +exec go build -buildmode=archive -toolexec=$LOOPVEC_TOOLEXEC -o a2.a . +grep -count=2 '^rewrote 1 loop\(s\) in example\.com/lib$' log +cmp a1.a a2.a + +-- go.mod -- +module example.com/lib + +go 1.27.1 +-- lib.go -- +package lib + +func Add(dst, a, b []float32) { + for i := range dst { + dst[i] = a[i] + b[i] + } +}