Skip to content

Commit

Permalink
update to avi/typelattice, incoming type lattice overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Oct 13, 2021
1 parent af1a2fc commit 7599fa8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
55 changes: 47 additions & 8 deletions src/Cthulhu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ using InteractiveUtils
using UUIDs
using REPL: REPL, AbstractTerminal

using Core: MethodInstance
import Core: MethodInstance, OpaqueClosure
const Compiler = Core.Compiler
using Core.Compiler: MethodMatch, LimitedAccuracy, ignorelimited
import Core.Compiler: MethodMatch, LimitedAccuracy, widenconst, ignorelimited, Const,
PartialStruct, InterConditional, PartialOpaque

const mapany = Base.mapany

Expand All @@ -21,6 +22,15 @@ else
macro constprop(_, ex); esc(ex); end
end

const IS_OVERHAULED = isdefined(Core.Compiler, :TypeLattice)
@static if IS_OVERHAULED
import Core.Compiler: ⊤, ⊥, TypeLattice, NativeType, TypeLattice, isConst,
isLimitedAccuracy, Lattices, SSAValueTypes
else
const Lattices = Vector{Any}
const SSAValueTypes = Vector{Any}
end

Base.@kwdef mutable struct CthulhuConfig
enable_highlighter::Bool = false
highlighter::Cmd = `pygmentize -l`
Expand Down Expand Up @@ -167,15 +177,35 @@ const descend = descend_code_typed

descend(interp::CthulhuInterpreter, mi::MethodInstance; kwargs...) = _descend(interp, mi; iswarn=false, interruptexc=false, kwargs...)

@static if IS_OVERHAULED
import Core.Compiler: InterConditionalInfo
function codeinst_rt(code::CodeInstance)
rettype = code.rettype
if isdefined(code, :rettype_const)
rettype_const = code.rettype_const
if isa(rettype_const, Vector{Any}) && !(Vector{Any} <: rettype)
return PartialStruct(rettype, rettype_const)
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
return rettype_const
elseif isa(rettype_const, InterConditionalInfo) && !(InterConditionalInfo <: rettype)
return InterConditional(rettype_const.slot, rettype_const.vtype, rettype_const.elsetype)
else
return Const(rettype_const)
end
else
return rettype
end
end
else # @static if IS_OVERHAULED
function codeinst_rt(code::CodeInstance)
rettype = code.rettype
if isdefined(code, :rettype_const)
rettype_const = code.rettype_const
if isa(rettype_const, Vector{Any}) && !(Vector{Any} <: rettype)
return Core.PartialStruct(rettype, rettype_const)
elseif isa(rettype_const, Core.PartialOpaque) && rettype <: Core.OpaqueClosure
return PartialStruct(rettype, rettype_const)
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
return rettype_const
elseif isa(rettype_const, Core.InterConditional) && !(Core.InterConditional <: rettype)
elseif isa(rettype_const, InterConditional) && !(InterConditional <: rettype)
return rettype_const
else
return Const(rettype_const)
Expand All @@ -184,6 +214,7 @@ function codeinst_rt(code::CodeInstance)
return rettype
end
end
end # @static if IS_OVERHAULED

# `@constprop :aggressive` here in order to make sure the constant propagation of `allow_no_codeinf`
@constprop :aggressive function lookup(interp::CthulhuInterpreter, mi::MethodInstance, optimize::Bool; allow_no_codeinf::Bool=false)
Expand All @@ -193,7 +224,11 @@ end
infos = interp.unopt[mi].stmt_infos
slottypes = codeinf.slottypes
if isnothing(slottypes)
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
@static if IS_OVERHAULED
slottypes = TypeLattice[ ⊤ for i = 1:length(codeinf.slotflags) ]
else
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
end
end
else
codeinst = interp.opt[mi]
Expand All @@ -209,7 +244,11 @@ end
# But with coverage on, the empty function body isn't empty due to :code_coverage_effect expressions.
codeinf = nothing
infos = []
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
@static if IS_OVERHAULED
slottypes = AbstractLatticce[NativeType(t) for t in Base.unwrap_unionall(mi.specTypes).parameters]
else
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
end
else
Core.eval(Main, quote
interp = $interp
Expand All @@ -219,7 +258,7 @@ end
error("couldn't find the source; inspect `Main.interp` and `Main.mi`")
end
end
(codeinf, rt, infos, slottypes::Vector{Any})
return codeinf, rt, infos, slottypes::Lattices
end

##
Expand Down
6 changes: 3 additions & 3 deletions src/callsite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct MICallInfo <: CallInfo
mi::MethodInstance
rt
function MICallInfo(mi::MethodInstance, @nospecialize(rt))
if isa(rt, LimitedAccuracy)
if @static IS_OVERHAULED ? isLimitedAccuracy(rt) : isa(rt, LimitedAccuracy)
return LimitedCallInfo(new(mi, ignorelimited(rt)))
else
return new(mi, rt)
Expand Down Expand Up @@ -36,9 +36,9 @@ struct UncachedCallInfo <: WrappedCallInfo
end

struct PureCallInfo <: CallInfo
argtypes::Vector{Any}
argtypes::Lattices
rt
PureCallInfo(argtypes::Vector{Any}, @nospecialize(rt)) =
PureCallInfo(argtypes::Lattices, @nospecialize(rt)) =
new(argtypes, rt)
end
get_mi(::PureCallInfo) = nothing
Expand Down
14 changes: 10 additions & 4 deletions src/codeview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
if isa(src, Core.CodeInfo)
# we're working on pre-optimization state, need to ignore `LimitedAccuracy`
src = copy(src)
src.ssavaluetypes = Base.mapany(ignorelimited, src.ssavaluetypes::Vector{Any})
@static if IS_OVERHAULED
src.ssavaluetypes = SSAValueTypes([ignorelimited(t) for t in src.ssavaluetypes::SSAValueTypes])
else
src.ssavaluetypes = Base.mapany(ignorelimited, src.ssavaluetypes::SSAValueTypes)
end
src.rettype = ignorelimited(src.rettype)

if src.slotnames !== nothing
Expand All @@ -144,8 +148,9 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
isa(mi, MethodInstance) || throw("`mi::MethodInstance` is required")
code = src isa IRCode ? src.stmts.inst : src.code
cst = Vector{Int}(undef, length(code))
sptypes = Core.Compiler.sptypes_from_meth_instance(mi)
params = Core.Compiler.OptimizationParams(Core.Compiler.NativeInterpreter())
maxcost = Core.Compiler.statement_costs!(cst, code, src, Any[mi.sparam_vals...], false, params)
maxcost = Core.Compiler.statement_costs!(cst, code, src, sptypes, false, params)
nd = ndigits(maxcost)
_lineprinter = lineprinter(src)
function preprinter(io, linestart, idx)
Expand Down Expand Up @@ -195,8 +200,9 @@ function show_variables(io, src, slotnames)
slottypes = src.slottypes
for i = 1:length(slotnames)
print(io, " ", slotnames[i])
if isa(slottypes, Vector{Any})
InteractiveUtils.warntype_type_printer(io, slottypes[i], true)
if isa(slottypes, Lattices)
typ = (@static IS_OVERHAULED ? Core.Compiler.unwraptype : identity)(slottypes[i])
InteractiveUtils.warntype_type_printer(io, typ, true)
end
println(io)
end
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ end
@static if isdefined(Compiler, :is_stmt_inline)
function Compiler.inlining_policy(
interp::CthulhuInterpreter, @nospecialize(src), stmt_flag::UInt8,
mi::MethodInstance, argtypes::Vector{Any})
@assert isa(src, OptimizedSource) || isnothing(src)
mi::MethodInstance, argtypes::Lattices)
@assert isa(src, OptimizedSource) || isnothing(src) "`inlining_policy(::CthulhuInterpreter, ...)` got unexpected source: `$(typeof(src))`"
if isa(src, OptimizedSource)
if Compiler.is_stmt_inline(stmt_flag) || src.isinlineable
return src.ir
Expand All @@ -100,7 +100,7 @@ function Compiler.inlining_policy(
# the default inlining policy may try additional effor to find the source in a local cache
return Base.@invoke Compiler.inlining_policy(
interp::AbstractInterpreter, nothing, stmt_flag::UInt8,
mi::MethodInstance, argtypes::Vector{Any})
mi::MethodInstance, argtypes::Lattices)
end
return nothing
end
Expand Down
6 changes: 3 additions & 3 deletions src/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##

using Base.Meta
using .Compiler: widenconst, argextype, Const, MethodMatchInfo,
using .Compiler: widenconst, argextype, MethodMatchInfo,
UnionSplitApplyCallInfo, UnionSplitInfo, ConstCallInfo,
MethodResultPure, ApplyCallInfo

Expand All @@ -23,13 +23,13 @@ function transform(::Val{:CuFunction}, callsite, callexpr, CI, mi, slottypes; pa
sptypes = sptypes_from_meth_instance(mi)
tt = argextype(callexpr.args[4], CI, sptypes, slottypes)
ft = argextype(callexpr.args[3], CI, sptypes, slottypes)
isa(tt, Const) || return callsite
(@static IS_OVERHAULED ? isConst(tt) : isa(tt, Const)) || return callsite
return Callsite(callsite.id, CuCallInfo(callinfo(Tuple{widenconst(ft), tt.val.parameters...}, Nothing, params)), callsite.head)
end

function find_callsites(interp::CthulhuInterpreter, CI::Union{Core.CodeInfo, IRCode},
stmt_info::Union{Vector, Nothing}, mi::Core.MethodInstance,
slottypes::Vector{Any}, optimize::Bool=true;
slottypes::Lattices, optimize::Bool=true;
params=current_params())
sptypes = sptypes_from_meth_instance(mi)
callsites = Callsite[]
Expand Down

0 comments on commit 7599fa8

Please sign in to comment.