Skip to content

Commit

Permalink
clean up imports and make them explicit (#614)
Browse files Browse the repository at this point in the history
Using ExplicitImports.jl, hopefully this makes it more explicit what are imported into JET's namespace.
  • Loading branch information
aviatesk authored Feb 25, 2024
1 parent 56520e0 commit 32374d0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 123 deletions.
94 changes: 25 additions & 69 deletions src/JET.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,94 +24,51 @@ Base.Experimental.@optlevel 1

const CC = Core.Compiler

# imports
# =======

# `AbstractAnalyzer`
import .CC:
#= cicache.jl =#
# get, getindex, haskey, push!, setindex!,
#= types.jl =#
InferenceParams, OptimizationParams, add_remark!, bail_out_call, bail_out_toplevel_call,
code_cache, get_inference_cache, lock_mi_inference, may_compress, may_discard_trees,
may_optimize, unlock_mi_inference, verbose_stmt_info, method_table,
#= inferenceresult.jl =#
cache_lookup,
#= inferencestate.jl =#
InferenceState,
#= tfuncs.jl =#
builtin_tfunction, return_type_tfunc,
#= abstractinterpretation.jl =#
abstract_call_known, abstract_call_gf_by_type, abstract_call_method,
abstract_call_method_with_const_args, abstract_eval_value_expr, abstract_eval_special_value,
abstract_eval_statement, abstract_eval_value, abstract_invoke, add_call_backedges!,
concrete_eval_call, concrete_eval_eligible, from_interprocedural!,
#= typeinfer.jl =#
_typeinf, finish!, finish, transform_result_for_cache, typeinf, typeinf_edge,
#= optimize.jl =#
inlining_policy

# `ConcreteInterpreter`
import JuliaInterpreter: evaluate_call_recurse!, handle_err, step_expr!

# Test.jl integration
import Test: record

# usings
# ======

using Core:
Argument, Builtin, CodeInfo, CodeInstance, Const, GlobalRef, GotoIfNot, GotoNode,
IntrinsicFunction, Intrinsics, LineInfoNode, MethodInstance, MethodMatch, MethodTable,
ReturnNode, SSAValue, SimpleVector, SlotNumber, svec
using Core: Builtin, IntrinsicFunction, Intrinsics, SimpleVector, svec

using Core.IR

using .CC: @nospecs, ,
AbstractInterpreter, ArgInfo, BasicBlock, Bottom, CFG, CachedMethodTable, CallMeta,
ConstCallInfo, InferenceResult, InternalMethodTable, InvokeCallInfo, LimitedAccuracy,
MethodCallResult, MethodLookupResult, MethodMatchInfo, MethodMatches, NOT_FOUND,
OptimizationState, OverlayMethodTable, StmtInfo, UnionSplitInfo, UnionSplitMethodMatches,
VarState, VarTable, WorldRange, WorldView,
argextype, argtype_by_index, argtype_tail, argtypes_to_type, compute_basic_blocks,
get_compileable_sig, hasintersect, has_free_typevars, ignorelimited, inlining_enabled,
instanceof_tfunc, is_inlineable, is_throw_call, isType, isconstType, issingletontype,
istopfunction, may_invoke_generator, singleton_type, slot_id, specialize_method,
switchtupleunion, tmerge, widenconst
AbstractInterpreter, AbstractLattice, ArgInfo, Bottom, CFG, CachedMethodTable, CallMeta,
ConstCallInfo, InferenceParams, InferenceResult, InferenceState, InternalMethodTable,
InvokeCallInfo, MethodCallResult, MethodMatchInfo, MethodMatches, NOT_FOUND,
OptimizationState, OptimizationParams, OverlayMethodTable, StmtInfo, UnionSplitInfo,
UnionSplitMethodMatches, VarState, VarTable, WorldRange, WorldView,
argextype, argtype_by_index, argtypes_to_type, hasintersect, ignorelimited,
instanceof_tfunc, istopfunction, singleton_type, slot_id, specialize_method,
tmeet, tmerge, typeinf_lattice, widenconst, widenlattice

using Base: IdSet
using Base: IdSet, get_world_counter

using Base.Meta: ParseError, _parse_string, isexpr, lower
using Base.Meta: ParseError, isexpr, lower

using Base.Experimental: @MethodTable, @overlay

using LoweredCodeUtils, JuliaInterpreter

using LoweredCodeUtils:
#=NamedVar,=# add_control_flow!, #=add_named_dependencies!, add_requests!,=#
add_ssa_preds!, add_typedefs!, callee_matches, find_typedefs, ismethod, istypedef,
print_with_code, pushall!, rng
using LoweredCodeUtils: LoweredCodeUtils, callee_matches

using JuliaInterpreter:
@lookup, _INACTIVE_EXCEPTION, bypass_builtins, collect_args, #=finish!,=#
is_quotenode_egal, maybe_evaluate_builtin, moduleof
using JuliaInterpreter: _INACTIVE_EXCEPTION, Frame, JuliaInterpreter, is_quotenode_egal

using MacroTools: @capture, MacroTools, normalise, striplines
using MacroTools: @capture, normalise, striplines

using InteractiveUtils
using InteractiveUtils: gen_call_with_extracted_types_and_kwargs

using Pkg, Pkg.TOML
using Pkg: Pkg, TOML

using Test:
Broken, DefaultTestSet, Error, Fail, FallbackTestSet, FallbackTestSetException, Pass,
Result, TESTSET_PRINT_ENABLE, Test, get_testset

using Preferences
using Preferences: Preferences

# common
# ======

const Argtypes = Vector{Any}

const JET_DEV_MODE = @load_preference("JET_DEV_MODE", false)
const JET_DEV_MODE = Preferences.@load_preference("JET_DEV_MODE", false)

const CONFIG_FILE_NAME = ".JET.toml"

Expand Down Expand Up @@ -139,9 +96,8 @@ end

@static if VERSION v"1.11.0-DEV.1498"
import .CC: get_inference_world
using Base: get_world_counter
else
import .CC: get_world_counter, get_world_counter as get_inference_world
import .CC: get_world_counter as get_inference_world
end

# macros
Expand Down Expand Up @@ -301,7 +257,7 @@ end
function is_compileable_mi(mi::MethodInstance)
def = mi.def
isa(def, Method) || return false
return get_compileable_sig(def, mi.specTypes, mi.sparam_vals) !== nothing
return CC.get_compileable_sig(def, mi.specTypes, mi.sparam_vals) !== nothing
end

get_linfo(sv::State) = sv.linfo
Expand Down Expand Up @@ -657,7 +613,7 @@ function analyze_gf_by_type!(analyzer::AbstractAnalyzer, @nospecialize(tt::Type{
end

function find_single_match(@nospecialize(tt), analyzer::AbstractAnalyzer)
match = Base._which(tt; method_table=method_table(analyzer), world=get_inference_world(analyzer), raise=false)
match = Base._which(tt; method_table=CC.method_table(analyzer), world=get_inference_world(analyzer), raise=false)
match === nothing && single_match_error(tt)
return match
end
Expand Down Expand Up @@ -709,7 +665,7 @@ end

function analyze_frame!(analyzer::AbstractAnalyzer, frame::InferenceState)
set_entry!(analyzer, frame.linfo)
typeinf(analyzer, frame)
CC.typeinf(analyzer, frame)
return analyzer, frame.result
end

Expand Down Expand Up @@ -1103,7 +1059,7 @@ function call_test_ex(funcname::Symbol, testname::Symbol, ex0, __module__, __sou
end

function _call_test_ex(funcname::Symbol, testname::Symbol, ex0, __module__, __source__)
analysis = InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, funcname, ex0)
analysis = gen_call_with_extracted_types_and_kwargs(__module__, funcname, ex0)
orig_expr = QuoteNode(Expr(:macrocall, GlobalRef(@__MODULE__, testname), __source__, ex0...))
source = QuoteNode(__source__)
testres = :(try
Expand Down
14 changes: 7 additions & 7 deletions src/abstractinterpret/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function analyze_additional_pass_by_type!(analyzer::AbstractAnalyzer, @nospecial
# the threaded code block as a usual code block, and thus the side-effects won't (hopefully)
# confuse the abstract interpretation, which is supposed to terminate on any kind of code
match = find_single_match(tt, newanalyzer)
abstract_call_method(newanalyzer, match.method, match.spec_types, match.sparams,
CC.abstract_call_method(newanalyzer, match.method, match.spec_types, match.sparams,
#=hardlimit=#false, #=si=#StmtInfo(false), sv)

return nothing
Expand All @@ -132,7 +132,7 @@ function CC.return_type_tfunc(analyzer::AbstractAnalyzer, argtypes::Argtypes, si
oldresult = analyzer[result]
init_result!(analyzer, result)
newanalyzer = AbstractAnalyzer(analyzer)
ret = @invoke return_type_tfunc(newanalyzer::AbstractInterpreter, argtypes::Argtypes, si::StmtInfo, sv::InferenceState)
ret = @invoke CC.return_type_tfunc(newanalyzer::AbstractInterpreter, argtypes::Argtypes, si::StmtInfo, sv::InferenceState)
analyzer[result] = oldresult
return ret
end
Expand Down Expand Up @@ -166,7 +166,7 @@ CC.haskey(wvc::WorldView{<:AbstractAnalyzerView}, mi::MethodInstance) = haskey(A

function CC.typeinf_edge(analyzer::AbstractAnalyzer, method::Method, @nospecialize(atype), sparams::SimpleVector, caller::InferenceState)
set_cache_target!(analyzer, :typeinf_edge => caller.result)
ret = @invoke typeinf_edge(analyzer::AbstractInterpreter, method::Method, atype::Any, sparams::SimpleVector, caller::InferenceState)
ret = @invoke CC.typeinf_edge(analyzer::AbstractInterpreter, method::Method, atype::Any, sparams::SimpleVector, caller::InferenceState)
@assert get_cache_target(analyzer) === nothing "invalid JET analysis state"
return ret
end
Expand Down Expand Up @@ -274,7 +274,7 @@ function CC.cache_lookup(𝕃ᵢ::CC.AbstractLattice, mi::MethodInstance, given_
cache_target = get_cache_target(analyzer)
set_cache_target!(analyzer, nothing)

inf_result = cache_lookup(𝕃ᵢ, mi, given_argtypes, get_inf_cache(view.analyzer))
inf_result = CC.cache_lookup(𝕃ᵢ, mi, given_argtypes, get_inf_cache(view.analyzer))

isa(inf_result, InferenceResult) || return inf_result

Expand Down Expand Up @@ -334,7 +334,7 @@ function CC.typeinf(analyzer::AbstractAnalyzer, frame::InferenceState)
filter_lineages!(analyzer, (parent::InferenceState).result, linfo)
end

ret = @invoke typeinf(analyzer::AbstractInterpreter, frame::InferenceState)
ret = @invoke CC.typeinf(analyzer::AbstractInterpreter, frame::InferenceState)

# elapsed = round(time() - sec; digits = 3)
# print_rails(io, depth)
Expand Down Expand Up @@ -459,7 +459,7 @@ function CC._typeinf(analyzer::AbstractAnalyzer, frame::InferenceState)
end
end
for caller in frames
finish!(#=CHANGED caller.interp=#analyzer, caller)
CC.finish!(#=CHANGED caller.interp=#analyzer, caller)
if CC.is_cached(caller)
CC.cache_result!(#=CHANGED caller.interp=#analyzer, caller.result)
end
Expand Down Expand Up @@ -540,7 +540,7 @@ function CC.transform_result_for_cache(analyzer::AbstractAnalyzer,
end
cache_report!(cache, report)
end
inferred_result = @invoke transform_result_for_cache(analyzer::AbstractInterpreter,
inferred_result = @invoke CC.transform_result_for_cache(analyzer::AbstractInterpreter,
linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult)
return CachedAnalysisResult(inferred_result, cache)
end
Expand Down
37 changes: 14 additions & 23 deletions src/analyzers/jetanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ CC.may_optimize(::JETAnalyzer) = false

CC.method_table(analyzer::JETAnalyzer) = analyzer.method_table

import .CC:
typeinf_lattice, ipo_lattice, widenlattice, is_valid_lattice_norec,
, tmerge, tmeet, _getfield_tfunc
using .CC:
AbstractLattice, InferenceLattice, MustAliasesLattice, InterMustAliasesLattice,
BaseInferenceLattice, IPOResultLattice

"""
IntrinsicErrorCheckLattice <: AbstractLattice
Expand All @@ -111,9 +104,9 @@ CC.is_valid_lattice_norec(::IntrinsicErrorCheckLattice, @nospecialize(elem)) = f
@nospecs CC.:(𝕃::IntrinsicErrorCheckLattice, x, y) = (widenlattice(𝕃), x, y)
@nospecs CC.tmerge(𝕃::IntrinsicErrorCheckLattice, x, y) = tmerge(widenlattice(𝕃), x, y)
@nospecs CC.tmeet(𝕃::IntrinsicErrorCheckLattice, x, t::Type) = tmeet(widenlattice(𝕃), x, t)
@nospecs CC._getfield_tfunc(𝕃::IntrinsicErrorCheckLattice, xs...) = _getfield_tfunc(widenlattice(𝕃), xs...)
CC.typeinf_lattice(::JETAnalyzer) = InferenceLattice(IntrinsicErrorCheckLattice(MustAliasesLattice(BaseInferenceLattice.instance)))
CC.ipo_lattice(::JETAnalyzer) = InferenceLattice(IntrinsicErrorCheckLattice(InterMustAliasesLattice(IPOResultLattice.instance)))
@nospecs CC._getfield_tfunc(𝕃::IntrinsicErrorCheckLattice, xs...) = CC._getfield_tfunc(widenlattice(𝕃), xs...)
CC.typeinf_lattice(::JETAnalyzer) = CC.InferenceLattice(IntrinsicErrorCheckLattice(CC.MustAliasesLattice(CC.BaseInferenceLattice.instance)))
CC.ipo_lattice(::JETAnalyzer) = CC.InferenceLattice(IntrinsicErrorCheckLattice(CC.InterMustAliasesLattice(CC.IPOResultLattice.instance)))

# AbstractAnalyzer API
# ====================
Expand Down Expand Up @@ -249,7 +242,7 @@ function CC.from_interprocedural!(analyzer::JETAnalyzer,
end

"""
bail_out_call(analyzer::JETAnalyzer, ...)
Core.Compiler.bail_out_call(analyzer::JETAnalyzer, ...)
This overload makes call inference performed by `JETAnalyzer` not bail out even when
inferred return type grows up to `Any` to collect as much error reports as possible.
Expand All @@ -261,7 +254,7 @@ CC.bail_out_call(::JETAnalyzer, ::CC.InferenceLoopState, ::InferenceState) = fal
struct __DummyConcrete__ end

"""
add_call_backedges!(analyzer::JETAnalyzer, ...)
Core.Compiler.add_call_backedges!(analyzer::JETAnalyzer, ...)
An overload for `abstract_call_gf_by_type(analyzer::JETAnalyzer, ...)`, which always add
backedges (even if a new method can't refine the return type grew up to `Any`).
Expand Down Expand Up @@ -450,7 +443,7 @@ function (::SoundBasicPass)(::Type{GeneratorErrorReport}, analyzer::JETAnalyzer,
m = mi.def::Method
if isdefined(m, :generator)
# analyze_method_instance!(analyzer, linfo) XXX doesn't work
may_invoke_generator(mi) || return false
CC.may_invoke_generator(mi) || return false
try
ccall(:jl_code_for_staged, Any, (Any,), mi) # invoke the "erroneous" generator again
catch err
Expand Down Expand Up @@ -544,9 +537,9 @@ function report_uncaught_exceptions!(analyzer::JETAnalyzer, frame::InferenceStat
for (pc, stmt) in enumerate(stmts)
isa(stmt, Expr) || continue
@static if VERSION v"1.11.0-DEV.888"
is_throw_call(stmt, stmts) || continue
CC.is_throw_call(stmt, stmts) || continue
else
is_throw_call(stmt) || continue
CC.is_throw_call(stmt) || continue
end
# if this `throw` is already reported, don't duplicate
if !isnothing(reported_locs) && linetable[codelocs[pc]]::LineInfoNode in reported_locs
Expand Down Expand Up @@ -674,7 +667,7 @@ function report_method_error_for_union_split!(analyzer::JETAnalyzer,
for (i, matchinfo) in enumerate(info.matches)
if is_empty_match(matchinfo)
if isnothing(split_argtypes)
split_argtypes = switchtupleunion(typeinf_lattice(analyzer), argtypes)
split_argtypes = CC.switchtupleunion(typeinf_lattice(analyzer), argtypes)
end
argtypes′ = split_argtypes[i]::Vector{Any}
if !sound && report_reduce_empty_error!(analyzer, sv, argtypes′)
Expand All @@ -688,7 +681,7 @@ function report_method_error_for_union_split!(analyzer::JETAnalyzer,
push!(empty_matches[1], sig_n)
elseif sound && !is_fully_covered(matchinfo)
if isnothing(split_argtypes)
split_argtypes = switchtupleunion(typeinf_lattice(analyzer), argtypes)
split_argtypes = CC.switchtupleunion(typeinf_lattice(analyzer), argtypes)
end
argtypes′ = split_argtypes[i]::Vector{Any}
if uncovered_matches === nothing
Expand Down Expand Up @@ -778,7 +771,7 @@ function print_report_message(io::IO, (; argtypes)::InvalidInvokeErrorReport)
return
end

argtype = argtypes_to_type(argtype_tail(argtypes, 4))
argtype = argtypes_to_type(CC.argtype_tail(argtypes, 4))
nargtype = typeintersect(types, argtype)
@assert nargtype === Bottom
print(io, "actual argument type `", argtype, "` doesn't intersect with specified argument type `", types, '`')
Expand Down Expand Up @@ -1153,8 +1146,6 @@ end
return ok
end

import .CC: bitcast_tfunc, conversion_tfunc, math_tfunc, shift_tfunc, cmp_tfunc, chk_tfunc

@nospecs CC.bitcast_tfunc(𝕃::IntrinsicErrorCheckLattice, t, x) = with_conversion_errorcheck(t, x, #=bitshift=#true)
@nospecs CC.conversion_tfunc(𝕃::IntrinsicErrorCheckLattice, t, x) = with_conversion_errorcheck(t, x)
@nospecs CC.math_tfunc(𝕃::IntrinsicErrorCheckLattice, a, bs...) = with_intrinsic_errorcheck(widenconst(a), a, bs...)
Expand Down Expand Up @@ -1273,11 +1264,11 @@ function report_fieldaccess!(analyzer::JETAnalyzer, sv::InferenceState, @nospeci

isa(name, Const) || return false
s = Base.unwrap_unionall(s00)
if isType(s)
if CC.isType(s)
if f === fieldtype
# XXX this is a hack to share more code between `getfield`/`setfield!`/`fieldtype`
s00 = s = s.parameters[1]
elseif isconstType(s)
elseif CC.isconstType(s)
s = (s00::DataType).parameters[1]
else
return false
Expand Down Expand Up @@ -1448,7 +1439,7 @@ The [general configurations](@ref) and [the error analysis specific configuratio
can be specified as an optional argument.
"""
macro report_call(ex0...)
return InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :report_call, ex0)
return gen_call_with_extracted_types_and_kwargs(__module__, :report_call, ex0)
end

# Test.jl integration
Expand Down
4 changes: 2 additions & 2 deletions src/analyzers/optanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ function CC.const_prop_call(analyzer::OptAnalyzer,
nothing::Nothing)
end

# TODO better to work only `finish!`
# TODO better to work only `CC.finish!`
function CC.finish(frame::InferenceState, analyzer::OptAnalyzer)
ret = @invoke CC.finish(frame::InferenceState, analyzer::AbstractAnalyzer)

Expand Down Expand Up @@ -449,7 +449,7 @@ The [general configurations](@ref) and [the optimization analysis specific confi
can be specified as an optional argument.
"""
macro report_opt(ex0...)
return InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :report_opt, ex0)
return gen_call_with_extracted_types_and_kwargs(__module__, :report_opt, ex0)
end

"""
Expand Down
Loading

0 comments on commit 32374d0

Please sign in to comment.