Repository navigation
Description
I am currently working on some changes to the inliner for #15561, the current state of my work is https://go-review.googlesource.com/c/go/+/72490
While testing my changes, I noticed a very odd behavior, namely that code miscompiled when my inliner changes where triggered, was compiled correctly when passing the -m flag.
The following code is reduced reproducer from a failure observed in generateTrace from cmd/trace/trace.go
import (
"fmt"
)
type elem struct {
f uint64
}
func f() int {
m := make(map[uint64]*elem)
getOrCreate := func(g uint64) *elem {
info, ok := m[g]
if !ok {
info = &elem{}
m[g] = info
}
return info
}
getOrCreate(42)
return len(m)
}
func main() {
fmt.Println(f())
}
Diffing a disassembly of this program, compiled with - l -l and -l -l -m respectively, gives the following:
16c16
< 0x1091a6e 4889442420 MOVQ AX, 0x20(SP)
---
> 0x1091a6e 4889442430 MOVQ AX, 0x30(SP)
27c27
< 0x1091a9a 488b442420 MOVQ 0x20(SP), AX
---
> 0x1091a9a 488b442430 MOVQ 0x30(SP), AX
47c47
< 0x1091adf 488b4c2420 MOVQ 0x20(SP), CX
---
> 0x1091adf 488b4c2430 MOVQ 0x30(SP), CX
The only other reference to the 0x20(SP) slot is, in both cases, the return value of the mapaccess call inside the inlined closure body :
0x1091a8c e86f7ff7ff CALL runtime.mapaccess2_fast64(SB)
0x1091a91 0fb6442420 MOVZX 0x20(SP), AX
I realize that there is probably something wrong with my inliner changes. However, I'm submitting this bug nonetheless because I find it very surprising that the -m flag somehow fixes this issue, even though it should only affect compiler logs and not the resulting binary. This seems like it might be a latent issue in the compiler that would be worth fixing.
I have manually audited all locations that read Debug['m'] inside the compiler and didn't notice anything suspicious/side-effecty so I'm pretty stumped at this point and could use another pair of eyes.
Activity
dsnet commentedon Oct 22, 2017
\cc @randall77 @cherrymui
cherrymui commentedon Oct 23, 2017
I cannot reproduce it, with both Go 1.9 and tip (006bc57), at least on darwin/amd64. Then binaries generated with
-l -land-l -l -mare identical.@huguesb, which version of Go, and what GOARCH/GOOS are you using?
huguesb commentedon Oct 23, 2017
@cherrymui I'm on darwin/amd64. This issue is reliably reproducible with CL72490 applied on top of tip (
006bc57095):cherrymui commentedon Oct 23, 2017
Ok, it seems there is something racy here, which may or may not be related to your CL.
With your CL applied, I run
go build -gcflags='-l -l' x.goin a loop, and see sporadic crashes of the compiler. Since Go 1.9 the compiler, if invoked bycmd/go, by default runs concurrently. Butcmd/godisables concurrent compilation if-mis specified, so the race does not happen there.Without concurrent compilation, it still generates identical binaries:
randall77 commentedon Oct 23, 2017
@josharian
huguesb commentedon Oct 23, 2017
@cherrymui thanks for taking a look, I did not think of concurrent compilation. Is there an easy way of building the compiler with the race detector enabled? The naive
GO_GCFLAGS=-race ./make.bashdoesn't seem to workcherrymui commentedon Oct 24, 2017
I'm actually not sure. I guess
go install -race cmd/compilewill build a compiler with race detector enabled, and then you can test it by using it to build other programs.huguesb commentedon Oct 25, 2017
I tried building a race-enabled compiler with
go install -a -race cmd/compile(and same forcmd/go) but it did not make any observable difference. The issue is still reproducible and I'm not seeing any logs from the race detector when I use the newly built compiler to build other programshuguesb commentedon Oct 25, 2017
I finally figured out the problem: there was a subtle bug in my change which resulted in references to captured variables inside the inlined body using the same node as inside the regular closure body, instead of being substituted with local temporaries in every inlined location. I'm guessing the SSA phase either modifies the AST in-place or uses AST nodes as map keys, which leads to the observed issue when the regular closure body and a function with the inlined closure are compiled concurrently. Is this documented somewhere?
mdempsky commentedon Oct 25, 2017
Yes, order and walk mutate the function's AST.
I don't think so. The general rule is the goroutine that's compiling a function has exclusive ownership over that function during concurrent compilation. If you need to do interprocedural stuff, you should do it before concurrent compilation.
Some stuff is safe to inspect externally. For example, the ONAME Nodes representing the function name and parameters are safe for basic use, but these basically become read-only once the early analysis phases are done.
huguesb commentedon Oct 25, 2017
That's not the issue here though. Order and walk are not executed concurrently at this point, only the SSA phase is.
cherrymui commentedon Oct 26, 2017
Yes, there are a few in cmd/compile/internal/gc/ssa.go, also plive.go.