Skip to content

Commit

Permalink
improve constraint propagation with multiple ||
Browse files Browse the repository at this point in the history
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
  • Loading branch information
simeonschaub committed Feb 11, 2021
1 parent 4f854b4 commit 9dcdc87
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1926,13 +1926,24 @@
(blk? (and (pair? test) (eq? (car test) 'block)))
(stmts (if blk? (cdr (butlast test)) '()))
(test (if blk? (last test) test)))
(if (and (pair? test) (eq? (car test) '&&))
(let ((clauses `(&& ,@(map expand-forms (cdr (flatten-ex '&& test))))))
`(if ,(if blk?
`(block ,@(map expand-forms stmts) ,clauses)
clauses)
,@(map expand-forms (cddr e))))
(cons (car e) (map expand-forms (cdr e))))))
(cond ((and (pair? test) (eq? (car test) '&&))
(let ((clauses `(&& ,@(map expand-forms (cdr (flatten-ex '&& test))))))
`(if ,(if blk?
`(block ,@(map expand-forms stmts) ,clauses)
clauses)
,@(map expand-forms (cddr e)))))
((and (pair? test) (eq? (car test) '|\|\||))
(let ((clauses (cdr (flatten-ex '|\|\|| test)))
(label (gensym)))
(define (nest-if conds)
(if (length= conds 1)
`(if ,(car conds) (block (symboliclabel ,label) ,(caddr e)) ,@(cdddr e))
`(if ,(car conds) (symbolicgoto ,label) ,(nest-if (cdr conds)))))
(expand-forms
(if blk?
(nest-if `((block ,@stmts ,(car clauses)) ,@(cdr clauses)))
(nest-if clauses)))))
(else (cons (car e) (map expand-forms (cdr e)))))))

(define (expand-vcat e
(vcat '((top vcat)))
Expand Down
9 changes: 9 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3020,3 +3020,12 @@ end
# Bare Core.Argument in IR
@eval f_bare_argument(x) = $(Core.Argument(2))
@test Base.return_types(f_bare_argument, (Int,))[1] == Int

# issue #39611

Base.return_types((Union{Int,Nothing},)) do x
if x === nothing || x < 0
return 0
end
x
end == [Int]

0 comments on commit 9dcdc87

Please sign in to comment.