Skip to content

Commit 8d7ac53

Browse files
authored
Merge pull request #35545 from meg-gupta/looprotateossa
Enable LoopRotate on OSSA
2 parents b3bbf0c + 6c32cac commit 8d7ac53

15 files changed

+683
-26
lines changed

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SILSSAUpdater {
4949

5050
SILType type;
5151

52+
ValueOwnershipKind ownershipKind;
53+
5254
// The SSAUpdaterTraits specialization uses this sentinel to mark 'new' phi
5355
// nodes (all the incoming edge arguments have this sentinel set).
5456
std::unique_ptr<SILUndef, void (*)(SILUndef *)> phiSentinel;
@@ -69,8 +71,8 @@ class SILSSAUpdater {
6971
insertedPhis = inputInsertedPhis;
7072
}
7173

72-
/// Initialize for a use of a value of type.
73-
void initialize(SILType type);
74+
/// Initialize for a use of a value of type and ownershipKind
75+
void initialize(SILType type, ValueOwnershipKind ownershipKind);
7476

7577
bool hasValueForBlock(SILBasicBlock *block) const;
7678
void addAvailableValue(SILBasicBlock *block, SILValue value);

lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class RegionCloner : public SILCloner<RegionCloner> {
516516
return;
517517

518518
// Update SSA form.
519-
SSAUp.initialize(V->getType());
519+
SSAUp.initialize(V->getType(), V.getOwnershipKind());
520520
SSAUp.addAvailableValue(OrigBB, V);
521521
SILValue NewVal = getMappedValue(V);
522522
SSAUp.addAvailableValue(getOpBasicBlock(OrigBB), NewVal);

lib/SILOptimizer/LoopTransforms/LICM.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ hoistLoadsAndStores(AccessPath accessPath, SILLoop *loop) {
13261326

13271327
if (!storeAddr) {
13281328
storeAddr = SI->getDest();
1329-
ssaUpdater.initialize(storeAddr->getType().getObjectType());
1329+
ssaUpdater.initialize(storeAddr->getType().getObjectType(),
1330+
storeAddr.getOwnershipKind());
13301331
} else if (SI->getDest()->getType() != storeAddr->getType()) {
13311332
// This transformation assumes that the values of all stores in the loop
13321333
// must be interchangeable. It won't work if stores different types

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ using namespace swift;
3939
/// loops in the swift benchmarks).
4040
static llvm::cl::opt<int> LoopRotateSizeLimit("looprotate-size-limit",
4141
llvm::cl::init(20));
42+
static llvm::cl::opt<bool> RotateSingleBlockLoop("looprotate-single-block-loop",
43+
llvm::cl::init(false));
4244

4345
/// Check whether all operands are loop invariant.
4446
static bool
@@ -131,7 +133,7 @@ static void updateSSAForUseOfValue(
131133
assert(Res->getType() == MappedValue->getType() && "The types must match");
132134

133135
insertedPhis.clear();
134-
updater.initialize(Res->getType());
136+
updater.initialize(Res->getType(), Res.getOwnershipKind());
135137
updater.addAvailableValue(Header, Res);
136138
updater.addAvailableValue(EntryCheckBlock, MappedValue);
137139

@@ -228,9 +230,9 @@ static bool rotateLoopAtMostUpToLatch(SILLoop *loop, DominanceInfo *domInfo,
228230
return false;
229231
}
230232

231-
bool didRotate =
232-
rotateLoop(loop, domInfo, loopInfo, false /* rotateSingleBlockLoops */,
233-
latch, ShouldVerify);
233+
bool didRotate = rotateLoop(
234+
loop, domInfo, loopInfo,
235+
RotateSingleBlockLoop /* rotateSingleBlockLoops */, latch, ShouldVerify);
234236

235237
// Keep rotating at most until we hit the original latch.
236238
if (didRotate)
@@ -440,9 +442,6 @@ class LoopRotation : public SILFunctionTransform {
440442

441443
SILFunction *f = getFunction();
442444
assert(f);
443-
// FIXME: Add ownership support.
444-
if (f->hasOwnership())
445-
return;
446445

447446
SILLoopInfo *loopInfo = loopAnalysis->get(f);
448447
assert(loopInfo);

lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ updateSSA(SILModule &M, SILLoop *Loop,
335335
if (!Loop->contains(Use->getUser()->getParent()))
336336
UseList.push_back(UseWrapper(Use));
337337
// Update SSA of use with the available values.
338-
SSAUp.initialize(OrigValue->getType());
338+
SSAUp.initialize(OrigValue->getType(), OrigValue.getOwnershipKind());
339339
SSAUp.addAvailableValue(OrigValue->getParentBlock(), OrigValue);
340340
for (auto NewValue : MapEntry.second)
341341
SSAUp.addAvailableValue(NewValue->getParentBlock(), NewValue);

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
245245
// lifetime respecting loops.
246246
SmallVector<SILPhiArgument *, 8> insertedPhis;
247247
SILSSAUpdater updater(&insertedPhis);
248-
updater.initialize(optionalEscapingClosureTy);
248+
updater.initialize(optionalEscapingClosureTy, fn.hasOwnership()
249+
? OwnershipKind::Owned
250+
: OwnershipKind::None);
249251

250252
// Create an Optional<() -> ()>.none in the entry block of the function and
251253
// add it as an available value to the SSAUpdater.
@@ -850,7 +852,9 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
850852

851853
SmallVector<SILPhiArgument *, 8> insertedPhis;
852854
SILSSAUpdater updater(&insertedPhis);
853-
updater.initialize(optionalEscapingClosureTy);
855+
updater.initialize(optionalEscapingClosureTy, fn.hasOwnership()
856+
? OwnershipKind::Owned
857+
: OwnershipKind::None);
854858

855859
// Create the Optional.none as the beginning available value.
856860
SILValue entryBlockOptionalNone;

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
684684
// have multiple insertion points if we are storing exactly the same value
685685
// implying that we can just copy firstVal at each insertion point.
686686
SILSSAUpdater updater(&insertedPhiNodes);
687-
updater.initialize(loadTy);
687+
updater.initialize(loadTy, B.hasOwnership() ? OwnershipKind::Owned
688+
: OwnershipKind::None);
688689

689690
Optional<SILValue> singularValue;
690691
for (auto *insertPt : insertPts) {
@@ -860,7 +861,8 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
860861
// never have the same value along all paths unless we have a trivial value
861862
// meaning the SSA updater given a non-trivial value must /always/ be used.
862863
SILSSAUpdater updater(&insertedPhiNodes);
863-
updater.initialize(loadTy);
864+
updater.initialize(loadTy, B.hasOwnership() ? OwnershipKind::Owned
865+
: OwnershipKind::None);
864866

865867
Optional<SILValue> singularValue;
866868
for (auto *i : insertPts) {

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ bool DCE::removeDead(SILFunction &F) {
524524

525525
arg->replaceAllUsesWithUndef();
526526

527-
if (!F.hasOwnership() || arg->getType().isTrivial(F)) {
527+
if (!F.hasOwnership() || arg->getOwnershipKind() == OwnershipKind::None) {
528528
i++;
529529
Changed = true;
530530
BranchesChanged = true;

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ static void insertReleases(ArrayRef<StoreInst*> Stores,
559559
assert(!Stores.empty());
560560
SILValue StVal = Stores.front()->getSrc();
561561

562-
SSAUp.initialize(StVal->getType());
562+
SSAUp.initialize(StVal->getType(), StVal.getOwnershipKind());
563563

564564
for (auto *Store : Stores)
565565
SSAUp.addAvailableValue(Store->getParent(), Store->getSrc());

lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,9 @@ SILValue RLEContext::computePredecessorLocationValue(SILBasicBlock *BB,
13491349
// the SSAUpdater.
13501350
Updater.initialize(
13511351
L.getType(&BB->getModule(), TypeExpansionContext(*BB->getParent()))
1352-
.getObjectType());
1352+
.getObjectType(),
1353+
Values[0].second.getOwnershipKind());
1354+
13531355
for (auto V : Values) {
13541356
Updater.addAvailableValue(V.first, V.second);
13551357
}

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void BasicBlockCloner::updateSSAAfterCloning() {
101101

102102
SILSSAUpdater ssaUpdater;
103103
for (auto availValPair : availVals) {
104-
ValueBase *inst = availValPair.first;
104+
auto inst = availValPair.first;
105105
if (inst->use_empty())
106106
continue;
107107

@@ -112,7 +112,7 @@ void BasicBlockCloner::updateSSAAfterCloning() {
112112
for (auto *use : inst->getUses())
113113
useList.push_back(UseWrapper(use));
114114

115-
ssaUpdater.initialize(inst->getType());
115+
ssaUpdater.initialize(inst->getType(), inst.getOwnershipKind());
116116
ssaUpdater.addAvailableValue(origBB, inst);
117117
ssaUpdater.addAvailableValue(getNewBB(), newResult);
118118

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ SILValue swift::makeCopiedValueAvailable(
19971997
if (!value->getFunction()->hasOwnership())
19981998
return value;
19991999

2000-
if (value->getType().isTrivial(*value->getFunction()))
2000+
if (value.getOwnershipKind() == OwnershipKind::None)
20012001
return value;
20022002

20032003
auto insertPt = getInsertAfterPoint(value).getValue();
@@ -2013,7 +2013,7 @@ SILValue swift::makeNewValueAvailable(
20132013
if (!value->getFunction()->hasOwnership())
20142014
return value;
20152015

2016-
if (value->getType().isTrivial(*value->getFunction()))
2016+
if (value.getOwnershipKind() == OwnershipKind::None)
20172017
return value;
20182018

20192019
assert(value->getUses().empty() &&

lib/SILOptimizer/Utils/SILSSAUpdater.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ void SILSSAUpdater::deallocateSentinel(SILUndef *undef) {
3434
}
3535

3636
SILSSAUpdater::SILSSAUpdater(SmallVectorImpl<SILPhiArgument *> *phis)
37-
: blockToAvailableValueMap(nullptr),
37+
: blockToAvailableValueMap(nullptr), ownershipKind(OwnershipKind::None),
3838
phiSentinel(nullptr, deallocateSentinel), insertedPhis(phis) {}
3939

4040
SILSSAUpdater::~SILSSAUpdater() = default;
4141

42-
void SILSSAUpdater::initialize(SILType inputType) {
42+
void SILSSAUpdater::initialize(SILType inputType, ValueOwnershipKind kind) {
4343
type = inputType;
44+
ownershipKind = kind;
4445

4546
phiSentinel = std::unique_ptr<SILUndef, void (*)(SILUndef *)>(
4647
SILUndef::getSentinelValue(inputType, this),
@@ -59,6 +60,7 @@ bool SILSSAUpdater::hasValueForBlock(SILBasicBlock *block) const {
5960
/// Indicate that a rewritten value is available in the specified block with the
6061
/// specified value.
6162
void SILSSAUpdater::addAvailableValue(SILBasicBlock *block, SILValue value) {
63+
assert(value.getOwnershipKind().isCompatibleWith(ownershipKind));
6264
(*blockToAvailableValueMap)[block] = value;
6365
}
6466

@@ -316,7 +318,7 @@ class SSAUpdaterTraits<SILSSAUpdater> {
316318
SILSSAUpdater *ssaUpdater) {
317319
// Add the argument to the block.
318320
SILValue phi(
319-
block->createPhiArgument(ssaUpdater->type, OwnershipKind::Owned));
321+
block->createPhiArgument(ssaUpdater->type, ssaUpdater->ownershipKind));
320322

321323
// Mark all predecessor blocks with the sentinel undef value.
322324
SmallVector<SILBasicBlock *, 4> predBlockList(

0 commit comments

Comments
 (0)