[lld] r374275 - [lld][WebAssembly] Refactor markLive.cpp. NFC
Sam Clegg via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 9 20:23:06 PDT 2019
Author: sbc
Date: Wed Oct 9 20:23:06 2019
New Revision: 374275
URL: http://llvm.org/viewvc/llvm-project?rev=374275&view=rev
Log:
[lld][WebAssembly] Refactor markLive.cpp. NFC
This pattern matches the ELF implementation add if also useful as
part of a planned change where running `mark` more than once is needed.
Differential Revision: https://reviews.llvm.org/D68749
Modified:
lld/trunk/wasm/MarkLive.cpp
Modified: lld/trunk/wasm/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/MarkLive.cpp?rev=374275&r1=374274&r2=374275&view=diff
==============================================================================
--- lld/trunk/wasm/MarkLive.cpp (original)
+++ lld/trunk/wasm/MarkLive.cpp Wed Oct 9 20:23:06 2019
@@ -31,38 +31,52 @@
using namespace llvm;
using namespace llvm::wasm;
-void lld::wasm::markLive() {
- if (!config->gcSections)
- return;
+namespace lld {
+namespace wasm {
- LLVM_DEBUG(dbgs() << "markLive\n");
- SmallVector<InputChunk *, 256> q;
+namespace {
+
+class MarkLive {
+public:
+ void run();
+
+private:
+ void enqueue(Symbol *sym);
+ void markSymbol(Symbol *sym);
+ void mark();
+
+ // A list of chunks to visit.
+ SmallVector<InputChunk *, 256> queue;
+};
- std::function<void(Symbol*)> enqueue = [&](Symbol *sym) {
- if (!sym || sym->isLive())
- return;
- LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
- sym->markLive();
- if (InputChunk *chunk = sym->getChunk())
- q.push_back(chunk);
-
- // The ctor functions are all referenced by the synthetic callCtors
- // function. However, this function does not contain relocations so we
- // have to manually mark the ctors as live if callCtors itself is live.
- if (sym == WasmSym::callCtors) {
- if (config->isPic)
- enqueue(WasmSym::applyRelocs);
- for (const ObjFile *obj : symtab->objectFiles) {
- const WasmLinkingData &l = obj->getWasmObj()->linkingData();
- for (const WasmInitFunc &f : l.InitFunctions) {
- auto* initSym = obj->getFunctionSymbol(f.Symbol);
- if (!initSym->isDiscarded())
- enqueue(initSym);
- }
+} // namespace
+
+void MarkLive::enqueue(Symbol *sym) {
+ if (!sym || sym->isLive())
+ return;
+ LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
+ sym->markLive();
+ if (InputChunk *chunk = sym->getChunk())
+ queue.push_back(chunk);
+
+ // The ctor functions are all referenced by the synthetic callCtors
+ // function. However, this function does not contain relocations so we
+ // have to manually mark the ctors as live if callCtors itself is live.
+ if (sym == WasmSym::callCtors) {
+ if (config->isPic)
+ enqueue(WasmSym::applyRelocs);
+ for (const ObjFile *obj : symtab->objectFiles) {
+ const WasmLinkingData &l = obj->getWasmObj()->linkingData();
+ for (const WasmInitFunc &f : l.InitFunctions) {
+ auto* initSym = obj->getFunctionSymbol(f.Symbol);
+ if (!initSym->isDiscarded())
+ enqueue(initSym);
}
}
- };
+ }
+}
+void MarkLive::run() {
// Add GC root symbols.
if (!config->entry.empty())
enqueue(symtab->find(config->entry));
@@ -87,9 +101,13 @@ void lld::wasm::markLive() {
if (config->sharedMemory && !config->shared)
enqueue(WasmSym::initMemory);
+ mark();
+}
+
+void MarkLive::mark() {
// Follow relocations to mark all reachable chunks.
- while (!q.empty()) {
- InputChunk *c = q.pop_back_val();
+ while (!queue.empty()) {
+ InputChunk *c = queue.pop_back_val();
for (const WasmRelocation reloc : c->getRelocations()) {
if (reloc.Type == R_WASM_TYPE_INDEX_LEB)
@@ -113,6 +131,16 @@ void lld::wasm::markLive() {
enqueue(sym);
}
}
+}
+
+void markLive() {
+ if (!config->gcSections)
+ return;
+
+ LLVM_DEBUG(dbgs() << "markLive\n");
+
+ MarkLive marker;
+ marker.run();
// Report garbage-collected sections.
if (config->printGcSections) {
@@ -138,3 +166,6 @@ void lld::wasm::markLive() {
message("removing unused section " + toString(g));
}
}
+
+} // namespace wasm
+} // namespace lld
More information about the llvm-commits
mailing list