| 
									
										
											  
											
												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>
											
										 
											2015-03-26 16:36:15 -04:00
										 |  |  | // errorcheck -0 -m -l | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-10 14:32:26 -07:00
										 |  |  | // Copyright 2015 The Go Authors. All rights reserved. | 
					
						
							| 
									
										
											  
											
												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>
											
										 
											2015-03-26 16:36:15 -04:00
										 |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-20 15:16:34 -04:00
										 |  |  | // Test escape analysis for arrays and some large things | 
					
						
							| 
									
										
											  
											
												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>
											
										 
											2015-03-26 16:36:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | package foo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Ssink *string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type U [2]*string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func bar(a, b *string) U { // ERROR "leaking param: a to result ~r2 level=0$" "leaking param: b to result ~r2 level=0$" | 
					
						
							|  |  |  | 	return U{a, b} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func foo(x U) U { // ERROR "leaking param: x to result ~r1 level=0$" | 
					
						
							|  |  |  | 	return U{x[1], x[0]} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func bff(a, b *string) U { // ERROR "leaking param: a to result ~r2 level=0$" "leaking param: b to result ~r2 level=0$" | 
					
						
							|  |  |  | 	return foo(foo(bar(a, b))) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func tbff1() *string { | 
					
						
							|  |  |  | 	a := "cat" | 
					
						
							|  |  |  | 	b := "dog"       // ERROR "moved to heap: b$" | 
					
						
							|  |  |  | 	u := bff(&a, &b) // ERROR "tbff1 &a does not escape$" "tbff1 &b does not escape$" | 
					
						
							|  |  |  | 	_ = u[0] | 
					
						
							|  |  |  | 	return &b // ERROR "&b escapes to heap$" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BAD: need fine-grained analysis to track u[0] and u[1] differently. | 
					
						
							|  |  |  | func tbff2() *string { | 
					
						
							|  |  |  | 	a := "cat"       // ERROR "moved to heap: a$" | 
					
						
							|  |  |  | 	b := "dog"       // ERROR "moved to heap: b$" | 
					
						
							|  |  |  | 	u := bff(&a, &b) // ERROR "&a escapes to heap$" "&b escapes to heap$" | 
					
						
							|  |  |  | 	_ = u[0] | 
					
						
							|  |  |  | 	return u[1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func car(x U) *string { // ERROR "leaking param: x to result ~r1 level=0$" | 
					
						
							|  |  |  | 	return x[0] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BAD: need fine-grained analysis to track x[0] and x[1] differently. | 
					
						
							|  |  |  | func fun(x U, y *string) *string { // ERROR "leaking param: x to result ~r2 level=0$" "leaking param: y to result ~r2 level=0$" | 
					
						
							|  |  |  | 	x[0] = y | 
					
						
							|  |  |  | 	return x[1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func fup(x *U, y *string) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param: y$" | 
					
						
							|  |  |  | 	x[0] = y // leaking y to heap is intended | 
					
						
							|  |  |  | 	return x[1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func fum(x *U, y **string) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param content: y$" | 
					
						
							|  |  |  | 	x[0] = *y | 
					
						
							|  |  |  | 	return x[1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func fuo(x *U, y *U) *string { // ERROR "leaking param: x to result ~r2 level=1$" "leaking param content: y$" | 
					
						
							|  |  |  | 	x[0] = y[0] | 
					
						
							|  |  |  | 	return x[1] | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-20 15:16:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // These two tests verify that: | 
					
						
							|  |  |  | // small array literals are stack allocated; | 
					
						
							|  |  |  | // pointers stored in small array literals do not escape; | 
					
						
							|  |  |  | // large array literals are heap allocated; | 
					
						
							|  |  |  | // pointers stored in large array literals escape. | 
					
						
							|  |  |  | func hugeLeaks1(x **string, y **string) { // ERROR "leaking param content: x" "hugeLeaks1 y does not escape" "mark escaped content: x" | 
					
						
							|  |  |  | 	a := [10]*string{*y} | 
					
						
							|  |  |  | 	_ = a | 
					
						
							|  |  |  | 	// 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger. | 
					
						
							|  |  |  | 	b := [4000000]*string{*x} // ERROR "moved to heap: b" | 
					
						
							|  |  |  | 	_ = b | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func hugeLeaks2(x *string, y *string) { // ERROR "leaking param: x" "hugeLeaks2 y does not escape" | 
					
						
							|  |  |  | 	a := [10]*string{y} | 
					
						
							|  |  |  | 	_ = a | 
					
						
							|  |  |  | 	// 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger. | 
					
						
							|  |  |  | 	b := [4000000]*string{x} // ERROR "moved to heap: b" | 
					
						
							|  |  |  | 	_ = b | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BAD: x need not leak. | 
					
						
							|  |  |  | func doesNew1(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" | 
					
						
							|  |  |  | 	a := new([10]*string) // ERROR "new\(\[10\]\*string\) does not escape" | 
					
						
							|  |  |  | 	a[0] = x | 
					
						
							|  |  |  | 	b := new([65537]*string) // ERROR "new\(\[65537\]\*string\) escapes to heap" | 
					
						
							|  |  |  | 	b[0] = y | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type a10 struct { | 
					
						
							|  |  |  | 	s *string | 
					
						
							|  |  |  | 	i [10]int32 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type a65537 struct { | 
					
						
							|  |  |  | 	s *string | 
					
						
							|  |  |  | 	i [65537]int32 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BAD: x need not leak. | 
					
						
							|  |  |  | func doesNew2(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" | 
					
						
							|  |  |  | 	a := new(a10) // ERROR "new\(a10\) does not escape" | 
					
						
							|  |  |  | 	a.s = x | 
					
						
							|  |  |  | 	b := new(a65537) // ERROR "new\(a65537\) escapes to heap" | 
					
						
							|  |  |  | 	b.s = y | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // BAD: x need not leak. | 
					
						
							|  |  |  | func doesMakeSlice(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y" | 
					
						
							|  |  |  | 	a := make([]*string, 10) // ERROR "make\(\[\]\*string, 10\) does not escape" | 
					
						
							|  |  |  | 	a[0] = x | 
					
						
							|  |  |  | 	b := make([]*string, 65537) // ERROR "make\(\[\]\*string, 65537\) escapes to heap" | 
					
						
							|  |  |  | 	b[0] = y | 
					
						
							|  |  |  | } |