Skip to content

Commit 69a1635

Browse files
committed
[move-only] Hide the AvailableValue impl used by BorrowToDestructureTransform in the implementation file.
1 parent 5d1fe77 commit 69a1635

File tree

2 files changed

+66
-56
lines changed

2 files changed

+66
-56
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyBorrowToDestructure.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,48 +48,6 @@ class IntervalMapAllocator {
4848
}
4949
};
5050

51-
// We reserve more bits that we need at the beginning so that we can avoid
52-
// reallocating and potentially breaking our internal mutable array ref
53-
// points into the data store.
54-
struct AvailableValues {
55-
MutableArrayRef<SILValue> values;
56-
57-
SILValue operator[](unsigned index) const { return values[index]; }
58-
SILValue &operator[](unsigned index) { return values[index]; }
59-
unsigned size() const { return values.size(); }
60-
61-
AvailableValues() : values() {}
62-
AvailableValues(MutableArrayRef<SILValue> values) : values(values) {}
63-
64-
void print(llvm::raw_ostream &os, const char *prefix = nullptr) const;
65-
SWIFT_DEBUG_DUMP;
66-
};
67-
68-
struct AvailableValueStore {
69-
std::vector<SILValue> dataStore;
70-
llvm::DenseMap<SILBasicBlock *, AvailableValues> blockToValues;
71-
unsigned nextOffset = 0;
72-
unsigned numBits;
73-
74-
AvailableValueStore(const FieldSensitivePrunedLiveness &liveness)
75-
: dataStore(liveness.getDiscoveredBlocks().size() *
76-
liveness.getNumSubElements()),
77-
numBits(liveness.getNumSubElements()) {}
78-
79-
std::pair<AvailableValues *, bool> get(SILBasicBlock *block) {
80-
auto iter = blockToValues.try_emplace(block, AvailableValues());
81-
82-
if (!iter.second) {
83-
return {&iter.first->second, false};
84-
}
85-
86-
iter.first->second.values =
87-
MutableArrayRef<SILValue>(&dataStore[nextOffset], numBits);
88-
nextOffset += numBits;
89-
return {&iter.first->second, true};
90-
}
91-
};
92-
9351
struct Implementation;
9452

9553
} // namespace borrowtodestructure
@@ -98,8 +56,6 @@ class BorrowToDestructureTransform {
9856
friend borrowtodestructure::Implementation;
9957

10058
using IntervalMapAllocator = borrowtodestructure::IntervalMapAllocator;
101-
using AvailableValueStore = borrowtodestructure::AvailableValueStore;
102-
using AvailableValues = borrowtodestructure::AvailableValues;
10359

10460
IntervalMapAllocator &allocator;
10561
MarkMustCheckInst *mmci;
@@ -109,7 +65,6 @@ class BorrowToDestructureTransform {
10965
SmallVector<Operand *, 8> destructureNeedingUses;
11066
PostOrderAnalysis *poa;
11167
PostOrderFunctionInfo *pofi = nullptr;
112-
Optional<AvailableValueStore> blockToAvailableValues;
11368
SILValue initialValue;
11469
SmallVector<SILInstruction *, 8> createdDestructures;
11570
SmallVector<SILPhiArgument *, 8> createdPhiArguments;

lib/SILOptimizer/Mandatory/MoveOnlyBorrowToDestructureTransform.cpp

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,64 @@ using namespace swift::siloptimizer::borrowtodestructure;
5252
// MARK: Utilities
5353
//===----------------------------------------------------------------------===//
5454

55+
/// Return a loc that can be used regardless if \p inst is a terminator or not.
56+
static SILLocation getSafeLoc(SILInstruction *inst) {
57+
if (isa<TermInst>(inst))
58+
return RegularLocation::getDiagnosticsOnlyLocation(inst->getLoc(),
59+
inst->getModule());
60+
return inst->getLoc();
61+
}
62+
63+
//===----------------------------------------------------------------------===//
64+
// MARK: Available Values
65+
//===----------------------------------------------------------------------===//
66+
67+
namespace {
68+
69+
// We reserve more bits that we need at the beginning so that we can avoid
70+
// reallocating and potentially breaking our internal mutable array ref
71+
// points into the data store.
72+
struct AvailableValues {
73+
MutableArrayRef<SILValue> values;
74+
75+
SILValue operator[](unsigned index) const { return values[index]; }
76+
SILValue &operator[](unsigned index) { return values[index]; }
77+
unsigned size() const { return values.size(); }
78+
79+
AvailableValues() : values() {}
80+
AvailableValues(MutableArrayRef<SILValue> values) : values(values) {}
81+
82+
void print(llvm::raw_ostream &os, const char *prefix = nullptr) const;
83+
SWIFT_DEBUG_DUMP;
84+
};
85+
86+
struct AvailableValueStore {
87+
std::vector<SILValue> dataStore;
88+
llvm::DenseMap<SILBasicBlock *, AvailableValues> blockToValues;
89+
unsigned nextOffset = 0;
90+
unsigned numBits;
91+
92+
AvailableValueStore(const FieldSensitivePrunedLiveness &liveness)
93+
: dataStore(liveness.getDiscoveredBlocks().size() *
94+
liveness.getNumSubElements()),
95+
numBits(liveness.getNumSubElements()) {}
96+
97+
std::pair<AvailableValues *, bool> get(SILBasicBlock *block) {
98+
auto iter = blockToValues.try_emplace(block, AvailableValues());
99+
100+
if (!iter.second) {
101+
return {&iter.first->second, false};
102+
}
103+
104+
iter.first->second.values =
105+
MutableArrayRef<SILValue>(&dataStore[nextOffset], numBits);
106+
nextOffset += numBits;
107+
return {&iter.first->second, true};
108+
}
109+
};
110+
111+
} // namespace
112+
55113
void AvailableValues::print(llvm::raw_ostream &os, const char *prefix) const {
56114
if (prefix)
57115
os << prefix;
@@ -70,21 +128,18 @@ void AvailableValues::print(llvm::raw_ostream &os, const char *prefix) const {
70128

71129
void AvailableValues::dump() const { print(llvm::dbgs(), nullptr); }
72130

73-
/// Return a loc that can be used regardless if \p inst is a terminator or not.
74-
static SILLocation getSafeLoc(SILInstruction *inst) {
75-
if (isa<TermInst>(inst))
76-
return RegularLocation::getDiagnosticsOnlyLocation(inst->getLoc(),
77-
inst->getModule());
78-
return inst->getLoc();
79-
}
80-
81131
//===----------------------------------------------------------------------===//
82132
// MARK: Private Implementation
83133
//===----------------------------------------------------------------------===//
84134

85135
struct borrowtodestructure::Implementation {
86136
BorrowToDestructureTransform &interface;
87137

138+
Optional<AvailableValueStore> blockToAvailableValues;
139+
140+
Implementation(BorrowToDestructureTransform &interface)
141+
: interface(interface) {}
142+
88143
bool gatherUses(SILValue value);
89144

90145
/// Once we have gathered up all of our destructure uses and liveness
@@ -654,7 +709,7 @@ AvailableValues &Implementation::computeAvailableValues(SILBasicBlock *block) {
654709
// First grab our block. If we already have state for the block, just return
655710
// its available values. We already computed the available values and
656711
// potentially updated it with new destructured values for our block.
657-
auto pair = interface.blockToAvailableValues->get(block);
712+
auto pair = blockToAvailableValues->get(block);
658713
if (!pair.second) {
659714
LLVM_DEBUG(llvm::dbgs()
660715
<< " Already have values! Returning them!\n");
@@ -1535,7 +1590,7 @@ bool BorrowToDestructureTransform::transform() {
15351590

15361591
// Attempt to gather uses. Return false if we saw something that we did not
15371592
// understand.
1538-
Implementation impl{*this};
1593+
Implementation impl(*this);
15391594
for (auto *bbi : borrowWorklist) {
15401595
if (!impl.gatherUses(bbi))
15411596
return false;
@@ -1568,7 +1623,7 @@ bool BorrowToDestructureTransform::transform() {
15681623

15691624
// At this point, we know that all of our destructure requiring uses are on
15701625
// the boundary of our live range. Now we need to do the rewriting.
1571-
blockToAvailableValues.emplace(*liveness);
1626+
impl.blockToAvailableValues.emplace(*liveness);
15721627
impl.rewriteUses();
15731628

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

0 commit comments

Comments
 (0)