Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion src/Tracing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Member

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?

Copy link
Collaborator Author

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 ConcreteToTraced is now converted to:
Foo{TracedRNumber{Float64}, Bar{TracedRNumber{Float64}}, TracedRArray{Float64, 1}}

The field b of the original type is Bar{Float64}. When tracing this, it just returns Bar{Float64} because of lack of constraints. So what should we compare the fieldtype against?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct T
   d::Array
end

Copy link
Member

@wsmoses wsmoses Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct T{D<:Array}
   d::D
end


struct T{D<:AbstractArray{Float64}}
   d::D
end

Copy link
Member

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

  • must be skipped when that typevar is itself a dependency of another type. If this is true in any case, we need the fancy make_tracer
  • may be run when that typevar is itself not a dependency of another type. If this is true in all cases, we don't need the upgrade

Copy link
Member

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

struct MyNewArray{FT, N, D <: AbstractArray{FT, N}}
    data::D
    off::FT
end

# 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)
Expand Down Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The 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}
Expand Down
Loading