Skip to content

Commit 02611c6

Browse files
committed
Allow constprop to improve constant results to Bottom
This is consistent with what we would do for a non-constant return. This helps the compiler dump less code onto LLVM in situations where there is a big basic block that our constprop can see is dead. That said, for that particular situation, it might be better to have a constant propagation pass at the IR level that runs after inlining. We can revisit that after #44660.
1 parent 6026b5f commit 02611c6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,9 @@ function const_prop_entry_heuristic(interp::AbstractInterpreter, result::MethodC
901901
# thus there won't be much benefit in constant-prop' here
902902
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (limited accuracy)")
903903
return false
904+
elseif result.edge_effects.nothrow !== ALWAYS_TRUE
905+
# Could be improved to Bottom (or at least could see the effects improved)
906+
return true
904907
else
905908
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (unimprovable return type)")
906909
return false

test/compiler/inline.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,14 +1136,14 @@ ambig_effect_test(a, b::Int) = 1
11361136
ambig_effect_test(a, b) = 1
11371137
global ambig_unknown_type_global=1
11381138
@noinline function conditionally_call_ambig(b::Bool, a)
1139-
if b
1140-
ambig_effect_test(a, ambig_unknown_type_global)
1141-
end
1142-
return 0
1139+
if b
1140+
ambig_effect_test(a, ambig_unknown_type_global)
1141+
end
1142+
return 0
11431143
end
11441144
function call_call_ambig(b::Bool)
1145-
conditionally_call_ambig(b, 1)
1146-
return 1
1145+
conditionally_call_ambig(b, 1)
1146+
return 1
11471147
end
11481148
@test !fully_eliminated(call_call_ambig, Tuple{Bool})
11491149

@@ -1246,3 +1246,17 @@ end
12461246
@test !fully_eliminated() do
12471247
getglobal(@__MODULE__, :my_defined_var, :foo)
12481248
end
1249+
1250+
# Test for deletion of value-dependent control flow that is apparent
1251+
# at inference time, but hard to delete later.
1252+
function maybe_error_int(x::Int)
1253+
if x > 2
1254+
Base.donotdelete(Base.inferencebarrier(x))
1255+
error()
1256+
end
1257+
return 1
1258+
end
1259+
function call_maybe_error_int()
1260+
return maybe_error(1)
1261+
end
1262+
@test fully_eliminated(maybe_error_int)

0 commit comments

Comments
 (0)