]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: remove trivial closure reference
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Mon, 9 Sep 2024 15:03:54 +0000 (22:03 +0700)
committerGopher Robot <gobot@golang.org>
Tue, 10 Sep 2024 21:30:16 +0000 (21:30 +0000)
Trivial closures will be converted to global functions, thus they are
not closures anymore. Using fn.IsClosure function is enough, allow
removing the trivial/non-trivial closures in the code.

Change-Id: Iceb186dd92c1732b101e221ebc13406db35c69ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/611995
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Tim King <taking@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/deadlocals/deadlocals.go
src/cmd/compile/internal/inline/inl.go
src/cmd/compile/internal/ir/func.go
src/cmd/compile/internal/ir/scc.go
src/cmd/compile/internal/staticinit/sched.go
src/cmd/compile/internal/walk/closure.go

index 8bc04b7ff0fe0185623d1f2fcc563b3a65594790..238450416a94638aae120ac1fb9e7537fcbda2ea 100644 (file)
@@ -36,7 +36,7 @@ func Funcs(fns []*ir.Func) {
                                // Kludge for "missing func info" linker panic.
                                // See also closureInitLSym in inline/inl.go.
                                if clo, ok := (*as.rhs).(*ir.ClosureExpr); ok && clo.Op() == ir.OCLOSURE {
-                                       if !ir.IsTrivialClosure(clo) {
+                                       if clo.Func.IsClosure() {
                                                ir.InitLSym(clo.Func, true)
                                        }
                                }
index c9b9e18eaf01489db3d043f7f497798d6b92f95b..9834737bfb12d7ee16fbe430fda23939835aebaf 100644 (file)
@@ -1020,7 +1020,7 @@ func mkinlcall(callerfn *ir.Func, n *ir.CallExpr, fn *ir.Func, bigCaller bool) *
                // typecheck.Target.Decls (ir.UseClosure adds all closures to
                // Decls).
                //
-               // However, non-trivial closures in Decls are ignored, and are
+               // However, closures in Decls are ignored, and are
                // instead enqueued when walk of the calling function
                // discovers them.
                //
@@ -1045,8 +1045,8 @@ func mkinlcall(callerfn *ir.Func, n *ir.CallExpr, fn *ir.Func, bigCaller bool) *
                }
 
                clo := n.Fun.(*ir.ClosureExpr)
-               if ir.IsTrivialClosure(clo) {
-                       // enqueueFunc will handle trivial closures anyways.
+               if !clo.Func.IsClosure() {
+                       // enqueueFunc will handle non closures anyways.
                        return
                }
 
index 0675150b2d8f39700eec4a24dccfcb6ddf5cab1e..f9af358ef4aec01b18b81c7956148c5aa6d43d3c 100644 (file)
@@ -282,12 +282,12 @@ func (f *Func) SetWBPos(pos src.XPos) {
        }
 }
 
+// IsClosure reports whether f is a function literal that captures at least one value.
 func (f *Func) IsClosure() bool {
        if f.OClosure == nil {
                return false
        }
-       // Trivial closure will be converted to global.
-       return !IsTrivialClosure(f.OClosure)
+       return len(f.ClosureVars) > 0
 }
 
 // FuncName returns the name (without the package) of the function f.
@@ -419,12 +419,6 @@ func ClosureDebugRuntimeCheck(clo *ClosureExpr) {
        }
 }
 
-// IsTrivialClosure reports whether closure clo has an
-// empty list of captured vars.
-func IsTrivialClosure(clo *ClosureExpr) bool {
-       return len(clo.Func.ClosureVars) == 0
-}
-
 // globClosgen is like Func.Closgen, but for the global scope.
 var globClosgen int32
 
index 265dce251eaf8c522a52a28ed21d5ca5f6f2ec24..b6056040f7ba901d813e374fd10c30dace674887 100644 (file)
@@ -14,7 +14,7 @@ package ir
 // The algorithm (known as Tarjan's algorithm) for doing that is taken from
 // Sedgewick, Algorithms, Second Edition, p. 482, with two adaptations.
 //
-// First, a non-trivial closure function (fn.OClosure != nil) cannot be
+// First, a closure function (fn.IsClosure()) cannot be
 // the root of a connected component. Refusing to use it as a root forces
 // it into the component of the function in which it appears.  This is
 // more convenient for escape analysis.
index 56203120b2c41faff6f5d491f420cf1685b1fe00..66ef167d35894ddc482e94d29c9b02c3145c2901 100644 (file)
@@ -389,7 +389,7 @@ func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Ty
 
        case ir.OCLOSURE:
                r := r.(*ir.ClosureExpr)
-               if ir.IsTrivialClosure(r) {
+               if !r.Func.IsClosure() {
                        if base.Debug.Closure > 0 {
                                base.WarnfAt(r.Pos(), "closure converted to global")
                        }
@@ -668,7 +668,7 @@ func (s *Schedule) staticAssignInlinedCall(l *ir.Name, loff int64, call *ir.Inli
                count[x.(*ir.Name)] = 0
        }
 
-       hasNonTrivialClosure := false
+       hasClosure := false
        ir.Visit(as2body.Rhs[0], func(n ir.Node) {
                if name, ok := n.(*ir.Name); ok {
                        if c, ok := count[name]; ok {
@@ -676,13 +676,13 @@ func (s *Schedule) staticAssignInlinedCall(l *ir.Name, loff int64, call *ir.Inli
                        }
                }
                if clo, ok := n.(*ir.ClosureExpr); ok {
-                       hasNonTrivialClosure = hasNonTrivialClosure || !ir.IsTrivialClosure(clo)
+                       hasClosure = hasClosure || clo.Func.IsClosure()
                }
        })
 
-       // If there's a non-trivial closure, it has captured the param,
+       // If there's a closure, it has captured the param,
        // so we can't substitute arg for param.
-       if hasNonTrivialClosure {
+       if hasClosure {
                return false
        }
 
index 38c6c03dc496faba9741b1ad3e7b6279d94cb04b..0abf5a0f06057b08a236eac95ea065f71729d718 100644 (file)
@@ -33,7 +33,7 @@ func directClosureCall(n *ir.CallExpr) {
        clo := n.Fun.(*ir.ClosureExpr)
        clofn := clo.Func
 
-       if ir.IsTrivialClosure(clo) {
+       if !clofn.IsClosure() {
                return // leave for walkClosure to handle
        }
 
@@ -87,16 +87,15 @@ func directClosureCall(n *ir.CallExpr) {
 
        // Add to Closures for enqueueFunc. It's no longer a proper
        // closure, but we may have already skipped over it in the
-       // functions list as a non-trivial closure, so this just
-       // ensures it's compiled.
+       // functions list, so this just ensures it's compiled.
        ir.CurFunc.Closures = append(ir.CurFunc.Closures, clofn)
 }
 
 func walkClosure(clo *ir.ClosureExpr, init *ir.Nodes) ir.Node {
        clofn := clo.Func
 
-       // If no closure vars, don't bother wrapping.
-       if ir.IsTrivialClosure(clo) {
+       // If not a closure, don't bother wrapping.
+       if !clofn.IsClosure() {
                if base.Debug.Closure > 0 {
                        base.WarnfAt(clo.Pos(), "closure converted to global")
                }