Skip to content

Commit

Permalink
Update to changes to construct_ssa (#234)
Browse files Browse the repository at this point in the history
* =Update to changes to construct_ssa

* adjust to the latest JuliaLang/julia#master

Particularily the update for `CC.finish!` is essential.

---------

Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>
  • Loading branch information
oxinabox and aviatesk authored Nov 23, 2023
1 parent 5c980a0 commit 146ae3d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
10 changes: 8 additions & 2 deletions src/stage1/recurse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,16 @@ function optic_transform!(ci, mi, nargs, N)
# SSA conversion
meth = mi.def::Method
domtree = construct_domtree(ir.cfg.blocks)
stmts = VERSION < v"1.11.0-DEV.258" ? ir.stmts.inst : ir.stmts.stmt
stmts = @static VERSION < v"1.11.0-DEV.258" ? ir.stmts.inst : ir.stmts.stmt
defuse_insts = scan_slot_def_use(Int(meth.nargs), ci, stmts)
ci.ssavaluetypes = Any[Any for i = 1:ci.ssavaluetypes]
ir = construct_ssa!(ci, ir, domtree, defuse_insts, ci.slottypes, SimpleInferenceLattice.instance)
@static if VERSION > v"1.11.0-DEV.337"
interp = NativeInterpreter() # dummy interpreter (not used by `construct_ssa!`)
opt_state=OptimizationState(mi, ci, interp)
ir = construct_ssa!(ci, ir, opt_state, domtree, defuse_insts, SimpleInferenceLattice.instance)
else
ir = construct_ssa!(ci, ir, domtree, defuse_insts, ci.slottypes, SimpleInferenceLattice.instance)
end
ir = compact!(ir)

nfixedargs = Int(meth.nargs)
Expand Down
18 changes: 8 additions & 10 deletions src/stage2/abstractinterpret.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Core.Compiler: abstract_call_gf_by_type, abstract_call
using Core.Compiler: Const, isconstType, argtypes_to_type, tuple_tfunc, Const,
getfield_tfunc, _methods_by_ftype, VarTable, cache_lookup, nfields_tfunc,
import .CC: abstract_call_gf_by_type, abstract_call_opaque_closure
using .CC: Const, isconstType, argtypes_to_type, tuple_tfunc, Const,
getfield_tfunc, _methods_by_ftype, VarTable, nfields_tfunc,
ArgInfo, singleton_type, CallMeta, MethodMatchInfo, specialize_method,
PartialOpaque, UnionSplitApplyCallInfo, typeof_tfunc, apply_type_tfunc, instanceof_tfunc,
StmtInfo
using Core: PartialStruct
using Base.Meta

function Core.Compiler.abstract_call_gf_by_type(interp::ADInterpreter, @nospecialize(f),
function CC.abstract_call_gf_by_type(interp::ADInterpreter, @nospecialize(f),
arginfo::ArgInfo, si::StmtInfo, @nospecialize(atype), sv::InferenceState, max_methods::Int)
(;argtypes) = arginfo
if interp.backward
Expand All @@ -25,7 +25,7 @@ function Core.Compiler.abstract_call_gf_by_type(interp::ADInterpreter, @nospecia
mi = specialize_method(call.info.results.matches[1], preexisting=true)
ci = get(rinterp.unopt[rinterp.current_level], mi, nothing)
clos = AbstractCompClosure(rinterp.current_level, 1, call.info, ci.stmt_info)
clos = Core.PartialOpaque(Core.OpaqueClosure{<:Tuple, <:Any}, nothing, sv.linfo, clos)
clos = PartialOpaque(Core.OpaqueClosure{<:Tuple, <:Any}, nothing, sv.linfo, clos)
elseif isa(call.info, RRuleInfo)
if rinterp.current_level == 1
clos = getfield_tfunc(call.info.rrule_rt, Const(2))
Expand Down Expand Up @@ -550,7 +550,7 @@ function infer_prim_closure(interp::ADInterpreter, pc::PrimClosure, @nospecializ
error()
end

function Core.Compiler.abstract_call_opaque_closure(interp::ADInterpreter,
function CC.abstract_call_opaque_closure(interp::ADInterpreter,
closure::PartialOpaque, arginfo::ArgInfo, sv::InferenceState, check::Bool=true)

if isa(closure.source, AbstractCompClosure)
Expand All @@ -565,8 +565,6 @@ function Core.Compiler.abstract_call_opaque_closure(interp::ADInterpreter,
return infer_prim_closure(interp, closure.source, argtypes[2], sv)
end

rt = invoke(Core.Compiler.abstract_call_opaque_closure, Tuple{AbstractInterpreter, PartialOpaque, ArgInfo, InferenceState, Bool},
interp, closure, arginfo, sv, check)

return rt
return @invoke CC.abstract_call_opaque_closure(interp::AbstractInterpreter,
closure::PartialOpaque, arginfo::ArgInfo, sv::InferenceState, check::Bool)
end
22 changes: 7 additions & 15 deletions src/stage2/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ CC.get_inference_cache(ei::ADInterpreter) = get_inference_cache(ei.native_interp
CC.lock_mi_inference(ei::ADInterpreter, mi::MethodInstance) = nothing
CC.unlock_mi_inference(ei::ADInterpreter, mi::MethodInstance) = nothing

struct CodeInfoView
d::Dict{MethodInstance, Any}
end

function CC.code_cache(ei::ADInterpreter)
while ei.current_level > lastindex(ei.opt)
push!(ei.opt, Dict{MethodInstance, Any}())
Expand All @@ -273,16 +269,6 @@ end
CC.may_optimize(ei::ADInterpreter) = true
CC.may_compress(ei::ADInterpreter) = false
CC.may_discard_trees(ei::ADInterpreter) = false
function CC.get(view::CodeInfoView, mi::MethodInstance, default)
r = get(view.d, mi, nothing)
if r === nothing
return default
end
if isa(r, OptimizationState)
r = r.src
end
return r::CodeInfo
end

function CC.add_remark!(interp::ADInterpreter, sv::InferenceState, msg)
key = CC.any(sv.result.overridden_by_const) ? sv.result : sv.linfo
Expand Down Expand Up @@ -365,11 +351,17 @@ function CC.optimize(interp::ADInterpreter, opt::OptimizationState,
end
=#

function CC.finish!(interp::ADInterpreter, caller::InferenceResult)
function _finish!(caller::InferenceResult)
effects = caller.ipo_effects
caller.src = Cthulhu.create_cthulhu_source(caller.src, effects)
end

@static if VERSION v"1.11.0-DEV.737"
CC.finish!(::ADInterpreter, caller::InferenceState) = _finish!(caller.result)
else
CC.finish!(::ADInterpreter, caller::InferenceResult) = _finish!(caller)
end

function ir2codeinst(ir::IRCode, inst::CodeInstance, ci::CodeInfo)
CodeInstance(inst.def, inst.rettype, isdefined(inst, :rettype_const) ? inst.rettype_const : nothing,
Cthulhu.OptimizedSource(CC.copy(ir), ci, inst.inferred.isinlineable, CC.decode_effects(inst.purity_bits)),
Expand Down

0 comments on commit 146ae3d

Please sign in to comment.