Skip to content

Commit

Permalink
Allow constprop to improve constant results to Bottom (JuliaLang#45146)
Browse files Browse the repository at this point in the history
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 JuliaLang#44660.
  • Loading branch information
Keno authored May 2, 2022
1 parent b9d8280 commit 3d787a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
8 changes: 7 additions & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,13 @@ function const_prop_entry_heuristic(interp::AbstractInterpreter, result::MethodC
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (limited accuracy)")
return false
else
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (unimprovable return type)")
if isa(rt, Const)
if result.edge_effects.nothrow !== ALWAYS_TRUE
# Could still be improved to Bottom (or at least could see the effects improved)
return true
end
end
add_remark!(interp, sv, "[constprop] Disabled by entry heuristic (unimprovable result)")
return false
end
end
Expand Down
25 changes: 19 additions & 6 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1136,14 +1136,14 @@ ambig_effect_test(a, b::Int) = 1
ambig_effect_test(a, b) = 1
global ambig_unknown_type_global=1
@noinline function conditionally_call_ambig(b::Bool, a)
if b
ambig_effect_test(a, ambig_unknown_type_global)
end
return 0
if b
ambig_effect_test(a, ambig_unknown_type_global)
end
return 0
end
function call_call_ambig(b::Bool)
conditionally_call_ambig(b, 1)
return 1
conditionally_call_ambig(b, 1)
return 1
end
@test !fully_eliminated(call_call_ambig, Tuple{Bool})

Expand Down Expand Up @@ -1246,3 +1246,16 @@ end
@test !fully_eliminated() do
getglobal(@__MODULE__, :my_defined_var, :foo)
end

# Test for deletion of value-dependent control flow that is apparent
# at inference time, but hard to delete later.
function maybe_error_int(x::Int)
if x > 2
Base.donotdelete(Base.inferencebarrier(x))
error()
end
return 1
end
@test fully_eliminated() do
return maybe_error_int(1)
end

0 comments on commit 3d787a7

Please sign in to comment.