[lld] ccf1fb0 - [lld][macho][NFC] Factor count zeros into helper function (#161241)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 30 09:44:27 PDT 2025
Author: Ellis Hoag
Date: 2025-09-30T09:44:23-07:00
New Revision: ccf1fb00fee3a79b09adacdc95a32660546f9f14
URL: https://github.com/llvm/llvm-project/commit/ccf1fb00fee3a79b09adacdc95a32660546f9f14
DIFF: https://github.com/llvm/llvm-project/commit/ccf1fb00fee3a79b09adacdc95a32660546f9f14.diff
LOG: [lld][macho][NFC] Factor count zeros into helper function (#161241)
Move `llvm::countr_zero()` into a helper function to reduce code
duplication between `CStringSection` and `DeduplicatedCStringSection`.
More importantly, this moves a giant comment to that helper function
since it pertains to both classes.
Added:
Modified:
lld/MachO/SyntheticSections.cpp
Removed:
################################################################################
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 228b84db21c2a..5645d8a05a28f 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -1685,31 +1685,7 @@ void CStringSection::writeTo(uint8_t *buf) const {
}
}
-void CStringSection::finalizeContents() {
- uint64_t offset = 0;
- // TODO: Call buildCStringPriorities() to support cstring ordering when
- // deduplication is off, although this may negatively impact build
- // performance.
- for (CStringInputSection *isec : inputs) {
- for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
- if (!piece.live)
- continue;
- // See comment above DeduplicatedCStringSection for how alignment is
- // handled.
- uint32_t pieceAlign = 1
- << llvm::countr_zero(isec->align | piece.inSecOff);
- offset = alignToPowerOf2(offset, pieceAlign);
- piece.outSecOff = offset;
- isec->isFinal = true;
- StringRef string = isec->getStringRef(i);
- offset += string.size() + 1; // account for null terminator
- }
- }
- size = offset;
-}
-
-// Mergeable cstring literals are found under the __TEXT,__cstring section. In
-// contrast to ELF, which puts strings that need
diff erent alignments into
+// In contrast to ELF, which puts strings that need
diff erent alignments into
//
diff erent sections, clang's Mach-O backend puts them all in one section.
// Strings that need to be aligned have the .p2align directive emitted before
// them, which simply translates into zero padding in the object file. In other
@@ -1744,8 +1720,33 @@ void CStringSection::finalizeContents() {
// requires its operand addresses to be 16-byte aligned). However, there will
// typically also be other cstrings in the same file that aren't used via SIMD
// and don't need this alignment. They will be emitted at some arbitrary address
-// `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16
-// % A`.
+// `A`, but ld64 will treat them as being 16-byte aligned with an offset of
+// `16 % A`.
+static uint8_t getStringPieceAlignment(const CStringInputSection *isec,
+ const StringPiece &piece) {
+ return llvm::countr_zero(isec->align | piece.inSecOff);
+}
+
+void CStringSection::finalizeContents() {
+ uint64_t offset = 0;
+ // TODO: Call buildCStringPriorities() to support cstring ordering when
+ // deduplication is off, although this may negatively impact build
+ // performance.
+ for (CStringInputSection *isec : inputs) {
+ for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
+ if (!piece.live)
+ continue;
+ uint32_t pieceAlign = 1 << getStringPieceAlignment(isec, piece);
+ offset = alignToPowerOf2(offset, pieceAlign);
+ piece.outSecOff = offset;
+ isec->isFinal = true;
+ StringRef string = isec->getStringRef(i);
+ offset += string.size() + 1; // account for null terminator
+ }
+ }
+ size = offset;
+}
+
void DeduplicatedCStringSection::finalizeContents() {
// Find the largest alignment required for each string.
for (const CStringInputSection *isec : inputs) {
@@ -1754,7 +1755,7 @@ void DeduplicatedCStringSection::finalizeContents() {
continue;
auto s = isec->getCachedHashStringRef(i);
assert(isec->align != 0);
- uint8_t trailingZeros = llvm::countr_zero(isec->align | piece.inSecOff);
+ uint8_t trailingZeros = getStringPieceAlignment(isec, piece);
auto it = stringOffsetMap.insert(
std::make_pair(s, StringOffset(trailingZeros)));
if (!it.second && it.first->second.trailingZeros < trailingZeros)
More information about the llvm-commits
mailing list