Skip to content

Commit fedcb34

Browse files
committed
update to avi/typelattice, incoming type lattice overhaul
1 parent 1a7ab8b commit fedcb34

File tree

5 files changed

+73
-25
lines changed

5 files changed

+73
-25
lines changed

src/Cthulhu.jl

+50-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ using InteractiveUtils
77
using UUIDs
88
using REPL: REPL, AbstractTerminal
99

10-
using Core: MethodInstance
10+
import Core: MethodInstance, OpaqueClosure
1111
const Compiler = Core.Compiler
12-
import Core.Compiler: MethodMatch, LimitedAccuracy, ignorelimited, specialize_method
12+
import Core.Compiler: MethodMatch, specialize_method,
13+
widenconst, ignorelimited,
14+
LimitedAccuracy, Const, PartialStruct, InterConditional, PartialOpaque
1315
import Base: unwrapva, isvarargtype
16+
1417
const mapany = Base.mapany
1518

1619
# branch on https://github.com/JuliaLang/julia/pull/42125
@@ -21,6 +24,15 @@ else
2124
macro constprop(_, ex); esc(ex); end
2225
end
2326

27+
const IS_OVERHAULED = isdefined(Core.Compiler, :LatticeElement)
28+
@static if IS_OVERHAULED
29+
import Core.Compiler: ⊤, ⊥, LatticeElement, NativeType, LatticeElement, isConst,
30+
isLimitedAccuracy, Argtypes, SSAValueTypes
31+
else
32+
const Argtypes = Vector{Any}
33+
const SSAValueTypes = Vector{Any}
34+
end
35+
2436
Base.@kwdef mutable struct CthulhuConfig
2537
enable_highlighter::Bool = false
2638
highlighter::Cmd = `pygmentize -l`
@@ -167,15 +179,36 @@ const descend = descend_code_typed
167179

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

182+
@static if IS_OVERHAULED
183+
import Core.Compiler: ConditionalInfo
170184
function codeinst_rt(code::CodeInstance)
171185
rettype = code.rettype
172186
if isdefined(code, :rettype_const)
173187
rettype_const = code.rettype_const
174188
if isa(rettype_const, Vector{Any}) && !(Vector{Any} <: rettype)
175-
return Core.PartialStruct(rettype, rettype_const)
176-
elseif isa(rettype_const, Core.PartialOpaque) && rettype <: Core.OpaqueClosure
189+
return PartialStruct(rettype, rettype_const)
190+
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
177191
return rettype_const
178-
elseif isa(rettype_const, Core.InterConditional) && !(Core.InterConditional <: rettype)
192+
elseif isa(rettype_const, ConditionalInfo) && !(ConditionalInfo <: rettype)
193+
@assert rettype_const.inter
194+
return InterConditional(rettype_const.slot_id, rettype_const.vtype, rettype_const.elsetype), mi
195+
else
196+
return Const(rettype_const)
197+
end
198+
else
199+
return rettype
200+
end
201+
end
202+
else # @static if IS_OVERHAULED
203+
function codeinst_rt(code::CodeInstance)
204+
rettype = code.rettype
205+
if isdefined(code, :rettype_const)
206+
rettype_const = code.rettype_const
207+
if isa(rettype_const, Vector{Any}) && !(Vector{Any} <: rettype)
208+
return PartialStruct(rettype, rettype_const)
209+
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
210+
return rettype_const
211+
elseif isa(rettype_const, InterConditional) && !(InterConditional <: rettype)
179212
return rettype_const
180213
else
181214
return Const(rettype_const)
@@ -184,6 +217,7 @@ function codeinst_rt(code::CodeInstance)
184217
return rettype
185218
end
186219
end
220+
end # @static if IS_OVERHAULED
187221

188222
# `@constprop :aggressive` here in order to make sure the constant propagation of `allow_no_codeinf`
189223
@constprop :aggressive function lookup(interp::CthulhuInterpreter, mi::MethodInstance, optimize::Bool; allow_no_codeinf::Bool=false)
@@ -193,7 +227,11 @@ end
193227
infos = interp.unopt[mi].stmt_infos
194228
slottypes = codeinf.slottypes
195229
if isnothing(slottypes)
196-
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
230+
@static if IS_OVERHAULED
231+
slottypes = LatticeElement[ ⊤ for i = 1:length(codeinf.slotflags) ]
232+
else
233+
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
234+
end
197235
end
198236
else
199237
codeinst = interp.opt[mi]
@@ -209,7 +247,11 @@ end
209247
# But with coverage on, the empty function body isn't empty due to :code_coverage_effect expressions.
210248
codeinf = nothing
211249
infos = []
212-
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
250+
@static if IS_OVERHAULED
251+
slottypes = AbstractLatticce[NativeType(t) for t in Base.unwrap_unionall(mi.specTypes).parameters]
252+
else
253+
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
254+
end
213255
else
214256
Core.eval(Main, quote
215257
interp = $interp
@@ -219,7 +261,7 @@ end
219261
error("couldn't find the source; inspect `Main.interp` and `Main.mi`")
220262
end
221263
end
222-
(codeinf, rt, infos, slottypes::Vector{Any})
264+
return codeinf, rt, infos, slottypes::Argtypes
223265
end
224266

225267
##

src/callsite.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct MICallInfo <: CallInfo
77
mi::MethodInstance
88
rt
99
function MICallInfo(mi::MethodInstance, @nospecialize(rt))
10-
if isa(rt, LimitedAccuracy)
10+
if @static IS_OVERHAULED ? isLimitedAccuracy(rt) : isa(rt, LimitedAccuracy)
1111
return LimitedCallInfo(new(mi, ignorelimited(rt)))
1212
else
1313
return new(mi, rt)
@@ -36,9 +36,9 @@ struct UncachedCallInfo <: WrappedCallInfo
3636
end
3737

3838
struct PureCallInfo <: CallInfo
39-
argtypes::Vector{Any}
39+
argtypes::Argtypes
4040
rt
41-
PureCallInfo(argtypes::Vector{Any}, @nospecialize(rt)) =
41+
PureCallInfo(argtypes::Argtypes, @nospecialize(rt)) =
4242
new(argtypes, rt)
4343
end
4444
get_mi(::PureCallInfo) = nothing
@@ -239,7 +239,7 @@ end
239239
function show_callinfo(limiter, ci::Union{MultiCallInfo, FailedCallInfo, GeneratedCallInfo})
240240
types = (ci.sig::DataType).parameters
241241
ft, tt = types[1], types[2:end]
242-
f = Compiler.singleton_type(ft)
242+
f = Compiler.singleton_type((@static IS_OVERHAULED ? LatticeElement : identity)(ft))
243243
if f !== nothing
244244
name = "$f"
245245
elseif ft isa Union

src/codeview.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
119119
if isa(src, Core.CodeInfo)
120120
# we're working on pre-optimization state, need to ignore `LimitedAccuracy`
121121
src = copy(src)
122-
src.ssavaluetypes = Base.mapany(ignorelimited, src.ssavaluetypes::Vector{Any})
122+
@static if IS_OVERHAULED
123+
src.ssavaluetypes = SSAValueTypes([ignorelimited(t) for t in src.ssavaluetypes::SSAValueTypes])
124+
else
125+
src.ssavaluetypes = Base.mapany(ignorelimited, src.ssavaluetypes::SSAValueTypes)
126+
end
123127
src.rettype = ignorelimited(src.rettype)
124128

125129
if src.slotnames !== nothing
@@ -144,8 +148,9 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
144148
isa(mi, MethodInstance) || throw("`mi::MethodInstance` is required")
145149
code = src isa IRCode ? src.stmts.inst : src.code
146150
cst = Vector{Int}(undef, length(code))
151+
sptypes = Core.Compiler.sptypes_from_meth_instance(mi)
147152
params = Core.Compiler.OptimizationParams(Core.Compiler.NativeInterpreter())
148-
maxcost = Core.Compiler.statement_costs!(cst, code, src, Any[mi.sparam_vals...], false, params)
153+
maxcost = Core.Compiler.statement_costs!(cst, code, src, sptypes, false, params)
149154
nd = ndigits(maxcost)
150155
_lineprinter = lineprinter(src)
151156
function preprinter(io, linestart, idx)
@@ -195,8 +200,9 @@ function show_variables(io, src, slotnames)
195200
slottypes = src.slottypes
196201
for i = 1:length(slotnames)
197202
print(io, " ", slotnames[i])
198-
if isa(slottypes, Vector{Any})
199-
InteractiveUtils.warntype_type_printer(io, slottypes[i], true)
203+
if isa(slottypes, Argtypes)
204+
typ = (@static IS_OVERHAULED ? Core.Compiler.unwraptype : identity)(slottypes[i])
205+
InteractiveUtils.warntype_type_printer(io, typ, true)
200206
end
201207
println(io)
202208
end

src/interpreter.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ end
9292
@static if isdefined(Compiler, :is_stmt_inline)
9393
function Compiler.inlining_policy(
9494
interp::CthulhuInterpreter, @nospecialize(src), stmt_flag::UInt8,
95-
mi::MethodInstance, argtypes::Vector{Any})
96-
@assert isa(src, OptimizedSource) || isnothing(src)
95+
mi::MethodInstance, argtypes::Argtypes)
96+
@assert isa(src, OptimizedSource) || isnothing(src) "`inlining_policy(::CthulhuInterpreter, ...)` got unexpected source: `$(typeof(src))`"
9797
if isa(src, OptimizedSource)
9898
if Compiler.is_stmt_inline(stmt_flag) || src.isinlineable
9999
return src.ir
@@ -102,7 +102,7 @@ function Compiler.inlining_policy(
102102
# the default inlining policy may try additional effor to find the source in a local cache
103103
return Base.@invoke Compiler.inlining_policy(
104104
interp::AbstractInterpreter, nothing, stmt_flag::UInt8,
105-
mi::MethodInstance, argtypes::Vector{Any})
105+
mi::MethodInstance, argtypes::Argtypes)
106106
end
107107
return nothing
108108
end

src/reflection.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ function transform(::Val{:CuFunction}, callsite, callexpr, CI, mi, slottypes; pa
2323
sptypes = sptypes_from_meth_instance(mi)
2424
tt = argextype(callexpr.args[4], CI, sptypes, slottypes)
2525
ft = argextype(callexpr.args[3], CI, sptypes, slottypes)
26-
isa(tt, Const) || return callsite
26+
(@static IS_OVERHAULED ? isConst(tt) : isa(tt, Const)) || return callsite
2727
return Callsite(callsite.id, CuCallInfo(callinfo(Tuple{widenconst(ft), tt.val.parameters...}, Nothing, params)), callsite.head)
2828
end
2929

30-
const ArgTypes = Vector{Any}
31-
3230
function find_callsites(interp::CthulhuInterpreter, CI::Union{Core.CodeInfo, IRCode},
3331
stmt_info::Union{Vector, Nothing}, mi::Core.MethodInstance,
34-
slottypes::Vector{Any}, optimize::Bool=true;
32+
slottypes::Argtypes, optimize::Bool=true;
3533
params=current_params())
3634
sptypes = sptypes_from_meth_instance(mi)
3735
callsites = Callsite[]
@@ -49,7 +47,9 @@ function find_callsites(interp::CthulhuInterpreter, CI::Union{Core.CodeInfo, IRC
4947
if !optimize
5048
args = (ignorelhs(c)::Expr).args
5149
end
52-
argtypes = mapany(function (@nospecialize(arg),)
50+
argtypes = @static IS_OVERHAULED ?
51+
LatticeElement[argextype(arg, CI, sptypes, slottypes) for arg in args] :
52+
mapany(function (@nospecialize(arg),)
5353
t = argextype(arg, CI, sptypes, slottypes)
5454
return widenconst(ignorelimited(t))
5555
end, args)
@@ -104,7 +104,7 @@ function find_callsites(interp::CthulhuInterpreter, CI::Union{Core.CodeInfo, IRC
104104
return callsites
105105
end
106106

107-
function process_info(interp, @nospecialize(info), argtypes::ArgTypes, @nospecialize(rt), optimize::Bool)
107+
function process_info(interp, @nospecialize(info), argtypes::Argtypes, @nospecialize(rt), optimize::Bool)
108108
is_cached(@nospecialize(key)) = haskey(optimize ? interp.opt : interp.unopt, key)
109109
process_recursive(@nospecialize(newinfo)) = process_info(interp, newinfo, argtypes, rt, optimize)
110110

0 commit comments

Comments
 (0)