From: Austin Clements Date: Fri, 30 Jun 2023 20:33:49 +0000 (-0400) Subject: cmd/compile,cmd/dist,cmd/go: compute -+ flag from package path X-Git-Tag: go1.22rc1~1150 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fbcf43c60ba5170309a238b0e42fd5879d419776;p=gostls13.git cmd/compile,cmd/dist,cmd/go: compute -+ flag from package path 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 TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go index 817dfd4ca5..8ffb41b16d 100644 --- a/src/cmd/compile/internal/base/flag.go +++ b/src/cmd/compile/internal/base/flag.go @@ -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 diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index 031a8d9013..193db6f52d 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -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) } diff --git a/src/cmd/go/internal/work/gc.go b/src/cmd/go/internal/work/gc.go index 216cbcf344..5ced6eebd4 100644 --- a/src/cmd/go/internal/work/gc.go +++ b/src/cmd/go/internal/work/gc.go @@ -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)) diff --git a/src/cmd/internal/objabi/pkgspecial.go b/src/cmd/internal/objabi/pkgspecial.go index ac38c1b52e..22b974a06c 100644 --- a/src/cmd/internal/objabi/pkgspecial.go +++ b/src/cmd/internal/objabi/pkgspecial.go @@ -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 }) }