[lld] r323621 - [WebAssemly] Associate symbol with InputChunk in which they are defined. NFC.

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 28 11:57:01 PST 2018


Author: sbc
Date: Sun Jan 28 11:57:01 2018
New Revision: 323621

URL: http://llvm.org/viewvc/llvm-project?rev=323621&view=rev
Log:
[WebAssemly] Associate symbol with InputChunk in which they are defined. NFC.

Summary:
Rather than explicit Function or InputSegment points store a
pointer to the base class (InputChunk) and use llvm dynamic
casts when we need a subtype.

This change is useful for add the upcoming gc-section support
wants to deal with all input chunks.

Subscribers: aheejin, llvm-commits

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

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

Modified: lld/trunk/wasm/InputChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.h?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.h (original)
+++ lld/trunk/wasm/InputChunks.h Sun Jan 28 11:57:01 2018
@@ -34,6 +34,10 @@ class OutputSegment;
 
 class InputChunk {
 public:
+  enum Kind { DataSegment, Function };
+
+  Kind kind() const { return SectionKind; };
+
   uint32_t getSize() const { return data().size(); }
 
   void copyRelocations(const WasmSection &Section);
@@ -54,7 +58,7 @@ public:
   std::vector<OutputRelocation> OutRelocations;
 
 protected:
-  InputChunk(const ObjFile *F) : File(F) {}
+  InputChunk(const ObjFile *F, Kind K) : File(F), SectionKind(K) {}
   virtual ~InputChunk() = default;
   void calcRelocations();
   virtual ArrayRef<uint8_t> data() const = 0;
@@ -63,6 +67,7 @@ protected:
   std::vector<WasmRelocation> Relocations;
   int32_t OutputOffset = 0;
   const ObjFile *File;
+  Kind SectionKind;
 };
 
 // Represents a WebAssembly data segment which can be included as part of
@@ -76,7 +81,9 @@ protected:
 class InputSegment : public InputChunk {
 public:
   InputSegment(const WasmSegment &Seg, const ObjFile *F)
-      : InputChunk(F), Segment(Seg) {}
+      : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
+
+  static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
 
   // Translate an offset in the input segment to an offset in the output
   // segment.
@@ -113,7 +120,9 @@ class InputFunction : public InputChunk
 public:
   InputFunction(const WasmSignature &S, const WasmFunction *Func,
                 const ObjFile *F)
-      : InputChunk(F), Signature(S), Function(Func) {}
+      : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
+
+  static bool classof(const InputChunk *C) { return C->kind() == InputChunk::Function; }
 
   virtual StringRef getName() const { return Function->Name; }
   StringRef getComdat() const override { return Function->Comdat; }

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Sun Jan 28 11:57:01 2018
@@ -245,8 +245,7 @@ void ObjFile::initializeSymbols() {
     case WasmSymbol::SymbolType::FUNCTION_EXPORT: {
       InputFunction *Function = getFunction(WasmSym);
       if (!isExcludedByComdat(Function)) {
-        S = createDefined(WasmSym, Symbol::Kind::DefinedFunctionKind, nullptr,
-                          Function);
+        S = createDefined(WasmSym, Symbol::Kind::DefinedFunctionKind, Function);
         break;
       } else {
         Function->Discarded = true;
@@ -261,7 +260,7 @@ void ObjFile::initializeSymbols() {
       InputSegment *Segment = getSegment(WasmSym);
       if (!isExcludedByComdat(Segment)) {
         S = createDefined(WasmSym, Symbol::Kind::DefinedGlobalKind,
-                          Segment, nullptr, getGlobalValue(WasmSym));
+                          Segment, getGlobalValue(WasmSym));
         break;
       } else {
         Segment->Discarded = true;
@@ -300,15 +299,14 @@ Symbol *ObjFile::createUndefined(const W
 }
 
 Symbol *ObjFile::createDefined(const WasmSymbol &Sym, Symbol::Kind Kind,
-                               const InputSegment *Segment,
-                               InputFunction *Function, uint32_t Address) {
+                               InputChunk *Chunk, uint32_t Address) {
   Symbol *S;
   if (Sym.isLocal()) {
     S = make<Symbol>(Sym.Name, true);
-    S->update(Kind, this, Sym.Flags, Segment, Function, Address);
+    S->update(Kind, this, Sym.Flags, Chunk, Address);
     return S;
   }
-  return Symtab->addDefined(Sym.Name, Kind, Sym.Flags, this, Segment, Function,
+  return Symtab->addDefined(Sym.Name, Kind, Sym.Flags, this, Chunk,
                             Address);
 }
 

Modified: lld/trunk/wasm/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.h?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.h (original)
+++ lld/trunk/wasm/InputFiles.h Sun Jan 28 11:57:01 2018
@@ -117,8 +117,7 @@ private:
   uint32_t relocateTableIndex(uint32_t Original) const;
 
   Symbol *createDefined(const WasmSymbol &Sym, Symbol::Kind Kind,
-                        const InputSegment *Segment = nullptr,
-                        InputFunction *Function = nullptr,
+                        InputChunk *Chunk = nullptr,
                         uint32_t Address = UINT32_MAX);
   Symbol *createUndefined(const WasmSymbol &Sym, Symbol::Kind Kind,
                           const WasmSignature *Signature = nullptr);

Modified: lld/trunk/wasm/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/SymbolTable.cpp?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/SymbolTable.cpp (original)
+++ lld/trunk/wasm/SymbolTable.cpp Sun Jan 28 11:57:01 2018
@@ -119,6 +119,14 @@ static void checkSymbolTypes(const Symbo
         " in " + F.getName());
 }
 
+static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
+                             Symbol::Kind Kind, const InputChunk *Chunk) {
+  const WasmSignature* Sig = nullptr;
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk))
+    Sig = &F->Signature;
+  return checkSymbolTypes(Existing, F, Kind, Sig);
+}
+
 Symbol *SymbolTable::addDefinedFunction(StringRef Name,
                                         const WasmSignature *Type,
                                         uint32_t Flags) {
@@ -156,26 +164,25 @@ Symbol *SymbolTable::addDefinedGlobal(St
 
 Symbol *SymbolTable::addDefined(StringRef Name, Symbol::Kind Kind,
                                 uint32_t Flags, InputFile *F,
-                                const InputSegment *Segment,
-                                InputFunction *Function, uint32_t Address) {
+                                InputChunk *Chunk, uint32_t Address) {
   DEBUG(dbgs() << "addDefined: " << Name << " addr:" << Address << "\n");
   Symbol *S;
   bool WasInserted;
 
   std::tie(S, WasInserted) = insert(Name);
   if (WasInserted) {
-    S->update(Kind, F, Flags, Segment, Function, Address);
+    S->update(Kind, F, Flags, Chunk, Address);
   } else if (S->isLazy()) {
     // The existing symbol is lazy. Replace it without checking types since
     // lazy symbols don't have any type information.
     DEBUG(dbgs() << "replacing existing lazy symbol: " << Name << "\n");
-    S->update(Kind, F, Flags, Segment, Function, Address);
+    S->update(Kind, F, Flags, Chunk, Address);
   } else if (!S->isDefined()) {
     // The existing symbol table entry is undefined. The new symbol replaces
     // it, after checking the type matches
     DEBUG(dbgs() << "resolving existing undefined symbol: " << Name << "\n");
-    checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
-    S->update(Kind, F, Flags, Segment, Function, Address);
+    checkSymbolTypes(*S, *F, Kind, Chunk);
+    S->update(Kind, F, Flags, Chunk, Address);
   } else if ((Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
     // the new symbol is weak we can ignore it
     DEBUG(dbgs() << "existing symbol takes precedence\n");
@@ -183,8 +190,8 @@ Symbol *SymbolTable::addDefined(StringRe
     // the new symbol is not weak and the existing symbol is, so we replace
     // it
     DEBUG(dbgs() << "replacing existing weak symbol\n");
-    checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
-    S->update(Kind, F, Flags, Segment, Function, Address);
+    checkSymbolTypes(*S, *F, Kind, Chunk);
+    S->update(Kind, F, Flags, Chunk, Address);
   } else {
     // neither symbol is week. They conflict.
     reportDuplicate(S, F);

Modified: lld/trunk/wasm/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/SymbolTable.h?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/SymbolTable.h (original)
+++ lld/trunk/wasm/SymbolTable.h Sun Jan 28 11:57:01 2018
@@ -50,8 +50,8 @@ public:
   ObjFile *findComdat(StringRef Name) const;
 
   Symbol *addDefined(StringRef Name, Symbol::Kind Kind, uint32_t Flags,
-                     InputFile *F, const InputSegment *Segment = nullptr,
-                     InputFunction *Function = nullptr, uint32_t Address = 0);
+                     InputFile *F,
+                     InputChunk *Chunk = nullptr, uint32_t Address = 0);
   Symbol *addUndefined(StringRef Name, Symbol::Kind Kind, uint32_t Flags,
                        InputFile *F, const WasmSignature *Signature = nullptr);
   Symbol *addUndefinedFunction(StringRef Name, const WasmSignature *Type);

Modified: lld/trunk/wasm/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.cpp?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.cpp (original)
+++ lld/trunk/wasm/Symbols.cpp Sun Jan 28 11:57:01 2018
@@ -23,8 +23,8 @@ using namespace lld;
 using namespace lld::wasm;
 
 const WasmSignature &Symbol::getFunctionType() const {
-  if (Function != nullptr)
-    return Function->Signature;
+  if (Chunk != nullptr)
+    return dyn_cast<InputFunction>(Chunk)->Signature;
 
   assert(FunctionType != nullptr);
   return *FunctionType;
@@ -32,25 +32,25 @@ const WasmSignature &Symbol::getFunction
 
 void Symbol::setFunctionType(const WasmSignature *Type) {
   assert(FunctionType == nullptr);
-  assert(Function == nullptr);
+  assert(!Chunk);
   FunctionType = Type;
 }
 
 uint32_t Symbol::getVirtualAddress() const {
   assert(isGlobal());
   DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
-  return Segment ? Segment->translateVA(VirtualAddress) : VirtualAddress;
+  return Chunk ? dyn_cast<InputSegment>(Chunk)->translateVA(VirtualAddress) : VirtualAddress;
 }
 
 bool Symbol::hasOutputIndex() const {
-  if (Function)
-    return Function->hasOutputIndex();
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk))
+    return F->hasOutputIndex();
   return OutputIndex.hasValue();
 }
 
 uint32_t Symbol::getOutputIndex() const {
-  if (Function)
-    return Function->getOutputIndex();
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk))
+    return F->getOutputIndex();
   return OutputIndex.getValue();
 }
 
@@ -62,20 +62,20 @@ void Symbol::setVirtualAddress(uint32_t
 
 void Symbol::setOutputIndex(uint32_t Index) {
   DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
-  assert(!Function);
+  assert(!dyn_cast_or_null<InputFunction>(Chunk));
   assert(!OutputIndex.hasValue());
   OutputIndex = Index;
 }
 
 uint32_t Symbol::getTableIndex() const {
-  if (Function)
-    return Function->getTableIndex();
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk))
+    return F->getTableIndex();
   return TableIndex.getValue();
 }
 
 bool Symbol::hasTableIndex() const {
-  if (Function)
-    return Function->hasTableIndex();
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk))
+    return F->hasTableIndex();
   return TableIndex.hasValue();
 }
 
@@ -83,8 +83,8 @@ void Symbol::setTableIndex(uint32_t Inde
   // For imports, we set the table index here on the Symbol; for defined
   // functions we set the index on the InputFunction so that we don't export
   // the same thing twice (keeps the table size down).
-  if (Function) {
-    Function->setTableIndex(Index);
+  if (auto* F = dyn_cast_or_null<InputFunction>(Chunk)) {
+    F->setTableIndex(Index);
     return;
   }
   DEBUG(dbgs() << "setTableIndex " << Name << " -> " << Index << "\n");
@@ -93,13 +93,12 @@ void Symbol::setTableIndex(uint32_t Inde
 }
 
 void Symbol::update(Kind K, InputFile *F, uint32_t Flags_,
-                    const InputSegment *Seg, InputFunction *Func,
+                    InputChunk *Chunk_,
                     uint32_t Address) {
   SymbolKind = K;
   File = F;
   Flags = Flags_;
-  Segment = Seg;
-  Function = Func;
+  Chunk = Chunk_;
   if (Address != UINT32_MAX)
     setVirtualAddress(Address);
 }

Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Sun Jan 28 11:57:01 2018
@@ -21,8 +21,7 @@ namespace lld {
 namespace wasm {
 
 class InputFile;
-class InputSegment;
-class InputFunction;
+class InputChunk;
 
 class Symbol {
 public:
@@ -62,6 +61,7 @@ public:
 
   // Returns the file from which this symbol was created.
   InputFile *getFile() const { return File; }
+  InputChunk *getChunk() const { return Chunk; }
 
   bool hasFunctionType() const { return FunctionType != nullptr; }
   const WasmSignature &getFunctionType() const;
@@ -92,12 +92,11 @@ public:
   void setVirtualAddress(uint32_t VA);
 
   void update(Kind K, InputFile *F = nullptr, uint32_t Flags = 0,
-              const InputSegment *Segment = nullptr,
-              InputFunction *Function = nullptr, uint32_t Address = UINT32_MAX);
+              InputChunk *chunk = nullptr,
+              uint32_t Address = UINT32_MAX);
 
   void setArchiveSymbol(const Archive::Symbol &Sym) { ArchiveSymbol = Sym; }
   const Archive::Symbol &getArchiveSymbol() { return ArchiveSymbol; }
-  InputFunction *getFunction() { return Function; }
 
 protected:
   uint32_t Flags;
@@ -107,8 +106,7 @@ protected:
   Archive::Symbol ArchiveSymbol = {nullptr, 0, 0};
   Kind SymbolKind = InvalidKind;
   InputFile *File = nullptr;
-  const InputSegment *Segment = nullptr;
-  InputFunction *Function = nullptr;
+  InputChunk *Chunk = nullptr;
   llvm::Optional<uint32_t> OutputIndex;
   llvm::Optional<uint32_t> TableIndex;
   const WasmSignature *FunctionType = nullptr;

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=323621&r1=323620&r2=323621&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Sun Jan 28 11:57:01 2018
@@ -648,7 +648,7 @@ void Writer::calculateExports() {
         continue;
       if (Sym->isGlobal())
         continue;
-      if (Sym->getFunction()->Discarded)
+      if (Sym->getChunk()->Discarded)
         continue;
 
       if ((Sym->isHidden() || Sym->isLocal()) && !ExportHidden)




More information about the llvm-commits mailing list