Skip to content
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

[pull] master from JuliaLang:master #1796

Merged
merged 3 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,9 @@ function abstract_call_method_with_const_args(interp::AbstractInterpreter,
if code !== nothing
ir = codeinst_to_ir(interp, code)
if isa(ir, IRCode)
irsv = IRInterpretationState(interp, ir, mi, sv.world, arginfo.argtypes)
rt, nothrow = ir_abstract_constant_propagation(interp, irsv)
irinterp = switch_to_irinterp(interp)
irsv = IRInterpretationState(irinterp, ir, mi, sv.world, arginfo.argtypes)
rt, nothrow = ir_abstract_constant_propagation(irinterp, irsv)
@assert !(rt isa Conditional || rt isa MustAlias) "invalid lattice element returned from IR interpretation"
if !isa(rt, Type) || typeintersect(rt, Bool) === Union{}
new_effects = Effects(result.effects; nothrow=nothrow)
Expand Down
77 changes: 52 additions & 25 deletions base/compiler/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,51 +312,64 @@ function OptimizationParams(
end

"""
NativeInterpreter
NativeInterpreter <: AbstractInterpreter

This represents Julia's native type inference algorithm and the Julia-LLVM codegen backend.
It contains many parameters used by the compilation pipeline.
"""
struct NativeInterpreter <: AbstractInterpreter
# Cache of inference results for this particular interpreter
cache::Vector{InferenceResult}
# The world age we're working inside of
world::UInt

# method table to lookup for during inference on this world age
method_table::CachedMethodTable{InternalMethodTable}

# Cache of inference results for this particular interpreter
inf_cache::Vector{InferenceResult}

# Parameters for inference and optimization
inf_params::InferenceParams
opt_params::OptimizationParams

function NativeInterpreter(world::UInt = get_world_counter();
inf_params = InferenceParams(),
opt_params = OptimizationParams(),
)
cache = Vector{InferenceResult}() # Initially empty cache
# a boolean flag to indicate if this interpreter is performing semi concrete interpretation
irinterp::Bool
end

function NativeInterpreter(world::UInt = get_world_counter();
inf_params::InferenceParams = InferenceParams(),
opt_params::OptimizationParams = OptimizationParams())
# Sometimes the caller is lazy and passes typemax(UInt).
# we cap it to the current world age
if world == typemax(UInt)
world = get_world_counter()
end

# Sometimes the caller is lazy and passes typemax(UInt).
# we cap it to the current world age
if world == typemax(UInt)
world = get_world_counter()
end
# If they didn't pass typemax(UInt) but passed something more subtly
# incorrect, fail out loudly.
@assert world <= get_world_counter()

method_table = CachedMethodTable(InternalMethodTable(world))
method_table = CachedMethodTable(InternalMethodTable(world))

# If they didn't pass typemax(UInt) but passed something more subtly
# incorrect, fail out loudly.
@assert world <= get_world_counter()
inf_cache = Vector{InferenceResult}() # Initially empty cache

return new(cache, world, method_table, inf_params, opt_params)
end
return NativeInterpreter(world, method_table, inf_cache, inf_params, opt_params, #=irinterp=#false)
end

function NativeInterpreter(interp::NativeInterpreter;
world::UInt = interp.world,
method_table::CachedMethodTable{InternalMethodTable} = interp.method_table,
inf_cache::Vector{InferenceResult} = interp.inf_cache,
inf_params::InferenceParams = interp.inf_params,
opt_params::OptimizationParams = interp.opt_params,
irinterp::Bool = interp.irinterp)
return NativeInterpreter(world, method_table, inf_cache, inf_params, opt_params, irinterp)
end

# Quickly and easily satisfy the AbstractInterpreter API contract
InferenceParams(ni::NativeInterpreter) = ni.inf_params
OptimizationParams(ni::NativeInterpreter) = ni.opt_params
get_world_counter(ni::NativeInterpreter) = ni.world
get_inference_cache(ni::NativeInterpreter) = ni.cache
code_cache(ni::NativeInterpreter) = WorldView(GLOBAL_CI_CACHE, get_world_counter(ni))
InferenceParams(interp::NativeInterpreter) = interp.inf_params
OptimizationParams(interp::NativeInterpreter) = interp.opt_params
get_world_counter(interp::NativeInterpreter) = interp.world
get_inference_cache(interp::NativeInterpreter) = interp.inf_cache
code_cache(interp::NativeInterpreter) = WorldView(GLOBAL_CI_CACHE, get_world_counter(interp))

"""
already_inferred_quick_test(::AbstractInterpreter, ::MethodInstance)
Expand Down Expand Up @@ -442,6 +455,20 @@ typeinf_lattice(::AbstractInterpreter) = InferenceLattice(BaseInferenceLattice.i
ipo_lattice(::AbstractInterpreter) = InferenceLattice(IPOResultLattice.instance)
optimizer_lattice(::AbstractInterpreter) = OptimizerLattice(SimpleInferenceLattice.instance)

typeinf_lattice(interp::NativeInterpreter) = interp.irinterp ? optimizer_lattice(interp) : InferenceLattice(BaseInferenceLattice.instance)
ipo_lattice(interp::NativeInterpreter) = interp.irinterp ? optimizer_lattice(interp) : InferenceLattice(IPOResultLattice.instance)
optimizer_lattice(interp::NativeInterpreter) = OptimizerLattice(SimpleInferenceLattice.instance)

"""
switch_to_irinterp(interp::AbstractInterpreter) -> irinterp::AbstractInterpreter

Optionally convert `interp` to new `irinterp::AbstractInterpreter` to perform semi-concrete
interpretation. `NativeInterpreter` uses this interface to switch its lattice to
`optimizer_lattice` during semi-concrete interpretation on `IRCode`.
"""
switch_to_irinterp(interp::AbstractInterpreter) = interp
switch_to_irinterp(interp::NativeInterpreter) = NativeInterpreter(interp; irinterp=true)

abstract type CallInfo end

@nospecialize
Expand Down
7 changes: 6 additions & 1 deletion base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ function promote_typejoin(@nospecialize(a), @nospecialize(b))
c = typejoin(_promote_typesubtract(a), _promote_typesubtract(b))
return Union{a, b, c}::Type
end
_promote_typesubtract(@nospecialize(a)) = typesplit(a, Union{Nothing, Missing})
_promote_typesubtract(@nospecialize(a)) =
a === Any ? a :
a >: Union{Nothing, Missing} ? typesplit(a, Union{Nothing, Missing}) :
a >: Nothing ? typesplit(a, Nothing) :
a >: Missing ? typesplit(a, Missing) :
a

function promote_typejoin_union(::Type{T}) where T
if T === Union{}
Expand Down
11 changes: 6 additions & 5 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -578,19 +578,20 @@ elements that are not

function _sort!(v::AbstractVector, a::MissingOptimization, o::Ordering, kw)
@getkw lo hi
if nonmissingtype(eltype(v)) != eltype(v) && o isa DirectOrdering
if o isa DirectOrdering && eltype(v) >: Missing && nonmissingtype(eltype(v)) != eltype(v)
lo, hi = send_to_end!(ismissing, v, o; lo, hi)
_sort!(WithoutMissingVector(v, unsafe=true), a.next, o, (;kw..., lo, hi))
elseif eltype(v) <: Integer && o isa Perm && o.order isa DirectOrdering &&
nonmissingtype(eltype(o.data)) != eltype(o.data) &&
elseif o isa Perm && o.order isa DirectOrdering && eltype(v) <: Integer &&
eltype(o.data) >: Missing && nonmissingtype(eltype(o.data)) != eltype(o.data) &&
all(i === j for (i,j) in zip(v, eachindex(o.data)))
# TODO make this branch known at compile time
# This uses a custom function because we need to ensure stability of both sides and
# we can assume v is equal to eachindex(o.data) which allows a copying partition
# without allocations.
lo_i, hi_i = lo, hi
for (i,x) in zip(eachindex(o.data), o.data)
if ismissing(x) == (o.order == Reverse) # should i go at the beginning?
for i in eachindex(o.data) # equal to copy(v)
x = o.data[i]
if ismissing(x) == (o.order == Reverse) # should x go at the beginning/end?
v[lo_i] = i
lo_i += 1
else
Expand Down
8 changes: 5 additions & 3 deletions src/opaque_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static jl_opaque_closure_t *new_opaque_closure(jl_tupletype_t *argt, jl_value_t
JL_GC_PROMISE_ROOTED(oc_type);

jl_method_instance_t *mi = jl_specializations_get_linfo(source, sigtype, jl_emptysvec);
size_t world = jl_atomic_load_acquire(&jl_world_counter);
size_t world = jl_current_task->world_age;
jl_code_instance_t *ci = NULL;
if (do_compile)
ci = jl_compile_method_internal(mi, world);
Expand Down Expand Up @@ -127,12 +127,14 @@ JL_DLLEXPORT jl_opaque_closure_t *jl_new_opaque_closure_from_code_info(jl_tuplet
JL_GC_PUSH3(&root, &sigtype, &inst);
root = jl_box_long(lineno);
root = jl_new_struct(jl_linenumbernode_type, root, file);
root = (jl_value_t*)jl_make_opaque_closure_method(mod, jl_nothing, nargs, root, ci, isva);
jl_method_t *meth = jl_make_opaque_closure_method(mod, jl_nothing, nargs, root, ci, isva);
root = (jl_value_t*)meth;
meth->primary_world = jl_current_task->world_age;

sigtype = prepend_type(jl_typeof(env), argt);
jl_method_instance_t *mi = jl_specializations_get_linfo((jl_method_t*)root, sigtype, jl_emptysvec);
inst = jl_new_codeinst(mi, rt_ub, NULL, (jl_value_t*)ci,
0, ((jl_method_t*)root)->primary_world, -1, 0, 0, jl_nothing, 0);
0, meth->primary_world, -1, 0, 0, jl_nothing, 0);
jl_mi_cache_insert(mi, inst);

jl_opaque_closure_t *oc = new_opaque_closure(argt, rt_lb, rt_ub, root, env, do_compile);
Expand Down