Skip to content

Commit 78a6213

Browse files
committed
Clear context in ownership rauw when we cannot fix ownership for rauw.
If not, subsequent unrelated call of OwnershipRAUWHelper::perform will have stale entries for transitiveBorrowedUses etc
1 parent 3e330ab commit 78a6213

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ static bool canFixUpOwnershipForRAUW(SILValue oldValue, SILValue newValue,
241241
if (oldValue.getOwnershipKind() == OwnershipKind::Guaranteed) {
242242
// Check that the old lifetime can be extended and record the necessary
243243
// book-keeping in the OwnershipFixupContext.
244-
return findTransitiveBorrowedUses(oldValue, context.transitiveBorrowedUses,
245-
context.recursiveReborrows);
244+
if (!findTransitiveBorrowedUses(oldValue, context.transitiveBorrowedUses,
245+
context.recursiveReborrows)) {
246+
context.clear();
247+
return false;
248+
}
246249
}
247250
return true;
248251
}

test/SILOptimizer/cse_ossa_nontrivial.sil

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,34 @@ bb3(%borrow : @guaranteed $Klass):
630630
return %copy : $NonTrivialStruct
631631
}
632632

633+
// Test to make sure we clear the context if we fail while checking if ownership rauw is possible
634+
// CHECK-LABEL: sil [ossa] @test_rauwfailsandthensucceeds :
635+
// CHECK: bb0
636+
// CHECK: struct_extract
637+
// CHECK: struct_extract
638+
// CHECK: struct $_SliceBuffer
639+
// CHECK-NOT: struct $_SliceBuffer
640+
// CHECK-LABEL: } // end sil function 'test_rauwfailsandthensucceeds'
641+
sil [ossa] @test_rauwfailsandthensucceeds : $@convention(method) <Element> (UnsafeMutablePointer<Element>, @guaranteed _ContiguousArrayBuffer<Element>, Int, UInt) -> @owned _SliceBuffer<Element> {
642+
bb0(%0 : $UnsafeMutablePointer<Element>, %1 : @guaranteed $_ContiguousArrayBuffer<Element>, %2 : $Int, %3 : $UInt):
643+
%4 = struct_extract %1 : $_ContiguousArrayBuffer<Element>, #_ContiguousArrayBuffer._storage
644+
%5 = copy_value %4 : $__ContiguousArrayStorageBase
645+
%6 = init_existential_ref %5 : $__ContiguousArrayStorageBase : $__ContiguousArrayStorageBase, $AnyObject
646+
%7 = struct_extract %1 : $_ContiguousArrayBuffer<Element>, #_ContiguousArrayBuffer._storage
647+
%8 = ref_tail_addr %7 : $__ContiguousArrayStorageBase, $Element
648+
%9 = address_to_pointer %8 : $*Element to $Builtin.RawPointer
649+
%10 = struct $UnsafeMutablePointer<Element> (%9 : $Builtin.RawPointer)
650+
%11 = begin_borrow %6 : $AnyObject
651+
%12 = struct $_SliceBuffer<Element> (%11 : $AnyObject, %0 : $UnsafeMutablePointer<Element>, %2 : $Int, %3 : $UInt)
652+
%13 = copy_value %12 : $_SliceBuffer<Element>
653+
end_borrow %11 : $AnyObject
654+
destroy_value %13 : $_SliceBuffer<Element>
655+
%14 = begin_borrow %6 : $AnyObject
656+
%15 = struct $_SliceBuffer<Element> (%14 : $AnyObject, %0 : $UnsafeMutablePointer<Element>, %2 : $Int, %3 : $UInt)
657+
%16 = copy_value %15 : $_SliceBuffer<Element>
658+
end_borrow %14 : $AnyObject
659+
%17 = struct $_SliceBuffer<Element> (%6 : $AnyObject, %0 : $UnsafeMutablePointer<Element>, %2 : $Int, %3 : $UInt)
660+
destroy_value %17 : $_SliceBuffer<Element>
661+
return %16 : $_SliceBuffer<Element>
662+
}
663+

0 commit comments

Comments
 (0)