Skip to content

Commit ebd39f5

Browse files
committed
sweet: rebuild cmd/compile and cmd/link for go-build benchmark
go-build is a subtly strange benchmark. For every other benchmark, sweet builds the benchmarked program in set up, using Config.BuildEnv. go-build does not do that because the benchmarked programs (cmd/compile, cmd/link, and (to a lesser degree) cmd/go) are conveniently prebuilt in GOROOT. But this breaks the intuition that BuildEnv can control build flags for the benchmarked program, particularly for sweet run -pgo, where sweet itself expects a PGO flag in BuildEnv to impact the benchmarked program. Adjust go-build to follow these standards by copying GOROOT for each config and running `go install cmd/compile cmd/link` in the copied GOROOT, allowing BuildEnv to take effect. This fixes the general case for PGO, though it could use more work as really cmd/compile and cmd/link should receive _different_ PGO profiles. We also run the dummy build with the ExecEnv (matching how the actual benchmark will run) to avoid passing -pgo to those builds. For golang/go#55022. Change-Id: I36e4486c79ee4200c2e10ccba913d559187bdad2
1 parent 8e0b737 commit ebd39f5

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

sweet/harnesses/go-build.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"golang.org/x/benchmarks/sweet/common"
13+
"golang.org/x/benchmarks/sweet/common/fileutil"
1314
"golang.org/x/benchmarks/sweet/common/log"
1415
)
1516

@@ -72,12 +73,30 @@ func (h GoBuild) Get(srcDir string) error {
7273
return nil
7374
}
7475

75-
func (h GoBuild) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
76+
func (h GoBuild) Build(pcfg *common.Config, bcfg *common.BuildConfig) error {
77+
// Local copy of config for updating GOROOT.
78+
cfg := pcfg.Copy()
79+
7680
benchmarks := buildBenchmarks
7781
if bcfg.Short {
7882
// Do only the pkgsite benchmark.
7983
benchmarks = []*buildBenchmark{buildBenchmarks[2]}
8084
}
85+
86+
// cfg.GoRoot is our source toolchain. We need to rebuild cmd/compile
87+
// and cmd/link with cfg.BuildEnv to apply any configured build options
88+
// (e.g., PGO).
89+
//
90+
// Do so by `go install`ing them into a copied GOROOT.
91+
goroot := filepath.Join(bcfg.BinDir, "goroot")
92+
if err := fileutil.CopyDir(goroot, cfg.GoRoot, nil); err != nil {
93+
return fmt.Errorf("error copying GOROOT: %v", err)
94+
}
95+
cfg.GoRoot = goroot
96+
if err := cfg.GoTool().Do("install", "cmd/compile", "cmd/link"); err != nil {
97+
return fmt.Errorf("error building cmd/compile and cmd/link: %v", err)
98+
}
99+
81100
for _, bench := range benchmarks {
82101
// Generate a symlink to the repository and put it in bin.
83102
// It's not a binary, but it's the only place we can put it
@@ -89,9 +108,14 @@ func (h GoBuild) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
89108
}
90109

91110
// Build the benchmark once, pulling in any requisite packages.
111+
//
112+
// Run the go tool with ExecEnv, as that is what we will use
113+
// when benchmarking.
92114
pkgPath := filepath.Join(bcfg.BinDir, bench.name, bench.pkg)
93115
dummyBin := filepath.Join(bcfg.BinDir, "dummy")
94-
if err := cfg.GoTool().BuildPath(pkgPath, dummyBin); err != nil {
116+
goTool := cfg.GoTool()
117+
goTool.Env = cfg.ExecEnv.MustSet("GOROOT=" + cfg.GoRoot)
118+
if err := goTool.BuildPath(pkgPath, dummyBin); err != nil {
95119
return fmt.Errorf("error building %s %s: %w", bench.name, bench.pkg, err)
96120
}
97121
}
@@ -102,7 +126,11 @@ func (h GoBuild) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
102126
return nil
103127
}
104128

105-
func (h GoBuild) Run(cfg *common.Config, rcfg *common.RunConfig) error {
129+
func (h GoBuild) Run(pcfg *common.Config, rcfg *common.RunConfig) error {
130+
// Local copy of config for updating GOROOT.
131+
cfg := pcfg.Copy()
132+
cfg.GoRoot = filepath.Join(rcfg.BinDir, "goroot") // see Build, above.
133+
106134
benchmarks := buildBenchmarks
107135
if rcfg.Short {
108136
// Do only the pkgsite benchmark.

0 commit comments

Comments
 (0)