Skip to content

transform: PtrToInt and IntToPtr do not escape #4889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions transform/allocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func valueEscapesAt(value llvm.Value) llvm.Value {
case llvm.ICmp:
// Comparing pointers don't let the pointer escape.
// This is often a compiler-inserted nil check.
case llvm.PtrToInt:
// Pointer to int conversion doesn't let the pointer escape.
case llvm.IntToPtr:
// Int to pointer conversion doesn't let the pointer escape.
default:
// Unknown instruction, might escape.
return use
Expand Down
23 changes: 15 additions & 8 deletions transform/testdata/allocs2.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import "unsafe"

func main() {
n1 := 5
derefInt(&n1)

// This should eventually be modified to not escape.
n2 := 6 // OUT: object allocated on the heap: escapes at line 9
n2 := 6 // OUT: object allocated on the heap: escapes at line 11
returnIntPtr(&n2)

s1 := make([]int, 3)
Expand All @@ -15,36 +17,36 @@ func main() {
readIntSlice(s2[:])

// This should also be modified to not escape.
s3 := make([]int, 3) // OUT: object allocated on the heap: escapes at line 19
s3 := make([]int, 3) // OUT: object allocated on the heap: escapes at line 21
returnIntSlice(s3)

useSlice(make([]int, getUnknownNumber())) // OUT: object allocated on the heap: size is not constant

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) // OUT: object allocated on the heap: escapes at line 29
_ = append(s5, 5)

s6 := make([]int, 3)
s7 := []int{1, 2, 3}
copySlice(s6, s7)

c1 := getComplex128() // OUT: object allocated on the heap: escapes at line 34
c1 := getComplex128() // OUT: object allocated on the heap: escapes at line 36
useInterface(c1)

n3 := 5
func() int {
return n3
}()

callVariadic(3, 5, 8) // OUT: object allocated on the heap: escapes at line 41
callVariadic(3, 5, 8) // OUT: object allocated on the heap: escapes at line 43

s8 := []int{3, 5, 8} // OUT: object allocated on the heap: escapes at line 44
s8 := []int{3, 5, 8} // OUT: object allocated on the heap: escapes at line 46
callVariadic(s8...)

n4 := 3 // OUT: object allocated on the heap: escapes at line 48
n5 := 7 // OUT: object allocated on the heap: escapes at line 48
n4 := 3 // OUT: object allocated on the heap: escapes at line 50
n5 := 7 // OUT: object allocated on the heap: escapes at line 50
func() {
n4 = n5
}()
Expand All @@ -58,6 +60,11 @@ func main() {
var rbuf [5]rune
s = string(rbuf[:])
println(s)

// This shouldn't escape either.
n6 := 3
n7 := uintptr(unsafe.Pointer(&n6))
println(n7)
}

func derefInt(x *int) int {
Expand Down