Skip to content

Commit 99e2604

Browse files
authored
sroa: Fix accidentally dropped condition (#50555)
Fixes the issue noted in [1] (#50522) and adds a test to make sure that it doesn't regress. [1] 9b73611#r121641452
2 parents 7735556 + e464218 commit 99e2604

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/compiler/ssair/passes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ function sroa_pass!(ir::IRCode, inlining::Union{Nothing,InliningState}=nothing)
11071107
struct_typ = widenconst(argextype(val, compact))
11081108
struct_argtyp = argument_datatype(struct_typ)
11091109
if struct_argtyp === nothing
1110-
if isa(struct_typ, Union)
1110+
if isa(struct_typ, Union) && is_isdefined
11111111
lift_comparison!(isdefined, compact, idx, stmt, lifting_cache, 𝕃ₒ)
11121112
end
11131113
continue

test/compiler/irpasses.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,3 +1421,20 @@ let src = code_typed1(sroaunswitchunionstruct1, Tuple{Bool, Int, Float64})
14211421
end
14221422
@test sroaunswitchunionstruct2(true, 1, 1.0) === 1
14231423
@test sroaunswitchunionstruct2(false, 1, 1.0) === 1.0
1424+
1425+
# Test SROA of union into getfield
1426+
struct SingleFieldStruct1
1427+
x::Int
1428+
end
1429+
struct SingleFieldStruct2
1430+
x::Int
1431+
end
1432+
function foo(b, x)
1433+
if b
1434+
f = SingleFieldStruct1(x)
1435+
else
1436+
f = SingleFieldStruct2(x)
1437+
end
1438+
getfield(f, :x) + 1
1439+
end
1440+
@test foo(true, 1) == 2

0 commit comments

Comments
 (0)