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] + } +}