[lld] f428693 - Reland "[lld][WebAssembly] Cleanup duplicate fields in Symbols.h. NFC"

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 20 12:13:21 PDT 2021


Author: Sam Clegg
Date: 2021-07-20T12:13:08-07:00
New Revision: f428693de0aafbd397706e11722a84bc9c20a346

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

LOG: Reland "[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).

Originally commit: 9b965b37c75d626c01951184088314590e38d299
Reverted in: 16aac493e59519377071e900d119ba2e7e5b525d.

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 243f194b75310..a883b8ac58657 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 96f7f30553161..dcbf1302cc910 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -147,19 +147,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 42dbc27261f2b..e94ea179d4dd3 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -562,14 +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();
-
-  return false;
+
+  return sym->importName.hasValue();
 }
 
 void Writer::calculateImports() {


        


More information about the llvm-commits mailing list