Skip to content

Commit 1545deb

Browse files
authored
PostEmscripten: Preserve __em_js__ exports in side modules (#5780)
1 parent 34c10dc commit 1545deb

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/passes/PostEmscripten.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ IString EM_JS_PREFIX("__em_js__");
186186
IString EM_JS_DEPS_PREFIX("__em_lib_deps_");
187187

188188
struct EmJsWalker : public PostWalker<EmJsWalker> {
189+
bool sideModule;
189190
std::vector<Export> toRemove;
190191

192+
EmJsWalker(bool sideModule) : sideModule(sideModule) {}
193+
191194
void visitExport(Export* curr) {
192-
if (curr->name.startsWith(EM_JS_PREFIX)) {
195+
if (!sideModule && curr->name.startsWith(EM_JS_PREFIX)) {
193196
toRemove.push_back(*curr);
194197
}
195198
if (curr->name.startsWith(EM_JS_DEPS_PREFIX)) {
@@ -215,14 +218,15 @@ struct PostEmscripten : public Pass {
215218
auto& options = getPassOptions();
216219
auto sideModule = options.hasArgument("post-emscripten-side-module");
217220
if (!sideModule) {
221+
removeData(module, segmentOffsets, "__start_em_asm", "__stop_em_asm");
222+
removeData(module, segmentOffsets, "__start_em_js", "__stop_em_js");
223+
218224
// Side modules read EM_ASM data from the module based on these exports
219225
// so we need to keep them around in that case.
220-
removeData(module, segmentOffsets, "__start_em_asm", "__stop_em_asm");
221226
module.removeExport("__start_em_asm");
222227
module.removeExport("__stop_em_asm");
223228
}
224229

225-
removeData(module, segmentOffsets, "__start_em_js", "__stop_em_js");
226230
removeData(
227231
module, segmentOffsets, "__start_em_lib_deps", "__stop_em_lib_deps");
228232
module.removeExport("__start_em_js");
@@ -232,7 +236,9 @@ struct PostEmscripten : public Pass {
232236
}
233237

234238
void removeEmJsExports(Module& module) {
235-
EmJsWalker walker;
239+
auto& options = getPassOptions();
240+
auto sideModule = options.hasArgument("post-emscripten-side-module");
241+
EmJsWalker walker(sideModule);
236242
walker.walkModule(&module);
237243
for (const Export& exp : walker.toRemove) {
238244
if (exp.kind == ExternalKind::Function) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
;; RUN: wasm-opt %s --post-emscripten --pass-arg=post-emscripten-side-module -S -o - | filecheck %s
3+
4+
;; Checks that the __start_em_asm/__stop_em_asm are preserverd, along with
5+
;; all __em_js__ exports.
6+
7+
(module
8+
;; CHECK: (global $em_asm_start i32 (i32.const 1000))
9+
(global $em_asm_start i32 (i32.const 1000))
10+
;; CHECK: (global $em_asm_stop i32 (i32.const 1011))
11+
(global $em_asm_stop i32 (i32.const 1011))
12+
;; CHECK: (global $em_js_start i32 (i32.const 2006))
13+
(global $em_js_start i32 (i32.const 2006))
14+
;; CHECK: (global $em_js_stop i32 (i32.const 2015))
15+
(global $em_js_stop i32 (i32.const 2015))
16+
;; CHECK: (global $__em_js__foo i32 (i32.const 2015))
17+
(global $__em_js__foo i32 (i32.const 2015))
18+
;; CHECK: (global $em_lib_deps_start i32 (i32.const 3000))
19+
(global $em_lib_deps_start i32 (i32.const 3000))
20+
;; CHECK: (global $em_lib_deps_stop i32 (i32.const 3009))
21+
(global $em_lib_deps_stop i32 (i32.const 3009))
22+
;; CHECK: (global $foo_start i32 (i32.const 4000))
23+
(global $foo_start i32 (i32.const 4000))
24+
;; CHECK: (global $foo_stop i32 (i32.const 4015))
25+
(global $foo_stop i32 (i32.const 4015))
26+
(memory 10 10)
27+
;; CHECK: (memory $0 10 10)
28+
29+
;; CHECK: (data $data1 (i32.const 1000) "hello world")
30+
(data $data1 (i32.const 1000) "hello world")
31+
;; CHECK: (data $data2 (i32.const 2000) "hello DELETE ME world")
32+
(data $data2 (i32.const 2000) "hello DELETE ME world")
33+
;; CHECK: (data $data3 (i32.const 3000) "")
34+
(data $data3 (i32.const 3000) "some deps")
35+
;; CHECK: (export "__start_em_asm" (global $em_asm_start))
36+
(export "__start_em_asm" (global $em_asm_start))
37+
;; CHECK: (export "__stop_em_asm" (global $em_asm_stop))
38+
(export "__stop_em_asm" (global $em_asm_stop))
39+
(export "__start_em_js" (global $em_js_start))
40+
(export "__stop_em_js" (global $em_js_stop))
41+
;; CHECK: (export "__em_js__foo" (global $__em_js__foo))
42+
(export "__em_js__foo" (global $__em_js__foo))
43+
(export "__start_em_lib_deps" (global $em_lib_deps_start))
44+
(export "__stop_em_lib_deps" (global $em_lib_deps_stop))
45+
;; CHECK: (export "__start_foo" (global $foo_start))
46+
(export "__start_foo" (global $foo_start))
47+
;; CHECK: (export "__stop_foo" (global $foo_stop))
48+
(export "__stop_foo" (global $foo_stop))
49+
)

test/lit/passes/post-emscripten.wast

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@
4141
;; CHECK: (export "__stop_foo" (global $foo_stop))
4242
(export "__stop_foo" (global $foo_stop))
4343
)
44-

0 commit comments

Comments
 (0)