Skip to content

Commit 689c6ac

Browse files
committed
[OwnershipOptUtils] Don't RAUW lexical values.
Replacing all uses of a lexical value with another value, even another lexical value, may change the lifetime of the value in ways that aren't permitted for a lexical lifetime.
1 parent 0f16053 commit 689c6ac

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ bool OwnershipRAUWHelper::hasValidRAUWOwnership(SILValue oldValue,
300300
SILValue newValue) {
301301
auto newOwnershipKind = newValue.getOwnershipKind();
302302

303+
// If the either value is lexical, replacing its uses may result in
304+
// shortening or lengthening its lifetime in ways that don't respect lexical
305+
// scope and deinit barriers.
306+
if (oldValue->isLexical() || newValue->isLexical())
307+
return false;
308+
303309
// If our new kind is ValueOwnershipKind::None, then we are fine. We
304310
// trivially support that. This check also ensures that we can always
305311
// replace any value with a ValueOwnershipKind::None value.

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,6 +4218,27 @@ bb0(%0 : @guaranteed $B):
42184218
return %2 : $B
42194219
}
42204220

4221+
// Check that a mark_dependence on an enum on a lexical value doesn't result in
4222+
// RAUWing the enum with the lexical value and extending the latter's lifetime.
4223+
//
4224+
// CHECK-LABEL: sil [ossa] @mark_dependence_base1_lexical1 : {{.*}} {
4225+
// CHECK: {{bb[0-9]+}}([[ADDR:%[^,]+]] : $*Builtin.Int64, [[INSTANCE:%[^,]+]] : @owned $B):
4226+
// CHECK: [[LIFETIME:%[^,]+]] = move_value [lexical] [[INSTANCE]]
4227+
// CHECK: [[OPTIONAL:%[^,]+]] = enum $FakeOptional<B>, #FakeOptional.some!enumelt, [[LIFETIME]]
4228+
// CHECK: [[DEPENDENT_ADDR:%[^,]+]] = mark_dependence [[ADDR]]
4229+
// CHECK: [[VALUE:%[^,]+]] = load [trivial] [[DEPENDENT_ADDR]]
4230+
// CHECK: return [[VALUE]]
4231+
// CHECK-LABEL: } // end sil function 'mark_dependence_base1_lexical1'
4232+
sil [ossa] @mark_dependence_base1_lexical1 : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
4233+
bb0(%addr : $*Builtin.Int64, %instance : @owned $B):
4234+
%lifetime = move_value [lexical] %instance : $B
4235+
%optional = enum $FakeOptional<B>, #FakeOptional.some!enumelt, %lifetime : $B
4236+
%dependent_addr = mark_dependence %addr : $*Builtin.Int64 on %optional : $FakeOptional<B>
4237+
%value = load [trivial] %dependent_addr : $*Builtin.Int64
4238+
destroy_value %optional : $FakeOptional<B>
4239+
return %value : $Builtin.Int64
4240+
}
4241+
42214242
protocol _NSArrayCore {}
42224243

42234244
// CHECK-LABEL: sil [ossa] @mark_dependence_base2 :

0 commit comments

Comments
 (0)