Skip to content

Commit 8c92897

Browse files
committed
cmd/compile: rework TestPGOHash to not rebuild dependencies
TestPGOHash may rebuild dependencies as we pass -trimpath to the go command. This CL makes it pass -trimpath compiler flag to only the current package instead, as we only need the current package to have a stable source file path. Also refactor buildPGOInliningTest to only take compiler flags, not go flags, to avoid accidental rebuild. Should fix #63733. Change-Id: Iec6c4e90cf659790e21083ee2e697f518234c5b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/535915 Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 5613882 commit 8c92897

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/cmd/compile/internal/test/pgo_inl_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"testing"
1919
)
2020

21-
func buildPGOInliningTest(t *testing.T, dir string, flags ...string) []byte {
21+
func buildPGOInliningTest(t *testing.T, dir string, gcflag string) []byte {
2222
const pkg = "example.com/pgo/inline"
2323

2424
// Add a go.mod so we have a consistent symbol names in this temp dir.
@@ -30,10 +30,11 @@ go 1.19
3030
}
3131

3232
exe := filepath.Join(dir, "test.exe")
33-
args := []string{"test", "-c", "-o", exe}
34-
args = append(args, flags...)
35-
cmd := testenv.CleanCmdEnv(testenv.Command(t, testenv.GoToolPath(t), args...))
33+
args := []string{"test", "-c", "-o", exe, "-gcflags=" + gcflag}
34+
cmd := testenv.Command(t, testenv.GoToolPath(t), args...)
3635
cmd.Dir = dir
36+
cmd = testenv.CleanCmdEnv(cmd)
37+
t.Log(cmd)
3738
out, err := cmd.CombinedOutput()
3839
if err != nil {
3940
t.Fatalf("build failed: %v, output:\n%s", err, out)
@@ -86,7 +87,7 @@ func testPGOIntendedInlining(t *testing.T, dir string) {
8687
// Build the test with the profile. Use a smaller threshold to test.
8788
// TODO: maybe adjust the test to work with default threshold.
8889
pprof := filepath.Join(dir, "inline_hot.pprof")
89-
gcflag := fmt.Sprintf("-gcflags=-m -m -pgoprofile=%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90", pprof)
90+
gcflag := fmt.Sprintf("-m -m -pgoprofile=%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90", pprof)
9091
out := buildPGOInliningTest(t, dir, gcflag)
9192

9293
scanner := bufio.NewScanner(bytes.NewReader(out))
@@ -300,6 +301,8 @@ func TestPGOHash(t *testing.T) {
300301
testenv.MustHaveGoRun(t)
301302
t.Parallel()
302303

304+
const pkg = "example.com/pgo/inline"
305+
303306
wd, err := os.Getwd()
304307
if err != nil {
305308
t.Fatalf("error getting wd: %v", err)
@@ -316,25 +319,25 @@ func TestPGOHash(t *testing.T) {
316319
}
317320

318321
pprof := filepath.Join(dir, "inline_hot.pprof")
319-
gcflag0 := fmt.Sprintf("-gcflags=-pgoprofile=%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90,pgodebug=1,", pprof)
322+
// build with -trimpath so the source location (thus the hash)
323+
// does not depend on the temporary directory path.
324+
gcflag0 := fmt.Sprintf("-pgoprofile=%s -trimpath %s=>%s -d=pgoinlinebudget=160,pgoinlinecdfthreshold=90,pgodebug=1", pprof, dir, pkg)
320325

321326
// Check that a hash match allows PGO inlining.
322327
const srcPos = "example.com/pgo/inline/inline_hot.go:81:19"
323328
const hashMatch = "pgohash triggered " + srcPos + " (inline)"
324329
pgoDebugRE := regexp.MustCompile(`hot-budget check allows inlining for call .* at ` + strings.ReplaceAll(srcPos, ".", "\\."))
325330
hash := "v1" // 1 matches srcPos, v for verbose (print source location)
326331
gcflag := gcflag0 + ",pgohash=" + hash
327-
// build with -trimpath so the source location (thus the hash)
328-
// does not depend on the temporary directory path.
329-
out := buildPGOInliningTest(t, dir, gcflag, "-trimpath")
332+
out := buildPGOInliningTest(t, dir, gcflag)
330333
if !bytes.Contains(out, []byte(hashMatch)) || !pgoDebugRE.Match(out) {
331334
t.Errorf("output does not contain expected source line, out:\n%s", out)
332335
}
333336

334337
// Check that a hash mismatch turns off PGO inlining.
335338
hash = "v0" // 0 should not match srcPos
336339
gcflag = gcflag0 + ",pgohash=" + hash
337-
out = buildPGOInliningTest(t, dir, gcflag, "-trimpath")
340+
out = buildPGOInliningTest(t, dir, gcflag)
338341
if bytes.Contains(out, []byte(hashMatch)) || pgoDebugRE.Match(out) {
339342
t.Errorf("output contains unexpected source line, out:\n%s", out)
340343
}

0 commit comments

Comments
 (0)