Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / wasm / InputChunks.h
blob1e430832fb84c726f730f4954b47ea8282eba488
1 //===- InputChunks.h --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // An InputChunks represents an indivisible opaque region of a input wasm file.
10 // i.e. a single wasm data segment or a single wasm function.
12 // They are written directly to the mmap'd output file after which relocations
13 // are applied. Because each Chunk is independent they can be written in
14 // parallel.
16 // Chunks are also unit on which garbage collection (--gc-sections) operates.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLD_WASM_INPUT_CHUNKS_H
21 #define LLD_WASM_INPUT_CHUNKS_H
23 #include "Config.h"
24 #include "InputFiles.h"
25 #include "lld/Common/ErrorHandler.h"
26 #include "lld/Common/LLVM.h"
27 #include "llvm/ADT/CachedHashString.h"
28 #include "llvm/MC/StringTableBuilder.h"
29 #include "llvm/Object/Wasm.h"
30 #include <optional>
32 namespace lld {
33 namespace wasm {
35 class ObjFile;
36 class OutputSegment;
37 class OutputSection;
39 class InputChunk {
40 public:
41 enum Kind {
42 DataSegment,
43 Merge,
44 MergedChunk,
45 Function,
46 SyntheticFunction,
47 Section,
50 StringRef name;
51 StringRef debugName;
53 Kind kind() const { return (Kind)sectionKind; }
55 uint32_t getSize() const;
56 uint32_t getInputSize() const;
58 void writeTo(uint8_t *buf) const;
59 void relocate(uint8_t *buf) const;
61 ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
62 void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
64 // Translate an offset into the input chunk to an offset in the output
65 // section.
66 uint64_t getOffset(uint64_t offset) const;
67 // Translate an offset into the input chunk into an offset into the output
68 // chunk. For data segments (InputSegment) this will return and offset into
69 // the output segment. For MergeInputChunk, this will return an offset into
70 // the parent merged chunk. For other chunk types this is no-op and we just
71 // return unmodified offset.
72 uint64_t getChunkOffset(uint64_t offset) const;
73 uint64_t getVA(uint64_t offset = 0) const;
75 uint32_t getComdat() const { return comdat; }
76 StringRef getComdatName() const;
77 uint32_t getInputSectionOffset() const { return inputSectionOffset; }
79 size_t getNumRelocations() const { return relocations.size(); }
80 void writeRelocations(llvm::raw_ostream &os) const;
81 void generateRelocationCode(raw_ostream &os) const;
83 bool isTLS() const { return flags & llvm::wasm::WASM_SEG_FLAG_TLS; }
85 ObjFile *file;
86 OutputSection *outputSec = nullptr;
87 uint32_t comdat = UINT32_MAX;
88 uint32_t inputSectionOffset = 0;
89 uint32_t alignment;
90 uint32_t flags;
92 // Only applies to data segments.
93 uint32_t outputSegmentOffset = 0;
94 const OutputSegment *outputSeg = nullptr;
96 // After assignAddresses is called, this represents the offset from
97 // the beginning of the output section this chunk was assigned to.
98 int32_t outSecOff = 0;
100 uint8_t sectionKind : 3;
102 // Signals that the section is part of the output. The garbage collector,
103 // and COMDAT handling can set a sections' Live bit.
104 // If GC is disabled, all sections start out as live by default.
105 unsigned live : 1;
107 // Signals the chunk was discarded by COMDAT handling.
108 unsigned discarded : 1;
110 protected:
111 InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
112 uint32_t flags = 0)
113 : name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
114 live(!config->gcSections), discarded(false) {}
115 ArrayRef<uint8_t> data() const { return rawData; }
116 uint64_t getTombstone() const;
118 ArrayRef<WasmRelocation> relocations;
119 ArrayRef<uint8_t> rawData;
122 // Represents a WebAssembly data segment which can be included as part of
123 // an output data segments. Note that in WebAssembly, unlike ELF and other
124 // formats, used the term "data segment" to refer to the continuous regions of
125 // memory that make on the data section. See:
126 // https://webassembly.github.io/spec/syntax/modules.html#syntax-data
128 // For example, by default, clang will produce a separate data section for
129 // each global variable.
130 class InputSegment : public InputChunk {
131 public:
132 InputSegment(const WasmSegment &seg, ObjFile *f)
133 : InputChunk(f, InputChunk::DataSegment, seg.Data.Name,
134 seg.Data.Alignment, seg.Data.LinkingFlags),
135 segment(seg) {
136 rawData = segment.Data.Content;
137 comdat = segment.Data.Comdat;
138 inputSectionOffset = segment.SectionOffset;
141 static bool classof(const InputChunk *c) { return c->kind() == DataSegment; }
143 protected:
144 const WasmSegment &segment;
147 class SyntheticMergedChunk;
149 // Merge segment handling copied from lld/ELF/InputSection.h. Keep in sync
150 // where possible.
152 // SectionPiece represents a piece of splittable segment contents.
153 // We allocate a lot of these and binary search on them. This means that they
154 // have to be as compact as possible, which is why we don't store the size (can
155 // be found by looking at the next one).
156 struct SectionPiece {
157 SectionPiece(size_t off, uint32_t hash, bool live)
158 : inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
160 uint32_t inputOff;
161 uint32_t live : 1;
162 uint32_t hash : 31;
163 uint64_t outputOff = 0;
166 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
168 // This corresponds segments marked as WASM_SEG_FLAG_STRINGS.
169 class MergeInputChunk : public InputChunk {
170 public:
171 MergeInputChunk(const WasmSegment &seg, ObjFile *f)
172 : InputChunk(f, Merge, seg.Data.Name, seg.Data.Alignment,
173 seg.Data.LinkingFlags) {
174 rawData = seg.Data.Content;
175 comdat = seg.Data.Comdat;
176 inputSectionOffset = seg.SectionOffset;
179 MergeInputChunk(const WasmSection &s, ObjFile *f)
180 : InputChunk(f, Merge, s.Name, 0, llvm::wasm::WASM_SEG_FLAG_STRINGS) {
181 assert(s.Type == llvm::wasm::WASM_SEC_CUSTOM);
182 comdat = s.Comdat;
183 rawData = s.Content;
186 static bool classof(const InputChunk *s) { return s->kind() == Merge; }
187 void splitIntoPieces();
189 // Translate an offset in the input section to an offset in the parent
190 // MergeSyntheticSection.
191 uint64_t getParentOffset(uint64_t offset) const;
193 // Splittable sections are handled as a sequence of data
194 // rather than a single large blob of data.
195 std::vector<SectionPiece> pieces;
197 // Returns I'th piece's data. This function is very hot when
198 // string merging is enabled, so we want to inline.
199 LLVM_ATTRIBUTE_ALWAYS_INLINE
200 llvm::CachedHashStringRef getData(size_t i) const {
201 size_t begin = pieces[i].inputOff;
202 size_t end =
203 (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
204 return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
207 // Returns the SectionPiece at a given input section offset.
208 SectionPiece *getSectionPiece(uint64_t offset);
209 const SectionPiece *getSectionPiece(uint64_t offset) const {
210 return const_cast<MergeInputChunk *>(this)->getSectionPiece(offset);
213 SyntheticMergedChunk *parent = nullptr;
215 private:
216 void splitStrings(ArrayRef<uint8_t> a);
219 // SyntheticMergedChunk is a class that allows us to put mergeable
220 // sections with different attributes in a single output sections. To do that we
221 // put them into SyntheticMergedChunk synthetic input sections which are
222 // attached to regular output sections.
223 class SyntheticMergedChunk : public InputChunk {
224 public:
225 SyntheticMergedChunk(StringRef name, uint32_t alignment, uint32_t flags)
226 : InputChunk(nullptr, InputChunk::MergedChunk, name, alignment, flags),
227 builder(llvm::StringTableBuilder::RAW, llvm::Align(1ULL << alignment)) {
230 static bool classof(const InputChunk *c) {
231 return c->kind() == InputChunk::MergedChunk;
234 void addMergeChunk(MergeInputChunk *ms) {
235 comdat = ms->getComdat();
236 ms->parent = this;
237 chunks.push_back(ms);
240 void finalizeContents();
242 llvm::StringTableBuilder builder;
244 protected:
245 std::vector<MergeInputChunk *> chunks;
248 // Represents a single wasm function within and input file. These are
249 // combined to create the final output CODE section.
250 class InputFunction : public InputChunk {
251 public:
252 InputFunction(const WasmSignature &s, const WasmFunction *func, ObjFile *f)
253 : InputChunk(f, InputChunk::Function, func->SymbolName), signature(s),
254 function(func),
255 exportName(func && func->ExportName ? (*func->ExportName).str()
256 : std::optional<std::string>()) {
257 inputSectionOffset = function->CodeSectionOffset;
258 rawData =
259 file->codeSection->Content.slice(inputSectionOffset, function->Size);
260 debugName = function->DebugName;
261 comdat = function->Comdat;
264 InputFunction(StringRef name, const WasmSignature &s)
265 : InputChunk(nullptr, InputChunk::Function, name), signature(s) {}
267 static bool classof(const InputChunk *c) {
268 return c->kind() == InputChunk::Function ||
269 c->kind() == InputChunk::SyntheticFunction;
272 std::optional<StringRef> getExportName() const {
273 return exportName ? std::optional<StringRef>(*exportName)
274 : std::optional<StringRef>();
276 void setExportName(std::string exportName) { this->exportName = exportName; }
277 uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
278 uint32_t getFunctionCodeOffset() const { return function->CodeOffset; }
279 uint32_t getFunctionIndex() const { return *functionIndex; }
280 bool hasFunctionIndex() const { return functionIndex.has_value(); }
281 void setFunctionIndex(uint32_t index);
282 uint32_t getTableIndex() const { return *tableIndex; }
283 bool hasTableIndex() const { return tableIndex.has_value(); }
284 void setTableIndex(uint32_t index);
285 void writeCompressed(uint8_t *buf) const;
287 // The size of a given input function can depend on the values of the
288 // LEB relocations within it. This finalizeContents method is called after
289 // all the symbol values have be calculated but before getSize() is ever
290 // called.
291 void calculateSize();
293 const WasmSignature &signature;
295 uint32_t getCompressedSize() const {
296 assert(compressedSize);
297 return compressedSize;
300 const WasmFunction *function;
302 protected:
303 std::optional<std::string> exportName;
304 std::optional<uint32_t> functionIndex;
305 std::optional<uint32_t> tableIndex;
306 uint32_t compressedFuncSize = 0;
307 uint32_t compressedSize = 0;
310 class SyntheticFunction : public InputFunction {
311 public:
312 SyntheticFunction(const WasmSignature &s, StringRef name,
313 StringRef debugName = {})
314 : InputFunction(name, s) {
315 sectionKind = InputChunk::SyntheticFunction;
316 this->debugName = debugName;
319 static bool classof(const InputChunk *c) {
320 return c->kind() == InputChunk::SyntheticFunction;
323 void setBody(ArrayRef<uint8_t> body) { rawData = body; }
326 // Represents a single Wasm Section within an input file.
327 class InputSection : public InputChunk {
328 public:
329 InputSection(const WasmSection &s, ObjFile *f)
330 : InputChunk(f, InputChunk::Section, s.Name),
331 tombstoneValue(getTombstoneForSection(s.Name)), section(s) {
332 assert(section.Type == llvm::wasm::WASM_SEC_CUSTOM);
333 comdat = section.Comdat;
334 rawData = section.Content;
337 static bool classof(const InputChunk *c) {
338 return c->kind() == InputChunk::Section;
341 const uint64_t tombstoneValue;
343 protected:
344 static uint64_t getTombstoneForSection(StringRef name);
345 const WasmSection &section;
348 } // namespace wasm
350 std::string toString(const wasm::InputChunk *);
351 StringRef relocTypeToString(uint8_t relocType);
353 } // namespace lld
355 #endif // LLD_WASM_INPUT_CHUNKS_H