Skip to content

Commit d72ad28

Browse files
committed
Revert "Improve funciton signature @owned return result to "not owned" conversion"
This reverts commit c6de7c0. Broke OSS linux and OSX builds.
1 parent c6de7c0 commit d72ad28

File tree

4 files changed

+25
-134
lines changed

4 files changed

+25
-134
lines changed

include/swift/SILOptimizer/Analysis/ARCAnalysis.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ class ConsumedReturnValueToEpilogueRetainMatcher {
114114
public:
115115
enum class ExitKind { Return, Throw };
116116

117-
/// The state on how retains are found in a basic block.
118-
enum class FindRetainKind {
119-
None, ///< Did not find a retain.
120-
Found, ///< Found a retain.
121-
Recursion, ///< Found a retain and its due to self-recursion.
122-
Blocked ///< Found a blocking instructions, i.e. MayDecrement.
123-
};
117+
enum class FindRetainKind { None, Found, Blocked };
124118

125119
using RetainKindValue = std::pair<FindRetainKind, SILInstruction *>;
126120

lib/SILOptimizer/Analysis/ARCAnalysis.cpp

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@
2626

2727
using namespace swift;
2828

29-
using BasicBlockRetainValue = std::pair<SILBasicBlock *, SILValue>;
30-
31-
32-
//===----------------------------------------------------------------------===//
33-
// Utilities
34-
//===----------------------------------------------------------------------===//
35-
36-
static SILValue
37-
findIncomingValue(SILBasicBlock *BB,
38-
llvm::SmallVector<std::pair<SILBasicBlock *, SILValue>, 2> Values) {
39-
for (auto &X : Values)
40-
if (X.first == BB)
41-
return X.second;
42-
return SILValue();
43-
}
44-
4529
//===----------------------------------------------------------------------===//
4630
// Decrement Analysis
4731
//===----------------------------------------------------------------------===//
@@ -497,6 +481,7 @@ void ConsumedReturnValueToEpilogueRetainMatcher::recompute() {
497481
findMatchingRetains(&*BB);
498482
}
499483

484+
500485
void
501486
ConsumedReturnValueToEpilogueRetainMatcher::
502487
findMatchingRetains(SILBasicBlock *BB) {
@@ -517,69 +502,47 @@ findMatchingRetains(SILBasicBlock *BB) {
517502
// OK. we've found the return value, now iterate on the CFG to find all the
518503
// post-dominating retains.
519504
constexpr unsigned WorkListMaxSize = 8;
520-
505+
RV = RCFI->getRCIdentityRoot(RV);
521506
llvm::DenseSet<SILBasicBlock *> RetainFrees;
522-
llvm::SmallVector<BasicBlockRetainValue, 4> WorkList;
507+
llvm::SmallVector<SILBasicBlock *, 4> WorkList;
523508
llvm::DenseSet<SILBasicBlock *> HandledBBs;
524-
WorkList.push_back(std::make_pair(BB, RV));
509+
WorkList.push_back(BB);
525510
HandledBBs.insert(BB);
526511
while (!WorkList.empty()) {
512+
auto *CBB = WorkList.pop_back_val();
513+
RetainKindValue Kind = findMatchingRetainsInner(CBB, RV);
514+
527515
// Too many blocks ?.
528516
if (WorkList.size() > WorkListMaxSize) {
529517
EpilogueRetainInsts.clear();
530518
return;
531519
}
532520

533-
// Try to find a retain %value in this basic block.
534-
auto BVP = WorkList.pop_back_val();
535-
RetainKindValue Kind = findMatchingRetainsInner(BVP.first, BVP.second);
536-
537-
// We've found a retain on this path.
538-
if (Kind.first == FindRetainKind::Found) {
539-
EpilogueRetainInsts.push_back(Kind.second);
540-
continue;
541-
}
542-
543521
// There is a MayDecrement instruction.
544522
if (Kind.first == FindRetainKind::Blocked)
545523
return;
546-
547-
// There is a self-recursion. Use the apply instruction as the retain.
548-
if (Kind.first == FindRetainKind::Recursion) {
549-
EpilogueRetainInsts.push_back(Kind.second);
550-
continue;
551-
}
552524

553-
// Did not find a retain in this block, try to go to its predecessors.
554525
if (Kind.first == FindRetainKind::None) {
526+
RetainFrees.insert(CBB);
527+
555528
// We can not find a retain in a block with no predecessors.
556-
if (BVP.first->getPreds().begin() == BVP.first->getPreds().end()) {
529+
if (CBB->getPreds().begin() == CBB->getPreds().end()) {
557530
EpilogueRetainInsts.clear();
558531
return;
559532
}
560533

561-
// This block does not have a retain.
562-
RetainFrees.insert(BVP.first);
563-
564534
// Check the predecessors.
565-
bool FoundArgValues = false;
566-
llvm::SmallVector<std::pair<SILBasicBlock *, SILValue>, 2> IncomingValues;
567-
if (auto *SA = dyn_cast<SILArgument>(BVP.second))
568-
FoundArgValues = SA->getIncomingValues(IncomingValues);
569-
570-
for (auto X : BVP.first->getPreds()){
535+
for (auto X : CBB->getPreds()){
571536
if (HandledBBs.find(X) != HandledBBs.end())
572537
continue;
573-
574-
// Try to use the predecessor edge-value.
575-
if (FoundArgValues)
576-
WorkList.push_back(std::make_pair(X, findIncomingValue(X, IncomingValues)));
577-
else
578-
WorkList.push_back(std::make_pair(X, BVP.second));
579-
538+
WorkList.push_back(X);
580539
HandledBBs.insert(X);
581540
}
582541
}
542+
543+
// We've found a retain on this path.
544+
if (Kind.first == FindRetainKind::Found)
545+
EpilogueRetainInsts.push_back(Kind.second);
583546
}
584547

585548
// For every block with retain, we need to check the transitive
@@ -610,11 +573,6 @@ ConsumedReturnValueToEpilogueRetainMatcher::RetainKindValue
610573
ConsumedReturnValueToEpilogueRetainMatcher::
611574
findMatchingRetainsInner(SILBasicBlock *BB, SILValue V) {
612575
for (auto II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
613-
// Handle self-recursion.
614-
if (ApplyInst *AI = dyn_cast<ApplyInst>(&*II))
615-
if (AI->getCalleeFunction() == BB->getParent())
616-
return std::make_pair(FindRetainKind::Recursion, AI);
617-
618576
// If we do not have a retain_value or strong_retain...
619577
if (!isa<RetainValueInst>(*II) && !isa<StrongRetainInst>(*II)) {
620578
// we can ignore it if it can not decrement the reference count of the

lib/SILOptimizer/IPO/FunctionSignatureOpts.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ using namespace swift;
4141
STATISTIC(NumFunctionSignaturesOptimized, "Total func sig optimized");
4242
STATISTIC(NumDeadArgsEliminated, "Total dead args eliminated");
4343
STATISTIC(NumOwnedConvertedToGuaranteed, "Total owned args -> guaranteed args");
44-
STATISTIC(NumOwnedConvertedToNotOwnedResult, "Total owned result -> not owned result");
44+
STATISTIC(NumOwnedConvertedToGuaranteedReturnValue, "Total owned args -> guaranteed return value");
4545
STATISTIC(NumCallSitesOptimized, "Total call sites optimized");
4646
STATISTIC(NumSROAArguments, "Total SROA arguments optimized");
4747

@@ -75,7 +75,6 @@ static bool hasIdenticalReleases(ReleaseList LHS, ReleaseList RHS) {
7575
return true;
7676
}
7777

78-
7978
/// Returns .Some(I) if I is a release that is the only non-debug instruction
8079
/// with side-effects in the use-def graph originating from Arg. Returns
8180
/// .Some(nullptr), if all uses from the arg were either debug insts or do not
@@ -655,7 +654,7 @@ bool SignatureAnalyzer::analyze() {
655654
if (!Retains.empty()) {
656655
RI.CalleeRetain = Retains;
657656
ShouldOptimize = true;
658-
++NumOwnedConvertedToNotOwnedResult;
657+
++NumOwnedConvertedToGuaranteedReturnValue;
659658
}
660659
}
661660

@@ -782,21 +781,11 @@ SILFunction *SignatureOptimizer::createEmptyFunctionWithOptimizedSig(
782781
static void addRetainsForConvertedDirectResults(SILBuilder &Builder,
783782
SILLocation Loc,
784783
SILValue ReturnValue,
785-
SILInstruction *AI,
786784
ArrayRef<ResultDescriptor> DirectResults) {
787785
for (auto I : indices(DirectResults)) {
788786
auto &RV = DirectResults[I];
789787
if (RV.CalleeRetain.empty()) continue;
790788

791-
bool IsSelfRecursionEpilogueRetain = false;
792-
for (auto &X : RV.CalleeRetain) {
793-
IsSelfRecursionEpilogueRetain |= (AI == X);
794-
}
795-
796-
// We do not create an retain if this ApplyInst is a self-recursion.
797-
if (IsSelfRecursionEpilogueRetain)
798-
continue;
799-
800789
// Extract the return value if necessary.
801790
SILValue SpecificResultValue = ReturnValue;
802791
if (DirectResults.size() != 1)
@@ -815,7 +804,6 @@ static void addRetainsForConvertedDirectResults(SILBuilder &Builder,
815804
static void rewriteApplyInstToCallNewFunction(SignatureOptimizer &Optimizer,
816805
SILFunction *NewF,
817806
const ApplyList &CallSites) {
818-
llvm::DenseSet<SILInstruction *> ApplysToRemove;
819807
for (auto FAS : CallSites) {
820808
auto *AI = FAS.getInstruction();
821809

@@ -877,19 +865,14 @@ static void rewriteApplyInstToCallNewFunction(SignatureOptimizer &Optimizer,
877865

878866
// If we have converted the return value from @owned to @guaranteed,
879867
// insert a retain_value at the callsite.
880-
addRetainsForConvertedDirectResults(Builder, Loc, ReturnValue, AI,
868+
addRetainsForConvertedDirectResults(Builder, Loc, ReturnValue,
881869
Optimizer.getResultDescList());
882-
883-
// Make sure we remove this apply in the end.
884-
ApplysToRemove.insert(AI);
885-
++NumCallSitesOptimized;
886-
}
887-
888-
// Lastly, we have rewritten all the callsites, erase the old applys and
889-
// its callee.
890-
for (auto *AI : ApplysToRemove) {
870+
871+
// Erase the old apply and its callee.
891872
recursivelyDeleteTriviallyDeadInstructions(AI, true,
892873
[](SILInstruction *) {});
874+
875+
++NumCallSitesOptimized;
893876
}
894877
}
895878

@@ -954,7 +937,7 @@ static void createThunkBody(SILBasicBlock *BB, SILFunction *NewF,
954937
}
955938

956939
// Handle @owned to @unowned return value conversion.
957-
addRetainsForConvertedDirectResults(Builder, Loc, ReturnValue, nullptr,
940+
addRetainsForConvertedDirectResults(Builder, Loc, ReturnValue,
958941
Optimizer.getResultDescList());
959942

960943
// Function that are marked as @NoReturn must be followed by an 'unreachable'
@@ -1073,8 +1056,6 @@ static bool optimizeFunctionSignature(llvm::BumpPtrAllocator &BPA,
10731056
// to unowned conversion.
10741057
for (ResultDescriptor &RD : Optimizer.getResultDescList()) {
10751058
for (auto &X : RD.CalleeRetain) {
1076-
if (!isa<StrongRetainInst>(X) && !isa<RetainValueInst>(X))
1077-
continue;
10781059
X->eraseFromParent();
10791060
}
10801061
}

test/SILOptimizer/functionsigopts.sil

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,6 @@ bb0(%0 : $boo):
112112
return %0 : $boo
113113
}
114114

115-
116-
// CHECK-LABEL: sil [thunk] @single_owned_return_value_with_self_recursion
117-
// CHECK: function_ref @_TTSf4n_g_single_owned_return_value
118-
// CHECK: [[RET:%.*]] = apply
119-
// CHECK: retain_value [[RET]]
120-
sil @single_owned_return_value_with_self_recursion : $@convention(thin) (@owned boo) -> @owned boo {
121-
bb0(%0 : $boo):
122-
cond_br undef, bb1, bb2
123-
bb1:
124-
retain_value %0 : $boo
125-
br bb3(%0 : $boo)
126-
bb2:
127-
%2 = function_ref @single_owned_return_value_with_self_recursion : $@convention(thin) (@owned boo) -> @owned boo
128-
%3 = apply %2(%0) : $@convention(thin) (@owned boo) -> @owned boo
129-
br bb3 (%3 : $boo)
130-
bb3(%4 : $boo):
131-
return %4 : $boo
132-
}
133-
134115
// CHECK-LABEL: sil [thunk] @single_owned_return_value_with_interfering_release
135116
// CHECK: bb0([[INPUT_ARG0:%[0-9]+]] : $boo):
136117
// CHECK: [[IN1:%.*]] = function_ref @_TTSf4s__single_owned_return_value_with_interfering_release
@@ -264,22 +245,6 @@ bb0(%0 : $boo):
264245
return %5 : $()
265246
}
266247

267-
// Make sure we pull out the retain_value from the callee.
268-
//
269-
// The retain and release can be got rid of easily by arc this way.
270-
//
271-
// CHECK-LABEL: sil @single_owned_return_value_with_self_recursion_callsite
272-
// CHECK: [[RET:%.*]] = apply
273-
// CHECK: retain_value [[RET]]
274-
// CHECK: release_value [[RET]]
275-
sil @single_owned_return_value_with_self_recursion_callsite : $@convention(thin) (@owned boo) -> () {
276-
bb0(%0 : $boo):
277-
%2 = function_ref @single_owned_return_value_with_self_recursion : $@convention(thin) (@owned boo) -> @owned boo
278-
%4 = apply %2(%0) : $@convention(thin) (@owned boo) -> @owned boo
279-
release_value %4 : $boo
280-
%5 = tuple()
281-
return %5 : $()
282-
}
283248

284249
sil [fragile] @exploded_release_to_dead_param_callsite : $@convention(thin) (@owned boo) -> () {
285250
bb0(%0 : $boo):
@@ -683,13 +648,6 @@ bb0(%0 : $Builtin.Int32):
683648
// CHECK-NOT: retain_value
684649
// CHECK: return
685650

686-
// There should not be a single retain in this function.
687-
//
688-
// CHECK-LABEL: sil @_TTSf4n_g_single_owned_return_value_with_self_recursion : $@convention(thin) (@owned boo) -> boo
689-
// CHECK: bb0
690-
// CHECK-NOT: retain_value
691-
// CHECK: return
692-
693651
// CHECK-LABEL: sil @_TTSf4s__single_owned_return_value_with_interfering_release : $@convention(thin) (@owned foo, @owned foo, Int) -> @owned boo {
694652
// CHECK: retain_value
695653
// CHECK: release_value

0 commit comments

Comments
 (0)