Skip to content

Commit 63a8d8c

Browse files
authored
Merge pull request #70440 from meg-gupta/badcopy
Disable a silcombine pattern that creates unoptimizable copies
2 parents 1edbbce + 4c6b540 commit 63a8d8c

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

836812
return nullptr;

test/SILOptimizer/sil_combine_ossa.sil

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

15731573
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
15741574
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @owned $B):
1575-
// CHECK-NOT: ref_to_raw_pointer
1576-
// CHECK-NOT: raw_pointer_to_ref
1577-
// CHECK: unchecked_ref_cast
1578-
// CHECK-NOT: ref_to_raw_pointer
1579-
// CHECK-NOT: raw_pointer_to_ref
1575+
// CHECK: ref_to_raw_pointer
1576+
// CHECK: raw_pointer_to_ref
15801577
// CHECK: } // end sil function 'unchecked_ref_cast_formation_owned'
15811578
sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
15821579
bb0(%0 : @owned $B):
@@ -1589,11 +1586,8 @@ bb0(%0 : @owned $B):
15891586

15901587
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
15911588
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @guaranteed $B):
1592-
// CHECK-NOT: ref_to_raw_pointer
1593-
// CHECK-NOT: raw_pointer_to_ref
1594-
// CHECK: unchecked_ref_cast
1595-
// CHECK-NOT: ref_to_raw_pointer
1596-
// CHECK-NOT: raw_pointer_to_ref
1589+
// CHECK: ref_to_raw_pointer
1590+
// CHECK: raw_pointer_to_ref
15971591
// CHECK: } // end sil function 'unchecked_ref_cast_formation_guaranteed'
15981592
sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
15991593
bb0(%0 : @guaranteed $B):
@@ -1605,11 +1599,8 @@ bb0(%0 : @guaranteed $B):
16051599

16061600
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
16071601
// CHECK: bb0([[INPUT_REF:%[0-9]+]] :
1608-
// CHECK-NOT: ref_to_raw_pointer
1609-
// CHECK-NOT: raw_pointer_to_ref
1610-
// CHECK: unchecked_ref_cast
1611-
// CHECK-NOT: ref_to_raw_pointer
1612-
// CHECK-NOT: raw_pointer_to_ref
1602+
// CHECK: ref_to_raw_pointer
1603+
// CHECK: raw_pointer_to_ref
16131604
// CHECK: } // end sil function 'unchecked_ref_cast_formation_unowned'
16141605
sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
16151606
bb0(%0 : @unowned $B):

0 commit comments

Comments
 (0)