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

Adjust to EnterNode change #118

Merged
merged 2 commits into from
Dec 13, 2023
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
3 changes: 3 additions & 0 deletions src/ir/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ end

function print_stmt(io::IO, ::Val{:enter}, ex)
print(io, "try #$(ex.args[1])")
if length(ex.args) >= 2
print(io, " with scope ", ex.args[2])
end
end

function print_stmt(io::IO, ::Val{:leave}, ex)
Expand Down
16 changes: 13 additions & 3 deletions src/ir/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function IRCode(ir::IR)
for (v, st) in b
defs[v] = Variable(length(stmts)+1)
ex = varmap(x -> get(defs, x, x), st.expr) |> unvars
@static if isdefined(Core.IR, :EnterNode)
ex = isexpr(ex, :enter) ? Core.EnterNode(ex.args...) : ex
end
push!(stmts, ex)
push!(types, st.type)
push!(lines, st.line)
Expand All @@ -43,7 +46,7 @@ function IRCode(ir::IR)
else
push!(stmts, Expr(:call, GlobalRef(Core, :throw), "unreachable"))
end
elseif br.condition == nothing
elseif br.condition === nothing
push!(stmts, GotoNode(br.block))
else
cond = get(defs, br.condition, br.condition) |> unvars
Expand Down Expand Up @@ -130,8 +133,15 @@ function IR(ci::CodeInfo, nargs::Integer; meta = nothing)
ex = ci.code[i]
if ex isa Core.NewvarNode
continue
elseif isexpr(ex, :enter)
_rename[Core.SSAValue(i)] = push!(ir, Expr(:enter, findfirst(==(ex.args[1]), bs)+1))
elseif @static if isdefined(Core.IR, :EnterNode); ex isa Core.IR.EnterNode else isexpr(ex, :enter) end
catch_dest = @static if isdefined(Core.IR, :EnterNode)
ex.catch_dest
else
ex.args[1]
end
enter_expr = Expr(:enter, findfirst(==(catch_dest), bs)+1)
isdefined(ex, :scope) && push!(enter_expr.args, ex.scope)
_rename[Core.SSAValue(i)] = push!(ir, enter_expr)
elseif ex isa GotoNode
branch!(ir, findfirst(==(ex.label), bs)+1)
elseif isgotoifnot(ex)
Expand Down
18 changes: 18 additions & 0 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,21 @@ end
@test (code_typed(func_ir, Tuple{typeof(func_ir)}) |> only
isa Pair{Core.CodeInfo,DataType})
end

function f_try_catch(x)
y = 0.
try
y = sqrt(x)
catch

end
y
end

@testset "try/catch" begin
ir = @code_ir f_try_catch(1.)
@test true
fir = func(ir)
@test fir(nothing,1.) == 1.
@test_broken fir(nothing,-1.) == 1.
end
21 changes: 18 additions & 3 deletions test/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ end
let
ir1 = @code_ir foo(1, 2)
ir2 = @code_ir bar(1, 2)
ir = IRTools.inline(ir1, IRTools.var(4), ir2)
inline_at = findfirst(ir1) do (_, stmt)
IRTools.isexpr(stmt.expr, :call) &&
stmt.expr.args[1] == GlobalRef(@__MODULE__, :bar)
end
@test inline_at !== nothing
ir = IRTools.inline(ir1, inline_at, ir2)
f = IRTools.func(ir)
@test f(nothing, 2, 3) == 3
@test f(nothing, 3, 2) == 3
Expand All @@ -66,7 +71,12 @@ end
let
ir = @code_ir foo2(1)
ir2 = @code_ir foo1(1)
ir3 = IRTools.inline(ir, IRTools.var(4), ir2)
inline_at = findfirst(ir) do (_, stmt)
IRTools.isexpr(stmt.expr, :call) &&
stmt.expr.args[1] == GlobalRef(@__MODULE__, :foo1)
end
@test inline_at !== nothing
ir3 = IRTools.inline(ir, inline_at, ir2)
@test IRTools.func(ir3)(nothing, 2) == 12
@test IRTools.func(ir3)(nothing, 101) == 101
end
Expand All @@ -87,7 +97,12 @@ end
let
ir = @code_ir foo2(1)
ir2 = @code_ir foo1(1)
ir3 = IRTools.inline(ir, IRTools.var(3), ir2)
inline_at = findfirst(ir) do (_, stmt)
IRTools.isexpr(stmt.expr, :call) &&
stmt.expr.args[1] == GlobalRef(@__MODULE__, :foo1)
end
@test inline_at !== nothing
ir3 = IRTools.inline(ir, inline_at, ir2)
@test IRTools.func(ir3)(nothing, 2) == foo2(2)
@test IRTools.func(ir3)(nothing, -2) == foo2(-2)
end