]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/internal/gc: transitive inlining
authorRuss Cox <rsc@golang.org>
Tue, 24 Feb 2015 17:19:01 +0000 (12:19 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 26 Feb 2015 17:36:00 +0000 (17:36 +0000)
Inlining refuses to inline bodies containing an actual function call, so that
if that call or a child uses runtime.Caller it cannot observe
the inlining.

However, inlining was also refusing to inline bodies that contained
function calls that were themselves inlined away. For example:

func f() int {
return f1()
}

func f1() int {
return f2()
}

func f2() int {
return 2
}

The f2 call in f1 would be inlined, but the f1 call in f would not,
because f1's call to f2 blocked the inlining, despite itself eventually
being inlined away.

Account properly for this kind of transitive inlining and enable.

Also bump the inlining budget a bit, so that the runtime's
heapBits.next is inlined.

This reduces the time for '6g *.go' in html/template by around 12% (!).
(For what it's worth, closing Chrome reduces the time by about 17%.)

Change-Id: If1aa673bf3e583082dcfb5f223e67355c984bfc1
Reviewed-on: https://go-review.googlesource.com/5952
Reviewed-by: Austin Clements <austin@google.com>
src/cmd/internal/gc/go.go
src/cmd/internal/gc/inl.go
src/cmd/internal/gc/lex.go
test/escape4.go
test/inline.go [new file with mode: 0644]

index b87dcf7370c2440ab98ff8f2c3636c22ab0797c1..a1debbd9d6ea5cfab95f34f24319e309c052ef04 100644 (file)
@@ -226,6 +226,7 @@ type Node struct {
        Escretval      *NodeList
        Escloopdepth   int
        Sym            *Sym
+       InlCost        int32
        Vargen         int32
        Lineno         int32
        Endlineno      int32
index 8b088a7f7f45ebe123010465f1738ac2fe094770..57a0ab6502bf669c16a607104ab651aad1ed8972 100644 (file)
@@ -15,7 +15,6 @@
 //      2: early typechecking of all imported bodies
 //      3: allow variadic functions
 //      4: allow non-leaf functions , (breaks runtime.Caller)
-//      5: transitive inlining
 //
 //  At some point this may get another default and become switch-offable with -N.
 //
@@ -125,8 +124,9 @@ func caninl(fn *Node) {
                }
        }
 
-       budget := 40 // allowed hairyness
-       if ishairylist(fn.Nbody, &budget) {
+       const maxBudget = 80
+       budget := maxBudget // allowed hairyness
+       if ishairylist(fn.Nbody, &budget) || budget < 0 {
                return
        }
 
@@ -136,6 +136,7 @@ func caninl(fn *Node) {
        fn.Nname.Inl = fn.Nbody
        fn.Nbody = inlcopylist(fn.Nname.Inl)
        fn.Nname.Inldcl = inlcopylist(fn.Nname.Defn.Dcl)
+       fn.Nname.InlCost = int32(maxBudget - budget)
 
        // hack, TODO, check for better way to link method nodes back to the thing with the ->inl
        // this is so export can find the body of a method
@@ -165,12 +166,42 @@ func ishairy(n *Node, budget *int) bool {
                return false
        }
 
-       // Things that are too hairy, irrespective of the budget
        switch n.Op {
+       // Call is okay if inlinable and we have the budget for the body.
+       case OCALLFUNC:
+               if n.Left.Inl != nil {
+                       *budget -= int(n.Left.InlCost)
+                       break
+               }
+               if n.Left.Op == ONAME && n.Left.Left != nil && n.Left.Left.Op == OTYPE && n.Left.Right != nil && n.Left.Right.Op == ONAME { // methods called as functions
+                       if n.Left.Sym.Def != nil && n.Left.Sym.Def.Inl != nil {
+                               *budget -= int(n.Left.Sym.Def.InlCost)
+                               break
+                       }
+               }
+               if Debug['l'] < 4 {
+                       return true
+               }
+
+       // Call is okay if inlinable and we have the budget for the body.
+       case OCALLMETH:
+               if n.Left.Type == nil {
+                       Fatal("no function type for [%p] %v\n", n.Left, Nconv(n.Left, obj.FmtSign))
+               }
+               if n.Left.Type.Nname == nil {
+                       Fatal("no function definition for [%p] %v\n", n.Left.Type, Tconv(n.Left.Type, obj.FmtSign))
+               }
+               if n.Left.Type.Nname.Inl != nil {
+                       *budget -= int(n.Left.Type.Nname.InlCost)
+                       break
+               }
+               if Debug['l'] < 4 {
+                       return true
+               }
+
+       // Things that are too hairy, irrespective of the budget
        case OCALL,
-               OCALLFUNC,
                OCALLINTER,
-               OCALLMETH,
                OPANIC,
                ORECOVER:
                if Debug['l'] < 4 {
@@ -778,20 +809,20 @@ func mkinlcall1(np **Node, fn *Node, isddd int) {
        inlfn = saveinlfn
 
        // transitive inlining
-       // TODO do this pre-expansion on fn->inl directly.  requires
-       // either supporting exporting statemetns with complex ninits
-       // or saving inl and making inlinl
-       if Debug['l'] >= 5 {
-               body := fn.Inl
-               fn.Inl = nil // prevent infinite recursion
-               inlnodelist(call.Nbody)
-               for ll := call.Nbody; ll != nil; ll = ll.Next {
-                       if ll.N.Op == OINLCALL {
-                               inlconv2stmt(ll.N)
-                       }
-               }
-               fn.Inl = body
-       }
+       // might be nice to do this before exporting the body,
+       // but can't emit the body with inlining expanded.
+       // instead we emit the things that the body needs
+       // and each use must redo the inlining.
+       // luckily these are small.
+       body = fn.Inl
+       fn.Inl = nil // prevent infinite recursion (shouldn't happen anyway)
+       inlnodelist(call.Nbody)
+       for ll := call.Nbody; ll != nil; ll = ll.Next {
+               if ll.N.Op == OINLCALL {
+                       inlconv2stmt(ll.N)
+               }
+       }
+       fn.Inl = body
 
        if Debug['m'] > 2 {
                fmt.Printf("%v: After inlining %v\n\n", n.Line(), Nconv(*np, obj.FmtSign))
index 55c8d76bc246fe8b99970b0be95ed1d77d19e846..2c90e7e06267ed38f0e5af5310266f971e4012b7 100644 (file)
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+//go:generate go tool yacc go.y
+
 package gc
 
 import (
@@ -413,18 +415,14 @@ func Main() {
 
        if Debug['l'] != 0 {
                // Find functions that can be inlined and clone them before walk expands them.
-               for l := xtop; l != nil; l = l.Next {
-                       if l.N.Op == ODCLFUNC {
-                               caninl(l.N)
-                       }
-               }
-
-               // Expand inlineable calls in all functions
-               for l := xtop; l != nil; l = l.Next {
-                       if l.N.Op == ODCLFUNC {
-                               inlcalls(l.N)
+               visitBottomUp(xtop, func(list *NodeList, recursive bool) {
+                       for l := list; l != nil; l = l.Next {
+                               if l.N.Op == ODCLFUNC {
+                                       caninl(l.N)
+                                       inlcalls(l.N)
+                               }
                        }
-               }
+               })
        }
 
        // Phase 6: Escape analysis.
index 83bc8eb123dd976c93d2dfb3fefc131fb56efde1..248f8a96b977696c380482a934baf49d9902345e 100644 (file)
@@ -22,11 +22,11 @@ func f1() {
 
        // Escape analysis used to miss inlined code in closures.
 
-       func() { // ERROR "func literal does not escape"
+       func() { // ERROR "func literal does not escape" "can inline f1.func1"
                p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
        }()
 
-       f = func() { // ERROR "func literal escapes to heap"
+       f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2"
                p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
        }
        f()
@@ -42,7 +42,7 @@ func f5() *byte {
        type T struct {
                x [1]byte
        }
-       t := new(T) // ERROR "new.T. escapes to heap"
+       t := new(T)    // ERROR "new.T. escapes to heap"
        return &t.x[0] // ERROR "&t.x.0. escapes to heap"
 }
 
@@ -52,6 +52,6 @@ func f6() *byte {
                        y byte
                }
        }
-       t := new(T) // ERROR "new.T. escapes to heap"
+       t := new(T)   // ERROR "new.T. escapes to heap"
        return &t.x.y // ERROR "&t.x.y escapes to heap"
 }
diff --git a/test/inline.go b/test/inline.go
new file mode 100644 (file)
index 0000000..54f7b3e
--- /dev/null
@@ -0,0 +1,24 @@
+// errorcheck -0 -m
+
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test, using compiler diagnostic flags, that inlining is working.
+// Compiles but does not run.
+
+package foo
+
+import "unsafe"
+
+func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result"
+       return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1"
+}
+
+func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result"
+       return unsafe.Pointer(uintptr(p) + x)
+}
+
+func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result"
+       return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1"
+}