In the future, how to keep runtime.KeepAlive from dead code elimination?

276 views
Skip to first unread message

Cholerae Hu

unread,
Aug 24, 2016, 11:42:10 PM8/24/16
to golang-nuts
Hi all,

I've read the source of package runtime, and found that runtime.KeepAlive is an empty function. If go compiler can do dead code elimination in the future, how to protect runtime.KeepAlive from being optimized?

Ian Lance Taylor

unread,
Aug 24, 2016, 11:55:12 PM8/24/16
to Cholerae Hu, golang-nuts
The KeepAlive function is marked with the magic go:noinline comment,
so it can't be inlined.

In any case it seems possible that the compiler will recognize the
function specially in the future, for greater efficiency.

Ian

Cholerae Hu

unread,
Aug 25, 2016, 3:15:19 AM8/25/16
to golang-nuts, chole...@gmail.com
Does that mean that only inlined functions will be optimized and any functions not inlined will not be optimized ? 

在 2016年8月25日星期四 UTC+8上午11:55:12,Ian Lance Taylor写道:

Dave Cheney

unread,
Aug 25, 2016, 3:25:18 AM8/25/16
to golang-nuts
Not really. Runtime.KeepAlive is special, it'll continue to be special in the future.

Ian Lance Taylor

unread,
Aug 25, 2016, 12:04:57 PM8/25/16
to Cholerae Hu, golang-nuts
On Thu, Aug 25, 2016 at 12:15 AM, Cholerae Hu <chole...@gmail.com> wrote:
> Does that mean that only inlined functions will be optimized and any
> functions not inlined will not be optimized ?

I'm not really sure what you are asking.

A function that is not inlined will not be inlined. That is
sufficient to ensure that runtime.KeepAlive works as intended.

A function that is not inlined, for whatever reason, will still be
optimized as usual.

Ian

> 在 2016年8月25日星期四 UTC+8上午11:55:12,Ian Lance Taylor写道:
>>
>> On Wed, Aug 24, 2016 at 7:06 PM, Cholerae Hu <chole...@gmail.com> wrote:
>> >
>> > I've read the source of package runtime, and found that
>> > runtime.KeepAlive is
>> > an empty function. If go compiler can do dead code elimination in the
>> > future, how to protect runtime.KeepAlive from being optimized?
>>
>> The KeepAlive function is marked with the magic go:noinline comment,
>> so it can't be inlined.
>>
>> In any case it seems possible that the compiler will recognize the
>> function specially in the future, for greater efficiency.
>>
>> Ian
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Cholerae Hu

unread,
Aug 26, 2016, 5:56:49 AM8/26/16
to golang-nuts, chole...@gmail.com
I'm curious that how does compiler recognize runtime.KeepAlive specially?

在 2016年8月26日星期五 UTC+8上午12:04:57,Ian Lance Taylor写道:

Dave Cheney

unread,
Aug 26, 2016, 6:56:37 AM8/26/16
to golang-nuts, chole...@gmail.com
runtime/mfinal.go:464

Ian Lance Taylor

unread,
Aug 26, 2016, 10:21:34 AM8/26/16
to Cholerae Hu, golang-nuts
On Fri, Aug 26, 2016 at 2:56 AM, Cholerae Hu <chole...@gmail.com> wrote:
> I'm curious that how does compiler recognize runtime.KeepAlive specially?

The compiler already recognizes some functions specially, most notably
math.Sqrt. Search for "Sqrt" in cmd/compile/internal/gc/walk.go.

Ian

Cholerae Hu

unread,
Aug 27, 2016, 1:59:19 AM8/27/16
to golang-nuts
I see, thanks.

在 2016年8月25日星期四 UTC+8上午11:42:10,Cholerae Hu写道:
Reply all
Reply to author
Forward
0 new messages