Skip to content

Commit 13d0139

Browse files
committed
[region-isolation] Change the region -> transferring operand map to use a MapVector instead of a DenseMap.
This ensures that we can efficiently iterate over the map which we will need to do for equality queries. I am going to add the equality queries in a subsequent commit. Just chopping off a larger commit.
1 parent 3bb25de commit 13d0139

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

include/swift/SILOptimizer/Utils/PartitionUtils.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/Basic/LLVM.h"
2020
#include "swift/SIL/SILFunction.h"
2121
#include "swift/SIL/SILInstruction.h"
22+
#include "llvm/ADT/MapVector.h"
2223
#include "llvm/ADT/SmallVector.h"
2324
#include "llvm/Support/Debug.h"
2425
#include <algorithm>
@@ -689,7 +690,7 @@ class Partition {
689690
/// multi map here. The implication of this is that when we are performing
690691
/// dataflow we use a union operation to combine CFG elements and just take
691692
/// the first instruction that we see.
692-
llvm::SmallDenseMap<Region, TransferringOperandSet *, 2>
693+
llvm::SmallMapVector<Region, TransferringOperandSet *, 2>
693694
regionToTransferredOpMap;
694695

695696
/// Label each index with a non-negative (unsigned) label if it is associated

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -3001,6 +3001,8 @@ bool BlockPartitionState::recomputeExitFromEntry(
30013001
exitPartition = workingPartition;
30023002
LLVM_DEBUG(llvm::dbgs() << " Exit Partition: ";
30033003
exitPartition.print(llvm::dbgs()));
3004+
LLVM_DEBUG(llvm::dbgs() << " Updated Partition: "
3005+
<< (exitUpdated ? "yes\n" : "no\n"));
30043006
return exitUpdated;
30053007
}
30063008

lib/SILOptimizer/Utils/PartitionUtils.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ void Partition::markTransferred(Element val,
332332
// Otherwise, we already have this value in the map. Try to insert it.
333333
auto iter1 = elementToRegionMap.find(val);
334334
assert(iter1 != elementToRegionMap.end());
335-
auto iter2 = regionToTransferredOpMap.try_emplace(iter1->second,
336-
transferredOperandSet);
335+
auto iter2 =
336+
regionToTransferredOpMap.insert({iter1->second, transferredOperandSet});
337337

338338
// If we did insert, just return. We were not tracking any state.
339339
if (iter2.second)
340340
return;
341341

342342
// Otherwise, we need to merge the sets.
343-
iter2.first->getSecond() = iter2.first->second->merge(transferredOperandSet);
343+
iter2.first->second = iter2.first->second->merge(transferredOperandSet);
344344
}
345345

346346
bool Partition::undoTransfer(Element val) {
@@ -525,11 +525,11 @@ Partition Partition::join(const Partition &fst, Partition &mutableSnd) {
525525
// mergedRegion is transferred in result.
526526
auto sndIter = snd.regionToTransferredOpMap.find(sndRegionNumber);
527527
if (sndIter != snd.regionToTransferredOpMap.end()) {
528-
auto resultIter = result.regionToTransferredOpMap.try_emplace(
529-
resultRegion, sndIter->second);
528+
auto resultIter = result.regionToTransferredOpMap.insert(
529+
{resultRegion, sndIter->second});
530530
if (!resultIter.second) {
531-
resultIter.first->getSecond() =
532-
resultIter.first->getSecond()->merge(sndIter->second);
531+
resultIter.first->second =
532+
resultIter.first->second->merge(sndIter->second);
533533
}
534534
}
535535
continue;
@@ -574,11 +574,10 @@ Partition Partition::join(const Partition &fst, Partition &mutableSnd) {
574574
result.pushNewElementRegion(sndEltNumber);
575575
auto sndIter = snd.regionToTransferredOpMap.find(sndRegionNumber);
576576
if (sndIter != snd.regionToTransferredOpMap.end()) {
577-
auto fstIter = result.regionToTransferredOpMap.try_emplace(
578-
sndRegionNumber, sndIter->second);
577+
auto fstIter = result.regionToTransferredOpMap.insert(
578+
{sndRegionNumber, sndIter->second});
579579
if (!fstIter.second)
580-
fstIter.first->getSecond() =
581-
fstIter.first->second->merge(sndIter->second);
580+
fstIter.first->second = fstIter.first->second->merge(sndIter->second);
582581
}
583582
if (result.fresh_label <= sndRegionNumber)
584583
result.fresh_label = Region(sndEltNumber + 1);
@@ -629,7 +628,7 @@ void Partition::print(llvm::raw_ostream &os) const {
629628
bool isTransferred = iter != regionToTransferredOpMap.end();
630629
bool isClosureCaptured = false;
631630
if (isTransferred) {
632-
isClosureCaptured = llvm::any_of(iter->getSecond()->range(),
631+
isClosureCaptured = llvm::any_of(iter->second->range(),
633632
[](const TransferringOperand *operand) {
634633
return operand->isClosureCaptured();
635634
});
@@ -671,7 +670,7 @@ void Partition::printVerbose(llvm::raw_ostream &os) const {
671670
bool isTransferred = iter != regionToTransferredOpMap.end();
672671
bool isClosureCaptured = false;
673672
if (isTransferred) {
674-
isClosureCaptured = llvm::any_of(iter->getSecond()->range(),
673+
isClosureCaptured = llvm::any_of(iter->second->range(),
675674
[](const TransferringOperand *operand) {
676675
return operand->isClosureCaptured();
677676
});
@@ -700,7 +699,7 @@ void Partition::printVerbose(llvm::raw_ostream &os) const {
700699
os << "\n";
701700
os << "TransferInsts:\n";
702701
if (isTransferred) {
703-
for (auto op : iter->getSecond()->data()) {
702+
for (auto op : iter->second->data()) {
704703
os << " ";
705704
op->print(os);
706705
}
@@ -821,7 +820,7 @@ Region Partition::merge(Element fst, Element snd, bool updateHistory) {
821820
if (iter != regionToTransferredOpMap.end()) {
822821
auto operand = iter->second;
823822
regionToTransferredOpMap.erase(iter);
824-
regionToTransferredOpMap.try_emplace(fstRegion, operand);
823+
regionToTransferredOpMap.insert({fstRegion, operand});
825824
}
826825

827826
assert(is_canonical_correct());

0 commit comments

Comments
 (0)