-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: inline functions with only an error wrapper #48192
Comments
Yes please. In practice I suspect the easiest way to accomplish immediately this is to do pattern-matching and increase the budget on match. More broadly speaking it would be nice if we could somehow predict when partial inlining will result in escape analysis changes, and use that as a signal to increase the budget. But I don't know how to do that simply and cheaply. cc @mdempsky |
Not just escape analysis changes, but also significant dead code elimination 😃 |
Yep. One question is about making inlining decisions per-function vs per call-site. The latter can get more expensive more quickly, but is more powerful. It occurs to me that potential escape analysis changes might be available by comparing the escape analysis of the trampoline function and the called function, when they have the same signature. Plausible, @mdempsky? I guess we have a phase ordering problem, but escape analysis is cheap and high impact, so maybe worth doing twice? The dead code removal has to be be per-callsite. But if we are doing purely local, brittle AST-based heuristics, like "err != nil", maybe that'd be cheap enough. |
cc also @CAFxX for lock inlining |
I think this and other cases like #48195 boil down to the question if we want to add a new inline trigger that gets a function and makes analysis like:
paired with a more tuned cost model of ast nodes and patterns. Then we could save some extra information like: If this argument is constant at the calling site then inline, if this output is unused then inline. This e.g. should also trigger for using Unless there is some near time direction to support feedback driven optimisation as a first class citizen I would say lets go for it if someone can commit todo the work. We should however invest a bit in making the cost and trigger model easy to configure and verify. e.g. have test similar to asm tests that check some code samples are still inlined as wanted. I would not go so far as having a per ARCH cost model yet but I think the current cost model of ast nodes could be improved to assign lesser costs on patterns of comparisons of Another case that I would hope will get optimised as a pattern is e.g.:
where the function is just an internal forward to another package e.g. arch specific asm function that is selected by build tags. Note that the call might have some extra duplicated or cost arguments. |
In a number of cases, an input argument only escapes through the error value. Examples of this:
net.UDPConn.WriteToUDP
, theaddr
input only escapes when constructing an errorstrconv.ParseXXX
, the input strings
only escapes when constructing an errorIn order to avoid escaping the input, one possibility is to copy the input before placing it in an error. This has the unfortunate downside of always allocating in the error case in situations when the input would have already been treated as being escaped.
Another way to work around this is to take advantage of "function outlining", where the part that causes an input to escape is moved to an inlineable wrapper function so that the compiler can better prove whether to allocate depending on how the function is used by the caller.
Example attempt at function outlining in
strconv
:Unfortunately
ParseFloat
is not inlineable since it exceeds the inline budget. However, if it were within budget, then there are situations where we can avoid theNumError
allocation and treatings
as escaping. For example:In this snippet, the caller doesn't even care about the error value other than the fact that it was non-nil. If
ParseFloat
was inlineable, the compiler can prove thatNumError
does not escape and avoid heap allocating it. In fact, the compiler can (in theory) go a step further and realize that the error wrapping is dead code and eliminate it entirely (all we cared about was whether it was nil).It would be nice if functions of the following pattern could be inlinable:
I don't know if this means increasing the inline budget or matching on such code patterns.
\cc @bradfitz @josharian @martisch @FiloSottile
The text was updated successfully, but these errors were encountered: