diff --git a/src/passes/Asyncify.cpp b/src/passes/Asyncify.cpp index 7ac9cf48921..994142e482f 100644 --- a/src/passes/Asyncify.cpp +++ b/src/passes/Asyncify.cpp @@ -244,6 +244,10 @@ // Logs out instrumentation decisions to the console. This can help figure // out why a certain function was instrumented. // +// --pass-arg=asyncify-memory@memory +// Picks which exported memory of the module to store and load data from +// and to (useful if the module contains multiple memories). +// // For manual fine-tuning of the list of instrumented functions, there are lists // that you can set. These must be used carefully, as misuse can break your // application - for example, if a function is called that should be @@ -1648,14 +1652,31 @@ struct Asyncify : public Pass { auto propagateAddList = hasArgument("asyncify-propagate-addlist"); // Ensure there is a memory, as we need it. + if (secondaryMemory) { auto secondaryMemorySizeString = getArgumentOrDefault("asyncify-secondary-memory-size", "1"); Address secondaryMemorySize = std::stoi(secondaryMemorySizeString); asyncifyMemory = createSecondaryMemory(module, secondaryMemorySize); } else { - MemoryUtils::ensureExists(module); - asyncifyMemory = module->memories[0]->name; + if (module->memories.size() <= 1) { + MemoryUtils::ensureExists(module); + asyncifyMemory = module->memories[0]->name; + } else { + auto asyncifyMemoryValue = + getArgumentOrDefault("asyncify-memory", "memory"); + for (auto& theExport : module->exports) { + if (theExport->kind == ExternalKind::Memory && + theExport->name == asyncifyMemoryValue) { + asyncifyMemory = theExport->value; + break; + } + } + if (!asyncifyMemory) { + Fatal() << "Please specify which of the multiple memories to use, " + "with --pass-arg=asyncify-memory@memory"; + } + } } pointerType = module->getMemory(asyncifyMemory)->is64() ? Type::i64 : Type::i32; diff --git a/test/lit/passes/asyncify_pass-arg=asyncify-memory@memory.wast b/test/lit/passes/asyncify_pass-arg=asyncify-memory@memory.wast new file mode 100644 index 00000000000..86bc1d10aba --- /dev/null +++ b/test/lit/passes/asyncify_pass-arg=asyncify-memory@memory.wast @@ -0,0 +1,124 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; RUN: foreach %s %t wasm-opt --enable-multimemory --enable-mutable-globals --asyncify --pass-arg=asyncify-memory@memory -S -o - | filecheck %s + +;; This test checks that the asyncify-memory argument can pick which memory to use in asyncify. +(module + ;; CHECK: (type $0 (func (param i32))) + + ;; CHECK: (type $1 (func)) + + ;; CHECK: (type $2 (func (result i32))) + + ;; CHECK: (global $__asyncify_state (mut i32) (i32.const 0)) + + ;; CHECK: (global $__asyncify_data (mut i32) (i32.const 0)) + + ;; CHECK: (memory $unused0 1 1) + (memory $unused0 (export "unused0") 1 1) + ;; CHECK: (memory $mem 1 1) + (memory $mem (export "memory") 1 1) + ;; CHECK: (memory $ignore 1 1) + (memory $ignore (export "unused") 1 1) +) +;; CHECK: (export "unused0" (memory $unused0)) + +;; CHECK: (export "memory" (memory $mem)) + +;; CHECK: (export "unused" (memory $ignore)) + +;; CHECK: (export "asyncify_start_unwind" (func $asyncify_start_unwind)) + +;; CHECK: (export "asyncify_stop_unwind" (func $asyncify_stop_unwind)) + +;; CHECK: (export "asyncify_start_rewind" (func $asyncify_start_rewind)) + +;; CHECK: (export "asyncify_stop_rewind" (func $asyncify_stop_rewind)) + +;; CHECK: (export "asyncify_get_state" (func $asyncify_get_state)) + +;; CHECK: (func $asyncify_start_unwind (param $0 i32) +;; CHECK-NEXT: (global.set $__asyncify_state +;; CHECK-NEXT: (i32.const 1) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (global.set $__asyncify_data +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (if +;; CHECK-NEXT: (i32.gt_u +;; CHECK-NEXT: (i32.load $mem +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (i32.load $mem offset=4 +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (then +;; CHECK-NEXT: (unreachable) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $asyncify_stop_unwind +;; CHECK-NEXT: (global.set $__asyncify_state +;; CHECK-NEXT: (i32.const 0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (if +;; CHECK-NEXT: (i32.gt_u +;; CHECK-NEXT: (i32.load $mem +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (i32.load $mem offset=4 +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (then +;; CHECK-NEXT: (unreachable) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $asyncify_start_rewind (param $0 i32) +;; CHECK-NEXT: (global.set $__asyncify_state +;; CHECK-NEXT: (i32.const 2) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (global.set $__asyncify_data +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (if +;; CHECK-NEXT: (i32.gt_u +;; CHECK-NEXT: (i32.load $mem +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (i32.load $mem offset=4 +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (then +;; CHECK-NEXT: (unreachable) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $asyncify_stop_rewind +;; CHECK-NEXT: (global.set $__asyncify_state +;; CHECK-NEXT: (i32.const 0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (if +;; CHECK-NEXT: (i32.gt_u +;; CHECK-NEXT: (i32.load $mem +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (i32.load $mem offset=4 +;; CHECK-NEXT: (global.get $__asyncify_data) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (then +;; CHECK-NEXT: (unreachable) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $asyncify_get_state (result i32) +;; CHECK-NEXT: (global.get $__asyncify_state) +;; CHECK-NEXT: )