Skip to content

Commit

Permalink
transform: don't escape append arguments if the return value doesn't
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasnaur committed Nov 1, 2024
1 parent 0edeaf6 commit 8691402
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions transform/allocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,18 @@ func valueEscapesAt(value llvm.Value) llvm.Value {
return use
}
case llvm.Call:
if !hasFlag(use, value, "nocapture") {
return use
if hasFlag(use, value, "nocapture") {
break
}
// If built-in append function escapes its first argument if and
// only if the return value escapes.
if fn := use.CalledValue(); !fn.IsAFunction().IsNil() && fn.Name() == "runtime.sliceAppend" {
if at := valueEscapesAt(use); !at.IsNil() {
return at
}
break
}
return use
case llvm.ICmp:
// Comparing pointers don't let the pointer escape.
// This is often a compiler-inserted nil check.
Expand Down
2 changes: 1 addition & 1 deletion transform/testdata/allocs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
s4 := make([]byte, 300) // OUT: object allocated on the heap: object size 300 exceeds maximum stack allocation size 256
readByteSlice(s4)

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

s6 := make([]int, 3)
Expand Down

0 comments on commit 8691402

Please sign in to comment.