b.initFunc(fn)
}
for _, fn := range fns {
- if !fn.IsHiddenClosure() {
+ if !fn.IsClosure() {
b.walkFunc(fn)
}
}
k = e.spill(k, n)
e.closures = append(e.closures, closure{k, n})
- if fn := n.Func; fn.IsHiddenClosure() {
+ if fn := n.Func; fn.IsClosure() {
for _, cv := range fn.ClosureVars {
if loc := e.oldLoc(cv); !loc.captured {
loc.captured = true
return
}
- // Don't try compiling dead hidden closure.
- if fn.IsDeadcodeClosure() {
- return
- }
-
- if clo := fn.OClosure; clo != nil && !ir.IsTrivialClosure(clo) {
+ if fn.IsClosure() {
return // we'll get this as part of its enclosing function
}
})
}
-// GarbageCollectUnreferencedHiddenClosures makes a pass over all the
-// top-level (non-hidden-closure) functions looking for nested closure
-// functions that are reachable, then sweeps through the Target.Decls
-// list and marks any non-reachable hidden closure function as dead.
-// See issues #59404 and #59638 for more context.
-func GarbageCollectUnreferencedHiddenClosures() {
-
- liveFuncs := make(map[*ir.Func]bool)
-
- var markLiveFuncs func(fn *ir.Func)
- markLiveFuncs = func(fn *ir.Func) {
- if liveFuncs[fn] {
- return
- }
- liveFuncs[fn] = true
- ir.Visit(fn, func(n ir.Node) {
- if clo, ok := n.(*ir.ClosureExpr); ok {
- markLiveFuncs(clo.Func)
- }
- })
- }
-
- for i := 0; i < len(typecheck.Target.Funcs); i++ {
- fn := typecheck.Target.Funcs[i]
- if fn.IsHiddenClosure() {
- continue
- }
- markLiveFuncs(fn)
- }
-
- for i := 0; i < len(typecheck.Target.Funcs); i++ {
- fn := typecheck.Target.Funcs[i]
- if !fn.IsHiddenClosure() {
- continue
- }
- if fn.IsDeadcodeClosure() {
- continue
- }
- if liveFuncs[fn] {
- continue
- }
- fn.SetIsDeadcodeClosure(true)
- if base.Flag.LowerM > 2 {
- fmt.Printf("%v: unreferenced closure %v marked as dead\n", ir.Line(fn), fn)
- }
- if fn.Inl != nil && fn.LSym == nil {
- ir.InitLSym(fn, true)
- }
- }
-}
-
// inlineBudget determines the max budget for function 'fn' prior to
// analyzing the hairiness of the body of 'fn'. We pass in the pgo
// profile if available (which can change the budget), also a
}
if base.Flag.LowerL != 0 {
- // Perform a garbage collection of hidden closures functions that
- // are no longer reachable from top-level functions following
- // inlining. See #59404 and #59638 for more context.
- inline.GarbageCollectUnreferencedHiddenClosures()
-
if base.Debug.DumpInlFuncProps != "" {
inlheur.DumpFuncProps(nil, base.Debug.DumpInlFuncProps)
}
type ScopeID int32
const (
- funcDupok = 1 << iota // duplicate definitions ok
- funcWrapper // hide frame from users (elide in tracebacks, don't count as a frame for recover())
- funcABIWrapper // is an ABI wrapper (also set flagWrapper)
- funcNeedctxt // function uses context register (has closure variables)
- // true if closure inside a function; false if a simple function or a
- // closure in a global variable initialization
- funcIsHiddenClosure
- funcIsDeadcodeClosure // true if closure is deadcode
- funcHasDefer // contains a defer statement
- funcNilCheckDisabled // disable nil checks when compiling this function
- funcInlinabilityChecked // inliner has already determined whether the function is inlinable
- funcNeverReturns // function never returns (in most cases calls panic(), os.Exit(), or equivalent)
- funcOpenCodedDeferDisallowed // can't do open-coded defers
- funcClosureResultsLost // closure is called indirectly and we lost track of its results; used by escape analysis
- funcPackageInit // compiler emitted .init func for package
+ funcDupok = 1 << iota // duplicate definitions ok
+ funcWrapper // hide frame from users (elide in tracebacks, don't count as a frame for recover())
+ funcABIWrapper // is an ABI wrapper (also set flagWrapper)
+ funcNeedctxt // function uses context register (has closure variables)
+ funcHasDefer // contains a defer statement
+ funcNilCheckDisabled // disable nil checks when compiling this function
+ funcInlinabilityChecked // inliner has already determined whether the function is inlinable
+ funcNeverReturns // function never returns (in most cases calls panic(), os.Exit(), or equivalent)
+ funcOpenCodedDeferDisallowed // can't do open-coded defers
+ funcClosureResultsLost // closure is called indirectly and we lost track of its results; used by escape analysis
+ funcPackageInit // compiler emitted .init func for package
)
type SymAndPos struct {
func (f *Func) Wrapper() bool { return f.flags&funcWrapper != 0 }
func (f *Func) ABIWrapper() bool { return f.flags&funcABIWrapper != 0 }
func (f *Func) Needctxt() bool { return f.flags&funcNeedctxt != 0 }
-func (f *Func) IsHiddenClosure() bool { return f.flags&funcIsHiddenClosure != 0 }
-func (f *Func) IsDeadcodeClosure() bool { return f.flags&funcIsDeadcodeClosure != 0 }
func (f *Func) HasDefer() bool { return f.flags&funcHasDefer != 0 }
func (f *Func) NilCheckDisabled() bool { return f.flags&funcNilCheckDisabled != 0 }
func (f *Func) InlinabilityChecked() bool { return f.flags&funcInlinabilityChecked != 0 }
func (f *Func) SetWrapper(b bool) { f.flags.set(funcWrapper, b) }
func (f *Func) SetABIWrapper(b bool) { f.flags.set(funcABIWrapper, b) }
func (f *Func) SetNeedctxt(b bool) { f.flags.set(funcNeedctxt, b) }
-func (f *Func) SetIsHiddenClosure(b bool) { f.flags.set(funcIsHiddenClosure, b) }
-func (f *Func) SetIsDeadcodeClosure(b bool) { f.flags.set(funcIsDeadcodeClosure, b) }
func (f *Func) SetHasDefer(b bool) { f.flags.set(funcHasDefer, b) }
func (f *Func) SetNilCheckDisabled(b bool) { f.flags.set(funcNilCheckDisabled, b) }
func (f *Func) SetInlinabilityChecked(b bool) { f.flags.set(funcInlinabilityChecked, b) }
}
}
+func (f *Func) IsClosure() bool {
+ if f.OClosure == nil {
+ return false
+ }
+ // Trivial closure will be converted to global.
+ return !IsTrivialClosure(f.OClosure)
+}
+
// FuncName returns the name (without the package) of the function f.
func FuncName(f *Func) string {
if f == nil || f.Nname == nil {
// should have an inline-adjusted position, whereas the ODCLFUNC and
// ONAME must not.
//
-// outerfn is the enclosing function, if any. The returned function is
+// outerfn is the enclosing function. The returned function is
// appending to pkg.Funcs.
//
// why is the reason we're generating this Func. It can be OCLOSURE
// (for a normal function literal) or OGO or ODEFER (for wrapping a
// call expression that has parameters or results).
func NewClosureFunc(fpos, cpos src.XPos, why Op, typ *types.Type, outerfn *Func, pkg *Package) *Func {
- fn := NewFunc(fpos, fpos, closureName(outerfn, cpos, why), typ)
- fn.SetIsHiddenClosure(outerfn != nil)
- if outerfn != nil {
- fn.SetDupok(outerfn.Dupok()) // if the outer function is dupok, so is the closure
+ if outerfn == nil {
+ base.FatalfAt(fpos, "outerfn is nil")
}
+ fn := NewFunc(fpos, fpos, closureName(outerfn, cpos, why), typ)
+ fn.SetDupok(outerfn.Dupok()) // if the outer function is dupok, so is the closure
+
clo := &ClosureExpr{Func: fn}
clo.op = OCLOSURE
clo.pos = cpos
// The algorithm (known as Tarjan's algorithm) for doing that is taken from
// Sedgewick, Algorithms, Second Edition, p. 482, with two adaptations.
//
-// First, a hidden closure function (n.Func.IsHiddenClosure()) 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.
+// First, a non-trivial closure function (fn.OClosure != nil) 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.
//
// Second, each function becomes two virtual nodes in the graph,
// with numbers n and n+1. We record the function's node number as n
v.analyze = analyze
v.nodeID = make(map[*Func]uint32)
for _, n := range list {
- if !n.IsHiddenClosure() {
+ if !n.IsClosure() {
v.visit(n)
}
}
}
})
- if (min == id || min == id+1) && !n.IsHiddenClosure() {
+ if (min == id || min == id+1) && !n.IsClosure() {
// This node is the root of a strongly connected component.
// The original min was id+1. If the bottomUpVisitor found its way
r.addBody(fn, nil)
- // un-hide closures belong to init function.
- if (r.curfn.IsPackageInit() || strings.HasPrefix(r.curfn.Sym().Name, "init.")) && ir.IsTrivialClosure(fn.OClosure) {
- fn.SetIsHiddenClosure(false)
- }
-
return fn.OClosure
}
if base.Debug.Closure > 0 {
base.WarnfAt(r.Pos(), "closure converted to global")
}
- // Issue 59680: if the closure we're looking at was produced
- // by inlining, it could be marked as hidden, which we don't
- // want (moving the func to a static init will effectively
- // hide it from escape analysis). Mark as non-hidden here.
- // so that it will participated in escape analysis.
- r.Func.SetIsHiddenClosure(false)
// Closures with no captured variables are globals,
// so the assignment can be done at link time.
// TODO if roff != 0 { panic }
return x + 2
}
y, sink = func() (func(int) int, int) { // ERROR "can inline main.func12"
- return func(x int) int { // ERROR "can inline main.func12"
+ return func(x int) int { // ERROR "can inline main.func12" "func literal escapes to heap"
return x + 1
}, 42
}() // ERROR "func literal does not escape" "inlining call to main.func12"
return x + 2
}
y, sink = func() (func(int) int, int) { // ERROR "can inline main.func13.2"
- return func(x int) int { // ERROR "can inline main.func13.2"
+ return func(x int) int { // ERROR "can inline main.func13.2" "func literal escapes to heap"
return x + 1
}, 42
}() // ERROR "func literal does not escape" "inlining call to main.func13.2"
// Escape analysis used to miss inlined code in closures.
func() { // ERROR "can inline f1.func1"
- p = alloc(3) // ERROR "inlining call to alloc"
+ p = alloc(3) // ERROR "inlining call to alloc" "moved to heap: x"
}() // ERROR "inlining call to f1.func1" "inlining call to alloc" "moved to heap: x"
f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2"
func ClosureCallArgs15() {
x := 0 // ERROR "moved to heap: x"
p := &x
- sink = func(p **int) *int { // ERROR "leaking param content: p" "func literal does not escape"
+ sink = func(p **int) *int { // ERROR "leaking param: p to result ~r0 level=1" "func literal does not escape"
return *p
}(&p)
}