From e588599235133ccc4abec8374b9c029199b6387b Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 23 Sep 2025 15:14:00 -0700 Subject: [PATCH] [lld][WebAssembly] Allow --import-memory to take single name 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); --- lld/test/wasm/memory-naming.test | 15 +++++++++++++++ lld/wasm/Driver.cpp | 16 +++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) 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(defaultModule, memoryName); - } else { - ctx.arg.memoryImport = - std::optional>(); + 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( - defaultModule, memoryName); + ctx.arg.memoryImport = {defaultModule, memoryName}; } }