Skip to content

Commit a0f5fb1

Browse files
committed
Add asyncify-export-globals argument
In the past emscripten would build the main module as relocatable, but these days we build it statically and in that case it makes sense for the main module to define and export these globals, rather than defining them in JS.
1 parent 6a34213 commit a0f5fb1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/passes/Asyncify.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,8 @@ struct Asyncify : public Pass {
17591759
String::Split::NewLineOr(","));
17601760
auto asserts = hasArgument("asyncify-asserts");
17611761
auto verbose = hasArgument("asyncify-verbose");
1762-
auto relocatable = hasArgument("asyncify-relocatable");
1762+
auto importGlobals = hasArgument("asyncify-relocatable");
1763+
auto exportGlobals = hasArgument("asyncify-export-globals");
17631764
auto secondaryMemory = hasArgument("asyncify-in-secondary-memory");
17641765
auto propagateAddList = hasArgument("asyncify-propagate-addlist");
17651766

@@ -1826,7 +1827,7 @@ struct Asyncify : public Pass {
18261827
verbose);
18271828

18281829
// Add necessary globals before we emit code to use them.
1829-
addGlobals(module, relocatable);
1830+
addGlobals(module, importGlobals, exportGlobals);
18301831

18311832
// Compute the set of functions we will instrument. All of the passes we run
18321833
// below only need to run there.
@@ -1904,8 +1905,11 @@ struct Asyncify : public Pass {
19041905
}
19051906

19061907
private:
1907-
void addGlobals(Module* module, bool imported) {
1908+
void addGlobals(Module* module, bool imported, bool exported) {
19081909
Builder builder(*module);
1910+
// It doesn't make sense to both import and export these globals at the
1911+
// same time.
1912+
assert(!(imported && exported));
19091913

19101914
auto asyncifyState = builder.makeGlobal(ASYNCIFY_STATE,
19111915
Type::i32,
@@ -1926,6 +1930,11 @@ struct Asyncify : public Pass {
19261930
asyncifyData->base = ASYNCIFY_DATA;
19271931
}
19281932
module->addGlobal(std::move(asyncifyData));
1933+
1934+
if (exported) {
1935+
module->addExport(builder.makeExport(ASYNCIFY_STATE, ASYNCIFY_STATE, ExternalKind::Global));
1936+
module->addExport(builder.makeExport(ASYNCIFY_DATA, ASYNCIFY_DATA, ExternalKind::Global));
1937+
}
19291938
}
19301939

19311940
void addFunctions(Module* module) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
;; RUN: wasm-opt --enable-mutable-globals --asyncify --pass-arg=asyncify-export-globals -S -o - | filecheck %s
2+
3+
(module
4+
)
5+
;; CHECK: (global $__asyncify_state (mut i32) (i32.const 0))
6+
7+
;; CHECK: (global $__asyncify_data (mut i32) (i32.const 0))
8+
9+
;; CHECK: (export "__asyncify_state" (global $__asyncify_state))
10+
11+
;; CHECK: (export "__asyncify_data" (global $__asyncify_data))

0 commit comments

Comments
 (0)