Skip to content

Commit c59fff7

Browse files
Re-use GlobalResolution map for key de-duplication
1 parent 6d50637 commit c59fff7

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/include/llvm/LTO/LTO.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,10 @@ class LTO {
353353
DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID;
354354
} ThinLTO;
355355

356-
std::unique_ptr<llvm::BumpPtrAllocator> Alloc =
357-
std::make_unique<BumpPtrAllocator>();
356+
std::unique_ptr<llvm::BumpPtrAllocator> Alloc;
358357

359-
std::unique_ptr<llvm::UniqueStringSaver> UniqueSymbolSaver =
360-
std::make_unique<llvm::UniqueStringSaver>(*Alloc);
358+
// Symbol saver for global resolution map.
359+
std::unique_ptr<llvm::StringSaver> GlobalResolutionSymbolSaver;
361360

362361
// The global resolution for a particular (mangled) symbol name. This is in
363362
// particular necessary to track whether each symbol can be internalized.

llvm/lib/LTO/LTO.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ cl::opt<bool> EnableLTOInternalization(
7878
"enable-lto-internalization", cl::init(true), cl::Hidden,
7979
cl::desc("Enable global value internalization in LTO"));
8080

81+
static cl::opt<bool>
82+
LTOKeepSymbolCopies("lto-keep-symbol-copies", cl::init(false), cl::Hidden,
83+
cl::desc("Keep copies of symbols in LTO indexing"));
84+
8185
/// Indicate we are linking with an allocator that supports hot/cold operator
8286
/// new interfaces.
8387
extern cl::opt<bool> SupportsHotColdNew;
@@ -609,7 +613,12 @@ LTO::LTO(Config Conf, ThinBackend Backend,
609613
ThinLTO(std::move(Backend)),
610614
GlobalResolutions(
611615
std::make_unique<DenseMap<StringRef, GlobalResolution>>()),
612-
LTOMode(LTOMode) {}
616+
LTOMode(LTOMode) {
617+
if (Conf.KeepSymbolNameCopies || LTOKeepSymbolCopies) {
618+
Alloc = std::make_unique<BumpPtrAllocator>();
619+
GlobalResolutionSymbolSaver = std::make_unique<llvm::StringSaver>(*Alloc);
620+
}
621+
}
613622

614623
// Requires a destructor for MapVector<BitcodeModule>.
615624
LTO::~LTO() = default;
@@ -628,8 +637,9 @@ void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
628637
SymbolResolution Res = *ResI++;
629638

630639
StringRef SymbolName = Sym.getName();
631-
if (Conf.KeepSymbolNameCopies)
632-
SymbolName = UniqueSymbolSaver->save(SymbolName);
640+
// Keep copies of symbols if the client of LTO says so.
641+
if (GlobalResolutionSymbolSaver && !GlobalResolutions->contains(SymbolName))
642+
SymbolName = GlobalResolutionSymbolSaver->save(SymbolName);
633643

634644
auto &GlobalRes = (*GlobalResolutions)[SymbolName];
635645
GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
@@ -1803,7 +1813,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
18031813
// and export lists.
18041814
GlobalResolutions.reset();
18051815
// Reset the bump pointer allocator to release its memory.
1806-
UniqueSymbolSaver.reset();
1816+
GlobalResolutionSymbolSaver.reset();
18071817
Alloc.reset();
18081818

18091819
if (Conf.OptLevel > 0)

0 commit comments

Comments
 (0)