[lld] 87d3795 - [lld-macho] Remove cuIndices indirection in UnwindInfoSection. NFC (#170252)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 2 00:18:14 PST 2025
Author: Fangrui Song
Date: 2025-12-02T00:18:09-08:00
New Revision: 87d37956b33d4b582e6ff7a0ed4707e70bef361d
URL: https://github.com/llvm/llvm-project/commit/87d37956b33d4b582e6ff7a0ed4707e70bef361d
DIFF: https://github.com/llvm/llvm-project/commit/87d37956b33d4b582e6ff7a0ed4707e70bef361d.diff
LOG: [lld-macho] Remove cuIndices indirection in UnwindInfoSection. NFC (#170252)
cuEntries was sorted indirectly through a separate `cuIndices`.
Eliminate cuIndices for simplicity.
Linking chromium_framework from `#48001` with `-no_uuid` gives identical
executable using this patch.
Added:
Modified:
lld/MachO/UnwindInfoSection.cpp
Removed:
################################################################################
diff --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp
index 6e9f6c2aba749..bf01b12d11dfd 100644
--- a/lld/MachO/UnwindInfoSection.cpp
+++ b/lld/MachO/UnwindInfoSection.cpp
@@ -153,8 +153,6 @@ class UnwindInfoSectionImpl final : public UnwindInfoSection {
// The entries here will be in the same order as their originating symbols
// in symbolsVec.
std::vector<CompactUnwindEntry> cuEntries;
- // Indices into the cuEntries vector.
- std::vector<size_t> cuIndices;
std::vector<Symbol *> personalities;
SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *>
personalityTable;
@@ -400,8 +398,7 @@ void UnwindInfoSectionImpl::relocateCompactUnwind(
// There should only be a handful of unique personality pointers, so we can
// encode them as 2-bit indices into a small array.
void UnwindInfoSectionImpl::encodePersonalities() {
- for (size_t idx : cuIndices) {
- CompactUnwindEntry &cu = cuEntries[idx];
+ for (CompactUnwindEntry &cu : cuEntries) {
if (cu.personality == nullptr)
continue;
// Linear search is fast enough for a small array.
@@ -467,27 +464,24 @@ void UnwindInfoSectionImpl::finalize() {
symbolsVec = symbols.takeVector();
relocateCompactUnwind(cuEntries);
- // Rather than sort & fold the 32-byte entries directly, we create a
- // vector of indices to entries and sort & fold that instead.
- cuIndices.resize(cuEntries.size());
- std::iota(cuIndices.begin(), cuIndices.end(), 0);
- llvm::sort(cuIndices, [&](size_t a, size_t b) {
- return cuEntries[a].functionAddress < cuEntries[b].functionAddress;
+ // Sort the entries by address.
+ llvm::sort(cuEntries, [&](auto &a, auto &b) {
+ return a.functionAddress < b.functionAddress;
});
// Record the ending boundary before we fold the entries.
- cueEndBoundary = cuEntries[cuIndices.back()].functionAddress +
- cuEntries[cuIndices.back()].functionLength;
+ cueEndBoundary =
+ cuEntries.back().functionAddress + cuEntries.back().functionLength;
// Fold adjacent entries with matching encoding+personality and without LSDA
- // We use three iterators on the same cuIndices to fold in-situ:
+ // We use three iterators to fold in-situ:
// (1) `foldBegin` is the first of a potential sequence of matching entries
// (2) `foldEnd` is the first non-matching entry after `foldBegin`.
// The semi-open interval [ foldBegin .. foldEnd ) contains a range
// entries that can be folded into a single entry and written to ...
// (3) `foldWrite`
- auto foldWrite = cuIndices.begin();
- for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) {
+ auto foldWrite = cuEntries.begin();
+ for (auto foldBegin = cuEntries.begin(); foldBegin != cuEntries.end();) {
auto foldEnd = foldBegin;
// Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from
// a base address. The base address is normally not contained directly in
@@ -503,9 +497,9 @@ void UnwindInfoSectionImpl::finalize() {
// directly in the LSDA, two functions at
diff erent addresses would
// necessarily have
diff erent LSDAs, so their CU entries would not have been
// folded anyway.
- while (++foldEnd < cuIndices.end() &&
- cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding &&
- !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda &&
+ while (++foldEnd != cuEntries.end() &&
+ foldBegin->encoding == foldEnd->encoding && !foldBegin->lsda &&
+ !foldEnd->lsda &&
// If we've gotten to this point, we don't have an LSDA, which should
// also imply that we don't have a personality function, since in all
// likelihood a personality function needs the LSDA to do anything
@@ -513,21 +507,20 @@ void UnwindInfoSectionImpl::finalize() {
// and no LSDA though (e.g. the C++ personality __gxx_personality_v0
// is just a no-op without LSDA), so we still check for personality
// function equivalence to handle that case.
- cuEntries[*foldBegin].personality ==
- cuEntries[*foldEnd].personality &&
- canFoldEncoding(cuEntries[*foldEnd].encoding))
+ foldBegin->personality == foldEnd->personality &&
+ canFoldEncoding(foldEnd->encoding))
;
*foldWrite++ = *foldBegin;
foldBegin = foldEnd;
}
- cuIndices.erase(foldWrite, cuIndices.end());
+ cuEntries.erase(foldWrite, cuEntries.end());
encodePersonalities();
// Count frequencies of the folded encodings
EncodingMap encodingFrequencies;
- for (size_t idx : cuIndices)
- encodingFrequencies[cuEntries[idx].encoding]++;
+ for (const CompactUnwindEntry &cu : cuEntries)
+ encodingFrequencies[cu.encoding]++;
// Make a vector of encodings, sorted by descending frequency
for (const auto &frequency : encodingFrequencies)
@@ -558,21 +551,19 @@ void UnwindInfoSectionImpl::finalize() {
// and 127..255 references a local per-second-level-page table.
// First we try the compact format and determine how many entries fit.
// If more entries fit in the regular format, we use that.
- for (size_t i = 0; i < cuIndices.size();) {
- size_t idx = cuIndices[i];
+ for (size_t i = 0; i < cuEntries.size();) {
secondLevelPages.emplace_back();
SecondLevelPage &page = secondLevelPages.back();
page.entryIndex = i;
uint64_t functionAddressMax =
- cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
+ cuEntries[i].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
size_t n = commonEncodings.size();
size_t wordsRemaining =
SECOND_LEVEL_PAGE_WORDS -
sizeof(unwind_info_compressed_second_level_page_header) /
sizeof(uint32_t);
- while (wordsRemaining >= 1 && i < cuIndices.size()) {
- idx = cuIndices[i];
- const CompactUnwindEntry *cuPtr = &cuEntries[idx];
+ while (wordsRemaining >= 1 && i < cuEntries.size()) {
+ const CompactUnwindEntry *cuPtr = &cuEntries[i];
if (cuPtr->functionAddress >= functionAddressMax)
break;
if (commonEncodingIndexes.count(cuPtr->encoding) ||
@@ -593,21 +584,21 @@ void UnwindInfoSectionImpl::finalize() {
// If this is not the final page, see if it's possible to fit more entries
// by using the regular format. This can happen when there are many unique
// encodings, and we saturated the local encoding table early.
- if (i < cuIndices.size() &&
+ if (i < cuEntries.size() &&
page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) {
page.kind = UNWIND_SECOND_LEVEL_REGULAR;
page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX,
- cuIndices.size() - page.entryIndex);
+ cuEntries.size() - page.entryIndex);
i = page.entryIndex + page.entryCount;
} else {
page.kind = UNWIND_SECOND_LEVEL_COMPRESSED;
}
}
- for (size_t idx : cuIndices) {
- lsdaIndex[idx] = entriesWithLsda.size();
- if (cuEntries[idx].lsda)
- entriesWithLsda.push_back(idx);
+ for (size_t i = 0; i < cuEntries.size(); ++i) {
+ lsdaIndex[i] = entriesWithLsda.size();
+ if (cuEntries[i].lsda)
+ entriesWithLsda.push_back(i);
}
// compute size of __TEXT,__unwind_info section
@@ -626,7 +617,7 @@ void UnwindInfoSectionImpl::finalize() {
// All inputs are relocated and output addresses are known, so write!
void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
- assert(!cuIndices.empty() && "call only if there is unwind info");
+ assert(!cuEntries.empty() && "call only if there is unwind info");
// section header
auto *uip = reinterpret_cast<unwind_info_section_header *>(buf);
@@ -660,7 +651,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
uint64_t l2PagesOffset = level2PagesOffset;
auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p);
for (const SecondLevelPage &page : secondLevelPages) {
- size_t idx = cuIndices[page.entryIndex];
+ size_t idx = page.entryIndex;
iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr;
iep->secondLevelPagesSectionOffset = l2PagesOffset;
iep->lsdaIndexArraySectionOffset =
@@ -695,7 +686,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
for (const SecondLevelPage &page : secondLevelPages) {
if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) {
uintptr_t functionAddressBase =
- cuEntries[cuIndices[page.entryIndex]].functionAddress;
+ cuEntries[page.entryIndex].functionAddress;
auto *p2p =
reinterpret_cast<unwind_info_compressed_second_level_page_header *>(
pp);
@@ -708,8 +699,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
p2p->encodingsCount = page.localEncodings.size();
auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
for (size_t i = 0; i < page.entryCount; i++) {
- const CompactUnwindEntry &cue =
- cuEntries[cuIndices[page.entryIndex + i]];
+ const CompactUnwindEntry &cue = cuEntries[page.entryIndex + i];
auto it = commonEncodingIndexes.find(cue.encoding);
if (it == commonEncodingIndexes.end())
it = page.localEncodingIndexes.find(cue.encoding);
@@ -728,8 +718,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
p2p->entryCount = page.entryCount;
auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
for (size_t i = 0; i < page.entryCount; i++) {
- const CompactUnwindEntry &cue =
- cuEntries[cuIndices[page.entryIndex + i]];
+ const CompactUnwindEntry &cue = cuEntries[page.entryIndex + i];
*ep++ = cue.functionAddress;
*ep++ = cue.encoding;
}
More information about the llvm-commits
mailing list