From 126fdec669f857ad2b1d39cdf365009ce84737a7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 22 Jun 2023 20:34:37 -0700 Subject: [PATCH] PostEmscripten: Preserve __em_js__ exports in side modules --- src/passes/PostEmscripten.cpp | 14 ++++-- .../passes/post-emscripten-side-module.wast | 49 +++++++++++++++++++ test/lit/passes/post-emscripten.wast | 1 - 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 test/lit/passes/post-emscripten-side-module.wast diff --git a/src/passes/PostEmscripten.cpp b/src/passes/PostEmscripten.cpp index b569cd0e054..39bc3e8890b 100644 --- a/src/passes/PostEmscripten.cpp +++ b/src/passes/PostEmscripten.cpp @@ -186,10 +186,13 @@ IString EM_JS_PREFIX("__em_js__"); IString EM_JS_DEPS_PREFIX("__em_lib_deps_"); struct EmJsWalker : public PostWalker { + bool sideModule; std::vector toRemove; + EmJsWalker(bool sideModule) : sideModule(sideModule) {} + void visitExport(Export* curr) { - if (curr->name.startsWith(EM_JS_PREFIX)) { + if (!sideModule && curr->name.startsWith(EM_JS_PREFIX)) { toRemove.push_back(*curr); } if (curr->name.startsWith(EM_JS_DEPS_PREFIX)) { @@ -215,14 +218,15 @@ struct PostEmscripten : public Pass { auto& options = getPassOptions(); auto sideModule = options.hasArgument("post-emscripten-side-module"); if (!sideModule) { + removeData(module, segmentOffsets, "__start_em_asm", "__stop_em_asm"); + removeData(module, segmentOffsets, "__start_em_js", "__stop_em_js"); + // Side modules read EM_ASM data from the module based on these exports // so we need to keep them around in that case. - removeData(module, segmentOffsets, "__start_em_asm", "__stop_em_asm"); module.removeExport("__start_em_asm"); module.removeExport("__stop_em_asm"); } - removeData(module, segmentOffsets, "__start_em_js", "__stop_em_js"); removeData( module, segmentOffsets, "__start_em_lib_deps", "__stop_em_lib_deps"); module.removeExport("__start_em_js"); @@ -232,7 +236,9 @@ struct PostEmscripten : public Pass { } void removeEmJsExports(Module& module) { - EmJsWalker walker; + auto& options = getPassOptions(); + auto sideModule = options.hasArgument("post-emscripten-side-module"); + EmJsWalker walker(sideModule); walker.walkModule(&module); for (const Export& exp : walker.toRemove) { if (exp.kind == ExternalKind::Function) { diff --git a/test/lit/passes/post-emscripten-side-module.wast b/test/lit/passes/post-emscripten-side-module.wast new file mode 100644 index 00000000000..9da77f5588b --- /dev/null +++ b/test/lit/passes/post-emscripten-side-module.wast @@ -0,0 +1,49 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; RUN: wasm-opt %s --post-emscripten --pass-arg=post-emscripten-side-module -S -o - | filecheck %s + +;; Checks that the __start_em_asm/__stop_em_asm are preserverd, along with +;; all __em_js__ exports. + +(module + ;; CHECK: (global $em_asm_start i32 (i32.const 1000)) + (global $em_asm_start i32 (i32.const 1000)) + ;; CHECK: (global $em_asm_stop i32 (i32.const 1011)) + (global $em_asm_stop i32 (i32.const 1011)) + ;; CHECK: (global $em_js_start i32 (i32.const 2006)) + (global $em_js_start i32 (i32.const 2006)) + ;; CHECK: (global $em_js_stop i32 (i32.const 2015)) + (global $em_js_stop i32 (i32.const 2015)) + ;; CHECK: (global $__em_js__foo i32 (i32.const 2015)) + (global $__em_js__foo i32 (i32.const 2015)) + ;; CHECK: (global $em_lib_deps_start i32 (i32.const 3000)) + (global $em_lib_deps_start i32 (i32.const 3000)) + ;; CHECK: (global $em_lib_deps_stop i32 (i32.const 3009)) + (global $em_lib_deps_stop i32 (i32.const 3009)) + ;; CHECK: (global $foo_start i32 (i32.const 4000)) + (global $foo_start i32 (i32.const 4000)) + ;; CHECK: (global $foo_stop i32 (i32.const 4015)) + (global $foo_stop i32 (i32.const 4015)) + (memory 10 10) + ;; CHECK: (memory $0 10 10) + + ;; CHECK: (data $data1 (i32.const 1000) "hello world") + (data $data1 (i32.const 1000) "hello world") + ;; CHECK: (data $data2 (i32.const 2000) "hello DELETE ME world") + (data $data2 (i32.const 2000) "hello DELETE ME world") + ;; CHECK: (data $data3 (i32.const 3000) "") + (data $data3 (i32.const 3000) "some deps") + ;; CHECK: (export "__start_em_asm" (global $em_asm_start)) + (export "__start_em_asm" (global $em_asm_start)) + ;; CHECK: (export "__stop_em_asm" (global $em_asm_stop)) + (export "__stop_em_asm" (global $em_asm_stop)) + (export "__start_em_js" (global $em_js_start)) + (export "__stop_em_js" (global $em_js_stop)) + ;; CHECK: (export "__em_js__foo" (global $__em_js__foo)) + (export "__em_js__foo" (global $__em_js__foo)) + (export "__start_em_lib_deps" (global $em_lib_deps_start)) + (export "__stop_em_lib_deps" (global $em_lib_deps_stop)) + ;; CHECK: (export "__start_foo" (global $foo_start)) + (export "__start_foo" (global $foo_start)) + ;; CHECK: (export "__stop_foo" (global $foo_stop)) + (export "__stop_foo" (global $foo_stop)) +) diff --git a/test/lit/passes/post-emscripten.wast b/test/lit/passes/post-emscripten.wast index fc61e8845b7..cce4b55a5de 100644 --- a/test/lit/passes/post-emscripten.wast +++ b/test/lit/passes/post-emscripten.wast @@ -41,4 +41,3 @@ ;; CHECK: (export "__stop_foo" (global $foo_stop)) (export "__stop_foo" (global $foo_stop)) ) -