Skip to content

Commit b244df0

Browse files
committed
[move-only][borrow2destructure] Sink liveness into the implementation and allow for it to be initialized separately from construction.
1 parent 69a1635 commit b244df0

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyBorrowToDestructure.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class BorrowToDestructureTransform {
6060
IntervalMapAllocator &allocator;
6161
MarkMustCheckInst *mmci;
6262
DiagnosticEmitter &diagnosticEmitter;
63-
// Temporarily optional as this code is refactored.
64-
Optional<FieldSensitiveSSAPrunedLiveRange> liveness;
6563
SmallVector<Operand *, 8> destructureNeedingUses;
6664
PostOrderAnalysis *poa;
6765
PostOrderFunctionInfo *pofi = nullptr;
@@ -78,19 +76,13 @@ class BorrowToDestructureTransform {
7876
SmallFrozenMultiMap<SILInstruction *, Operand *, 8>
7977
instToInterestingOperandIndexMap;
8078

81-
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
82-
8379
public:
8480
BorrowToDestructureTransform(IntervalMapAllocator &allocator,
8581
MarkMustCheckInst *mmci,
8682
DiagnosticEmitter &diagnosticEmitter,
8783
PostOrderAnalysis *poa)
8884
: allocator(allocator), mmci(mmci), diagnosticEmitter(diagnosticEmitter),
89-
liveness(), poa(poa) {
90-
liveness.emplace(mmci->getFunction(), &discoveredBlocks);
91-
liveness->init(mmci);
92-
liveness->initializeDef(mmci, TypeTreeLeafTypeRange(mmci));
93-
}
85+
poa(poa) {}
9486

9587
bool transform();
9688

lib/SILOptimizer/Mandatory/MoveOnlyBorrowToDestructureTransform.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,18 @@ struct borrowtodestructure::Implementation {
137137

138138
Optional<AvailableValueStore> blockToAvailableValues;
139139

140-
Implementation(BorrowToDestructureTransform &interface)
141-
: interface(interface) {}
140+
// Temporarily optional as this code is refactored.
141+
FieldSensitiveSSAPrunedLiveRange liveness;
142+
143+
Implementation(BorrowToDestructureTransform &interface,
144+
SmallVectorImpl<SILBasicBlock *> &discoveredBlocks)
145+
: interface(interface),
146+
liveness(interface.mmci->getFunction(), &discoveredBlocks) {}
147+
148+
void init(SILValue rootAddress) {
149+
liveness.init(rootAddress);
150+
liveness.initializeDef(rootAddress, TypeTreeLeafTypeRange(rootAddress));
151+
}
142152

143153
bool gatherUses(SILValue value);
144154

@@ -236,8 +246,8 @@ bool Implementation::gatherUses(SILValue value) {
236246
interface.blocksToUses.insert(
237247
nextUse->getParentBlock(),
238248
{nextUse, {*leafRange, false /*is lifetime ending*/}});
239-
interface.liveness->updateForUse(nextUse->getUser(), *leafRange,
240-
false /*is lifetime ending*/);
249+
liveness.updateForUse(nextUse->getUser(), *leafRange,
250+
false /*is lifetime ending*/);
241251
interface.instToInterestingOperandIndexMap.insert(nextUse->getUser(),
242252
nextUse);
243253
continue;
@@ -263,8 +273,8 @@ bool Implementation::gatherUses(SILValue value) {
263273
interface.blocksToUses.insert(
264274
nextUse->getParentBlock(),
265275
{nextUse, {*leafRange, true /*is lifetime ending*/}});
266-
interface.liveness->updateForUse(nextUse->getUser(), *leafRange,
267-
true /*is lifetime ending*/);
276+
liveness.updateForUse(nextUse->getUser(), *leafRange,
277+
true /*is lifetime ending*/);
268278
interface.instToInterestingOperandIndexMap.insert(nextUse->getUser(),
269279
nextUse);
270280
continue;
@@ -304,7 +314,7 @@ void Implementation::checkForErrorsOnSameInstruction() {
304314
// check if any of our consuming uses that are on the boundary are used by the
305315
// same instruction as a different consuming or non-consuming use.
306316
interface.instToInterestingOperandIndexMap.setFrozen();
307-
SmallBitVector usedBits(interface.liveness->getNumSubElements());
317+
SmallBitVector usedBits(liveness.getNumSubElements());
308318

309319
for (auto instRangePair :
310320
interface.instToInterestingOperandIndexMap.getRange()) {
@@ -423,8 +433,7 @@ void Implementation::checkDestructureUsesOnBoundary() const {
423433

424434
auto destructureUseSpan =
425435
*TypeTreeLeafTypeRange::get(use->get(), getRootValue());
426-
if (!interface.liveness->isWithinBoundary(use->getUser(),
427-
destructureUseSpan)) {
436+
if (!liveness.isWithinBoundary(use->getUser(), destructureUseSpan)) {
428437
LLVM_DEBUG(llvm::dbgs()
429438
<< " On boundary or within boundary! No error!\n");
430439
continue;
@@ -439,9 +448,8 @@ void Implementation::checkDestructureUsesOnBoundary() const {
439448
// TODO: Fix diagnostic to use destructure needing use and boundary
440449
// uses.
441450
LLVM_DEBUG(llvm::dbgs() << " Within boundary! Emitting error!\n");
442-
FieldSensitivePrunedLivenessBoundary boundary(
443-
interface.liveness->getNumSubElements());
444-
interface.liveness->computeBoundary(boundary);
451+
FieldSensitivePrunedLivenessBoundary boundary(liveness.getNumSubElements());
452+
liveness.computeBoundary(boundary);
445453
getDiagnostics().emitObjectDestructureNeededWithinBorrowBoundary(
446454
getMarkedValue(), use->getUser(), destructureUseSpan, boundary);
447455
}
@@ -1096,7 +1104,7 @@ void Implementation::rewriteUses() {
10961104
<< "Performing BorrowToDestructureTransform::rewriteUses()!\n");
10971105

10981106
llvm::SmallPtrSet<Operand *, 8> seenOperands;
1099-
SmallBitVector bitsNeededInBlock(interface.liveness->getNumSubElements());
1107+
SmallBitVector bitsNeededInBlock(liveness.getNumSubElements());
11001108
IntervalMapAllocator::Map typeSpanToValue(getAllocator());
11011109

11021110
auto *fn = getMarkedValue()->getFunction();
@@ -1590,7 +1598,9 @@ bool BorrowToDestructureTransform::transform() {
15901598

15911599
// Attempt to gather uses. Return false if we saw something that we did not
15921600
// understand.
1593-
Implementation impl(*this);
1601+
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
1602+
Implementation impl(*this, discoveredBlocks);
1603+
impl.init(mmci);
15941604
for (auto *bbi : borrowWorklist) {
15951605
if (!impl.gatherUses(bbi))
15961606
return false;
@@ -1623,7 +1633,7 @@ bool BorrowToDestructureTransform::transform() {
16231633

16241634
// At this point, we know that all of our destructure requiring uses are on
16251635
// the boundary of our live range. Now we need to do the rewriting.
1626-
impl.blockToAvailableValues.emplace(*liveness);
1636+
impl.blockToAvailableValues.emplace(impl.liveness);
16271637
impl.rewriteUses();
16281638

16291639
// Now that we have done our rewritting, we need to do a few cleanups.

0 commit comments

Comments
 (0)