Skip to content

Commit 7b7da48

Browse files
committed
Disable a silcombine pattern that creates unoptimizable copies
1 parent 7857b67 commit 7b7da48

File tree

2 files changed

+9
-42
lines changed

2 files changed

+9
-42
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -799,37 +799,13 @@ SILCombiner::visitRawPointerToRefInst(RawPointerToRefInst *rawToRef) {
799799
// ->
800800
// (unchecked_ref_cast x X->Z)
801801
if (auto *refToRaw = dyn_cast<RefToRawPointerInst>(rawToRef->getOperand())) {
802+
// We do this optimization only in non-ossa.
803+
// In ossa, the copy created by ossa rauw is unoptimizable, skipping for
804+
// this reason.
802805
if (!hasOwnership()) {
803806
return Builder.createUncheckedRefCast(
804807
rawToRef->getLoc(), refToRaw->getOperand(), rawToRef->getType());
805808
}
806-
807-
// raw_pointer_to_ref produces an unowned value. So we need to handle it
808-
// especially with ownership.
809-
{
810-
SILValue originalRef = refToRaw->getOperand();
811-
OwnershipRAUWHelper helper(ownershipFixupContext, rawToRef, originalRef);
812-
if (helper) {
813-
// Since we are using std::next, we use getAutogeneratedLocation to
814-
// avoid any issues if our next insertion point is a terminator.
815-
auto loc = RegularLocation::getAutoGeneratedLocation();
816-
auto replacement = helper.prepareReplacement();
817-
auto *newInst = Builder.createUncheckedRefCast(
818-
loc, replacement, rawToRef->getType());
819-
// If we have an operand with ownership, we need to change our
820-
// unchecked_ref_cast to produce an unowned value. This is because
821-
// otherwise, our unchecked_ref_cast will consume the underlying owned
822-
// value, changing a BitwiseEscape to a LifetimeEnding use?! In
823-
// contrast, for guaranteed, we are replacing a BitwiseEscape use
824-
// (ref_to_rawpointer) with a ForwardedBorrowingUse (unchecked_ref_cast)
825-
// which is safe.
826-
if (newInst->getForwardingOwnershipKind() == OwnershipKind::Owned) {
827-
newInst->setForwardingOwnershipKind(OwnershipKind::Unowned);
828-
}
829-
helper.perform(newInst);
830-
return nullptr;
831-
}
832-
}
833809
}
834810

835811
return nullptr;

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,11 +1548,8 @@ bb0(%0 : @owned $B):
15481548

15491549
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
15501550
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @owned $B):
1551-
// CHECK-NOT: ref_to_raw_pointer
1552-
// CHECK-NOT: raw_pointer_to_ref
1553-
// CHECK: unchecked_ref_cast
1554-
// CHECK-NOT: ref_to_raw_pointer
1555-
// CHECK-NOT: raw_pointer_to_ref
1551+
// CHECK: ref_to_raw_pointer
1552+
// CHECK: raw_pointer_to_ref
15561553
// CHECK: } // end sil function 'unchecked_ref_cast_formation_owned'
15571554
sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
15581555
bb0(%0 : @owned $B):
@@ -1565,11 +1562,8 @@ bb0(%0 : @owned $B):
15651562

15661563
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
15671564
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @guaranteed $B):
1568-
// CHECK-NOT: ref_to_raw_pointer
1569-
// CHECK-NOT: raw_pointer_to_ref
1570-
// CHECK: unchecked_ref_cast
1571-
// CHECK-NOT: ref_to_raw_pointer
1572-
// CHECK-NOT: raw_pointer_to_ref
1565+
// CHECK: ref_to_raw_pointer
1566+
// CHECK: raw_pointer_to_ref
15731567
// CHECK: } // end sil function 'unchecked_ref_cast_formation_guaranteed'
15741568
sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
15751569
bb0(%0 : @guaranteed $B):
@@ -1581,11 +1575,8 @@ bb0(%0 : @guaranteed $B):
15811575

15821576
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
15831577
// CHECK: bb0([[INPUT_REF:%[0-9]+]] :
1584-
// CHECK-NOT: ref_to_raw_pointer
1585-
// CHECK-NOT: raw_pointer_to_ref
1586-
// CHECK: unchecked_ref_cast
1587-
// CHECK-NOT: ref_to_raw_pointer
1588-
// CHECK-NOT: raw_pointer_to_ref
1578+
// CHECK: ref_to_raw_pointer
1579+
// CHECK: raw_pointer_to_ref
15891580
// CHECK: } // end sil function 'unchecked_ref_cast_formation_unowned'
15901581
sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
15911582
bb0(%0 : @unowned $B):

0 commit comments

Comments
 (0)