Skip to content

Commit

Permalink
inference: stronger state update from branching
Browse files Browse the repository at this point in the history
This change addresses 
<JuliaLang#40832 (review)>
  • Loading branch information
aviatesk committed Jun 3, 2021
1 parent 8c66f95 commit ae85281
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 6 additions & 5 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1734,11 +1734,6 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
empty!(frame.pclimitations)
break
end
if !(isa(condt, Const) || isa(condt, Conditional)) && isa(condx, SlotNumber)
# if this non-`Conditional` object is a slot, we form and propagate
# the conditional constraint on it
condt = Conditional(condx, Const(true), Const(false))
end
condval = maybe_extract_const_bool(condt)
l = stmt.dest::Int
if !isempty(frame.pclimitations)
Expand All @@ -1760,6 +1755,12 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
changes_else = conditional_changes(changes_else, condt.elsetype, condt.var)
changes = conditional_changes(changes, condt.vtype, condt.var)
end
if isa(condx, SlotNumber)
tfalse = isa(condt, Conditional) ? Conditional(condt.var, Bottom, condt.elsetype) : Const(false)
ttrue = isa(condt, Conditional) ? Conditional(condt.var, condt.vtype, Bottom) : Const(true)
changes_else = add_state_change!(changes_else, condx, tfalse, true)
changes = add_state_change!(changes, condx, ttrue, true)
end
newstate_else = stupdate!(states[l], changes_else)
if newstate_else !== nothing
# add else branch to active IP list
Expand Down
14 changes: 13 additions & 1 deletion test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,8 @@ end
end == Any[Tuple{Int,Int}]
end

@testset "conditional constraint propagation from non-`Conditional` object" begin
@testset "state update on branching" begin
# refine condition type into constant boolean value on branching
@test Base.return_types((Bool,)) do b
if b
return !b ? nothing : 1 # ::Int
Expand All @@ -1842,13 +1843,24 @@ end
end
end == Any[Int]

# even when the original type isn't boolean type
@test Base.return_types((Any,)) do b
if b
return b # ::Bool
else
return nothing
end
end == Any[Union{Bool,Nothing}]

# even when the original type is `Conditional`
@test Base.return_types((Any,)) do a
b = isa(a, Int)
if b
return !b ? nothing : a # ::Int
else
return 0
end
end == Any[Int]
end

function f25579(g)
Expand Down

0 comments on commit ae85281

Please sign in to comment.