labeled loop vs for statement inlining

226 views
Skip to first unread message

Ariel Mashraki

unread,
Aug 29, 2016, 10:48:22 AM8/29/16
to golang-nuts

Hello,

Running `go build -gcflags -m` on the given code below will produce:


   main.go:3: can inline f1

   main.go:24: inlining call to f1


Can someone please explain why doesn't the f2 function get inlined ?

Thanks



package main

func f1
() int {

       i := 0

loop:

       if i > 10 {

               return i

       }

       i++

       goto loop

}


func f2() int {

       i := 0

       for {

               if i > 10 {

                       return i

               }

               i++

       }

}


func main() {

       f1()

       f2()

}


Chris Manghane

unread,
Aug 29, 2016, 11:00:04 AM8/29/16
to Ariel Mashraki, golang-nuts
I can't explain exactly because my explanation is likely very flawed, but the logic you are looking for is in https://github.com/golang/go/blob/320ddcf8344beb1c322f3a7f0a251eea5e442a10/src/cmd/compile/internal/gc/inl.go#L186. Basically, labeled loops are not considered "hairy" by the compiler and can be inlined.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Mogensen

unread,
Aug 29, 2016, 11:04:49 AM8/29/16
to golan...@googlegroups.com


On 2016-08-29 16:59, 'Chris Manghane' via golang-nuts wrote:
> I can't explain exactly because my explanation is likely very flawed,
> but the logic you are looking for is
> in https://github.com/golang/go/blob/320ddcf8344beb1c322f3a7f0a251eea5e442a10/src/cmd/compile/internal/gc/inl.go#L186.
> Basically, labeled loops are not considered "hairy" by the compiler and
> can be inlined.

On the other hand, f1() uses goto to jump back in previous execution
path. *shudder*

... hopefully not something to be encouraged.

/Peter

Sam Whited

unread,
Aug 29, 2016, 11:09:19 AM8/29/16
to Ariel Mashraki, golang-nuts
On Mon, Aug 29, 2016 at 9:40 AM, Ariel Mashraki
<ariel.m...@ironsrc.com> wrote:
> Can someone please explain why doesn't the f2 function get inlined ?

If you build with m=2 (go build -gcflags -m=2) it will spit out a reason:

> cannot inline f2: unhandled op for

(closures with "for" in them apparently can't be inlined)

—Sam



--
Sam Whited
pub 4096R/54083AE104EA7AD3

Ariel Mashraki

unread,
Aug 29, 2016, 11:57:25 AM8/29/16
to golang-nuts, ariel.m...@ironsrc.com
I didn't know it has more levels. thanks 

Sokolov Yura

unread,
Aug 30, 2016, 1:58:30 AM8/30/16
to golang-nuts
But still question is interesting: why decision is based on source code and not on instructions size? Both functions likely to produce same assembler code.

Dave Cheney

unread,
Aug 30, 2016, 2:00:26 AM8/30/16
to golang-nuts
Inlining is conservative. As well as the size of the code being inlined, current 40 units (unit has not scale), some operations are not inlined, as they are considered hairy. switch used to be considered hairy, 1.7 fixed that, for is still hairy.
Reply all
Reply to author
Forward
0 new messages