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

update to https://github.com/JuliaLang/julia/pull/42082 #246

Merged
merged 1 commit into from
Sep 4, 2021
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
7 changes: 3 additions & 4 deletions examples/dispatch_analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ import JET:
JET,
@invoke,
get_source,
@isexpr,
gen_call_with_extracted_types_and_kwargs
@isexpr

struct DispatchAnalyzer{T} <: AbstractAnalyzer
state::AnalyzerState
Expand Down Expand Up @@ -170,7 +169,7 @@ end
# So we defined our analyzer.
# Let's setup utility analysis entries first:

using JET # to use analysis entry points
using JET, InteractiveUtils # to use analysis entry points

function report_dispatch(@nospecialize(f), @nospecialize(types = Tuple{});
analyzer = DispatchAnalyzer,
Expand All @@ -179,7 +178,7 @@ function report_dispatch(@nospecialize(f), @nospecialize(types = Tuple{});
report_call(f, types; analyzer, jetconfigs...)
end
macro report_dispatch(ex0...)
return gen_call_with_extracted_types_and_kwargs(__module__, :report_dispatch, ex0)
return InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :report_dispatch, ex0)
end

# Now we can just call `@report_dispatch f(args...)` and check if there are any problematic
Expand Down
42 changes: 19 additions & 23 deletions src/JET.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,8 @@ __init__() = foreach(@nospecialize(f)->f(), INIT_HOOKS)
# compat
# ------

# early take in https://github.com/JuliaLang/julia/pull/41040
function gen_call_with_extracted_types_and_kwargs(__module__, fcn, ex0)
kws = Expr[]
arg = ex0[end] # Mandatory argument
for i in 1:length(ex0)-1
x = ex0[i]
if x isa Expr && x.head === :(=) # Keyword given of the form "foo=bar"
if length(x.args) != 2
return Expr(:call, :error, "Invalid keyword argument: $x")
end
push!(kws, Expr(:kw, esc(x.args[1]), esc(x.args[2])))
else
return Expr(:call, :error, "@$fcn expects only one non-keyword argument")
end
end
return InteractiveUtils.gen_call_with_extracted_types(__module__, fcn, arg, kws)
end
# branch on https://github.com/JuliaLang/julia/pull/42082
const IS_AFTER_42082 = hasmethod(InferenceState, (InferenceResult, Symbol, AbstractInterpreter))

# macros
# ------
Expand Down Expand Up @@ -661,15 +646,22 @@ analyze_method_signature!(analyzer::AbstractAnalyzer, m::Method, @nospecialize(a
function analyze_method_instance!(analyzer::AbstractAnalyzer, mi::MethodInstance)
result = InferenceResult(mi)

frame = InferenceState(result, #=cached=# true, analyzer)
@static if IS_AFTER_42082
frame = InferenceState(result, #=cache=# :global, analyzer)
else # static if IS_AFTER_42082
frame = InferenceState(result, #=cached=# true, analyzer)
end # static if IS_AFTER_42082

isnothing(frame) && return analyzer, result

return analyze_frame!(analyzer, frame)
end

function InferenceState(result::InferenceResult, cached::Bool, analyzer::AbstractAnalyzer)
const CACHE_ARG_TYPE = IS_AFTER_42082 ? Symbol : Bool

function InferenceState(result::InferenceResult, cache::CACHE_ARG_TYPE, analyzer::AbstractAnalyzer)
set_result!(result) # modify `result` for succeeding JET analysis
return @invoke InferenceState(result::InferenceResult, cached::Bool, analyzer::AbstractInterpreter)
return @invoke InferenceState(result::InferenceResult, cache::CACHE_ARG_TYPE, analyzer::AbstractInterpreter)
end

function analyze_frame!(analyzer::AbstractAnalyzer, frame::InferenceState)
Expand Down Expand Up @@ -1062,7 +1054,11 @@ function analyze_toplevel!(analyzer::AbstractAnalyzer, src::CodeInfo)
# toplevel frames don't really need to be cached, but still better to be optimized
# in order to get reasonable `LocalUndefVarErrorReport` and `UncaughtExceptionReport`
# NOTE and also, otherwise `typeinf_edge` won't add "toplevel-to-callee" edges
frame = InferenceState(result, src, #=cached=# true, analyzer);
@static if IS_AFTER_42082
frame = InferenceState(result, src, #=cache=# :global, analyzer)
else # @static if IS_AFTER_42082
frame = InferenceState(result, src, #=cached=# true, analyzer)
end # @static if IS_AFTER_42082

return analyze_frame!(analyzer, frame)
end
Expand Down Expand Up @@ -1164,7 +1160,7 @@ julia> @report_call aggressive_constant_propagation=false rand(Bool)
```
"""
macro report_call(ex0...)
return gen_call_with_extracted_types_and_kwargs(__module__, :report_call, ex0)
return InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :report_call, ex0)
end

"""
Expand Down Expand Up @@ -1339,7 +1335,7 @@ end
end

function test_exs(ex0, m, source)
analysis = gen_call_with_extracted_types_and_kwargs(m, :report_call, ex0)
analysis = InteractiveUtils.gen_call_with_extracted_types_and_kwargs(m, :report_call, ex0)
orig_expr = QuoteNode(
Expr(:macrocall, GlobalRef(@__MODULE__, Symbol("@test_call")), source, ex0...))
source = QuoteNode(source)
Expand Down
20 changes: 13 additions & 7 deletions src/abstractinterpret/abstractanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,25 +639,31 @@ CC.may_compress(analyzer::AbstractAnalyzer) = false
CC.may_discard_trees(analyzer::AbstractAnalyzer) = false
CC.verbose_stmt_info(analyzer::AbstractAnalyzer) = false

# branch on https://github.com/JuliaLang/julia/pull/41328
@static if isdefined(CC, :is_stmt_inline)
# branch on https://github.com/JuliaLang/julia/pull/41328 & https://github.com/JuliaLang/julia/pull/42082
@static if IS_AFTER_42082
@doc """
inlining_policy(analyzer::AbstractAnalyzer, @nospecialize(src), stmt_flag::UInt8) -> source::Any
inlining_policy(
analyzer::AbstractAnalyzer, @nospecialize(src), stmt_flag::UInt8,
mi::MethodInstance, argtypes::Vector{Any}) -> source::Any

Implements inlining policy for `AbstractAnalyzer`.
Since `AbstractAnalyzer` works on `InferenceResult` whose `src` field keeps
[`JETResult`](@ref) or [`JETCachedResult`](@ref), this implementation just bypasses
their wrapped source to `inlining_policy(::AbstractInterpreter, ::Any, ::UInt8)`.
"""
function CC.inlining_policy(analyzer::AbstractAnalyzer, @nospecialize(src), stmt_flag::UInt8)
function CC.inlining_policy(
analyzer::AbstractAnalyzer, @nospecialize(src), stmt_flag::UInt8,
mi::MethodInstance, argtypes::Vector{Any})
if isa(src, JETResult)
src = get_source(src)
elseif isa(src, JETCachedResult)
src = get_source(src)
end
return @invoke CC.inlining_policy(analyzer::AbstractInterpreter, @nospecialize(src), stmt_flag::UInt8)
return @invoke CC.inlining_policy(
analyzer::AbstractInterpreter, @nospecialize(src), stmt_flag::UInt8,
mi::MethodInstance, argtypes::Vector{Any})
end
else # @static if isdefined(Compiler, :is_stmt_inline)
else # @static if IS_AFTER_42082
@doc """
inlining_policy(::AbstractAnalyzer) = jet_inlining_policy
jet_inlining_policy(@nospecialize(src)) -> source::Any
Expand All @@ -676,7 +682,7 @@ CC.inlining_policy(::AbstractAnalyzer) = jet_inlining_policy
end
return CC.default_inlining_policy(src)
end
end # @static if isdefined(Compiler, :is_stmt_inline)
end # @static if IS_AFTER_42082

# AbstractAnalyzer
# ================
Expand Down
4 changes: 2 additions & 2 deletions src/analyzers/jetanalyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const SoundBasicPass = Union{SoundPass,BasicPass}
# analysis
# ========

function InferenceState(result::InferenceResult, cached::Bool, analyzer::JETAnalyzer)
frame = @invoke InferenceState(result::InferenceResult, cached::Bool, analyzer::AbstractAnalyzer)
function InferenceState(result::InferenceResult, cache::CACHE_ARG_TYPE, analyzer::JETAnalyzer)
frame = @invoke InferenceState(result::InferenceResult, cache::CACHE_ARG_TYPE, analyzer::AbstractAnalyzer)
if isnothing(frame) # indicates something bad happened within `retrieve_code_info`
ReportPass(analyzer)(GeneratorErrorReport, analyzer, result)
end
Expand Down