Skip to content

[NFC] Refactor PredictableMemoryOptimization #76714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/OptimizerBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ BridgedValue BridgedPassContext::getSILUndef(BridgedType type) const {
}

bool BridgedPassContext::optimizeMemoryAccesses(BridgedFunction f) const {
return swift::optimizeMemoryAccesses(f.getFunction(), this->getDomTree().di);
return swift::optimizeMemoryAccesses(f.getFunction());
}
bool BridgedPassContext::eliminateDeadAllocations(BridgedFunction f) const {
return swift::eliminateDeadAllocations(f.getFunction(),
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SILOptimizer/Utils/InstOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ IntegerLiteralInst *optimizeBuiltinCanBeObjCClass(BuiltinInst *bi,
/// Performs "predictable" memory access optimizations.
///
/// See the PredictableMemoryAccessOptimizations pass.
bool optimizeMemoryAccesses(SILFunction *fn, DominanceInfo *domInfo);
bool optimizeMemoryAccesses(SILFunction *fn);

/// Performs "predictable" dead allocation optimizations.
///
Expand Down
23 changes: 17 additions & 6 deletions lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ElementUseCollector {
SILModule &Module;
const PMOMemoryObjectInfo &TheMemory;
SmallVectorImpl<PMOMemoryUse> &Uses;
SmallVectorImpl<SILInstruction *> &Releases;
SmallVectorImpl<SILInstruction *> *Releases = nullptr;

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

Expand Down Expand Up @@ -212,7 +212,9 @@ bool ElementUseCollector::collectContainerUses(SILValue boxValue) {
// eliminated. That should be implemented and fixed.
if (isa<StrongReleaseInst>(user) || isa<ReleaseValueInst>(user) ||
isa<DestroyValueInst>(user)) {
Releases.push_back(user);
if (Releases) {
Releases->push_back(user);
}
continue;
}

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

// We model destroy_addr as a release of the entire value.
if (isa<DestroyAddrInst>(User)) {
Releases.push_back(User);
if (Releases) {
Releases->push_back(User);
}
continue;
}

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

/// collectPMOElementUsesFrom - Analyze all uses of the specified allocation
/// instruction (alloc_box, alloc_stack or mark_uninitialized), classifying them
/// and storing the information found into the Uses and Releases lists.
/// and storing the information found into the Uses lists.
bool swift::collectPMOElementUsesFrom(
const PMOMemoryObjectInfo &MemoryInfo, SmallVectorImpl<PMOMemoryUse> &Uses)
{
return
ElementUseCollector(MemoryInfo, Uses, /*Releases*/nullptr).collectFrom();
}

bool swift::collectPMOElementUsesAndDestroysFrom(
const PMOMemoryObjectInfo &MemoryInfo, SmallVectorImpl<PMOMemoryUse> &Uses,
SmallVectorImpl<SILInstruction *> &Releases) {
return ElementUseCollector(MemoryInfo, Uses, Releases).collectFrom();
return ElementUseCollector(MemoryInfo, Uses, &Releases).collectFrom();
}
18 changes: 13 additions & 5 deletions lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,21 @@ struct PMOMemoryUse {
bool isValid() const { return Inst != nullptr; }
};

/// collectPMOElementUsesFrom - Analyze all uses of the specified allocation
/// instruction (alloc_box, alloc_stack or mark_uninitialized), classifying them
/// and storing the information found into the Uses and Releases lists.
/// Analyze all uses of the specified allocation instruction (alloc_box,
/// alloc_stack or mark_uninitialized), classifying them and storing the
/// information found into the Uses lists.
[[nodiscard]] bool
collectPMOElementUsesFrom(const PMOMemoryObjectInfo &MemoryInfo,
SmallVectorImpl<PMOMemoryUse> &Uses,
SmallVectorImpl<SILInstruction *> &Releases);
SmallVectorImpl<PMOMemoryUse> &Uses);

/// Analyze all uses of the specified allocation instruction (alloc_box,
/// alloc_stack or mark_uninitialized), classifying them and storing the
/// information found into the Uses and Releases lists.
[[nodiscard]] bool
collectPMOElementUsesAndDestroysFrom(
const PMOMemoryObjectInfo &MemoryInfo,
SmallVectorImpl<PMOMemoryUse> &Uses,
SmallVectorImpl<SILInstruction *> &Releases);

} // end namespace swift

Expand Down
Loading