[lld] r327326 - [WebAssembly] Refactor to avoid conflating global and fucntion index space. NFC

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 12 12:56:23 PDT 2018


Author: sbc
Date: Mon Mar 12 12:56:23 2018
New Revision: 327326

URL: http://llvm.org/viewvc/llvm-project?rev=327326&view=rev
Log:
[WebAssembly] Refactor to avoid conflating global and fucntion index space. NFC

Differential Revision: https://reviews.llvm.org/D44358

Modified:
    lld/trunk/wasm/InputChunks.cpp
    lld/trunk/wasm/InputChunks.h
    lld/trunk/wasm/InputFiles.cpp
    lld/trunk/wasm/InputGlobal.h
    lld/trunk/wasm/Symbols.cpp
    lld/trunk/wasm/Symbols.h
    lld/trunk/wasm/Writer.cpp

Modified: lld/trunk/wasm/InputChunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.cpp?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.cpp (original)
+++ lld/trunk/wasm/InputChunks.cpp Mon Mar 12 12:56:23 2018
@@ -122,11 +122,11 @@ void InputChunk::writeRelocations(raw_os
   }
 }
 
-void InputFunction::setOutputIndex(uint32_t Index) {
-  DEBUG(dbgs() << "InputFunction::setOutputIndex: " << getName() << " -> "
+void InputFunction::setFunctionIndex(uint32_t Index) {
+  DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> "
                << Index << "\n");
-  assert(!hasOutputIndex());
-  OutputIndex = Index;
+  assert(!hasFunctionIndex());
+  FunctionIndex = Index;
 }
 
 void InputFunction::setTableIndex(uint32_t Index) {

Modified: lld/trunk/wasm/InputChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.h?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.h (original)
+++ lld/trunk/wasm/InputChunks.h Mon Mar 12 12:56:23 2018
@@ -126,9 +126,9 @@ public:
 
   StringRef getName() const override { return Function->Name; }
   StringRef getComdat() const override { return Function->Comdat; }
-  uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
-  bool hasOutputIndex() const { return OutputIndex.hasValue(); }
-  void setOutputIndex(uint32_t Index);
+  uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); }
+  bool hasFunctionIndex() const { return FunctionIndex.hasValue(); }
+  void setFunctionIndex(uint32_t Index);
   uint32_t getTableIndex() const { return TableIndex.getValue(); }
   bool hasTableIndex() const { return TableIndex.hasValue(); }
   void setTableIndex(uint32_t Index);
@@ -145,7 +145,7 @@ protected:
   }
 
   const WasmFunction *Function;
-  llvm::Optional<uint32_t> OutputIndex;
+  llvm::Optional<uint32_t> FunctionIndex;
   llvm::Optional<uint32_t> TableIndex;
 };
 

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Mon Mar 12 12:56:23 2018
@@ -106,9 +106,9 @@ uint32_t ObjFile::calcNewValue(const Was
   case R_WEBASSEMBLY_TYPE_INDEX_LEB:
     return TypeMap[Reloc.Index];
   case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
-    return getFunctionSymbol(Reloc.Index)->getOutputIndex();
+    return getFunctionSymbol(Reloc.Index)->getFunctionIndex();
   case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
-    return getGlobalSymbol(Reloc.Index)->getOutputIndex();
+    return getGlobalSymbol(Reloc.Index)->getGlobalIndex();
   default:
     llvm_unreachable("unknown relocation type");
   }

Modified: lld/trunk/wasm/InputGlobal.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputGlobal.h?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/InputGlobal.h (original)
+++ lld/trunk/wasm/InputGlobal.h Mon Mar 12 12:56:23 2018
@@ -30,11 +30,11 @@ public:
 
   const WasmGlobalType &getType() const { return Global.Type; }
 
-  uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
-  bool hasOutputIndex() const { return OutputIndex.hasValue(); }
-  void setOutputIndex(uint32_t Index) {
-    assert(!hasOutputIndex());
-    OutputIndex = Index;
+  uint32_t getGlobalIndex() const { return GlobalIndex.getValue(); }
+  bool hasGlobalIndex() const { return GlobalIndex.hasValue(); }
+  void setGlobalIndex(uint32_t Index) {
+    assert(!hasGlobalIndex());
+    GlobalIndex = Index;
   }
 
   bool Live = false;
@@ -42,7 +42,7 @@ public:
   WasmGlobal Global;
 
 protected:
-  llvm::Optional<uint32_t> OutputIndex;
+  llvm::Optional<uint32_t> GlobalIndex;
 };
 
 } // namespace wasm

Modified: lld/trunk/wasm/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.cpp?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.cpp (original)
+++ lld/trunk/wasm/Symbols.cpp Mon Mar 12 12:56:23 2018
@@ -39,28 +39,6 @@ WasmSymbolType Symbol::getWasmType() con
   llvm_unreachable("invalid symbol kind");
 }
 
-bool Symbol::hasOutputIndex() const {
-  if (auto *F = dyn_cast<DefinedFunction>(this))
-    if (F->Function)
-      return F->Function->hasOutputIndex();
-  if (auto *G = dyn_cast<DefinedGlobal>(this))
-    if (G->Global)
-      return G->Global->hasOutputIndex();
-  return OutputIndex != INVALID_INDEX;
-}
-
-uint32_t Symbol::getOutputIndex() const {
-  assert(!isa<DataSymbol>(this));
-  if (auto *F = dyn_cast<DefinedFunction>(this))
-    if (F->Function)
-      return F->Function->getOutputIndex();
-  if (auto *G = dyn_cast<DefinedGlobal>(this))
-    if (G->Global)
-      return G->Global->getOutputIndex();
-  assert(OutputIndex != INVALID_INDEX);
-  return OutputIndex;
-}
-
 InputChunk *Symbol::getChunk() const {
   if (auto *F = dyn_cast<DefinedFunction>(this))
     return F->Function;
@@ -89,13 +67,6 @@ void Symbol::setOutputSymbolIndex(uint32
   OutputSymbolIndex = Index;
 }
 
-void Symbol::setOutputIndex(uint32_t Index) {
-  DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
-  assert(!isa<DataSymbol>(this));
-  assert(OutputIndex == INVALID_INDEX);
-  OutputIndex = Index;
-}
-
 bool Symbol::isWeak() const {
   return (Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
 }
@@ -117,6 +88,25 @@ void Symbol::setHidden(bool IsHidden) {
     Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
 }
 
+uint32_t FunctionSymbol::getFunctionIndex() const {
+  if (auto *F = dyn_cast<DefinedFunction>(this))
+    return F->Function->getFunctionIndex();
+  assert(FunctionIndex != INVALID_INDEX);
+  return FunctionIndex;
+}
+
+void FunctionSymbol::setFunctionIndex(uint32_t Index) {
+  DEBUG(dbgs() << "setFunctionIndex " << Name << " -> " << Index << "\n");
+  assert(FunctionIndex == INVALID_INDEX);
+  FunctionIndex = Index;
+}
+
+bool FunctionSymbol::hasFunctionIndex() const {
+  if (auto *F = dyn_cast<DefinedFunction>(this))
+    return F->Function->hasFunctionIndex();
+  return FunctionIndex != INVALID_INDEX;
+}
+
 uint32_t FunctionSymbol::getTableIndex() const {
   if (auto *F = dyn_cast<DefinedFunction>(this))
     return F->Function->getTableIndex();
@@ -172,6 +162,25 @@ uint32_t DefinedData::getOutputSegmentIn
   return Segment->OutputSeg->Index;
 }
 
+uint32_t GlobalSymbol::getGlobalIndex() const {
+  if (auto *F = dyn_cast<DefinedGlobal>(this))
+    return F->Global->getGlobalIndex();
+  assert(GlobalIndex != INVALID_INDEX);
+  return GlobalIndex;
+}
+
+void GlobalSymbol::setGlobalIndex(uint32_t Index) {
+  DEBUG(dbgs() << "setGlobalIndex " << Name << " -> " << Index << "\n");
+  assert(GlobalIndex == INVALID_INDEX);
+  GlobalIndex = Index;
+}
+
+bool GlobalSymbol::hasGlobalIndex() const {
+  if (auto *F = dyn_cast<DefinedGlobal>(this))
+    return F->Global->hasGlobalIndex();
+  return GlobalIndex != INVALID_INDEX;
+}
+
 DefinedGlobal::DefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File,
                              InputGlobal *Global)
     : GlobalSymbol(Name, DefinedGlobalKind, Flags, File,

Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Mon Mar 12 12:56:23 2018
@@ -76,18 +76,8 @@ public:
 
   void setHidden(bool IsHidden);
 
-  uint32_t getOutputIndex() const;
-
-  // Returns true if an output index has been set for this symbol
-  bool hasOutputIndex() const;
-
-  // Set the output index of the symbol, in the Wasm index space of the output
-  // object - that is, for defined symbols only, its position in the list of
-  // Wasm imports+code for functions, imports+globals for globals.
-  void setOutputIndex(uint32_t Index);
-
-  // Get/set the output symbol index, in the Symbol index space.  This is
-  // only used for relocatable output.
+  // Get/set the index in the output symbol table.  This is only used for
+  // relocatable output.
   uint32_t getOutputSymbolIndex() const;
   void setOutputSymbolIndex(uint32_t Index);
 
@@ -101,7 +91,6 @@ protected:
   Kind SymbolKind;
   uint32_t Flags;
   InputFile *File;
-  uint32_t OutputIndex = INVALID_INDEX;
   uint32_t OutputSymbolIndex = INVALID_INDEX;
 };
 
@@ -114,13 +103,15 @@ public:
 
   const WasmSignature *getFunctionType() const { return FunctionType; }
 
+  // Get/set the table index
+  void setTableIndex(uint32_t Index);
   uint32_t getTableIndex() const;
-
-  // Returns true if a table index has been set for this symbol
   bool hasTableIndex() const;
 
-  // Set the table index of the symbol
-  void setTableIndex(uint32_t Index);
+  // Get/set the function index
+  uint32_t getFunctionIndex() const;
+  void setFunctionIndex(uint32_t Index);
+  bool hasFunctionIndex() const;
 
 protected:
   FunctionSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
@@ -128,6 +119,7 @@ protected:
       : Symbol(Name, K, Flags, F), FunctionType(Type) {}
 
   uint32_t TableIndex = INVALID_INDEX;
+  uint32_t FunctionIndex = INVALID_INDEX;
 
   const WasmSignature *FunctionType;
 };
@@ -213,6 +205,11 @@ public:
 
   const WasmGlobalType *getGlobalType() const { return GlobalType; }
 
+  // Get/set the global index
+  uint32_t getGlobalIndex() const;
+  void setGlobalIndex(uint32_t Index);
+  bool hasGlobalIndex() const;
+
 protected:
   GlobalSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
                const WasmGlobalType *GlobalType)
@@ -221,6 +218,7 @@ protected:
   // Explicit function type, needed for undefined or synthetic functions only.
   // For regular defined globals this information comes from the InputChunk.
   const WasmGlobalType *GlobalType;
+  uint32_t GlobalIndex = INVALID_INDEX;
 };
 
 class DefinedGlobal : public GlobalSymbol {

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=327326&r1=327325&r2=327326&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Mon Mar 12 12:56:23 2018
@@ -67,7 +67,7 @@ struct WasmSignatureDenseMapInfo {
 // An init entry to be written to either the synthetic init func or the
 // linking metadata.
 struct WasmInitEntry {
-  const Symbol *Sym;
+  const FunctionSymbol *Sym;
   uint32_t Priority;
 };
 
@@ -287,10 +287,10 @@ void Writer::createExportSection() {
     WasmExport Export;
     DEBUG(dbgs() << "Export: " << Name << "\n");
 
-    if (isa<DefinedFunction>(Sym))
-      Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()};
-    else if (isa<DefinedGlobal>(Sym))
-      Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()};
+    if (auto *F = dyn_cast<DefinedFunction>(Sym))
+      Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
+    else if (auto *G = dyn_cast<DefinedGlobal>(Sym))
+      Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
     else if (isa<DefinedData>(Sym))
       Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
     else
@@ -317,7 +317,7 @@ void Writer::createElemSection() {
   uint32_t TableIndex = kInitialTableOffset;
   for (const FunctionSymbol *Sym : IndirectFunctions) {
     assert(Sym->getTableIndex() == TableIndex);
-    writeUleb128(OS, Sym->getOutputIndex(), "function index");
+    writeUleb128(OS, Sym->getFunctionIndex(), "function index");
     ++TableIndex;
   }
 }
@@ -428,14 +428,16 @@ void Writer::createLinkingSection() {
       writeU8(Sub.OS, Kind, "sym kind");
       writeUleb128(Sub.OS, Flags, "sym flags");
 
-      switch (Kind) {
-      case llvm::wasm::WASM_SYMBOL_TYPE_FUNCTION:
-      case llvm::wasm::WASM_SYMBOL_TYPE_GLOBAL:
-        writeUleb128(Sub.OS, Sym->getOutputIndex(), "index");
+      if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
+        writeUleb128(Sub.OS, F->getFunctionIndex(), "index");
         if (Sym->isDefined())
           writeStr(Sub.OS, Sym->getName(), "sym name");
-        break;
-      case llvm::wasm::WASM_SYMBOL_TYPE_DATA:
+      } else if (auto *G = dyn_cast<GlobalSymbol>(Sym)) {
+        writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
+        if (Sym->isDefined())
+          writeStr(Sub.OS, Sym->getName(), "sym name");
+      } else {
+        assert(isa<DataSymbol>(Sym));
         writeStr(Sub.OS, Sym->getName(), "sym name");
         if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
           writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
@@ -443,7 +445,6 @@ void Writer::createLinkingSection() {
                        "data offset");
           writeUleb128(Sub.OS, DataSym->getSize(), "data size");
         }
-        break;
       }
     }
 
@@ -481,7 +482,7 @@ void Writer::createLinkingSection() {
     StringRef Comdat = F->getComdat();
     if (!Comdat.empty())
       Comdats[Comdat].emplace_back(
-          ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()});
+          ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()});
   }
   for (uint32_t I = 0; I < Segments.size(); ++I) {
     const auto &InputSegments = Segments[I]->InputSegments;
@@ -531,14 +532,14 @@ void Writer::createNameSection() {
   // and InputFunctions are numbered in order with imported functions coming
   // first.
   for (const Symbol *S : ImportedSymbols) {
-    if (!isa<FunctionSymbol>(S))
-      continue;
-    writeUleb128(Sub.OS, S->getOutputIndex(), "import index");
-    writeStr(Sub.OS, S->getName(), "symbol name");
+    if (auto *F = dyn_cast<FunctionSymbol>(S)) {
+      writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
+      writeStr(Sub.OS, F->getName(), "symbol name");
+    }
   }
   for (const InputFunction *F : InputFunctions) {
     if (!F->getName().empty()) {
-      writeUleb128(Sub.OS, F->getOutputIndex(), "func index");
+      writeUleb128(Sub.OS, F->getFunctionIndex(), "func index");
       writeStr(Sub.OS, F->getName(), "symbol name");
     }
   }
@@ -658,10 +659,10 @@ void Writer::calculateImports() {
 
     DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
     ImportedSymbols.emplace_back(Sym);
-    if (isa<FunctionSymbol>(Sym))
-      Sym->setOutputIndex(NumImportedFunctions++);
+    if (auto *F = dyn_cast<FunctionSymbol>(Sym))
+      F->setFunctionIndex(NumImportedFunctions++);
     else
-      Sym->setOutputIndex(NumImportedGlobals++);
+      cast<GlobalSymbol>(Sym)->setGlobalIndex(NumImportedGlobals++);
   }
 }
 
@@ -753,7 +754,7 @@ void Writer::assignIndexes() {
     if (!Func->Live)
       return;
     InputFunctions.emplace_back(Func);
-    Func->setOutputIndex(FunctionIndex++);
+    Func->setFunctionIndex(FunctionIndex++);
   };
 
   for (InputFunction *Func : Symtab->SyntheticFunctions)
@@ -775,7 +776,7 @@ void Writer::assignIndexes() {
       if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 ||
           Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) {
         FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
-        if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
+        if (Sym->hasTableIndex() || !Sym->hasFunctionIndex())
           continue;
         Sym->setTableIndex(TableIndex++);
         IndirectFunctions.emplace_back(Sym);
@@ -806,7 +807,7 @@ void Writer::assignIndexes() {
   auto AddDefinedGlobal = [&](InputGlobal *Global) {
     if (Global->Live) {
       DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
-      Global->setOutputIndex(GlobalIndex++);
+      Global->setGlobalIndex(GlobalIndex++);
       InputGlobals.push_back(Global);
     }
   };
@@ -864,7 +865,7 @@ void Writer::createCtorFunction() {
     writeUleb128(OS, 0, "num locals");
     for (const WasmInitEntry &F : InitFunctions) {
       writeU8(OS, OPCODE_CALL, "CALL");
-      writeUleb128(OS, F.Sym->getOutputIndex(), "function index");
+      writeUleb128(OS, F.Sym->getFunctionIndex(), "function index");
     }
     writeU8(OS, OPCODE_END, "END");
   }




More information about the llvm-commits mailing list