[llvm] b4f4e37 - [WebAssebmly][MC] Support .import_name/.import_field asm directives

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 15:10:07 PST 2019


Author: Sam Clegg
Date: 2019-12-06T15:09:56-08:00
New Revision: b4f4e370b59a753a51f11848f54e9705f43cccaf

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

LOG: [WebAssebmly][MC] Support .import_name/.import_field asm directives

Convert the MC test to use asm rather than bitcode.

This is a precursor to https://reviews.llvm.org/D70520.

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

Added: 
    lld/test/wasm/import-name.ll
    llvm/test/MC/WebAssembly/import-module.s

Modified: 
    clang/include/clang/Basic/AttrDocs.td
    llvm/include/llvm/MC/MCSymbolWasm.h
    llvm/lib/MC/WasmObjectWriter.cpp
    llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp

Removed: 
    lld/test/wasm/import-names.ll
    llvm/test/MC/WebAssembly/import-module.ll


################################################################################
diff  --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index c309785a534c..764ac2485350 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -4104,7 +4104,7 @@ or `msvc documentation <https://docs.microsoft.com/pl-pl/cpp/cpp/selectany>`_.
 def WebAssemblyImportModuleDocs : Documentation {
   let Category = DocCatFunction;
   let Content = [{
-Clang supports the ``__attribute__((import_module(<module_name>)))`` 
+Clang supports the ``__attribute__((import_module(<module_name>)))``
 attribute for the WebAssembly target. This attribute may be attached to a
 function declaration, where it modifies how the symbol is to be imported
 within the WebAssembly linking environment.
@@ -4114,14 +4114,14 @@ name, which typically identifies a module from which to import, and a field
 name, which typically identifies a field from that module to import. By
 default, module names for C/C++ symbols are assigned automatically by the
 linker. This attribute can be used to override the default behavior, and
-reuqest a specific module name be used instead.
+request a specific module name be used instead.
   }];
 }
 
 def WebAssemblyImportNameDocs : Documentation {
   let Category = DocCatFunction;
   let Content = [{
-Clang supports the ``__attribute__((import_name(<name>)))`` 
+Clang supports the ``__attribute__((import_name(<name>)))``
 attribute for the WebAssembly target. This attribute may be attached to a
 function declaration, where it modifies how the symbol is to be imported
 within the WebAssembly linking environment.
@@ -4131,7 +4131,7 @@ name, which typically identifies a module from which to import, and a field
 name, which typically identifies a field from that module to import. By
 default, field names for C/C++ symbols are the same as their C/C++ symbol
 names. This attribute can be used to override the default behavior, and
-reuqest a specific field name be used instead.
+request a specific field name be used instead.
   }];
 }
 

diff  --git a/lld/test/wasm/import-names.ll b/lld/test/wasm/import-name.ll
similarity index 100%
rename from lld/test/wasm/import-names.ll
rename to lld/test/wasm/import-name.ll

diff  --git a/llvm/include/llvm/MC/MCSymbolWasm.h b/llvm/include/llvm/MC/MCSymbolWasm.h
index 95beebe3f75a..c60d4069a559 100644
--- a/llvm/include/llvm/MC/MCSymbolWasm.h
+++ b/llvm/include/llvm/MC/MCSymbolWasm.h
@@ -78,6 +78,7 @@ class MCSymbolWasm : public MCSymbol {
   }
   void setImportModule(StringRef Name) { ImportModule = Name; }
 
+  bool hasImportName() const { return ImportName.hasValue(); }
   const StringRef getImportName() const {
       if (ImportName.hasValue()) {
           return ImportName.getValue();

diff  --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index c1ff3cc2480c..b22a393fcd4d 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -1452,7 +1452,7 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
         Flags |= wasm::WASM_SYMBOL_EXPORTED;
       }
     }
-    if (WS.getName() != WS.getImportName())
+    if (WS.hasImportName())
       Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;
 
     wasm::WasmSymbolInfo Info;

diff  --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 53a96fd6a97d..138ce85a23fc 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -712,6 +712,30 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
       return expect(AsmToken::EndOfStatement, "EOL");
     }
 
+    if (DirectiveID.getString() == ".import_module") {
+      auto SymName = expectIdent();
+      if (SymName.empty())
+        return true;
+      if (expect(AsmToken::Comma, ","))
+        return true;
+      auto ImportModule = expectIdent();
+      auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
+      WasmSym->setImportModule(ImportModule);
+      TOut.emitImportModule(WasmSym, ImportModule);
+    }
+
+    if (DirectiveID.getString() == ".import_name") {
+      auto SymName = expectIdent();
+      if (SymName.empty())
+        return true;
+      if (expect(AsmToken::Comma, ","))
+        return true;
+      auto ImportName = expectIdent();
+      auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
+      WasmSym->setImportName(ImportName);
+      TOut.emitImportName(WasmSym, ImportName);
+    }
+
     if (DirectiveID.getString() == ".eventtype") {
       auto SymName = expectIdent();
       if (SymName.empty())

diff  --git a/llvm/test/MC/WebAssembly/import-module.ll b/llvm/test/MC/WebAssembly/import-module.ll
deleted file mode 100644
index 1da142123ed2..000000000000
--- a/llvm/test/MC/WebAssembly/import-module.ll
+++ /dev/null
@@ -1,31 +0,0 @@
-; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
-
-target triple = "wasm32-unknown-unknown"
-
-define void @test() {
-  call void @foo()
-  call void @plain()
-  ret void
-}
-
-declare void @foo() #0
-declare void @plain()
-
-attributes #0 = { "wasm-import-module"="bar" "wasm-import-name"="qux" }
-
-; CHECK:        - Type:            IMPORT
-; CHECK-NEXT:     Imports:
-; CHECK:            - Module:          bar
-; CHECK-NEXT:         Field:           qux
-; CHECK-NEXT:         Kind:            FUNCTION
-
-; CHECK:            - Module:          env
-; CHECK-NEXT:         Field:           plain
-; CHECK-NEXT:         Kind:            FUNCTION
-
-; CHECK:        - Type:            CUSTOM
-; CHECK:              Name:            foo
-; CHECK-NEXT:         Flags:           [ UNDEFINED, EXPLICIT_NAME ]
-
-; CHECK:              Name:            plain
-; CHECK-NEXT:         Flags:           [ UNDEFINED ]

diff  --git a/llvm/test/MC/WebAssembly/import-module.s b/llvm/test/MC/WebAssembly/import-module.s
new file mode 100644
index 000000000000..a59ee184eab5
--- /dev/null
+++ b/llvm/test/MC/WebAssembly/import-module.s
@@ -0,0 +1,33 @@
+# RUN: llvm-mc -triple=wasm32 < %s | FileCheck %s -check-prefix=CHECK-ASM
+# RUN: llvm-mc -triple=wasm32 -filetype=obj -o - < %s | obj2yaml | FileCheck %s
+
+test:
+  .functype test () -> ()
+  call      foo
+  call      plain
+  end_function
+
+  .functype foo () -> ()
+  .functype plain () -> ()
+  .import_module  foo, bar
+  .import_name  foo, qux
+
+# CHECK-ASM: .import_module  foo, bar
+# CHECK-ASM: .import_name  foo, qux
+
+# CHECK:        - Type:            IMPORT
+# CHECK-NEXT:     Imports:
+# CHECK:            - Module:          bar
+# CHECK-NEXT:         Field:           qux
+# CHECK-NEXT:         Kind:            FUNCTION
+
+# CHECK:            - Module:          env
+# CHECK-NEXT:         Field:           plain
+# CHECK-NEXT:         Kind:            FUNCTION
+
+# CHECK:        - Type:            CUSTOM
+# CHECK:              Name:            foo
+# CHECK-NEXT:         Flags:           [ UNDEFINED, EXPLICIT_NAME ]
+
+# CHECK:              Name:            plain
+# CHECK-NEXT:         Flags:           [ UNDEFINED ]


        


More information about the llvm-commits mailing list