go runtime/stubs.go noescape

152 views
Skip to first unread message

tokers

unread,
Oct 12, 2019, 4:47:03 AM10/12/19
to golang-nuts
Hello!

Recently I read some go source code and when I read the noescape function in runtime/stubs.go, I have a doubt about it.

// noescape hides a pointer from escape analysis.  noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input.  noescape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
   x
:= uintptr(p)
   
return unsafe.Pointer(x ^ 0)
}


I don't know what's the purpose of the exclusive or operation? Does it necessary?

tokers

unread,
Oct 12, 2019, 4:48:32 AM10/12/19
to golang-nuts
Is this operation necessary?

Ian Lance Taylor

unread,
Oct 15, 2019, 6:49:34 PM10/15/19
to tokers, golang-nuts
The function comment seems clear. Can you be more specific about your question?

This is necessary because sometimes the runtime needs to not allocate
a value that would otherwise be allocated. For example, the runtime
includes the implementation of the heap allocator, and clearly that
implementation cannot itself allocate anything. The noescape function
is used where allocation would otherwise occur, to ensure that it does
not actually allocate.

Ian

Kevin Malachowski

unread,
Oct 15, 2019, 7:43:42 PM10/15/19
to golang-nuts
I believe the question was about the specific implementation of noescape, specifically the "exclusive or" in that unsafe expression.

To the original poster: are you just curious why, or is there a more specific question you have? I'm assuming that function looks like that to correctly trick the compiler; perhaps escape analysis gives up if you start xor-ing uintptr values or something like that.

tokers

unread,
Oct 17, 2019, 4:04:46 AM10/17/19
to golang-nuts
Yes, I'm just curious about the effect of the "exclusive or".

tokers

unread,
Oct 17, 2019, 4:08:59 AM10/17/19
to golang-nuts
I did some experiments to mimic the noescape function
and modify it with/without the "exclusive-or",but it seems
no influence for the escape analysis, sigh.
Reply all
Reply to author
Forward
0 new messages