Skip to content

Commit

Permalink
minor followups on recent CodeInstance refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Mar 4, 2024
1 parent 206b258 commit f8d7b0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
47 changes: 20 additions & 27 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,6 @@ function finish!(interp::AbstractInterpreter, caller::InferenceState)
if opt isa OptimizationState
result.src = opt = ir_to_codeinf!(opt)
end
if opt isa CodeInfo
caller.src = opt
else
# In this case `caller.src` is invalid for clients (such as `typeinf_ext`) to use
# but that is what's permitted by `caller.cache_mode`.
# This is hopefully unreachable from such clients using `NativeInterpreter`.
end
return nothing
end

Expand Down Expand Up @@ -277,7 +270,7 @@ function is_result_constabi_eligible(result::InferenceResult)
return isa(result_type, Const) && is_foldable_nothrow(result.ipo_effects) && is_inlineable_constant(result_type.val)
end
function CodeInstance(interp::AbstractInterpreter, result::InferenceResult;
can_discard_trees::Bool=may_discard_trees(interp))
can_discard_trees::Bool=may_discard_trees(interp))
local const_flags::Int32
result_type = result.result
@assert !(result_type === nothing || result_type isa LimitedAccuracy)
Expand Down Expand Up @@ -315,7 +308,7 @@ function CodeInstance(interp::AbstractInterpreter, result::InferenceResult;
inferred_result = nothing
relocatability = 0x1
else
inferred_result = transform_result_for_cache(interp, result.linfo, result.valid_worlds, result, can_discard_trees)
inferred_result = transform_result_for_cache(interp, result)
if inferred_result isa CodeInfo
uncompressed = inferred_result
inferred_result = maybe_compress_codeinfo(interp, result.linfo, inferred_result, can_discard_trees)
Expand All @@ -341,19 +334,17 @@ function CodeInstance(interp::AbstractInterpreter, result::InferenceResult;
relocatability)
end

function transform_result_for_cache(interp::AbstractInterpreter,
linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult,
can_discard_trees::Bool=may_discard_trees(interp))
function transform_result_for_cache(interp::AbstractInterpreter, result::InferenceResult)
return result.src
end

function maybe_compress_codeinfo(interp::AbstractInterpreter, linfo::MethodInstance, ci::CodeInfo,
can_discard_trees::Bool=may_discard_trees(interp))
def = linfo.def
function maybe_compress_codeinfo(interp::AbstractInterpreter, mi::MethodInstance, ci::CodeInfo,
can_discard_trees::Bool=may_discard_trees(interp))
def = mi.def
isa(def, Method) || return ci # don't compress toplevel code
cache_the_tree = true
if can_discard_trees
cache_the_tree = is_inlineable(ci) || isa_compileable_sig(linfo.specTypes, linfo.sparam_vals, def)
cache_the_tree = is_inlineable(ci) || isa_compileable_sig(mi.specTypes, mi.sparam_vals, def)
end
if cache_the_tree
if may_compress(interp)
Expand Down Expand Up @@ -572,13 +563,13 @@ function finish(me::InferenceState, interp::AbstractInterpreter)
# annotate fulltree with type information,
# either because we are the outermost code, or we might use this later
type_annotate!(interp, me)
doopt = (me.cache_mode != CACHE_MODE_NULL || me.parent !== nothing)
# Disable the optimizer if we've already determined that there's nothing for
# it to do.
if may_discard_trees(interp) && is_result_constabi_eligible(me.result)
doopt = false
end
if doopt && may_optimize(interp)
mayopt = may_optimize(interp)
doopt = mayopt &&
# disable optimization if we don't use this later
(me.cache_mode != CACHE_MODE_NULL || me.parent !== nothing) &&
# disable optimization if we've already obtained very accurate result
!result_is_constabi(interp, me.result, mayopt)
if doopt
me.result.src = OptimizationState(me, interp)
else
me.result.src = me.src # for reflection etc.
Expand Down Expand Up @@ -948,7 +939,8 @@ function codeinstance_for_const_with_code(interp::AbstractInterpreter, code::Cod
code.relocatability)
end

result_is_constabi(interp::AbstractInterpreter, run_optimizer::Bool, result::InferenceResult) =
result_is_constabi(interp::AbstractInterpreter, result::InferenceResult,
run_optimizer::Bool=may_optimize(interp)) =
run_optimizer && may_discard_trees(interp) && is_result_constabi_eligible(result)

# compute an inferred AST and return type
Expand All @@ -961,7 +953,7 @@ function typeinf_code(interp::AbstractInterpreter, mi::MethodInstance, run_optim
frame = typeinf_frame(interp, mi, run_optimizer)
frame === nothing && return nothing, Any
is_inferred(frame) || return nothing, Any
if result_is_constabi(interp, run_optimizer, frame.result)
if result_is_constabi(interp, frame.result, run_optimizer)
rt = frame.result.result::Const
return codeinfo_for_const(interp, frame.linfo, rt.val), widenconst(rt)
end
Expand Down Expand Up @@ -1137,8 +1129,9 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
# Inference result is not cacheable or is was cacheable, but we do not want to
# store the source in the cache, but the caller wanted it anyway (e.g. for reflection).
# We construct a new CodeInstance for it that is not part of the cache hierarchy.
code = CodeInstance(interp, result, can_discard_trees=(
source_mode != SOURCE_MODE_FORCE_SOURCE && source_mode != SOURCE_MODE_FORCE_SOURCE_UNCACHED))
can_discard_trees = !(source_mode == SOURCE_MODE_FORCE_SOURCE ||
source_mode == SOURCE_MODE_FORCE_SOURCE_UNCACHED)
code = CodeInstance(interp, result; can_discard_trees)

# If the caller cares about the code and this is constabi, still use our synthesis function
# anyway, because we will have not finished inferring the code inside the CodeInstance once
Expand Down
9 changes: 4 additions & 5 deletions test/compiler/AbstractInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CC.may_optimize(::AbsIntOnlyInterp1) = false
# it should work even if the interpreter discards inferred source entirely
@newinterp AbsIntOnlyInterp2
CC.may_optimize(::AbsIntOnlyInterp2) = false
CC.transform_result_for_cache(::AbsIntOnlyInterp2, ::Core.MethodInstance, ::CC.WorldRange, ::CC.InferenceResult) = nothing
CC.transform_result_for_cache(::AbsIntOnlyInterp2, ::CC.InferenceResult) = nothing
@test Base.infer_return_type(Base.init_stdio, (Ptr{Cvoid},); interp=AbsIntOnlyInterp2()) >: IO

# OverlayMethodTable
Expand Down Expand Up @@ -490,10 +490,9 @@ struct CustomData
inferred
CustomData(@nospecialize inferred) = new(inferred)
end
function CC.transform_result_for_cache(interp::CustomDataInterp,
mi::Core.MethodInstance, valid_worlds::CC.WorldRange, result::CC.InferenceResult)
inferred_result = @invoke CC.transform_result_for_cache(interp::CC.AbstractInterpreter,
mi::Core.MethodInstance, valid_worlds::CC.WorldRange, result::CC.InferenceResult)
function CC.transform_result_for_cache(interp::CustomDataInterp, result::CC.InferenceResult)
inferred_result = @invoke CC.transform_result_for_cache(
interp::CC.AbstractInterpreter, result::CC.InferenceResult)
return CustomData(inferred_result)
end
function CC.src_inlining_policy(interp::CustomDataInterp, @nospecialize(src),
Expand Down
7 changes: 3 additions & 4 deletions test/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1787,10 +1787,9 @@ let newinterp_path = abspath("compiler/newinterp.jl")
inferred
CustomData(@nospecialize inferred) = new(inferred)
end
function CC.transform_result_for_cache(interp::PrecompileInterpreter,
mi::Core.MethodInstance, valid_worlds::CC.WorldRange, result::CC.InferenceResult)
inferred_result = @invoke CC.transform_result_for_cache(interp::CC.AbstractInterpreter,
mi::Core.MethodInstance, valid_worlds::CC.WorldRange, result::CC.InferenceResult)
function CC.transform_result_for_cache(interp::PrecompileInterpreter, result::CC.InferenceResult)
inferred_result = @invoke CC.transform_result_for_cache(
interp::CC.AbstractInterpreter, result::CC.InferenceResult)
return CustomData(inferred_result)
end
function CC.src_inlining_policy(interp::PrecompileInterpreter, @nospecialize(src),
Expand Down

0 comments on commit f8d7b0f

Please sign in to comment.