Skip to content

Commit 2556a1a

Browse files
committed
run EA for SROA and check its accuracy
1 parent 757a5e0 commit 2556a1a

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

base/compiler/optimize.jl

+2-7
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,11 @@ function run_passes(ci::CodeInfo, sv::OptimizationState)
517517
@timeit "Inlining" ir = ssa_inlining_pass!(ir, ir.linetable, sv.inlining, ci.propagate_inbounds)
518518
# @timeit "verify 2" verify_ir(ir)
519519
@timeit "compact 2" ir = compact!(ir)
520-
@timeit "SROA" ir = sroa_pass!(ir)
520+
nargs = let def = sv.linfo.def; isa(def, Method) ? Int(def.nargs) : 0; end
521+
@timeit "SROA" ir = sroa_pass!(ir, nargs)
521522
@timeit "ADCE" ir = adce_pass!(ir)
522523
@timeit "type lift" ir = type_lift_pass!(ir)
523524
@timeit "compact 3" ir = compact!(ir)
524-
# TODO validate EA by comparing it to the existing SROA
525-
nargs = let def = sv.linfo.def
526-
isa(def, Method) ? Int(def.nargs) : 0
527-
end
528-
@timeit "EA" estate = analyze_escapes(ir, nargs)
529-
cache_escapes!(sv.linfo, estate, ir)
530525
if JLOptions().debug_level == 2
531526
@timeit "verify 3" (verify_ir(ir); verify_linetable(ir.linetable))
532527
end

base/compiler/ssair/EscapeAnalysis/EAUtils.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ function run_passes_with_ea(interp::EscapeAnalyzer, ci::CodeInfo, sv::Optimizati
252252
@timeit "Inlining" ir = ssa_inlining_pass!(ir, ir.linetable, sv.inlining, ci.propagate_inbounds)
253253
# @timeit "verify 2" verify_ir(ir)
254254
@timeit "compact 2" ir = compact!(ir)
255-
nargs = let def = sv.linfo.def
256-
isa(def, Method) ? Int(def.nargs) : 0
257-
end
255+
nargs = let def = sv.linfo.def; isa(def, Method) ? Int(def.nargs) : 0; end
258256
local state
259257
try
260258
@timeit "collect escape information" state = analyze_escapes(ir, nargs)
@@ -270,7 +268,7 @@ function run_passes_with_ea(interp::EscapeAnalyzer, ci::CodeInfo, sv::Optimizati
270268
interp.ir = cacheir
271269
interp.state = state
272270
interp.linfo = sv.linfo
273-
@timeit "SROA" ir = sroa_pass!(ir)
271+
@timeit "SROA" ir = sroa_pass!(ir, nargs)
274272
@timeit "ADCE" ir = adce_pass!(ir)
275273
@timeit "type lift" ir = type_lift_pass!(ir)
276274
@timeit "compact 3" ir = compact!(ir)

base/compiler/ssair/passes.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ its argument).
641641
In a case when all usages are fully eliminated, `struct` allocation may also be erased as
642642
a result of succeeding dead code elimination.
643643
"""
644-
function sroa_pass!(ir::IRCode)
644+
function sroa_pass!(ir::IRCode, nargs::Int)
645645
compact = IncrementalCompact(ir)
646646
defuses = nothing # will be initialized once we encounter mutability in order to reduce dynamic allocations
647647
lifting_cache = IdDict{Pair{AnySSAValue, Any}, AnySSAValue}()
@@ -813,17 +813,18 @@ function sroa_pass!(ir::IRCode)
813813
used_ssas = copy(compact.used_ssas)
814814
simple_dce!(compact, (x::SSAValue) -> used_ssas[x.id] -= 1)
815815
ir = complete(compact)
816-
sroa_mutables!(ir, defuses, used_ssas)
816+
sroa_mutables!(ir, defuses, used_ssas, nargs)
817817
return ir
818818
else
819819
simple_dce!(compact)
820820
return complete(compact)
821821
end
822822
end
823823

824-
function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse}}, used_ssas::Vector{Int})
824+
function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse}}, used_ssas::Vector{Int}, nargs::Int)
825825
# initialization of domtree is delayed to avoid the expensive computation in many cases
826826
local domtree = nothing
827+
estate = analyze_escapes(ir, nargs)
827828
for (idx, (intermediaries, defuse)) in defuses
828829
intermediaries = collect(intermediaries)
829830
# Check if there are any uses we did not account for. If so, the variable
@@ -899,6 +900,7 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int, Tuple{SPCSet, SSADefUse
899900
end
900901
end
901902
end
903+
is_sroa_eligible(estate[SSAValue(idx)]) || println("[EA] bad EA: ", ir.argtypes[1:nargs], " at ", idx)
902904
# Everything accounted for. Go field by field and perform idf:
903905
# Compute domtree now, needed below, now that we have finished compacting the IR.
904906
# This needs to be after we iterate through the IR with `IncrementalCompact`

test/compiler/irpasses.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ let m = Meta.@lower 1 + 1
407407
src.ssaflags = fill(Int32(0), nstmts)
408408
ir = Core.Compiler.inflate_ir(src, Any[], Any[Any, Any])
409409
@test Core.Compiler.verify_ir(ir) === nothing
410-
ir = @test_nowarn Core.Compiler.sroa_pass!(ir)
410+
ir = @test_nowarn Core.Compiler.sroa_pass!(ir, 0)
411411
@test Core.Compiler.verify_ir(ir) === nothing
412412
end
413413

0 commit comments

Comments
 (0)