Skip to content

Commit 5960fee

Browse files
authored
Revert "Reduce llvm-gsymutil memory usage (#91023)"
This reverts commit 60cd3eb.
1 parent 56f0ecd commit 5960fee

File tree

3 files changed

+14
-105
lines changed

3 files changed

+14
-105
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
2323
#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
2424
#include "llvm/Support/DataExtractor.h"
25-
#include "llvm/Support/RWMutex.h"
2625
#include <cassert>
2726
#include <cstddef>
2827
#include <cstdint>
@@ -258,10 +257,6 @@ class DWARFUnit {
258257

259258
std::shared_ptr<DWARFUnit> DWO;
260259

261-
mutable llvm::sys::RWMutex FreeDIEsMutex;
262-
mutable llvm::sys::RWMutex ExtractCUDieMutex;
263-
mutable llvm::sys::RWMutex ExtractNonCUDIEsMutex;
264-
265260
protected:
266261
friend dwarf_linker::parallel::CompileUnit;
267262

@@ -571,9 +566,6 @@ class DWARFUnit {
571566

572567
Error tryExtractDIEsIfNeeded(bool CUDieOnly);
573568

574-
/// clearDIEs - Clear parsed DIEs to keep memory usage low.
575-
void clearDIEs(bool KeepCUDie);
576-
577569
private:
578570
/// Size in bytes of the .debug_info data associated with this compile unit.
579571
size_t getDebugInfoSize() const {
@@ -585,22 +577,13 @@ class DWARFUnit {
585577
/// hasn't already been done
586578
void extractDIEsIfNeeded(bool CUDieOnly);
587579

588-
/// extracCUDieIfNeeded - Parse CU DIE if it hasn't already been done.
589-
/// Only to be used from extractDIEsIfNeeded, which holds the correct locks.
590-
bool extractCUDieIfNeeded(bool CUDieOnly, bool &HasCUDie);
591-
592-
/// extractNonCUDIEsIfNeeded - Parses non-CU DIE's for a given CU if needed.
593-
/// Only to be used from extractDIEsIfNeeded, which holds the correct locks.
594-
Error extractNonCUDIEsIfNeeded(bool HasCUDie);
595-
596-
/// extractNonCUDIEsHelper - helper to be invoked *only* from inside
597-
/// tryExtractDIEsIfNeeded, which holds the correct locks.
598-
Error extractNonCUDIEsHelper();
599-
600580
/// extractDIEsToVector - Appends all parsed DIEs to a vector.
601581
void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
602582
std::vector<DWARFDebugInfoEntry> &DIEs) const;
603583

584+
/// clearDIEs - Clear parsed DIEs to keep memory usage low.
585+
void clearDIEs(bool KeepCUDie);
586+
604587
/// parseDWO - Parses .dwo file for current compile unit. Returns true if
605588
/// it was actually constructed.
606589
/// The \p AlternativeLocation specifies an alternative location to get

llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -495,78 +495,21 @@ void DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
495495
Context.getRecoverableErrorHandler()(std::move(e));
496496
}
497497

498-
static bool DoubleCheckedRWLocker(llvm::sys::RWMutex &Mutex,
499-
const std::function<bool()> &reader,
500-
const std::function<void()> &writer) {
501-
{
502-
llvm::sys::ScopedReader Lock(Mutex);
503-
if (reader())
504-
return true;
505-
}
506-
llvm::sys::ScopedWriter Lock(Mutex);
507-
if (reader())
508-
return true;
509-
// If we get here, then the reader function returned false. This means that
510-
// no one else is currently writing to this data structure and it's safe for
511-
// us to write to it now. The scoped writer lock guarantees there are no
512-
// other readers or writers at this point.
513-
writer();
514-
return false;
515-
}
498+
Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) {
499+
if ((CUDieOnly && !DieArray.empty()) ||
500+
DieArray.size() > 1)
501+
return Error::success(); // Already parsed.
516502

517-
// Helper to safely check if the Compile-Unit DIE has been extracted already.
518-
// If not, then extract it, and return false, indicating that it was *not*
519-
// already extracted.
520-
bool DWARFUnit::extractCUDieIfNeeded(bool CUDieOnly, bool &HasCUDie) {
521-
return DoubleCheckedRWLocker(
522-
ExtractCUDieMutex,
523-
// Calculate if the CU DIE has been extracted already.
524-
[&]() {
525-
return ((CUDieOnly && !DieArray.empty()) || DieArray.size() > 1);
526-
},
527-
// Lambda to extract the CU DIE.
528-
[&]() {
529-
HasCUDie = !DieArray.empty();
530-
extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
531-
});
532-
}
503+
bool HasCUDie = !DieArray.empty();
504+
extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
533505

534-
// Helper to safely check if the non-Compile-Unit DIEs have been parsed
535-
// already. If they haven't been parsed, go ahead and parse them.
536-
Error DWARFUnit::extractNonCUDIEsIfNeeded(bool HasCUDie) {
537-
Error Result = Error::success();
538-
DoubleCheckedRWLocker(
539-
ExtractNonCUDIEsMutex,
540-
// Lambda to check if all DIEs have been extracted already.
541-
[=]() { return (DieArray.empty() || HasCUDie); },
542-
// Lambda to extract all the DIEs using the helper function
543-
[&]() {
544-
if (Error E = extractNonCUDIEsHelper()) {
545-
// Consume the success placeholder and save the actual error
546-
consumeError(std::move(Result));
547-
Result = std::move(E);
548-
}
549-
});
550-
return Result;
551-
}
552-
553-
Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) {
554-
// Acquire the FreeDIEsMutex lock (in read-mode) to prevent the Compile Unit
555-
// DIE from being freed by a thread calling clearDIEs() after the CU DIE was
556-
// parsed, but before the rest of the DIEs are parsed, as there are no other
557-
// locks held during that brief period.
558-
llvm::sys::ScopedReader FreeLock(FreeDIEsMutex);
559-
bool HasCUDie = false;
560-
if (extractCUDieIfNeeded(CUDieOnly, HasCUDie))
506+
if (DieArray.empty())
561507
return Error::success();
562-
// Right here is where the above-mentioned race condition exists.
563-
return extractNonCUDIEsIfNeeded(HasCUDie);
564-
}
565508

566-
// Helper used from the tryExtractDIEsIfNeeded function: it must already have
567-
// acquired the ExtractNonCUDIEsMutex for writing.
568-
Error DWARFUnit::extractNonCUDIEsHelper() {
569509
// If CU DIE was just parsed, copy several attribute values from it.
510+
if (HasCUDie)
511+
return Error::success();
512+
570513
DWARFDie UnitDie(this, &DieArray[0]);
571514
if (std::optional<uint64_t> DWOId =
572515
toUnsigned(UnitDie.find(DW_AT_GNU_dwo_id)))
@@ -710,10 +653,6 @@ bool DWARFUnit::parseDWO(StringRef DWOAlternativeLocation) {
710653
}
711654

712655
void DWARFUnit::clearDIEs(bool KeepCUDie) {
713-
// We need to acquire the FreeDIEsMutex lock in write-mode, because we are
714-
// going to free the DIEs, when other threads might be trying to create them.
715-
llvm::sys::ScopedWriter FreeLock(FreeDIEsMutex);
716-
717656
// Do not use resize() + shrink_to_fit() to free memory occupied by dies.
718657
// shrink_to_fit() is a *non-binding* request to reduce capacity() to size().
719658
// It depends on the implementation whether the request is fulfilled.

llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,6 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
587587
DWARFDie Die = getDie(*CU);
588588
CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
589589
handleDie(Out, CUI, Die);
590-
// Release the line table, once we're done.
591-
DICtx.clearLineTableForUnit(CU.get());
592-
// Free any DIEs that were allocated by the DWARF parser.
593-
// If/when they're needed by other CU's, they'll be recreated.
594-
CU->clearDIEs(/*KeepCUDie=*/false);
595590
}
596591
} else {
597592
// LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up
@@ -617,16 +612,11 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
617612
DWARFDie Die = getDie(*CU);
618613
if (Die) {
619614
CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
620-
pool.async([this, CUI, &CU, &LogMutex, &Out, Die]() mutable {
615+
pool.async([this, CUI, &LogMutex, &Out, Die]() mutable {
621616
std::string storage;
622617
raw_string_ostream StrStream(storage);
623618
OutputAggregator ThreadOut(Out.GetOS() ? &StrStream : nullptr);
624619
handleDie(ThreadOut, CUI, Die);
625-
// Release the line table once we're done.
626-
DICtx.clearLineTableForUnit(CU.get());
627-
// Free any DIEs that were allocated by the DWARF parser.
628-
// If/when they're needed by other CU's, they'll be recreated.
629-
CU->clearDIEs(/*KeepCUDie=*/false);
630620
// Print ThreadLogStorage lines into an actual stream under a lock
631621
std::lock_guard<std::mutex> guard(LogMutex);
632622
if (Out.GetOS()) {
@@ -639,9 +629,6 @@ Error DwarfTransformer::convert(uint32_t NumThreads, OutputAggregator &Out) {
639629
}
640630
pool.wait();
641631
}
642-
// Now get rid of all the DIEs that may have been recreated
643-
for (const auto &CU : DICtx.compile_units())
644-
CU->clearDIEs(/*KeepCUDie=*/false);
645632
size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
646633
Out << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n";
647634
return Error::success();

0 commit comments

Comments
 (0)