Skip to content

Commit

Permalink
Add docstring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zentrik committed Aug 30, 2023
1 parent 732c5c3 commit e8c9f99
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
14 changes: 13 additions & 1 deletion base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,18 @@ function perform_symbolic_evaluation(stmt::PhiNode, ssa_to_ssa, blockidx)
return svec(key...)
end

"""
gnv!(ir::IRCode) -> newir::IRCode
Global Value Numbering (GVN) pass.
This is based on the NewGVN pass in LLVM.
GVN partitions all the statements in the IR into congruence classes. All elements in a congruence class are guaranteed to be the same.
It implements the RPO value numbering algorithm based on the paper "SCC based value numbering" by L. Taylor Simpson.
The elimination step is based on the implemenation in LLVM's NewGVN pass.
"""
function gvn!(ir::IRCode)
changed = true
ssa_to_ssa = fill(0, length(ir.stmts.stmt)) # Map from ssa to ssa of equivalent value
Expand All @@ -1973,7 +1985,7 @@ function gvn!(ir::IRCode)
while changed
changed = false

# RPO Traversal
# Reverse Post Order traversal of dominator tree
for (blockidx, block) in enumerate(ir.cfg.blocks), i in block.stmts
if !(ir.stmts.stmt[i] isa Expr) & !(ir.stmts.stmt[i] isa PhiNode)
ssa_to_ssa[i] = i
Expand Down
5 changes: 3 additions & 2 deletions test/compiler/EscapeAnalysis/EAUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ import Core:
CodeInstance, MethodInstance, CodeInfo
import .CC:
InferenceResult, OptimizationState, IRCode, copy as cccopy,
@timeit, convert_to_ircode, slot2reg, compact!, ssa_inlining_pass!, sroa_pass!,
adce_pass!, JLOptions, verify_ir, verify_linetable
@timeit, convert_to_ircode, slot2reg, compact!, ssa_inlining_pass!, gvn!,
sroa_pass!, adce_pass!, JLOptions, verify_ir, verify_linetable
import .EA: analyze_escapes, ArgEscapeCache, EscapeInfo, EscapeState, is_ipo_profitable

# when working outside of Core.Compiler,
Expand Down Expand Up @@ -222,6 +222,7 @@ function run_passes_with_ea(interp::EscapeAnalyzer, ci::CodeInfo, sv::Optimizati
interp.state = state
interp.linfo = sv.linfo
end
@timeit "GVN" ir = gvn!(ir)
@timeit "SROA" ir = sroa_pass!(ir)
@timeit "ADCE" ir = adce_pass!(ir)
@timeit "compact 3" ir = compact!(ir)
Expand Down
22 changes: 22 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1452,3 +1452,25 @@ end
fully_eliminated(; retval=Core.Argument(2)) do x::Float64
return ifelse(isa(x, Float64), x, exp(x))
end

# Test that gvn! works on example from the paper "SCC based value numbering" by L. Taylor Simpson.
let src = @eval Module() begin
function g()
x = 1
y = 1
while true
x += 1
y += 1
end
end
code_typed(g, Tuple{})[1][1]
end
nphi = 0
for stmt in src.code
if stmt isa Core.PhiNode
nphi += 1
end
end
@test nphi == 1
@test count(iscall((src, Base.add_int)), src.code) == 1
end

0 comments on commit e8c9f99

Please sign in to comment.