From b63fea1feefce42be49fa28fd85d01d4a52603a2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 11 Jan 2022 14:16:34 -0800 Subject: [ctor-eval] Stop if there are any memory.init instructions (#4442) This tool depends (atm) on flattening memory segments. That is not compatible with memory.init which cares about segment identities. This changes flatten() only by adding the check for MemoryInit. The rest is unchanged, although I saw the other two params are not needed and I removed them while I was there. --- src/tools/wasm-ctor-eval.cpp | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src/tools/wasm-ctor-eval.cpp') diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index e6883e351..3923d137c 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -663,11 +663,6 @@ void evalCtors(Module& wasm, CtorEvalExternalInterface interface(linkedInstances); try { - // flatten memory, so we do not depend on the layout of data segments - if (!MemoryUtils::flatten(wasm.memory)) { - Fatal() << " ...stopping since could not flatten memory\n"; - } - // create an instance for evalling EvallingModuleInstance instance(wasm, &interface, linkedInstances); // we should not add new globals from here on; as a result, using @@ -712,6 +707,16 @@ void evalCtors(Module& wasm, } } +static bool canEval(Module& wasm) { + // Check if we can flatten memory. We need to do so currently because of how + // we assume memory is simple and flat. TODO + if (!MemoryUtils::flatten(wasm)) { + std::cout << " ...stopping since could not flatten memory\n"; + return false; + } + return true; +} + } // anonymous namespace // @@ -808,19 +813,21 @@ int main(int argc, const char* argv[]) { Fatal() << "error in validating input"; } - evalCtors(wasm, ctors, keptExports); - - // Do some useful optimizations after the evalling - { - PassRunner passRunner(&wasm); - passRunner.add("memory-packing"); // we flattened it, so re-optimize - // TODO: just do -Os for the one function - passRunner.add("remove-unused-names"); - passRunner.add("dce"); - passRunner.add("merge-blocks"); - passRunner.add("vacuum"); - passRunner.add("remove-unused-module-elements"); - passRunner.run(); + if (canEval(wasm)) { + evalCtors(wasm, ctors, keptExports); + + // Do some useful optimizations after the evalling + { + PassRunner passRunner(&wasm); + passRunner.add("memory-packing"); // we flattened it, so re-optimize + // TODO: just do -Os for the one function + passRunner.add("remove-unused-names"); + passRunner.add("dce"); + passRunner.add("merge-blocks"); + passRunner.add("vacuum"); + passRunner.add("remove-unused-module-elements"); + passRunner.run(); + } } if (options.extra.count("output") > 0) { -- cgit v1.2.3