Skip to content

Commit 7599fa8

Browse files
committed
update to avi/typelattice, incoming type lattice overhaul
1 parent af1a2fc commit 7599fa8

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

src/Cthulhu.jl

+47-8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ 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-
using Core.Compiler: MethodMatch, LimitedAccuracy, ignorelimited
12+
import Core.Compiler: MethodMatch, LimitedAccuracy, widenconst, ignorelimited, Const,
13+
PartialStruct, InterConditional, PartialOpaque
1314

1415
const mapany = Base.mapany
1516

@@ -21,6 +22,15 @@ else
2122
macro constprop(_, ex); esc(ex); end
2223
end
2324

25+
const IS_OVERHAULED = isdefined(Core.Compiler, :TypeLattice)
26+
@static if IS_OVERHAULED
27+
import Core.Compiler: ⊤, ⊥, TypeLattice, NativeType, TypeLattice, isConst,
28+
isLimitedAccuracy, Lattices, SSAValueTypes
29+
else
30+
const Lattices = Vector{Any}
31+
const SSAValueTypes = Vector{Any}
32+
end
33+
2434
Base.@kwdef mutable struct CthulhuConfig
2535
enable_highlighter::Bool = false
2636
highlighter::Cmd = `pygmentize -l`
@@ -167,15 +177,35 @@ const descend = descend_code_typed
167177

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

180+
@static if IS_OVERHAULED
181+
import Core.Compiler: InterConditionalInfo
182+
function codeinst_rt(code::CodeInstance)
183+
rettype = code.rettype
184+
if isdefined(code, :rettype_const)
185+
rettype_const = code.rettype_const
186+
if isa(rettype_const, Vector{Any}) && !(Vector{Any} <: rettype)
187+
return PartialStruct(rettype, rettype_const)
188+
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
189+
return rettype_const
190+
elseif isa(rettype_const, InterConditionalInfo) && !(InterConditionalInfo <: rettype)
191+
return InterConditional(rettype_const.slot, rettype_const.vtype, rettype_const.elsetype)
192+
else
193+
return Const(rettype_const)
194+
end
195+
else
196+
return rettype
197+
end
198+
end
199+
else # @static if IS_OVERHAULED
170200
function codeinst_rt(code::CodeInstance)
171201
rettype = code.rettype
172202
if isdefined(code, :rettype_const)
173203
rettype_const = code.rettype_const
174204
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
205+
return PartialStruct(rettype, rettype_const)
206+
elseif isa(rettype_const, PartialOpaque) && rettype <: OpaqueClosure
177207
return rettype_const
178-
elseif isa(rettype_const, Core.InterConditional) && !(Core.InterConditional <: rettype)
208+
elseif isa(rettype_const, InterConditional) && !(InterConditional <: rettype)
179209
return rettype_const
180210
else
181211
return Const(rettype_const)
@@ -184,6 +214,7 @@ function codeinst_rt(code::CodeInstance)
184214
return rettype
185215
end
186216
end
217+
end # @static if IS_OVERHAULED
187218

188219
# `@constprop :aggressive` here in order to make sure the constant propagation of `allow_no_codeinf`
189220
@constprop :aggressive function lookup(interp::CthulhuInterpreter, mi::MethodInstance, optimize::Bool; allow_no_codeinf::Bool=false)
@@ -193,7 +224,11 @@ end
193224
infos = interp.unopt[mi].stmt_infos
194225
slottypes = codeinf.slottypes
195226
if isnothing(slottypes)
196-
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
227+
@static if IS_OVERHAULED
228+
slottypes = TypeLattice[ ⊤ for i = 1:length(codeinf.slotflags) ]
229+
else
230+
slottypes = Any[ Any for i = 1:length(codeinf.slotflags) ]
231+
end
197232
end
198233
else
199234
codeinst = interp.opt[mi]
@@ -209,7 +244,11 @@ end
209244
# But with coverage on, the empty function body isn't empty due to :code_coverage_effect expressions.
210245
codeinf = nothing
211246
infos = []
212-
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
247+
@static if IS_OVERHAULED
248+
slottypes = AbstractLatticce[NativeType(t) for t in Base.unwrap_unionall(mi.specTypes).parameters]
249+
else
250+
slottypes = Any[Base.unwrap_unionall(mi.specTypes).parameters...]
251+
end
213252
else
214253
Core.eval(Main, quote
215254
interp = $interp
@@ -219,7 +258,7 @@ end
219258
error("couldn't find the source; inspect `Main.interp` and `Main.mi`")
220259
end
221260
end
222-
(codeinf, rt, infos, slottypes::Vector{Any})
261+
return codeinf, rt, infos, slottypes::Lattices
223262
end
224263

225264
##

src/callsite.jl

+3-3
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::Lattices
4040
rt
41-
PureCallInfo(argtypes::Vector{Any}, @nospecialize(rt)) =
41+
PureCallInfo(argtypes::Lattices, @nospecialize(rt)) =
4242
new(argtypes, rt)
4343
end
4444
get_mi(::PureCallInfo) = nothing

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, Lattices)
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
@@ -90,8 +90,8 @@ end
9090
@static if isdefined(Compiler, :is_stmt_inline)
9191
function Compiler.inlining_policy(
9292
interp::CthulhuInterpreter, @nospecialize(src), stmt_flag::UInt8,
93-
mi::MethodInstance, argtypes::Vector{Any})
94-
@assert isa(src, OptimizedSource) || isnothing(src)
93+
mi::MethodInstance, argtypes::Lattices)
94+
@assert isa(src, OptimizedSource) || isnothing(src) "`inlining_policy(::CthulhuInterpreter, ...)` got unexpected source: `$(typeof(src))`"
9595
if isa(src, OptimizedSource)
9696
if Compiler.is_stmt_inline(stmt_flag) || src.isinlineable
9797
return src.ir
@@ -100,7 +100,7 @@ function Compiler.inlining_policy(
100100
# the default inlining policy may try additional effor to find the source in a local cache
101101
return Base.@invoke Compiler.inlining_policy(
102102
interp::AbstractInterpreter, nothing, stmt_flag::UInt8,
103-
mi::MethodInstance, argtypes::Vector{Any})
103+
mi::MethodInstance, argtypes::Lattices)
104104
end
105105
return nothing
106106
end

src/reflection.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
##
44

55
using Base.Meta
6-
using .Compiler: widenconst, argextype, Const, MethodMatchInfo,
6+
using .Compiler: widenconst, argextype, MethodMatchInfo,
77
UnionSplitApplyCallInfo, UnionSplitInfo, ConstCallInfo,
88
MethodResultPure, ApplyCallInfo
99

@@ -23,13 +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

3030
function find_callsites(interp::CthulhuInterpreter, CI::Union{Core.CodeInfo, IRCode},
3131
stmt_info::Union{Vector, Nothing}, mi::Core.MethodInstance,
32-
slottypes::Vector{Any}, optimize::Bool=true;
32+
slottypes::Lattices, optimize::Bool=true;
3333
params=current_params())
3434
sptypes = sptypes_from_meth_instance(mi)
3535
callsites = Callsite[]

0 commit comments

Comments
 (0)