diff options
| author | Jez Ng <jezng@fb.com> | 2023-03-10 22:28:36 -0500 |
|---|---|---|
| committer | Jez Ng <jezng@fb.com> | 2023-03-11 01:40:14 -0500 |
| commit | bb69a66ced27918f894fb0d5e58e22fda6958d99 (patch) | |
| tree | d7fd4815650ae105b21bf42e6526dce5d7f39354 /lld/MachO/SymbolTable.cpp | |
| parent | 3b4cb1e96c645bb833fe710856479c31383859bb (diff) | |
| download | llvm-bb69a66ced27918f894fb0d5e58e22fda6958d99.tar.gz llvm-bb69a66ced27918f894fb0d5e58e22fda6958d99.tar.bz2 llvm-bb69a66ced27918f894fb0d5e58e22fda6958d99.zip | |
[lld-macho] Coalesce local symbol aliases along with their aliased weak def
This supersedes {D139069}. In some ways we are now closer to ld64's
behavior: we previously only did this coalescing for private-label
symbols, but now we do it for all locals, just like ld64. However, we no
longer generate weak binds when a local alias to a weak symbol is
referenced. This is merely for implementation simplicity; it's not clear
to me that any real-world programs depend on us emulating this behavior.
The problem with the previous approach is that we ended up with
duplicate references to the same symbol instance in our InputFiles,
which translated into duplicate symbols in our output. While we could
work around that problem by performing a dedup step before emitting the
symbol table, it seems cleaner to not generate duplicate references in
the first place.
Numbers for chromium_framework on my 16 Core Intel Mac Pro:
base diff difference (95% CI)
sys_time 2.243 ± 0.093 2.231 ± 0.066 [ -2.5% .. +1.4%]
user_time 6.529 ± 0.087 6.080 ± 0.050 [ -7.5% .. -6.3%]
wall_time 6.928 ± 0.175 6.474 ± 0.112 [ -7.7% .. -5.4%]
samples 26 31
Yep, that's a massive win... because it turns out that {D140606} and
{D139069} caused a regression (of about the same size.) I just didn't
think to measure them back then. I'm guessing all the extra symbols we
have been emitting did not help perf at all...
Reviewed By: lgrey
Differential Revision: https://reviews.llvm.org/D145455
Diffstat (limited to 'lld/MachO/SymbolTable.cpp')
| -rw-r--r-- | lld/MachO/SymbolTable.cpp | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp index 03631239ccba..09b3d8a376e6 100644 --- a/lld/MachO/SymbolTable.cpp +++ b/lld/MachO/SymbolTable.cpp @@ -61,6 +61,38 @@ struct DuplicateSymbolDiag { SmallVector<DuplicateSymbolDiag> dupSymDiags; } // namespace +// Move symbols at \p fromOff in \p fromIsec into \p toIsec, unless that symbol +// is \p skip. +static void transplantSymbolsAtOffset(InputSection *fromIsec, + InputSection *toIsec, Defined *skip, + uint64_t fromOff, uint64_t toOff) { + // Ensure the symbols will still be in address order after our insertions. + auto insertIt = llvm::upper_bound(toIsec->symbols, toOff, + [](uint64_t off, const Symbol *s) { + return cast<Defined>(s)->value < off; + }); + llvm::erase_if(fromIsec->symbols, [&](Symbol *s) { + auto *d = cast<Defined>(s); + if (d->value != fromOff) + return false; + if (d != skip) { + // This repeated insertion will be quadratic unless insertIt is the end + // iterator. However, that is typically the case for files that have + // .subsections_via_symbols set. + insertIt = toIsec->symbols.insert(insertIt, d); + d->isec = toIsec; + d->value = toOff; + // We don't want to have more than one unwindEntry at a given address, so + // drop the redundant ones. We We can safely drop the unwindEntries of + // the symbols in fromIsec since we will be adding another unwindEntry as + // we finish parsing toIsec's file. (We can assume that toIsec has its + // own unwindEntry because of the ODR.) + d->unwindEntry = nullptr; + } + return true; + }); +} + Defined *SymbolTable::addDefined(StringRef name, InputFile *file, InputSection *isec, uint64_t value, uint64_t size, bool isWeakDef, @@ -82,18 +114,27 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file, defined->referencedDynamically |= isReferencedDynamically; defined->noDeadStrip |= noDeadStrip; } - // FIXME: Handle this for bitcode files. - if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec)) + if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec)) { concatIsec->wasCoalesced = true; + // Any local symbols that alias the coalesced symbol should be moved + // into the prevailing section. Note that we have sorted the symbols + // in ObjFile::parseSymbols() such that extern weak symbols appear + // last, so we don't need to worry about subsequent symbols being + // added to an already-coalesced section. + if (defined->isec) + transplantSymbolsAtOffset(concatIsec, defined->isec, + /*skip=*/nullptr, value, defined->value); + } return defined; } if (defined->isWeakDef()) { - // FIXME: Handle this for bitcode files. if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(defined->isec)) { concatIsec->wasCoalesced = true; - concatIsec->symbols.erase(llvm::find(concatIsec->symbols, defined)); + if (isec) + transplantSymbolsAtOffset(concatIsec, isec, defined, defined->value, + value); } } else { std::string srcLoc1 = defined->getSourceLocation(); @@ -382,7 +423,7 @@ struct UndefinedDiag { }; MapVector<const Undefined *, UndefinedDiag> undefs; -} +} // namespace void macho::reportPendingDuplicateSymbols() { for (const auto &duplicate : dupSymDiags) { |
