Skip to content

Commit a635e8a

Browse files
committed
[NFC] Refactor PredictableMemoryOptimization
In preparation for adding mark_dependence support. Required to support addressors (unsafeAddress) in places other than UnsafePointer.pointee.
1 parent 6a5ae8d commit a635e8a

File tree

5 files changed

+331
-257
lines changed

5 files changed

+331
-257
lines changed

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ BridgedValue BridgedPassContext::getSILUndef(BridgedType type) const {
253253
}
254254

255255
bool BridgedPassContext::optimizeMemoryAccesses(BridgedFunction f) const {
256-
return swift::optimizeMemoryAccesses(f.getFunction(), this->getDomTree().di);
256+
return swift::optimizeMemoryAccesses(f.getFunction());
257257
}
258258
bool BridgedPassContext::eliminateDeadAllocations(BridgedFunction f) const {
259259
return swift::eliminateDeadAllocations(f.getFunction(),

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ IntegerLiteralInst *optimizeBuiltinCanBeObjCClass(BuiltinInst *bi,
599599
/// Performs "predictable" memory access optimizations.
600600
///
601601
/// See the PredictableMemoryAccessOptimizations pass.
602-
bool optimizeMemoryAccesses(SILFunction *fn, DominanceInfo *domInfo);
602+
bool optimizeMemoryAccesses(SILFunction *fn);
603603

604604
/// Performs "predictable" dead allocation optimizations.
605605
///

lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class ElementUseCollector {
136136
SILModule &Module;
137137
const PMOMemoryObjectInfo &TheMemory;
138138
SmallVectorImpl<PMOMemoryUse> &Uses;
139-
SmallVectorImpl<SILInstruction *> &Releases;
139+
SmallVectorImpl<SILInstruction *> *Releases = nullptr;
140140

141141
/// When walking the use list, if we index into a struct element, keep track
142142
/// of this, so that any indexes into tuple subelements don't affect the
@@ -146,7 +146,7 @@ class ElementUseCollector {
146146
public:
147147
ElementUseCollector(const PMOMemoryObjectInfo &TheMemory,
148148
SmallVectorImpl<PMOMemoryUse> &Uses,
149-
SmallVectorImpl<SILInstruction *> &Releases)
149+
SmallVectorImpl<SILInstruction *> *Releases)
150150
: Module(TheMemory.MemoryInst->getModule()), TheMemory(TheMemory),
151151
Uses(Uses), Releases(Releases) {}
152152

@@ -212,7 +212,9 @@ bool ElementUseCollector::collectContainerUses(SILValue boxValue) {
212212
// eliminated. That should be implemented and fixed.
213213
if (isa<StrongReleaseInst>(user) || isa<ReleaseValueInst>(user) ||
214214
isa<DestroyValueInst>(user)) {
215-
Releases.push_back(user);
215+
if (Releases) {
216+
Releases->push_back(user);
217+
}
216218
continue;
217219
}
218220

@@ -436,7 +438,9 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
436438

437439
// We model destroy_addr as a release of the entire value.
438440
if (isa<DestroyAddrInst>(User)) {
439-
Releases.push_back(User);
441+
if (Releases) {
442+
Releases->push_back(User);
443+
}
440444
continue;
441445
}
442446

@@ -539,9 +543,16 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
539543

540544
/// collectPMOElementUsesFrom - Analyze all uses of the specified allocation
541545
/// instruction (alloc_box, alloc_stack or mark_uninitialized), classifying them
542-
/// and storing the information found into the Uses and Releases lists.
546+
/// and storing the information found into the Uses lists.
543547
bool swift::collectPMOElementUsesFrom(
548+
const PMOMemoryObjectInfo &MemoryInfo, SmallVectorImpl<PMOMemoryUse> &Uses)
549+
{
550+
return
551+
ElementUseCollector(MemoryInfo, Uses, /*Releases*/nullptr).collectFrom();
552+
}
553+
554+
bool swift::collectPMOElementUsesAndDestroysFrom(
544555
const PMOMemoryObjectInfo &MemoryInfo, SmallVectorImpl<PMOMemoryUse> &Uses,
545556
SmallVectorImpl<SILInstruction *> &Releases) {
546-
return ElementUseCollector(MemoryInfo, Uses, Releases).collectFrom();
557+
return ElementUseCollector(MemoryInfo, Uses, &Releases).collectFrom();
547558
}

lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,21 @@ struct PMOMemoryUse {
145145
bool isValid() const { return Inst != nullptr; }
146146
};
147147

148-
/// collectPMOElementUsesFrom - Analyze all uses of the specified allocation
149-
/// instruction (alloc_box, alloc_stack or mark_uninitialized), classifying them
150-
/// and storing the information found into the Uses and Releases lists.
148+
/// Analyze all uses of the specified allocation instruction (alloc_box,
149+
/// alloc_stack or mark_uninitialized), classifying them and storing the
150+
/// information found into the Uses lists.
151151
[[nodiscard]] bool
152152
collectPMOElementUsesFrom(const PMOMemoryObjectInfo &MemoryInfo,
153-
SmallVectorImpl<PMOMemoryUse> &Uses,
154-
SmallVectorImpl<SILInstruction *> &Releases);
153+
SmallVectorImpl<PMOMemoryUse> &Uses);
154+
155+
/// Analyze all uses of the specified allocation instruction (alloc_box,
156+
/// alloc_stack or mark_uninitialized), classifying them and storing the
157+
/// information found into the Uses and Releases lists.
158+
[[nodiscard]] bool
159+
collectPMOElementUsesAndDestroysFrom(
160+
const PMOMemoryObjectInfo &MemoryInfo,
161+
SmallVectorImpl<PMOMemoryUse> &Uses,
162+
SmallVectorImpl<SILInstruction *> &Releases);
155163

156164
} // end namespace swift
157165

0 commit comments

Comments
 (0)