From 652b51ecdd46efacef29070abdcaa764ff283e5f Mon Sep 17 00:00:00 2001 From: R Date: Sun, 15 Nov 2026 04:21:17 -0300 Subject: [PATCH] build: link emscripten programs at their compile's level of optimization b2 5.5.3's emscripten toolset compiles at the level names (-O3 in release, as gcc.jam maps it) and gives its link no level, which em++ runs at -O0: a release program was linked without optimization, so Binaryen's wasm-opt never ran on the whole module and Asyncify instrumented it unoptimized, saving whole frames at each suspension. The Jamroot now maps to em++'s link options with toolset.flags on emscripten.link: off -O0, speed -O3, space -Os, minimal -O1, debug -Og. They are the toolset's own options, before , so a program that asks for another level still gets it, and they reach every program b2 links with em++, whatever project declares it. A debug build links as before (its outputs are byte-identical). Measured with Emscripten 6.0.11, asio's release timers test went from 735,400 bytes of .wasm to 489,216, and the deepest suspension of its asyncify_stack test from 2,900 bytes to 388 (1,900 to 304 without exceptions), the same as its optimized_link build. tools/test/emscripten_test.py checks the level of each value against the compile's, and that a program's own linkflags come last. AGENTS.md, CODING-GUIDELINES.md and the 0.1.0 release notes say so. --- AGENTS.md | 24 +++++++++++++++++++ CODING-GUIDELINES.md | 11 +++++++++ Jamroot | 20 ++++++++++++++++ doc/releases/0.1.0.adoc | 1 + tools/test/emscripten_test.py | 44 +++++++++++++++++++++++++++++++---- 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 2f0a0d8e..5906959e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -905,6 +905,26 @@ testing.launcher on emscripten", chapter 6). Every program that node runs without it, node gives it an empty file system of Emscripten's own, and a test reads its input file as empty. No other program gets it. +**A program on emscripten links at its compile's level of optimization** +(the Jamroot's region `emscripten-link-optimization`): `-O0` for +`off`, `-O3` for `speed`, `-Os` for `space`, `-O1` for +`minimal` and `-Og` for `debug`, so `-O3` in a release build and `-O0` in a +debug one. b2 5.5.3's toolset gives `` to the compile alone, +and `em++` given no level links at `-O0` (chapter 6), so a release program +was linked without optimization: Binaryen's wasm-opt never ran on it, and +Asyncify instrumented it unoptimized, saving whole frames at each +suspension. Measured with Emscripten 6.0.11, asio's release test +`timers` went from 735,400 bytes of `.wasm` to 489,216, and the deepest +suspension of its `asyncify_stack` test from 2,900 bytes to 388. The level +is among the toolset's own options, before ``, so a program's +own `-O` there comes last, and `em++` keeps it. A link at `-O1` or above +also turns Emscripten's `ASSERTIONS` off, and a release link at `-O3` +minifies the JavaScript, drops the runtime functions nothing calls and, +outside JSPI, gives the module's exports short names: an abort prints +`Aborted()` without the reason assertions add, and a page or a driver finds +only what the program uses. `tools/test/emscripten_test.py` pins each level +and its order. + **The suspension variant on emscripten.** A program on emscripten that suspends, waiting inside a call for the browser's event loop, is built for one of two variants, the feature `webcpp-suspend` (the region `suspend` of @@ -2054,6 +2074,10 @@ Each of these was measured; each has cost time. refuses it. - **With `--out-xml`, b2 exits 0 even when a test fails;** the report's exit status is the verdict, and the report needs `--dump-tests`. +- **b2 5.5.3's emscripten toolset links at no level of optimization:** it + compiles at the one `` names (`-O3` in release), as gcc.jam + maps it, and gives its link none, which `em++` runs at `-O0`. The Jamroot + gives the link the compile's level (chapter 4, Targets). - **b2 5.5.3's `--command-database` writes nothing,** so the compilation database comes from `tools/lint/compile_commands.py`, which reads b2's dry run (`b2 -n -a`). diff --git a/CODING-GUIDELINES.md b/CODING-GUIDELINES.md index 3e948ae8..85fc6723 100644 --- a/CODING-GUIDELINES.md +++ b/CODING-GUIDELINES.md @@ -1246,6 +1246,17 @@ site's own decisions follow in the next sections. Browsers do not all run memory64, pthreads need cross-origin isolation in a page, and Emscripten's shared libraries are experimental. Source: `AGENTS.md` (webcpp). +- **On emscripten a program links at the level of optimization it compiles + at: `-O3` in a release build, `-O0` in a debug one.** b2 5.5.3's emscripten + toolset gives `` to the compile alone, and `em++` given no + level links at `-O0`: a release program was linked without optimization, + Binaryen's wasm-opt never ran on it, and Asyncify instrumented it + unoptimized (asio's deepest suspension wrote 2,900 bytes, against 388 + linked with `-O3`). The Jamroot gives the link its compile's level, before + a program's own ``, whose `-O` still wins. A release link + also turns Emscripten's `ASSERTIONS` off and drops the runtime JavaScript + the program never calls (`AGENTS.md` chapter 4). + Source: `AGENTS.md` (webcpp); `Jamroot` (webcpp). - **wasip2 and wasip3 are b2's `clang` registered against wasi-sdk (`clang-wasip2`, `clang-wasip3`), linked statically, and the Jamroot adds `wasi` only after the toolsets have loaded.** wasm32-wasip2 and diff --git a/Jamroot b/Jamroot index 7862cac8..8a36efd8 100644 --- a/Jamroot +++ b/Jamroot @@ -18,6 +18,7 @@ import numbers ; import os ; import package ; import path ; +import toolset ; import type ; # The rules the libraries' test and example Jamfiles declare their programs @@ -52,6 +53,25 @@ if ! wasi in [ feature.values ] type.set-generated-target-suffix EXE : wasi : wasm ; # end::wasi-os[] +# b2 5.5.3's emscripten toolset compiles at the level names, +# as gcc.jam maps it (-O0, -O3, -Os, -O1, -Og), and maps to +# no option of its link, which em++ then runs at its default, -O0: a +# release program, compiled with -O3, was linked without optimization, so +# that Binaryen's wasm-opt never ran on the whole module and Asyncify +# instrumented it unoptimized, saving whole frames at each suspension. +# These flags give the link its compile's level, on every program b2 links +# with em++, whatever project declares it; off names em++'s default, so +# that a debug build links as before. They are the toolset's own options, +# which come before , so a program that links at another level +# with -O still gets it. +# tag::emscripten-link-optimization[] +toolset.flags emscripten.link OPTIONS off : -O0 : unchecked ; +toolset.flags emscripten.link OPTIONS speed : -O3 : unchecked ; +toolset.flags emscripten.link OPTIONS space : -Os : unchecked ; +toolset.flags emscripten.link OPTIONS minimal : -O1 : unchecked ; +toolset.flags emscripten.link OPTIONS debug : -Og : unchecked ; +# end::emscripten-link-optimization[] + # /boost is the `using boost` of user-config.jam, else the Boost tree that # BOOST_ROOT names, else Boost on the compiler's default include path (b2's # contrib/boost.jam). diff --git a/doc/releases/0.1.0.adoc b/doc/releases/0.1.0.adoc index 4c1d1ed5..005503fb 100644 --- a/doc/releases/0.1.0.adoc +++ b/doc/releases/0.1.0.adoc @@ -58,6 +58,7 @@ None. [#webcpp-0-1-0-tools-and-build] == Tools and build +* Fixed emscripten release programs linking unoptimized; they now link at `-O3`. * Fixed clean CMake consumer audits rejecting system libraries and phony dependency rules. * Changed CI to consume verified prepared environments without rebuilding images in PR jobs. * Added verified archive caches and isolated container availability from unrelated CI lanes. diff --git a/tools/test/emscripten_test.py b/tools/test/emscripten_test.py index db4cfdcd..1cfc1ca0 100644 --- a/tools/test/emscripten_test.py +++ b/tools/test/emscripten_test.py @@ -9,10 +9,12 @@ examples run under node, which b2's emscripten toolset runs itself, and a testing.launcher is refused there; a program that node runs reads the host's files and standard input (-sNODERAWFS=1), and no other program gets the flag; every program is wasm32, whatever address model is asked for; a -failure reaches the report; a program of webcpp.link is linked and never run, and fails on a symbol -nothing defines; a test of webcpp.drive runs its program through a script of node's on each of its -targets, in an own lane, a driver or a program that hangs fails it within its bound and leaves no -process behind, and one that no own lane names is refused; and a native build needs no Emscripten. +program links at the level of optimization it compiles at, -O3 in a release build, which b2's +toolset leaves to em++'s default, -O0, and before its own linkflags; a failure reaches the report; +a program of webcpp.link is linked and never run, and fails on a symbol nothing defines; a test of +webcpp.drive runs its program through a script of node's on each of its targets, in an own lane, a +driver or a program that hangs fails it within its bound and leaves no process behind, and one that +no own lane names is refused; and a native build needs no Emscripten. The suspension variant, jspi by default and asyncify on request, builds with the exceptions each needs, asyncify in a directory of its own, and natively adds nothing. Every program node runs links the test support (/webcpp//node-test), whose fixture library node_test_demo pins each case's exact @@ -191,6 +193,39 @@ def test_programs_are_wasm32(root): assert not directories, directories +# An option of em++'s that sets the level of optimization, a word of its own: -O0 to -O3, -Os, +# -Oz and -Og. +LEVEL = re.compile(r'(? str: + """The command line b2 (-d+2) compiled the source named source with.""" + compiled = re.compile(rf'-c -o "[^"]*" "[^"]*/{re.escape(source)}"$') + lines = [line for line in result.stdout.splitlines() if compiled.search(line)] + assert len(lines) == 1, (source, lines, result.stdout[-4000:]) + return lines[0] + + +def test_a_link_is_optimized_as_its_compile(root): + # b2 5.5.3's emscripten toolset compiles at the level names and links with + # none, which em++ takes for -O0: a release program, compiled with -O3, was linked without + # optimization, so binaryen never optimized it, and Asyncify instrumented it unoptimized. The + # Jamroot gives the link its compile's level, for every value of , before a + # program's own , so that a level they name comes last, which em++ keeps. + for request, level, linked_levels in ( + (('variant=debug',), '-O0', ['-O0']), (('variant=release',), '-O3', ['-O3']), + (('variant=release', 'optimization=space'), '-Os', ['-Os']), + (('optimization=minimal',), '-O1', ['-O1']), (('optimization=debug',), '-Og', ['-Og']), + (('variant=release', 'linkflags=-Oz'), '-O3', ['-O3', '-Oz'])): + result = harness.run_b2(root, '-a', '-n', '-d+2', *EMSCRIPTEN, *request, + 'libs/browser_demo/test//pointer') + harness.expect(result, True) + compiled = compile_line(result, 'pointer_test.cpp') + assert LEVEL.findall(compiled) == [level], (request, compiled) + linked = link_line(root, result, 'pointer') + assert LEVEL.findall(linked) == linked_levels, (request, linked) + + def test_suspend_variants_build_apart(root): # jspi, the default, compiles with WebAssembly exceptions and adds no directory; asyncify # composes JavaScript exceptions, which Asyncify needs (an Asyncify program built with @@ -606,6 +641,7 @@ def run(root): test_a_launcher_is_refused_on_emscripten, test_input_file_is_read_from_the_host, test_programs_are_wasm32, + test_a_link_is_optimized_as_its_compile, test_suspend_variants_build_apart, test_a_native_build_ignores_the_variant, test_a_test_ends_as_its_main_ends,