Skip to content

Commit b83858c

Browse files
committed
Update SILCombine now that OSSA RAUW always inserts borrow before the value to be replaced
1 parent 667dc89 commit b83858c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,8 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
337337
OwnershipRAUWHelper helper(ownershipFixupContext, PTAI,
338338
ATPI->getOperand());
339339
if (helper) {
340-
SILBuilderWithScope localBuilder(std::next(PTAI->getIterator()), Builder);
341340
auto replacement = helper.prepareReplacement();
342-
auto *newInst = localBuilder.createUncheckedAddrCast(
341+
auto *newInst = Builder.createUncheckedAddrCast(
343342
PTAI->getLoc(), replacement, PTAI->getType());
344343
helper.perform(newInst);
345344
return nullptr;
@@ -818,15 +817,11 @@ SILCombiner::visitRawPointerToRefInst(RawPointerToRefInst *rawToRef) {
818817
SILValue originalRef = refToRaw->getOperand();
819818
OwnershipRAUWHelper helper(ownershipFixupContext, rawToRef, originalRef);
820819
if (helper) {
821-
// We use the refToRaw's insertion point to insert our
822-
// unchecked_ref_cast, since we don't know if our guaranteed value
823-
SILBuilderWithScope localBuilder(std::next(refToRaw->getIterator()),
824-
Builder);
825820
// Since we are using std::next, we use getAutogeneratedLocation to
826821
// avoid any issues if our next insertion point is a terminator.
827822
auto loc = RegularLocation::getAutoGeneratedLocation();
828823
auto replacement = helper.prepareReplacement();
829-
auto *newInst = localBuilder.createUncheckedRefCast(
824+
auto *newInst = Builder.createUncheckedRefCast(
830825
loc, replacement, rawToRef->getType());
831826
// If we have an operand with ownership, we need to change our
832827
// unchecked_ref_cast to produce an unowned value. This is because

test/SILOptimizer/sil_combine_nocanonicalize_ossa.sil

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
sil_stage canonical
77

8+
import Swift
89
import Builtin
910

1011
class Klass {}
@@ -81,3 +82,25 @@ bb0:
8182
destroy_value %obj : $KlassWithTailAllocatedElems
8283
return %val : $Builtin.Word
8384
}
85+
86+
sil @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
87+
88+
// CHECK-LABEL: sil [ossa] @bitwise_combines_guaranteed :
89+
// CHECK: unchecked_ref_cast
90+
// CHECK-LABEL: } // end sil function 'bitwise_combines_guaranteed'
91+
sil [ossa] @bitwise_combines_guaranteed : $@convention(thin) () -> @owned Optional<Builtin.NativeObject> {
92+
bb0:
93+
%f = function_ref @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
94+
%0 = apply %f() : $@convention(thin) () -> @owned Builtin.NativeObject
95+
%b = begin_borrow [lexical] %0 : $Builtin.NativeObject
96+
br bb1
97+
98+
bb1:
99+
%2 = unchecked_trivial_bit_cast %b : $Builtin.NativeObject to $Builtin.Word
100+
%3 = unchecked_bitwise_cast %2 : $Builtin.Word to $Optional<Builtin.NativeObject>
101+
%c = copy_value %3 : $Optional<Builtin.NativeObject>
102+
end_borrow %b : $Builtin.NativeObject
103+
destroy_value %0 : $Builtin.NativeObject
104+
return %c : $Optional<Builtin.NativeObject>
105+
}
106+

0 commit comments

Comments
 (0)