Skip to content

Commit 87d3795

Browse files
authored
[lld-macho] Remove cuIndices indirection in UnwindInfoSection. NFC (llvm#170252)
cuEntries was sorted indirectly through a separate `cuIndices`. Eliminate cuIndices for simplicity. Linking chromium_framework from `llvm#48001` with `-no_uuid` gives identical executable using this patch.
1 parent b5f7058 commit 87d3795

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

lld/MachO/UnwindInfoSection.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ class UnwindInfoSectionImpl final : public UnwindInfoSection {
153153
// The entries here will be in the same order as their originating symbols
154154
// in symbolsVec.
155155
std::vector<CompactUnwindEntry> cuEntries;
156-
// Indices into the cuEntries vector.
157-
std::vector<size_t> cuIndices;
158156
std::vector<Symbol *> personalities;
159157
SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *>
160158
personalityTable;
@@ -400,8 +398,7 @@ void UnwindInfoSectionImpl::relocateCompactUnwind(
400398
// There should only be a handful of unique personality pointers, so we can
401399
// encode them as 2-bit indices into a small array.
402400
void UnwindInfoSectionImpl::encodePersonalities() {
403-
for (size_t idx : cuIndices) {
404-
CompactUnwindEntry &cu = cuEntries[idx];
401+
for (CompactUnwindEntry &cu : cuEntries) {
405402
if (cu.personality == nullptr)
406403
continue;
407404
// Linear search is fast enough for a small array.
@@ -467,27 +464,24 @@ void UnwindInfoSectionImpl::finalize() {
467464
symbolsVec = symbols.takeVector();
468465
relocateCompactUnwind(cuEntries);
469466

470-
// Rather than sort & fold the 32-byte entries directly, we create a
471-
// vector of indices to entries and sort & fold that instead.
472-
cuIndices.resize(cuEntries.size());
473-
std::iota(cuIndices.begin(), cuIndices.end(), 0);
474-
llvm::sort(cuIndices, [&](size_t a, size_t b) {
475-
return cuEntries[a].functionAddress < cuEntries[b].functionAddress;
467+
// Sort the entries by address.
468+
llvm::sort(cuEntries, [&](auto &a, auto &b) {
469+
return a.functionAddress < b.functionAddress;
476470
});
477471

478472
// Record the ending boundary before we fold the entries.
479-
cueEndBoundary = cuEntries[cuIndices.back()].functionAddress +
480-
cuEntries[cuIndices.back()].functionLength;
473+
cueEndBoundary =
474+
cuEntries.back().functionAddress + cuEntries.back().functionLength;
481475

482476
// Fold adjacent entries with matching encoding+personality and without LSDA
483-
// We use three iterators on the same cuIndices to fold in-situ:
477+
// We use three iterators to fold in-situ:
484478
// (1) `foldBegin` is the first of a potential sequence of matching entries
485479
// (2) `foldEnd` is the first non-matching entry after `foldBegin`.
486480
// The semi-open interval [ foldBegin .. foldEnd ) contains a range
487481
// entries that can be folded into a single entry and written to ...
488482
// (3) `foldWrite`
489-
auto foldWrite = cuIndices.begin();
490-
for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) {
483+
auto foldWrite = cuEntries.begin();
484+
for (auto foldBegin = cuEntries.begin(); foldBegin != cuEntries.end();) {
491485
auto foldEnd = foldBegin;
492486
// Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from
493487
// a base address. The base address is normally not contained directly in
@@ -503,31 +497,30 @@ void UnwindInfoSectionImpl::finalize() {
503497
// directly in the LSDA, two functions at different addresses would
504498
// necessarily have different LSDAs, so their CU entries would not have been
505499
// folded anyway.
506-
while (++foldEnd < cuIndices.end() &&
507-
cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding &&
508-
!cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda &&
500+
while (++foldEnd != cuEntries.end() &&
501+
foldBegin->encoding == foldEnd->encoding && !foldBegin->lsda &&
502+
!foldEnd->lsda &&
509503
// If we've gotten to this point, we don't have an LSDA, which should
510504
// also imply that we don't have a personality function, since in all
511505
// likelihood a personality function needs the LSDA to do anything
512506
// useful. It can be technically valid to have a personality function
513507
// and no LSDA though (e.g. the C++ personality __gxx_personality_v0
514508
// is just a no-op without LSDA), so we still check for personality
515509
// function equivalence to handle that case.
516-
cuEntries[*foldBegin].personality ==
517-
cuEntries[*foldEnd].personality &&
518-
canFoldEncoding(cuEntries[*foldEnd].encoding))
510+
foldBegin->personality == foldEnd->personality &&
511+
canFoldEncoding(foldEnd->encoding))
519512
;
520513
*foldWrite++ = *foldBegin;
521514
foldBegin = foldEnd;
522515
}
523-
cuIndices.erase(foldWrite, cuIndices.end());
516+
cuEntries.erase(foldWrite, cuEntries.end());
524517

525518
encodePersonalities();
526519

527520
// Count frequencies of the folded encodings
528521
EncodingMap encodingFrequencies;
529-
for (size_t idx : cuIndices)
530-
encodingFrequencies[cuEntries[idx].encoding]++;
522+
for (const CompactUnwindEntry &cu : cuEntries)
523+
encodingFrequencies[cu.encoding]++;
531524

532525
// Make a vector of encodings, sorted by descending frequency
533526
for (const auto &frequency : encodingFrequencies)
@@ -558,21 +551,19 @@ void UnwindInfoSectionImpl::finalize() {
558551
// and 127..255 references a local per-second-level-page table.
559552
// First we try the compact format and determine how many entries fit.
560553
// If more entries fit in the regular format, we use that.
561-
for (size_t i = 0; i < cuIndices.size();) {
562-
size_t idx = cuIndices[i];
554+
for (size_t i = 0; i < cuEntries.size();) {
563555
secondLevelPages.emplace_back();
564556
SecondLevelPage &page = secondLevelPages.back();
565557
page.entryIndex = i;
566558
uint64_t functionAddressMax =
567-
cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
559+
cuEntries[i].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
568560
size_t n = commonEncodings.size();
569561
size_t wordsRemaining =
570562
SECOND_LEVEL_PAGE_WORDS -
571563
sizeof(unwind_info_compressed_second_level_page_header) /
572564
sizeof(uint32_t);
573-
while (wordsRemaining >= 1 && i < cuIndices.size()) {
574-
idx = cuIndices[i];
575-
const CompactUnwindEntry *cuPtr = &cuEntries[idx];
565+
while (wordsRemaining >= 1 && i < cuEntries.size()) {
566+
const CompactUnwindEntry *cuPtr = &cuEntries[i];
576567
if (cuPtr->functionAddress >= functionAddressMax)
577568
break;
578569
if (commonEncodingIndexes.count(cuPtr->encoding) ||
@@ -593,21 +584,21 @@ void UnwindInfoSectionImpl::finalize() {
593584
// If this is not the final page, see if it's possible to fit more entries
594585
// by using the regular format. This can happen when there are many unique
595586
// encodings, and we saturated the local encoding table early.
596-
if (i < cuIndices.size() &&
587+
if (i < cuEntries.size() &&
597588
page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) {
598589
page.kind = UNWIND_SECOND_LEVEL_REGULAR;
599590
page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX,
600-
cuIndices.size() - page.entryIndex);
591+
cuEntries.size() - page.entryIndex);
601592
i = page.entryIndex + page.entryCount;
602593
} else {
603594
page.kind = UNWIND_SECOND_LEVEL_COMPRESSED;
604595
}
605596
}
606597

607-
for (size_t idx : cuIndices) {
608-
lsdaIndex[idx] = entriesWithLsda.size();
609-
if (cuEntries[idx].lsda)
610-
entriesWithLsda.push_back(idx);
598+
for (size_t i = 0; i < cuEntries.size(); ++i) {
599+
lsdaIndex[i] = entriesWithLsda.size();
600+
if (cuEntries[i].lsda)
601+
entriesWithLsda.push_back(i);
611602
}
612603

613604
// compute size of __TEXT,__unwind_info section
@@ -626,7 +617,7 @@ void UnwindInfoSectionImpl::finalize() {
626617
// All inputs are relocated and output addresses are known, so write!
627618

628619
void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
629-
assert(!cuIndices.empty() && "call only if there is unwind info");
620+
assert(!cuEntries.empty() && "call only if there is unwind info");
630621

631622
// section header
632623
auto *uip = reinterpret_cast<unwind_info_section_header *>(buf);
@@ -660,7 +651,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
660651
uint64_t l2PagesOffset = level2PagesOffset;
661652
auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p);
662653
for (const SecondLevelPage &page : secondLevelPages) {
663-
size_t idx = cuIndices[page.entryIndex];
654+
size_t idx = page.entryIndex;
664655
iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr;
665656
iep->secondLevelPagesSectionOffset = l2PagesOffset;
666657
iep->lsdaIndexArraySectionOffset =
@@ -695,7 +686,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
695686
for (const SecondLevelPage &page : secondLevelPages) {
696687
if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) {
697688
uintptr_t functionAddressBase =
698-
cuEntries[cuIndices[page.entryIndex]].functionAddress;
689+
cuEntries[page.entryIndex].functionAddress;
699690
auto *p2p =
700691
reinterpret_cast<unwind_info_compressed_second_level_page_header *>(
701692
pp);
@@ -708,8 +699,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
708699
p2p->encodingsCount = page.localEncodings.size();
709700
auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
710701
for (size_t i = 0; i < page.entryCount; i++) {
711-
const CompactUnwindEntry &cue =
712-
cuEntries[cuIndices[page.entryIndex + i]];
702+
const CompactUnwindEntry &cue = cuEntries[page.entryIndex + i];
713703
auto it = commonEncodingIndexes.find(cue.encoding);
714704
if (it == commonEncodingIndexes.end())
715705
it = page.localEncodingIndexes.find(cue.encoding);
@@ -728,8 +718,7 @@ void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
728718
p2p->entryCount = page.entryCount;
729719
auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
730720
for (size_t i = 0; i < page.entryCount; i++) {
731-
const CompactUnwindEntry &cue =
732-
cuEntries[cuIndices[page.entryIndex + i]];
721+
const CompactUnwindEntry &cue = cuEntries[page.entryIndex + i];
733722
*ep++ = cue.functionAddress;
734723
*ep++ = cue.encoding;
735724
}

0 commit comments

Comments
 (0)