Skip to content

Commit f445f4f

Browse files
committed
Optimizer: create and update borrowed-from instructions in C++ optimizer passes
1 parent 2572539 commit f445f4f

File tree

12 files changed

+32
-17
lines changed

12 files changed

+32
-17
lines changed

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
209209
// If available, the current DeadEndBlocks for incremental update.
210210
DeadEndBlocks *deBlocks;
211211

212+
SILPassManager *pm;
213+
212214
public:
213215
/// An ordered list of old to new available value pairs.
214216
///
@@ -217,8 +219,8 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
217219
SmallVector<std::pair<SILValue, SILValue>, 16> availVals;
218220

219221
// Clone blocks starting at `origBB`, within the same function.
220-
BasicBlockCloner(SILBasicBlock *origBB, DeadEndBlocks *deBlocks = nullptr)
221-
: SILCloner(*origBB->getParent()), origBB(origBB), deBlocks(deBlocks) {}
222+
BasicBlockCloner(SILBasicBlock *origBB, SILPassManager *pm, DeadEndBlocks *deBlocks = nullptr)
223+
: SILCloner(*origBB->getParent()), origBB(origBB), deBlocks(deBlocks), pm(pm) {}
222224

223225
bool canCloneBlock() {
224226
for (auto &inst : *origBB) {

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ SILLinkage getSpecializedLinkage(SILFunction *f, SILLinkage linkage);
231231
/// Tries to perform jump-threading on all checked_cast_br instruction in
232232
/// function \p Fn.
233233
bool tryCheckedCastBrJumpThreading(
234-
SILFunction *fn, DominanceInfo *dt, DeadEndBlocks *deBlocks,
234+
SILFunction *fn, SILPassManager *pm, DominanceInfo *dt, DeadEndBlocks *deBlocks,
235235
SmallVectorImpl<SILBasicBlock *> &blocksForWorklist,
236236
bool EnableOSSARewriteTerminator);
237237

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/SILOptimizer/PassManager/Transforms.h"
2727
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
2828
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
29+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
2930
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
3031
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
3132
#include "swift/SILOptimizer/Utils/StackNesting.h"
@@ -1480,6 +1481,7 @@ class ClosureLifetimeFixup : public SILFunctionTransform {
14801481

14811482
if (fixupClosureLifetimes(*getFunction(), dominanceAnalysis,
14821483
checkStackNesting, modifiedCFG)) {
1484+
updateBorrowedFrom(getPassManager(), getFunction());
14831485
if (checkStackNesting){
14841486
modifiedCFG |=
14851487
StackNesting::fixNesting(getFunction()) == StackNesting::Changes::CFG;

lib/SILOptimizer/SemanticARC/Context.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace semanticarc {
3131

3232
struct LLVM_LIBRARY_VISIBILITY Context {
3333
SILFunction &fn;
34+
SILPassManager *pm = nullptr;
3435
ARCTransformKind transformKind = ARCTransformKind::All;
3536
DeadEndBlocks &deadEndBlocks;
3637
ValueLifetimeAnalysis::Frontier lifetimeFrontier;
@@ -119,9 +120,9 @@ struct LLVM_LIBRARY_VISIBILITY Context {
119120

120121
DeadEndBlocks &getDeadEndBlocks() { return deadEndBlocks; }
121122

122-
Context(SILFunction &fn, DeadEndBlocks &deBlocks, bool onlyMandatoryOpts,
123+
Context(SILFunction &fn, SILPassManager *pm, DeadEndBlocks &deBlocks, bool onlyMandatoryOpts,
123124
InstModCallbacks callbacks)
124-
: fn(fn), deadEndBlocks(deBlocks), lifetimeFrontier(),
125+
: fn(fn), pm(pm), deadEndBlocks(deBlocks), lifetimeFrontier(),
125126
addressToExhaustiveWriteListCache(constructCacheValue),
126127
onlyMandatoryOpts(onlyMandatoryOpts), instModCallbacks(callbacks) {}
127128

lib/SILOptimizer/SemanticARC/OwnedToGuaranteedPhiOpt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Transforms.h"
2222
#include "swift/Basic/Defer.h"
2323
#include "swift/Basic/STLExtras.h"
24+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
2425

2526
using namespace swift;
2627
using namespace swift::semanticarc;
@@ -265,5 +266,8 @@ bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
265266
ctx.verify();
266267
}
267268

269+
if (madeChange)
270+
updateBorrowedFrom(ctx.pm, &ctx.fn);
271+
268272
return madeChange;
269273
}

lib/SILOptimizer/SemanticARC/SemanticARCOptVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ struct LLVM_LIBRARY_VISIBILITY SemanticARCOptVisitor
4949

5050
Context ctx;
5151

52-
explicit SemanticARCOptVisitor(SILFunction &fn, DeadEndBlocks &deBlocks,
52+
explicit SemanticARCOptVisitor(SILFunction &fn, SILPassManager *pm, DeadEndBlocks &deBlocks,
5353
bool onlyMandatoryOpts)
54-
: ctx(fn, deBlocks, onlyMandatoryOpts,
54+
: ctx(fn, pm, deBlocks, onlyMandatoryOpts,
5555
InstModCallbacks()
5656
.onDelete(
5757
[this](SILInstruction *inst) { eraseInstruction(inst); })

lib/SILOptimizer/SemanticARC/SemanticARCOpts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct SemanticARCOpts : SILFunctionTransform {
157157
"verification is enabled");
158158

159159
auto *deBlocksAnalysis = getAnalysis<DeadEndBlocksAnalysis>();
160-
SemanticARCOptVisitor visitor(f, *deBlocksAnalysis->get(&f),
160+
SemanticARCOptVisitor visitor(f, getPassManager(), *deBlocksAnalysis->get(&f),
161161
mandatoryOptsOnly);
162162

163163
#ifndef NDEBUG

lib/SILOptimizer/Transforms/ConditionForwarding.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class ConditionForwarding : public SILFunctionTransform {
116116
}
117117
}
118118
if (Changed) {
119+
updateBorrowedFrom(getPassManager(), F);
119120
invalidateAnalysis(SILAnalysis::InvalidationKind::BranchesAndInstructions);
120121
}
121122
}

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
2222
#include "swift/SILOptimizer/Utils/Devirtualize.h"
2323
#include "swift/SILOptimizer/Utils/Generics.h"
24+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
2425
#include "swift/SILOptimizer/Utils/PerformanceInlinerUtils.h"
2526
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
2627
#include "swift/SILOptimizer/Utils/StackNesting.h"
@@ -1206,6 +1207,7 @@ bool SILPerformanceInliner::inlineCallsIntoFunction(SILFunction *Caller) {
12061207
if (invalidatedStackNesting) {
12071208
StackNesting::fixNesting(Caller);
12081209
}
1210+
updateBorrowedFrom(pm, Caller);
12091211

12101212
// If we were asked to verify our caller after inlining all callees we could
12111213
// find into it, do so now. This makes it easier to catch verification bugs in

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
180180
<< " to bb" << ti.Dest->getDebugID() << '\n');
181181
auto *SrcTerm = cast<BranchInst>(ti.Src->getTerminator());
182182

183-
BasicBlockCloner Cloner(SrcTerm->getDestBB());
183+
BasicBlockCloner Cloner(SrcTerm->getDestBB(), PM);
184184
if (!Cloner.canCloneBlock())
185185
return false;
186186

@@ -571,7 +571,7 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
571571
// Do dominator based simplification of terminator condition. This does not
572572
// and MUST NOT change the CFG without updating the dominator tree to
573573
// reflect such change.
574-
if (tryCheckedCastBrJumpThreading(&Fn, DT, deBlocks, BlocksForWorklist,
574+
if (tryCheckedCastBrJumpThreading(&Fn, PM, DT, deBlocks, BlocksForWorklist,
575575
EnableOSSACheckedCastBrJumpThreading)) {
576576
for (auto BB: BlocksForWorklist)
577577
addToWorklist(BB);
@@ -1023,7 +1023,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
10231023
// If it looks potentially interesting, decide whether we *can* do the
10241024
// operation and whether the block is small enough to be worth duplicating.
10251025
int copyCosts = 0;
1026-
BasicBlockCloner Cloner(DestBB);
1026+
BasicBlockCloner Cloner(DestBB, PM);
10271027
for (auto &inst : *DestBB) {
10281028
copyCosts += getThreadingCost(&inst);
10291029
if (ThreadingBudget <= copyCosts)
@@ -2985,7 +2985,7 @@ bool SimplifyCFG::tailDuplicateObjCMethodCallSuccessorBlocks() {
29852985
// Okay, it looks like we want to do this and we can. Duplicate the
29862986
// destination block into this one, rewriting uses of the BBArgs to use the
29872987
// branch arguments as we go.
2988-
BasicBlockCloner Cloner(DestBB);
2988+
BasicBlockCloner Cloner(DestBB, PM);
29892989
if (!Cloner.canCloneBlock())
29902990
continue;
29912991

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ void BasicBlockCloner::updateSSAAfterCloning() {
192192
ssaUpdater.rewriteUse(*use);
193193
}
194194
}
195+
updateBorrowedFromPhis(pm, updateSSAPhis);
195196
}
196197

197198
void BasicBlockCloner::sinkAddressProjections() {

lib/SILOptimizer/Utils/CheckedCastBrJumpThreading.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class CheckedCastBrJumpThreading {
5050
// need to be recomputed each time tryCheckedCastBrJumpThreading is called.
5151
DeadEndBlocks *deBlocks;
5252

53+
SILPassManager *pm;
54+
5355
// Enable non-trivial terminator rewriting in OSSA.
5456
bool EnableOSSARewriteTerminator;
5557

@@ -141,10 +143,10 @@ class CheckedCastBrJumpThreading {
141143

142144
public:
143145
CheckedCastBrJumpThreading(
144-
SILFunction *Fn, DominanceInfo *DT, DeadEndBlocks *deBlocks,
146+
SILFunction *Fn, SILPassManager *pm, DominanceInfo *DT, DeadEndBlocks *deBlocks,
145147
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
146148
bool EnableOSSARewriteTerminator)
147-
: Fn(Fn), DT(DT), deBlocks(deBlocks),
149+
: Fn(Fn), DT(DT), deBlocks(deBlocks), pm(pm),
148150
EnableOSSARewriteTerminator(EnableOSSARewriteTerminator),
149151
rauwContext(callbacks, *deBlocks),
150152
BlocksForWorklist(BlocksForWorklist), BlocksToEdit(Fn),
@@ -772,7 +774,7 @@ void CheckedCastBrJumpThreading::optimizeFunction() {
772774
if (edit->SuccessArg->isErased())
773775
continue;
774776

775-
BasicBlockCloner Cloner(edit->CCBBlock, deBlocks);
777+
BasicBlockCloner Cloner(edit->CCBBlock, pm, deBlocks);
776778
if (!Cloner.canCloneBlock())
777779
continue;
778780

@@ -800,7 +802,7 @@ void CheckedCastBrJumpThreading::optimizeFunction() {
800802
namespace swift {
801803

802804
bool tryCheckedCastBrJumpThreading(
803-
SILFunction *Fn, DominanceInfo *DT, DeadEndBlocks *deBlocks,
805+
SILFunction *Fn, SILPassManager *pm, DominanceInfo *DT, DeadEndBlocks *deBlocks,
804806
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
805807
bool EnableOSSARewriteTerminator) {
806808

@@ -809,7 +811,7 @@ bool tryCheckedCastBrJumpThreading(
809811
return false;
810812
}
811813

812-
CheckedCastBrJumpThreading CCBJumpThreading(Fn, DT, deBlocks,
814+
CheckedCastBrJumpThreading CCBJumpThreading(Fn, pm, DT, deBlocks,
813815
BlocksForWorklist,
814816
EnableOSSARewriteTerminator);
815817
CCBJumpThreading.optimizeFunction();

0 commit comments

Comments
 (0)