-
Notifications
You must be signed in to change notification settings - Fork 33
apply_type_with_promotion #1681
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
Draft
jumerckx
wants to merge
4
commits into
main
Choose a base branch
from
jm/promote_type_params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -736,7 +736,7 @@ Base.@nospecializeinfer function traced_type_inner( | |
| end | ||
|
|
||
| if !isempty(subParms) | ||
| TT2 = Core.apply_type(T.name.wrapper, subParms...) | ||
| TT2 = apply_type_with_promotion(T.name.wrapper, subParms) | ||
| else | ||
| TT2 = T | ||
| end | ||
|
|
@@ -745,6 +745,14 @@ Base.@nospecializeinfer function traced_type_inner( | |
| if fieldcount(T) == fieldcount(TT2) | ||
| legal = true | ||
| for f in 1:fieldcount(T) | ||
| if isa(Base.unwrap_unionall(T.name.wrapper).types[f], TypeVar) | ||
| # The field is constrained by a TypeVar directly, | ||
| # so we don't need to check. | ||
| # (The check below would fail if the typevar was promoted as | ||
| # we don't get the same result when calling traced_type_inner | ||
| # on the field type directly.) | ||
| continue | ||
| end | ||
| subT = fieldtype(T, f) | ||
| subT2 = fieldtype(TT2, f) | ||
| subTT = traced_type_inner(subT, seen3, mode, track_numbers, sharding, runtime) | ||
|
|
@@ -857,6 +865,79 @@ const traced_type_cache = Dict{Tuple{TraceMode,Type,Any},Dict{Type,Type}}() | |
| # $(Expr(:meta, :generated, traced_type_generator)) | ||
| # end | ||
|
|
||
| """ | ||
| This function tries to apply the param types to the wrapper type. | ||
| When there's a constraint conflict, it tries to resolve it by promoting the conflicting types. The new param type is then propagated in any param type that depends on it. | ||
| """ | ||
| function apply_type_with_promotion(wrapper, params, relevant_typevars=typevar_dict(wrapper)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add some tests? |
||
| unwrapped = Base.unwrap_unionall(wrapper) # remove all the typevars | ||
|
|
||
| new_params = copy(params) | ||
|
|
||
| changed = true | ||
| iter = 0 | ||
| while changed && iter < 100 | ||
| changed = false | ||
| for (i, param) in enumerate(params) | ||
| # Add back the typevars to only one of the parameters: | ||
| rewrapped = Base.rewrap_unionall(unwrapped.parameters[i], wrapper) | ||
|
|
||
| sz = @ccall jl_subtype_env_size(rewrapped::Any)::Cint | ||
| arr = Array{Any}(undef, sz) | ||
|
|
||
| # Verify that the currently selected parameter subtypes the param in the wrapper type. | ||
| # In the process, `arr` is filled with with the required types for each parameter used by the current parameter: | ||
| is_subtype = | ||
| (@ccall jl_subtype_env( | ||
| params[i]::Any, rewrapped::Any, arr::Ptr{Any}, sz::Cint | ||
| )::Cint) == 1 | ||
| !is_subtype && error( | ||
| "Failed to find a valid type for typevar $i ($(params[i]) <: $(rewrapped) == false)", | ||
| ) | ||
|
|
||
| # Check whether the required types are supertypes of all the parameter types we currently have: | ||
| current_unionall = rewrapped | ||
| for value in arr | ||
| # Peel open the unionall to figure out which typevar each `value` corresponds to: | ||
| typevar = current_unionall.var | ||
| current_unionall = current_unionall.body | ||
|
|
||
| # `param` might have other typevars that don't occur in `wrapper`, | ||
| # here we first check if the typevar is actually relevant: | ||
| if haskey(relevant_typevars, typevar) | ||
| param_i = relevant_typevars[typevar] | ||
| value <: params[param_i] && continue | ||
|
|
||
| # Found a conflict! Figure out a new param type by promoting: | ||
| promoted = promote_type(value, params[param_i]) | ||
| new_params[param_i] = promoted | ||
|
|
||
| if value != promoted | ||
| # This happens when `value` lost the promotion battle. | ||
| # At this point, we need to update the problematic parameter in`value`. | ||
| d = typevar_dict(rewrapped) | ||
| v = [param.parameters...] | ||
| v[d[typevar]] = promoted | ||
| new_params[i] = params[i] = apply_type_with_promotion(rewrapped, v) | ||
| end | ||
| changed = true | ||
| end | ||
| end | ||
| end | ||
| params .= new_params | ||
| iter += 1 | ||
| end | ||
| return Core.apply_type(wrapper, params...) | ||
| end | ||
|
|
||
| function typevar_dict(t) | ||
| d = Dict() | ||
| for (i, name) in enumerate(Base.unwrap_unionall(t).parameters) | ||
| d[name] = i | ||
| end | ||
| return d | ||
| end | ||
|
|
||
| Base.@assume_effects :total @inline function traced_type( | ||
| T::Type, ::Val{mode}, track_numbers::Type, sharding, runtime | ||
| ) where {mode} | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we still need to check below that the subtype matches, even if constrained? just to confirm we did do the typevar solve correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure...
The reason I added this early return is for e.g.:
Foo{Float64, Bar{Float64}, ConcretePJRTArray{Float64, 1}}Which, with
ConcreteToTracedis now converted to:Foo{TracedRNumber{Float64}, Bar{TracedRNumber{Float64}}, TracedRArray{Float64, 1}}The field
bof the original type isBar{Float64}. When tracing this, it just returnsBar{Float64}because of lack of constraints. So what should we compare the fieldtype against?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check can be skipped when an unconstrained typevar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case where need to skip checj