Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type constraints from multiple || conditions don't propagate well #39611

Closed
aviatesk opened this issue Feb 11, 2021 · 1 comment · Fixed by #39618
Closed

type constraints from multiple || conditions don't propagate well #39611

aviatesk opened this issue Feb 11, 2021 · 1 comment · Fixed by #39618
Labels
compiler:inference Type inference

Comments

@aviatesk
Copy link
Member

aviatesk commented Feb 11, 2021

Originally posted at https://github.com/JuliaLang/julia/pull/39549/files#r571434978

Constraints don't propagate well in the following case:

julia> Base.return_types((Union{Int,Nothing},)) do x
           if x === nothing || x < 0
               return 0
           end
           x # x::Int, ideally
       end
1-element Vector{Any}:
 Union{Nothing, Int64}

Ideas to fix are quoted from @vtjnash 's response:

It is the lowering structure. For && it is lowered to a nested if block, but not so for ||. So for entry to #4, we have the tmerge for [x = Nothing, _3 = Conditional(:x, Nothing, Int)] and [x = Int, _3 = Bool]. Instead, lowering could fuse the goto #4 into the next line (jump-threading), and do goto #5 there to fix this. Or we should narrow the later _3 = Bool to the equivalent _3 = Conditional(x, Int, Int) before the corresponding tmerge. Either way, a good update to consider for later:

julia> code_typed((Union{Int,Nothing},), optimize=false) do x
           if x === nothing || x < 0
               return 0
           end
           x # x::Int, ideally
       end
1-element Vector{Any}:
 CodeInfo(
    @ REPL[6]:2 within `#13'
1 ─ %1 = (x === Main.nothing)::Bool
└──      goto #3 if not %1
2 ─      (@_3 = %1)::Bool
└──      goto #4
3 ─      (@_3 = x::Int64 < 0)::Bool
4 ┄      goto #6 if not @_3
    @ REPL[6]:3 within `#13'
5 ─      return 0
    @ REPL[6]:5 within `#13'
6 ─      return x
) => Union{Nothing, Int64}
@aviatesk aviatesk added the compiler:inference Type inference label Feb 11, 2021
simeonschaub added a commit that referenced this issue Feb 11, 2021
What this PR does is expand for example
```julia
if x === nothing || x < 0
    return 0
end
```
to
```julia
if x === nothing
    @goto l
else
    if x < 0
        @Label l
        0
    end
end
```
in lowering, which IIUC correctly should correspond to the first option @vtjnash proposed in https://github.com/JuliaLang/julia/pull/39549/files#r573995564. Are there any potential problems with emitting `goto`s here? Or is there a nicer way to fix this than in `expand-if`?
fixes #39611
simeonschaub added a commit that referenced this issue Feb 11, 2021
What this PR does is expand for example
```julia
if x === nothing || x < 0
    return 0
end
```
to
```julia
if x === nothing
    @goto l
else
    if x < 0
        @Label l
        0
    end
end
```
in lowering, which IIUC should correspond to the first option @vtjnash proposed in https://github.com/JuliaLang/julia/pull/39549/files#r573995564. Are there any potential problems with emitting `goto`s here? Or is there a nicer way to fix this than in `expand-if`?
fixes #39611
@vtjnash
Copy link
Member

vtjnash commented Feb 12, 2021

We still might want to do the inference improvement too, since that should handle the case where the user wrote it out rather verbosely (like below). It might however be odd that Conditional would get a better tmerge result than Const(true) however, so also perhaps not.

julia> function f(x)
         x = x[]
         y = x === nothing
         if y
           z = y
         else
           z = x < 0
         end
         if z
           return 0
         end
         return x
       end

aviatesk added a commit to aviatesk/julia that referenced this issue Feb 13, 2021
aviatesk added a commit to aviatesk/julia that referenced this issue Feb 15, 2021
aviatesk added a commit to aviatesk/julia that referenced this issue Feb 16, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this issue May 4, 2021
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this issue May 9, 2021
KristofferC pushed a commit that referenced this issue Jul 12, 2021
KristofferC pushed a commit that referenced this issue Aug 31, 2021
staticfloat pushed a commit that referenced this issue Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:inference Type inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants