Skip to content

Commit e0f0cd0

Browse files
committed
Update SemanticARCOpts for @guaranteed forwarding phi support
1 parent 6914758 commit e0f0cd0

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

lib/SILOptimizer/SemanticARC/OwnedToGuaranteedPhiOpt.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,6 @@ bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
242242
originalIncomingValues[pair.second] = pair.first;
243243
}
244244

245-
// Then convert the phi's live range to be guaranteed.
246-
std::move(joinedLiveRange)
247-
.convertJoinedLiveRangePhiToGuaranteed(
248-
ctx.getDeadEndBlocks(), ctx.lifetimeFrontier, ctx.instModCallbacks);
249-
250245
// Now if our phi operand consumes/forwards its guaranteed input, insert a
251246
// begin_borrow along the incoming value edges. We have to do this after
252247
// converting the incoming values to be guaranteed to avoid tripping
@@ -257,14 +252,20 @@ bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
257252
auto incomingValueOperand = incomingValueOperandList.pop_back_val();
258253
SILValue originalValue = originalIncomingValues.pop_back_val();
259254
if (incomingValueOperand.isGuaranteedConsuming() &&
260-
originalValue->getOwnershipKind() != OwnershipKind::None) {
255+
isa<SILFunctionArgument>(originalValue) &&
256+
originalValue->getOwnershipKind() == OwnershipKind::Guaranteed) {
261257
auto loc = RegularLocation::getAutoGeneratedLocation();
262258
SILBuilderWithScope builder(incomingValueOperand.getInst());
263259
originalValue = builder.createBeginBorrow(loc, originalValue);
264260
}
265261
incomingValueOperand.getOperand()->set(originalValue);
266262
}
267263

264+
// Then convert the phi's live range to be guaranteed.
265+
std::move(joinedLiveRange)
266+
.convertJoinedLiveRangePhiToGuaranteed(
267+
ctx.getDeadEndBlocks(), ctx.lifetimeFrontier, ctx.instModCallbacks);
268+
268269
madeChange = true;
269270
ctx.verify();
270271
}

lib/SILOptimizer/SemanticARC/OwnershipLiveRange.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ void OwnershipLiveRange::convertJoinedLiveRangePhiToGuaranteed(
315315
// Then insert end_borrows at each of our destroys if we are consuming. We
316316
// have to convert the phi to guaranteed first since otherwise, the ownership
317317
// check when we create the end_borrows will trigger.
318-
if (introducer.hasConsumingGuaranteedOperands()) {
319-
insertEndBorrowsAtDestroys(phiValue, deadEndBlocks, scratch);
318+
if (auto *phi = dyn_cast<SILPhiArgument>(phiValue)) {
319+
if (!isGuaranteedForwardingPhi(phi)) {
320+
insertEndBorrowsAtDestroys(phiValue, deadEndBlocks, scratch);
321+
}
320322
}
321323

322324
// Then eliminate all of the destroys...

test/SILOptimizer/semantic-arc-opts-canonical.sil

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,7 @@ bb3:
455455
//
456456
// CHECK: bb1([[CAST:%.*]] : @guaranteed $Klass):
457457
// CHECK: [[SOME:%.*]] = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, [[CAST]] : $Klass
458-
// CHECK: [[BORROW:%.*]] = begin_borrow [[SOME]] : $FakeOptional<Klass>
459-
// CHECK: br bb3([[BORROW]] : $FakeOptional<Klass>)
458+
// CHECK: br bb3([[SOME]] : $FakeOptional<Klass>)
460459
//
461460
// CHECK: bb2([[FAIL:%.*]] : @guaranteed $Klass):
462461
// CHECK-NEXT: [[NONE:%.*]] = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
@@ -466,11 +465,9 @@ bb3:
466465
// CHECK: switch_enum [[PHI]] : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb5, case #FakeOptional.none!enumelt: bb4
467466
//
468467
// CHECK: bb4:
469-
// CHECK-NEXT: end_borrow [[PHI]] : $FakeOptional<Klass>
470468
// CHECK-NEXT: br bb6
471469
//
472470
// CHECK: bb5(%{{.*}} : @guaranteed $Klass):
473-
// CHECK-NEXT: end_borrow [[PHI]] : $FakeOptional<Klass>
474471
// CHECK-NEXT: br bb6
475472
// CHECK-LABEL: } // end sil function 'testTypeDependentCheckCast'
476473
sil [ossa] @testTypeDependentCheckCast : $@convention(thin) (@guaranteed Klass, @thick @dynamic_self Klass.Type) -> () {

test/SILOptimizer/semantic-arc-opts.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,3 +1630,30 @@ exit:
16301630
%res = tuple ()
16311631
return %res : $()
16321632
}
1633+
1634+
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed_phi :
1635+
// CHECK-NOT: copy_value
1636+
// CHECK-LABEL: } // end sil function 'test_owned_to_guaranteed_phi'
1637+
sil [ossa] @test_owned_to_guaranteed_phi : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> () {
1638+
bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass):
1639+
%some = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
1640+
%copysome = copy_value %some : $FakeOptional<Klass>
1641+
switch_enum %copysome : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb2, case #FakeOptional.none!enumelt: bb3
1642+
1643+
bb2(%2 : @owned $Klass):
1644+
br bb4(%2 : $Klass)
1645+
1646+
bb3:
1647+
%3 = copy_value %1 : $Klass
1648+
br bb4(%3 : $Klass)
1649+
1650+
bb4(%4 : @owned $Klass):
1651+
%b = begin_borrow %4 : $Klass
1652+
%f = function_ref @guaranteed_klass_user : $@convention(thin) (@guaranteed Klass) -> ()
1653+
apply %f(%b) : $@convention(thin) (@guaranteed Klass) -> ()
1654+
end_borrow %b : $Klass
1655+
destroy_value %4 : $Klass
1656+
%t = tuple ()
1657+
return %t : $()
1658+
}
1659+

0 commit comments

Comments
 (0)