Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Mar 23, 2024
1 parent 6820f4c commit 6764904
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
25 changes: 12 additions & 13 deletions src/codegen/reverse.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Codegen shared by both stage1 and stage2

function make_opaque_closure(interp, typ, name, meth_nargs::Int, isva, lno, cis, revs...)
function make_opaque_closure(interp, typ, name, meth_nargs::Int, isva, lno, ci, revs...)
if interp !== nothing
cis.inferred = true
# ci.inferred = true
rettype = Any # ci.rettype # TODO revisit
ocm = ccall(:jl_new_opaque_closure_from_code_info, Any, (Any, Any, Any, Any, Any, Cint, Any, Cint, Cint, Any),
typ, Union{}, cis.rettype, @__MODULE__, cis, lno.line, lno.file, meth_nargs, isva, ()).source
return Expr(:new_opaque_closure, typ, Union{}, Any,
ocm, revs...)
typ, Union{}, rettype, @__MODULE__, ci, lno.line, lno.file, meth_nargs, isva, ()).source
return Expr(:new_opaque_closure, typ, Union{}, Any, ocm, revs...)
else
oc_nargs = Int64(meth_nargs)
Expr(:new_opaque_closure, typ, Union{}, Any,
Expr(:opaque_closure_method, name, oc_nargs, isva, lno, cis), revs...)
Expr(:opaque_closure_method, name, oc_nargs, isva, lno, ci), revs...)
end
end

Expand Down Expand Up @@ -107,8 +107,8 @@ function diffract_ir!(ir, ci, meth, sparams::Core.SimpleVector, nargs::Int, N::I
opaque_ci.slotnames = [Symbol("#oc#"), ci.slotnames...]
opaque_ci.slotflags = UInt8[0, ci.slotflags...]
end
opaque_ci.linetable = Core.LineInfoNode[ci.linetable[1]]
opaque_ci.inferred = false
opaque_ci.debuginfo = ci.debuginfo
# opaque_ci.inferred = false
opaque_ci
end

Expand Down Expand Up @@ -393,12 +393,11 @@ function diffract_ir!(ir, ci, meth, sparams::Core.SimpleVector, nargs::Int, N::I
code = opaque_ci.code = expand_switch(code, bb_ranges, slot_map)
end

opaque_ci.codelocs = Int32[0 for i=1:length(code)]
opaque_ci.debuginfo = Core.DebugInfo(Core.Compiler.DebugInfoStream(nothing, opaque_ci.debuginfo, length(code)), length(code))
opaque_ci.ssavaluetypes = length(code)
opaque_ci.ssaflags = UInt8[0 for i=1:length(code)]
opaque_ci.ssaflags = SSAFlagType[zero(SSAFlagType) for i=1:length(code)]
end


for nc = 2:2:n_closures
fwds = Any[nothing for i = 1:length(ir.stmts)]

Expand Down Expand Up @@ -475,9 +474,9 @@ function diffract_ir!(ir, ci, meth, sparams::Core.SimpleVector, nargs::Int, N::I
end
end

opaque_ci.codelocs = Int32[0 for i=1:length(code)]
opaque_ci.codelocs = Core.DebugInfo(Core.Compiler.DebugInfoStream(nothing, opaque_ci.debuginfo, length(code)), length(code))
opaque_ci.ssavaluetypes = length(code)
opaque_ci.ssaflags = UInt8[0 for i=1:length(code)]
opaque_ci.ssaflags = SSAFlagType[zero(SSAFlagType) for i=1:length(code)]
end

# TODO: This is absolutely aweful, but the best we can do given the data structures we have
Expand Down
5 changes: 3 additions & 2 deletions src/stage1/hacks.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Updated copy of the same code in Base, but with bugs fixed
using Core.Compiler: count_added_node!, NewSSAValue, add_pending!,
StmtRange, BasicBlock
using Core.Compiler:
NewSSAValue, OldSSAValue, StmtRange, BasicBlock,
count_added_node!, add_pending!

# Re-named in https://github.com/JuliaLang/julia/pull/47051
const add! = Core.Compiler.add_inst!
Expand Down
28 changes: 19 additions & 9 deletions src/stage1/recurse.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Core.IR
using Core.Compiler:
BasicBlock, CallInfo, CFG, IRCode, IncrementalCompact, Instruction, NewInstruction,
NoCallInfo, OldSSAValue, StmtRange,
BasicBlock, CFG, IRCode, IncrementalCompact, Instruction, NewInstruction, NoCallInfo, StmtRange,
bbidxiter, cfg_delete_edge!, cfg_insert_edge!, compute_basic_blocks, complete,
construct_domtree, construct_ssa!, domsort_ssa!, finish, insert_node!,
insert_node_here!, non_dce_finish!, quoted, retrieve_code_info,
Expand Down Expand Up @@ -255,13 +254,15 @@ function sptypes(sparams)
VarState[Core.Compiler.VarState.(sparams, false)...]
end

function optic_transform(ci, args...)
function optic_transform(ci::CodeInfo, args...)
newci = copy(ci)
optic_transform!(newci, args...)
return newci
end

function optic_transform!(ci, mi, nargs, N)
const SSAFlagType = @static VERSION v"1.11.0-DEV.377" ? UInt32 : UInt8

function optic_transform!(ci::CodeInfo, mi::MethodInstance, nargs::Int, N::Int)
code = ci.code
sparams = mi.sparam_vals

Expand All @@ -270,11 +271,20 @@ function optic_transform!(ci, mi, nargs, N)
ci.slotflags = UInt8[0x00, 0x00, ci.slotflags...]
ci.slottypes = ci.slottypes === nothing ? Any[Any for _ in 1:length(ci.slotflags)] : Any[Any, Any, ci.slottypes...]

type = Any[]
info = CallInfo[NoCallInfo() for i = 1:length(code)]
flag = SSAFlagType[zero(SSAFlagType) for i = 1:length(code)]
argtypes = Any[Any for i = 1:2]
meta = Expr[]
ir = IRCode(Core.Compiler.InstructionStream(code, Any[],
CallInfo[NoCallInfo() for i = 1:length(code)],
ci.codelocs, UInt8[0 for i = 1:length(code)]), cfg, Core.LineInfoNode[ci.linetable...],
Any[Any for i = 1:2], meta, sptypes(sparams))
@static if VERSION v"1.12.0-DEV.173"
debuginfo = Core.Compiler.DebugInfoStream(mi, ci.debuginfo, length(code))
stmts = Core.Compiler.InstructionStream(code, type, info, debuginfo.codelocs, flag)
ir = IRCode(stmts, cfg, debuginfo, argtypes, meta, sptypes(sparams))
else
linetable = Core.LineInfoNode[ci.linetable...]
stmts = Core.Compiler.InstructionStream(code, type, info, ci.codelocs, flag)
ir = IRCode(stmts, cfg, debuginfo, argtypes, meta, sptypes(sparams))
end

# SSA conversion
meth = mi.def::Method
Expand All @@ -300,7 +310,7 @@ function optic_transform!(ci, mi, nargs, N)
Core.Compiler.replace_code_newstyle!(ci, ir)

ci.ssavaluetypes = length(ci.code)
ci.ssaflags = UInt8[0x00 for i=1:length(ci.code)]
ci.ssaflags = SSAFlagType[zero(SSAFlagType) for i=1:length(ci.code)]
ci.method_for_inference_limit_heuristics = meth
ci.edges = MethodInstance[mi]

Expand Down

0 comments on commit 6764904

Please sign in to comment.