From 9b965b37c75d626c01951184088314590e38d299 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 14 Jul 2021 17:16:15 -0700 Subject: [PATCH] [lld][WebAssembly] Cleanup duplicate fields in Symbols.h. NFC This avoids duplication and simplifies the code in several places without increasing the size of the symbol union (at least not above the assert'd limit of 120 bytes). Differential Revision: https://reviews.llvm.org/D106026 --- lld/wasm/Symbols.h | 29 +++++++++++++++-------------- lld/wasm/SyntheticSections.cpp | 34 ++++------------------------------ lld/wasm/Writer.cpp | 8 ++------ 3 files changed, 21 insertions(+), 50 deletions(-) diff --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h index 243f194..a883b8a 100644 --- a/lld/wasm/Symbols.h +++ b/lld/wasm/Symbols.h @@ -172,6 +172,9 @@ public: bool isStub : 1; uint32_t flags; + + llvm::Optional importName; + llvm::Optional importModule; }; class FunctionSymbol : public Symbol { @@ -222,15 +225,15 @@ public: const WasmSignature *type = nullptr, bool isCalledDirectly = true) : FunctionSymbol(name, UndefinedFunctionKind, flags, file, type), - importName(importName), importModule(importModule), - isCalledDirectly(isCalledDirectly) {} + isCalledDirectly(isCalledDirectly) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedFunctionKind; } - llvm::Optional importName; - llvm::Optional importModule; DefinedFunction *stubFunction = nullptr; bool isCalledDirectly; }; @@ -354,15 +357,14 @@ public: llvm::Optional importModule, uint32_t flags, InputFile *file = nullptr, const WasmGlobalType *type = nullptr) - : GlobalSymbol(name, UndefinedGlobalKind, flags, file, type), - importName(importName), importModule(importModule) {} + : GlobalSymbol(name, UndefinedGlobalKind, flags, file, type) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedGlobalKind; } - - llvm::Optional importName; - llvm::Optional importModule; }; class TableSymbol : public Symbol { @@ -403,15 +405,14 @@ public: UndefinedTable(StringRef name, llvm::Optional importName, llvm::Optional importModule, uint32_t flags, InputFile *file, const WasmTableType *type) - : TableSymbol(name, UndefinedTableKind, flags, file, type), - importName(importName), importModule(importModule) {} + : TableSymbol(name, UndefinedTableKind, flags, file, type) { + this->importName = importName; + this->importModule = importModule; + } static bool classof(const Symbol *s) { return s->kind() == UndefinedTableKind; } - - llvm::Optional importName; - llvm::Optional importModule; }; // A tag is a general format to distinguish typed entities. Each tag has an diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp index 93159f4..ceb196a 100644 --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -107,24 +107,11 @@ void ImportSection::addGOTEntry(Symbol *sym) { gotSymbols.push_back(sym); } -template -static void getImportModuleAndName(Symbol *sym, StringRef *outModule, - StringRef *outName) { - if (auto *undef = dyn_cast(sym)) { - *outModule = undef->importModule ? *undef->importModule : defaultModule; - *outName = undef->importName ? *undef->importName : sym->getName(); - } else { - *outModule = defaultModule; - *outName = sym->getName(); - } -} - void ImportSection::addImport(Symbol *sym) { assert(!isSealed); - StringRef module; - StringRef name; + StringRef module = sym->importModule.getValueOr(defaultModule); + StringRef name = sym->importName.getValueOr(sym->getName()); if (auto *f = dyn_cast(sym)) { - getImportModuleAndName(sym, &module, &name); ImportKey key(*(f->getSignature()), module, name); auto entry = importedFunctions.try_emplace(key, numImportedFunctions); if (entry.second) { @@ -134,7 +121,6 @@ void ImportSection::addImport(Symbol *sym) { f->setFunctionIndex(entry.first->second); } } else if (auto *g = dyn_cast(sym)) { - getImportModuleAndName(sym, &module, &name); ImportKey key(*(g->getGlobalType()), module, name); auto entry = importedGlobals.try_emplace(key, numImportedGlobals); if (entry.second) { @@ -150,7 +136,6 @@ void ImportSection::addImport(Symbol *sym) { t->setTagIndex(numImportedTags++); } else { auto *table = cast(sym); - getImportModuleAndName(sym, &module, &name); ImportKey key(*(table->getTableType()), module, name); auto entry = importedTables.try_emplace(key, numImportedTables); if (entry.second) { @@ -189,19 +174,8 @@ void ImportSection::writeBody() { for (const Symbol *sym : importedSymbols) { WasmImport import; - if (auto *f = dyn_cast(sym)) { - import.Field = f->importName ? *f->importName : sym->getName(); - import.Module = f->importModule ? *f->importModule : defaultModule; - } else if (auto *g = dyn_cast(sym)) { - import.Field = g->importName ? *g->importName : sym->getName(); - import.Module = g->importModule ? *g->importModule : defaultModule; - } else if (auto *t = dyn_cast(sym)) { - import.Field = t->importName ? *t->importName : sym->getName(); - import.Module = t->importModule ? *t->importModule : defaultModule; - } else { - import.Field = sym->getName(); - import.Module = defaultModule; - } + import.Field = sym->importName.getValueOr(sym->getName()); + import.Module = sym->importModule.getValueOr(defaultModule); if (auto *functionSym = dyn_cast(sym)) { import.Kind = WASM_EXTERNAL_FUNCTION; diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index 42dbc27..53fe6b6 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -562,12 +562,8 @@ static bool shouldImport(Symbol *sym) { return true; if (config->allowUndefinedSymbols.count(sym->getName()) != 0) return true; - if (auto *g = dyn_cast(sym)) - return g->importName.hasValue(); - if (auto *f = dyn_cast(sym)) - return f->importName.hasValue(); - if (auto *t = dyn_cast(sym)) - return t->importName.hasValue(); + if (sym->isUndefined()) + return sym->importName.hasValue(); return false; } -- 2.7.4