diff options
author | David Chase <drchase@google.com> | 2015-03-26 16:36:15 -0400 |
---|---|---|
committer | David Chase <drchase@google.com> | 2015-05-01 13:47:20 +0000 |
commit | 7fbb1b36c37ac49db78042adc7533fb4ab83a4bc (patch) | |
tree | 053b665f5469d0ba1ad6bf82dd7f4469818bd2d6 /test/escape_field.go | |
parent | 4044adedf7eb8c3ab89f00479965be62e029f350 (diff) | |
download | go-7fbb1b36c37ac49db78042adc7533fb4ab83a4bc.tar.gz go-7fbb1b36c37ac49db78042adc7533fb4ab83a4bc.zip |
cmd/internal/gc: improve flow of input params to output params
This includes the following information in the per-function summary:
outK = paramJ encoded in outK bits for paramJ
outK = *paramJ encoded in outK bits for paramJ
heap = paramJ EscHeap
heap = *paramJ EscContentEscapes
Note that (currently) if the address of a parameter is taken and
returned, necessarily a heap allocation occurred to contain that
reference, and the heap can never refer to stack, therefore the
parameter and everything downstream from it escapes to the heap.
The per-function summary information now has a tuneable number of bits
(2 is probably noticeably better than 1, 3 is likely overkill, but it
is now easy to check and the -m debugging output includes information
that allows you to figure out if more would be better.)
A new test was added to check pointer flow through struct-typed and
*struct-typed parameters and returns; some of these are sensitive to
the number of summary bits, and ought to yield better results with a
more competent escape analysis algorithm. Another new test checks
(some) correctness with array parameters, results, and operations.
The old analysis inferred a piece of plan9 runtime was non-escaping by
counteracting overconservative analysis with buggy analysis; with the
bug fixed, the result was too conservative (and it's not easy to fix
in this framework) so the source code was tweaked to get the desired
result. A test was added against the discovered bug.
The escape analysis was further improved splitting the "level" into
3 parts, one tracking the conventional "level" and the other two
computing the highest-level-suffix-from-copy, which is used to
generally model the cancelling effect of indirection applied to
address-of.
With the improved escape analysis enabled, it was necessary to
modify one of the runtime tests because it now attempts to allocate
too much on the (small, fixed-size) G0 (system) stack and this
failed the test.
Compiling src/std after touching src/runtime/*.go with -m logging
turned on shows 420 fewer heap allocation sites (10538 vs 10968).
Profiling allocations in src/html/template with
for i in {1..5} ;
do go tool 6g -memprofile=mastx.${i}.prof -memprofilerate=1 *.go;
go tool pprof -alloc_objects -text mastx.${i}.prof ;
done
showed a 15% reduction in allocations performed by the compiler.
Update #3753
Update #4720
Fixes #10466
Change-Id: I0fd97d5f5ac527b45f49e2218d158a6e89951432
Reviewed-on: https://go-review.googlesource.com/8202
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'test/escape_field.go')
-rw-r--r-- | test/escape_field.go | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/test/escape_field.go b/test/escape_field.go index dcf8a31d2b..16d1e741b8 100644 --- a/test/escape_field.go +++ b/test/escape_field.go @@ -23,7 +23,7 @@ type Y struct { func field0() { i := 0 // ERROR "moved to heap: i$" var x X - x.p1 = &i // ERROR "&i escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" sink = x.p1 // ERROR "x\.p1 escapes to heap" } @@ -31,7 +31,7 @@ func field1() { i := 0 // ERROR "moved to heap: i$" var x X // BAD: &i should not escape - x.p1 = &i // ERROR "&i escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" sink = x.p2 // ERROR "x\.p2 escapes to heap" } @@ -39,7 +39,7 @@ func field3() { i := 0 // ERROR "moved to heap: i$" var x X x.p1 = &i // ERROR "&i escapes to heap$" - sink = x // ERROR "x escapes to heap" + sink = x // ERROR "x escapes to heap" } func field4() { @@ -54,22 +54,21 @@ func field5() { i := 0 // ERROR "moved to heap: i$" var x X // BAD: &i should not escape here - x.a[0] = &i // ERROR "&i escapes to heap$" + x.a[0] = &i // ERROR "&i escapes to heap$" sink = x.a[1] // ERROR "x\.a\[1\] escapes to heap" } // BAD: we are not leaking param x, only x.p2 -func field6(x *X) { // ERROR "leaking param: x$" +func field6(x *X) { // ERROR "leaking param content: x$" sink = x.p2 // ERROR "x\.p2 escapes to heap" } func field6a() { - i := 0 // ERROR "moved to heap: i$" - var x X // ERROR "moved to heap: x$" + i := 0 // ERROR "moved to heap: i$" + var x X // BAD: &i should not escape - x.p1 = &i // ERROR "&i escapes to heap$" - // BAD: &x should not escape - field6(&x) // ERROR "&x escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" + field6(&x) // ERROR "field6a &x does not escape" } func field7() { @@ -116,40 +115,40 @@ func field10() { func field11() { i := 0 // ERROR "moved to heap: i$" x := X{p1: &i} // ERROR "&i escapes to heap$" - sink = x.p1 // ERROR "x\.p1 escapes to heap" + sink = x.p1 // ERROR "x\.p1 escapes to heap" } func field12() { i := 0 // ERROR "moved to heap: i$" // BAD: &i should not escape x := X{p1: &i} // ERROR "&i escapes to heap$" - sink = x.p2 // ERROR "x\.p2 escapes to heap" + sink = x.p2 // ERROR "x\.p2 escapes to heap" } func field13() { i := 0 // ERROR "moved to heap: i$" x := &X{p1: &i} // ERROR "&i escapes to heap$" "field13 &X literal does not escape$" - sink = x.p1 // ERROR "x\.p1 escapes to heap" + sink = x.p1 // ERROR "x\.p1 escapes to heap" } func field14() { i := 0 // ERROR "moved to heap: i$" // BAD: &i should not escape x := &X{p1: &i} // ERROR "&i escapes to heap$" "field14 &X literal does not escape$" - sink = x.p2 // ERROR "x\.p2 escapes to heap" + sink = x.p2 // ERROR "x\.p2 escapes to heap" } func field15() { i := 0 // ERROR "moved to heap: i$" x := &X{p1: &i} // ERROR "&X literal escapes to heap$" "&i escapes to heap$" - sink = x // ERROR "x escapes to heap" + sink = x // ERROR "x escapes to heap" } func field16() { i := 0 // ERROR "moved to heap: i$" var x X // BAD: &i should not escape - x.p1 = &i // ERROR "&i escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" var iface interface{} = x // ERROR "x escapes to heap" x1 := iface.(X) sink = x1.p2 // ERROR "x1\.p2 escapes to heap" @@ -158,7 +157,7 @@ func field16() { func field17() { i := 0 // ERROR "moved to heap: i$" var x X - x.p1 = &i // ERROR "&i escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" var iface interface{} = x // ERROR "x escapes to heap" x1 := iface.(X) sink = x1.p1 // ERROR "x1\.p1 escapes to heap" @@ -168,8 +167,8 @@ func field18() { i := 0 // ERROR "moved to heap: i$" var x X // BAD: &i should not escape - x.p1 = &i // ERROR "&i escapes to heap$" + x.p1 = &i // ERROR "&i escapes to heap$" var iface interface{} = x // ERROR "x escapes to heap" - y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized. - sink = y // ERROR "y escapes to heap" + y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized. + sink = y // ERROR "y escapes to heap" } |