Skip to content

Commit 484f62f

Browse files
authored
Fix EM_ASM+pthreads (#1891)
To calculate the metadata, we must look at the segments. If we split them out earlier (which we do for threads), they aren't there.
1 parent ddb5acd commit 484f62f

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,8 @@ int main(int argc, const char *argv[]) {
204204

205205
generator.generateDynCallThunks();
206206
generator.generateJSCallThunks(numReservedFunctionPointers);
207-
if (!dataSegmentFile.empty()) {
208-
Output memInitFile(dataSegmentFile, Flags::Binary, Flags::Release);
209-
generator.separateDataSegments(&memInitFile);
210-
}
211207

212-
// Finally, legalize the wasm.
208+
// Legalize the wasm.
213209
{
214210
PassRunner passRunner(&wasm);
215211
passRunner.setDebug(options.debug);
@@ -221,9 +217,16 @@ int main(int argc, const char *argv[]) {
221217
passRunner.run();
222218
}
223219

224-
// All changes to the wasm are done, create the metadata.
220+
// Substantial changes to the wasm are done, enough to create the metadata.
225221
std::string metadata = generator.generateEmscriptenMetadata(dataSize, initializerFunctions, numReservedFunctionPointers);
226222

223+
// Finally, separate out data segments if relevant (they may have been needed
224+
// for metadata).
225+
if (!dataSegmentFile.empty()) {
226+
Output memInitFile(dataSegmentFile, Flags::Binary, Flags::Release);
227+
generator.separateDataSegments(&memInitFile);
228+
}
229+
227230
if (options.debug) {
228231
std::cerr << "Module after:\n";
229232
WasmPrinter::printModule(&wasm, std::cerr);
Binary file not shown.

test/lld/em_asm.wast.mem.out

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
(module
2+
(type $0 (func (param i32) (result i32)))
3+
(type $1 (func (param i32 i32 i32) (result i32)))
4+
(type $2 (func (param i32 i32) (result i32)))
5+
(type $3 (func (result i32)))
6+
(type $4 (func))
7+
(type $FUNCSIG$ii (func (param i32) (result i32)))
8+
(type $FUNCSIG$iiii (func (param i32 i32 i32) (result i32)))
9+
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
10+
(import "env" "emscripten_asm_const_i" (func $emscripten_asm_const_i (param i32) (result i32)))
11+
(import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
12+
(import "env" "emscripten_asm_const_ii" (func $emscripten_asm_const_ii (param i32 i32) (result i32)))
13+
(memory $0 2)
14+
(table $0 1 1 funcref)
15+
(global $global$0 (mut i32) (i32.const 16384))
16+
(global $global$1 i32 (i32.const 66192))
17+
(global $global$2 i32 (i32.const 652))
18+
(export "memory" (memory $0))
19+
(export "__post_instantiate" (func $__wasm_call_ctors))
20+
(export "main" (func $main))
21+
(export "__heap_base" (global $global$1))
22+
(export "__data_end" (global $global$2))
23+
(export "stackSave" (func $stackSave))
24+
(export "stackAlloc" (func $stackAlloc))
25+
(export "stackRestore" (func $stackRestore))
26+
(export "__growWasmMemory" (func $__growWasmMemory))
27+
(func $main (; 3 ;) (type $3) (result i32)
28+
(drop
29+
(call $emscripten_asm_const_i
30+
(i32.const 0)
31+
)
32+
)
33+
(drop
34+
(call $emscripten_asm_const_ii
35+
(i32.const 2)
36+
(call $emscripten_asm_const_iii
37+
(i32.const 1)
38+
(i32.const 13)
39+
(i32.const 27)
40+
)
41+
)
42+
)
43+
(i32.const 0)
44+
)
45+
(func $__wasm_call_ctors (; 4 ;) (type $4)
46+
(nop)
47+
)
48+
(func $stackSave (; 5 ;) (result i32)
49+
(global.get $global$0)
50+
)
51+
(func $stackAlloc (; 6 ;) (param $0 i32) (result i32)
52+
(local $1 i32)
53+
(global.set $global$0
54+
(local.tee $1
55+
(i32.and
56+
(i32.sub
57+
(global.get $global$0)
58+
(local.get $0)
59+
)
60+
(i32.const -16)
61+
)
62+
)
63+
)
64+
(local.get $1)
65+
)
66+
(func $stackRestore (; 7 ;) (param $0 i32)
67+
(global.set $global$0
68+
(local.get $0)
69+
)
70+
)
71+
(func $__growWasmMemory (; 8 ;) (param $newSize i32) (result i32)
72+
(grow_memory
73+
(local.get $newSize)
74+
)
75+
)
76+
)
77+
(;
78+
--BEGIN METADATA --
79+
{
80+
"asmConsts": {
81+
"2": ["{ Module.print(\"Got \" + $0); }", ["ii"], [""]],
82+
"0": ["{ Module.print(\"Hello world\"); }", ["i"], [""]],
83+
"1": ["{ return $0 + $1; }", ["iii"], [""]]
84+
},
85+
"staticBump": 84,
86+
"tableSize": 1,
87+
"initializers": [
88+
"__post_instantiate"
89+
],
90+
"declares": [
91+
],
92+
"externs": [
93+
],
94+
"implementedFunctions": [
95+
"___post_instantiate",
96+
"_main",
97+
"_stackSave",
98+
"_stackAlloc",
99+
"_stackRestore",
100+
"___growWasmMemory"
101+
],
102+
"exports": [
103+
"memory",
104+
"__post_instantiate",
105+
"main",
106+
"__heap_base",
107+
"__data_end",
108+
"stackSave",
109+
"stackAlloc",
110+
"stackRestore",
111+
"__growWasmMemory"
112+
],
113+
"invokeFuncs": [
114+
]
115+
}
116+
-- END METADATA --
117+
;)

0 commit comments

Comments
 (0)