Skip to content

Commit cf17bf7

Browse files
committed
[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
1 parent 1511e98 commit cf17bf7

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

llvm/include/llvm/DWARFLinker/DWARFLinker.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
#include "llvm/CodeGen/AccelTable.h"
1414
#include "llvm/CodeGen/NonRelocatableStringpool.h"
1515
#include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
16+
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1819
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
1920
#include <map>
2021

2122
namespace llvm {
22-
class DWARFContext;
2323
class DWARFExpression;
2424
class DWARFUnit;
2525
class DataExtractor;
@@ -196,19 +196,26 @@ using UnitListTy = std::vector<std::unique_ptr<CompileUnit>>;
196196
/// and it`s address map.
197197
class DWARFFile {
198198
public:
199-
DWARFFile(StringRef Name, DWARFContext *Dwarf, AddressesMap *Addresses,
199+
DWARFFile(StringRef Name, std::unique_ptr<DWARFContext> Dwarf,
200+
std::unique_ptr<AddressesMap> Addresses,
200201
const std::vector<std::string> &Warnings)
201-
: FileName(Name), Dwarf(Dwarf), Addresses(Addresses), Warnings(Warnings) {
202-
}
202+
: FileName(Name), Dwarf(std::move(Dwarf)),
203+
Addresses(std::move(Addresses)), Warnings(Warnings) {}
203204

204205
/// object file name.
205206
StringRef FileName;
206-
/// source DWARF information.
207-
DWARFContext *Dwarf = nullptr;
208-
/// helpful address information(list of valid address ranges, relocations).
209-
AddressesMap *Addresses = nullptr;
207+
/// The source DWARF information.
208+
std::unique_ptr<DWARFContext> Dwarf;
209+
/// Helpful address information(list of valid address ranges, relocations).
210+
std::unique_ptr<AddressesMap> Addresses;
210211
/// warnings for object file.
211212
const std::vector<std::string> &Warnings;
213+
214+
/// Unloads object file and corresponding AddressesMap and Dwarf Context.
215+
void unload() {
216+
Addresses.reset();
217+
Dwarf.reset();
218+
}
212219
};
213220

214221
typedef std::function<void(const Twine &Warning, StringRef Context,
@@ -445,7 +452,8 @@ class DWARFLinker {
445452
/// the debug object.
446453
void clear() {
447454
CompileUnits.clear();
448-
File.Addresses->clear();
455+
ModuleUnits.clear();
456+
File.unload();
449457
}
450458
};
451459

llvm/tools/dsymutil/DwarfLinkerForBinary.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,9 @@ DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
272272
auto ErrorOrObj = loadObject(Obj, DebugMap.getTriple());
273273

274274
if (ErrorOrObj) {
275-
ContextForLinking.push_back(
276-
std::unique_ptr<DWARFContext>(DWARFContext::create(*ErrorOrObj)));
277-
AddressMapForLinking.push_back(
278-
std::make_unique<AddressManager>(*this, *ErrorOrObj, Obj));
279-
280275
ObjectsForLinking.push_back(std::make_unique<DWARFFile>(
281-
Obj.getObjectFilename(), ContextForLinking.back().get(),
282-
AddressMapForLinking.back().get(),
276+
Obj.getObjectFilename(), DWARFContext::create(*ErrorOrObj),
277+
std::make_unique<AddressManager>(*this, *ErrorOrObj, Obj),
283278
Obj.empty() ? Obj.getWarnings() : EmptyWarnings));
284279

285280
Error E = RL.link(*ErrorOrObj);
@@ -554,8 +549,6 @@ bool DwarfLinkerForBinary::link(const DebugMap &Map) {
554549
return false;
555550

556551
ObjectsForLinking.clear();
557-
ContextForLinking.clear();
558-
AddressMapForLinking.clear();
559552

560553
DebugMap DebugMap(Map.getTriple(), Map.getBinaryPath());
561554

llvm/tools/dsymutil/DwarfLinkerForBinary.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ class DwarfLinkerForBinary {
221221
LinkOptions Options;
222222
std::unique_ptr<DwarfStreamer> Streamer;
223223
std::vector<std::unique_ptr<DWARFFile>> ObjectsForLinking;
224-
std::vector<std::unique_ptr<DWARFContext>> ContextForLinking;
225-
std::vector<std::unique_ptr<AddressManager>> AddressMapForLinking;
226224
std::vector<std::string> EmptyWarnings;
227225

228226
/// A list of all .swiftinterface files referenced by the debug

llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ Error linkDebugInfo(object::ObjectFile &File, const Options &Options,
305305
DebugInfoLinker.setUpdate(!Options.DoGarbageCollection);
306306

307307
std::vector<std::unique_ptr<DWARFFile>> ObjectsForLinking(1);
308-
std::vector<std::unique_ptr<AddressesMap>> AddresssMapForLinking(1);
309308
std::vector<std::string> EmptyWarnings;
310309

311310
// Unknown debug sections would be removed. Display warning
@@ -319,11 +318,9 @@ Error linkDebugInfo(object::ObjectFile &File, const Options &Options,
319318
}
320319

321320
// Add object files to the DWARFLinker.
322-
AddresssMapForLinking[0] =
323-
std::make_unique<ObjFileAddressMap>(*Context, Options, File);
324-
325321
ObjectsForLinking[0] = std::make_unique<DWARFFile>(
326-
File.getFileName(), &*Context, AddresssMapForLinking[0].get(),
322+
File.getFileName(), DWARFContext::create(File),
323+
std::make_unique<ObjFileAddressMap>(*Context, Options, File),
327324
EmptyWarnings);
328325

329326
for (size_t I = 0; I < ObjectsForLinking.size(); I++)

0 commit comments

Comments
 (0)