Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,63 @@ function InferredSource(state::InferenceState)
exct)
end

function cthulhu_finish(@specialize(finishfunc), state::InferenceState, interp::CthulhuInterpreter, cycleid::Int)
res = @invoke finishfunc(state::InferenceState, interp::AbstractInterpreter, cycleid::Int)
function CC.finishinfer!(state::InferenceState, interp::CthulhuInterpreter, cycleid::Int)
res = @invoke CC.finishinfer!(state::InferenceState, interp::AbstractInterpreter, cycleid::Int)
key = get_inference_key(state)
if key !== nothing
interp.unopt[key] = InferredSource(state)
end
return res
end

function create_cthulhu_source(result::InferenceResult, effects::Effects)
isa(result.src, OptimizationState) || return result.src
opt = result.src
function retrieve_ir(opt::OptimizationState)
@static if VERSION ≥ v"1.13-"
optresult = opt.optresult::CC.OptimizationResult
optresult.simplified || CC.simplify_ir!(optresult)
opt.src.inlining_cost = CC.compute_inlining_cost(opt.inlining.interp, result, optresult)
ir = CC.compact!(copy(optresult.ir))
return CC.compact!(copy(optresult.ir))
else
# get the (theoretically) same effect as the jl_compress_ir -> jl_uncompress_ir -> inflate_ir round-trip
ir = CC.compact!(CC.cfg_simplify!(CC.copy(opt.ir::IRCode)))
return CC.compact!(CC.cfg_simplify!(CC.copy(opt.ir::IRCode)))
end
end

@static if VERSION ≥ v"1.13-"

function CC.transform_result_for_local_cache(interp::CthulhuInterpreter, result::InferenceResult)
src = @invoke CC.transform_result_for_local_cache(interp::AbstractInterpreter, result::InferenceResult)
isa(src, OptimizationState) || return src
opt = src
ir = retrieve_ir(opt)
return OptimizedSource(ir, opt.src, opt.src.inlineable, result.ipo_effects)
end

function CC.transform_result_for_cache(interp::CthulhuInterpreter, result::InferenceResult, edges::Core.SimpleVector)
if isa(result.src, CodeInfo)
result.src.edges = edges
end
return OptimizedSource(ir, opt.src, opt.src.inlineable, effects)
result.src = CC.transform_result_for_local_cache(interp, result)
end

else

function create_cthulhu_source(result::InferenceResult)
isa(result.src, OptimizationState) || return result.src
opt = result.src
ir = retrieve_ir(opt)
return OptimizedSource(ir, opt.src, opt.src.inlineable, result.ipo_effects)
end

function set_cthulhu_source!(result::InferenceResult)
result.src = create_cthulhu_source(result, result.ipo_effects)
result.src = create_cthulhu_source(result)
end

CC.finishinfer!(state::InferenceState, interp::CthulhuInterpreter, cycleid::Int) = cthulhu_finish(CC.finishinfer!, state, interp, cycleid)
function CC.finish!(interp::CthulhuInterpreter, caller::InferenceState, validation_world::UInt, time_before::UInt64)
set_cthulhu_source!(caller.result)
return @invoke CC.finish!(interp::AbstractInterpreter, caller::InferenceState, validation_world::UInt, time_before::UInt64)
end

end # @static if

function CC.src_inlining_policy(interp::CthulhuInterpreter,
@nospecialize(src), @nospecialize(info::CCCallInfo), stmt_flag::UInt32)
if isa(src, OptimizedSource)
Expand Down
3 changes: 3 additions & 0 deletions test/cthulhu.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Cthulhu: CTHULHU_MODULE
const Cthulhu = CTHULHU_MODULE[]
const CC = Cthulhu.CC
Comment on lines +1 to +3

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's to ensure that we can do using Compiler and have tests switch to using the extension module and keep working.

2 changes: 0 additions & 2 deletions test/generate_irshow.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module generate_irshow

using Cthulhu

include("setup.jl")
include("irshowutils.jl")
include("IRShowSandbox.jl")
Expand Down
1 change: 0 additions & 1 deletion test/irutils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Core.IR
using Cthulhu: Cthulhu
using Base.Meta: isexpr
using InteractiveUtils: gen_call_with_extracted_types_and_kwargs

Expand Down
6 changes: 4 additions & 2 deletions test/setup.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Test, Cthulhu, InteractiveUtils
using Test, InteractiveUtils
if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
using ..VSCodeServer
end

# InteractiveUtils.@activate Compiler # use the Compiler.jl stdlib for the Base reflections too

include("cthulhu.jl")

function cthulhu_info(@nospecialize(f), @nospecialize(tt=());
optimize=true, interp=Cthulhu.CC.NativeInterpreter())
optimize=true, interp=CC.NativeInterpreter())
(interp, codeinst) = Cthulhu.mkinterp(f, tt; interp)
(; src, rt, exct, infos, slottypes, effects) =
Cthulhu.lookup(interp, codeinst, optimize; allow_no_src=true)
Expand Down
6 changes: 3 additions & 3 deletions test/test_AbstractInterpreter.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module test_AbstractInterpreter

using Test, Cthulhu
using Test
if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
using ..VSCodeServer
end

include("cthulhu.jl")

@doc """
@newinterp NewInterpreter [ephemeral_cache::Bool=false]

Expand Down Expand Up @@ -66,8 +68,6 @@ macro newinterp(InterpName, ephemeral_cache::Bool=false)
end
end

const CC = Cthulhu.CC

# `OverlayMethodTable`
# --------------------
using Base.Experimental: @MethodTable, @overlay
Expand Down
8 changes: 3 additions & 5 deletions test/test_Cthulhu.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module test_Cthulhu

using Test, Cthulhu, StaticArrays, Random
using Test, StaticArrays, Random
using Core: Const
const CC = Cthulhu.CTHULHU_MODULE[].CC

include("setup.jl")
include("irutils.jl")
Expand Down Expand Up @@ -774,14 +773,13 @@ end
@testset "ascend interface" begin
m = Module()
@eval m begin
using Cthulhu
struct FunnyMI end
struct HasName
name::Symbol
end
Cthulhu.method(::FunnyMI) = HasName(:funny)
$Cthulhu.method(::FunnyMI) = HasName(:funny)
funny(c::Char) = "haha"
Cthulhu.specTypes(::FunnyMI) = Tuple{typeof(funny),Char}
$Cthulhu.specTypes(::FunnyMI) = Tuple{typeof(funny),Char}
end

io = IOBuffer()
Expand Down
2 changes: 1 addition & 1 deletion test/test_codeview.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module test_codeview

using Cthulhu, Test, Revise
using Test, Revise

include("setup.jl")

Expand Down
3 changes: 2 additions & 1 deletion test/test_codeview_vscode.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module test_codeview_vscode

using Cthulhu, Test, Revise, REPL, ..VSCodeServer, TypedSyntax
using Test, Revise, REPL, ..VSCodeServer, TypedSyntax
import TypedSyntax: InlayHintKinds

include("cthulhu.jl")
include("test_vscode_example_functions.jl")

@testset "VSCode descend test" begin
Expand Down
2 changes: 1 addition & 1 deletion test/test_irshow.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tests_irshow

using Cthulhu, Test, DeepDiffs
using Test, DeepDiffs

include("setup.jl")
include("irshowutils.jl")
Expand Down
4 changes: 3 additions & 1 deletion test/test_terminal.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module test_terminal

using Test, REPL, Cthulhu, Revise
using Test, REPL, Revise
if isdefined(parentmodule(@__MODULE__), :VSCodeServer)
using ..VSCodeServer
end

include("cthulhu.jl")

if !isdefined(@__MODULE__, :fake_terminal)
@eval (@__MODULE__) begin
includet(@__MODULE__, normpath(@__DIR__, "FakeTerminals.jl")) # FIXME change to include
Expand Down