Skip to content

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Sep 23, 2025

Previously we only tested it as taking a pair of module and name, e.g --import-memory=mymodule,mymemory.

However, rather confusingly, if you just specified --import-memory=foo it would set the module to foo and the name to empty string.

This changes the interpretation of --import-memory=foo to mean import foo from the default module (which is currently env, but one day we make it configurable).

Previously it required a pair of `module` and `name`, e.g
`--import-memory=mymodule,mymemory`.

However, rather confusingly if you just specified `--import-memory=foo`
it would set the module to `foo` and the name to empty string.

This changes the interpretation of `--import-memory=foo` to mean import
`foo` from the default module (which is currently `env`, but one day we
make it configurable);
@llvmbot
Copy link
Member

llvmbot commented Sep 23, 2025

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

Changes

Previously it required a pair of module and name, e.g --import-memory=mymodule,mymemory.

However, rather confusingly if you just specified --import-memory=foo it would set the module to foo and the name to empty string.

This changes the interpretation of --import-memory=foo to mean import foo from the default module (which is currently env, but one day we make it configurable);


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

2 Files Affected:

  • (modified) lld/test/wasm/memory-naming.test (+15)
  • (modified) lld/wasm/Driver.cpp (+7-9)
diff --git a/lld/test/wasm/memory-naming.test b/lld/test/wasm/memory-naming.test
index b4aabaeeac357..766d9cd59050b 100644
--- a/lld/test/wasm/memory-naming.test
+++ b/lld/test/wasm/memory-naming.test
@@ -65,6 +65,21 @@
 # CHECK-IMPORT-NEXT:        Index:           0
 # CHECK-IMPORT-NEXT:  - Type:
 
+# RUN:wasm-ld --import-memory=foo -o %t.import.wasm %t.start.o
+# RUN: obj2yaml %t.import.wasm | FileCheck -check-prefix=CHECK-IMPORT-DEFAULT %s
+
+# Verify that memory import module defaults to `env`, which is the default
+# module for all imports.
+
+# CHECK-IMPORT-DEFAULT:       - Type:            IMPORT
+# CHECK-IMPORT-DEFAULT-NEXT:    Imports:
+# CHECK-IMPORT-DEFAULT-NEXT:      - Module:          env
+# CHECK-IMPORT-DEFAULT-NEXT:        Field:           foo
+# CHECK-IMPORT-DEFAULT-NEXT:        Kind:            MEMORY
+# CHECK-IMPORT-DEFAULT-NEXT:        Memory:
+# CHECK-IMPORT-DEFAULT-NEXT:          Minimum:         0x2
+# CHECK-IMPORT-DEFAULT-NEXT:  - Type:
+
 # RUN:wasm-ld --import-memory=foo,bar --export-memory=qux -o %t.both.wasm %t.start.o
 # RUN: obj2yaml %t.both.wasm | FileCheck -check-prefix=CHECK-BOTH %s
 
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 6332f1a793b2a..9b85b6c00b26d 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -542,14 +542,13 @@ static void readConfigs(opt::InputArgList &args) {
   ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
 
   if (args.hasArg(OPT_import_memory_with_name)) {
-    ctx.arg.memoryImport =
-        args.getLastArgValue(OPT_import_memory_with_name).split(",");
+    auto argValue = args.getLastArgValue(OPT_import_memory_with_name);
+    if (argValue.contains(','))
+      ctx.arg.memoryImport = argValue.split(",");
+    else
+      ctx.arg.memoryImport = {defaultModule, argValue};
   } else if (args.hasArg(OPT_import_memory)) {
-    ctx.arg.memoryImport =
-        std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
-  } else {
-    ctx.arg.memoryImport =
-        std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
+    ctx.arg.memoryImport = {defaultModule, memoryName};
   }
 
   if (args.hasArg(OPT_export_memory_with_name)) {
@@ -746,8 +745,7 @@ static void setConfigs() {
       error("--export-memory is incompatible with --shared");
     }
     if (!ctx.arg.memoryImport.has_value()) {
-      ctx.arg.memoryImport = std::pair<llvm::StringRef, llvm::StringRef>(
-          defaultModule, memoryName);
+      ctx.arg.memoryImport = {defaultModule, memoryName};
     }
   }
 

@llvmbot
Copy link
Member

llvmbot commented Sep 23, 2025

@llvm/pr-subscribers-lld-wasm

Author: Sam Clegg (sbc100)

Changes

Previously it required a pair of module and name, e.g --import-memory=mymodule,mymemory.

However, rather confusingly if you just specified --import-memory=foo it would set the module to foo and the name to empty string.

This changes the interpretation of --import-memory=foo to mean import foo from the default module (which is currently env, but one day we make it configurable);


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

2 Files Affected:

  • (modified) lld/test/wasm/memory-naming.test (+15)
  • (modified) lld/wasm/Driver.cpp (+7-9)
diff --git a/lld/test/wasm/memory-naming.test b/lld/test/wasm/memory-naming.test
index b4aabaeeac357..766d9cd59050b 100644
--- a/lld/test/wasm/memory-naming.test
+++ b/lld/test/wasm/memory-naming.test
@@ -65,6 +65,21 @@
 # CHECK-IMPORT-NEXT:        Index:           0
 # CHECK-IMPORT-NEXT:  - Type:
 
+# RUN:wasm-ld --import-memory=foo -o %t.import.wasm %t.start.o
+# RUN: obj2yaml %t.import.wasm | FileCheck -check-prefix=CHECK-IMPORT-DEFAULT %s
+
+# Verify that memory import module defaults to `env`, which is the default
+# module for all imports.
+
+# CHECK-IMPORT-DEFAULT:       - Type:            IMPORT
+# CHECK-IMPORT-DEFAULT-NEXT:    Imports:
+# CHECK-IMPORT-DEFAULT-NEXT:      - Module:          env
+# CHECK-IMPORT-DEFAULT-NEXT:        Field:           foo
+# CHECK-IMPORT-DEFAULT-NEXT:        Kind:            MEMORY
+# CHECK-IMPORT-DEFAULT-NEXT:        Memory:
+# CHECK-IMPORT-DEFAULT-NEXT:          Minimum:         0x2
+# CHECK-IMPORT-DEFAULT-NEXT:  - Type:
+
 # RUN:wasm-ld --import-memory=foo,bar --export-memory=qux -o %t.both.wasm %t.start.o
 # RUN: obj2yaml %t.both.wasm | FileCheck -check-prefix=CHECK-BOTH %s
 
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 6332f1a793b2a..9b85b6c00b26d 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -542,14 +542,13 @@ static void readConfigs(opt::InputArgList &args) {
   ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
 
   if (args.hasArg(OPT_import_memory_with_name)) {
-    ctx.arg.memoryImport =
-        args.getLastArgValue(OPT_import_memory_with_name).split(",");
+    auto argValue = args.getLastArgValue(OPT_import_memory_with_name);
+    if (argValue.contains(','))
+      ctx.arg.memoryImport = argValue.split(",");
+    else
+      ctx.arg.memoryImport = {defaultModule, argValue};
   } else if (args.hasArg(OPT_import_memory)) {
-    ctx.arg.memoryImport =
-        std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
-  } else {
-    ctx.arg.memoryImport =
-        std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
+    ctx.arg.memoryImport = {defaultModule, memoryName};
   }
 
   if (args.hasArg(OPT_export_memory_with_name)) {
@@ -746,8 +745,7 @@ static void setConfigs() {
       error("--export-memory is incompatible with --shared");
     }
     if (!ctx.arg.memoryImport.has_value()) {
-      ctx.arg.memoryImport = std::pair<llvm::StringRef, llvm::StringRef>(
-          defaultModule, memoryName);
+      ctx.arg.memoryImport = {defaultModule, memoryName};
     }
   }
 

@sbc100 sbc100 merged commit cf7a97e into llvm:main Sep 23, 2025
12 checks passed
@sbc100 sbc100 deleted the import_memory_default branch September 23, 2025 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants