diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -4104,7 +4104,7 @@ def WebAssemblyImportModuleDocs : Documentation { let Category = DocCatFunction; let Content = [{ -Clang supports the ``__attribute__((import_module()))`` +Clang supports the ``__attribute__((import_module()))`` 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 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()))`` +Clang supports the ``__attribute__((import_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 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 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 --- a/llvm/include/llvm/MC/MCSymbolWasm.h +++ b/llvm/include/llvm/MC/MCSymbolWasm.h @@ -78,6 +78,7 @@ } 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 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -1452,7 +1452,7 @@ 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 --- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -712,6 +712,30 @@ 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(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(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 --- 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 --- /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 ]