diff options
author | Ashley Nelson <nashley@google.com> | 2022-08-17 18:44:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-17 18:44:29 -0700 |
commit | 3aff4c6e85623c970280219c6699a66bc9de5f9b (patch) | |
tree | e5440bc966e523a7404ae2cec3458dacbe1281d1 /src/ir/module-utils.h | |
parent | b70fe755aa4c90727edfd91dc0a9a51febf0239d (diff) | |
download | binaryen-3aff4c6e85623c970280219c6699a66bc9de5f9b.tar.gz binaryen-3aff4c6e85623c970280219c6699a66bc9de5f9b.tar.bz2 binaryen-3aff4c6e85623c970280219c6699a66bc9de5f9b.zip |
Mutli-Memories Support in IR (#4811)
This PR removes the single memory restriction in IR, adding support for a single module to reference multiple memories. To support this change, a new memory name field was added to 13 memory instructions in order to identify the memory for the instruction.
It is a goal of this PR to maintain backwards compatibility with existing text and binary wasm modules, so memory indexes remain optional for memory instructions. Similarly, the JS API makes assumptions about which memory is intended when only one memory is present in the module. Another goal of this PR is that existing tests behavior be unaffected. That said, tests must now explicitly define a memory before invoking memory instructions or exporting a memory, and memory names are now printed for each memory instruction in the text format.
There remain quite a few places where a hardcoded reference to the first memory persist (memory flattening, for example, will return early if more than one memory is present in the module). Many of these call-sites, particularly within passes, will require us to rethink how the optimization works in a multi-memories world. Other call-sites may necessitate more invasive code restructuring to fully convert away from relying on a globally available, single memory pointer.
Diffstat (limited to 'src/ir/module-utils.h')
-rw-r--r-- | src/ir/module-utils.h | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h index 4f731748e..81f832b40 100644 --- a/src/ir/module-utils.h +++ b/src/ir/module-utils.h @@ -106,10 +106,22 @@ inline Table* copyTable(const Table* table, Module& out) { return out.addTable(std::move(ret)); } +inline Memory* copyMemory(const Memory* memory, Module& out) { + auto ret = Builder::makeMemory(memory->name); + ret->hasExplicitName = memory->hasExplicitName; + ret->initial = memory->initial; + ret->max = memory->max; + ret->shared = memory->shared; + ret->indexType = memory->indexType; + + return out.addMemory(std::move(ret)); +} + inline DataSegment* copyDataSegment(const DataSegment* segment, Module& out) { auto ret = Builder::makeDataSegment(); ret->name = segment->name; ret->hasExplicitName = segment->hasExplicitName; + ret->memory = segment->memory; ret->isPassive = segment->isPassive; if (!segment->isPassive) { auto offset = ExpressionManipulator::copy(segment->offset, out); @@ -141,10 +153,12 @@ inline void copyModule(const Module& in, Module& out) { for (auto& curr : in.tables) { copyTable(curr.get(), out); } + for (auto& curr : in.memories) { + copyMemory(curr.get(), out); + } for (auto& curr : in.dataSegments) { copyDataSegment(curr.get(), out); } - out.memory = in.memory; out.start = in.start; out.userSections = in.userSections; out.debugInfoFileNames = in.debugInfoFileNames; @@ -207,14 +221,27 @@ inline void renameFunction(Module& wasm, Name oldName, Name newName) { // Convenient iteration over imported/non-imported module elements template<typename T> inline void iterImportedMemories(Module& wasm, T visitor) { - if (wasm.memory.exists && wasm.memory.imported()) { - visitor(&wasm.memory); + for (auto& import : wasm.memories) { + if (import->imported()) { + visitor(import.get()); + } } } template<typename T> inline void iterDefinedMemories(Module& wasm, T visitor) { - if (wasm.memory.exists && !wasm.memory.imported()) { - visitor(&wasm.memory); + for (auto& import : wasm.memories) { + if (!import->imported()) { + visitor(import.get()); + } + } +} + +template<typename T> +inline void iterMemorySegments(Module& wasm, Name memory, T visitor) { + for (auto& segment : wasm.dataSegments) { + if (!segment->isPassive && segment->memory == memory) { + visitor(segment.get()); + } } } |