Skip to content

Commit 461fe0a

Browse files
authored
Merge pull request #64617 from meg-gupta/fix5.9
[5.9] Fix OSSA RAUW utility's insertion point
2 parents d332c4e + b83858c commit 461fe0a

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
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

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -943,23 +943,9 @@ BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUses(
943943
makeUserRange(guaranteedUsePoints));
944944
}
945945

946-
// Return the borrow position when replacing guaranteedValue with newValue.
947-
//
948-
// Precondition: newValue's block dominates and reaches guaranteedValue's block.
949-
//
950-
// Postcondition: The returned instruction's block is guaranteedValue's block.
951-
//
952-
// If \p newValue and \p guaranteedValue are in the same block, borrow at the
953-
// newValue just in case it is defined later in the block (to avoid scanning
954-
// instructions). Otherwise, borrow in the guaranteedValue's block to avoid
955-
// introducing the borrow scope too early--not only would this require extra
956-
// cleanup, but it would hinder optimization.
957-
static SILBasicBlock::iterator getBorrowPoint(SILValue newValue,
958-
SILValue guaranteedValue) {
959-
if (newValue->getParentBlock() == guaranteedValue->getParentBlock())
960-
return newValue->getNextInstruction()->getIterator();
961-
962-
return guaranteedValue->getNextInstruction()->getIterator();
946+
// Return the borrow position when replacing oldValue.
947+
static SILBasicBlock::iterator getBorrowPoint(SILValue oldValue) {
948+
return oldValue->getDefiningInsertionPoint()->getIterator();
963949
}
964950

965951
/// Borrow \p newValue over the lifetime of \p guaranteedValue. Return the
@@ -991,7 +977,7 @@ OwnershipLifetimeExtender::borrowOverValue(SILValue newValue,
991977
return newValue;
992978

993979
// FIXME: use GuaranteedOwnershipExtension
994-
auto borrowPt = getBorrowPoint(newValue, guaranteedValue);
980+
auto borrowPt = getBorrowPoint(guaranteedValue);
995981
return borrowCopyOverGuaranteedUses(
996982
newValue, borrowPt, ArrayRef<Operand *>(ctx.guaranteedUsePoints));
997983
}
@@ -1151,7 +1137,7 @@ SILValue OwnershipRAUWPrepare::prepareUnowned(SILValue newValue) {
11511137
}
11521138

11531139
auto extender = getLifetimeExtender();
1154-
auto borrowPt = getBorrowPoint(newValue, oldValue);
1140+
auto borrowPt = getBorrowPoint(oldValue);
11551141
SILValue borrow = extender.borrowCopyOverGuaranteedUses(
11561142
newValue, borrowPt, oldValue->getUses());
11571143
return borrow;
@@ -1267,7 +1253,7 @@ OwnershipRAUWHelper::getReplacementAddress() {
12671253
// value. Then we RAUW as appropriate.
12681254
OwnershipLifetimeExtender extender{*ctx};
12691255
auto base = ctx->extraAddressFixupInfo.base;
1270-
auto borrowPt = getBorrowPoint(newValue, oldValue);
1256+
auto borrowPt = getBorrowPoint(oldValue);
12711257
// FIXME: why does this use allAddressUsesFromOldValue instead of
12721258
// guaranteedUsePoints?
12731259
BeginBorrowInst *bbi = extender.borrowCopyOverGuaranteedUses(

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)