[lld] r323379 - [WebAssembly] Don't duplicate functions in table output

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 13:45:25 PST 2018


Author: sbc
Date: Wed Jan 24 13:45:25 2018
New Revision: 323379

URL: http://llvm.org/viewvc/llvm-project?rev=323379&view=rev
Log:
[WebAssembly] Don't duplicate functions in table output

Previously, we were ensuring that the "output index" for
InputFunctions was unique across all symbols that referenced
a function body, but allowing the same function body to have
multiple table indexes.

Now, we use the same mechanism for table indexes as we already
do for output indexes, ensuring that each InputFunction is only
placed in the table once.

This makes the LLD output table denser and smaller, but should
not change the behaviour.

Note that we still need the `Symbol::TableIndex` member, to
store the table index for function Symbols that don't have an
InputFunction, i.e. for address-taken imports.

Patch by Nicholas Wilson!

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

Modified:
    lld/trunk/test/wasm/weak-alias.ll
    lld/trunk/wasm/InputChunks.cpp
    lld/trunk/wasm/InputChunks.h
    lld/trunk/wasm/Symbols.cpp
    lld/trunk/wasm/Symbols.h

Modified: lld/trunk/test/wasm/weak-alias.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/weak-alias.ll?rev=323379&r1=323378&r2=323379&view=diff
==============================================================================
--- lld/trunk/test/wasm/weak-alias.ll (original)
+++ lld/trunk/test/wasm/weak-alias.ll Wed Jan 24 13:45:25 2018
@@ -35,8 +35,8 @@ entry:
 ; CHECK-NEXT:       - ElemType:        ANYFUNC
 ; CHECK-NEXT:         Limits:
 ; CHECK-NEXT:           Flags:           [ HAS_MAX ]
-; CHECK-NEXT:           Initial:         0x00000003
-; CHECK-NEXT:           Maximum:         0x00000003
+; CHECK-NEXT:           Initial:         0x00000002
+; CHECK-NEXT:           Maximum:         0x00000002
 ; CHECK-NEXT:   - Type:            MEMORY
 ; CHECK-NEXT:     Memories:
 ; CHECK-NEXT:       - Initial:         0x00000002
@@ -88,7 +88,7 @@ entry:
 ; CHECK-NEXT:       - Offset:
 ; CHECK-NEXT:           Opcode:          I32_CONST
 ; CHECK-NEXT:           Value:           1
-; CHECK-NEXT:         Functions:       [ 1, 1 ]
+; CHECK-NEXT:         Functions:       [ 1 ]
 ; CHECK-NEXT:   - Type:            CODE
 ; CHECK-NEXT:     Functions:
 ; CHECK-NEXT:       - Index:           0
@@ -112,7 +112,7 @@ entry:
 ; CHECK-NEXT:         Locals:
 ; CHECK-NEXT:           - Type:            I32
 ; CHECK-NEXT:             Count:           2
-; CHECK-NEXT:         Body:            23808080800041106B220024808080800020004182808080003602081081808080002101200041106A24808080800020010B
+; CHECK-NEXT:         Body:            23808080800041106B220024808080800020004181808080003602081081808080002101200041106A24808080800020010B
 ; CHECK-NEXT:       - Index:           6
 ; CHECK-NEXT:         Locals:
 ; CHECK-NEXT:         Body:            0B
@@ -164,8 +164,8 @@ entry:
 ; RELOC-NEXT:       - ElemType:        ANYFUNC
 ; RELOC-NEXT:         Limits:
 ; RELOC-NEXT:           Flags:           [ HAS_MAX ]
-; RELOC-NEXT:           Initial:         0x00000003
-; RELOC-NEXT:           Maximum:         0x00000003
+; RELOC-NEXT:           Initial:         0x00000002
+; RELOC-NEXT:           Maximum:         0x00000002
 ; RELOC-NEXT:   - Type:            MEMORY
 ; RELOC-NEXT:     Memories:
 ; RELOC-NEXT:       - Initial:         0x00000000
@@ -197,7 +197,7 @@ entry:
 ; RELOC-NEXT:       - Offset:
 ; RELOC-NEXT:           Opcode:          I32_CONST
 ; RELOC-NEXT:           Value:           1
-; RELOC-NEXT:         Functions:       [ 1, 1 ]
+; RELOC-NEXT:         Functions:       [ 1 ]
 ; RELOC-NEXT:   - Type:            CODE
 ; RELOC-NEXT:     Relocations:
 ; RELOC-NEXT:       - Type:            R_WEBASSEMBLY_FUNCTION_INDEX_LEB
@@ -261,7 +261,7 @@ entry:
 ; RELOC-NEXT:         Locals:
 ; RELOC-NEXT:           - Type:            I32
 ; RELOC-NEXT:             Count:           2
-; RELOC-NEXT:         Body:            23808080800041106B220024808080800020004182808080003602081081808080002101200041106A24808080800020010B
+; RELOC-NEXT:         Body:            23808080800041106B220024808080800020004181808080003602081081808080002101200041106A24808080800020010B
 ; RELOC-NEXT:   - Type:            CUSTOM
 ; RELOC-NEXT:     Name:            linking
 ; RELOC-NEXT:     DataSize:        0

Modified: lld/trunk/wasm/InputChunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.cpp?rev=323379&r1=323378&r2=323379&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.cpp (original)
+++ lld/trunk/wasm/InputChunks.cpp Wed Jan 24 13:45:25 2018
@@ -117,3 +117,9 @@ void InputFunction::setOutputIndex(uint3
   assert(!hasOutputIndex());
   OutputIndex = Index;
 }
+
+void InputFunction::setTableIndex(uint32_t Index) {
+  DEBUG(dbgs() << "InputFunction::setTableIndex " << Index << "\n");
+  assert(!hasTableIndex());
+  TableIndex = Index;
+}

Modified: lld/trunk/wasm/InputChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.h?rev=323379&r1=323378&r2=323379&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.h (original)
+++ lld/trunk/wasm/InputChunks.h Wed Jan 24 13:45:25 2018
@@ -120,6 +120,9 @@ public:
   uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
   bool hasOutputIndex() const { return OutputIndex.hasValue(); }
   void setOutputIndex(uint32_t Index);
+  uint32_t getTableIndex() const { return TableIndex.getValue(); }
+  bool hasTableIndex() const { return TableIndex.hasValue(); }
+  void setTableIndex(uint32_t Index);
 
   const WasmSignature &Signature;
 
@@ -134,6 +137,7 @@ protected:
 
   const WasmFunction *Function;
   llvm::Optional<uint32_t> OutputIndex;
+  llvm::Optional<uint32_t> TableIndex;
 };
 
 class SyntheticFunction : public InputFunction {

Modified: lld/trunk/wasm/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.cpp?rev=323379&r1=323378&r2=323379&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.cpp (original)
+++ lld/trunk/wasm/Symbols.cpp Wed Jan 24 13:45:25 2018
@@ -67,7 +67,26 @@ void Symbol::setOutputIndex(uint32_t Ind
   OutputIndex = Index;
 }
 
+uint32_t Symbol::getTableIndex() const {
+  if (Function)
+    return Function->getTableIndex();
+  return TableIndex.getValue();
+}
+
+bool Symbol::hasTableIndex() const {
+  if (Function)
+    return Function->hasTableIndex();
+  return TableIndex.hasValue();
+}
+
 void Symbol::setTableIndex(uint32_t Index) {
+  // 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);
+    return;
+  }
   DEBUG(dbgs() << "setTableIndex " << Name << " -> " << Index << "\n");
   assert(!TableIndex.hasValue());
   TableIndex = Index;

Modified: lld/trunk/wasm/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Symbols.h?rev=323379&r1=323378&r2=323379&view=diff
==============================================================================
--- lld/trunk/wasm/Symbols.h (original)
+++ lld/trunk/wasm/Symbols.h Wed Jan 24 13:45:25 2018
@@ -77,10 +77,10 @@ public:
   // space of the output object.
   void setOutputIndex(uint32_t Index);
 
-  uint32_t getTableIndex() const { return TableIndex.getValue(); }
+  uint32_t getTableIndex() const;
 
   // Returns true if a table index has been set for this symbol
-  bool hasTableIndex() const { return TableIndex.hasValue(); }
+  bool hasTableIndex() const;
 
   // Set the table index of the symbol
   void setTableIndex(uint32_t Index);




More information about the llvm-commits mailing list