Skip to content

Commit cf7a97e

Browse files
authored
[lld][WebAssembly] Allow --import-memory to take single name (#160409)
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).
1 parent fc57ca3 commit cf7a97e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lld/test/wasm/memory-naming.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@
6565
# CHECK-IMPORT-NEXT: Index: 0
6666
# CHECK-IMPORT-NEXT: - Type:
6767

68+
# RUN:wasm-ld --import-memory=foo -o %t.import.wasm %t.start.o
69+
# RUN: obj2yaml %t.import.wasm | FileCheck -check-prefix=CHECK-IMPORT-DEFAULT %s
70+
71+
# Verify that memory import module defaults to `env`, which is the default
72+
# module for all imports.
73+
74+
# CHECK-IMPORT-DEFAULT: - Type: IMPORT
75+
# CHECK-IMPORT-DEFAULT-NEXT: Imports:
76+
# CHECK-IMPORT-DEFAULT-NEXT: - Module: env
77+
# CHECK-IMPORT-DEFAULT-NEXT: Field: foo
78+
# CHECK-IMPORT-DEFAULT-NEXT: Kind: MEMORY
79+
# CHECK-IMPORT-DEFAULT-NEXT: Memory:
80+
# CHECK-IMPORT-DEFAULT-NEXT: Minimum: 0x2
81+
# CHECK-IMPORT-DEFAULT-NEXT: - Type:
82+
6883
# RUN:wasm-ld --import-memory=foo,bar --export-memory=qux -o %t.both.wasm %t.start.o
6984
# RUN: obj2yaml %t.both.wasm | FileCheck -check-prefix=CHECK-BOTH %s
7085

lld/wasm/Driver.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,13 @@ static void readConfigs(opt::InputArgList &args) {
542542
ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
543543

544544
if (args.hasArg(OPT_import_memory_with_name)) {
545-
ctx.arg.memoryImport =
546-
args.getLastArgValue(OPT_import_memory_with_name).split(",");
545+
auto argValue = args.getLastArgValue(OPT_import_memory_with_name);
546+
if (argValue.contains(','))
547+
ctx.arg.memoryImport = argValue.split(",");
548+
else
549+
ctx.arg.memoryImport = {defaultModule, argValue};
547550
} else if (args.hasArg(OPT_import_memory)) {
548-
ctx.arg.memoryImport =
549-
std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
550-
} else {
551-
ctx.arg.memoryImport =
552-
std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
551+
ctx.arg.memoryImport = {defaultModule, memoryName};
553552
}
554553

555554
if (args.hasArg(OPT_export_memory_with_name)) {
@@ -746,8 +745,7 @@ static void setConfigs() {
746745
error("--export-memory is incompatible with --shared");
747746
}
748747
if (!ctx.arg.memoryImport.has_value()) {
749-
ctx.arg.memoryImport = std::pair<llvm::StringRef, llvm::StringRef>(
750-
defaultModule, memoryName);
748+
ctx.arg.memoryImport = {defaultModule, memoryName};
751749
}
752750
}
753751

0 commit comments

Comments
 (0)