Skip to content

Commit 649d22b

Browse files
KristofferCaviatesk
andcommitted
verify that optimize_until is a valid pass (#58033)
I've noticed a few places in tests where `optimize_until` is called with a non-existent pass name. This is an attempt to catch those places and hopefully prevent further possible regressions when someone renames a pass and forgets to update the tests that matches on that name. --------- Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com> (cherry picked from commit 8681bb8)
1 parent ea04e83 commit 649d22b

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

Compiler/src/optimize.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,14 +1004,16 @@ function optimize(interp::AbstractInterpreter, opt::OptimizationState, caller::I
10041004
return finish(interp, opt, ir, caller)
10051005
end
10061006

1007-
macro pass(name, expr)
1007+
const ALL_PASS_NAMES = String[]
1008+
macro pass(name::String, expr)
10081009
optimize_until = esc(:optimize_until)
10091010
stage = esc(:__stage__)
1010-
macrocall = :(@timeit $(esc(name)) $(esc(expr)))
1011+
macrocall = :(@timeit $name $(esc(expr)))
10111012
macrocall.args[2] = __source__ # `@timeit` may want to use it
1013+
push!(ALL_PASS_NAMES, name)
10121014
quote
10131015
$macrocall
1014-
matchpass($optimize_until, ($stage += 1), $(esc(name))) && $(esc(:(@goto __done__)))
1016+
matchpass($optimize_until, ($stage += 1), $name) && $(esc(:(@goto __done__)))
10151017
end
10161018
end
10171019

@@ -1022,8 +1024,13 @@ matchpass(::Nothing, _, _) = false
10221024
function run_passes_ipo_safe(
10231025
ci::CodeInfo,
10241026
sv::OptimizationState,
1025-
optimize_until = nothing, # run all passes by default
1026-
)
1027+
optimize_until::Union{Nothing, Int, String} = nothing) # run all passes by default
1028+
if optimize_until isa String && !contains_is(ALL_PASS_NAMES, optimize_until)
1029+
error("invalid `optimize_until` argument, no such optimization pass")
1030+
elseif optimize_until isa Int && (optimize_until < 1 || optimize_until > length(ALL_PASS_NAMES))
1031+
error("invalid `optimize_until` argument, no such optimization pass")
1032+
end
1033+
10271034
__stage__ = 0 # used by @pass
10281035
# NOTE: The pass name MUST be unique for `optimize_until::String` to work
10291036
@pass "convert" ir = convert_to_ircode(ci, sv)

Compiler/test/irpasses.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ function f_with_early_try_catch_exit()
14581458
result
14591459
end
14601460

1461-
let ir = first(only(Base.code_ircode(f_with_early_try_catch_exit, (); optimize_until="compact")))
1461+
let ir = first(only(Base.code_ircode(f_with_early_try_catch_exit, (); optimize_until="slot2reg")))
14621462
for i = 1:length(ir.stmts)
14631463
expr = ir.stmts[i][:stmt]
14641464
if isa(expr, PhiCNode)

Compiler/test/ssair.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,6 @@ let cl = Int32[32, 1, 1, 1000, 240, 230]
820820
cl2 = ccall(:jl_uncompress_codelocs, Any, (Any, Int), str, 2)
821821
@test cl == cl2
822822
end
823+
824+
@test_throws ErrorException Base.code_ircode(+, (Float64, Float64); optimize_until = "nonexisting pass name")
825+
@test_throws ErrorException Base.code_ircode(+, (Float64, Float64); optimize_until = typemax(Int))

test/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,7 @@ end
25332533
mktemp() do f, io
25342534
redirect_stdout(io) do
25352535
let io = IOBuffer()
2536-
for i = 1:10
2536+
for i = 1:length(Base.Compiler.ALL_PASS_NAMES)
25372537
# make sure we don't error on printing IRs at any optimization level
25382538
ir = only(Base.code_ircode(sin, (Float64,); optimize_until=i))[1]
25392539
@test try; show(io, ir); true; catch; false; end

0 commit comments

Comments
 (0)