Skip to content

Commit 1054085

Browse files
randall77broady
authored andcommitted
[release-branch.go1.8] cmd/compile: fix store chain in schedule pass
Cherry-pick of CL 43294. Tuple ops are weird. They are essentially a pair of ops, one which consumes a mem and one which generates a mem (the Select1). The schedule pass didn't handle these quite right. Fix the scheduler to include both parts of the paired op in the store chain. That makes sure that loads are correctly ordered with respect to the first of the pair. Add a check for the ssacheck builder, that there is only one live store at a time. I thought we already had such a check, but apparently not... Fixes #20335 Change-Id: I59eb3446a329100af38d22820b1ca2190ca46a78 Reviewed-on: https://go-review.googlesource.com/43411 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 18a13d3 commit 1054085

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/cmd/compile/internal/ssa/check.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,39 @@ func checkFunc(f *Func) {
294294
}
295295
}
296296
}
297+
298+
// Check that if a tuple has a memory type, it is second.
299+
for _, b := range f.Blocks {
300+
for _, v := range b.Values {
301+
if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
302+
f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
303+
}
304+
}
305+
}
306+
307+
// Check that only one memory is live at any point.
308+
// TODO: make this check examine interblock.
309+
if f.scheduled {
310+
for _, b := range f.Blocks {
311+
var mem *Value // the live memory
312+
for _, v := range b.Values {
313+
if v.Op != OpPhi {
314+
for _, a := range v.Args {
315+
if a.Type.IsMemory() || a.Type.IsTuple() && a.Type.FieldType(1).IsMemory() {
316+
if mem == nil {
317+
mem = a
318+
} else if mem != a {
319+
f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
320+
}
321+
}
322+
}
323+
}
324+
if v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory() {
325+
mem = v
326+
}
327+
}
328+
}
329+
}
297330
}
298331

299332
// domCheck reports whether x dominates y (including x==y).

src/cmd/compile/internal/ssa/schedule.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,20 @@ func schedule(f *Func) {
148148
}
149149
}
150150

151+
// TODO: make this logic permanent in types.IsMemory?
152+
isMem := func(v *Value) bool {
153+
return v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory()
154+
}
155+
151156
for _, b := range f.Blocks {
152157
// Find store chain for block.
153158
// Store chains for different blocks overwrite each other, so
154159
// the calculated store chain is good only for this block.
155160
for _, v := range b.Values {
156-
if v.Op != OpPhi && v.Type.IsMemory() {
157-
mem := v
158-
if v.Op == OpSelect1 {
159-
v = v.Args[0]
160-
}
161+
if v.Op != OpPhi && isMem(v) {
161162
for _, w := range v.Args {
162-
if w.Type.IsMemory() {
163-
nextMem[w.ID] = mem
163+
if isMem(w) {
164+
nextMem[w.ID] = v
164165
}
165166
}
166167
}
@@ -179,15 +180,15 @@ func schedule(f *Func) {
179180
uses[w.ID]++
180181
}
181182
// Any load must come before the following store.
182-
if v.Type.IsMemory() || !w.Type.IsMemory() {
183-
continue // not a load
184-
}
185-
s := nextMem[w.ID]
186-
if s == nil || s.Block != b {
187-
continue
183+
if !isMem(v) && isMem(w) {
184+
// v is a load.
185+
s := nextMem[w.ID]
186+
if s == nil || s.Block != b {
187+
continue
188+
}
189+
additionalArgs[s.ID] = append(additionalArgs[s.ID], v)
190+
uses[v.ID]++
188191
}
189-
additionalArgs[s.ID] = append(additionalArgs[s.ID], v)
190-
uses[v.ID]++
191192
}
192193
}
193194

test/fixedbugs/issue20335.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile
2+
3+
// Copyright 2017 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Issue 20335: don't reorder loads with stores.
8+
// This test should fail on the ssacheck builder
9+
// without the fix in the CL that added this file.
10+
// TODO: check the generated assembly?
11+
12+
package a
13+
14+
import "sync/atomic"
15+
16+
func f(p, q *int32) bool {
17+
x := *q
18+
return atomic.AddInt32(p, 1) == x
19+
}

0 commit comments

Comments
 (0)