Skip to content

Commit 8691402

Browse files
committed
transform: don't escape append arguments if the return value doesn't
1 parent 0edeaf6 commit 8691402

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

transform/allocs.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,18 @@ func valueEscapesAt(value llvm.Value) llvm.Value {
154154
return use
155155
}
156156
case llvm.Call:
157-
if !hasFlag(use, value, "nocapture") {
158-
return use
157+
if hasFlag(use, value, "nocapture") {
158+
break
159+
}
160+
// If built-in append function escapes its first argument if and
161+
// only if the return value escapes.
162+
if fn := use.CalledValue(); !fn.IsAFunction().IsNil() && fn.Name() == "runtime.sliceAppend" {
163+
if at := valueEscapesAt(use); !at.IsNil() {
164+
return at
165+
}
166+
break
159167
}
168+
return use
160169
case llvm.ICmp:
161170
// Comparing pointers don't let the pointer escape.
162171
// This is often a compiler-inserted nil check.

transform/testdata/allocs2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323
s4 := make([]byte, 300) // OUT: object allocated on the heap: object size 300 exceeds maximum stack allocation size 256
2424
readByteSlice(s4)
2525

26-
s5 := make([]int, 4) // OUT: object allocated on the heap: escapes at line 27
26+
s5 := make([]int, 4)
2727
_ = append(s5, 5)
2828

2929
s6 := make([]int, 3)

0 commit comments

Comments
 (0)