]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: simplify inlining of closures
authorMatthew Dempsky <mdempsky@google.com>
Fri, 1 Jan 2021 10:39:00 +0000 (02:39 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 1 Jan 2021 11:30:21 +0000 (11:30 +0000)
Closures have their own ONAMEs for captured variables, which their
function bodies refer to. So during inlining, we need to account for
this and ensure the references still work.

The previous inlining handled this by actually declaring the variables
and then either copying the original value or creating a pointer to
them, as appropriate for variables captured by value or by reference.

But this is needlessly complicated. When inlining the function body,
we need to rewrite all variable references anyway. We can just detect
closure variables and change them to directly point to the enclosing
function's version of this variable. No need for copying or further
indirection.

Does not pass toolstash -cmp. Presumably because we're able to
generate better code in some circumstances.

Change-Id: I8f0ccf7b098f39b8cd33f3bcefb875c8132d2c62
Reviewed-on: https://go-review.googlesource.com/c/go/+/280996
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/inline/inl.go

index df797da2d1c8eb581ce3f29f14668b6dfa4b6bfe..9e9d0bba7ce05a4770015f530fa0438cf3696ae2 100644 (file)
@@ -753,42 +753,6 @@ func mkinlcall(n *ir.CallExpr, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]b
        // record formals/locals for later post-processing
        var inlfvars []ir.Node
 
-       // Handle captured variables when inlining closures.
-       if c := fn.OClosure; c != nil {
-               for _, v := range fn.ClosureVars {
-                       if v.Op() == ir.OXXX {
-                               continue
-                       }
-
-                       o := v.Outer
-                       // make sure the outer param matches the inlining location
-                       // NB: if we enabled inlining of functions containing OCLOSURE or refined
-                       // the reassigned check via some sort of copy propagation this would most
-                       // likely need to be changed to a loop to walk up to the correct Param
-                       if o == nil || o.Curfn != ir.CurFunc {
-                               base.Fatalf("%v: unresolvable capture %v %v\n", ir.Line(n), fn, v)
-                       }
-
-                       if v.Byval() {
-                               iv := typecheck.Expr(inlvar(v))
-                               ninit.Append(ir.NewDecl(base.Pos, ir.ODCL, iv.(*ir.Name)))
-                               ninit.Append(typecheck.Stmt(ir.NewAssignStmt(base.Pos, iv, o)))
-                               inlvars[v] = iv
-                       } else {
-                               addr := typecheck.NewName(typecheck.Lookup("&" + v.Sym().Name))
-                               addr.SetType(types.NewPtr(v.Type()))
-                               ia := typecheck.Expr(inlvar(addr))
-                               ninit.Append(ir.NewDecl(base.Pos, ir.ODCL, ia.(*ir.Name)))
-                               ninit.Append(typecheck.Stmt(ir.NewAssignStmt(base.Pos, ia, typecheck.NodAddr(o))))
-                               inlvars[addr] = ia
-
-                               // When capturing by reference, all occurrence of the captured var
-                               // must be substituted with dereference of the temporary address
-                               inlvars[v] = typecheck.Expr(ir.NewStarExpr(base.Pos, ia))
-                       }
-               }
-       }
-
        for _, ln := range fn.Inl.Dcl {
                if ln.Op() != ir.ONAME {
                        continue
@@ -1088,6 +1052,25 @@ func (subst *inlsubst) node(n ir.Node) ir.Node {
        switch n.Op() {
        case ir.ONAME:
                n := n.(*ir.Name)
+
+               // Handle captured variables when inlining closures.
+               if n.IsClosureVar() {
+                       o := n.Outer
+
+                       // make sure the outer param matches the inlining location
+                       // NB: if we enabled inlining of functions containing OCLOSURE or refined
+                       // the reassigned check via some sort of copy propagation this would most
+                       // likely need to be changed to a loop to walk up to the correct Param
+                       if o == nil || o.Curfn != ir.CurFunc {
+                               base.Fatalf("%v: unresolvable capture %v\n", ir.Line(n), n)
+                       }
+
+                       if base.Flag.LowerM > 2 {
+                               fmt.Printf("substituting captured name %+v  ->  %+v\n", n, o)
+                       }
+                       return o
+               }
+
                if inlvar := subst.inlvars[n]; inlvar != nil { // These will be set during inlnode
                        if base.Flag.LowerM > 2 {
                                fmt.Printf("substituting name %+v  ->  %+v\n", n, inlvar)