Skip to content

Allow unused destructors to be completely elided #20519

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
Oct 24, 2023
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
12 changes: 12 additions & 0 deletions test/other/test_unused_destructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdlib.h>

__attribute((destructor)) void dtor() {
printf("hello from dtor");
abort();
}

int main() {
printf("in main\n");
return 0;
}
5 changes: 5 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14048,3 +14048,8 @@ def test_emmalloc_in_addition(self, args):
emmalloc = path_from_root('system', 'lib', 'emmalloc.c')
self.run_process([EMCC, test_file('other/test_emmalloc_in_addition.c'), emmalloc] + args)
self.assertContained('success', self.run_js('a.out.js'))

def test_unused_destructor(self):
self.do_runf(test_file('other/test_unused_destructor.c'), emcc_args=['-flto', '-O2'])
# Verify that the string constant in the destructor is not included in the binary
self.assertNotIn(b'hello from dtor', read_binary('test_unused_destructor.wasm'))
9 changes: 9 additions & 0 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ def lld_flags_for_executable(external_symbols):
if settings.LINKABLE:
cmd.append('--export-dynamic')

if settings.LTO and not settings.EXIT_RUNTIME:
# The WebAssembly backend can generate new references to `__cxa_atexit` at
# LTO time. This `-u` flag forces the `__cxa_atexit` symbol to be
# included at LTO time. For other such symbols we exclude them from LTO
# and always build them as normal object files, but that would inhibit the
# LowerGlobalDtors optimization which allows destructors to be completely
# removed when __cxa_atexit is a no-op.
cmd.append('-u__cxa_atexit')

c_exports = [e for e in settings.EXPORTED_FUNCTIONS if is_c_symbol(e)]
# Strip the leading underscores
c_exports = [demangle_c_symbol_name(e) for e in c_exports]
Expand Down
3 changes: 0 additions & 3 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,6 @@ class libcompiler_rt(MTLibrary, SjLjLibrary):

class libnoexit(Library):
name = 'libnoexit'
# __cxa_atexit calls can be generated during LTO the implemenation cannot
# itself be LTO. See `get_libcall_files` below for more details.
force_object_files = True
src_dir = 'system/lib/libc'
src_files = ['atexit_dummy.c']

Expand Down