Skip to content

Disable a silcombine pattern that creates unoptimizable copies #70440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,37 +800,13 @@ SILCombiner::visitRawPointerToRefInst(RawPointerToRefInst *rawToRef) {
// ->
// (unchecked_ref_cast x X->Z)
if (auto *refToRaw = dyn_cast<RefToRawPointerInst>(rawToRef->getOperand())) {
// We do this optimization only in non-ossa.
// In ossa, the copy created by ossa rauw is unoptimizable, skipping for
// this reason.
if (!hasOwnership()) {
return Builder.createUncheckedRefCast(
rawToRef->getLoc(), refToRaw->getOperand(), rawToRef->getType());
}

// raw_pointer_to_ref produces an unowned value. So we need to handle it
// especially with ownership.
{
SILValue originalRef = refToRaw->getOperand();
OwnershipRAUWHelper helper(ownershipFixupContext, rawToRef, originalRef);
if (helper) {
// Since we are using std::next, we use getAutogeneratedLocation to
// avoid any issues if our next insertion point is a terminator.
auto loc = RegularLocation::getAutoGeneratedLocation();
auto replacement = helper.prepareReplacement();
auto *newInst = Builder.createUncheckedRefCast(
loc, replacement, rawToRef->getType());
// If we have an operand with ownership, we need to change our
// unchecked_ref_cast to produce an unowned value. This is because
// otherwise, our unchecked_ref_cast will consume the underlying owned
// value, changing a BitwiseEscape to a LifetimeEnding use?! In
// contrast, for guaranteed, we are replacing a BitwiseEscape use
// (ref_to_rawpointer) with a ForwardedBorrowingUse (unchecked_ref_cast)
// which is safe.
if (newInst->getForwardingOwnershipKind() == OwnershipKind::Owned) {
newInst->setForwardingOwnershipKind(OwnershipKind::Unowned);
}
helper.perform(newInst);
return nullptr;
}
}
}

return nullptr;
Expand Down
21 changes: 6 additions & 15 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1572,11 +1572,8 @@ bb0(%0 : @owned $B):

// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @owned $B):
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: unchecked_ref_cast
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: ref_to_raw_pointer
// CHECK: raw_pointer_to_ref
// CHECK: } // end sil function 'unchecked_ref_cast_formation_owned'
sil [ossa] @unchecked_ref_cast_formation_owned : $@convention(thin) (@owned B) -> @owned F {
bb0(%0 : @owned $B):
Expand All @@ -1589,11 +1586,8 @@ bb0(%0 : @owned $B):

// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
// CHECK: bb0([[INPUT_REF:%[0-9]+]] : @guaranteed $B):
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: unchecked_ref_cast
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: ref_to_raw_pointer
// CHECK: raw_pointer_to_ref
// CHECK: } // end sil function 'unchecked_ref_cast_formation_guaranteed'
sil [ossa] @unchecked_ref_cast_formation_guaranteed : $@convention(thin) (@guaranteed B) -> @owned F {
bb0(%0 : @guaranteed $B):
Expand All @@ -1605,11 +1599,8 @@ bb0(%0 : @guaranteed $B):

// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
// CHECK: bb0([[INPUT_REF:%[0-9]+]] :
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: unchecked_ref_cast
// CHECK-NOT: ref_to_raw_pointer
// CHECK-NOT: raw_pointer_to_ref
// CHECK: ref_to_raw_pointer
// CHECK: raw_pointer_to_ref
// CHECK: } // end sil function 'unchecked_ref_cast_formation_unowned'
sil [ossa] @unchecked_ref_cast_formation_unowned : $@convention(thin) (B) -> F {
bb0(%0 : @unowned $B):
Expand Down