transform: add allocation alias regression cases

Add allocation diagnostics coverage for pointer-returning helpers, aggregate
slice returns, and conditional pointer returns before changing the allocation
optimizer.

The golden files record the current heap-allocation behavior so later commits
show exactly which diagnostics each optimizer improvement removes.
This commit is contained in:
Jake Bailey
2026-06-12 11:21:53 -07:00
committed by Ron Evans
parent 11c2b76e29
commit 335b085fd2
3 changed files with 83 additions and 22 deletions
+55 -2
View File
@@ -9,7 +9,6 @@ func main() {
n1 := 5
derefInt(&n1)
// This should eventually be modified to not escape.
n2 := 6
returnIntPtr(&n2)
@@ -19,7 +18,6 @@ func main() {
s2 := [3]int{}
readIntSlice(s2[:])
// This should also be modified to not escape.
s3 := make([]int, 3)
returnIntSlice(s3)
@@ -78,6 +76,61 @@ func main() {
keepAliveNoEscape(unsafe.Pointer(&dmaBuf2[0]))
}
type vector3 [3]float32
func scaleVector3(vec *vector3, f float32) *vector3 {
vec[0] *= f
vec[1] *= f
vec[2] *= f
return vec
}
func crossVector3(a, b *vector3) vector3 {
return vector3{
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0],
}
}
func nonEscapingReturnedPointer() vector3 {
a := vector3{1, 2, 3}
b := vector3{4, 5, 6}
c := scaleVector3(&b, 0.5)
return crossVector3(&a, c)
}
var escapedSlice []int
func escapingReturnedSlice() {
s := make([]int, 3)
escapedSlice = returnIntSlice(s)
}
var escapedVector3 *vector3
func escapingReturnedPointer() {
b := vector3{4, 5, 6}
c := scaleVector3(&b, 0.5)
escapedVector3 = c
}
func recursiveScaleVector3(vec *vector3, n int) *vector3 {
if n == 0 {
return vec
}
return recursiveScaleVector3(vec, n-1)
}
func recursiveReturnedPointer() vector3 {
b := vector3{4, 5, 6}
c := recursiveScaleVector3(&b, 1)
return *c
}
func derefInt(x *int) int {
return *x
}
+14 -10
View File
@@ -1,10 +1,14 @@
testdata/allocs2.go:13.1,13.9 1 0
testdata/allocs2.go:23.1,23.22 1 0
testdata/allocs2.go:26.1,26.43 1 0
testdata/allocs2.go:28.1,28.25 1 0
testdata/allocs2.go:31.1,31.22 1 0
testdata/allocs2.go:38.1,38.23 1 0
testdata/allocs2.go:46.1,46.23 1 0
testdata/allocs2.go:48.1,48.22 1 0
testdata/allocs2.go:51.1,51.9 1 0
testdata/allocs2.go:52.1,52.9 1 0
testdata/allocs2.go:12.1,12.9 1 0
testdata/allocs2.go:21.1,21.22 1 0
testdata/allocs2.go:24.1,24.43 1 0
testdata/allocs2.go:26.1,26.25 1 0
testdata/allocs2.go:29.1,29.22 1 0
testdata/allocs2.go:36.1,36.23 1 0
testdata/allocs2.go:44.1,44.23 1 0
testdata/allocs2.go:46.1,46.22 1 0
testdata/allocs2.go:49.1,49.9 1 0
testdata/allocs2.go:50.1,50.9 1 0
testdata/allocs2.go:98.1,98.23 1 0
testdata/allocs2.go:107.1,107.21 1 0
testdata/allocs2.go:114.1,114.23 1 0
testdata/allocs2.go:128.1,128.23 1 0
+14 -10
View File
@@ -1,10 +1,14 @@
testdata/allocs2.go:13:2: object allocated on the heap: escapes at line 14
testdata/allocs2.go:23:12: object allocated on the heap: escapes at line 24
testdata/allocs2.go:26:15: object allocated on the heap: size is not constant
testdata/allocs2.go:28:12: object allocated on the heap: object size 300 exceeds maximum stack allocation size 256
testdata/allocs2.go:31:12: object allocated on the heap: escapes at line 32
testdata/allocs2.go:38:21: object allocated on the heap: escapes at line 39
testdata/allocs2.go:46:22: object allocated on the heap: escapes at line 46
testdata/allocs2.go:48:13: object allocated on the heap: escapes at line 49
testdata/allocs2.go:51:2: object allocated on the heap: escapes at line 53
testdata/allocs2.go:52:2: object allocated on the heap: escapes at line 53
testdata/allocs2.go:12:2: object allocated on the heap: escapes at line 13
testdata/allocs2.go:21:12: object allocated on the heap: escapes at line 22
testdata/allocs2.go:24:15: object allocated on the heap: size is not constant
testdata/allocs2.go:26:12: object allocated on the heap: object size 300 exceeds maximum stack allocation size 256
testdata/allocs2.go:29:12: object allocated on the heap: escapes at line 30
testdata/allocs2.go:36:21: object allocated on the heap: escapes at line 37
testdata/allocs2.go:44:22: object allocated on the heap: escapes at line 44
testdata/allocs2.go:46:13: object allocated on the heap: escapes at line 47
testdata/allocs2.go:49:2: object allocated on the heap: escapes at line 51
testdata/allocs2.go:50:2: object allocated on the heap: escapes at line 51
testdata/allocs2.go:98:2: object allocated on the heap: escapes at line 100
testdata/allocs2.go:107:11: object allocated on the heap: escapes at line 108
testdata/allocs2.go:114:2: object allocated on the heap: escapes at line 116
testdata/allocs2.go:128:2: object allocated on the heap: escapes at line 130