Skip to content

Commit 205c24f

Browse files
committed
Support PhiNodes in IR renumbering
1 parent 9c6a6e6 commit 205c24f

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

base/compiler/optimize.jl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,23 @@ function renumber_ir_elements!(body::Vector{Any}, changemap::Vector{Int})
591591
return renumber_ir_elements!(body, changemap, changemap)
592592
end
593593

594-
function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, labelchangemap::Vector{Int})
595-
for i = 2:length(labelchangemap)
596-
labelchangemap[i] += labelchangemap[i - 1]
594+
function cumsum_ssamap!(ssamap::Vector{Int})
595+
rel_change = 0
596+
for i = 1:length(ssamap)
597+
rel_change += ssamap[i]
598+
if ssamap[i] == -1
599+
# Keep a marker that this statement was deleted
600+
ssamap[i] = typemin(Int)
601+
else
602+
ssamap[i] = rel_change
603+
end
597604
end
605+
end
606+
607+
function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, labelchangemap::Vector{Int})
608+
cumsum_ssamap!(labelchangemap)
598609
if ssachangemap !== labelchangemap
599-
for i = 2:length(ssachangemap)
600-
ssachangemap[i] += ssachangemap[i - 1]
601-
end
610+
cumsum_ssamap!(ssachangemap)
602611
end
603612
if labelchangemap[end] == 0 && ssachangemap[end] == 0
604613
return
@@ -619,6 +628,24 @@ function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, lab
619628
end
620629
elseif isa(el, SSAValue)
621630
body[i] = SSAValue(el.id + ssachangemap[el.id])
631+
elseif isa(el, PhiNode)
632+
i = 1
633+
edges = el.edges
634+
values = el.values
635+
while i <= length(edges)
636+
was_deleted = ssachangemap[edges[i]] == typemin(Int)
637+
if was_deleted
638+
deleteat!(edges, i)
639+
deleteat!(values, i)
640+
else
641+
edges[i] += ssachangemap[edges[i]]
642+
val = values[i]
643+
if isa(val, SSAValue)
644+
values[i] = SSAValue(val.id + ssachangemap[val.id])
645+
end
646+
i += 1
647+
end
648+
end
622649
elseif isa(el, Expr)
623650
if el.head === :(=) && el.args[2] isa Expr
624651
el = el.args[2]::Expr

base/compiler/typeinfer.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,7 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
672672
deleteat!(src.codelocs, i)
673673
deleteat!(sv.stmt_info, i)
674674
nexpr -= 1
675-
if oldidx < length(changemap)
676-
changemap[oldidx + 1] = -1
677-
end
675+
changemap[oldidx] = -1
678676
continue
679677
else
680678
body[i] = Const(expr) # annotate that this statement actually is dead

src/codegen.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,9 +4060,13 @@ static void emit_vi_assignment_unboxed(jl_codectx_t &ctx, jl_varinfo_t &vi, Valu
40604060
static void emit_phinode_assign(jl_codectx_t &ctx, ssize_t idx, jl_value_t *r)
40614061
{
40624062
jl_value_t *ssavalue_types = (jl_value_t*)ctx.source->ssavaluetypes;
4063-
assert(jl_is_array(ssavalue_types));
4063+
jl_value_t *phiType = NULL;
4064+
if (jl_is_array(ssavalue_types)) {
4065+
phiType = jl_array_ptr_ref(ssavalue_types, idx);
4066+
} else {
4067+
phiType = (jl_value_t*)jl_any_type;
4068+
}
40644069
jl_array_t *edges = (jl_array_t*)jl_fieldref_noalloc(r, 0);
4065-
jl_value_t *phiType = jl_array_ptr_ref(ssavalue_types, idx);
40664070
BasicBlock *BB = ctx.builder.GetInsertBlock();
40674071
auto InsertPt = BB->getFirstInsertionPt();
40684072
if (phiType == jl_bottom_type) {

test/compiler/inference.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,4 +3346,8 @@ let
33463346
oc = @eval b->$(Expr(:new_opaque_closure, Tuple{Bool, Float64}, false, Any, Any,
33473347
Expr(:opaque_closure_method, nothing, 2, LineNumberNode(0, nothing), ci)))(b, 1.0)
33483348
@test Base.return_types(oc, Tuple{Bool}) == Any[Float64]
3349+
3350+
oc = @eval ()->$(Expr(:new_opaque_closure, Tuple{Bool, Float64}, false, Any, Any,
3351+
Expr(:opaque_closure_method, nothing, 2, LineNumberNode(0, nothing), ci)))(true, 1.0)
3352+
@test Base.return_types(oc, Tuple{}) == Any[Float64]
33493353
end

0 commit comments

Comments
 (0)