Skip to content

Commit

Permalink
optimizer: minor tweak on insert_node!(::IncrementalCompact) (#47491)
Browse files Browse the repository at this point in the history
So that it can take a new instruction without flag already computed.
  • Loading branch information
aviatesk authored Nov 8, 2022
1 parent bfaaad3 commit 4ff526a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ function recompute_inst_flag(newinst::NewInstruction, src::Union{IRCode,Incremen
end

function insert_node!(compact::IncrementalCompact, @nospecialize(before), newinst::NewInstruction, attach_after::Bool=false)
newflag = newinst.flag::UInt8
newflag = recompute_inst_flag(newinst, compact)
if isa(before, SSAValue)
if before.id < compact.result_idx
count_added_node!(compact, newinst.stmt)
Expand Down
56 changes: 55 additions & 1 deletion test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,13 @@ end
@test Core.Compiler.verify_ir(ir) === nothing
end

# insert_node! for pending node
# insert_node! operations
# =======================

import Core: SSAValue
import Core.Compiler: NewInstruction, insert_node!

# insert_node! for pending node
let ir = Base.code_ircode((Int,Int); optimize_until="inlining") do a, b
a^b
end |> only |> first
Expand All @@ -550,3 +554,53 @@ let ir = Base.code_ircode((Int,Int); optimize_until="inlining") do a, b
@test iscall((ir,println), call2)
@test call2.args[2] === SSAValue(2)
end

# insert_node! with new instruction with flag computed
let ir = Base.code_ircode((Int,Int); optimize_until="inlining") do a, b
a^b
end |> only |> first
invoke_idx = findfirst(ir.stmts.inst) do @nospecialize(x)
Meta.isexpr(x, :invoke)
end
@test invoke_idx !== nothing
invoke_expr = ir.stmts.inst[invoke_idx]

# effect-ful node
let compact = Core.Compiler.IncrementalCompact(Core.Compiler.copy(ir))
insert_node!(compact, SSAValue(1), NewInstruction(Expr(:call, println, SSAValue(1)), Nothing), #=attach_after=#true)
state = Core.Compiler.iterate(compact)
while state !== nothing
state = Core.Compiler.iterate(compact, state[2])
end
ir = Core.Compiler.finish(compact)
new_invoke_idx = findfirst(ir.stmts.inst) do @nospecialize(x)
x == invoke_expr
end
@test new_invoke_idx !== nothing
new_call_idx = findfirst(ir.stmts.inst) do @nospecialize(x)
iscall((ir,println), x) && x.args[2] === SSAValue(invoke_idx)
end
@test new_call_idx !== nothing
@test new_call_idx == new_invoke_idx+1
end

# effect-free node
let compact = Core.Compiler.IncrementalCompact(Core.Compiler.copy(ir))
insert_node!(compact, SSAValue(1), NewInstruction(Expr(:call, GlobalRef(Base, :add_int), SSAValue(1), SSAValue(1)), Int), #=attach_after=#true)
state = Core.Compiler.iterate(compact)
while state !== nothing
state = Core.Compiler.iterate(compact, state[2])
end
ir = Core.Compiler.finish(compact)

ir = Core.Compiler.finish(compact)
new_invoke_idx = findfirst(ir.stmts.inst) do @nospecialize(x)
x == invoke_expr
end
@test new_invoke_idx !== nothing
new_call_idx = findfirst(ir.stmts.inst) do @nospecialize(x)
iscall((ir,Base.add_int), x) && x.args[2] === SSAValue(invoke_idx)
end
@test new_call_idx === nothing # should be deleted during the compaction
end
end

0 comments on commit 4ff526a

Please sign in to comment.