Skip to content

Commit 978af9c

Browse files
committed
cmd/compile: fix store chain in schedule pass
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/43294 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent e5bb5e3 commit 978af9c

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,39 @@ func checkFunc(f *Func) {
309309
}
310310
}
311311
}
312+
313+
// Check that if a tuple has a memory type, it is second.
314+
for _, b := range f.Blocks {
315+
for _, v := range b.Values {
316+
if v.Type.IsTuple() && v.Type.FieldType(0).IsMemory() {
317+
f.Fatalf("memory is first in a tuple: %s\n", v.LongString())
318+
}
319+
}
320+
}
321+
322+
// Check that only one memory is live at any point.
323+
// TODO: make this check examine interblock.
324+
if f.scheduled {
325+
for _, b := range f.Blocks {
326+
var mem *Value // the live memory
327+
for _, v := range b.Values {
328+
if v.Op != OpPhi {
329+
for _, a := range v.Args {
330+
if a.Type.IsMemory() || a.Type.IsTuple() && a.Type.FieldType(1).IsMemory() {
331+
if mem == nil {
332+
mem = a
333+
} else if mem != a {
334+
f.Fatalf("two live mems @ %s: %s and %s", v, mem, a)
335+
}
336+
}
337+
}
338+
}
339+
if v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory() {
340+
mem = v
341+
}
342+
}
343+
}
344+
}
312345
}
313346

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

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

+16-15
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,20 @@ func schedule(f *Func) {
132132
}
133133
}
134134

135+
// TODO: make this logic permanent in types.IsMemory?
136+
isMem := func(v *Value) bool {
137+
return v.Type.IsMemory() || v.Type.IsTuple() && v.Type.FieldType(1).IsMemory()
138+
}
139+
135140
for _, b := range f.Blocks {
136141
// Find store chain for block.
137142
// Store chains for different blocks overwrite each other, so
138143
// the calculated store chain is good only for this block.
139144
for _, v := range b.Values {
140-
if v.Op != OpPhi && v.Type.IsMemory() {
141-
mem := v
142-
if v.Op == OpSelect1 {
143-
v = v.Args[0]
144-
}
145+
if v.Op != OpPhi && isMem(v) {
145146
for _, w := range v.Args {
146-
if w.Type.IsMemory() {
147-
nextMem[w.ID] = mem
147+
if isMem(w) {
148+
nextMem[w.ID] = v
148149
}
149150
}
150151
}
@@ -163,15 +164,15 @@ func schedule(f *Func) {
163164
uses[w.ID]++
164165
}
165166
// Any load must come before the following store.
166-
if v.Type.IsMemory() || !w.Type.IsMemory() {
167-
continue // not a load
168-
}
169-
s := nextMem[w.ID]
170-
if s == nil || s.Block != b {
171-
continue
167+
if !isMem(v) && isMem(w) {
168+
// v is a load.
169+
s := nextMem[w.ID]
170+
if s == nil || s.Block != b {
171+
continue
172+
}
173+
additionalArgs[s.ID] = append(additionalArgs[s.ID], v)
174+
uses[v.ID]++
172175
}
173-
additionalArgs[s.ID] = append(additionalArgs[s.ID], v)
174-
uses[v.ID]++
175176
}
176177
}
177178

test/fixedbugs/issue20335.go

+19
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)