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
19061924private:
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 ) {
0 commit comments