Skip to content

In ASYNCIFY=2 build mode function arguments come in as nulls #20490

Done

Description

@juj

a.cpp

#include <emscripten.h>
struct options
{
	int x;
};

extern "C" int navigator_gpu_request_adapter_sync(options *opts);

int main()
{
	options opts = { 100 };
	int adapter = navigator_gpu_request_adapter_sync(&opts);
	EM_ASM(console.log(`Got adapter ${0}`), adapter);
}

lib_a.js

mergeInto(LibraryManager.library, {
  navigator_gpu_request_adapter_sync__sig: 'ip',
  navigator_gpu_request_adapter_sync__async: true,
  navigator_gpu_request_adapter_sync: function(opts) {
    console.error(`navigator_gpu_request_adapter_sync: opts: ${opts}`);
    if (!opts) throw 'opts cannot be null';
    var promise = navigator.gpu.requestAdapter().then(a => { return 42; });
    return Asyncify.handleAsync(() => {
      return promise;
    });
  }
});
em++ a.cpp --js-library lib_a.js -sASYNCIFY=1 -sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync'] -o a.html

works as expected, but

em++ a.cpp --js-library lib_a.js -sASYNCIFY=2 -sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync'] -o a.html

doesn't, and instead raises the exception opts cannot be null.

Tested in Chrome Canary 120.0.6076.0 (Official Build) canary (64-bit) on Windows.

I find there's not much documentation on JSPI, but based on the test suite code, it looks like it should work by just replacing -sASYNCIFY=1 with -sASYNCIFY=2 (and adding the __sig directives)?

Activity

  1. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    em++ a.cpp --js-library lib_a.js -sASYNCIFY=1 -sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync'] -o a.html
    works as expected

    Actually, looks like ASYNCIFY=1 does not work like I expect it to. What I am seeing is that the function navigator_gpu_request_adapter_sync() ends up being called twice:

    image

    I wonder if I've got the syntax wrong? Although I see I first implemented ASYNCIFY=1 support into wasm_webgpu two years ago, and back then I did not experience this double call mechanism - and the code has not changed since. That is a bit puzzling..

  2. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    @juj The problem is that Asyncify.handleAsync must wrap the entire import. The wrapper is what takes care of imports being called twice - once for actual call before unwind and once for returning result during rewind - and ensures that the JS function inside is actually called only once like a regular JS function.

  3. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    Something like this should work as expected (made couple more minor changes along the way):

    mergeInto(LibraryManager.library, {
      navigator_gpu_request_adapter_sync__sig: 'ip',
      navigator_gpu_request_adapter_sync__async: true,
      navigator_gpu_request_adapter_sync: (opts) => Asyncify.handleAsync(async () => {
        console.error(`navigator_gpu_request_adapter_sync: opts: ${opts}`);
        if (!opts) throw new Error('opts cannot be null');
        await navigator.gpu.requestAdapter();
        return 42;
      }),
    });
  4. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    The problem is that Asyncify.handleAsync must wrap the entire import.

    Thanks. This requirement is only for JSPI, and not for ASYNCIFY=1 right?

    Would that form of code work on both ASYNCIFY=1 and ASYNCIFY=2?

  5. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    This requirement is only for JSPI, and not for ASYNCIFY=1 right?

    No, that's for both. In JSPI it matters less actually because it does less work in the wrapper (only runtime keepalive counter tracking), but it's still useful to keep it for both.

  6. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    For the sake of another example, this is how we implement internal functions too, e.g.:

    _emval_await__async: true,
    _emval_await: (promise) => {
    return Asyncify.handleAsync(() => {
    promise = Emval.toValue(promise);
    return promise.then(Emval.toHandle);
    });
    },
    or
    emscripten_idb_load__async: true,
    emscripten_idb_load__deps: ['malloc'],
    emscripten_idb_load: (db, id, pbuffer, pnum, perror) => {
    return Asyncify.handleSleep((wakeUp) => {
    IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), (error, byteArray) => {
    if (error) {
    {{{ makeSetValue('perror', 0, '1', 'i32') }}};
    wakeUp();
    return;
    }
    var buffer = _malloc(byteArray.length); // must be freed by the caller!
    HEAPU8.set(byteArray, buffer);
    {{{ makeSetValue('pbuffer', 0, 'buffer', 'i32') }}};
    {{{ makeSetValue('pnum', 0, 'byteArray.length', 'i32') }}};
    {{{ makeSetValue('perror', 0, '0', 'i32') }}};
    wakeUp();
    });
    });
    },

  7. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    The problem is that Asyncify.handleAsync must wrap the entire import.

    How do I handle parameters that are passed in to the function on the stack?

    If I do

      navigator_gpu_request_adapter_sync: function(opts) {
        return Asyncify.handleAsync(() => {
          let someOption = HEAPU32[opts>>2];
          ...
        });
      }

    is that first part executed synchronously before yielding back from the function? (so that I know that the caller's stack is still intact?)

  8. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    Something like this should work as expected

    I find that with that example, I do still get the error Error: opts cannot be null when building with -sASYNCIFY=2.

  9. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    is that first part executed synchronously before yielding back from the function?

    Hmm looks like that is the case.. so all good there.

  10. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    How do I handle parameters that are passed in to the function on the stack?

    From the PoV of JS function inside, parameters should always just be there and don't need special handling.

    They're captured by closure + Wasm itself is suspended so pointers are always valid during the entire lifetime of the async function, both before and after suspending.

  11. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    Alright, I was able to get -sASYNCIFY=1 to work by wrapping the code over the whole function (juj/wasm_webgpu@750df43). Although there is one issue, I'll open a separate ticket about that.

    Still struggling to get -sASYNCIFY=2 to work, I am unable to get function parameters into the asyncified functions.

  12. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    Yeah sorry can't help with JSPI, I only played with it briefly and also ran into couple of bugs before.

  13. RReverser commented on Oct 19, 2023

    @RReverser
    Collaborator

    Side-note: if you're not limited by very old browsers (which, in case of WebGPU, I can only assume you're not), I'd really recommend to use async-await over .then. Aside from readability, thanks to sequential nature and avoiding closures, it can be better optimised by modern engines and results in shallower and more readable stacktraces in DevTools and errors. https://v8.dev/blog/fast-async

    Emscripten already has problems with very deep stacktraces due to usage of .then to simulate async loops (e.g. in proxying and Asyncify itself) but Emscripten is limited by having to support old engines, so can't use native async-await everywhere. But for a project targeting specifically modern browsers anyway it shouldn't be an issue.

  14. juj commented on Oct 19, 2023

    @juj
    CollaboratorAuthor

    Thanks, I didn't know that there were any functional differences with these keywords. I thought they were just syntactic sugar. I'll look into migrating the code over.

  15. juj commented on Dec 1, 2026

    @juj
    CollaboratorAuthor

    This issue looks to have been fixed at some point.

    Claude's test log:


    Issue #20490 is no longer relevant: on current main the pointer argument arrives intact under JSPI, with both the original library code from the issue and the wrapped form RReverser suggested.

    I used your a.cpp and library, replacing only the navigator.gpu.requestAdapter() call with a timer-based promise so it can run in Node, and making the library also print opts.x:

    ┌──────────────┬────────────────────────────────────────────────────┬───────────────────────────────┐
    │    Build     │               Original library form                │         Wrapped form          │
    ├──────────────┼────────────────────────────────────────────────────┼───────────────────────────────┤
    │ -sASYNCIFY=1 │ opts.x: 100, "Got adapter 42" (JS body runs twice) │ opts.x: 100, "Got adapter 42" │
    ├──────────────┼────────────────────────────────────────────────────┼───────────────────────────────┤
    │ -sASYNCIFY=2 │ opts.x: 100, "Got adapter 42"                      │ opts.x: 100, "Got adapter 42" │
    ├──────────────┼────────────────────────────────────────────────────┼───────────────────────────────┤
    │ -sJSPI       │ opts.x: 100, "Got adapter 42"                      │ opts.x: 100, "Got adapter 42" │
    └──────────────┴────────────────────────────────────────────────────┴───────────────────────────────┘
    
    • Engines: all six ran in Node 24.19 with --experimental-wasm-jspi. The original-form -sJSPI build also ran correctly in headless Chrome 150, since the issue was reported in Chrome.
    • Double call under -sASYNCIFY=1: the original form still runs its JS body twice, as you noted in your first comment. That is the expected behaviour RReverser explained (the import is re-entered on rewind unless Asyncify.handleAsync wraps the whole function), not a bug.
    • Flag name: -sASYNCIFY=2 still works but is documented as deprecated in favour of -sJSPI.

    I did not find which change fixed the null argument, and I did not test the real WebGPU call. Tested on 504e57f, three commits behind today's origin/main; none of those touch the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions