[llvm] [MC][WebAssembly] add string support for import/export instructions (PR #173513)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 24 14:42:21 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-webassembly

Author: None (daxpedda)

<details>
<summary>Changes</summary>

This change allows strings to be used as import modules, import names and export names alongside identifiers.

Fixes: https://github.com/llvm/llvm-project/issues/173479

---
Full diff: https://github.com/llvm/llvm-project/pull/173513.diff


10 Files Affected:

- (modified) llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp (+13-3) 
- (modified) llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp (+4-6) 
- (modified) llvm/test/CodeGen/WebAssembly/export-name.ll (+2-2) 
- (modified) llvm/test/CodeGen/WebAssembly/import-module.ll (+2-2) 
- (modified) llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll (+2-2) 
- (modified) llvm/test/MC/WebAssembly/export-name-invalid.s (+1-1) 
- (modified) llvm/test/MC/WebAssembly/export-name.s (+1-1) 
- (modified) llvm/test/MC/WebAssembly/import-module-invalid.s (+1-1) 
- (modified) llvm/test/MC/WebAssembly/import-module.s (+2-2) 
- (modified) llvm/test/MC/WebAssembly/import-name-invalid.s (+1-1) 


``````````diff
diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
index 9175b2731dac0..416f437a5d0fa 100644
--- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
+++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
@@ -415,6 +415,16 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
     return Name;
   }
 
+  StringRef expectIdentOrString() {
+    if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) {
+      error("Expected identifier or string, got: ", Lexer.getTok());
+      return StringRef();
+    }
+    auto Name = Lexer.getTok().getString();
+    Parser.Lex();
+    return Name;
+  }
+
   bool parseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) {
     while (Lexer.is(AsmToken::Identifier)) {
       auto Type = WebAssembly::parseType(Lexer.getTok().getString());
@@ -1041,7 +1051,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
         return ParseStatus::Failure;
       if (expect(AsmToken::Comma, ","))
         return ParseStatus::Failure;
-      auto ExportName = expectIdent();
+      auto ExportName = expectIdentOrString();
       if (ExportName.empty())
         return ParseStatus::Failure;
       auto *WasmSym =
@@ -1057,7 +1067,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
         return ParseStatus::Failure;
       if (expect(AsmToken::Comma, ","))
         return ParseStatus::Failure;
-      auto ImportModule = expectIdent();
+      auto ImportModule = expectIdentOrString();
       if (ImportModule.empty())
         return ParseStatus::Failure;
       auto *WasmSym =
@@ -1073,7 +1083,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
         return ParseStatus::Failure;
       if (expect(AsmToken::Comma, ","))
         return ParseStatus::Failure;
-      auto ImportName = expectIdent();
+      auto ImportName = expectIdentOrString();
       if (ImportName.empty())
         return ParseStatus::Failure;
       auto *WasmSym =
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
index c18de5fc1939e..5cff8a9e092cd 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp
@@ -96,20 +96,18 @@ void WebAssemblyTargetAsmStreamer::emitTagType(const MCSymbolWasm *Sym) {
 
 void WebAssemblyTargetAsmStreamer::emitImportModule(const MCSymbolWasm *Sym,
                                                     StringRef ImportModule) {
-  OS << "\t.import_module\t" << Sym->getName() << ", "
-                             << ImportModule << '\n';
+  OS << "\t.import_module\t" << Sym->getName() << ", \"" << ImportModule
+     << "\"\n";
 }
 
 void WebAssemblyTargetAsmStreamer::emitImportName(const MCSymbolWasm *Sym,
                                                   StringRef ImportName) {
-  OS << "\t.import_name\t" << Sym->getName() << ", "
-                           << ImportName << '\n';
+  OS << "\t.import_name\t" << Sym->getName() << ", \"" << ImportName << "\"\n";
 }
 
 void WebAssemblyTargetAsmStreamer::emitExportName(const MCSymbolWasm *Sym,
                                                   StringRef ExportName) {
-  OS << "\t.export_name\t" << Sym->getName() << ", "
-                           << ExportName << '\n';
+  OS << "\t.export_name\t" << Sym->getName() << ", \"" << ExportName << "\"\n";
 }
 
 void WebAssemblyTargetAsmStreamer::emitIndIdx(const MCExpr *Value) {
diff --git a/llvm/test/CodeGen/WebAssembly/export-name.ll b/llvm/test/CodeGen/WebAssembly/export-name.ll
index 9941520f21918..815f6d3b4ed1e 100644
--- a/llvm/test/CodeGen/WebAssembly/export-name.ll
+++ b/llvm/test/CodeGen/WebAssembly/export-name.ll
@@ -12,5 +12,5 @@ declare void @test2() #1
 attributes #0 = { "wasm-export-name"="foo" }
 attributes #1 = { "wasm-export-name"="bar" }
 
-; CHECK: .export_name test, foo
-; CHECK: .export_name test2, bar
+; CHECK: .export_name test, "foo"
+; CHECK: .export_name test2, "bar"
diff --git a/llvm/test/CodeGen/WebAssembly/import-module.ll b/llvm/test/CodeGen/WebAssembly/import-module.ll
index 1d423fbf5426d..101459f47d482 100644
--- a/llvm/test/CodeGen/WebAssembly/import-module.ll
+++ b/llvm/test/CodeGen/WebAssembly/import-module.ll
@@ -14,6 +14,6 @@ declare void @plain()
 attributes #0 = { "wasm-import-module"="bar" "wasm-import-name"="qux" }
 
 ; CHECK-NOT: .import_module plain
-;     CHECK: .import_module foo, bar
-;     CHECK: .import_name foo, qux
+;     CHECK: .import_module foo, "bar"
+;     CHECK: .import_name foo, "qux"
 ; CHECK-NOT: .import_module plain
diff --git a/llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll b/llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll
index 2c4d52937d944..2989b3e5ae964 100644
--- a/llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll
+++ b/llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll
@@ -5,8 +5,8 @@
 target triple = "wasm32-unknown-unknown"
 
 ; EH: .functype  invoke_vi (i32, i32) -> ()
-; EH: .import_module  invoke_vi, env
-; EH: .import_name  invoke_vi, invoke_vi
+; EH: .import_module  invoke_vi, "env"
+; EH: .import_name  invoke_vi, "invoke_vi"
 ; EH-NOT: .functype  __invoke_void_i32
 ; EH-NOT: .import_module  __invoke_void_i32
 ; EH-NOT: .import_name  __invoke_void_i32
diff --git a/llvm/test/MC/WebAssembly/export-name-invalid.s b/llvm/test/MC/WebAssembly/export-name-invalid.s
index ad322ce949f5e..4ea977eee66f8 100644
--- a/llvm/test/MC/WebAssembly/export-name-invalid.s
+++ b/llvm/test/MC/WebAssembly/export-name-invalid.s
@@ -6,7 +6,7 @@
 # CHECK: [[#@LINE+1]]:17: error: Expected ,, instead got:
 .export_name foo
 
-# CHECK: [[#@LINE+1]]:18: error: Expected identifier, got:
+# CHECK: [[#@LINE+1]]:18: error: Expected identifier or string, got:
 .export_name foo,
 
 # CHECK: [[#@LINE+1]]:22: error: Expected EOL, instead got: ,
diff --git a/llvm/test/MC/WebAssembly/export-name.s b/llvm/test/MC/WebAssembly/export-name.s
index 51e1bcf73dba2..b95d35bdf191b 100644
--- a/llvm/test/MC/WebAssembly/export-name.s
+++ b/llvm/test/MC/WebAssembly/export-name.s
@@ -8,7 +8,7 @@ foo:
     .export_name foo, bar
     end_function
 
-# CHECK: .export_name foo, bar
+# CHECK: .export_name foo, "bar"
 
 # CHECK-OBJ:        - Type:            EXPORT
 # CHECK-OBJ-NEXT:     Exports:
diff --git a/llvm/test/MC/WebAssembly/import-module-invalid.s b/llvm/test/MC/WebAssembly/import-module-invalid.s
index a9f93e83dba39..0c445ac93a93d 100644
--- a/llvm/test/MC/WebAssembly/import-module-invalid.s
+++ b/llvm/test/MC/WebAssembly/import-module-invalid.s
@@ -6,7 +6,7 @@
 # CHECK: [[#@LINE+1]]:19: error: Expected ,, instead got:
 .import_module foo
 
-# CHECK: [[#@LINE+1]]:20: error: Expected identifier, got:
+# CHECK: [[#@LINE+1]]:20: error: Expected identifier or string, got:
 .import_module foo,
 
 # CHECK: [[#@LINE+1]]:24: error: Expected EOL, instead got: ,
diff --git a/llvm/test/MC/WebAssembly/import-module.s b/llvm/test/MC/WebAssembly/import-module.s
index 5d28d5b9c0b92..a85ffb60ac65b 100644
--- a/llvm/test/MC/WebAssembly/import-module.s
+++ b/llvm/test/MC/WebAssembly/import-module.s
@@ -13,8 +13,8 @@ test:
   .import_module  foo, bar
   .import_name  foo, qux
 
-# CHECK-ASM: .import_module  foo, bar
-# CHECK-ASM: .import_name  foo, qux
+# CHECK-ASM: .import_module  foo, "bar"
+# CHECK-ASM: .import_name  foo, "qux"
 
 # CHECK:        - Type:            IMPORT
 # CHECK-NEXT:     Imports:
diff --git a/llvm/test/MC/WebAssembly/import-name-invalid.s b/llvm/test/MC/WebAssembly/import-name-invalid.s
index da8ed0d4617b0..d15b7bd4eaad3 100644
--- a/llvm/test/MC/WebAssembly/import-name-invalid.s
+++ b/llvm/test/MC/WebAssembly/import-name-invalid.s
@@ -6,7 +6,7 @@
 # CHECK: [[#@LINE+1]]:17: error: Expected ,, instead got:
 .import_name foo
 
-# CHECK: [[#@LINE+1]]:18: error: Expected identifier, got:
+# CHECK: [[#@LINE+1]]:18: error: Expected identifier or string, got:
 .import_name foo,
 
 # CHECK: [[#@LINE+1]]:22: error: Expected EOL, instead got: ,

``````````

</details>


https://github.com/llvm/llvm-project/pull/173513


More information about the llvm-commits mailing list