]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make PGO profile path per package
authorCherry Mui <cherryyz@google.com>
Mon, 27 Feb 2023 20:37:32 +0000 (15:37 -0500)
committerCherry Mui <cherryyz@google.com>
Mon, 6 Mar 2023 19:22:03 +0000 (19:22 +0000)
Currently, the PGO profile path is global for a single go command
invocation, as it applies to all packages being built (or none).
With -pgo=auto mode with multiple main packages, packages from a
single go command invocation could have different profiles. So it
is necessary that the PGO profile path is per package, which is
this CL does.

For #58099.

Change-Id: I148a15970ec907272db85b4b27ad6b08c41d6c0c
Reviewed-on: https://go-review.googlesource.com/c/go/+/472357
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/load/test.go
src/cmd/go/internal/work/exec.go
src/cmd/go/internal/work/gc.go

index a6ad7390efe40847e86b66114a62fa9905c31da8..2037e7cf063bc3b6809c2e619e86fa8b6c57e410 100644 (file)
@@ -81,7 +81,6 @@ var (
        BuildO                 string                  // -o flag
        BuildP                 = runtime.GOMAXPROCS(0) // -p flag
        BuildPGO               string                  // -pgo flag
-       BuildPGOFile           string                  // profile selected by -pgo flag, an absolute path (if not empty)
        BuildPkgdir            string                  // -pkgdir flag
        BuildRace              bool                    // -race flag
        BuildToolexec          []string                // -toolexec flag
index 799f7de85e468c305e8953cfa6998e08e4575bf2..cfb853e9796fd7a5b4276a35052205b4e3778713 100644 (file)
@@ -232,6 +232,7 @@ type PackageInternal struct {
        TestmainGo        *[]byte              // content for _testmain.go
        Embed             map[string][]string  // //go:embed comment mapping
        OrigImportPath    string               // original import path before adding '_test' suffix
+       PGOProfile        string               // path to PGO profile
 
        Asmflags   []string // -asmflags for this package
        Gcflags    []string // -gcflags for this package
@@ -2385,11 +2386,11 @@ func (p *Package) setBuildInfo(autoVCS bool) {
                        appendSetting("-ldflags", ldflags)
                }
        }
-       if cfg.BuildPGOFile != "" {
+       if p.Internal.PGOProfile != "" {
                if cfg.BuildTrimpath {
-                       appendSetting("-pgo", filepath.Base(cfg.BuildPGOFile))
+                       appendSetting("-pgo", filepath.Base(p.Internal.PGOProfile))
                } else {
-                       appendSetting("-pgo", cfg.BuildPGOFile)
+                       appendSetting("-pgo", p.Internal.PGOProfile)
                }
        }
        if cfg.BuildMSan {
@@ -2894,7 +2895,7 @@ func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string)
        return pkgs
 }
 
-// setPGOProfilePath sets cfg.BuildPGOFile to the PGO profile path.
+// setPGOProfilePath sets the PGO profile path for pkgs.
 // In -pgo=auto mode, it finds the default PGO profile.
 func setPGOProfilePath(pkgs []*Package) {
        switch cfg.BuildPGO {
@@ -2929,16 +2930,21 @@ func setPGOProfilePath(pkgs []*Package) {
                }
                file := filepath.Join(mainpkg.Dir, "default.pgo")
                if fi, err := os.Stat(file); err == nil && !fi.IsDir() {
-                       cfg.BuildPGOFile = file
+                       for _, p := range PackageList(pkgs) {
+                               p.Internal.PGOProfile = file
+                       }
                }
 
        default:
                // Profile specified from the command line.
                // Make it absolute path, as the compiler runs on various directories.
-               if p, err := filepath.Abs(cfg.BuildPGO); err != nil {
+               file, err := filepath.Abs(cfg.BuildPGO)
+               if err != nil {
                        base.Fatalf("fail to get absolute path of PGO file %s: %v", cfg.BuildPGO, err)
-               } else {
-                       cfg.BuildPGOFile = p
+               }
+
+               for _, p := range PackageList(pkgs) {
+                       p.Internal.PGOProfile = file
                }
        }
 }
index 38afd96aa472b4ba2bfed7d98d2dd8ee74321bca..64e5b74cc240155bd21b57f049f87b5db54a6393 100644 (file)
@@ -206,6 +206,7 @@ func TestPackagesAndErrors(ctx context.Context, opts PackageOpts, p *Package, co
                ptest.Internal.Embed = testEmbed
                ptest.EmbedFiles = str.StringList(p.EmbedFiles, p.TestEmbedFiles)
                ptest.Internal.OrigImportPath = p.Internal.OrigImportPath
+               ptest.Internal.PGOProfile = p.Internal.PGOProfile
                ptest.Internal.Build.Directives = append(slices.Clip(p.Internal.Build.Directives), p.Internal.Build.TestDirectives...)
                ptest.collectDeps()
        } else {
@@ -243,6 +244,7 @@ func TestPackagesAndErrors(ctx context.Context, opts PackageOpts, p *Package, co
                                Gccgoflags:     p.Internal.Gccgoflags,
                                Embed:          xtestEmbed,
                                OrigImportPath: p.Internal.OrigImportPath,
+                               PGOProfile:     p.Internal.PGOProfile,
                        },
                }
                if pxtestNeedsPtest {
@@ -270,6 +272,7 @@ func TestPackagesAndErrors(ctx context.Context, opts PackageOpts, p *Package, co
                        Ldflags:        p.Internal.Ldflags,
                        Gccgoflags:     p.Internal.Gccgoflags,
                        OrigImportPath: p.Internal.OrigImportPath,
+                       PGOProfile:     p.Internal.PGOProfile,
                },
        }
 
index 573a58627ac29c7113afb42950f8fe73a10f4f81..6a0a53429f142b5e3017c81278befb123256ae46 100644 (file)
@@ -385,8 +385,8 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
        for _, file := range inputFiles {
                fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
        }
-       if cfg.BuildPGOFile != "" {
-               fmt.Fprintf(h, "pgofile %s\n", b.fileHash(cfg.BuildPGOFile))
+       if p.Internal.PGOProfile != "" {
+               fmt.Fprintf(h, "pgofile %s\n", b.fileHash(p.Internal.PGOProfile))
        }
        for _, a1 := range a.Deps {
                p1 := a1.Package
index 51d1760d9ce2fe457f8354284dfe08358d4f7780..ec01798e091a3cbdc92a6413804ca0fd4c1cbec6 100644 (file)
@@ -144,8 +144,8 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
        if p.Internal.CoverageCfg != "" {
                defaultGcFlags = append(defaultGcFlags, "-coveragecfg="+p.Internal.CoverageCfg)
        }
-       if cfg.BuildPGOFile != "" {
-               defaultGcFlags = append(defaultGcFlags, "-pgoprofile="+cfg.BuildPGOFile)
+       if p.Internal.PGOProfile != "" {
+               defaultGcFlags = append(defaultGcFlags, "-pgoprofile="+p.Internal.PGOProfile)
        }
        if symabis != "" {
                defaultGcFlags = append(defaultGcFlags, "-symabis", symabis)