Skip to content

Commit

Permalink
compiler: mark the source argument to the append builtin as not escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasnaur committed Nov 1, 2024
1 parent 0edeaf6 commit ab62410
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compiler/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
// > allocsize argument to signify "not present."
llvmFn.AddFunctionAttr(c.ctx.CreateEnumAttribute(llvm.AttributeKindID("allocsize"), 0x0000_0000_ffff_ffff))
case "runtime.sliceAppend":
// The slice doesn't escape.
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
// Appending a slice will only read the to-be-appended slice, it won't
// be modified.
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
Expand Down
2 changes: 1 addition & 1 deletion compiler/testdata/slice.ll
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ entry:
ret { ptr, i32, i32 } %4
}

declare { ptr, i32, i32 } @runtime.sliceAppend(ptr, ptr nocapture readonly, i32, i32, i32, i32, ptr) #1
declare { ptr, i32, i32 } @runtime.sliceAppend(ptr nocapture, ptr nocapture readonly, i32, i32, i32, i32, ptr) #1

; Function Attrs: nounwind
define hidden { ptr, i32, i32 } @main.sliceAppendSlice(ptr %ints.data, i32 %ints.len, i32 %ints.cap, ptr %added.data, i32 %added.len, i32 %added.cap, ptr %context) unnamed_addr #2 {
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 ab62410

Please sign in to comment.