Repository navigation
Description
I have an asynchronous JS library function:
C:
void navigator_gpu_request_adapter_async(WGpuRequestAdapterCallback adapterCallback);JS:
navigator_gpu_request_adapter_async: function(adapterCallback) {
navigator['gpu']['requestAdapter']().then(adapter => {
{{{ makeDynCall('vi', 'adapterCallback') }}}(wgpuStore(adapter));
});
},To make it possible to synchronously initialize a WebGPU adapter, I want to use ASYNCIFY, so I create a new variant of this function, navigator_gpu_request_adapter_sync that reads
C:
WGpuAdapter navigator_gpu_request_adapter_sync(void);JS:
navigator_gpu_request_adapter_sync: function() {
return Asyncify.handleAsync(() => {
return navigator['gpu']['requestAdapter']().then(adapter => {
return wgpuStore(adapter);
})
});
},and build with -sASYNCIFY=1 linker flag.
However, at runtime I get the error:
Uncaught Error: import navigator_gpu_request_adapter_sync was not in ASYNCIFY_IMPORTS, but changed the state
Reading the documentation, the currently intended way to fix this would be to add the linker flag
-sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync']
However I would not like to do that, because of the two problems:
- it leaks abstractions to the user. Any user of my lib_webgpu.js library will need to know/remember which functions need to be annotated with
-sASYNCIFY_IMPORTSwhen they build their program. I.e. one more thing for them to remember to do. - if the users have their own asyncified functions, concatenating/merging lib_webpu.js asyncify imports and their own asyncify imports is not currently possible, i.e. I cannot document them to blindly pass
-sASYNCIFY_IMPORTS=['a','b','c']to their link line, since if they have their ownASYNCIFY_IMPORTSas well, either they will override my imports (hence having no effect), or my documented line will replace their imports (breaking their asyncified functions).
Instead, I would like to declare the function to be asynchronous in my JS file
navigator_gpu_request_adapter_sync__asyncify: true, // Declare this function to require asyncify
navigator_gpu_request_adapter_sync: function() {
return Asyncify.handleAsync(() => {
return navigator['gpu']['requestAdapter']().then(adapter => {
return wgpuStore(adapter);
})
});
},This way both problems 1) and 2) would be fixed, without asking the people to need to know about the internals of my WebGPU JS library and have them manually maintain these types of lists when using the library.
Would this be feasible to implement? (or does it come down to the same JS library parsing order issue that deps_info.py has?)
Activity
sbc100 commentedon Dec 11, 2021
Sounds like a good idea to me. @RReverser has also asked for source annotations to make native functions in needed asyncify.
I think we are fine with ordering because the JS library is compiled during
phase_emscriptwhich happens beforephase_binaryen(which run asyncify and other binaryen passes)RReverser commentedon Dec 11, 2021
Note that similar usecase is already solved via
EM_ASYNC_JSmacro I've added recently - it allows to declare JS functions that will be automatically added to Asyncify list, plus takes care of the wrapping. But yeah, it's not something available in Emscripten's JS libraries at the moment.sbc100 commentedon Dec 11, 2021
Oh interesting.. it looks like one could already piggyback on what you did and just start give the function a name that starts with with
__asyncjs__:emscripten/src/library_async.js
Lines 79 to 80 in 69409a6
But using the
__asyncifytag in the library code sounds good.juj commentedon Dec 11, 2021
That raises two thoughts:
Doesn't that
x.startsWith('__asyncjs__')break under Closure minification? Closure should be instructed never to minify any symbol that starts with__asyncjs__? (not sure if that's possible for Closure)Is there a need to do this
isAsyncifyImportcheck in release/production mode builds? Could that check be put behind ASSERTIONS?sbc100 commentedon Dec 11, 2021
Oh yes, that code I linked to in
emscripten/src/library_async.jsis already inside of#ifdef ASSERTIONSso doesn't effect release builds.The actual way this is handled is here:
emscripten/emcc.py
Lines 97 to 105 in cb545d9
RReverser commentedon Dec 12, 2021
Regardless of the resolution here, I would probably encourage looking into Embind's Asyncify integration added earlier as an alternative.
E.g. the code above
Could be rewritten to retrieve adapter as
valinstance like this:After this
adapteris already a synchronous handle that you can invoke any other APIs on. This approach also doesn't require you to modifyASYNCIFY_IMPORTS.sbc100 commentedon Dec 12, 2021
So far we don't have any emscripten-provided libraries that depend on embind.. so have webgpu depend on embind would be departure from that norm.
I'm not sure it makes sense to build our low level system libraries on embind since IIRC it can be somewhat heavy-weight. Perhaps we could land the version based on the
__asyncifyoption then propose switch to embind as a separate issue where we can assess the cost/benefit.RReverser commentedon Dec 12, 2021
Hmm is that Emscripten-provided library? The only reference to
wgpuStoreI found is coming from this repo, which seems external? https://github.com/juj/wasm_webgpuThat said, given the amount of code I can see suggestion to use Embind is not very practical, as it would be very significant refactoring, so yeah, using
__asyncifyoption makes sense.sbc100 commentedon Dec 12, 2021
Ah I see, yes. Well I think my point still stands, maybe some folks want to use asyncify to its fullest without also using embind?
RReverser commentedon Dec 13, 2021
Oh yeah, could be. The reason I suggested it in this case is because what
wgpuStoredoes (https://github.com/juj/wasm_webgpu/blob/bba80ae7b0dc389f5b3bc05e6816af48781169d8/lib/lib_webgpu.js#L36-L63) is very close to replicatingval's behaviour, and it seemed like a nice opportunity to kill two birds with one stone.But then again, given the amount of code, I can already see it's infeasible endeavour.
RReverser commentedon Dec 13, 2021
FWIW (again, doesn't solve the library use-case, just for the record) yet another alternative, this time without Embind, would be to use the mentioned
EM_ASYNC_JSfrom C++.Before:
After:
juj commentedon Dec 13, 2021
Thanks @RReverser for the suggestion. I am well familiar with embind, I was its maintainer for a while, and also commercially contracted in the past to develop embind back in 2013.
However for this purpose/project, I am not looking to build on top of embind as a dependency, but rather use the direct asyncify API.
Thanks - that reveals I did not much look at the code, but just briefly glanced a few lines around the call site :)
RReverser commentedon Dec 13, 2021
I'm aware :) But you might've not seen the Embind + Asyncify integrations or new Asyncify JS macros I've added during the last year, so it seemed worthwhile to point those out.
juj commentedon Dec 13, 2021
The EM_ASYNC_JS option would unfortunately not work for two reasons:
we do not consider EM_ASM or EM_JS (and hence by extension EM_ASYNC_JS) a production quality feature because of issue Why are JavaScript inline snippets embedded in wasm data? #9366 / EM_JS emits an odd data section to compiled code #12778 .
The WebGPU library is being used with Closure, so EM_ASYNC_JS scheme would break with that in ASSERTIONS builds.
(it is uncommon, but still sometimes important to be able to enable assertions in release builds, e.g. to debug when some release mode optimization mysteriously fails)
Though for curiosity, raised google/closure-compiler#3900 to discuss if Closure would be able to accommodate for this kind of
__asyncjs__naming scheme.RReverser commentedon Dec 13, 2021
Ouch, that does look bad, I thought we stripped those. @sbc100 you mentioned in one of the issues you had a promising fix?
Hmm, but then not only
__asyncjs__*functions should be affected, but any functions in theASYNCIFY_IMPORTSlist as the linked snippet usedvar isAsyncifyImport = ASYNCIFY_IMPORTS.indexOf(x) >= 0even before I added the 2nd part. How come other Asyncify functions aren't broken by Closure?RReverser commentedon Dec 13, 2021
Ah wait, looks like the first issue was fixed in #13443 + WebAssembly/binaryen@0a25916 and 2nd issue was just a duplicate but forgotten to be closed. So today those data sections should be stripped as expected.
juj commentedon Dec 13, 2021
Oh, thanks for digging up that update - closing the old issue.
juj commentedon Oct 19, 2023
It looks like this was addressed in PR #19093, so closing. Big thanks @brendandahl for implementing this!
Add support for JSPI (-sASYNCIFY=2) and add a new sample buffer_map_s…
RReverser commentedon Oct 19, 2023
Note that there are some issues with marking
asyncfunctions for now. Until we figure out automatic wrapping, it's probably best to only rely on__asyncannotation. #20289