Skip to content

Commit 56f233f

Browse files
gbaraldiKristofferC
authored andcommitted
Add 0 predecessor to entry basic block and handle it in inlining (#58683)
(cherry picked from commit da00451)
1 parent af00075 commit 56f233f

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

base/compiler/ssair/inlining.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ function cfg_inline_item!(ir::IRCode, idx::Int, todo::InliningTodo, state::CFGIn
128128
block = block_for_inst(ir, idx)
129129
inline_into_block!(state, block)
130130

131-
if !isempty(inlinee_cfg.blocks[1].preds)
131+
if length(inlinee_cfg.blocks[1].preds) > 1
132132
need_split_before = true
133+
else
134+
@assert inlinee_cfg.blocks[1].preds[1] == 0
133135
end
134-
135136
last_block_idx = last(state.cfg.blocks[block].stmts)
136137
if false # TODO: ((idx+1) == last_block_idx && isa(ir[SSAValue(last_block_idx)], GotoNode))
137138
need_split = false
@@ -168,12 +169,18 @@ function cfg_inline_item!(ir::IRCode, idx::Int, todo::InliningTodo, state::CFGIn
168169
end
169170
new_block_range = (length(state.new_cfg_blocks)-length(inlinee_cfg.blocks)+1):length(state.new_cfg_blocks)
170171

171-
# Fixup the edges of the newely added blocks
172+
# Fixup the edges of the newly added blocks
172173
for (old_block, new_block) in enumerate(bb_rename_range)
173174
if old_block != 1 || need_split_before
174175
p = state.new_cfg_blocks[new_block].preds
175176
let bb_rename_range = bb_rename_range
176177
map!(p, p) do old_pred_block
178+
# the meaning of predecessor 0 depends on the block we encounter it:
179+
# - in the first block, it represents the function entry and so needs to be re-mapped
180+
if old_block == 1 && old_pred_block == 0
181+
return first(bb_rename_range) - 1
182+
end
183+
# - elsewhere, it represents external control-flow from a caught exception which is un-affected by inlining
177184
return old_pred_block == 0 ? 0 : bb_rename_range[old_pred_block]
178185
end
179186
end
@@ -188,10 +195,6 @@ function cfg_inline_item!(ir::IRCode, idx::Int, todo::InliningTodo, state::CFGIn
188195
end
189196
end
190197

191-
if need_split_before
192-
push!(state.new_cfg_blocks[first(bb_rename_range)].preds, first(bb_rename_range)-1)
193-
end
194-
195198
any_edges = false
196199
for (old_block, new_block) in enumerate(bb_rename_range)
197200
if (length(state.new_cfg_blocks[new_block].succs) == 0)
@@ -437,7 +440,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
437440
else
438441
bb_offset, post_bb_id = popfirst!(todo_bbs)
439442
# This implements the need_split_before flag above
440-
need_split_before = !isempty(item.ir.cfg.blocks[1].preds)
443+
need_split_before = length(item.ir.cfg.blocks[1].preds) > 1
441444
if need_split_before
442445
finish_current_bb!(compact, 0)
443446
end

base/compiler/ssair/ir.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ function compute_basic_blocks(stmts::Vector{Any})
106106
end
107107
# Compute successors/predecessors
108108
for (num, b) in enumerate(blocks)
109+
if b.stmts.start == 1
110+
push!(b.preds, 0) # the entry block has a virtual predecessor
111+
end
109112
terminator = stmts[last(b.stmts)]
110113
if isa(terminator, ReturnNode)
111114
# return never has any successors

test/compiler/ssair.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,23 @@ end
752752
end
753753
end
754754
end
755+
756+
#57153 check that the CFG has a #0 block predecessor and that we don't fail to compile code that observes that
757+
function _worker_task57153()
758+
while true
759+
r = let
760+
try
761+
if @noinline rand(Bool)
762+
return nothing
763+
end
764+
q, m
765+
finally
766+
missing
767+
end
768+
end
769+
r[1]::Bool
770+
end
771+
end
772+
let ir = Base.code_ircode(_worker_task57153, (), optimize_until="CC: COMPACT_2")[1].first
773+
@test findfirst(x->x==0, ir.cfg.blocks[1].preds) !== nothing
774+
end

0 commit comments

Comments
 (0)