Skip to content

Commit 8607b2e

Browse files
committed
cmd/compile: optimize A->B->C Moves that include VarDefs
We have an existing optimization that recognizes memory moves of the form A -> B -> C and converts them into A -> C, in the hopes that the store to B will be end up being dead and thus eliminated. However, when A, B, and C are large types, the front end sometimes emits VarDef ops for the moves. This change adds an optimization to match that pattern. This required changing an old compiler test. The test assumed that a temporary was required to deal with a large return value. With this optimization in place, that temporary ended up being eliminated. Triggers 649 times during 'go build -a std cmd'. Cuts 16k off cmd/go. name old object-bytes new object-bytes delta Template 507kB ± 0% 507kB ± 0% -0.15% (p=0.008 n=5+5) Unicode 225kB ± 0% 225kB ± 0% ~ (all equal) GoTypes 1.85MB ± 0% 1.85MB ± 0% ~ (all equal) Flate 328kB ± 0% 328kB ± 0% ~ (all equal) GoParser 402kB ± 0% 402kB ± 0% -0.00% (p=0.008 n=5+5) Reflect 1.41MB ± 0% 1.41MB ± 0% -0.20% (p=0.008 n=5+5) Tar 458kB ± 0% 458kB ± 0% ~ (all equal) XML 601kB ± 0% 599kB ± 0% -0.21% (p=0.008 n=5+5) Change-Id: I9b5f25c8663a0b772ad1ee51fa61f74b74d26dd3 Reviewed-on: https://go-review.googlesource.com/c/143479 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Munday <[email protected]>
1 parent f9fff45 commit 8607b2e

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

src/cmd/compile/internal/ssa/gen/generic.rules

+9
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,15 @@
18151815
&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
18161816
-> (Move {t1} [s] dst src midmem)
18171817

1818+
// Same, but for large types that require VarDefs.
1819+
(Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
1820+
&& t1.(*types.Type).Compare(t2.(*types.Type)) == types.CMPeq
1821+
&& isSamePtr(tmp1, tmp2)
1822+
&& isStackPtr(src)
1823+
&& disjoint(src, s, tmp2, s)
1824+
&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
1825+
-> (Move {t1} [s] dst src midmem)
1826+
18181827
// Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
18191828
// However, this rule is needed to prevent the previous rule from looping forever in such cases.
18201829
(Move dst src mem) && isSamePtr(dst, src) -> mem

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

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue20780.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
// We have a limit of 1GB for stack frames.
88
// Make sure we include the callee args section.
9-
// (The dispatch wrapper which implements (*S).f
10-
// copies the return value from f to a stack temp, then
11-
// from that stack temp to the return value of (*S).f.
12-
// It uses ~800MB for each section.)
139

1410
package main
1511

16-
type S struct {
17-
i interface {
18-
f() [800e6]byte
19-
}
12+
func f() { // ERROR "stack frame too large"
13+
var x [800e6]byte
14+
g(x)
15+
return
2016
}
17+
18+
//go:noinline
19+
func g([800e6]byte) {}

0 commit comments

Comments
 (0)