[llvm-branch-commits] [lld] 53e3b81 - [lld][WebAssembly] Add support for handling table symbols

2021-01-14 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-14T11:13:13+01:00
New Revision: 53e3b81faaf32a495189182e0e4d635cbe19c5dd

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

LOG: [lld][WebAssembly] Add support for handling table symbols

This commit adds table symbol support in a partial way, while still
including some special cases for the __indirect_function_table symbol.
No change in tests.

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

Added: 
lld/wasm/InputTable.h

Modified: 
lld/include/lld/Common/LLVM.h
lld/wasm/InputChunks.cpp
lld/wasm/InputFiles.cpp
lld/wasm/InputFiles.h
lld/wasm/MarkLive.cpp
lld/wasm/SymbolTable.cpp
lld/wasm/SymbolTable.h
lld/wasm/Symbols.cpp
lld/wasm/Symbols.h
lld/wasm/SyntheticSections.cpp
lld/wasm/SyntheticSections.h
lld/wasm/Writer.cpp
lld/wasm/WriterUtils.cpp
lld/wasm/WriterUtils.h
llvm/include/llvm/BinaryFormat/Wasm.h
llvm/include/llvm/Object/Wasm.h
llvm/lib/Object/WasmObjectFile.cpp
llvm/tools/llvm-readobj/WasmDumper.cpp

Removed: 




diff  --git a/lld/include/lld/Common/LLVM.h b/lld/include/lld/Common/LLVM.h
index 663944771aa9..f6eca27b02ff 100644
--- a/lld/include/lld/Common/LLVM.h
+++ b/lld/include/lld/Common/LLVM.h
@@ -49,8 +49,11 @@ struct WasmEventType;
 struct WasmFunction;
 struct WasmGlobal;
 struct WasmGlobalType;
+struct WasmLimits;
 struct WasmRelocation;
 struct WasmSignature;
+struct WasmTable;
+struct WasmTableType;
 } // namespace wasm
 } // namespace llvm
 
@@ -88,8 +91,11 @@ using llvm::wasm::WasmEventType;
 using llvm::wasm::WasmFunction;
 using llvm::wasm::WasmGlobal;
 using llvm::wasm::WasmGlobalType;
+using llvm::wasm::WasmLimits;
 using llvm::wasm::WasmRelocation;
 using llvm::wasm::WasmSignature;
+using llvm::wasm::WasmTable;
+using llvm::wasm::WasmTableType;
 } // end namespace lld.
 
 namespace std {

diff  --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 52d19e6ddc10..d8c5f84c0487 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -69,6 +69,7 @@ void InputChunk::verifyRelocTargets() const {
 case R_WASM_GLOBAL_INDEX_LEB:
 case R_WASM_EVENT_INDEX_LEB:
 case R_WASM_MEMORY_ADDR_LEB:
+case R_WASM_TABLE_NUMBER_LEB:
   existingValue = decodeULEB128(loc, &bytesRead);
   break;
 case R_WASM_MEMORY_ADDR_LEB64:
@@ -152,6 +153,7 @@ void InputChunk::writeTo(uint8_t *buf) const {
 case R_WASM_GLOBAL_INDEX_LEB:
 case R_WASM_EVENT_INDEX_LEB:
 case R_WASM_MEMORY_ADDR_LEB:
+case R_WASM_TABLE_NUMBER_LEB:
   encodeULEB128(value, loc, 5);
   break;
 case R_WASM_MEMORY_ADDR_LEB64:
@@ -233,6 +235,7 @@ static unsigned writeCompressedReloc(uint8_t *buf, const 
WasmRelocation &rel,
   case R_WASM_EVENT_INDEX_LEB:
   case R_WASM_MEMORY_ADDR_LEB:
   case R_WASM_MEMORY_ADDR_LEB64:
+  case R_WASM_TABLE_NUMBER_LEB:
 return encodeULEB128(value, buf);
   case R_WASM_TABLE_INDEX_SLEB:
   case R_WASM_TABLE_INDEX_SLEB64:
@@ -251,6 +254,7 @@ static unsigned getRelocWidthPadded(const WasmRelocation 
&rel) {
   case R_WASM_GLOBAL_INDEX_LEB:
   case R_WASM_EVENT_INDEX_LEB:
   case R_WASM_MEMORY_ADDR_LEB:
+  case R_WASM_TABLE_NUMBER_LEB:
   case R_WASM_TABLE_INDEX_SLEB:
   case R_WASM_MEMORY_ADDR_SLEB:
 return 5;

diff  --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index eb37ae548b80..1d0f016f325a 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -11,6 +11,7 @@
 #include "InputChunks.h"
 #include "InputEvent.h"
 #include "InputGlobal.h"
+#include "InputTable.h"
 #include "OutputSegment.h"
 #include "SymbolTable.h"
 #include "lld/Common/ErrorHandler.h"
@@ -94,7 +95,8 @@ void ObjFile::dumpInfo() const {
   "\n  Symbols : " + Twine(symbols.size()) +
   "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) 
+
   "\n   Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) +
-  "\nEvent Imports : " + Twine(wasmObj->getNumImportedEvents()));
+  "\nEvent Imports : " + Twine(wasmObj->getNumImportedEvents()) +
+  "\nTable Imports : " + Twine(wasmObj->getNumImportedTables()));
 }
 
 // Relocations contain either symbol or type indices.  This function takes a
@@ -188,7 +190,8 @@ uint64_t ObjFile::calcExpectedValue(const WasmRelocation 
&reloc) const {
   case R_WASM_FUNCTION_INDEX_LEB:
   case R_WASM_GLOBAL_INDEX_LEB:
   case R_WASM_GLOBAL_INDEX_I32:
-  case R_WASM_EVENT_INDEX_LEB: {
+  case R_WASM_EVENT_INDEX_LEB:
+  case R_WASM_TABLE_NUMBER_LEB: {
 const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
 return sym.Info.ElementIndex;
   }
@@ -270,6 +273,8 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, 
uint64_t tombstone)
   }
   case R_WASM_SECTION_OFFSET_I32:

[llvm-branch-commits] [lld] 38dfce7 - [WebAssembly] Add support for table linking to wasm-ld

2021-01-15 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-15T09:21:52+01:00
New Revision: 38dfce706f796dc109ea495dd69a8cb4c8fa819d

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

LOG: [WebAssembly] Add support for table linking to wasm-ld

This patch adds support to wasm-ld for linking multiple table references
together, in a manner similar to wasm globals. The indirect function
table is synthesized as needed.

To manage the transitional period in which the compiler doesn't yet
produce TABLE_NUMBER relocations and doesn't residualize table symbols,
the linker will detect object files which have table imports or
definitions, but no table symbols. In that case it will synthesize
symbols for the defined and imported tables.

As a change, relocatable objects are now written with table symbols,
which can cause symbol renumbering in some of the tests. If no object
file requires an indirect function table, none will be written to the
file. Note that for legacy ObjFile inputs, this test is conservative: as
we don't have relocs for each use of the indirecy function table, we
just assume that any incoming indirect function table should be
propagated to the output.

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

Added: 


Modified: 
lld/test/wasm/alias.s
lld/test/wasm/init-fini.ll
lld/test/wasm/local-symbols.ll
lld/test/wasm/locals-duplicate.test
lld/test/wasm/pie.ll
lld/test/wasm/section-symbol-relocs.yaml
lld/test/wasm/shared.ll
lld/test/wasm/signature-mismatch.ll
lld/test/wasm/stack-pointer.ll
lld/test/wasm/weak-alias.ll
lld/wasm/Driver.cpp
lld/wasm/InputFiles.cpp
lld/wasm/InputFiles.h
lld/wasm/MarkLive.cpp
lld/wasm/SymbolTable.cpp
lld/wasm/SymbolTable.h
lld/wasm/Symbols.cpp
lld/wasm/Symbols.h
lld/wasm/SyntheticSections.cpp
lld/wasm/SyntheticSections.h
lld/wasm/Writer.cpp

Removed: 




diff  --git a/lld/test/wasm/alias.s b/lld/test/wasm/alias.s
index 6c99f69da8b8..b2ab45e98d95 100644
--- a/lld/test/wasm/alias.s
+++ b/lld/test/wasm/alias.s
@@ -22,14 +22,6 @@ _start:
 # CHECK-NEXT: ReturnTypes: []
 # CHECK-NEXT:   - Type:FUNCTION
 # CHECK-NEXT: FunctionTypes:   [ 0 ]
-# CHECK-NEXT:   - Type:TABLE
-# CHECK-NEXT: Tables:
-# CHECK-NEXT:   - Index:   0
-# CHECK-NEXT: ElemType:FUNCREF
-# CHECK-NEXT: Limits:
-# CHECK-NEXT:   Flags:   [ HAS_MAX ]
-# CHECK-NEXT:   Initial: 0x1
-# CHECK-NEXT:   Maximum: 0x1
 # CHECK-NEXT:   - Type:MEMORY
 # CHECK-NEXT: Memories:
 # CHECK-NEXT:   - Initial: 0x2

diff  --git a/lld/test/wasm/init-fini.ll b/lld/test/wasm/init-fini.ll
index 1e7644bc6d35..5631d58d68e5 100644
--- a/lld/test/wasm/init-fini.ll
+++ b/lld/test/wasm/init-fini.ll
@@ -139,15 +139,15 @@ entry:
 ; RELOC-NEXT:  InitFunctions [
 ; RELOC-NEXT:0 (priority=101)
 ; RELOC-NEXT:1 (priority=101)
-; RELOC-NEXT:14 (priority=101)
-; RELOC-NEXT:10 (priority=101)
-; RELOC-NEXT:20 (priority=101)
-; RELOC-NEXT:10 (priority=202)
-; RELOC-NEXT:22 (priority=202)
+; RELOC-NEXT:15 (priority=101)
+; RELOC-NEXT:11 (priority=101)
+; RELOC-NEXT:21 (priority=101)
+; RELOC-NEXT:11 (priority=202)
+; RELOC-NEXT:23 (priority=202)
 ; RELOC-NEXT:0 (priority=1001)
-; RELOC-NEXT:16 (priority=1001)
-; RELOC-NEXT:10 (priority=2002)
-; RELOC-NEXT:24 (priority=2002)
+; RELOC-NEXT:17 (priority=1001)
+; RELOC-NEXT:11 (priority=2002)
+; RELOC-NEXT:25 (priority=2002)
 ; RELOC-NEXT:9 (priority=4000)
-; RELOC-NEXT:18 (priority=4000)
+; RELOC-NEXT:19 (priority=4000)
 ; RELOC-NEXT:  ]

diff  --git a/lld/test/wasm/local-symbols.ll b/lld/test/wasm/local-symbols.ll
index 13c200d648e9..216aced9cf07 100644
--- a/lld/test/wasm/local-symbols.ll
+++ b/lld/test/wasm/local-symbols.ll
@@ -35,14 +35,6 @@ entry:
 ; CHECK-NEXT: ReturnTypes: []
 ; CHECK-NEXT:   - Type:FUNCTION
 ; CHECK-NEXT: FunctionTypes:   [ 0, 1 ]
-; CHECK-NEXT:   - Type:TABLE
-; CHECK-NEXT: Tables:
-; CHECK-NEXT:   - Index:   0
-; CHECK-NEXT: ElemType:FUNCREF
-; CHECK-NEXT: Limits:
-; CHECK-NEXT:   Flags:   [ HAS_MAX ]
-; CHECK-NEXT:   Initial: 0x1
-; CHECK-NEXT:   Maximum: 0x1
 ; CHECK-NEXT:   - Type:MEMORY
 ; CHECK-NEXT: Memories:
 ; CHECK-NEXT:   - Initial: 0x2

diff  --git a/lld/test/wasm/locals-duplicate.test 
b/lld/test/wasm/locals-duplicate.test
index 07abb7485381..cf9a148d4ab7 100644
--- a/lld/test/wasm/locals-duplicate.test
+++ b/lld/test/wasm/locals-duplicate.test
@@ -254,40 +254,40 @@
 ; RELOC-NEXT:   - Type:   

[llvm-branch-commits] [llvm] e9f1ed2 - [WebAssembly] MC layer writes table symbols to object files

2021-01-15 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-15T14:55:55+01:00
New Revision: e9f1ed2306b1b3aedcb1acef2b50e252a77a04b9

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

LOG: [WebAssembly] MC layer writes table symbols to object files

Now that the linker handles table symbols, we can allow the frontend to
produce them.

Depends on D91870.

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

Added: 


Modified: 
llvm/lib/MC/WasmObjectWriter.cpp
llvm/test/MC/WebAssembly/debug-info.ll
llvm/test/MC/WebAssembly/debug-info64.ll
llvm/test/MC/WebAssembly/function-alias.ll
llvm/test/MC/WebAssembly/global-ctor-dtor.ll
llvm/test/MC/WebAssembly/reloc-pic.s
llvm/test/MC/WebAssembly/type-index.s
llvm/test/MC/WebAssembly/weak-alias.s

Removed: 




diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index fb41c3763c15..b2d94dcadd81 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -519,6 +519,13 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
   Sym->setUndefined();
 }
 Sym->setUsedInReloc();
+// Any time we have a TABLE_INDEX relocation against a function symbol, we
+// need to ensure that table itself is part of the final output too.  In 
the
+// future we may want to define a new kind of reloc against both the
+// function and the table, so that the linker can see that the function
+// symbol keeps the table alive, but for now manually mark the table as
+// live.
+Sym->setNoStrip();
 Asm.registerSymbol(*Sym);
   }
 
@@ -1670,10 +1677,6 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler 
&Asm,
   WS.setIndex(InvalidIndex);
   continue;
 }
-if (WS.isTable() && WS.getName() == "__indirect_function_table") {
-  // For the moment, don't emit table symbols -- wasm-ld can't handle them.
-  continue;
-}
 LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
 
 uint32_t Flags = 0;

diff  --git a/llvm/test/MC/WebAssembly/debug-info.ll 
b/llvm/test/MC/WebAssembly/debug-info.ll
index 167d912c84a2..88d19d9191a9 100644
--- a/llvm/test/MC/WebAssembly/debug-info.ll
+++ b/llvm/test/MC/WebAssembly/debug-info.ll
@@ -89,44 +89,44 @@
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
-; CHECK-NEXT:Size: 91
+; CHECK-NEXT:Size: 95
 ; CHECK-NEXT:Offset: 731
 ; CHECK-NEXT:Name: linking
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 9
-; CHECK-NEXT:Offset: 836
+; CHECK-NEXT:Offset: 840
 ; CHECK-NEXT:Name: reloc.DATA
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 61
-; CHECK-NEXT:Offset: 862
+; CHECK-NEXT:Offset: 866
 ; CHECK-NEXT:Name: reloc..debug_info
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 947
+; CHECK-NEXT:Offset: 951
 ; CHECK-NEXT:Name: reloc..debug_pubnames
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 981
+; CHECK-NEXT:Offset: 985
 ; CHECK-NEXT:Name: reloc..debug_pubtypes
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 1015
+; CHECK-NEXT:Offset: 1019
 ; CHECK-NEXT:Name: reloc..debug_line
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 77
-; CHECK-NEXT:Offset: 1045
+; CHECK-NEXT:Offset: 1049
 ; CHECK-NEXT:Name: producers
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:]
@@ -238,6 +238,16 @@
 ; CHECK-NEXT:]
 ; CHECK-NEXT:ElementIndex: 0xC
 ; CHECK-NEXT:  }
+; CHECK-NEXT:  Symbol {
+; CHECK-NEXT:Name: __indirect_function_table
+; CHECK-NEXT:Type: TABLE (0x5)
+; CHECK-NEXT:Flags [ (0x90)
+; CHECK-NEXT:  NO_STRIP (0x80)
+; CHECK-NEXT:  UNDEFINED (0x10)
+; CHECK-NEXT:]
+; CHECK-NEXT:ImportModule: env
+; CHECK-NEXT:ElementIndex: 0x0
+; CHECK-NEXT:  }
 ; CHECK-NEXT:]
 
 ; generated from the following C code using: clang --target=wasm32 -g -O0 -S 
-emit-llvm test.c

diff  --git a/llvm/test/MC/WebAssembly/debug-info64.ll 
b/llvm/test/MC/WebAssembly/debug-info64.ll
index acba8aa27360..1755f2b7ef5c 100644
--- a/llvm/test/MC/WebAssembly/debug-info64.ll
+++ b/llvm/test/MC/WebAssembly/debug-info64.ll
@@ -89,44 +89,44 @@
 ; CHECK-NEXT:   }
 ; CHECK-NEXT:   Section {
 ; CHECK-NEXT: Type: CUSTOM (0x0)
-; CHECK-NEXT: Size: 91
+; CHECK-NEXT: Size: 95
 ; CHECK-NEXT: Offset: 759
 ; CHECK-NEXT: Name: linking
 ; CHECK-NEXT:   }
 ; CHECK-NEXT:   Section {
 ; CHECK-NEXT

[llvm-branch-commits] [lld] 6339382 - [WebAssembly] Add support for table linking to wasm-ld

2021-01-18 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-18T16:57:18+01:00
New Revision: 63393828078c382e8e69d9b8146372b70bbce20f

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

LOG: [WebAssembly] Add support for table linking to wasm-ld

This patch adds support to wasm-ld for linking multiple table references
together, in a manner similar to wasm globals. The indirect function
table is synthesized as needed.

To manage the transitional period in which the compiler doesn't yet
produce TABLE_NUMBER relocations and doesn't residualize table symbols,
the linker will detect object files which have table imports or
definitions, but no table symbols. In that case it will synthesize
symbols for the defined and imported tables.

As a change, relocatable objects are now written with table symbols,
which can cause symbol renumbering in some of the tests. If no object
file requires an indirect function table, none will be written to the
file. Note that for legacy ObjFile inputs, this test is conservative: as
we don't have relocs for each use of the indirecy function table, we
just assume that any incoming indirect function table should be
propagated to the output.

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

Added: 
lld/test/wasm/export-table-explicit.test

Modified: 
lld/test/wasm/alias.s
lld/test/wasm/init-fini.ll
lld/test/wasm/local-symbols.ll
lld/test/wasm/locals-duplicate.test
lld/test/wasm/pie.ll
lld/test/wasm/section-symbol-relocs.yaml
lld/test/wasm/shared.ll
lld/test/wasm/signature-mismatch.ll
lld/test/wasm/stack-pointer.ll
lld/test/wasm/weak-alias.ll
lld/wasm/Driver.cpp
lld/wasm/InputFiles.cpp
lld/wasm/InputFiles.h
lld/wasm/MarkLive.cpp
lld/wasm/SymbolTable.cpp
lld/wasm/SymbolTable.h
lld/wasm/Symbols.cpp
lld/wasm/Symbols.h
lld/wasm/SyntheticSections.cpp
lld/wasm/SyntheticSections.h
lld/wasm/Writer.cpp

Removed: 




diff  --git a/lld/test/wasm/alias.s b/lld/test/wasm/alias.s
index 6c99f69da8b87..b2ab45e98d959 100644
--- a/lld/test/wasm/alias.s
+++ b/lld/test/wasm/alias.s
@@ -22,14 +22,6 @@ _start:
 # CHECK-NEXT: ReturnTypes: []
 # CHECK-NEXT:   - Type:FUNCTION
 # CHECK-NEXT: FunctionTypes:   [ 0 ]
-# CHECK-NEXT:   - Type:TABLE
-# CHECK-NEXT: Tables:
-# CHECK-NEXT:   - Index:   0
-# CHECK-NEXT: ElemType:FUNCREF
-# CHECK-NEXT: Limits:
-# CHECK-NEXT:   Flags:   [ HAS_MAX ]
-# CHECK-NEXT:   Initial: 0x1
-# CHECK-NEXT:   Maximum: 0x1
 # CHECK-NEXT:   - Type:MEMORY
 # CHECK-NEXT: Memories:
 # CHECK-NEXT:   - Initial: 0x2

diff  --git a/lld/test/wasm/export-table-explicit.test 
b/lld/test/wasm/export-table-explicit.test
new file mode 100644
index 0..b6f03b99eb607
--- /dev/null
+++ b/lld/test/wasm/export-table-explicit.test
@@ -0,0 +1,31 @@
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/start.s 
-o %t.start.o
+# RUN: wasm-ld --export-table -o %t.wasm %t.start.o
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+# Verify the interaction between --export-table and declared tables
+
+.globl __indirect_function_table
+.tabletype __indirect_function_table,externref
+
+# CHECK:- Type:TABLE
+# CHECK-NEXT: Tables:
+# CHECK-NEXT:   - Index:   0
+# CHECK-NEXT: ElemType:FUNCREF
+# CHECK-NEXT: Limits:
+# CHECK-NEXT:   Flags:   [ HAS_MAX ]
+# CHECK-NEXT:   Initial: 0x1
+# CHECK-NEXT:   Maximum: 0x1
+# CHECK-NEXT:   - Type:
+
+# CHECK:- Type:EXPORT
+# CHECK-NEXT: Exports:
+# CHECK-NEXT:   - Name:memory
+# CHECK-NEXT: Kind:MEMORY
+# CHECK-NEXT: Index:   0
+# CHECK-NEXT:   - Name:_start
+# CHECK-NEXT: Kind:FUNCTION
+# CHECK-NEXT: Index:   0
+# CHECK-NEXT:   - Name:__indirect_function_table
+# CHECK-NEXT: Kind:TABLE
+# CHECK-NEXT: Index:   0
+# CHECK-NEXT:   - Type:

diff  --git a/lld/test/wasm/init-fini.ll b/lld/test/wasm/init-fini.ll
index 1e7644bc6d357..5631d58d68e5c 100644
--- a/lld/test/wasm/init-fini.ll
+++ b/lld/test/wasm/init-fini.ll
@@ -139,15 +139,15 @@ entry:
 ; RELOC-NEXT:  InitFunctions [
 ; RELOC-NEXT:0 (priority=101)
 ; RELOC-NEXT:1 (priority=101)
-; RELOC-NEXT:14 (priority=101)
-; RELOC-NEXT:10 (priority=101)
-; RELOC-NEXT:20 (priority=101)
-; RELOC-NEXT:10 (priority=202)
-; RELOC-NEXT:22 (priority=202)
+; RELOC-NEXT:15 (priority=101)
+; RELOC-NEXT:11 (priority=101)
+; RELOC-NEXT:21 (priority=101)
+; RELOC-NEXT:11

[llvm-branch-commits] [llvm] d806618 - [WebAssembly] MC layer writes table symbols to object files

2021-01-18 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-18T16:57:18+01:00
New Revision: d806618636f8a82bfc3f620e1fad83af4d2a2575

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

LOG: [WebAssembly] MC layer writes table symbols to object files

Now that the linker handles table symbols, we can allow the frontend to
produce them.

Depends on D91870.

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

Added: 


Modified: 
llvm/lib/MC/WasmObjectWriter.cpp
llvm/test/MC/WebAssembly/debug-info.ll
llvm/test/MC/WebAssembly/debug-info64.ll
llvm/test/MC/WebAssembly/function-alias.ll
llvm/test/MC/WebAssembly/global-ctor-dtor.ll
llvm/test/MC/WebAssembly/reloc-pic.s
llvm/test/MC/WebAssembly/type-index.s
llvm/test/MC/WebAssembly/weak-alias.s

Removed: 




diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index fb41c3763c15..b2d94dcadd81 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -519,6 +519,13 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
   Sym->setUndefined();
 }
 Sym->setUsedInReloc();
+// Any time we have a TABLE_INDEX relocation against a function symbol, we
+// need to ensure that table itself is part of the final output too.  In 
the
+// future we may want to define a new kind of reloc against both the
+// function and the table, so that the linker can see that the function
+// symbol keeps the table alive, but for now manually mark the table as
+// live.
+Sym->setNoStrip();
 Asm.registerSymbol(*Sym);
   }
 
@@ -1670,10 +1677,6 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler 
&Asm,
   WS.setIndex(InvalidIndex);
   continue;
 }
-if (WS.isTable() && WS.getName() == "__indirect_function_table") {
-  // For the moment, don't emit table symbols -- wasm-ld can't handle them.
-  continue;
-}
 LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
 
 uint32_t Flags = 0;

diff  --git a/llvm/test/MC/WebAssembly/debug-info.ll 
b/llvm/test/MC/WebAssembly/debug-info.ll
index 167d912c84a2..88d19d9191a9 100644
--- a/llvm/test/MC/WebAssembly/debug-info.ll
+++ b/llvm/test/MC/WebAssembly/debug-info.ll
@@ -89,44 +89,44 @@
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
-; CHECK-NEXT:Size: 91
+; CHECK-NEXT:Size: 95
 ; CHECK-NEXT:Offset: 731
 ; CHECK-NEXT:Name: linking
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 9
-; CHECK-NEXT:Offset: 836
+; CHECK-NEXT:Offset: 840
 ; CHECK-NEXT:Name: reloc.DATA
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 61
-; CHECK-NEXT:Offset: 862
+; CHECK-NEXT:Offset: 866
 ; CHECK-NEXT:Name: reloc..debug_info
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 947
+; CHECK-NEXT:Offset: 951
 ; CHECK-NEXT:Name: reloc..debug_pubnames
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 981
+; CHECK-NEXT:Offset: 985
 ; CHECK-NEXT:Name: reloc..debug_pubtypes
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 6
-; CHECK-NEXT:Offset: 1015
+; CHECK-NEXT:Offset: 1019
 ; CHECK-NEXT:Name: reloc..debug_line
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:  Section {
 ; CHECK-NEXT:Type: CUSTOM (0x0)
 ; CHECK-NEXT:Size: 77
-; CHECK-NEXT:Offset: 1045
+; CHECK-NEXT:Offset: 1049
 ; CHECK-NEXT:Name: producers
 ; CHECK-NEXT:  }
 ; CHECK-NEXT:]
@@ -238,6 +238,16 @@
 ; CHECK-NEXT:]
 ; CHECK-NEXT:ElementIndex: 0xC
 ; CHECK-NEXT:  }
+; CHECK-NEXT:  Symbol {
+; CHECK-NEXT:Name: __indirect_function_table
+; CHECK-NEXT:Type: TABLE (0x5)
+; CHECK-NEXT:Flags [ (0x90)
+; CHECK-NEXT:  NO_STRIP (0x80)
+; CHECK-NEXT:  UNDEFINED (0x10)
+; CHECK-NEXT:]
+; CHECK-NEXT:ImportModule: env
+; CHECK-NEXT:ElementIndex: 0x0
+; CHECK-NEXT:  }
 ; CHECK-NEXT:]
 
 ; generated from the following C code using: clang --target=wasm32 -g -O0 -S 
-emit-llvm test.c

diff  --git a/llvm/test/MC/WebAssembly/debug-info64.ll 
b/llvm/test/MC/WebAssembly/debug-info64.ll
index acba8aa27360..1755f2b7ef5c 100644
--- a/llvm/test/MC/WebAssembly/debug-info64.ll
+++ b/llvm/test/MC/WebAssembly/debug-info64.ll
@@ -89,44 +89,44 @@
 ; CHECK-NEXT:   }
 ; CHECK-NEXT:   Section {
 ; CHECK-NEXT: Type: CUSTOM (0x0)
-; CHECK-NEXT: Size: 91
+; CHECK-NEXT: Size: 95
 ; CHECK-NEXT: Offset: 759
 ; CHECK-NEXT: Name: linking
 ; CHECK-NEXT:   }
 ; CHECK-NEXT:   Section {
 ; CHECK-NEXT

[llvm-branch-commits] [llvm] 418df4a - [WebAssembly] call_indirect issues table number relocs

2021-01-19 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-19T09:32:45+01:00
New Revision: 418df4a6ab35d343cc0f2608c90a73dd9b8d0ab1

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

LOG: [WebAssembly] call_indirect issues table number relocs

This patch changes to make call_indirect explicitly refer to the
corresponding function table, residualizing TABLE_NUMBER relocs against
it.

With this change, wasm-ld now sees all references to tables, and can
link multiple tables.

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

Added: 
llvm/test/MC/WebAssembly/call-indirect-relocs.s

Modified: 
lld/test/wasm/call-indirect.ll
lld/test/wasm/compress-relocs.ll
lld/test/wasm/shared.ll
llvm/lib/MC/WasmObjectWriter.cpp
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyInstPrinter.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyInstrCall.td
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
llvm/test/CodeGen/WebAssembly/function-pointer64.ll
llvm/test/CodeGen/WebAssembly/multivalue.ll
llvm/test/MC/WebAssembly/basic-assembly.s
llvm/test/MC/WebAssembly/reloc-code.ll
llvm/test/MC/WebAssembly/tail-call-encodings.s
llvm/test/MC/WebAssembly/type-index.s
llvm/test/MC/WebAssembly/weak-alias.s

Removed: 




diff  --git a/lld/test/wasm/call-indirect.ll b/lld/test/wasm/call-indirect.ll
index 4acc1edae4f2..d54647d67da1 100644
--- a/lld/test/wasm/call-indirect.ll
+++ b/lld/test/wasm/call-indirect.ll
@@ -122,16 +122,16 @@ define void @call_ptr(i64 (i64)* %arg) {
 ; CHECK-NEXT: Body:42010B
 ; CHECK-NEXT:   - Index:   1
 ; CHECK-NEXT: Locals:
-; CHECK-NEXT: Body:
2802808880800011808080801A2802848880800011818080801A0B
+; CHECK-NEXT: Body:
2802808880800011808080800080808080001A2802848880800011818080800080808080001A0B
 ; CHECK-NEXT:   - Index:   2
 ; CHECK-NEXT: Locals:
 ; CHECK-NEXT: Body:41020B
 ; CHECK-NEXT:   - Index:   3
 ; CHECK-NEXT: Locals:
-; CHECK-NEXT: Body:4100280280800011818080801A0B
+; CHECK-NEXT: Body:
4100280280800011818080800080808080001A0B
 ; CHECK-NEXT:   - Index:   4
 ; CHECK-NEXT: Locals:
-; CHECK-NEXT: Body:4201200011828080801A0B
+; CHECK-NEXT: Body:4201200011828080800080808080001A0B
 ; CHECK-NEXT:   - Type:DATA
 ; CHECK-NEXT: Segments:
 ; CHECK-NEXT:   - SectionOffset:7

diff  --git a/lld/test/wasm/compress-relocs.ll 
b/lld/test/wasm/compress-relocs.ll
index 6c3533a108a5..ccfc525d4b83 100644
--- a/lld/test/wasm/compress-relocs.ll
+++ b/lld/test/wasm/compress-relocs.ll
@@ -22,5 +22,5 @@ entry:
 
 ; ERROR: wasm-ld: error: --compress-relocations is incompatible with output 
debug information. Please pass --strip-debug or --strip-all
 
-; CHECK:Body:
2802808880800011808080801A2802848880800011818080801A0B
+; CHECK:Body:
2802808880800011808080800080808080001A2802848880800011818080800080808080001A0B
 ; COMPRESS: Body:28028008111A280284081101001A0B

diff  --git a/lld/test/wasm/shared.ll b/lld/test/wasm/shared.ll
index 61337fcc6a3a..aca517ea7443 100644
--- a/lld/test/wasm/shared.ll
+++ b/lld/test/wasm/shared.ll
@@ -84,10 +84,6 @@ declare void @func_external()
 ; CHECK-NEXT: GlobalType:  I32
 ; CHECK-NEXT: GlobalMutable:   false
 ; CHECK-NEXT:   - Module:  env
-; CHECK-NEXT: Field:   func_external
-; CHECK-NEXT: Kind:FUNCTION
-; CHECK-NEXT: SigIndex:1
-; CHECK-NEXT:   - Module:  env
 ; CHECK-NEXT: Field:   __indirect_function_table
 ; CHECK-NEXT: Kind:TABLE
 ; CHECK-NEXT: Table:
@@ -95,6 +91,10 @@ declare void @func_external()
 ; CHECK-NEXT:   ElemType:FUNCREF
 ; CHECK-NEXT:   Limits:
 ; CHECK-NEXT: Initial: 0x2
+; CHECK-NEXT:   - Module:  env
+; CHECK-NEXT: Field:   func_external
+; CHECK-NEXT: Kind:FUNCTION
+; CHECK-NEXT: SigIndex:1
 ; CHECK-NEXT:   - Module:  GOT.mem
 ; CHECK-NEXT: Field:   indirect_func
 ; CHECK-NEXT: Kind:GLOBAL

diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index b2d94dcadd81..70a5e503c5a1 100644
--- a/llvm/lib/MC/WasmObjectWrit

[llvm-branch-commits] [lld] 831a143 - [WebAssembly] Change prefix on data segment flags to WASM_DATA_SEGMENT

2021-01-19 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-19T09:40:42+01:00
New Revision: 831a143e50cac873ec095fc7139a485173ba8c35

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

LOG: [WebAssembly] Change prefix on data segment flags to WASM_DATA_SEGMENT

Element sections will also need flags, so we shouldn't squat the
WASM_SEGMENT namespace.

Depends on D90948.

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

Added: 


Modified: 
lld/wasm/OutputSections.cpp
lld/wasm/Writer.cpp
llvm/include/llvm/BinaryFormat/Wasm.h
llvm/lib/MC/WasmObjectWriter.cpp
llvm/lib/Object/WasmObjectFile.cpp
llvm/lib/ObjectYAML/WasmEmitter.cpp
llvm/lib/ObjectYAML/WasmYAML.cpp

Removed: 




diff  --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index 1af448dcea74..47ad1891efa0 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -138,7 +138,7 @@ void DataSection::finalizeContents() {
 #ifndef NDEBUG
   unsigned activeCount = std::count_if(
   segments.begin(), segments.end(), [](OutputSegment *segment) {
-return (segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0;
+return (segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0;
   });
 #endif
 
@@ -154,9 +154,9 @@ void DataSection::finalizeContents() {
   continue;
 raw_string_ostream os(segment->header);
 writeUleb128(os, segment->initFlags, "init flags");
-if (segment->initFlags & WASM_SEGMENT_HAS_MEMINDEX)
+if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX)
   writeUleb128(os, 0, "memory index");
-if ((segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0) {
+if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
   WasmInitExpr initExpr;
   if (config->isPic) {
 initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index c4f4b6c8e571..3cf773fc4826 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -841,7 +841,7 @@ void Writer::createOutputSegments() {
 LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
 s = make(name);
 if (config->sharedMemory)
-  s->initFlags = WASM_SEGMENT_IS_PASSIVE;
+  s->initFlags = WASM_DATA_SEGMENT_IS_PASSIVE;
 // Exported memories are guaranteed to be zero-initialized, so no need
 // to emit data segments for bss sections.
 // TODO: consider initializing bss sections with memory.fill
@@ -886,7 +886,7 @@ static void createFunction(DefinedFunction *func, StringRef 
bodyContent) {
 }
 
 bool Writer::needsPassiveInitialization(const OutputSegment *segment) {
-  return segment->initFlags & WASM_SEGMENT_IS_PASSIVE &&
+  return segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE &&
  segment->name != ".tdata" && !segment->isBss;
 }
 

diff  --git a/llvm/include/llvm/BinaryFormat/Wasm.h 
b/llvm/include/llvm/BinaryFormat/Wasm.h
index bdef959fafdc..063c6a3f9449 100644
--- a/llvm/include/llvm/BinaryFormat/Wasm.h
+++ b/llvm/include/llvm/BinaryFormat/Wasm.h
@@ -146,8 +146,11 @@ struct WasmFunction {
 
 struct WasmDataSegment {
   uint32_t InitFlags;
-  uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX
-  WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0
+  // Present if InitFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX.
+  uint32_t MemoryIndex;
+  // Present if InitFlags & WASM_DATA_SEGMENT_IS_PASSIVE == 0.
+  WasmInitExpr Offset;
+
   ArrayRef Content;
   StringRef Name; // from the "segment info" section
   uint32_t Alignment;
@@ -300,8 +303,8 @@ enum : unsigned {
 };
 
 enum : unsigned {
-  WASM_SEGMENT_IS_PASSIVE = 0x01,
-  WASM_SEGMENT_HAS_MEMINDEX = 0x02,
+  WASM_DATA_SEGMENT_IS_PASSIVE = 0x01,
+  WASM_DATA_SEGMENT_HAS_MEMINDEX = 0x02,
 };
 
 // Feature policy prefixes used in the custom "target_features" section

diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index 70a5e503c5a1..0a6aee65b7bb 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -972,9 +972,9 @@ uint32_t WasmObjectWriter::writeDataSection(const 
MCAsmLayout &Layout) {
 
   for (const WasmDataSegment &Segment : DataSegments) {
 encodeULEB128(Segment.InitFlags, W->OS); // flags
-if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX)
+if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
   encodeULEB128(0, W->OS); // memory index
-if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) {
+if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
   W->OS << char(Segment.Offset > INT32_MAX ? wasm::WASM_OPCODE_I64_CONST
: wasm::WASM_OPCODE_I32_CONST);
   encodeSLEB128(Segment.Offset, W->OS); 

[llvm-branch-commits] [lld] 1a9b6e4 - [WebAssembly][lld] Fix call-indirect.s test to validate

2021-01-19 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-19T16:12:38+01:00
New Revision: 1a9b6e4a327f20189adde1129019c6652b818b43

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

LOG: [WebAssembly][lld] Fix call-indirect.s test to validate

Add missing address operand, so that we can validate the output files.

Depends on D92315.

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

Added: 


Modified: 
lld/test/wasm/Inputs/call-indirect.s
lld/test/wasm/call-indirect.ll
lld/test/wasm/compress-relocs.ll

Removed: 




diff  --git a/lld/test/wasm/Inputs/call-indirect.s 
b/lld/test/wasm/Inputs/call-indirect.s
index c181aa19ad6b..57dbeec009d4 100644
--- a/lld/test/wasm/Inputs/call-indirect.s
+++ b/lld/test/wasm/Inputs/call-indirect.s
@@ -7,9 +7,11 @@ bar:
   .globl  call_bar_indirect
 call_bar_indirect:
   .functype call_bar_indirect () -> ()
+  i32.const 0
   i32.load  indirect_bar
   call_indirect () -> (i64)
   drop
+  i32.const 0
   i32.load  indirect_foo
   call_indirect () -> (i32)
   drop

diff  --git a/lld/test/wasm/call-indirect.ll b/lld/test/wasm/call-indirect.ll
index d54647d67da1..08b4336c481d 100644
--- a/lld/test/wasm/call-indirect.ll
+++ b/lld/test/wasm/call-indirect.ll
@@ -122,7 +122,7 @@ define void @call_ptr(i64 (i64)* %arg) {
 ; CHECK-NEXT: Body:42010B
 ; CHECK-NEXT:   - Index:   1
 ; CHECK-NEXT: Locals:
-; CHECK-NEXT: Body:
2802808880800011808080800080808080001A2802848880800011818080800080808080001A0B
+; CHECK-NEXT: Body:
41002802808880800011808080800080808080001A41002802848880800011818080800080808080001A0B
 ; CHECK-NEXT:   - Index:   2
 ; CHECK-NEXT: Locals:
 ; CHECK-NEXT: Body:41020B

diff  --git a/lld/test/wasm/compress-relocs.ll 
b/lld/test/wasm/compress-relocs.ll
index ccfc525d4b83..9285b080a3b5 100644
--- a/lld/test/wasm/compress-relocs.ll
+++ b/lld/test/wasm/compress-relocs.ll
@@ -22,5 +22,5 @@ entry:
 
 ; ERROR: wasm-ld: error: --compress-relocations is incompatible with output 
debug information. Please pass --strip-debug or --strip-all
 
-; CHECK:Body:
2802808880800011808080800080808080001A2802848880800011818080800080808080001A0B
-; COMPRESS: Body:28028008111A280284081101001A0B
+; CHECK:Body:
41002802808880800011808080800080808080001A41002802848880800011818080800080808080001A0B
+; COMPRESS: Body:410028028008111A4100280284081101001A0B



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 9ad83fd - [WebAssembly] call_indirect causes indirect function table import

2021-01-05 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2021-01-05T11:09:24+01:00
New Revision: 9ad83fd6dc46330dcdea8af36c435061a31ac0c5

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

LOG: [WebAssembly] call_indirect causes indirect function table import

For wasm-ld table linking work to proceed, object files should indicate
if they use an indirect function table.  In the future this will be done
by the usual symbols and relocations mechanism, but until that support
lands in the linker, the presence of an `__indirect_function_table` in
the object file's import section shows that the object file needs an
indirect function table.

Prior to https://reviews.llvm.org/D91637, this condition was met by all
object files residualizing an `__indirect_function_table` import.

Since https://reviews.llvm.org/D91637, the intention has been that only
those object files needing an indirect function table would have the
`__indirect_function_table` import.  However, we missed the case of
object files which use the table via `call_indirect` but which
themselves do not declare any indirect functions.

This changeset makes it so that when we lower a call to `call_indirect`,
that we ensure that a `__indirect_function_table` symbol is present and
that it will be propagated to the linker.

A followup patch will revise this mechanism to make an explicit link
between `call_indirect` and its associated indirect function table; see
https://reviews.llvm.org/D90948.

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

Added: 
llvm/test/CodeGen/WebAssembly/call-indirect.ll

Modified: 
llvm/lib/MC/WasmObjectWriter.cpp
llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyUtilities.cpp
llvm/lib/Target/WebAssembly/WebAssemblyUtilities.h
llvm/test/MC/WebAssembly/type-index.s

Removed: 




diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index c8d2e5dbdbdc..683678b70ebc 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -405,6 +405,13 @@ void WasmObjectWriter::writeHeader(const MCAssembler &Asm) 
{
 
 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
 const MCAsmLayout &Layout) {
+  // As a stopgap measure until call_indirect instructions start explicitly
+  // referencing the indirect function table via TABLE_NUMBER relocs, ensure
+  // that the indirect function table import makes it to the output if anything
+  // in the compilation unit has caused it to be present.
+  if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table"))
+Asm.registerSymbol(*Sym);
+
   // Build a map of sections to the function that defines them, for use
   // in recordRelocation.
   for (const MCSymbol &S : Asm.symbols()) {

diff  --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp 
b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 55f79bafe414..18d7b642e044 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -160,6 +160,24 @@ struct WebAssemblyOperand : public MCParsedAsmOperand {
   }
 };
 
+static MCSymbolWasm *GetOrCreateFunctionTableSymbol(MCContext &Ctx,
+const StringRef &Name) {
+  // FIXME: Duplicates functionality from
+  // MC/WasmObjectWriter::recordRelocation, as well as WebAssemblyCodegen's
+  // WebAssembly:getOrCreateFunctionTableSymbol.
+  MCSymbolWasm *Sym = cast_or_null(Ctx.lookupSymbol(Name));
+  if (Sym) {
+if (!Sym->isFunctionTable())
+  Ctx.reportError(SMLoc(), "symbol is not a wasm funcref table");
+  } else {
+Sym = cast(Ctx.getOrCreateSymbol(Name));
+Sym->setFunctionTable();
+// The default function table is synthesized by the linker.
+Sym->setUndefined();
+  }
+  return Sym;
+}
+
 class WebAssemblyAsmParser final : public MCTargetAsmParser {
   MCAsmParser &Parser;
   MCAsmLexer &Lexer;
@@ -531,6 +549,15 @@ class WebAssemblyAsmParser final : public 
MCTargetAsmParser {
 return true;
 } else if (Name == "call_indirect" || Name == "return_call_indirect") {
   ExpectFuncType = true;
+  // Ensure that the object file has a __indirect_function_table import, as
+  // we call_indirect against it.
+  auto &Ctx = getStreamer().getContext();
+  MCSymbolWasm *Sym =
+  GetOrCreateFunctionTableSymbol(Ctx, "__indirect_function_table");
+  // Until call_indirect emits TABLE_NUMBER relocs against this symbol, 
mark
+  // it as NO_STRIP so as to ensure that the ind

[llvm-branch-commits] [llvm] d823cc7 - [WebAssembly][MC] Fix placement of table section

2020-12-07 Thread Andy Wingo via llvm-branch-commits

Author: Andy Wingo
Date: 2020-12-07T16:17:32+01:00
New Revision: d823cc7cad1d8fb61cac7fa2437a5191740804f5

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

LOG: [WebAssembly][MC] Fix placement of table section

The table section goes after functions.

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

Added: 


Modified: 
llvm/lib/MC/WasmObjectWriter.cpp

Removed: 




diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp 
b/llvm/lib/MC/WasmObjectWriter.cpp
index ab520ddcbe5d..211f0de3d242 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -1787,10 +1787,10 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler 
&Asm,
 writeTypeSection(Signatures);
 writeImportSection(Imports, DataSize, TableElems.size());
 writeFunctionSection(Functions);
+writeTableSection(Tables);
 // Skip the "memory" section; we import the memory instead.
 writeEventSection(Events);
 writeGlobalSection(Globals);
-writeTableSection(Tables);
 writeExportSection(Exports);
 writeElemSection(TableElems);
 writeDataCountSection();



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits