Skip to content

Commit

Permalink
ssair: compact! constant PiNode (#50768)
Browse files Browse the repository at this point in the history
Currently the `compact!`-ion pass fails to fold constant `PiNode` like
`PiNode(0.0, Const(0.0))`. This commit fixes it up.
  • Loading branch information
aviatesk committed Aug 4, 2023
1 parent f10c461 commit 2a70dac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,9 @@ function process_node!(compact::IncrementalCompact, result_idx::Int, inst::Instr
return result_idx
end
elseif !isa(pi_val, AnySSAValue) && !isa(pi_val, GlobalRef)
valtyp = isa(pi_val, QuoteNode) ? typeof(pi_val.value) : typeof(pi_val)
if valtyp === stmt.typ
pi_val′ = isa(pi_val, QuoteNode) ? pi_val.value : pi_val
stmttyp = stmt.typ
if isa(stmttyp, Const) ? pi_val′ === stmttyp.val : typeof(pi_val′) === stmttyp
ssa_rename[idx] = pi_val
return result_idx
end
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ function f44200()
x44200
end
let src = code_typed1(f44200)
@test_broken count(x -> isa(x, Core.PiNode), src.code) == 0
@test count(x -> isa(x, Core.PiNode), src.code) == 0
end

# Test that peeling off one case from (::Any) doesn't introduce
Expand Down
7 changes: 5 additions & 2 deletions test/compiler/irutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ isinvoke(y) = @nospecialize(x) -> isinvoke(y, x)
isinvoke(sym::Symbol, @nospecialize(x)) = isinvoke(mi->mi.def.name===sym, x)
isinvoke(pred::Function, @nospecialize(x)) = isexpr(x, :invoke) && pred(x.args[1]::MethodInstance)

function fully_eliminated(@nospecialize args...; retval=(@__FILE__), kwargs...)
code = code_typed1(args...; kwargs...).code
fully_eliminated(@nospecialize args...; retval=(@__FILE__), kwargs...) =
fully_eliminated(code_typed1(args...; kwargs...); retval)
fully_eliminated(src::CodeInfo; retval=(@__FILE__)) = fully_eliminated(src.code; retval)
fully_eliminated(ir::IRCode; retval=(@__FILE__)) = fully_eliminated(ir.stmts.inst; retval)
function fully_eliminated(code::Vector{Any}; retval=(@__FILE__), kwargs...)
if retval !== (@__FILE__)
length(code) == 1 || return false
code1 = code[1]
Expand Down
12 changes: 11 additions & 1 deletion test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Core.IR
const Compiler = Core.Compiler
using .Compiler: CFG, BasicBlock, NewSSAValue

include(normpath(@__DIR__, "irutils.jl"))
include("irutils.jl")

make_bb(preds, succs) = BasicBlock(Compiler.StmtRange(0, 0), preds, succs)

Expand Down Expand Up @@ -593,6 +593,16 @@ let ci = make_ci([
@test Core.Compiler.verify_ir(ir) === nothing
end

# compact constant PiNode
let ci = make_ci(Any[
PiNode(0.0, Const(0.0))
ReturnNode(SSAValue(1))
])
ir = Core.Compiler.inflate_ir(ci)
ir = Core.Compiler.compact!(ir)
@test fully_eliminated(ir)
end

# insert_node! with new instruction with flag computed
let ir = Base.code_ircode((Int,Int); optimize_until="inlining") do a, b
a^b
Expand Down

0 comments on commit 2a70dac

Please sign in to comment.