From 1ab94c74e7e62393844df68b042c0a82c94a0836 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Thu, 23 Sep 2021 11:12:12 +0900 Subject: [PATCH] compiler: unify `singleton_type` and `argtype_to_function` (#42342) They are supposed to do the same thing. Previously `singleton_type` didn't handle the case where `isconstType(ft) -> ft.parameters[1]`, but it can just include that. --- base/compiler/abstractinterpretation.jl | 16 ++-------------- base/compiler/ssair/inlining.jl | 9 --------- base/compiler/utilities.jl | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index 742783e2baa78a..a10e1878ad52fd 100644 --- a/base/compiler/abstractinterpretation.jl +++ b/base/compiler/abstractinterpretation.jl @@ -1235,7 +1235,7 @@ function abstract_invoke(interp::AbstractInterpreter, argtypes::Vector{Any}, sv: # t, a = ti.parameters[i], argtypes′[i] # argtypes′[i] = t ⊑ a ? t : a # end - const_result = abstract_call_method_with_const_args(interp, result, argtype_to_function(ft′), argtypes′, match, sv, false) + const_result = abstract_call_method_with_const_args(interp, result, singleton_type(ft′), argtypes′, match, sv, false) if const_result !== nothing const_rt, const_result = const_result if const_rt !== rt && const_rt ⊑ rt @@ -1379,7 +1379,7 @@ function abstract_call(interp::AbstractInterpreter, fargs::Union{Nothing,Vector{ sv::InferenceState, max_methods::Int = InferenceParams(interp).MAX_METHODS) #print("call ", e.args[1], argtypes, "\n\n") ft = argtypes[1] - f = argtype_to_function(ft) + f = singleton_type(ft) if isa(ft, PartialOpaque) return abstract_call_opaque_closure(interp, ft, argtypes[2:end], sv) elseif (uft = unwrap_unionall(ft); isa(uft, DataType) && uft.name === typename(Core.OpaqueClosure)) @@ -1396,18 +1396,6 @@ function abstract_call(interp::AbstractInterpreter, fargs::Union{Nothing,Vector{ return abstract_call_known(interp, f, fargs, argtypes, sv, max_methods) end -function argtype_to_function(@nospecialize(ft)) - if isa(ft, Const) - return ft.val - elseif isconstType(ft) - return ft.parameters[1] - elseif isa(ft, DataType) && isdefined(ft, :instance) - return ft.instance - else - return nothing - end -end - function sp_type_rewrap(@nospecialize(T), linfo::MethodInstance, isreturn::Bool) isref = false if T === Bottom diff --git a/base/compiler/ssair/inlining.jl b/base/compiler/ssair/inlining.jl index 7c229a3b2081a1..b59fd8a5bd3dfc 100644 --- a/base/compiler/ssair/inlining.jl +++ b/base/compiler/ssair/inlining.jl @@ -717,15 +717,6 @@ function rewrite_invoke_exprargs!(argexprs::Vector{Any}) return argexprs end -function singleton_type(@nospecialize(ft)) - if isa(ft, Const) - return ft.val - elseif ft isa DataType && isdefined(ft, :instance) - return ft.instance - end - return nothing -end - function compileable_specialization(et::Union{EdgeTracker, Nothing}, match::MethodMatch) mi = specialize_method(match; compilesig=true) mi !== nothing && et !== nothing && push!(et, mi::MethodInstance) diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index 85c81f7f12f547..1da32ebd7b386c 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -222,6 +222,10 @@ function method_for_inference_heuristics(method::Method, @nospecialize(sig), spa return nothing end +######### +# types # +######### + argextype(@nospecialize(x), state) = argextype(x, state.src, state.sptypes, state.slottypes) const empty_slottypes = Any[] @@ -259,6 +263,17 @@ function argextype(@nospecialize(x), src, sptypes::Vector{Any}, slottypes::Vecto end end +function singleton_type(@nospecialize(ft)) + if isa(ft, Const) + return ft.val + elseif isconstType(ft) + return ft.parameters[1] + elseif ft isa DataType && isdefined(ft, :instance) + return ft.instance + end + return nothing +end + ################### # SSAValues/Slots # ###################