From 07ffecb2257d40db7452a3f335f3b38796272e76 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 23 Aug 2020 09:52:41 -0500 Subject: [PATCH] More inferrability improvements Detected as part of my work on https://github.com/JuliaLang/julia/pull/37163 --- src/Cthulhu.jl | 12 ++++++++++-- src/reflection.jl | 16 ++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 9503afad..0b33f79b 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -7,6 +7,12 @@ using UUIDs using Core: MethodInstance const Compiler = Core.Compiler +if isdefined(Base, :mapany) + const mapany = Base.mapany +else + mapany(f, itr) = map!(f, Vector{Any}(undef, length(itr)::Int), itr) # convenient for Expr.args +end + Base.@kwdef mutable struct CthulhuConfig enable_highlighter::Bool = false highlighter::Cmd = `pygmentize -l` @@ -174,7 +180,9 @@ function _descend(mi::MethodInstance; iswarn::Bool, params=current_params(), opt callsite = callsites[cid] if callsite.info isa MultiCallInfo - sub_callsites = map(ci->Callsite(callsite.id, ci), callsite.info.callinfos) + sub_callsites = let callsite=callsite + map(ci->Callsite(callsite.id, ci), callsite.info.callinfos) + end if isempty(sub_callsites) @warn "Expected multiple callsites, but found none. Please fill an issue with a reproducing example" callsite.info continue @@ -231,7 +239,7 @@ function _descend(mi::MethodInstance; iswarn::Bool, params=current_params(), opt id = Base.PkgId(UUID("295af30f-e4ad-537b-8983-00126c2a3abe"), "Revise") mod = get(Base.loaded_modules, id, nothing) if mod !== nothing - revise = getfield(mod, :revise) + revise = getfield(mod, :revise)::Function revise() mi = first_method_instance(mi.specTypes) end diff --git a/src/reflection.jl b/src/reflection.jl index 14a26ed4..c145a45d 100644 --- a/src/reflection.jl +++ b/src/reflection.jl @@ -38,7 +38,7 @@ function transform(::Val{:CuFunction}, callsite, callexpr, CI, mi, slottypes; pa return Callsite(callsite.id, CuCallInfo(callinfo(Tuple{widenconst(ft), tt.val.parameters...}, Nothing, params=params))) end -function find_callsites(CI, mi, slottypes; params=current_params(), multichoose::Bool=false, kwargs...) +function find_callsites(CI::Core.CodeInfo, mi::Core.MethodInstance, slottypes; params=current_params(), multichoose::Bool=false, kwargs...) sptypes = sptypes_from_meth_instance(mi) callsites = Callsite[] @@ -87,7 +87,7 @@ function find_callsites(CI, mi, slottypes; params=current_params(), multichoose: end elseif c.head === :call rt = CI.ssavaluetypes[id] - types = map(arg -> widenconst(argextype(arg, CI, sptypes, slottypes)), c.args) + types = mapany(arg -> widenconst(argextype(arg, CI, sptypes, slottypes)), c.args) # Look through _apply ok = true @@ -142,9 +142,9 @@ function find_callsites(CI, mi, slottypes; params=current_params(), multichoose: end thatcher(types[1]) sigs = let types=types - map(ft-> [ft, types[2:end]...], fts) + mapany(ft-> Any[ft, types[2:end]...], fts) end - cis = map(t -> callinfo(Tuple{t...}, rt, params=params), sigs) + cis = CallInfo[callinfo(Tuple{t...}, rt, params=params) for t in sigs] callsite = Callsite(id, MultiCallInfo(Tuple{types...}, rt, cis)) else ft = Base.unwrap_unionall(types[1]) @@ -201,7 +201,7 @@ if isdefined(Core.Compiler, :AbstractInterpreter) ccall(:jl_typeinf_begin, Cvoid, ()) result = Core.Compiler.InferenceResult(mi) frame = Core.Compiler.InferenceState(result, false, interp) - frame === nothing && return (nothing, Any) + frame === nothing && return (nothing, Any, Any[]) if Compiler.typeinf(interp, frame) && run_optimizer oparams = Core.Compiler.OptimizationParams(interp) opt = Compiler.OptimizationState(frame, oparams, interp) @@ -209,7 +209,7 @@ if isdefined(Core.Compiler, :AbstractInterpreter) opt.src.inferred = true end ccall(:jl_typeinf_end, Cvoid, ()) - frame.inferred || return (nothing, Any) + frame.inferred || return (nothing, Any, Any[]) return (frame.src, result.result, frame.slottypes) end else @@ -217,14 +217,14 @@ else ccall(:jl_typeinf_begin, Cvoid, ()) result = Core.Compiler.InferenceResult(mi) frame = Core.Compiler.InferenceState(result, false, params) - frame === nothing && return (nothing, Any) + frame === nothing && return (nothing, Any, Any[]) if Compiler.typeinf(frame) && run_optimizer opt = Compiler.OptimizationState(frame) Compiler.optimize(opt, result.result) opt.src.inferred = true end ccall(:jl_typeinf_end, Cvoid, ()) - frame.inferred || return (nothing, Any) + frame.inferred || return (nothing, Any, Any[]) return (frame.src, result.result, frame.slottypes) end end