]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go, cmd/compile: always optimize when building runtime
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 20 Mar 2017 16:56:50 +0000 (09:56 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 20 Mar 2017 20:54:19 +0000 (20:54 +0000)
When optimizations are disabled, the compiler
cannot eliminate enough write barriers to satisfy
the runtime's nowritebarrier and nowritebarrierrec
annotations.

Enforce that requirement, and for convenience,
have cmd/go elide -N when compiling the runtime.

This came up in practice for me when running
toolstash -cmp. When toolstash -cmp detected
mismatches, it recompiled with -N, which caused
runtime compilation failures.

Change-Id: Ifcdef22c725baf2c59a09470f00124361508a8f3
Reviewed-on: https://go-review.googlesource.com/38380
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/main.go
src/cmd/go/internal/work/build.go

index 80370968327b93ff4338ddd86eef123fefb97564..975bc579ba79eefb91343872e2bb0858ae12d7a0 100644 (file)
@@ -268,6 +268,9 @@ func Main(archInit func(*Arch)) {
        } else if flag_race || flag_msan {
                instrumenting = true
        }
+       if compiling_runtime && Debug['N'] != 0 {
+               log.Fatal("cannot disable optimizations while compiling runtime")
+       }
 
        // parse -d argument
        if debugstr != "" {
index 7b07112508964ed7f24c43175bd1c58635c2b45e..c09d8d35130dfb2fc7d1fd4539b2655d2625bacb 100644 (file)
@@ -2176,7 +2176,8 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr b
        if p.Name == "main" {
                gcargs[1] = "main"
        }
-       if p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal")) {
+       compilingRuntime := p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal"))
+       if compilingRuntime {
                // runtime compiles with a special gc flag to emit
                // additional reflect type data.
                gcargs = append(gcargs, "-+")
@@ -2211,7 +2212,22 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr b
                }
        }
 
-       args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", b.WorkDir, buildGcflags, gcargs, "-D", p.Internal.LocalPrefix, importArgs}
+       gcflags := buildGcflags
+       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.
+               gcflags = make([]string, len(buildGcflags))
+               copy(gcflags, buildGcflags)
+               for i := 0; i < len(gcflags); i++ {
+                       if gcflags[i] == "-N" {
+                               copy(gcflags[i:], gcflags[i+1:])
+                               gcflags = gcflags[:len(gcflags)-1]
+                               i--
+                       }
+               }
+       }
+       args := []interface{}{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", b.WorkDir, gcflags, gcargs, "-D", p.Internal.LocalPrefix, importArgs}
        if ofile == archive {
                args = append(args, "-pack")
        }