Skip to content

Commit cfe77f2

Browse files
committed
[lld][Alignment][NFC] Use Align instead of log2 of alignment in Wasm Sections
I intend to slowly upgrade all alignments to the Align type in lld as well. Some places talk about alignment in Bytes while other specify them as Log2(Bytes). Let's make sure all of this is coherent. Differential Revision: https://reviews.llvm.org/D139181
1 parent 173f62d commit cfe77f2

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

lld/wasm/InputChunks.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class InputChunk {
8585
OutputSection *outputSec = nullptr;
8686
uint32_t comdat = UINT32_MAX;
8787
uint32_t inputSectionOffset = 0;
88-
uint32_t alignment;
88+
llvm::Align alignment;
8989
uint32_t flags;
9090

9191
// Only applies to data segments.
@@ -109,8 +109,8 @@ class InputChunk {
109109
protected:
110110
InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
111111
uint32_t flags = 0)
112-
: name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
113-
live(!config->gcSections), discarded(false) {}
112+
: name(name), file(f), alignment(1ULL << alignment), flags(flags),
113+
sectionKind(k), live(!config->gcSections), discarded(false) {}
114114
ArrayRef<uint8_t> data() const { return rawData; }
115115
uint64_t getTombstone() const;
116116

lld/wasm/OutputSegment.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ namespace wasm {
2222
void OutputSegment::addInputSegment(InputChunk *inSeg) {
2323
alignment = std::max(alignment, inSeg->alignment);
2424
inputSegments.push_back(inSeg);
25-
size = llvm::alignTo(size, 1ULL << inSeg->alignment);
25+
size = llvm::alignTo(size, inSeg->alignment);
2626
LLVM_DEBUG(dbgs() << "addInputSegment: " << inSeg->name << " oname=" << name
27-
<< " size=" << inSeg->getSize()
28-
<< " align=" << inSeg->alignment << " at:" << size << "\n");
27+
<< " size=" << inSeg->getSize() << " align="
28+
<< Log2(inSeg->alignment) << " at:" << size << "\n");
2929
inSeg->outputSeg = this;
3030
inSeg->outputSegmentOffset = size;
3131
size += inSeg->getSize();
@@ -56,8 +56,9 @@ void OutputSegment::finalizeInputSegments() {
5656
});
5757
if (i == mergedSegments.end()) {
5858
LLVM_DEBUG(llvm::dbgs() << "new merge segment: " << name
59-
<< " alignment=" << ms->alignment << "\n");
60-
auto *syn = make<SyntheticMergedChunk>(name, ms->alignment, ms->flags);
59+
<< " alignment=" << Log2(ms->alignment) << "\n");
60+
auto *syn =
61+
make<SyntheticMergedChunk>(name, Log2(ms->alignment), ms->flags);
6162
syn->outputSeg = this;
6263
mergedSegments.push_back(syn);
6364
i = std::prev(mergedSegments.end());
@@ -74,7 +75,7 @@ void OutputSegment::finalizeInputSegments() {
7475
inputSegments = newSegments;
7576
size = 0;
7677
for (InputChunk *seg : inputSegments) {
77-
size = llvm::alignTo(size, 1ULL << seg->alignment);
78+
size = llvm::alignTo(size, seg->alignment);
7879
LLVM_DEBUG(llvm::dbgs() << "outputSegmentOffset set: " << seg->name
7980
<< " -> " << size << "\n");
8081
seg->outputSegmentOffset = size;

lld/wasm/OutputSegment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OutputSegment {
3838
uint32_t linkingFlags = 0;
3939
uint32_t initFlags = 0;
4040
uint32_t sectionOffset = 0;
41-
uint32_t alignment = 0;
41+
llvm::Align alignment;
4242
uint64_t startVA = 0;
4343
std::vector<InputChunk *> inputSegments;
4444

lld/wasm/SyntheticSections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ void LinkingSection::writeBody() {
668668
writeUleb128(sub.os, dataSegments.size(), "num data segments");
669669
for (const OutputSegment *s : dataSegments) {
670670
writeStr(sub.os, s->name, "segment name");
671-
writeUleb128(sub.os, s->alignment, "alignment");
671+
writeUleb128(sub.os, Log2(s->alignment), "alignment");
672672
writeUleb128(sub.os, s->linkingFlags, "flags");
673673
}
674674
sub.writeTo(os);

lld/wasm/Writer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,12 @@ void Writer::layoutMemory() {
288288

289289
out.dylinkSec->memAlign = 0;
290290
for (OutputSegment *seg : segments) {
291-
out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment);
292-
memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment);
291+
out.dylinkSec->memAlign =
292+
std::max(out.dylinkSec->memAlign, Log2(seg->alignment));
293+
memoryPtr = alignTo(memoryPtr, seg->alignment);
293294
seg->startVA = memoryPtr;
294295
log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
295-
memoryPtr, seg->size, seg->alignment));
296+
memoryPtr, seg->size, Log2(seg->alignment)));
296297

297298
if (!config->relocatable && seg->isTLS()) {
298299
if (WasmSym::tlsSize) {
@@ -301,7 +302,7 @@ void Writer::layoutMemory() {
301302
}
302303
if (WasmSym::tlsAlign) {
303304
auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign);
304-
setGlobalPtr(tlsAlign, int64_t{1} << seg->alignment);
305+
setGlobalPtr(tlsAlign, seg->alignment.value());
305306
}
306307
if (!config->sharedMemory && WasmSym::tlsBase) {
307308
auto *tlsBase = cast<DefinedGlobal>(WasmSym::tlsBase);

0 commit comments

Comments
 (0)