Skip to content

Commit d66d329

Browse files
committed
[diagnose-unreachable] Adjust test cases for movable guaranteed phis.
The main change is that we do not eliminate end_borrows when propagating guaranteed phis. This is because phis now forward guaranteed ownership like owned ownership and since we only eliminate these arguments if all incomign values to the argument is the same (providing dominance).
1 parent 1fb56db commit d66d329

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,14 @@ class UnreachableUserCodeReportingState {
9191
llvm::DenseMap<const SILBasicBlock*, UnreachableInfo> MetaMap;
9292
};
9393

94-
static void deleteEndBorrows(SILValue v) {
95-
SmallVector<SILInstruction *, 4> endBorrowList;
96-
for (auto *use : v->getUses()) {
97-
if (auto *ebi = dyn_cast<EndBorrowInst>(use->getUser())) {
98-
endBorrowList.push_back(ebi);
99-
}
100-
}
101-
while (!endBorrowList.empty()) {
102-
endBorrowList.pop_back_val()->eraseFromParent();
103-
}
104-
}
105-
10694
/// Propagate/remove basic block input values when all predecessors
10795
/// supply the same arguments.
96+
///
97+
/// NOTE: Since BranchInst always forwards guaranteed and owned parameters the
98+
/// same way (like owned parameters), we do not need to add any special handling
99+
/// for guaranteed parameters here. This is because if all of the incoming
100+
/// values into my guaranteed phi is the same, then we know that said incoming
101+
/// value must dominate the phi by definition.
108102
static void propagateBasicBlockArgs(SILBasicBlock &BB) {
109103
// This functions would simplify the code as following:
110104
//
@@ -185,10 +179,6 @@ static void propagateBasicBlockArgs(SILBasicBlock &BB) {
185179
// this to CCP and trigger another round of copy propagation.
186180
SILArgument *Arg = *AI;
187181

188-
// If this argument is guaranteed and Args[Idx], delete the end_borrow.
189-
if (Arg->getOwnershipKind() == ValueOwnershipKind::Guaranteed)
190-
deleteEndBorrows(Arg);
191-
192182
// We were able to fold, so all users should use the new folded value.
193183
Arg->replaceAllUsesWith(Args[Idx]);
194184
NumBasicBlockArgsPropagated++;

test/SILOptimizer/diagnose_unreachable.sil

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,20 @@ bb3:
516516
return %9999 : $()
517517
}
518518

519+
// Rather than eliminate the end_borrow, we just propagate through since
520+
// branches now act like phis. We could eliminate the begin_borrow scope though,
521+
// but this pass does not perform that simplification.
522+
//
519523
// CHECK-LABEL: sil [ossa] @eliminate_end_borrow_when_propagating_bb_args : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
520-
// CHECK-NOT: end_borrow
524+
// CHECK: %1 = begin_borrow %0
521525
// CHECK: [[FUNC_REF:%.*]] = function_ref @guaranteed_nativeobject_user
522-
// CHECK-NEXT: apply [[FUNC_REF]](%0) :
523-
// CHECK-NOT: end_borrow
526+
// CHECK-NEXT: apply [[FUNC_REF]](%1) :
527+
// CHECK: end_borrow %1
524528
// CHECK: } // end sil function 'eliminate_end_borrow_when_propagating_bb_args'
525529
sil [ossa] @eliminate_end_borrow_when_propagating_bb_args : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
526530
bb0(%0 : @guaranteed $Builtin.NativeObject):
527-
br bb1(%0 : $Builtin.NativeObject)
531+
%0a = begin_borrow %0 : $Builtin.NativeObject
532+
br bb1(%0a : $Builtin.NativeObject)
528533

529534
bb1(%1 : @guaranteed $Builtin.NativeObject):
530535
%2 = function_ref @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
@@ -788,20 +793,20 @@ enum EnumWithB {
788793
func testit() -> Int
789794
}
790795

791-
// Test propagation of guaranteed phi arguments. The nested end_borrow
792-
// must be removed, even with the outer borrow is *not* a function
793-
// argument.
796+
// Test propagation of guaranteed phi arguments. The nested end_borrow should
797+
// not be removed since guaranteed phis propagate now.
794798
//
795799
// CHECK-LABEL: sil hidden [ossa] @testEliminatePropagateGuaranteedPhiAfterSwitch : $@convention(method) (@guaranteed EnumWithB) -> () {
796800
// CHECK: bb1([[PHI:%.*]] : @guaranteed $B):
797-
// CHECK-NOT: end_borrow
801+
// CHECK: end_borrow
798802
// CHECK-LABEL: } // end sil function 'testEliminatePropagateGuaranteedPhiAfterSwitch'
799803
sil hidden [ossa] @testEliminatePropagateGuaranteedPhiAfterSwitch : $@convention(method) (@guaranteed EnumWithB) -> () {
800804
bb0(%0 : @guaranteed $EnumWithB):
801805
switch_enum %0 : $EnumWithB, case #EnumWithB.A!enumelt.1: bb1
802806

803807
bb1(%2 : @guaranteed $B):
804-
br bb3(%2 : $B)
808+
%2a = begin_borrow %2 : $B
809+
br bb3(%2a : $B)
805810

806811
bb3(%4 : @guaranteed $B):
807812
%99 = tuple ()
@@ -811,8 +816,9 @@ bb3(%4 : @guaranteed $B):
811816

812817
// CHECK-LABEL: sil hidden [ossa] @testEliminatePropagateBeginBorrowGuaranteedPhiAfterSwitch : $@convention(method) (@owned B) -> () {
813818
// CHECK: [[BORROW:%.*]] = begin_borrow
819+
// CHECK: bb1:
820+
// CHECK: bb2:
814821
// CHECK: end_borrow [[BORROW]]
815-
// CHECK-NOT: end_borrow
816822
// CHECK-LABEL: } // end sil function 'testEliminatePropagateBeginBorrowGuaranteedPhiAfterSwitch'
817823
sil hidden [ossa] @testEliminatePropagateBeginBorrowGuaranteedPhiAfterSwitch : $@convention(method) (@owned B) -> () {
818824
bb0(%0 : @owned $B):
@@ -825,8 +831,6 @@ bb1(%2 : @guaranteed $B):
825831
bb3(%3 : @guaranteed $B):
826832
%99 = tuple ()
827833
end_borrow %3 : $B
828-
end_borrow %2 : $B
829-
end_borrow %1 : $B
830834
destroy_value %0 : $B
831835
return %99 : $()
832836
}
@@ -853,18 +857,18 @@ bb3(%4 : @guaranteed $B):
853857
// Make sure that we can handle iterated end_borrow.
854858
//
855859
// CHECK-LABEL: sil hidden [ossa] @testPropagateGuaranteedPhi : $@convention(method) (@guaranteed B) -> () {
856-
// CHECK-NOT: end_borrow
860+
// CHECK: end_borrow
857861
// CHECK-LABEL: } // end sil function 'testPropagateGuaranteedPhi'
858862
sil hidden [ossa] @testPropagateGuaranteedPhi : $@convention(method) (@guaranteed B) -> () {
859863
bb0(%0 : @guaranteed $B):
860-
br bb1(%0 : $B)
864+
%0a = begin_borrow %0 : $B
865+
br bb1(%0a : $B)
861866

862867
bb1(%1 : @guaranteed $B):
863868
br bb2(%1 : $B)
864869

865870
bb2(%2 : @guaranteed $B):
866871
%99 = tuple ()
867872
end_borrow %2 : $B
868-
end_borrow %1 : $B
869873
return %99 : $()
870874
}

0 commit comments

Comments
 (0)