Skip to content

Commit 8345c30

Browse files
authored
Revert "Followup for #61505"
1 parent 361f5b4 commit 8345c30

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4881,6 +4881,17 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
48814881
}
48824882

48834883
void checkBranchInst(BranchInst *BI) {
4884+
for (auto arg : BI->getArgs()) {
4885+
auto *borrow = dyn_cast<BeginBorrowInst>(arg);
4886+
if (!borrow) {
4887+
continue;
4888+
}
4889+
auto op = borrow->getOperand();
4890+
if (op->getOwnershipKind() != OwnershipKind::Guaranteed) {
4891+
continue;
4892+
}
4893+
assert(!isGuaranteedForwarding(op));
4894+
}
48844895
require(BI->getArgs().size() == BI->getDestBB()->args_size(),
48854896
"branch has wrong number of arguments for dest bb");
48864897
require(std::equal(BI->getArgs().begin(), BI->getArgs().end(),

lib/SILOptimizer/SemanticARC/OwnedToGuaranteedPhiOpt.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
251251
while (!incomingValueOperandList.empty()) {
252252
auto incomingValueOperand = incomingValueOperandList.pop_back_val();
253253
SILValue originalValue = originalIncomingValues.pop_back_val();
254+
if (incomingValueOperand.isGuaranteedConsuming() &&
255+
isa<SILFunctionArgument>(originalValue) &&
256+
originalValue->getOwnershipKind() == OwnershipKind::Guaranteed) {
257+
auto loc = RegularLocation::getAutoGeneratedLocation();
258+
SILBuilderWithScope builder(incomingValueOperand.getInst());
259+
originalValue = builder.createBeginBorrow(loc, originalValue);
260+
}
254261
incomingValueOperand.getOperand()->set(originalValue);
255262
}
256263

lib/SILOptimizer/SemanticARC/OwnershipLiveRange.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,16 @@ void OwnershipLiveRange::convertJoinedLiveRangePhiToGuaranteed(
310310
InstModCallbacks callbacks) && {
311311

312312
// First convert the phi value itself to be guaranteed.
313-
convertIntroducerToGuaranteed(introducer);
313+
SILValue phiValue = convertIntroducerToGuaranteed(introducer);
314+
315+
// Then insert end_borrows at each of our destroys if we are consuming. We
316+
// have to convert the phi to guaranteed first since otherwise, the ownership
317+
// check when we create the end_borrows will trigger.
318+
if (auto *phi = dyn_cast<SILPhiArgument>(phiValue)) {
319+
if (!isGuaranteedForwardingPhi(phi)) {
320+
insertEndBorrowsAtDestroys(phiValue, deadEndBlocks, scratch);
321+
}
322+
}
314323

315324
// Then eliminate all of the destroys...
316325
while (!destroyingUses.empty()) {

lib/SILOptimizer/Transforms/ConditionForwarding.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ bool ConditionForwarding::tryOptimize(SwitchEnumInst *SEI) {
202202
if (!EI)
203203
return false;
204204

205+
if (getFunction()->hasOwnership() && EI->hasOperand()) {
206+
auto some = EI->getOperand();
207+
if (some->getOwnershipKind() == OwnershipKind::Guaranteed &&
208+
isa<SILFunctionArgument>(some)) {
209+
return false;
210+
}
211+
}
212+
205213
if (CommonBranchBlock && PredPred != CommonBranchBlock)
206214
return false;
207215
CommonBranchBlock = PredPred;

test/SILOptimizer/conditionforwarding_nontrivial_ossa.sil

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ bb6:
7171
return %r : $()
7272
}
7373

74+
// Currently, this is not optimized away because enum has @guaranteed function
75+
// argument, which will need a borrow scope before being forwarded.
7476
// CHECK-LABEL: sil [ossa] @simple_forwarding2 :
75-
// CHECK-NOT: switch_enum
77+
// CHECK: switch_enum
7678
// CHECK-LABEL: } // end sil function 'simple_forwarding2'
7779
sil [ossa] @simple_forwarding2 : $@convention(thin) (Builtin.Int1, @guaranteed Klass) -> () {
7880
bb0(%0 : $Builtin.Int1, %1 : @guaranteed $Klass):
@@ -183,7 +185,6 @@ bb6:
183185

184186
// CHECK-LABEL: sil [ossa] @simple_switch_enum_forwarding2 :
185187
// CHECK: switch_enum
186-
// CHECK-NOT: switch_enum
187188
// CHECK-LABEL: } // end sil function 'simple_switch_enum_forwarding2'
188189
sil [ossa] @simple_switch_enum_forwarding2 : $@convention(thin) (@guaranteed E3) -> () {
189190
bb0(%0 : @guaranteed $E3):

test/SILOptimizer/ossa_rauw_tests.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ bb3:
673673
//
674674
// CHECK-LABEL: sil [ossa] @guaranteed_copy_rauw_owned : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> () {
675675
// CHECK-NOT: copy_value
676+
// CHECK: begin_borrow
677+
// CHECK-NOT: copy_value
676678
// CHECK: } // end sil function 'guaranteed_copy_rauw_owned'
677679
sil [ossa] @guaranteed_copy_rauw_owned : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> () {
678680
bb0(%0 : @guaranteed $FakeOptional<Klass>):

0 commit comments

Comments
 (0)