Skip to content

Fix closure errors with MINIMAL_RUNTIME + USE_PTHREADS. NFC. #14936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ def default_setting(name, new_default):
'_emscripten_tls_init',
'_pthread_self',
'_pthread_testcancel',
'_exit',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does adding to this list pin down the function from being DCEd if not used? exit() is not used always in pthreads builds?

Copy link
Collaborator Author

@sbc100 sbc100 Aug 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this entire like of functions cannot be DCEd in USE_PTHREADS builds.

However _exit is not special here. Just like all the other functions here is used by library_ptheads.js in general, but might be used in a given application. For example, imagine an application that builds with USE_PTHREADS but never actually creates in any threads.. all of the functions in this list would essentially be unused but also prevented from DCE.

I guess we want some other notion of EXPORTED_FUNCTIONS that also allows for DCE, but I don't know if that exists.

Perhaps we could add something like EXPORTED_FUNCTIONS_FOR_POSSIBLE_INTERNAL_JS_USAGE_ONLY?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe just POSSIBLY_USED_FUNCTIONS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference to _exit is from the exitProcess handling code, and I wonder if we could if that out when EXIT_RUNTIME is not set... that would be a good solution I think. On the other hand the proc_exit library functions (of which exit is an alias) is just a single line of code on MINIMAL_RUNTIME and given that USE_PTHREADS brings in many hundreds of extra lines of code (IIUC) it maybe not that urgent to fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I wrong, please ignore those previous replies.

Adding to EXPORTED_FUNCTIONS does not prevent DCE, because we don't consider them USER_EXPORTED_FUNCTIONS. Only USER_EXPORTED_FUNCTIONS are prevented from DCE.

]
# Some of these symbols are using by worker.js but otherwise unreferenced.
# Because emitDCEGraph only considered the main js file, and not worker.js
Expand Down
9 changes: 7 additions & 2 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var LibraryPThread = {
$PThread__deps: ['_emscripten_thread_init',
'emscripten_futex_wake', '$killThread',
'$cancelThread', '$cleanupThread',
'exit',
#if !MINIMAL_RUNTIME
'$handleException',
#endif
Expand Down Expand Up @@ -57,7 +58,9 @@ var LibraryPThread = {
// things.
PThread['receiveObjectTransfer'] = PThread.receiveObjectTransfer;
PThread['threadInit'] = PThread.threadInit;
#if !MINIMAL_RUNTIME
PThread['setExitStatus'] = PThread.setExitStatus;
#endif
#endif
},
// Maps pthread_t to pthread info objects
Expand Down Expand Up @@ -130,9 +133,11 @@ var LibraryPThread = {
},
#endif

#if !MINIMAL_RUNTIME
setExitStatus: function(status) {
EXITSTATUS = status;
},
#endif

terminateAllThreads: function() {
for (var t in PThread.pthreads) {
Expand Down Expand Up @@ -305,10 +310,10 @@ var LibraryPThread = {
err("exitProcess requested by worker");
#endif
#if MINIMAL_RUNTIME
exit(d['returnCode']);
_exit(d['returnCode']);
#else
try {
exit(d['returnCode']);
_exit(d['returnCode']);
} catch (e) {
handleException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/minimal_runtime_worker_externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
*/

// These externs are needed for MINIMAL_RUNTIME + USE_PTHREADS + !MODULARIZE
// These externs are needed for MINIMAL_RUNTIME + USE_PTHREADS
// This file should go away in the future when worker.js is refactored to live inside the JS module.

var ENVIRONMENT_IS_PTHREAD;
Expand Down
2 changes: 1 addition & 1 deletion src/preamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ if (!ENVIRONMENT_IS_PTHREAD) {
#if USE_PTHREADS
} else {
#if MODULARIZE
updateGlobalBufferAndViews({{{EXPORT_NAME}}}.buffer);
updateGlobalBufferAndViews(Module.buffer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this it fails closure compiler with MyModule not defined. I think this is because closure runs before modularization so at the point of closure compiler MyModule (the EXPORT_NAME) does not exist.

However Module.buffer works just fine here because the modularized code starts with

function(MyModule) {                                                             
  MyModule = MyModule || {};                                                     
                                                                                 
                                                                                 
                                                                                 
var Module = MyModule;  

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, sounds good. And closure will remove the extra var anyhow.

#else
updateGlobalBufferAndViews(wasmMemory.buffer);
#endif
Expand Down
6 changes: 2 additions & 4 deletions src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var ENVIRONMENT_IS_NODE = typeof process === 'object';
var ENVIRONMENT_IS_SHELL = typeof read === 'function';
#endif

#if ASSERTIONS
#if ASSERTIONS || USE_PTHREADS
#if !ENVIRONMENT_MAY_BE_NODE && !ENVIRONMENT_MAY_BE_SHELL
var ENVIRONMENT_IS_WEB = true
#elif ENVIRONMENT && !ENVIRONMENT.includes(',')
Expand All @@ -43,10 +43,8 @@ var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_SHELL;
var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_SHELL;
#else
var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE;
#else

#endif
#endif // ASSERTIONS
#endif // ASSERTIONS || USE_PTHREADS

#if ASSERTIONS && ENVIRONMENT_MAY_BE_NODE && ENVIRONMENT_MAY_BE_SHELL
if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
Expand Down
7 changes: 4 additions & 3 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4701,11 +4701,12 @@ def test_pthread_hello_thread(self, opts):
# Tests that a pthreads build of -s MINIMAL_RUNTIME=1 works well in different build modes
@parameterized({
'': ([],),
'O3': (['-O3'],)
'modularize': (['-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
'O3': (['-O3'],),
'O3_modularize': (['-O3', '-sMODULARIZE', '-sEXPORT_NAME=MyModule'],),
})
def test_minimal_runtime_hello_thread(self, opts):
for modularize in [[], ['-s', 'MODULARIZE', '-s', 'EXPORT_NAME=MyModule']]:
self.btest_exit(test_file('pthread/hello_thread.c'), expected='1', args=['-s', 'MINIMAL_RUNTIME', '-s', 'USE_PTHREADS'] + modularize + opts)
self.btest_exit(test_file('pthread/hello_thread.c'), expected='1', args=['--closure=1', '-sMINIMAL_RUNTIME', '-sUSE_PTHREADS'] + opts)

# Tests memory growth in pthreads mode, but still on the main thread.
@requires_threads
Expand Down
2 changes: 1 addition & 1 deletion tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ def add_to_path(dirname):
if settings.DYNCALLS:
CLOSURE_EXTERNS += [path_from_root('src/closure-externs/dyncall-externs.js')]

if settings.MINIMAL_RUNTIME and settings.USE_PTHREADS and not settings.MODULARIZE:
if settings.MINIMAL_RUNTIME and settings.USE_PTHREADS:
CLOSURE_EXTERNS += [path_from_root('src/minimal_runtime_worker_externs.js')]

args = ['--compilation_level', 'ADVANCED_OPTIMIZATIONS' if advanced else 'SIMPLE_OPTIMIZATIONS']
Expand Down