Skip to content

Commit

Permalink
sweet: rebuild cmd/compile and cmd/link for go-build benchmark
Browse files Browse the repository at this point in the history
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.

For golang/go#55022.

Change-Id: I36e4486c79ee4200c2e10ccba913d559187bdad2
  • Loading branch information
prattmic committed Oct 21, 2022
1 parent 247f8eb commit 7d6e9a7
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions sweet/harnesses/go-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"

"golang.org/x/benchmarks/sweet/common"
"golang.org/x/benchmarks/sweet/common/fileutil"
"golang.org/x/benchmarks/sweet/common/log"
)

Expand Down Expand Up @@ -72,12 +73,30 @@ func (h GoBuild) Get(srcDir string) error {
return nil
}

func (h GoBuild) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
func (h GoBuild) Build(pcfg *common.Config, bcfg *common.BuildConfig) error {
// Local copy of config for updating GOROOT.
cfg := pcfg.Copy()

benchmarks := buildBenchmarks
if bcfg.Short {
// Do only the pkgsite benchmark.
benchmarks = []*buildBenchmark{buildBenchmarks[2]}
}

// cfg.GoRoot is our source toolchain. We need to rebuild cmd/compile
// and cmd/link with cfg.BuildEnv to apply any configured build options
// (e.g., PGO).
//
// Do so by `go install`ing it into a copied GOROOT.
goroot := filepath.Join(bcfg.BinDir, "goroot")
if err := fileutil.CopyDir(goroot, cfg.GoRoot, nil); err != nil {
return fmt.Errorf("error copying GOROOT: %v", err)
}
cfg.GoRoot = goroot
if err := cfg.GoTool().Do("install", "cmd/compile", "cmd/link"); err != nil {
return fmt.Errorf("error building cmd/compile and cmd/link: %v", err)
}

for _, bench := range benchmarks {
// Generate a symlink to the repository and put it in bin.
// It's not a binary, but it's the only place we can put it
Expand All @@ -102,7 +121,11 @@ func (h GoBuild) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
return nil
}

func (h GoBuild) Run(cfg *common.Config, rcfg *common.RunConfig) error {
func (h GoBuild) Run(pcfg *common.Config, rcfg *common.RunConfig) error {
// Local copy of config for updating GOROOT.
cfg := pcfg.Copy()
cfg.GoRoot = filepath.Join(rcfg.BinDir, "goroot") // see Build, above.

benchmarks := buildBenchmarks
if rcfg.Short {
// Do only the pkgsite benchmark.
Expand Down

0 comments on commit 7d6e9a7

Please sign in to comment.