Skip to content

Commit da5d637

Browse files
KenoKristofferC
authored andcommitted
Fix use counts for mutable struct SROA
PR #28478 moved the computation of the use counts before the finish call. to fix #28444. However, the early parts of the finish call fixes up phi node arguments, which fail to get counted if we look at use counts before that fixup is performed. This causes #30594 where the only non-trivial use is on the backedge of the phi and would thus incorrectly fail to get accounted for. Fix that by taking the use count after phi fixup but before dce. (cherry picked from commit f8f2045)
1 parent bac9350 commit da5d637

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

base/compiler/ssair/passes.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,14 @@ function getfield_elim_pass!(ir::IRCode, domtree::DomTree)
694694
compact[idx] = val === nothing ? nothing : val.x
695695
end
696696

697-
# Copy the use count, `finish` may modify it and for our predicate
698-
# below we need it consistent with the state of the IR here.
697+
698+
non_dce_finish!(compact)
699+
# Copy the use count, `simple_dce!` may modify it and for our predicate
700+
# below we need it consistent with the state of the IR here (after tracking
701+
# phi node arguments, but before dce).
699702
used_ssas = copy(compact.used_ssas)
700-
ir = finish(compact)
703+
simple_dce!(compact)
704+
ir = complete(compact)
701705
# Now go through any mutable structs and see which ones we can eliminate
702706
for (idx, (intermediaries, defuse)) in defuses
703707
intermediaries = collect(intermediaries)

test/compiler/irpasses.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
using Test
44

5+
# Tests for SROA
6+
7+
mutable struct Foo30594; x::Float64; end
8+
Base.copy(x::Foo30594) = Foo30594(x.x)
9+
function add!(p::Foo30594, off::Foo30594)
10+
p.x += off.x
11+
return p
12+
end
13+
Base.:(+)(a::Foo30594, b::Foo30594) = add!(copy(a), b)
14+
15+
let results = Float64[]
16+
@noinline use30594(x) = push!(results, x.x); nothing
17+
function foo30594(cnt::Int, dx::Int)
18+
step = Foo30594(dx)
19+
curr = step + Foo30594(1)
20+
for i in 1:cnt
21+
use30594(curr)
22+
curr = curr + step
23+
end
24+
nothing
25+
end
26+
27+
foo30594(4, -1)
28+
@test results == [0.0, -1.0, -2.0, -3.0]
29+
end
30+
531
# Issue #29983
632
# This one is a bit hard to trigger, but the key is to create a case
733
# where SROA needs to introduce an intermediate type-unstable phi node

0 commit comments

Comments
 (0)