[llvm] Reduce llvm-gsymutil memory usage (lambda-free, and less locking) (PR #97640)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 3 14:29:27 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-debuginfo
Author: Kevin Frei (kevinfrei)
<details>
<summary>Changes</summary>
[Previous PR](https://github.com/llvm/llvm-project/pull/91023) exposed a compiler issue with the PPC64LE build, due to lambdas (which I had been warned about!) so it was reverted.
I spent an hour thinking about what @<!-- -->dwblaikie had mentioned (using a single recursive lock) and realized that what he suggested works fine with an almost immeasurable performance hit (Over 20 runs, the average speed was about 0.2% slower with the single, recursive lock, which seems like noise rather than a performance hit).
The end result is a much smaller diff (literally only adding the lock_guard's in the two places it's needed) plus the changes to gsymutil.
>From the original PR:
`llvm-gsymutil` eats a lot of RAM. On some large binaries, it causes OOM's on smaller hardware, consuming well over 64GB of RAM. This change frees line tables once we're done with them, and frees DWARFUnits's DIE's when we finish processing each DU, though they may get reconstituted if there are references from other DU's during processing. Once the conversion is complete, all DIE's are freed. The reduction in peak memory usage from these changes showed between 7-12% in my tests.
The mutex is there to prevent freeing of the DIE arrays _while_ they're being extracted. It needs to be a recursive mutex as there's a possible (and sometimes taken) recursive path through the final section of the code (`determineStringOffsetsTableContribution`) that may result in a call to this function.
---
Full diff: https://github.com/llvm/llvm-project/pull/97640.diff
3 Files Affected:
- (modified) llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h (+6-3)
- (modified) llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp (+10)
- (modified) llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp (+14-1)
``````````diff
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
index 80c27aea89312..be3c4fe7c4b19 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
@@ -27,6 +27,7 @@
#include <cstdint>
#include <map>
#include <memory>
+#include <mutex>
#include <set>
#include <utility>
#include <vector>
@@ -257,6 +258,8 @@ class DWARFUnit {
std::shared_ptr<DWARFUnit> DWO;
+ mutable std::recursive_mutex FreeDIEsMutex;
+
protected:
friend dwarf_linker::parallel::CompileUnit;
@@ -566,6 +569,9 @@ class DWARFUnit {
Error tryExtractDIEsIfNeeded(bool CUDieOnly);
+ /// clearDIEs - Clear parsed DIEs to keep memory usage low.
+ void clearDIEs(bool KeepCUDie);
+
private:
/// Size in bytes of the .debug_info data associated with this compile unit.
size_t getDebugInfoSize() const {
@@ -581,9 +587,6 @@ class DWARFUnit {
void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
std::vector<DWARFDebugInfoEntry> &DIEs) const;
- /// clearDIEs - Clear parsed DIEs to keep memory usage low.
- void clearDIEs(bool KeepCUDie);
-
/// parseDWO - Parses .dwo file for current compile unit. Returns true if
/// it was actually constructed.
/// The \p AlternativeLocation specifies an alternative location to get
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
index bdd04b00f557b..4f329f05a48cf 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
@@ -496,6 +496,12 @@ void DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
}
Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) {
+ // Acquire the FreeDIEsMutex recursive lock to prevent a different thread
+ // from freeing the DIE arrays while they're being extracted. It needs to
+ // be recursive, as there is a potentially recursive path through
+ // determineStringOffsetsTableContribution.
+ std::lock_guard<std::recursive_mutex> FreeLock(FreeDIEsMutex);
+
if ((CUDieOnly && !DieArray.empty()) ||
DieArray.size() > 1)
return Error::success(); // Already parsed.
@@ -653,6 +659,10 @@ bool DWARFUnit::parseDWO(StringRef DWOAlternativeLocation) {
}
void DWARFUnit::clearDIEs(bool KeepCUDie) {
+ // We need to acquire the FreeDIEsMutex lock in write-mode, because we are
+ // going to free the DIEs, when other threads might be trying to create them.
+ llvm::sys::ScopedWriter FreeLock(FreeDIEsMutex);
+
// Do not use resize() + shrink_to_fit() to free memory occupied by dies.
// shrink_to_fit() is a *non-binding* request to reduce capacity() to size().
// It depends on the implementation whether the request is fulfilled.
diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
index 601686fdd3dd5..e1b30648b6a77 100644
--- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -587,6 +587,11 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
DWARFDie Die = getDie(*CU);
CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
handleDie(Out, CUI, Die);
+ // Release the line table, once we're done.
+ DICtx.clearLineTableForUnit(CU.get());
+ // Free any DIEs that were allocated by the DWARF parser.
+ // If/when they're needed by other CU's, they'll be recreated.
+ CU->clearDIEs(/*KeepCUDie=*/false);
}
} else {
// LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up
@@ -612,11 +617,16 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
DWARFDie Die = getDie(*CU);
if (Die) {
CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
- pool.async([this, CUI, &LogMutex, &Out, Die]() mutable {
+ pool.async([this, CUI, &CU, &LogMutex, &Out, Die]() mutable {
std::string storage;
raw_string_ostream StrStream(storage);
OutputAggregator ThreadOut(Out.GetOS() ? &StrStream : nullptr);
handleDie(ThreadOut, CUI, Die);
+ // Release the line table once we're done.
+ DICtx.clearLineTableForUnit(CU.get());
+ // Free any DIEs that were allocated by the DWARF parser.
+ // If/when they're needed by other CU's, they'll be recreated.
+ CU->clearDIEs(/*KeepCUDie=*/false);
// Print ThreadLogStorage lines into an actual stream under a lock
std::lock_guard<std::mutex> guard(LogMutex);
if (Out.GetOS()) {
@@ -629,6 +639,9 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
}
pool.wait();
}
+ // Now get rid of all the DIEs that may have been recreated
+ for (const auto &CU : DICtx.compile_units())
+ CU->clearDIEs(/*KeepCUDie=*/false);
size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
Out << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n";
return Error::success();
``````````
</details>
https://github.com/llvm/llvm-project/pull/97640
More information about the llvm-commits
mailing list