Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/passes/cps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,29 @@ function _functional(ir, tree, vars = [], cs = captures(ir, tree))
return finish(pr)
end

# A chain of conditional branches is a nested if/else: move all but the first
# branch of blocks with more than two into a fresh block.
function splitbranches!(ir)
i = 1
while i <= length(blocks(ir))
bl = block(ir, i)
if length(branches(bl)) > 2
new = block!(ir, i+1)
brs = copy(branches(bl))
empty!(branches(bl))
push!(branches(bl), brs[1])
branch!(bl, new)
append!(branches(new), brs[2:end])
end
i += 1
end
return ir
end

# Unreachable blocks (e.g. left behind by a `break`) can branch anywhere, which
# can't be expressed by nesting block lambdas along the dominator tree, and they
# skew the dominators of the blocks they branch to. So drop them first.
function functional(ir)
ir = trimblocks!(explicitbranch!(copy(ir)))
ir = trimblocks!(splitbranches!(explicitbranch!(copy(ir))))
_functional(ir, domtree(ir))
end
32 changes: 32 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,38 @@ end
isa Pair{Core.CodeInfo,DataType})
end

# Blocks with more than two branches (#100).
@testset "multibranch" begin
f100(x, y) = x && y && error()
g100(a, b, c, d) = a && b && c && d && error()
function h100(n, a, b)
s = 0
for i = 1:n
a && b && i > 1 && (s += i)
end
return s
end
@test_nowarn functional(IR(typeof(f100), Bool, Bool))
@test_nowarn functional(IR(typeof(g100), Bool, Bool, Bool, Bool))
for x in (false, true), y in (false, true)
if x && y
@test_throws ErrorException anf(f100, x, y)
else
@test anf(f100, x, y) == f100(x, y)
end
end
for a in (false, true), b in (false, true), c in (false, true), d in (false, true)
if a && b && c && d
@test_throws ErrorException anf(g100, a, b, c, d)
else
@test anf(g100, a, b, c, d) == g100(a, b, c, d)
end
end
for a in (false, true), b in (false, true), n in (0, 1, 4)
@test anf(h100, n, a, b) == h100(n, a, b)
end
end

function f_try_catch(x)
y = 0.
try
Expand Down
Loading