[lld] 9b965b3 - [lld][WebAssembly] Cleanup duplicate fields in Symbols.h. NFC

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 19 14:31:23 PDT 2021


Author: Sam Clegg
Date: 2021-07-19T14:31:09-07:00
New Revision: 9b965b37c75d626c01951184088314590e38d299

URL: https://github.com/llvm/llvm-project/commit/9b965b37c75d626c01951184088314590e38d299
DIFF: https://github.com/llvm/llvm-project/commit/9b965b37c75d626c01951184088314590e38d299.diff

LOG: [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

Added: 
    

Modified: 
    lld/wasm/Symbols.h
    lld/wasm/SyntheticSections.cpp
    lld/wasm/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h
index 243f194b7531..a883b8ac5865 100644
--- a/lld/wasm/Symbols.h
+++ b/lld/wasm/Symbols.h
@@ -172,6 +172,9 @@ class Symbol {
   bool isStub : 1;
 
   uint32_t flags;
+
+  llvm::Optional<StringRef> importName;
+  llvm::Optional<StringRef> importModule;
 };
 
 class FunctionSymbol : public Symbol {
@@ -222,15 +225,15 @@ class UndefinedFunction : public FunctionSymbol {
                     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<StringRef> importName;
-  llvm::Optional<StringRef> importModule;
   DefinedFunction *stubFunction = nullptr;
   bool isCalledDirectly;
 };
@@ -354,15 +357,14 @@ class UndefinedGlobal : public GlobalSymbol {
                   llvm::Optional<StringRef> 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<StringRef> importName;
-  llvm::Optional<StringRef> importModule;
 };
 
 class TableSymbol : public Symbol {
@@ -403,15 +405,14 @@ class UndefinedTable : public TableSymbol {
   UndefinedTable(StringRef name, llvm::Optional<StringRef> importName,
                  llvm::Optional<StringRef> 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<StringRef> importName;
-  llvm::Optional<StringRef> 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 93159f4fa535..ceb196ae1a36 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 <typename UndefinedT>
-static void getImportModuleAndName(Symbol *sym, StringRef *outModule,
-                                   StringRef *outName) {
-  if (auto *undef = dyn_cast<UndefinedT>(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<FunctionSymbol>(sym)) {
-    getImportModuleAndName<UndefinedFunction>(sym, &module, &name);
     ImportKey<WasmSignature> 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<GlobalSymbol>(sym)) {
-    getImportModuleAndName<UndefinedGlobal>(sym, &module, &name);
     ImportKey<WasmGlobalType> 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<TableSymbol>(sym);
-    getImportModuleAndName<UndefinedTable>(sym, &module, &name);
     ImportKey<WasmTableType> 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<UndefinedFunction>(sym)) {
-      import.Field = f->importName ? *f->importName : sym->getName();
-      import.Module = f->importModule ? *f->importModule : defaultModule;
-    } else if (auto *g = dyn_cast<UndefinedGlobal>(sym)) {
-      import.Field = g->importName ? *g->importName : sym->getName();
-      import.Module = g->importModule ? *g->importModule : defaultModule;
-    } else if (auto *t = dyn_cast<UndefinedTable>(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<FunctionSymbol>(sym)) {
       import.Kind = WASM_EXTERNAL_FUNCTION;

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 42dbc27261f2..53fe6b62608c 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<UndefinedGlobal>(sym))
-    return g->importName.hasValue();
-  if (auto *f = dyn_cast<UndefinedFunction>(sym))
-    return f->importName.hasValue();
-  if (auto *t = dyn_cast<UndefinedTable>(sym))
-    return t->importName.hasValue();
+  if (sym->isUndefined())
+    return sym->importName.hasValue();
 
   return false;
 }


        


More information about the llvm-commits mailing list