[llvm] r324778 - [WebAssembly] Add mechanisms for specifying an explicit import module name.

Dan Gohman via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 9 15:13:22 PST 2018


Author: djg
Date: Fri Feb  9 15:13:22 2018
New Revision: 324778

URL: http://llvm.org/viewvc/llvm-project?rev=324778&view=rev
Log:
[WebAssembly] Add mechanisms for specifying an explicit import module name.

This adds a wasm-import-module function attribute and a .import_module
assembler directive, for specifying module import names for WebAssembly.
Currently these may only be used for function symbols; global variables
may be considered in the future.

WebAssembly has a two-level namespace scheme for symbols, and it's
normally the linker's job to assign the module name, which is the
first-level name. The attributes here allow users to specify their
own module names explicitly, which is useful for tools generating
bindings to modules defined in other languages.

This feature is not fully usable yet. It will evolve along with the
ongoing symbol table and lld changes.

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

Added:
    llvm/trunk/test/CodeGen/WebAssembly/import-module.ll
Modified:
    llvm/trunk/include/llvm/MC/MCSymbolWasm.h
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
    llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h
    llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
    llvm/trunk/test/MC/WebAssembly/external-func-address.ll

Modified: llvm/trunk/include/llvm/MC/MCSymbolWasm.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCSymbolWasm.h?rev=324778&r1=324777&r2=324778&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCSymbolWasm.h (original)
+++ llvm/trunk/include/llvm/MC/MCSymbolWasm.h Fri Feb  9 15:13:22 2018
@@ -54,6 +54,7 @@ public:
   void setComdat(bool isComdat) { IsComdat = isComdat; }
 
   const StringRef getModuleName() const { return ModuleName; }
+  void setModuleName(StringRef Name) { ModuleName = Name; }
 
   const SmallVector<wasm::ValType, 1> &getReturns() const {
     assert(ReturnsSet);

Modified: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp?rev=324778&r1=324777&r2=324778&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp Fri Feb  9 15:13:22 2018
@@ -107,6 +107,11 @@ void WebAssemblyTargetAsmStreamer::emitG
   OS << "\t.import_global\t" << name << '\n';
 }
 
+void WebAssemblyTargetAsmStreamer::emitImportModule(MCSymbolWasm *Sym,
+                                                    StringRef ModuleName) {
+  OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
+}
+
 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
   OS << "\t.indidx  \t" << *Value << '\n';
 }
@@ -144,6 +149,11 @@ void WebAssemblyTargetELFStreamer::emitI
 void WebAssemblyTargetELFStreamer::emitGlobalImport(StringRef name) {
 }
 
+void WebAssemblyTargetELFStreamer::emitImportModule(MCSymbolWasm *Sym,
+                                                    StringRef ModuleName) {
+  llvm_unreachable(".import_module encoding not yet implemented");
+}
+
 void WebAssemblyTargetWasmStreamer::emitParam(MCSymbol *Symbol,
                                               ArrayRef<MVT> Types) {
   SmallVector<wasm::ValType, 4> Params;
@@ -211,3 +221,8 @@ void WebAssemblyTargetWasmStreamer::emit
 void WebAssemblyTargetWasmStreamer::emitGlobalImport(StringRef name) {
   llvm_unreachable(".global_import is not needed for direct wasm output");
 }
+
+void WebAssemblyTargetWasmStreamer::emitImportModule(MCSymbolWasm *Sym,
+                                                     StringRef ModuleName) {
+  Sym->setModuleName(ModuleName);
+}

Modified: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h?rev=324778&r1=324777&r2=324778&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h (original)
+++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h Fri Feb  9 15:13:22 2018
@@ -24,6 +24,7 @@ namespace llvm {
 
 class MCELFStreamer;
 class MCWasmStreamer;
+class MCSymbolWasm;
 
 /// WebAssembly-specific streamer interface, to implement support
 /// WebAssembly-specific assembly directives.
@@ -47,6 +48,8 @@ public:
   virtual void emitIndIdx(const MCExpr *Value) = 0;
   /// .import_global
   virtual void emitGlobalImport(StringRef name) = 0;
+  /// .import_module
+  virtual void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) = 0;
 
 protected:
   void emitValueType(wasm::ValType Type);
@@ -68,6 +71,7 @@ public:
                                 SmallVectorImpl<MVT> &Results) override;
   void emitIndIdx(const MCExpr *Value) override;
   void emitGlobalImport(StringRef name) override;
+  void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override;
 };
 
 /// This part is for ELF object output
@@ -84,6 +88,7 @@ public:
                                 SmallVectorImpl<MVT> &Results) override;
   void emitIndIdx(const MCExpr *Value) override;
   void emitGlobalImport(StringRef name) override;
+  void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override;
 };
 
 /// This part is for Wasm object output
@@ -100,6 +105,7 @@ public:
                                 SmallVectorImpl<MVT> &Results) override;
   void emitIndIdx(const MCExpr *Value) override;
   void emitGlobalImport(StringRef name) override;
+  void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override;
 };
 
 } // end namespace llvm

Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp?rev=324778&r1=324777&r2=324778&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp Fri Feb  9 15:13:22 2018
@@ -84,8 +84,14 @@ void WebAssemblyAsmPrinter::EmitEndOfAsm
       SmallVector<MVT, 4> Results;
       SmallVector<MVT, 4> Params;
       ComputeSignatureVTs(F, TM, Params, Results);
-      getTargetStreamer()->emitIndirectFunctionType(getSymbol(&F), Params,
-                                                    Results);
+      MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(&F));
+      getTargetStreamer()->emitIndirectFunctionType(Sym, Params, Results);
+
+      if (F.hasFnAttribute("wasm-import-module")) {
+        StringRef Name = F.getFnAttribute("wasm-import-module")
+                             .getValueAsString();
+        getTargetStreamer()->emitImportModule(Sym, Name);
+      }
     }
   }
   for (const auto &G : M.globals()) {

Added: llvm/trunk/test/CodeGen/WebAssembly/import-module.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/WebAssembly/import-module.ll?rev=324778&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/WebAssembly/import-module.ll (added)
+++ llvm/trunk/test/CodeGen/WebAssembly/import-module.ll Fri Feb  9 15:13:22 2018
@@ -0,0 +1,19 @@
+; RUN: llc < %s -asm-verbose=false | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown-wasm"
+
+define void @test() {
+  call void @foo()
+  call void @plain()
+  ret void
+}
+
+declare void @foo() #0
+declare void @plain()
+
+attributes #0 = { "wasm-import-module"="bar" }
+
+; CHECK-NOT: .import_module plain
+;     CHECK: .import_module foo, bar
+; CHECK-NOT: .import_module plain

Modified: llvm/trunk/test/MC/WebAssembly/external-func-address.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/WebAssembly/external-func-address.ll?rev=324778&r1=324777&r2=324778&view=diff
==============================================================================
--- llvm/trunk/test/MC/WebAssembly/external-func-address.ll (original)
+++ llvm/trunk/test/MC/WebAssembly/external-func-address.ll Fri Feb  9 15:13:22 2018
@@ -5,6 +5,11 @@ target triple = "wasm32-unknown-unknown-
 ; Verify that addresses of external functions generate correctly typed
 ; imports and relocations or type R_TABLE_INDEX_I32.
 
+declare void @f0(i32) #0
+ at ptr_to_f0 = hidden global void (i32)* @f0, align 4
+
+attributes #0 = { "wasm-import-module"="somewhere" }
+
 declare void @f1(i32) #1
 @ptr_to_f1 = hidden global void (i32)* @f1, align 4
 
@@ -24,6 +29,8 @@ declare void @f1(i32) #1
 ; CHECK-NEXT:         Field:           __linear_memory
 ; CHECK:            - Module:          env
 ; CHECK-NEXT:         Field:           __indirect_function_table
+; CHECK:            - Module:          somewhere
+; CHECK-NEXT:         Field:           f0
 ; CHECK:            - Module:          env
 ; CHECK-NEXT:         Field:           f1
 ; CHECK-NEXT:         Kind:            FUNCTION
@@ -33,7 +40,7 @@ declare void @f1(i32) #1
 ; CHECK-NEXT:       - Offset:
 ; CHECK-NEXT:           Opcode:          I32_CONST
 ; CHECK-NEXT:           Value:           1
-; CHECK-NEXT:         Functions:       [ 0 ]
+; CHECK-NEXT:         Functions:       [ 0, 1 ]
 ; CHECK:        - Type:            DATA
 ; CHECK-NEXT:     Relocations:
 ; CHECK-NEXT:       - Type:            R_WEBASSEMBLY_TABLE_INDEX_I32




More information about the llvm-commits mailing list