Skip to content

Commit 652b51e

Browse files
author
R
committed
build: link emscripten programs at their compile's level of optimization
b2 5.5.3's emscripten toolset compiles at the level <optimization> 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 <optimization> 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 <linkflags>, 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.
1 parent f085d79 commit 652b51e

5 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎AGENTS.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,26 @@ testing.launcher on emscripten", chapter 6). Every program that node runs
905905
without it, node gives it an empty file system of Emscripten's own, and a
906906
test reads its input file as empty. No other program gets it.
907907

908+
**A program on emscripten links at its compile's level of optimization**
909+
(the Jamroot's region `emscripten-link-optimization`): `-O0` for
910+
`<optimization>off`, `-O3` for `speed`, `-Os` for `space`, `-O1` for
911+
`minimal` and `-Og` for `debug`, so `-O3` in a release build and `-O0` in a
912+
debug one. b2 5.5.3's toolset gives `<optimization>` to the compile alone,
913+
and `em++` given no level links at `-O0` (chapter 6), so a release program
914+
was linked without optimization: Binaryen's wasm-opt never ran on it, and
915+
Asyncify instrumented it unoptimized, saving whole frames at each
916+
suspension. Measured with Emscripten 6.0.11, asio's release test
917+
`timers` went from 735,400 bytes of `.wasm` to 489,216, and the deepest
918+
suspension of its `asyncify_stack` test from 2,900 bytes to 388. The level
919+
is among the toolset's own options, before `<linkflags>`, so a program's
920+
own `-O<n>` there comes last, and `em++` keeps it. A link at `-O1` or above
921+
also turns Emscripten's `ASSERTIONS` off, and a release link at `-O3`
922+
minifies the JavaScript, drops the runtime functions nothing calls and,
923+
outside JSPI, gives the module's exports short names: an abort prints
924+
`Aborted()` without the reason assertions add, and a page or a driver finds
925+
only what the program uses. `tools/test/emscripten_test.py` pins each level
926+
and its order.
927+
908928
**The suspension variant on emscripten.** A program on emscripten that
909929
suspends, waiting inside a call for the browser's event loop, is built for
910930
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.
20542074
refuses it.
20552075
- **With `--out-xml`, b2 exits 0 even when a test fails;** the report's exit
20562076
status is the verdict, and the report needs `--dump-tests`.
2077+
- **b2 5.5.3's emscripten toolset links at no level of optimization:** it
2078+
compiles at the one `<optimization>` names (`-O3` in release), as gcc.jam
2079+
maps it, and gives its link none, which `em++` runs at `-O0`. The Jamroot
2080+
gives the link the compile's level (chapter 4, Targets).
20572081
- **b2 5.5.3's `--command-database` writes nothing,** so the compilation
20582082
database comes from `tools/lint/compile_commands.py`, which reads b2's dry
20592083
run (`b2 -n -a`).

‎CODING-GUIDELINES.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,17 @@ site's own decisions follow in the next sections.
12461246
Browsers do not all run memory64, pthreads need cross-origin isolation in a
12471247
page, and Emscripten's shared libraries are experimental.
12481248
Source: `AGENTS.md` (webcpp).
1249+
- **On emscripten a program links at the level of optimization it compiles
1250+
at: `-O3` in a release build, `-O0` in a debug one.** b2 5.5.3's emscripten
1251+
toolset gives `<optimization>` to the compile alone, and `em++` given no
1252+
level links at `-O0`: a release program was linked without optimization,
1253+
Binaryen's wasm-opt never ran on it, and Asyncify instrumented it
1254+
unoptimized (asio's deepest suspension wrote 2,900 bytes, against 388
1255+
linked with `-O3`). The Jamroot gives the link its compile's level, before
1256+
a program's own `<linkflags>`, whose `-O<n>` still wins. A release link
1257+
also turns Emscripten's `ASSERTIONS` off and drops the runtime JavaScript
1258+
the program never calls (`AGENTS.md` chapter 4).
1259+
Source: `AGENTS.md` (webcpp); `Jamroot` (webcpp).
12491260
- **wasip2 and wasip3 are b2's `clang` registered against wasi-sdk
12501261
(`clang-wasip2`, `clang-wasip3`), linked statically, and the Jamroot adds
12511262
`<target-os>wasi` only after the toolsets have loaded.** wasm32-wasip2 and

‎Jamroot‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import numbers ;
1818
import os ;
1919
import package ;
2020
import path ;
21+
import toolset ;
2122
import type ;
2223

2324
# The rules the libraries' test and example Jamfiles declare their programs
@@ -52,6 +53,25 @@ if ! wasi in [ feature.values <target-os> ]
5253
type.set-generated-target-suffix EXE : <target-os>wasi : wasm ;
5354
# end::wasi-os[]
5455

56+
# b2 5.5.3's emscripten toolset compiles at the level <optimization> names,
57+
# as gcc.jam maps it (-O0, -O3, -Os, -O1, -Og), and maps <optimization> to
58+
# no option of its link, which em++ then runs at its default, -O0: a
59+
# release program, compiled with -O3, was linked without optimization, so
60+
# that Binaryen's wasm-opt never ran on the whole module and Asyncify
61+
# instrumented it unoptimized, saving whole frames at each suspension.
62+
# These flags give the link its compile's level, on every program b2 links
63+
# with em++, whatever project declares it; off names em++'s default, so
64+
# that a debug build links as before. They are the toolset's own options,
65+
# which come before <linkflags>, so a program that links at another level
66+
# with <linkflags>-O<n> still gets it.
67+
# tag::emscripten-link-optimization[]
68+
toolset.flags emscripten.link OPTIONS <optimization>off : -O0 : unchecked ;
69+
toolset.flags emscripten.link OPTIONS <optimization>speed : -O3 : unchecked ;
70+
toolset.flags emscripten.link OPTIONS <optimization>space : -Os : unchecked ;
71+
toolset.flags emscripten.link OPTIONS <optimization>minimal : -O1 : unchecked ;
72+
toolset.flags emscripten.link OPTIONS <optimization>debug : -Og : unchecked ;
73+
# end::emscripten-link-optimization[]
74+
5575
# /boost is the `using boost` of user-config.jam, else the Boost tree that
5676
# BOOST_ROOT names, else Boost on the compiler's default include path (b2's
5777
# contrib/boost.jam).

‎doc/releases/0.1.0.adoc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ None.
5858
[#webcpp-0-1-0-tools-and-build]
5959
== Tools and build
6060

61+
* Fixed emscripten release programs linking unoptimized; they now link at `-O3`.
6162
* Fixed clean CMake consumer audits rejecting system libraries and phony dependency rules.
6263
* Changed CI to consume verified prepared environments without rebuilding images in PR jobs.
6364
* Added verified archive caches and isolated container availability from unrelated CI lanes.

‎tools/test/emscripten_test.py‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
examples run under node, which b2's emscripten toolset runs itself, and a testing.launcher is
1010
refused there; a program that node runs reads the host's files and standard input (-sNODERAWFS=1),
1111
and no other program gets the flag; every program is wasm32, whatever address model is asked for; a
12-
failure reaches the report; a program of webcpp.link is linked and never run, and fails on a symbol
13-
nothing defines; a test of webcpp.drive runs its program through a script of node's on each of its
14-
targets, in an own lane, a driver or a program that hangs fails it within its bound and leaves no
15-
process behind, and one that no own lane names is refused; and a native build needs no Emscripten.
12+
program links at the level of optimization it compiles at, -O3 in a release build, which b2's
13+
toolset leaves to em++'s default, -O0, and before its own linkflags; a failure reaches the report;
14+
a program of webcpp.link is linked and never run, and fails on a symbol nothing defines; a test of
15+
webcpp.drive runs its program through a script of node's on each of its targets, in an own lane, a
16+
driver or a program that hangs fails it within its bound and leaves no process behind, and one that
17+
no own lane names is refused; and a native build needs no Emscripten.
1618
The suspension variant, jspi by default and asyncify on request, builds with the exceptions each
1719
needs, asyncify in a directory of its own, and natively adds nothing. Every program node runs links
1820
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):
191193
assert not directories, directories
192194

193195

196+
# An option of em++'s that sets the level of optimization, a word of its own: -O0 to -O3, -Os,
197+
# -Oz and -Og.
198+
LEVEL = re.compile(r'(?<!\S)-O[0-3szg](?!\S)')
199+
200+
201+
def compile_line(result: subprocess.CompletedProcess, source: str) -> str:
202+
"""The command line b2 (-d+2) compiled the source named source with."""
203+
compiled = re.compile(rf'-c -o "[^"]*" "[^"]*/{re.escape(source)}"$')
204+
lines = [line for line in result.stdout.splitlines() if compiled.search(line)]
205+
assert len(lines) == 1, (source, lines, result.stdout[-4000:])
206+
return lines[0]
207+
208+
209+
def test_a_link_is_optimized_as_its_compile(root):
210+
# b2 5.5.3's emscripten toolset compiles at the level <optimization> names and links with
211+
# none, which em++ takes for -O0: a release program, compiled with -O3, was linked without
212+
# optimization, so binaryen never optimized it, and Asyncify instrumented it unoptimized. The
213+
# Jamroot gives the link its compile's level, for every value of <optimization>, before a
214+
# program's own <linkflags>, so that a level they name comes last, which em++ keeps.
215+
for request, level, linked_levels in (
216+
(('variant=debug',), '-O0', ['-O0']), (('variant=release',), '-O3', ['-O3']),
217+
(('variant=release', 'optimization=space'), '-Os', ['-Os']),
218+
(('optimization=minimal',), '-O1', ['-O1']), (('optimization=debug',), '-Og', ['-Og']),
219+
(('variant=release', 'linkflags=-Oz'), '-O3', ['-O3', '-Oz'])):
220+
result = harness.run_b2(root, '-a', '-n', '-d+2', *EMSCRIPTEN, *request,
221+
'libs/browser_demo/test//pointer')
222+
harness.expect(result, True)
223+
compiled = compile_line(result, 'pointer_test.cpp')
224+
assert LEVEL.findall(compiled) == [level], (request, compiled)
225+
linked = link_line(root, result, 'pointer')
226+
assert LEVEL.findall(linked) == linked_levels, (request, linked)
227+
228+
194229
def test_suspend_variants_build_apart(root):
195230
# jspi, the default, compiles with WebAssembly exceptions and adds no directory; asyncify
196231
# composes JavaScript exceptions, which Asyncify needs (an Asyncify program built with
@@ -606,6 +641,7 @@ def run(root):
606641
test_a_launcher_is_refused_on_emscripten,
607642
test_input_file_is_read_from_the_host,
608643
test_programs_are_wasm32,
644+
test_a_link_is_optimized_as_its_compile,
609645
test_suspend_variants_build_apart,
610646
test_a_native_build_ignores_the_variant,
611647
test_a_test_ends_as_its_main_ends,

0 commit comments

Comments
 (0)