WIP: Allow inlining method matches with unmatched type parameters#44656
Closed
WIP: Allow inlining method matches with unmatched type parameters#44656
Conversation
Currently we do not allow inlining any methods that have unmatched
type parameters. The original reason for this restriction is that
I didn't really know what to put for an inlined :static_parameter,
so I just had inlining bail. As a result, in code like:
```
f(x) = Val{x}()
```
the call to `Val{x}()` would not be inlined unless `x` was known
through constant propagation.
This PR attempts to remidy that. A new builtin is added that computes
the static parameters for a given method/argument list. Additionally,
sroa gains the ability to simplify and fold this builtin. As a result,
inlining can insert an expression that computes the correct values
for the inlinees static parameters.
The change benchmarks favorably:
Before:
```
julia> function foo()
for i = 1:10000
Base.donotdelete(Val{i}())
end
end
foo (generic function with 1 method)
julia> @time foo()
0.375567 seconds (4.24 M allocations: 274.440 MiB, 14.67% gc time, 72.96% compilation time)
julia> @time foo()
0.012387 seconds (9.49 k allocations: 148.266 KiB)
```
After:
```
julia> function foo()
for i = 1:10000
Base.donotdelete(Val{i}())
end
end
foo (generic function with 1 method)
julia> @time foo()
0.003058 seconds (29.47 k allocations: 1.546 MiB)
julia> @time foo()
0.001200 seconds (9.49 k allocations: 148.266 KiB)
```
Note that this particular benchmark could also be fixed by #44654,
but this change is more general.
There is a potential downside, which is that we remove a specialization
barrier here. We already do that in the case when all type parameters
are matched, so it's not eggregious. However, there is anectodal
knowledge in the community that extra type parameters force specialization.
Some of that is due to the handling of type parameters in the specialization
code, but some of it might also be due to inlining's prior refusal
to perform this inlining. We'll have to keep an eye out for any
regressions.
Member
|
Ideally that is solved by the cost model. E.g. if you use the static parameter minimally or not all, go ahead and inline, but if not knowing the param makes the code slow then we won't inline it. |
Member
|
replaced by #45062 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently we do not allow inlining any methods that have unmatched
type parameters. The original reason for this restriction is that
I didn't really know what to put for an inlined :static_parameter,
so I just had inlining bail. As a result, in code like:
the call to
Val{x}()would not be inlined unlessxwas knownthrough constant propagation.
This PR attempts to remidy that. A new builtin is added that computes
the static parameters for a given method/argument list. Additionally,
sroa gains the ability to simplify and fold this builtin. As a result,
inlining can insert an expression that computes the correct values
for the inlinees static parameters.
The change benchmarks favorably:
Before:
After:
Note that this particular benchmark could also be fixed by #44654,
but this change is more general.
There is a potential downside, which is that we remove a specialization
barrier here. We already do that in the case when all type parameters
are matched, so it's not eggregious. However, there is anectodal
knowledge in the community that extra type parameters force specialization.
Some of that is due to the handling of type parameters in the specialization
code, but some of it might also be due to inlining's prior refusal
to perform this inlining. We'll have to keep an eye out for any
regressions.