Skip to content

Commit c40f7c3

Browse files
committed
[OwnershipUtils] Undef isn't introduced/enclosed.
Previously, calling `visitEnclosingDefs` or `visitBorrowIntroducers` would crash if the `SILValue` argument were an instance of `SILUndef` because both instantiate a `::FindEnclosingDefs` with the the result of of calling `getFunction()` on that argument and `::FindEnclosingDefs` in turn uses that result to create a `ValueSet`; but `SILUndef::getFunction` returns `nullptr`, which is illegal to use as the function for a `ValueSet`(or any other data structure relying on `NodeBits`). If the function specified as a separate argument, no values would be visited and `true` would be returned. Instead of burdening the API with a separate argument, check for `SILUndef` up front and return `true`.
1 parent cc0f42f commit c40f7c3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,12 +2230,16 @@ class FindEnclosingDefs {
22302230

22312231
bool swift::visitEnclosingDefs(SILValue value,
22322232
function_ref<bool(SILValue)> visitor) {
2233+
if (isa<SILUndef>(value))
2234+
return true;
22332235
return FindEnclosingDefs(value->getFunction())
22342236
.visitEnclosingDefs(value, visitor);
22352237
}
22362238

22372239
bool swift::visitBorrowIntroducers(SILValue value,
22382240
function_ref<bool(SILValue)> visitor) {
2241+
if (isa<SILUndef>(value))
2242+
return true;
22392243
return FindEnclosingDefs(value->getFunction())
22402244
.visitBorrowIntroducers(value, visitor);
22412245
}

test/SILOptimizer/canonicalize_ossa_lifetime_unit.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class C {}
44
sil @getOwned : $@convention(thin) () -> @owned C
55
sil @barrier : $@convention(thin) () -> ()
6+
struct S {}
67

78
// When access scopes are respected, the lifetime which previously extended
89
// beyond the access scope still extends beyond it.
@@ -115,3 +116,21 @@ bb0(%0 : $*C, %instance : @owned $C):
115116
%retval = tuple ()
116117
return %retval : $()
117118
}
119+
120+
// Don't crash on an adjacent phi with an incoming undef.
121+
sil [ossa] @adjacent_phi_with_incoming_undef : $@convention(thin) () -> () {
122+
entry:
123+
%getC = function_ref @getOwned : $@convention(thin) () -> @owned C
124+
%c2 = apply %getC() : $@convention(thin) () -> @owned C
125+
br right2(%c2 : $C)
126+
127+
right2(%c2p : @owned $C):
128+
br exit(%c2p : $C, undef : $S)
129+
130+
exit(%phi : @owned $C, %typhi : $S):
131+
debug_value [trace] %phi : $C
132+
test_specification "canonicalize-ossa-lifetime true false true @trace"
133+
destroy_value %phi : $C
134+
%retval = tuple ()
135+
return %retval : $()
136+
}

0 commit comments

Comments
 (0)