From cf17bf78720f05aedd3e4a4b5252977e9a6d3e22 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 6 Oct 2023 09:59:40 -0700 Subject: [PATCH] [DWARFLinker] Release input DWARF after object has been linked dsymutil is using an excessive amount of memory because it's holding on to the DWARF Context, even after it's done processing the corresponding object file. This patch releases the input DWARF after cloning, at which point it is no longer needed. This has always been the intended behavior, though I didn't bisect to figure out when this regressed. When linking swift, this reduces peak (dirty) memory usage from 25 to 15 gigabytes. rdar://111525100 --- llvm/include/llvm/DWARFLinker/DWARFLinker.h | 26 ++++++++++++------- llvm/tools/dsymutil/DwarfLinkerForBinary.cpp | 11 ++------ llvm/tools/dsymutil/DwarfLinkerForBinary.h | 2 -- llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp | 7 ++--- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinker.h b/llvm/include/llvm/DWARFLinker/DWARFLinker.h index 6c202c6b8e5a5..d6b8be7f4db47 100644 --- a/llvm/include/llvm/DWARFLinker/DWARFLinker.h +++ b/llvm/include/llvm/DWARFLinker/DWARFLinker.h @@ -13,13 +13,13 @@ #include "llvm/CodeGen/AccelTable.h" #include "llvm/CodeGen/NonRelocatableStringpool.h" #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" #include "llvm/DebugInfo/DWARF/DWARFDie.h" #include namespace llvm { -class DWARFContext; class DWARFExpression; class DWARFUnit; class DataExtractor; @@ -196,19 +196,26 @@ using UnitListTy = std::vector>; /// and it`s address map. class DWARFFile { public: - DWARFFile(StringRef Name, DWARFContext *Dwarf, AddressesMap *Addresses, + DWARFFile(StringRef Name, std::unique_ptr Dwarf, + std::unique_ptr Addresses, const std::vector &Warnings) - : FileName(Name), Dwarf(Dwarf), Addresses(Addresses), Warnings(Warnings) { - } + : FileName(Name), Dwarf(std::move(Dwarf)), + Addresses(std::move(Addresses)), Warnings(Warnings) {} /// object file name. StringRef FileName; - /// source DWARF information. - DWARFContext *Dwarf = nullptr; - /// helpful address information(list of valid address ranges, relocations). - AddressesMap *Addresses = nullptr; + /// The source DWARF information. + std::unique_ptr Dwarf; + /// Helpful address information(list of valid address ranges, relocations). + std::unique_ptr Addresses; /// warnings for object file. const std::vector &Warnings; + + /// Unloads object file and corresponding AddressesMap and Dwarf Context. + void unload() { + Addresses.reset(); + Dwarf.reset(); + } }; typedef std::functionclear(); + ModuleUnits.clear(); + File.unload(); } }; diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp index b7cc05a28712c..67648eddf81c4 100644 --- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp +++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp @@ -272,14 +272,9 @@ DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj, auto ErrorOrObj = loadObject(Obj, DebugMap.getTriple()); if (ErrorOrObj) { - ContextForLinking.push_back( - std::unique_ptr(DWARFContext::create(*ErrorOrObj))); - AddressMapForLinking.push_back( - std::make_unique(*this, *ErrorOrObj, Obj)); - ObjectsForLinking.push_back(std::make_unique( - Obj.getObjectFilename(), ContextForLinking.back().get(), - AddressMapForLinking.back().get(), + Obj.getObjectFilename(), DWARFContext::create(*ErrorOrObj), + std::make_unique(*this, *ErrorOrObj, Obj), Obj.empty() ? Obj.getWarnings() : EmptyWarnings)); Error E = RL.link(*ErrorOrObj); @@ -554,8 +549,6 @@ bool DwarfLinkerForBinary::link(const DebugMap &Map) { return false; ObjectsForLinking.clear(); - ContextForLinking.clear(); - AddressMapForLinking.clear(); DebugMap DebugMap(Map.getTriple(), Map.getBinaryPath()); diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.h b/llvm/tools/dsymutil/DwarfLinkerForBinary.h index 623441c749a5d..616af4882d80e 100644 --- a/llvm/tools/dsymutil/DwarfLinkerForBinary.h +++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.h @@ -221,8 +221,6 @@ class DwarfLinkerForBinary { LinkOptions Options; std::unique_ptr Streamer; std::vector> ObjectsForLinking; - std::vector> ContextForLinking; - std::vector> AddressMapForLinking; std::vector EmptyWarnings; /// A list of all .swiftinterface files referenced by the debug diff --git a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp index 5b1ded885cd52..23e01e35b35da 100644 --- a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp +++ b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp @@ -305,7 +305,6 @@ Error linkDebugInfo(object::ObjectFile &File, const Options &Options, DebugInfoLinker.setUpdate(!Options.DoGarbageCollection); std::vector> ObjectsForLinking(1); - std::vector> AddresssMapForLinking(1); std::vector EmptyWarnings; // Unknown debug sections would be removed. Display warning @@ -319,11 +318,9 @@ Error linkDebugInfo(object::ObjectFile &File, const Options &Options, } // Add object files to the DWARFLinker. - AddresssMapForLinking[0] = - std::make_unique(*Context, Options, File); - ObjectsForLinking[0] = std::make_unique( - File.getFileName(), &*Context, AddresssMapForLinking[0].get(), + File.getFileName(), DWARFContext::create(File), + std::make_unique(*Context, Options, File), EmptyWarnings); for (size_t I = 0; I < ObjectsForLinking.size(); I++)