Skip to content

Closure Compiler fails in PROXY_TO_PTHREAD + ENVIRONMENT=worker build #13595

Closed

Description

@RReverser

Sounds similar to #10502, but only occurs under PROXY_TO_PTHREAD:

building:ERROR: Closure compiler run failed:

building:ERROR: /tmp/emscripten_temp_9u9jsdhz/avif_enc_mt.wasm.js.pgrow.js.jso.js.jso.js.jso.js.jso.js:104:1: WARNING - [JSC_REFERENCE_BEFORE_DECLARE] Variable referenced before declaration: buffer
  104|  buffer = Module["buffer"];
        ^^^^^^

/tmp/emscripten_temp_9u9jsdhz/avif_enc_mt.wasm.js.pgrow.js.jso.js.jso.js.jso.js.jso.js:1164:18: ERROR - [JSC_UNDEFINED_VARIABLE] variable _main is undeclared
  1164|  var returnCode = _main(argc, argv);
                          ^^^^^

1 error(s), 1 warning(s)

Activity

  1. kripken commented on Mar 3, 2021

    @kripken
    Member

    That's very odd. I can't think of a situation in which buffer would not exist - it's really basic, and used in all modes at all times. We store the main ArrayBuffer there.

    I can't seem to reproduce this either. I tried this and variations (different opt levels), all work:

    ./emcc tests/hello_world.c -s ENVIRONMENT=worker --closure 1 -s PROXY_TO_PTHREAD -s USE_PTHREADS -O3
    
  2. RReverser commented on Mar 4, 2021

    @RReverser
    CollaboratorAuthor

    @kripken The specific codebase in question is this folder: https://github.com/GoogleChromeLabs/squoosh/tree/dev/codecs/avif

    To reproduce, download it (either as part of the full repo or via Github's "download directory" feature), add -s PROXY_TO_PTHREAD here and run npm run build in that folder which will build everything in a Docker container.

    You should see the same compilation error as above (well I also added -g1 on the same line to make error more readable, but it appears either way).

  3. RReverser commented on Mar 4, 2021

    @RReverser
    CollaboratorAuthor

    When I look at the generated code without Closure, indeed I can see this function:

    ...
    function ___call_main(argc, argv) {
     var returnCode = _main(argc, argv);
    }
    ...

    that is later used in:

    ...
    var asmLibraryArg = {
     "m": ___assert_fail,
     "T": ___call_main,
    ...

    even though _main is nowhere to be found.

    As for buffer, it is defined, yeah, but as the error says "Variable referenced before declaration" - as in, it's just an ordering issue.

    Code looks like this:

    ...
    // line 100
    if (ENVIRONMENT_IS_PTHREAD) {
     buffer = Module["buffer"];
    }
    ...
    // line 409
    var buffer, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64;
    ...

    so it technically still works, since JS hoists all variable declarations, but Closure warns about it as a potential issue.

    And then later in the code:

    // line 431
    ...
    if (ENVIRONMENT_IS_PTHREAD) {
     wasmMemory = Module["wasmMemory"];
     buffer = Module["buffer"];
    } else {
    ...
    // line 453
    if (wasmMemory) {
     buffer = wasmMemory.buffer;
    }

    so it looks like there is also some duplication happening too, where buffer keeps getting reassigned from different sources?

  4. RReverser commented on Mar 4, 2021

    @RReverser
    CollaboratorAuthor

    Okay, I can reproduce even with a tiny library sample:

    #include <emscripten.h>
    
    EMSCRIPTEN_KEEPALIVE
    int foo() {
    	return 42;
    }
    $ emcc temp.c -s ENVIRONMENT=worker -pthread -O3 -s PROXY_TO_PTHREAD -g1 --closure 1
    building:ERROR: Closure compiler run failed:
    
    building:ERROR: /tmp/emscripten_temp_zvt8ybgw/a.out.js.jso.js.jso.js.jso.js.jso.js:41:1: WARNING - [JSC_REFERENCE_BEFORE_DECLARE] Variable referenced before declaration: buffer
      41|  buffer = Module["buffer"];
           ^^^^^^
    
    /tmp/emscripten_temp_zvt8ybgw/a.out.js.jso.js.jso.js.jso.js.jso.js:835:18: ERROR - [JSC_UNDEFINED_VARIABLE] variable _main is undeclared
      835|  var returnCode = _main(argc, argv);
                             ^^^^^
    
    1 error(s), 1 warning(s)
  5. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    @kripken @sbc100 Can someone take a look at this please?

  6. kripken commented on Mar 8, 2021

    @kripken
    Member

    Thanks for the reduced testcase @RReverser

    So yes, the buffer issue is just a warning. The actual error is that reference to _main.

    What seems to happen here is that PROXY_TO_PTHREAD mode assumes there is a main() function. That is kind of the point of the mode, in that all it does is run main() on a worker. But here there is no main, and things break since main_thread() in C is kept alive in the table by runtime startup code, and that calls __call_main in JS which assumes there is a main().

    Why are you using that mode without a main @RReverser ? I think we should probably error in that mode. But if we want it to not error and instead do nothing when trying to call main, this fixes the testcase:

    diff --git a/src/library_pthread.js b/src/library_pthread.js
    index 93b184c0f..ce55a8761 100644
    --- a/src/library_pthread.js
    +++ b/src/library_pthread.js
    @@ -1195,6 +1195,7 @@ var LibraryPThread = {
       },
     
       __call_main: function(argc, argv) {
    +#if HAS_MAIN
         var returnCode = {{{ exportedAsmFunc('_main') }}}(argc, argv);
     #if EXIT_RUNTIME
         if (!noExitRuntime) {
    @@ -1211,6 +1212,7 @@ var LibraryPThread = {
         out('Proxied main thread 0x' + _pthread_self().toString(16) + ' finished with return code ' + returnCode + '. EXIT_RUNTIME=0 set, so keeping main thread alive for asynchronous event operations.');
     #endif
     #endif
    +#endif // HAS_MAIN
       },
     
       emscripten_conditional_set_current_thread_status_js: function(expectedStatus, newStatus) {
  7. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    Why are you using that mode without a main @RReverser ?

    It's the only alternative to PTHREAD_POOL_SIZE that allows to avoid blocking when thread pool is exhausted, and instead spawns new threads at-will (this was discovered in overlap with my work in #13591).

    Given that there are cases where it's not possible to predict upper limit for the number of threads, I believe we should support library use-case in PTHREAD_POOL_SIZE as well.

    E.g. this would be the only reliable fix for AVIF deadlocks in Squoosh until they fix maxThreads limit and don't spawn more threads depending on the input.

  8. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    So yes, the buffer issue is just a warning.

    Yeah, but probably should be fixed too - it's a bit strange that it's assigned from multiple places.

  9. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    That is kind of the point of the mode, in that all it does is run main() on a worker. 

    Hm, re-reading this, are you saying that it won't work for other exports and can't be used for libraries to workaround the pthread limit issue as described above?

  10. sbc100 commented on Mar 8, 2021

    @sbc100
    Collaborator

    I guess in that case we could need to proxy all the (library) calls into the module across to the "main" pthread? Do you know if there is any support for that today @kripken ? I does seem like a reasonable request. I would probably complicate the generated JS quite a bit as every exported wasm function would need to be wrapped in something like:

    if (isMainBrowserThread) {
       proxyToMainWorker(..)
    } else {
       runDirectly(..)
    }
    

    Maybe we already have some kind of support for this but I'm not aware of it?

  11. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    I mainly assumed it already worked because @kripken mentioned it as a response to my idea of a separate spawner thread in #11554 (comment):

    Thinking some more on the idea of creating a thread for thread creation, it's similar to what PROXY_TO_PTHREAD does, but also allows keeping main() on the main browser thread. That's an advantage in one way, but it will also be a disadvantage in responsiveness overall, since PROXY_TO_PTHREAD moves all non-essential things off the main thread. So I think it would be best to focus on promoting PROXY_TO_PTHREAD and improving its ease of use, rather than adding another mode.

    I didn't realise it was limited to main only at the moment.

  12. kripken commented on Mar 8, 2021

    @kripken
    Member

    Correct, PROXY_TO_PTHREAD just proxies main. Docs:

    // If set to 1, compiles in a small stub main() in between the real main() which
    // calls pthread_create() to run the application main() in a pthread.  This is
    // something that applications can do manually as well if they wish, this option
    // is provided as convenience.
    

    Proxying main has the effect of proxying the entire app for a normal application, but for a library it does nothing basically.

    (I didn't realize in that earlier discussion that it was a library.)

    For a library, though, how would this work? The difference with a normal application is that we proxy main once and then let things proceed from there. But a library may be called many times from JS, every time the user does Module._exportX(), and we don't want to proxy all those, I assume?

  13. RReverser commented on Mar 8, 2021

    @RReverser
    CollaboratorAuthor

    For a library, though, how would this work?

    Yeah, so I guess going back to the thread I quoted you from, the suggestion for a spawner thread is still valid. That is, we'd want to proxy only pthread_create calls, but not the exports, which makes it sort of the opposite of PROXY_TO_PTHREAD?

    Either way, it seems that an escape hatch for creating arbitrary number of threads would be useful, and this kind of proxying mechanism is the closest so far.

  14. kripken commented on Mar 10, 2021

    @kripken
    Member

    Yes, it sounds like a specific solution for pthread_create and other crucial calls could make sense.

    One possible way to do it is look at it like the reverse of PROXY_TO_PTHREAD, as you say. If the emscripten JS is run in one place, and a pthread is created, and the user's JS run there, then we could easily proxy those calls to the original place the JS was run. But that would require the user JS to be added where the worker is, so it wouldn't feel like the whole thing is a library.

    Alternatively, adding a special thread for running tasks is an option. #13298 could be used here perhaps?

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