Skip to content
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
24 changes: 24 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`<optimization>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 `<optimization>` 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 `<linkflags>`, so a program's
own `-O<n>` 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
Expand Down Expand Up @@ -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 `<optimization>` 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`).
Expand Down
11 changes: 11 additions & 0 deletions CODING-GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<optimization>` 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 `<linkflags>`, whose `-O<n>` 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
`<target-os>wasi` only after the toolsets have loaded.** wasm32-wasip2 and
Expand Down
20 changes: 20 additions & 0 deletions Jamroot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,6 +53,25 @@ if ! wasi in [ feature.values <target-os> ]
type.set-generated-target-suffix EXE : <target-os>wasi : wasm ;
# end::wasi-os[]

# b2 5.5.3's emscripten toolset compiles at the level <optimization> names,
# as gcc.jam maps it (-O0, -O3, -Os, -O1, -Og), and maps <optimization> 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 <linkflags>, so a program that links at another level
# with <linkflags>-O<n> still gets it.
# tag::emscripten-link-optimization[]
toolset.flags emscripten.link OPTIONS <optimization>off : -O0 : unchecked ;
toolset.flags emscripten.link OPTIONS <optimization>speed : -O3 : unchecked ;
toolset.flags emscripten.link OPTIONS <optimization>space : -Os : unchecked ;
toolset.flags emscripten.link OPTIONS <optimization>minimal : -O1 : unchecked ;
toolset.flags emscripten.link OPTIONS <optimization>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).
Expand Down
1 change: 1 addition & 0 deletions doc/releases/0.1.0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 40 additions & 4 deletions tools/test/emscripten_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'(?<!\S)-O[0-3szg](?!\S)')


def compile_line(result: subprocess.CompletedProcess, source: str) -> 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 <optimization> 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 <optimization>, before a
# program's own <linkflags>, 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
Expand Down Expand Up @@ -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,
Expand Down
Loading