Skip to content

Commit 8681bb8

Browse files
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>
1 parent 885d4f4 commit 8681bb8

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
@@ -988,14 +988,16 @@ function optimize(interp::AbstractInterpreter, opt::OptimizationState, caller::I
988988
return nothing
989989
end
990990

991-
macro pass(name, expr)
991+
const ALL_PASS_NAMES = String[]
992+
macro pass(name::String, expr)
992993
optimize_until = esc(:optimize_until)
993994
stage = esc(:__stage__)
994-
macrocall = :(@timeit $(esc(name)) $(esc(expr)))
995+
macrocall = :(@timeit $name $(esc(expr)))
995996
macrocall.args[2] = __source__ # `@timeit` may want to use it
997+
push!(ALL_PASS_NAMES, name)
996998
quote
997999
$macrocall
998-
matchpass($optimize_until, ($stage += 1), $(esc(name))) && $(esc(:(@goto __done__)))
1000+
matchpass($optimize_until, ($stage += 1), $name) && $(esc(:(@goto __done__)))
9991001
end
10001002
end
10011003

@@ -1006,8 +1008,13 @@ matchpass(::Nothing, _, _) = false
10061008
function run_passes_ipo_safe(
10071009
ci::CodeInfo,
10081010
sv::OptimizationState,
1009-
optimize_until = nothing, # run all passes by default
1010-
)
1011+
optimize_until::Union{Nothing, Int, String} = nothing) # run all passes by default
1012+
if optimize_until isa String && !contains_is(ALL_PASS_NAMES, optimize_until)
1013+
error("invalid `optimize_until` argument, no such optimization pass")
1014+
elseif optimize_until isa Int && (optimize_until < 1 || optimize_until > length(ALL_PASS_NAMES))
1015+
error("invalid `optimize_until` argument, no such optimization pass")
1016+
end
1017+
10111018
__stage__ = 0 # used by @pass
10121019
# NOTE: The pass name MUST be unique for `optimize_until::String` to work
10131020
@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
@@ -1459,7 +1459,7 @@ function f_with_early_try_catch_exit()
14591459
result
14601460
end
14611461

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

Compiler/test/ssair.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,6 @@ let cl = Int32[32, 1, 1, 1000, 240, 230]
821821
cl2 = ccall(:jl_uncompress_codelocs, Any, (Any, Int), str, 2)
822822
@test cl == cl2
823823
end
824+
825+
@test_throws ErrorException Base.code_ircode(+, (Float64, Float64); optimize_until = "nonexisting pass name")
826+
@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
@@ -2563,7 +2563,7 @@ end
25632563
mktemp() do f, io
25642564
redirect_stdout(io) do
25652565
let io = IOBuffer()
2566-
for i = 1:10
2566+
for i = 1:length(Base.Compiler.ALL_PASS_NAMES)
25672567
# make sure we don't error on printing IRs at any optimization level
25682568
ir = only(Base.code_ircode(sin, (Float64,); optimize_until=i))[1]
25692569
@test try; show(io, ir); true; catch; false; end

0 commit comments

Comments
 (0)