Skip to content

Commit c8e687f

Browse files
authored
use Compiler stdlib if available (#613)
This requires JuliaLang/julia#56553 to be merged, and the new Compiler.jl stdlib to be registered in General beforehand.
1 parent 3cd0baf commit c8e687f

File tree

11 files changed

+129
-90
lines changed

11 files changed

+129
-90
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- version: '1' # current stable
1717
os: ubuntu-latest
1818
arch: x64
19-
- version: '1.10.0' # lowerest version supported
19+
- version: '1.10' # lowest version supported
2020
os: ubuntu-latest
2121
arch: x64
2222
- version: '1.12-nightly' # next release

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name = "Cthulhu"
22
uuid = "f68482b8-f384-11e8-15f7-abe071a5a75f"
3+
version = "2.16.0"
34
authors = ["Valentin Churavy <v.churavy@gmail.com> and contributors"]
4-
version = "2.15.3"
55

66
[deps]
77
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
8+
Compiler = "807dbc54-b67e-4c79-8afb-eafe4df6f2e1"
89
FoldingTrees = "1eca21be-9b9b-4ed8-839a-6d8ae26b1781"
910
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1011
JuliaSyntax = "70703baa-626e-46a2-a12c-08ffd08c73b4"
@@ -18,6 +19,7 @@ WidthLimitedIO = "b8c1c048-cf81-46c6-9da0-18c1d99e41f2"
1819

1920
[compat]
2021
CodeTracking = "0.5, 1"
22+
Compiler = "0.0.2"
2123
FoldingTrees = "1"
2224
InteractiveUtils = "1.9"
2325
JuliaSyntax = "0.4"
@@ -28,7 +30,7 @@ TypedSyntax = "1.3.0"
2830
UUIDs = "1.9"
2931
Unicode = "1.9"
3032
WidthLimitedIO = "1"
31-
julia = "1.10.0"
33+
julia = "1.10"
3234

3335
[extras]
3436
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"

src/Cthulhu.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@ using TypedSyntax
1212
using WidthLimitedIO
1313

1414
using Core: MethodInstance, MethodMatch
15-
const CC = Core.Compiler
16-
using .CC: Effects, EFFECTS_TOTAL, LimitedAccuracy,
17-
compileable_specialization, ignorelimited, specialize_method
15+
@static if VERSION v"1.12.0-DEV.1581"
16+
using Compiler: Compiler as CC
17+
using Compiler.IRShow: IRShow
18+
else
19+
const CC = Core.Compiler
20+
const IRShow = Base.IRShow
21+
end
22+
using Core.IR
23+
using .CC: AbstractInterpreter, ApplyCallInfo, CallInfo as CCCallInfo, ConstCallInfo,
24+
EFFECTS_TOTAL, Effects, IncrementalCompact, InferenceParams, InferenceResult,
25+
InferenceState, IRCode, LimitedAccuracy, MethodMatchInfo, MethodResultPure,
26+
NativeInterpreter, NoCallInfo, OptimizationParams, OptimizationState,
27+
UnionSplitApplyCallInfo, UnionSplitInfo, WorldRange, WorldView,
28+
argextype, argtypes_to_type, compileable_specialization, ignorelimited, singleton_type,
29+
specialize_method, sptypes_from_meth_instance, widenconst
1830
using Base: @constprop, default_tt, isvarargtype, unwrapva, unwrap_unionall, rewrap_unionall
1931
const mapany = Base.mapany
2032

@@ -803,7 +815,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs
803815
@static if VERSION < v"1.12.0-DEV.669"
804816
view_cmd(iostream, mi, optimize, debuginfo, world, CONFIG)
805817
else
806-
src = Core.Compiler.typeinf_code(interp, mi, true)
818+
src = CC.typeinf_code(interp, mi, true)
807819
view_cmd(iostream, mi, src, optimize, debuginfo, world, CONFIG)
808820
end
809821
display_CI = false

src/codeview.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,15 @@ function cthulhu_ast(io::IO, mi, ::Bool, debuginfo, ::UInt, config::CthulhuConfi
100100
end
101101
end
102102

103-
using Base.IRShow: IRShow, _stmt, _type, should_print_ssa_type, IRShowConfig, show_ir
104-
105103
const __debuginfo = merge(IRShow.__debuginfo, Dict(
106-
:compact => src -> src isa CodeInfo ? __debuginfo[:source](src)
107-
: IRShow.inline_linfo_printer(src)
108-
))
104+
:compact => function (src)
105+
src isa CodeInfo ? IRShow.__debuginfo[:source](src) : IRShow.inline_linfo_printer(src)
106+
end))
109107

110108
function is_type_unstable(code::Union{IRCode, CodeInfo}, idx::Int, used::BitSet)
111-
stmt = _stmt(code, idx)
112-
type = _type(code, idx)
113-
should_print_ssa_type(stmt) || return false
109+
stmt = IRShow._stmt(code, idx)
110+
type = IRShow._type(code, idx)
111+
IRShow.should_print_ssa_type(stmt) || return false
114112
# `used` only contains used SSA values and ignores slots
115113
in_use = in(idx, used) || Meta.isexpr(stmt, :(=))
116114
return in_use && is_type_unstable(type)
@@ -308,7 +306,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
308306
should_print_stmt = hide_type_stable ? is_type_unstable : Returns(true)
309307
bb_color = (src isa IRCode && debuginfo === :compact) ? :normal : :light_black
310308

311-
irshow_config = IRShowConfig(preprinter, postprinter; should_print_stmt, bb_color)
309+
irshow_config = IRShow.IRShowConfig(preprinter, postprinter; should_print_stmt, bb_color)
312310

313311
if !inline_cost && iswarn
314312
print(lambda_io, "Body")
@@ -319,14 +317,14 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
319317
println(lambda_io)
320318
else
321319
isa(mi, MethodInstance) || throw("`mi::MethodInstance` is required")
322-
cfg = src isa IRCode ? src.cfg : Core.Compiler.compute_basic_blocks(src.code)
320+
cfg = src isa IRCode ? src.cfg : CC.compute_basic_blocks(src.code)
323321
max_bb_idx_size = length(string(length(cfg.blocks)))
324322
str = irshow_config.line_info_preprinter(lambda_io, " "^(max_bb_idx_size + 2), -1)
325323
callsite = Callsite(0, MICallInfo(mi, rettype, effects, exct), :invoke)
326324
println(lambda_io, "", ""^(max_bb_idx_size), str, " ", callsite)
327325
end
328326

329-
show_ir(lambda_io, src, irshow_config)
327+
IRShow.show_ir(lambda_io, src, irshow_config)
330328
return nothing
331329
end
332330

@@ -547,7 +545,7 @@ else
547545
raw = false,
548546
config = CONFIG,
549547
)
550-
src = Core.Compiler.typeinf_code(b.interp, b.mi, true)
548+
src = CC.typeinf_code(b.interp, b.mi, true)
551549
return cthulhu_llvm(
552550
io,
553551
b.mi,
@@ -570,7 +568,7 @@ else
570568
raw = false,
571569
config = CONFIG,
572570
)
573-
src = Core.Compiler.typeinf_code(b.interp, b.mi, true)
571+
src = CC.typeinf_code(b.interp, b.mi, true)
574572
return cthulhu_native(
575573
io,
576574
b.mi,

src/interpreter.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using .CC: AbstractInterpreter, CallInfo as CCCallInfo, CodeInfo, CodeInstance,
2-
InferenceParams, InferenceResult, InferenceState, IRCode, NativeInterpreter,
3-
NoCallInfo, OptimizationParams, OptimizationState, SSAValue, WorldRange, WorldView
4-
51
struct InferredSource
62
src::CodeInfo
73
stmt_info::Vector{CCCallInfo}

src/reflection.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
##
44

55
using Base.Meta
6-
import .CC: widenconst, argextype, Const, MethodMatchInfo,
7-
UnionSplitApplyCallInfo, UnionSplitInfo, ConstCallInfo,
8-
MethodResultPure, ApplyCallInfo,
9-
sptypes_from_meth_instance, argtypes_to_type
10-
import Base: may_invoke_generator
6+
using Base: may_invoke_generator
117

128
transform(::Val, callsite) = callsite
139
function transform(::Val{:CuFunction}, callsite, callexpr, CI, mi, slottypes; world=get_world_counter())
@@ -269,8 +265,8 @@ function is_call_expr(x::Expr, optimize::Bool)
269265
end
270266

271267
function dce!(ir::IRCode)
272-
ir = Core.Compiler.compact!(ir, #=allow_cfg_transform=#true)
273-
ir = Core.Compiler.compact!(ir, #=allow_cfg_transform=#true)
268+
ir = CC.compact!(ir, #=allow_cfg_transform=#true)
269+
ir = CC.compact!(ir, #=allow_cfg_transform=#true)
274270
return ir
275271
end
276272

@@ -355,14 +351,14 @@ end
355351

356352
function add_sourceline!(locs::Vector{Tuple{Core.LineInfoNode,Int}}, src::Union{CodeInfo,IRCode}, stmtidx::Int, caller::MethodInstance)
357353
@static if VERSION v"1.12.0-DEV.173"
358-
stack = Base.IRShow.buildLineInfoNode(src.debuginfo, caller, stmtidx)
354+
stack = IRShow.buildLineInfoNode(src.debuginfo, caller, stmtidx)
359355
for (i, di) in enumerate(stack)
360356
loc = Core.LineInfoNode(Main, di.method, di.file, di.line, zero(Int32))
361357
push!(locs, (loc, i-1))
362358
end
363359
else # VERSION < v"1.12.0-DEV.173"
364360
if isa(src, IRCode)
365-
stack = Base.IRShow.compute_loc_stack(src.linetable, src.stmts.line[stmtidx])
361+
stack = IRShow.compute_loc_stack(src.linetable, src.stmts.line[stmtidx])
366362
for (i, idx) in enumerate(stack)
367363
line = src.linetable[idx]
368364
line.line == 0 && continue

test/irutils.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Core: CodeInfo, ReturnNode, MethodInstance
2-
using Core.Compiler: IRCode, IncrementalCompact, singleton_type
1+
using Core.IR
2+
using Cthulhu: Cthulhu
33
using Base.Meta: isexpr
44
using InteractiveUtils: gen_call_with_extracted_types_and_kwargs
55

6-
argextype(@nospecialize args...) = Core.Compiler.argextype(args...)
7-
argextype(@nospecialize(x), src::CodeInfo) = argextype(x, src, Core.Compiler.VarState[])
6+
argextype(@nospecialize args...) = Cthulhu.CC.argextype(args...)
7+
argextype(@nospecialize(x), src::CodeInfo) = argextype(x, src, Cthulhu.CC.VarState[])
88
code_typed1(args...; kwargs...) = first(only(code_typed(args...; kwargs...)))::CodeInfo
99
macro code_typed1(ex0...)
1010
return gen_call_with_extracted_types_and_kwargs(__module__, :code_typed1, ex0)
@@ -20,9 +20,9 @@ isreturn(@nospecialize x) = isa(x, ReturnNode)
2020

2121
# check if `x` is a dynamic call of a given function
2222
iscall(y) = @nospecialize(x) -> iscall(y, x)
23-
function iscall((src, f)::Tuple{IR,Base.Callable}, @nospecialize(x)) where IR<:Union{CodeInfo,IRCode,IncrementalCompact}
23+
function iscall((src, f), @nospecialize(x))
2424
return iscall(x) do @nospecialize x
25-
singleton_type(argextype(x, src)) === f
25+
Cthulhu.CC.singleton_type(argextype(x, src)) === f
2626
end
2727
end
2828
function iscall(pred::Base.Callable, @nospecialize(x))

test/setup.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
33
using ..VSCodeServer
44
end
55

6+
@static if VERSION v"1.12.0-DEV.1581"
7+
InteractiveUtils.@activate Compiler # use the Compiler.jl stdlib for the Base reflections too
8+
end
9+
610
function cthulhu_info(@nospecialize(f), @nospecialize(tt=());
7-
optimize=true, interp=Core.Compiler.NativeInterpreter())
11+
optimize=true, interp=Cthulhu.CC.NativeInterpreter())
812
(interp, mi) = Cthulhu.mkinterp(f, tt; interp)
913
(; src, rt, exct, infos, slottypes, effects) =
1014
Cthulhu.lookup(interp, mi, optimize; allow_no_src=true)

test/test_AbstractInterpreter.jl

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,65 @@ if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
55
using ..VSCodeServer
66
end
77

8-
const CC = Core.Compiler
8+
@doc """
9+
@newinterp NewInterpreter [ephemeral_cache::Bool=false]
10+
11+
Defines new `NewInterpreter <: AbstractInterpreter` whose cache is separated
12+
from the native code cache, satisfying the minimum interface requirements.
913
14+
When the `ephemeral_cache=true` option is specified, `NewInterpreter` will hold
15+
`CodeInstance` in an ephemeral non-integrated cache, rather than in the integrated
16+
`Core.Compiler.InternalCodeCache`.
17+
Keep in mind that ephemeral cache lacks support for invalidation and doesn't persist across
18+
sessions. However it is an usual Julia object of the type `code_cache::IdDict{MethodInstance,CodeInstance}`,
19+
making it easier for debugging and inspecting the compiler behavior.
20+
"""
1021
@static if VERSION v"1.11.0-DEV.1552"
11-
macro newinterp(InterpName)
12-
InterpCacheName = QuoteNode(Symbol(string(InterpName, "Cache")))
22+
macro newinterp(InterpName, ephemeral_cache::Bool=false)
23+
cache_token = QuoteNode(gensym(string(InterpName, "CacheToken")))
24+
InterpCacheName = esc(Symbol(string(InterpName, "Cache")))
1325
InterpName = esc(InterpName)
1426
C = Core
15-
CC = Core.Compiler
27+
CC = Cthulhu.CC
1628
quote
29+
$(ephemeral_cache && quote
30+
struct $InterpCacheName
31+
dict::IdDict{$C.MethodInstance,$C.CodeInstance}
32+
end
33+
$InterpCacheName() = $InterpCacheName(IdDict{$C.MethodInstance,$C.CodeInstance}())
34+
end)
1735
struct $InterpName <: $CC.AbstractInterpreter
1836
meta # additional information
1937
world::UInt
2038
inf_params::$CC.InferenceParams
2139
opt_params::$CC.OptimizationParams
2240
inf_cache::Vector{$CC.InferenceResult}
41+
$(ephemeral_cache && :(code_cache::$InterpCacheName))
2342
function $InterpName(meta = nothing;
24-
world::UInt = Base.get_world_counter(),
25-
inf_params::$CC.InferenceParams = $CC.InferenceParams(),
26-
opt_params::$CC.OptimizationParams = $CC.OptimizationParams(),
27-
inf_cache::Vector{$CC.InferenceResult} = $CC.InferenceResult[])
28-
return new(meta, world, inf_params, opt_params, inf_cache)
43+
world::UInt = Base.get_world_counter(),
44+
inf_params::$CC.InferenceParams = $CC.InferenceParams(),
45+
opt_params::$CC.OptimizationParams = $CC.OptimizationParams(),
46+
inf_cache::Vector{$CC.InferenceResult} = $CC.InferenceResult[],
47+
$(ephemeral_cache ?
48+
Expr(:kw, :(code_cache::$InterpCacheName), :($InterpCacheName())) :
49+
Expr(:kw, :_, :nothing)))
50+
return $(ephemeral_cache ?
51+
:(new(meta, world, inf_params, opt_params, inf_cache, code_cache)) :
52+
:(new(meta, world, inf_params, opt_params, inf_cache)))
2953
end
3054
end
3155
$CC.InferenceParams(interp::$InterpName) = interp.inf_params
3256
$CC.OptimizationParams(interp::$InterpName) = interp.opt_params
3357
$CC.get_inference_world(interp::$InterpName) = interp.world
3458
$CC.get_inference_cache(interp::$InterpName) = interp.inf_cache
35-
$CC.cache_owner(::$InterpName) = $InterpCacheName
59+
$CC.cache_owner(::$InterpName) = $cache_token
60+
$(ephemeral_cache && quote
61+
$CC.code_cache(interp::$InterpName) = $CC.WorldView(interp.code_cache, $CC.WorldRange(interp.world))
62+
$CC.get(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance, default) = get(wvc.cache.dict, mi, default)
63+
$CC.getindex(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance) = getindex(wvc.cache.dict, mi)
64+
$CC.haskey(wvc::$CC.WorldView{$InterpCacheName}, mi::$C.MethodInstance) = haskey(wvc.cache.dict, mi)
65+
$CC.setindex!(wvc::$CC.WorldView{$InterpCacheName}, ci::$C.CodeInstance, mi::$C.MethodInstance) = setindex!(wvc.cache.dict, ci, mi)
66+
end)
3667
end
3768
end
3869
else
@@ -79,16 +110,11 @@ macro newinterp(InterpName)
79110
end
80111
end # if VERSION ≥ v"1.11.0-DEV.1552"
81112

82-
@doc """
83-
@newinterp NewInterpreter
84-
85-
Defines new `NewInterpreter <: AbstractInterpreter` whose cache is separated
86-
from the native code cache, satisfying the minimum interface requirements.
87-
""" var"@newinterp"
113+
const CC = Cthulhu.CC
88114

89115
# `OverlayMethodTable`
90116
# --------------------
91-
import Base.Experimental: @MethodTable, @overlay
117+
using Base.Experimental: @MethodTable, @overlay
92118

93119
@newinterp MTOverlayInterp
94120
@MethodTable OverlayedMT

0 commit comments

Comments
 (0)