aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2024-09-09 22:03:54 +0700
committerGopher Robot <gobot@golang.org>2024-09-10 21:30:16 +0000
commitd288776d9143370567fa56b44fa875d0e8fb02b6 (patch)
treef8bc441aed9c604ca81306a1ee045cdba51217fd /src
parentfe69121bc538260cf91f11dab705335b690e51a3 (diff)
downloadgo-d288776d9143370567fa56b44fa875d0e8fb02b6.tar.xz
cmd/compile: remove trivial closure reference
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>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/deadlocals/deadlocals.go2
-rw-r--r--src/cmd/compile/internal/inline/inl.go6
-rw-r--r--src/cmd/compile/internal/ir/func.go10
-rw-r--r--src/cmd/compile/internal/ir/scc.go2
-rw-r--r--src/cmd/compile/internal/staticinit/sched.go10
-rw-r--r--src/cmd/compile/internal/walk/closure.go9
6 files changed, 16 insertions, 23 deletions
diff --git a/src/cmd/compile/internal/deadlocals/deadlocals.go b/src/cmd/compile/internal/deadlocals/deadlocals.go
index 8bc04b7ff0..238450416a 100644
--- a/src/cmd/compile/internal/deadlocals/deadlocals.go
+++ b/src/cmd/compile/internal/deadlocals/deadlocals.go
@@ -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)
}
}
diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go
index c9b9e18eaf..9834737bfb 100644
--- a/src/cmd/compile/internal/inline/inl.go
+++ b/src/cmd/compile/internal/inline/inl.go
@@ -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
}
diff --git a/src/cmd/compile/internal/ir/func.go b/src/cmd/compile/internal/ir/func.go
index 0675150b2d..f9af358ef4 100644
--- a/src/cmd/compile/internal/ir/func.go
+++ b/src/cmd/compile/internal/ir/func.go
@@ -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
diff --git a/src/cmd/compile/internal/ir/scc.go b/src/cmd/compile/internal/ir/scc.go
index 265dce251e..b6056040f7 100644
--- a/src/cmd/compile/internal/ir/scc.go
+++ b/src/cmd/compile/internal/ir/scc.go
@@ -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.
diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go
index 56203120b2..66ef167d35 100644
--- a/src/cmd/compile/internal/staticinit/sched.go
+++ b/src/cmd/compile/internal/staticinit/sched.go
@@ -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
}
diff --git a/src/cmd/compile/internal/walk/closure.go b/src/cmd/compile/internal/walk/closure.go
index 38c6c03dc4..0abf5a0f06 100644
--- a/src/cmd/compile/internal/walk/closure.go
+++ b/src/cmd/compile/internal/walk/closure.go
@@ -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")
}