Skip to content

Commit ecf25d6

Browse files
authored
Add asyncify-export-globals argument (#8074)
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 ecf25d6

File tree

4 files changed

+52
-117
lines changed

4 files changed

+52
-117
lines changed

src/passes/Asyncify.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@
323323
// model where you can specify "instrument, but not indirect calls from me"
324324
// would likely have little benefit.)
325325
//
326+
// In addition, there are arguments for controlling the import/export of the
327+
// internal globals used by Asyncify. These can be useful in dynamic linking.
328+
// By default these globals are are internal and neither imported nor exported.
329+
//
330+
// --pass-arg=import-globals
331+
//
332+
// Import the internal globals used by Asyncify. This allows them to be
333+
// defined in another module.
334+
//
335+
// --pass-arg=export-globals
336+
//
337+
// Export the internal globals used by Asyncify. This allows them to be
338+
// imported into anther module built with --pass-arg=import-globals
339+
//
326340
// TODO When wasm has GC, extending the live ranges of locals can keep things
327341
// alive unnecessarily. We may want to set locals to null at the end
328342
// of their original range.
@@ -1759,7 +1773,11 @@ struct Asyncify : public Pass {
17591773
String::Split::NewLineOr(","));
17601774
auto asserts = hasArgument("asyncify-asserts");
17611775
auto verbose = hasArgument("asyncify-verbose");
1762-
auto relocatable = hasArgument("asyncify-relocatable");
1776+
// TODO: Remove the legacy asyncify-relocatable name once emscripten is
1777+
// updated.
1778+
auto importGlobals = hasArgument("asyncify-import-globals") ||
1779+
hasArgument("asyncify-relocatable");
1780+
auto exportGlobals = hasArgument("asyncify-export-globals");
17631781
auto secondaryMemory = hasArgument("asyncify-in-secondary-memory");
17641782
auto propagateAddList = hasArgument("asyncify-propagate-addlist");
17651783

@@ -1826,7 +1844,7 @@ struct Asyncify : public Pass {
18261844
verbose);
18271845

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

18311849
// Compute the set of functions we will instrument. All of the passes we run
18321850
// below only need to run there.
@@ -1904,8 +1922,11 @@ struct Asyncify : public Pass {
19041922
}
19051923

19061924
private:
1907-
void addGlobals(Module* module, bool imported) {
1925+
void addGlobals(Module* module, bool imported, bool exported) {
19081926
Builder builder(*module);
1927+
// It doesn't make sense to both import and export these globals at the
1928+
// same time.
1929+
assert(!(imported && exported));
19091930

19101931
auto asyncifyState = builder.makeGlobal(ASYNCIFY_STATE,
19111932
Type::i32,
@@ -1926,6 +1947,13 @@ struct Asyncify : public Pass {
19261947
asyncifyData->base = ASYNCIFY_DATA;
19271948
}
19281949
module->addGlobal(std::move(asyncifyData));
1950+
1951+
if (exported) {
1952+
module->addExport(builder.makeExport(
1953+
ASYNCIFY_STATE, ASYNCIFY_STATE, ExternalKind::Global));
1954+
module->addExport(
1955+
builder.makeExport(ASYNCIFY_DATA, ASYNCIFY_DATA, ExternalKind::Global));
1956+
}
19291957
}
19301958

19311959
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))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
;; RUN: wasm-opt --enable-mutable-globals --asyncify --pass-arg=asyncify-import-globals -S -o - | filecheck %s
2+
3+
;; Also test asyncify-relocatable which is an alias for asyncify-import-globals
4+
;; RUN: wasm-opt --enable-mutable-globals --asyncify --pass-arg=asyncify-relocatable -S -o - | filecheck %s
5+
6+
(module
7+
)
8+
9+
;; CHECK: (import "env" "__asyncify_state" (global $__asyncify_state (mut i32)))
10+
;; CHECK: (import "env" "__asyncify_data" (global $__asyncify_data (mut i32)))

test/lit/passes/asyncify_pass-arg=asyncify-side-module.wast

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)