Skip to content

Commit

Permalink
[lld][Alignment][NFC] Use Align instead of log2 of alignment in Wasm …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
gchatelet committed Dec 2, 2022
1 parent 173f62d commit cfe77f2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lld/wasm/InputChunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class InputChunk {
OutputSection *outputSec = nullptr;
uint32_t comdat = UINT32_MAX;
uint32_t inputSectionOffset = 0;
uint32_t alignment;
llvm::Align alignment;
uint32_t flags;

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

Expand Down
13 changes: 7 additions & 6 deletions lld/wasm/OutputSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ namespace wasm {
void OutputSegment::addInputSegment(InputChunk *inSeg) {
alignment = std::max(alignment, inSeg->alignment);
inputSegments.push_back(inSeg);
size = llvm::alignTo(size, 1ULL << inSeg->alignment);
size = llvm::alignTo(size, inSeg->alignment);
LLVM_DEBUG(dbgs() << "addInputSegment: " << inSeg->name << " oname=" << name
<< " size=" << inSeg->getSize()
<< " align=" << inSeg->alignment << " at:" << size << "\n");
<< " size=" << inSeg->getSize() << " align="
<< Log2(inSeg->alignment) << " at:" << size << "\n");
inSeg->outputSeg = this;
inSeg->outputSegmentOffset = size;
size += inSeg->getSize();
Expand Down Expand Up @@ -56,8 +56,9 @@ void OutputSegment::finalizeInputSegments() {
});
if (i == mergedSegments.end()) {
LLVM_DEBUG(llvm::dbgs() << "new merge segment: " << name
<< " alignment=" << ms->alignment << "\n");
auto *syn = make<SyntheticMergedChunk>(name, ms->alignment, ms->flags);
<< " alignment=" << Log2(ms->alignment) << "\n");
auto *syn =
make<SyntheticMergedChunk>(name, Log2(ms->alignment), ms->flags);
syn->outputSeg = this;
mergedSegments.push_back(syn);
i = std::prev(mergedSegments.end());
Expand All @@ -74,7 +75,7 @@ void OutputSegment::finalizeInputSegments() {
inputSegments = newSegments;
size = 0;
for (InputChunk *seg : inputSegments) {
size = llvm::alignTo(size, 1ULL << seg->alignment);
size = llvm::alignTo(size, seg->alignment);
LLVM_DEBUG(llvm::dbgs() << "outputSegmentOffset set: " << seg->name
<< " -> " << size << "\n");
seg->outputSegmentOffset = size;
Expand Down
2 changes: 1 addition & 1 deletion lld/wasm/OutputSegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class OutputSegment {
uint32_t linkingFlags = 0;
uint32_t initFlags = 0;
uint32_t sectionOffset = 0;
uint32_t alignment = 0;
llvm::Align alignment;
uint64_t startVA = 0;
std::vector<InputChunk *> inputSegments;

Expand Down
2 changes: 1 addition & 1 deletion lld/wasm/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ void LinkingSection::writeBody() {
writeUleb128(sub.os, dataSegments.size(), "num data segments");
for (const OutputSegment *s : dataSegments) {
writeStr(sub.os, s->name, "segment name");
writeUleb128(sub.os, s->alignment, "alignment");
writeUleb128(sub.os, Log2(s->alignment), "alignment");
writeUleb128(sub.os, s->linkingFlags, "flags");
}
sub.writeTo(os);
Expand Down
9 changes: 5 additions & 4 deletions lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,12 @@ void Writer::layoutMemory() {

out.dylinkSec->memAlign = 0;
for (OutputSegment *seg : segments) {
out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment);
memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment);
out.dylinkSec->memAlign =
std::max(out.dylinkSec->memAlign, Log2(seg->alignment));
memoryPtr = alignTo(memoryPtr, seg->alignment);
seg->startVA = memoryPtr;
log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
memoryPtr, seg->size, seg->alignment));
memoryPtr, seg->size, Log2(seg->alignment)));

if (!config->relocatable && seg->isTLS()) {
if (WasmSym::tlsSize) {
Expand All @@ -301,7 +302,7 @@ void Writer::layoutMemory() {
}
if (WasmSym::tlsAlign) {
auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign);
setGlobalPtr(tlsAlign, int64_t{1} << seg->alignment);
setGlobalPtr(tlsAlign, seg->alignment.value());
}
if (!config->sharedMemory && WasmSym::tlsBase) {
auto *tlsBase = cast<DefinedGlobal>(WasmSym::tlsBase);
Expand Down

0 comments on commit cfe77f2

Please sign in to comment.