You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm debugging a performance regression in CL 466099. One cause is that the compiler does not inline the call from newInlineUnwinder to resolveInternal, even though resolveInternal is inlinable according to the -m output and the call from next to resolveInternal a few lines down does get inlined. This appears to be because this call to typecheck.HaveInlineBody returns false for resolveInternal when we're visiting newInlineUnwinder and true when we're visiting next. Beyond that I get past my depth. The -m output seems to indicate that the inliner visits newInlineUnwinder, then resolveInternal, then next, which is surprising because that's not in bottom-up order (and this is regardless of source order). If visiting a function is what creates the inline body, then this visit order is clearly a problem.
Relevant -m output:
./symtabinl.go:49:6: cannot inline newInlineUnwinder: function too complex: cost 173 exceeds budget 80
./symtabinl.go:50:21: inlining call to funcdata
./symtabinl.go:60:6: can inline (*inlineUnwinder).resolveInternal with cost 73 as: method(*inlineUnwinder) func(uintptr) { u.pc = pc; u.index = pcdatavalue1(u.f, _PCDATA_InlTreeIndex, pc, u.cache, false) }
./symtab.go:1109:6: cannot inline pcdatavalue1: function too complex: cost 100 exceeds budget 80
./symtab.go:1113:32: inlining call to pcdatastart
./symtab.go:1113:32: inlining call to add
./symtabinl.go:72:6: cannot inline (*inlineUnwinder).next: function too complex: cost 170 exceeds budget 80
./symtabinl.go:78:29: inlining call to funcInfo.entry
./symtabinl.go:78:19: inlining call to (*inlineUnwinder).resolveInternal
It seems to me that in next, funcInfo.entry only has cost 65, so after it's inlined, the max inline budget is still not reached, so the inliner continue to process and inlining call to resolveInternal.
In newInlineUnwinder, funcInfo.funcdata has cost 73, so after it's inlined, the max inline budget is reached, so the inliner stop processing, causing resolveInternal not inlined.
The inliner doesn't track an ongoing cost within a function and stop inlining once some threshold is reached. With one exception, the threshold it applies is solely on the complexity of the target of a given call edge. The one exception is that if we're inlining into a "big" function, we reduce the threshold for all call edges coming from the big function, but none of the functions involved here are close to the "big" threshold. In this case, resolveInternal has cost 73, which is under the default threshold of 80, so it should be inlined into all non-big functions.
(We're looking into changing up all of this, but that's how it currently works.)
I don't see how that's related. All of the function references in my code in static calls, and this is a case where we simply don't inline a call, versus producing bad code from inlining.
and I can see the weird behavior. To get more info on the ordering, I added some code to the inliner's InlineDecls function to trace the order in which functions are processed. Specifically just before this line I inserted this code:
fmt.Fprintf(os.Stderr, "=-= bottom-up visit to: [")
for _, n := range list {
fmt.Fprintf(os.Stderr, " %s", ir.PkgFuncName(n))
}
fmt.Fprintf(os.Stderr, "]\n")
From the resulting trace output, it looks like the odd ordering is due to cycles in the call graph. Specifically it looks like there is a giant cycle that includes runtime.newInlineUnwinder, (*inlineUnwinder).resolveInternal, and a large number of other functions (on the order of 740). Here is the blob:
This would probably explain some of the weirdness, since runtime.(*inlineUnwinder).resolveInternal appears later in this list than runtime.newInlineUnwinder.
We could try to do some work to process nodes within the SCC in a better order, although I am not sure exactly how much work that would be.
Here is a smaller example program that illustrates the problem (easier
to see here than with the giant runtime package).
package p
var p func(int) int
func root(x int) int {
return mid(x + 1)
}
func mid(x int) int {
root(2)
return leaf(x + 1)
}
func leaf(x int) int {
if x < 10 {
return x
}
return 0
}
func apart(q, r int) int {
return q + p(r)
}
When the inliner processes this package, it calls ir.VisitFuncsBottomUp to collect up strongly connected components in the call graph; then handles all functions in a given SCC (either a trivial SCC or a non-trivial SCC where recursion is actually happening) as a clump.
For each function "f" in an SCC clump, the inliner first calls CanInline (to see if "f" itself can be inlined), then calls InlineCalls on the body of "f" (to inline things into "f").
For non-trivial SCCs, this implies that when processing a function earlier in the clump, we may miss inlining opportunities since functions later in the clump haven't been tested for inlinability yet.
In the example above, we'll have a clump { "root", "mid" }, then all other functions will be in their own trivial SCCs. Thus the "-m=2" output for this package looks like:
./p.go:14:6: can inline leaf with cost 8 as: func(int) int { if x < 10 { return x }; return 0 }
./p.go:5:6: can inline root with cost 63 as: func(int) int { return mid(x + 1) }
./p.go:9:6: can inline mid with cost 80 as: func(int) int { root(2); return leaf(x + 1) }
./p.go:10:6: inlining call to root
./p.go:11:13: inlining call to leaf
./p.go:21:6: can inline apart with cost 63 as: func(int, int) int { return q + p(r) }
Note that even though "mid" is inlinable, we don't inline the call to "mid" from "root", because at the point we are doing inlining in "root", "mid" hasn't been checked yet.
I am going to send a CL that changes this behavior for non-trivial SCCs (e.g. does all "CanInline" calls in an SCC before any "InlineCalls" operations).
This lends more support to the idea I've been pushing that inlining shouldn't be bottom-up at all, but should be done starting with the cheapest edge. That would fix SCC ordering issues by eliminating the SCC entirely.
For grins, I wrote a small CL that gathers statistics on how many functions are presented to the inliner as part of a non-trivial SCC. Here's what I got for cmd/compile and k8s kubelet:
Program
Total
nonTrivialSCC
Closure
cmd/compile
17969
1531
942
kubelet
172575
2261
7626
So it seems that SCC functions are relatively uncommon but not unheard of. Note that this is total functions presented to the inliner (which is not the same as total functions in the final executable).
Thanks. My particular issue was fixed by your change, so I'll close this. There's still the open question of how to deal better with large SCCs, but that's part of a bigger project. :)
Activity
cuonglm commentedon Mar 7, 2023
It seems to me that in
next,funcInfo.entryonly has cost 65, so after it's inlined, the max inline budget is still not reached, so the inliner continue to process and inlining call toresolveInternal.In
newInlineUnwinder,funcInfo.funcdatahas cost 73, so after it's inlined, the max inline budget is reached, so the inliner stop processing, causingresolveInternalnot inlined.aclements commentedon Mar 7, 2023
The inliner doesn't track an ongoing cost within a function and stop inlining once some threshold is reached. With one exception, the threshold it applies is solely on the complexity of the target of a given call edge. The one exception is that if we're inlining into a "big" function, we reduce the threshold for all call edges coming from the big function, but none of the functions involved here are close to the "big" threshold. In this case,
resolveInternalhas cost 73, which is under the default threshold of 80, so it should be inlined into all non-big functions.(We're looking into changing up all of this, but that's how it currently works.)
cuonglm commentedon Mar 7, 2023
Sounds like #54632
aclements commentedon Mar 7, 2023
I don't see how that's related. All of the function references in my code in static calls, and this is a case where we simply don't inline a call, versus producing bad code from inlining.
aclements commentedon Mar 7, 2023
Just in case it matters:
go envDetails
Bootstrap is go1.17.13.
thanm commentedon Mar 7, 2023
I pulled the patch, e.g.
and I can see the weird behavior. To get more info on the ordering, I added some code to the inliner's
InlineDeclsfunction to trace the order in which functions are processed. Specifically just before this line I inserted this code:From the resulting trace output, it looks like the odd ordering is due to cycles in the call graph. Specifically it looks like there is a giant cycle that includes
runtime.newInlineUnwinder,(*inlineUnwinder).resolveInternal, and a large number of other functions (on the order of 740). Here is the blob:This would probably explain some of the weirdness, since
runtime.(*inlineUnwinder).resolveInternalappears later in this list thanruntime.newInlineUnwinder.We could try to do some work to process nodes within the SCC in a better order, although I am not sure exactly how much work that would be.
thanm commentedon Mar 9, 2023
Here is a smaller example program that illustrates the problem (easier
to see here than with the giant runtime package).
When the inliner processes this package, it calls ir.VisitFuncsBottomUp to collect up strongly connected components in the call graph; then handles all functions in a given SCC (either a trivial SCC or a non-trivial SCC where recursion is actually happening) as a clump.
For each function "f" in an SCC clump, the inliner first calls CanInline (to see if "f" itself can be inlined), then calls InlineCalls on the body of "f" (to inline things into "f").
For non-trivial SCCs, this implies that when processing a function earlier in the clump, we may miss inlining opportunities since functions later in the clump haven't been tested for inlinability yet.
In the example above, we'll have a clump { "root", "mid" }, then all other functions will be in their own trivial SCCs. Thus the "-m=2" output for this package looks like:
Note that even though "mid" is inlinable, we don't inline the call to "mid" from "root", because at the point we are doing inlining in "root", "mid" hasn't been checked yet.
I am going to send a CL that changes this behavior for non-trivial SCCs (e.g. does all "CanInline" calls in an SCC before any "InlineCalls" operations).
gopherbot commentedon Mar 9, 2023
Change https://go.dev/cl/474955 mentions this issue:
cmd/compile: reorder operations in SCCs to enable more inliningaclements commentedon Mar 9, 2023
Thanks for figuring this out, Than.
This lends more support to the idea I've been pushing that inlining shouldn't be bottom-up at all, but should be done starting with the cheapest edge. That would fix SCC ordering issues by eliminating the SCC entirely.
thanm commentedon Mar 9, 2023
For grins, I wrote a small CL that gathers statistics on how many functions are presented to the inliner as part of a non-trivial SCC. Here's what I got for
cmd/compileand k8skubelet:cmd/compilekubeletSo it seems that SCC functions are relatively uncommon but not unheard of. Note that this is total functions presented to the inliner (which is not the same as total functions in the final executable).
cmd/compile: reorder operations in SCCs to enable more inlining
thanm commentedon Mar 10, 2023
Let me know if this issue can be closed out... thanks
aclements commentedon Mar 15, 2023
Thanks. My particular issue was fixed by your change, so I'll close this. There's still the open question of how to deal better with large SCCs, but that's part of a bigger project. :)
gopherbot commentedon May 3, 2023
Change https://go.dev/cl/492015 mentions this issue:
cmd/compile: remove debugging option InlineSCCOnePass from inlinercmd/compile: remove debugging option InlineSCCOnePass from inliner