-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Re-apply "[NFCI][LTO][lld] Optimize away symbol copies within LTO global resolution in ELF" #107792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
lld/ELF/InputFiles.cpp
Outdated
for (auto [i, irSym] : llvm::enumerate(obj->symbols())) { | ||
// Keep copies of per-module undefined symbols for LTO::GlobalResolutions | ||
// usage. | ||
irSym.Name = unique_saver().save(irSym.getName()); | ||
if (!irSym.isUndefined()) { | ||
// Symbols can be duplicated in bitcode files because of '#include' and | ||
// linkonce_odr. Use unique_saver to save symbol names for de-duplication. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unique_saver is now moved out, so should this comment be moved up too and merged with the "Keep copies of per-module undefined symbols" in some way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with a couple of comment fixes
llvm/include/llvm/LTO/LTO.h
Outdated
@@ -138,7 +138,7 @@ class InputFile { | |||
|
|||
/// The purpose of this class is to only expose the symbol information that an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/class/struct/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
lld/ELF/InputFiles.cpp
Outdated
@@ -1797,18 +1799,18 @@ void BitcodeFile::parse() { | |||
void BitcodeFile::parseLazy() { | |||
numSymbols = obj->symbols().size(); | |||
symbols = std::make_unique<Symbol *[]>(numSymbols); | |||
for (auto [i, irSym] : llvm::enumerate(obj->symbols())) | |||
for (auto [i, irSym] : llvm::enumerate(obj->symbols())) { | |||
// Keep copies of per-module undefined symbols for LTO::GlobalResolutions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe update the comment a bit since it only talks about undefined symbols but now applies to both. Probably pull up the other comment below on line 1806 and combine them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Fix the use-after-free bug and re-apply #106193
objSym.Name
could be destroyed even if string saver keeps a copy of the referenced string. This caused use-after-free.objSym.Name
to reference (viaStringRef
) the string saver's copy.Test:
lld/test/ELF/lto/asmundef.ll
, its test failure is reproducible with-DLLVM_USE_SANITIZER=Address
and gone with the fix.ELF/lto/asmundef.ll
aborted the multi-stage test at@@@BUILD_STEP stage2/asan_ubsan check@@@
, defined hereOriginal commit message
StringMap<T>
creates a copy of the string for entry insertions and intentionally keep copies since the implementation optimizes string memory usage. On the other hand, linker keeps copies of symbol names [1] inlld::elf::parseFiles
[2] before invokingcompileBitcodeFiles
[3].This change proposes to optimize away string copies inside LTO::GlobalResolutions, which will make LTO indexing more memory efficient for ELF. There are similar opportunities for other (COFF, wasm, MachO) formats.
The optimization takes place for lld (ELF) only. For the rest of use cases (gold plugin,
llvm-lto2
, etc), LTO owns a string saver to keep copies and use global resolution key for de-duplication.Together with @kazutakahirata's work to make
ComputeCrossModuleImport
more memory efficient, we see a ~20% peak memory usage reduction in a binary where peak memory usage needs to go down. Thanks to the optimization in 329ba52, the max (as opposed to the sum) ofComputeCrossModuleImport
orGlobalResolution
shows up in peak memory usage.obj->symbols
inBitcodeFile::parse
already. This change updatesBitcodeFile::parseLazy
to keep copies of per-module undefined symbols.[1] ELF
llvm-project/lld/ELF/InputFiles.cpp
Line 1748 in 1cea5c2
[2]
llvm-project/lld/ELF/Driver.cpp
Line 2863 in ef7b18a
[3]
llvm-project/lld/ELF/Driver.cpp
Line 2995 in ef7b18a