Skip to content

[orc] Reduce memory usage from empty materialization info DenseMaps #88167

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
Apr 9, 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
4 changes: 4 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,10 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,

void unlinkMaterializationResponsibility(MaterializationResponsibility &MR);

/// Attempt to reduce memory usage from empty \c UnmaterializedInfos and
/// \c MaterializingInfos tables.
void shrinkMaterializationInfoMemory();

ExecutionSession &ES;
enum { Open, Closing, Closed } State = Open;
std::mutex GeneratorsMutex;
Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,17 @@ void JITDylib::unlinkMaterializationResponsibility(
});
}

void JITDylib::shrinkMaterializationInfoMemory() {
// DenseMap::erase never shrinks its storage; use clear to heuristically free
// memory since we may have long-lived JDs after linking is done.

if (UnmaterializedInfos.empty())
UnmaterializedInfos.clear();

if (MaterializingInfos.empty())
MaterializingInfos.clear();
}

void JITDylib::setLinkOrder(JITDylibSearchOrder NewLinkOrder,
bool LinkAgainstThisJITDylibFirst) {
ES.runSessionLocked([&]() {
Expand Down Expand Up @@ -1112,6 +1123,8 @@ Error JITDylib::remove(const SymbolNameSet &Names) {
Symbols.erase(SymI);
}

shrinkMaterializationInfoMemory();

return Error::success();
});
}
Expand Down Expand Up @@ -1313,6 +1326,8 @@ JITDylib::removeTracker(ResourceTracker &RT) {
Symbols.erase(I);
}

shrinkMaterializationInfoMemory();

return Result;
}

Expand Down Expand Up @@ -2675,6 +2690,8 @@ void ExecutionSession::OL_completeLookup(
return true;
});

JD.shrinkMaterializationInfoMemory();

// Handle failure.
if (Err) {

Expand Down Expand Up @@ -3118,6 +3135,8 @@ void ExecutionSession::IL_makeEDUReady(

JD.MaterializingInfos.erase(MII);
}

JD.shrinkMaterializationInfoMemory();
}

void ExecutionSession::IL_makeEDUEmitted(
Expand Down Expand Up @@ -3632,6 +3651,8 @@ ExecutionSession::IL_failSymbols(JITDylib &JD,
ExtractFailedQueries(DepMI);
DepJD.MaterializingInfos.erase(SymbolStringPtr(DepName));
}

DepJD.shrinkMaterializationInfoMemory();
}

MI.DependantEDUs.clear();
Expand All @@ -3645,6 +3666,8 @@ ExecutionSession::IL_failSymbols(JITDylib &JD,
JD.MaterializingInfos.erase(Name);
}

JD.shrinkMaterializationInfoMemory();

#ifdef EXPENSIVE_CHECKS
verifySessionState("exiting ExecutionSession::IL_failSymbols");
#endif
Expand Down