Skip to content

Commit 3fcc2ef

Browse files
authored
IR: compress consecutive pop_loc nodes (#24109)
1 parent 4e1d791 commit 3fcc2ef

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

base/inference.jl

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5625,30 +5625,62 @@ function meta_elim_pass!(code::Array{Any,1}, do_coverage::Bool)
56255625
push!(prev_dbg_stack, 0)
56265626
push!(push_loc_pos_stack, i)
56275627
elseif arg1 === :pop_loc
5628-
prev_dbg = if length(prev_dbg_stack) > 1
5629-
pop!(prev_dbg_stack)
5630-
else
5631-
prev_dbg_stack[end]
5632-
end
5633-
if prev_dbg > 0
5634-
code[prev_dbg] = nothing
5628+
npops = (nargs > 1 ? args[2]::Int : 1)
5629+
for pop in 1:npops
5630+
prev_dbg = if length(prev_dbg_stack) > 1
5631+
pop!(prev_dbg_stack)
5632+
else
5633+
prev_dbg_stack[end]
5634+
end
5635+
if prev_dbg > 0
5636+
code[prev_dbg] = nothing
5637+
end
5638+
push_loc = if length(push_loc_pos_stack) > 1
5639+
pop!(push_loc_pos_stack)
5640+
else
5641+
push_loc_pos_stack[end]
5642+
end
5643+
if push_loc > 0
5644+
code[push_loc] = nothing
5645+
npops -= 1
5646+
else
5647+
prev_dbg_stack[end] = 0
5648+
push_loc_pos_stack[end] = 0
5649+
end
56355650
end
5636-
push_loc = if length(push_loc_pos_stack) > 1
5637-
pop!(push_loc_pos_stack)
5651+
if npops > 1
5652+
code[i] = Expr(:meta, :pop_loc, npops)
5653+
elseif npops == 1
5654+
code[i] = Expr(:meta, :pop_loc)
56385655
else
5639-
push_loc_pos_stack[end]
5640-
end
5641-
if push_loc > 0
5642-
code[push_loc] = nothing
56435656
code[i] = nothing
5644-
else
5645-
prev_dbg_stack[end] = 0
5646-
push_loc_pos_stack[end] = 0
56475657
end
56485658
else
56495659
continue
56505660
end
56515661
end
5662+
5663+
# combine consecutive :pop_loc instructions
5664+
lastpop = nothing
5665+
npops = 0
5666+
for i in 1:length(code)
5667+
ex = code[i]
5668+
if isa(ex, Expr) && ex.head === :meta && length(ex.args) > 0 && ex.args[1] == :pop_loc
5669+
npops += (length(ex.args) > 1 ? ex.args[2]::Int : 1)
5670+
if lastpop === nothing
5671+
lastpop = ex
5672+
else
5673+
code[i] = nothing
5674+
end
5675+
elseif ex !== nothing && lastpop !== nothing
5676+
if npops > 1
5677+
resize!(lastpop.args, 2)
5678+
lastpop.args[2] = npops
5679+
end
5680+
lastpop = nothing
5681+
npops = 0
5682+
end
5683+
end
56525684
end
56535685

56545686
# does the same job as alloc_elim_pass for allocations inline in getfields

base/show.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,9 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
12021202
elseif head === :meta && length(args) == 1 && args[1] === :pop_loc
12031203
print(io, "# meta: pop location")
12041204
show_type = false
1205+
elseif head === :meta && length(args) == 2 && args[1] === :pop_loc
1206+
print(io, "# meta: pop locations ($(args[2]))")
1207+
show_type = false
12051208
# print anything else as "Expr(head, args...)"
12061209
else
12071210
if head !== :invoke

doc/src/devdocs/ast.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ These symbols appear in the `head` field of `Expr`s in lowered form.
189189

190190
* `:pop_loc`: returns to the source location before the matching `:push_loc`.
191191

192+
* `args[2]::Int` (optional) specifies the number of `push_loc` to pop
193+
192194

193195
### Method
194196

src/codegen.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5503,15 +5503,20 @@ static std::unique_ptr<Module> emit_function(
55035503
cur_prop.loc_changed = true;
55045504
}
55055505
else if (meta_arg == (jl_value_t*)jl_symbol("pop_loc")) {
5506+
unsigned npops = 1;
5507+
if (jl_expr_nargs(expr) > 1)
5508+
npops = jl_unbox_long(jl_exprarg(expr, 1));
5509+
for (unsigned i = 1; i < npops; i++)
5510+
DI_stack.pop_back();
55065511
cur_prop.is_poploc = true;
55075512
auto &DI = DI_stack.back();
55085513
SP = DI.sp;
55095514
cur_prop.loc = DI.loc;
55105515
cur_prop.file = DI.file;
55115516
cur_prop.line = DI.line;
55125517
cur_prop.in_user_code = DI.in_user_code;
5513-
DI_stack.pop_back();
55145518
cur_prop.loc_changed = true;
5519+
DI_stack.pop_back();
55155520
}
55165521
}
55175522
stmtprops[i] = cur_prop;

0 commit comments

Comments
 (0)