Skip to content

Commit

Permalink
cmd/compile: rework TestPGOHash to not rebuild dependencies
Browse files Browse the repository at this point in the history
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 <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
  • Loading branch information
cherrymui committed Oct 27, 2023
1 parent 5613882 commit 8c92897
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/cmd/compile/internal/test/pgo_inl_test.go
Expand Up @@ -18,7 +18,7 @@ import (
"testing"
)

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

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

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

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

const pkg = "example.com/pgo/inline"

wd, err := os.Getwd()
if err != nil {
t.Fatalf("error getting wd: %v", err)
Expand All @@ -316,25 +319,25 @@ func TestPGOHash(t *testing.T) {
}

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

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

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

0 comments on commit 8c92897

Please sign in to comment.