]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile,cmd/dist,cmd/go: compute -+ flag from package path
authorAustin Clements <austin@google.com>
Fri, 30 Jun 2023 20:33:49 +0000 (16:33 -0400)
committerAustin Clements <austin@google.com>
Tue, 22 Aug 2023 19:18:28 +0000 (19:18 +0000)
As we did for the asm -compiling-runtime flag, this CL modifies the
compiler to compute the -+ (compiling runtime) flag from the package
path. Unlike for asm, some tests use -+ explicitly to opt in to
runtime restrictions, so we leave the flag, but it's no longer passed
by any build tools.

This lets us eliminate cmd/go's list of "runtime packages" in favor of
the unified objabi.LookupPkgSpecial. It also fixes an inconsistency
with dist, which only passed -+ when compiling "runtime" itself.

One consequence of this is that the compiler now ignores the -N flag
when compiling runtime packages. Previously, cmd/go would strip -N
when passing -+ and the compiler would fatal if it got both -N and -+,
so the overall effect was that the compiler never saw -N when
compiling a runtime package. Now we simply move that logic to disable
-N down into the compiler.

Change-Id: I4876047a1563210ed122a31b72d62798762cbcf5
Reviewed-on: https://go-review.googlesource.com/c/go/+/521699
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/base/flag.go
src/cmd/dist/build.go
src/cmd/go/internal/work/gc.go
src/cmd/internal/objabi/pkgspecial.go

index 817dfd4ca51cd184d1f7aa2457a5b475a6fe3471..8ffb41b16d446fc9a80eaa1ed7c7a463cec5b509 100644 (file)
@@ -201,6 +201,12 @@ func ParseFlags() {
                hashDebug = NewHashDebug("gossahash", Debug.Gossahash, nil)
        }
 
+       // Compute whether we're compiling the runtime from the package path. Test
+       // code can also use the flag to set this explicitly.
+       if Flag.Std && objabi.LookupPkgSpecial(Ctxt.Pkgpath).Runtime {
+               Flag.CompilingRuntime = true
+       }
+
        // Three inputs govern loop iteration variable rewriting, hash, experiment, flag.
        // The loop variable rewriting is:
        // IF non-empty hash, then hash determines behavior (function+line match) (*)
@@ -317,9 +323,6 @@ func ParseFlags() {
                }
        }
 
-       if Flag.CompilingRuntime && Flag.N != 0 {
-               log.Fatal("cannot disable optimizations while compiling runtime")
-       }
        if Flag.LowerC < 1 {
                log.Fatalf("-c must be at least 1, got %d", Flag.LowerC)
        }
@@ -328,6 +331,10 @@ func ParseFlags() {
        }
 
        if Flag.CompilingRuntime {
+               // It is not possible to build the runtime with no optimizations,
+               // because the compiler cannot eliminate enough write barriers.
+               Flag.N = 0
+
                // Runtime can't use -d=checkptr, at least not yet.
                Debug.Checkptr = 0
 
index 031a8d901362ad8b7e8a71ad76d6b816fa3a684b..193db6f52d9d73e536ae104dcc2617aa47f9a7f6 100644 (file)
@@ -946,9 +946,6 @@ func runInstall(pkg string, ch chan struct{}) {
        if gogcflags != "" {
                compile = append(compile, strings.Fields(gogcflags)...)
        }
-       if pkg == "runtime" {
-               compile = append(compile, "-+")
-       }
        if len(sfiles) > 0 {
                compile = append(compile, "-asmhdr", goasmh)
        }
index 216cbcf3448b648f478a20eb7915d6a0901898a2..5ced6eebd4739211ea1e9e062ad288db02cdce5a 100644 (file)
@@ -32,20 +32,6 @@ var ToolchainVersion = runtime.Version()
 // The 'path' used for GOROOT_FINAL when -trimpath is specified
 const trimPathGoRootFinal string = "$GOROOT"
 
-var runtimePackages = map[string]struct{}{
-       "internal/abi":             struct{}{},
-       "internal/bytealg":         struct{}{},
-       "internal/coverage/rtcov":  struct{}{},
-       "internal/cpu":             struct{}{},
-       "internal/goarch":          struct{}{},
-       "internal/goos":            struct{}{},
-       "runtime":                  struct{}{},
-       "runtime/internal/atomic":  struct{}{},
-       "runtime/internal/math":    struct{}{},
-       "runtime/internal/sys":     struct{}{},
-       "runtime/internal/syscall": struct{}{},
-}
-
 // The Go toolchain.
 
 type gcToolchain struct{}
@@ -93,14 +79,6 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
        if p.Standard {
                defaultGcFlags = append(defaultGcFlags, "-std")
        }
-       _, compilingRuntime := runtimePackages[p.ImportPath]
-       compilingRuntime = compilingRuntime && p.Standard
-       if compilingRuntime {
-               // runtime compiles with a special gc flag to check for
-               // memory allocations that are invalid in the runtime package,
-               // and to implement some special compiler pragmas.
-               defaultGcFlags = append(defaultGcFlags, "-+")
-       }
 
        // If we're giving the compiler the entire package (no C etc files), tell it that,
        // so that it can give good error messages about forward declarations.
@@ -146,18 +124,6 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg
        if p.Internal.FuzzInstrument {
                gcflags = append(gcflags, fuzzInstrumentFlags()...)
        }
-       if compilingRuntime {
-               // Remove -N, if present.
-               // It is not possible to build the runtime with no optimizations,
-               // because the compiler cannot eliminate enough write barriers.
-               for i := 0; i < len(gcflags); i++ {
-                       if gcflags[i] == "-N" {
-                               copy(gcflags[i:], gcflags[i+1:])
-                               gcflags = gcflags[:len(gcflags)-1]
-                               i--
-                       }
-               }
-       }
        // Add -c=N to use concurrent backend compilation, if possible.
        if c := gcBackendConcurrency(gcflags); c > 1 {
                defaultGcFlags = append(defaultGcFlags, fmt.Sprintf("-c=%d", c))
index ac38c1b52e7ddf3f64f662132ca1289f0580a4ea..22b974a06c7e6a66595a74dcf9717413912a036d 100644 (file)
@@ -9,6 +9,21 @@ import "sync"
 // PkgSpecial indicates special build properties of a given runtime-related
 // package.
 type PkgSpecial struct {
+       // Runtime indicates that this package is "runtime" or imported by
+       // "runtime". This has several effects (which maybe should be split out):
+       //
+       // - Implicit allocation is disallowed.
+       //
+       // - Various runtime pragmas are enabled.
+       //
+       // - Optimizations are always enabled.
+       //
+       // This should be set for runtime and all packages it imports, and may be
+       // set for additional packages.
+       //
+       // TODO(austin): Test that all of `go list -deps runtime` is marked Runtime.
+       Runtime bool
+
        // AllowAsmABI indicates that assembly in this package is allowed to use ABI
        // selectors in symbol names. Generally this is needed for packages that
        // interact closely with the runtime package or have performance-critical
@@ -16,6 +31,22 @@ type PkgSpecial struct {
        AllowAsmABI bool
 }
 
+var runtimePkgs = []string{
+       "runtime",
+
+       "runtime/internal/atomic",
+       "runtime/internal/math",
+       "runtime/internal/sys",
+       "runtime/internal/syscall",
+
+       "internal/abi",
+       "internal/bytealg",
+       "internal/coverage/rtcov",
+       "internal/cpu",
+       "internal/goarch",
+       "internal/goos",
+}
+
 var allowAsmABIPkgs = []string{
        "runtime",
        "reflect",
@@ -42,6 +73,9 @@ func LookupPkgSpecial(pkgPath string) PkgSpecial {
                        f(&s)
                        pkgSpecials[elt] = s
                }
+               for _, pkg := range runtimePkgs {
+                       set(pkg, func(ps *PkgSpecial) { ps.Runtime = true })
+               }
                for _, pkg := range allowAsmABIPkgs {
                        set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true })
                }