aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-01-12 12:00:58 -0800
committerMatthew Dempsky <mdempsky@google.com>2021-01-12 23:22:41 +0000
commitd6ad88b4db454813e1bdf09635cd853fe3b7ef13 (patch)
tree69790b43a39e0ef77e64c6e601169ecc1c1e7915 /src/cmd/compile/internal/gc
parent432f9ffb11231b00b67c8fa8047f21a8282fa914 (diff)
downloadgo-d6ad88b4db454813e1bdf09635cd853fe3b7ef13.tar.gz
go-d6ad88b4db454813e1bdf09635cd853fe3b7ef13.zip
[dev.regabi] cmd/compile: compile functions before closures
This CL reorders function compilation to ensure that functions are always compiled before any enclosed function literals. The primary goal of this is to reduce the risk of race conditions that arise due to compilation of function literals needing to inspect data from their closure variables. However, a pleasant side effect is that it allows skipping the redundant, separate compilation of function literals that were inlined into their enclosing function. Change-Id: I03ee96212988cb578c2452162b7e99cc5e92918f Reviewed-on: https://go-review.googlesource.com/c/go/+/282892 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Trust: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/gc')
-rw-r--r--src/cmd/compile/internal/gc/compile.go51
1 files changed, 40 insertions, 11 deletions
diff --git a/src/cmd/compile/internal/gc/compile.go b/src/cmd/compile/internal/gc/compile.go
index c2894ab012..410b3e90ea 100644
--- a/src/cmd/compile/internal/gc/compile.go
+++ b/src/cmd/compile/internal/gc/compile.go
@@ -37,6 +37,10 @@ func enqueueFunc(fn *ir.Func) {
return
}
+ if clo := fn.OClosure; clo != nil && !ir.IsTrivialClosure(clo) {
+ return // we'll get this as part of its enclosing function
+ }
+
if len(fn.Body) == 0 {
// Initialize ABI wrappers if necessary.
ssagen.InitLSym(fn, false)
@@ -45,11 +49,22 @@ func enqueueFunc(fn *ir.Func) {
}
errorsBefore := base.Errors()
- prepareFunc(fn)
+
+ todo := []*ir.Func{fn}
+ for len(todo) > 0 {
+ next := todo[len(todo)-1]
+ todo = todo[:len(todo)-1]
+
+ prepareFunc(next)
+ todo = append(todo, next.Closures...)
+ }
+
if base.Errors() > errorsBefore {
return
}
+ // Enqueue just fn itself. compileFunctions will handle
+ // scheduling compilation of its closures after it's done.
compilequeue = append(compilequeue, fn)
}
@@ -97,7 +112,6 @@ func compileFunctions() {
return
}
- types.CalcSizeDisabled = true // not safe to calculate sizes concurrently
if race.Enabled {
// Randomize compilation order to try to shake out races.
tmp := make([]*ir.Func, len(compilequeue))
@@ -114,22 +128,37 @@ func compileFunctions() {
return len(compilequeue[i].Body) > len(compilequeue[j].Body)
})
}
- var wg sync.WaitGroup
- base.Ctxt.InParallel = true
- c := make(chan *ir.Func, base.Flag.LowerC)
+
+ // We queue up a goroutine per function that needs to be
+ // compiled, but require them to grab an available worker ID
+ // before doing any substantial work to limit parallelism.
+ workerIDs := make(chan int, base.Flag.LowerC)
for i := 0; i < base.Flag.LowerC; i++ {
+ workerIDs <- i
+ }
+
+ var wg sync.WaitGroup
+ var asyncCompile func(*ir.Func)
+ asyncCompile = func(fn *ir.Func) {
wg.Add(1)
- go func(worker int) {
- for fn := range c {
- ssagen.Compile(fn, worker)
+ go func() {
+ worker := <-workerIDs
+ ssagen.Compile(fn, worker)
+ workerIDs <- worker
+
+ // Done compiling fn. Schedule it's closures for compilation.
+ for _, closure := range fn.Closures {
+ asyncCompile(closure)
}
wg.Done()
- }(i)
+ }()
}
+
+ types.CalcSizeDisabled = true // not safe to calculate sizes concurrently
+ base.Ctxt.InParallel = true
for _, fn := range compilequeue {
- c <- fn
+ asyncCompile(fn)
}
- close(c)
compilequeue = nil
wg.Wait()
base.Ctxt.InParallel = false