Skip to content

Commit fcb4648

Browse files
Add --fast-math to binaryen passes when linking with -ffast-math (#25513)
This PR implements the mapping from the `-ffast-math` compiler flag to the `wasm-opt --fast-math` optimization flag. Fixes: #21497
1 parent 541483d commit fcb4648

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

src/settings_internal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ var ASYNCIFY_IMPORTS_EXCEPT_JS_LIBS = [];
260260

261261
var WARN_DEPRECATED = true;
262262

263+
// Enable fast math optimizations in wasm-opt when -ffast-math is passed.
264+
// This enables aggressive floating-point optimizations that may violate
265+
// IEEE 754 semantics but can improve performance.
266+
var FAST_MATH = 0;
267+
263268
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
264269
// those always when possible.
265270
// We currently set this to false for certain browser when large memory sizes

test/test_other.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15208,6 +15208,14 @@ def has_defined_function(file, func):
1520815208
self.assertIn('foo.cpp', out)
1520915209
self.assertIn('/emsdk/emscripten/system/lib/libc/musl/src/string/strcmp.c', out)
1521015210

15211+
def test_binaryen_fast_math(self):
15212+
# Use a simple input; contents don't matter for -v flag inspection
15213+
err = self.run_process([EMCC, test_file('hello_world.c'), '-v', '-O2', '-ffast-math'], stderr=PIPE).stderr
15214+
self.assertContained('--fast-math', err)
15215+
15216+
err_no_fast = self.run_process([EMCC, test_file('hello_world.c'), '-v', '-O2'], stderr=PIPE).stderr
15217+
self.assertNotContained('--fast-math', err_no_fast)
15218+
1521115219
def test_relocatable(self):
1521215220
# This setting is due for removal:
1521315221
# https://github.com/emscripten-core/emscripten/issues/25262

tools/cmdline.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,8 @@ def consume_arg_file():
301301
settings.SHRINK_LEVEL = 0
302302
settings.DEBUG_LEVEL = max(settings.DEBUG_LEVEL, 1)
303303
elif requested_level == 'fast':
304-
# TODO(https://github.com/emscripten-core/emscripten/issues/21497):
305-
# If we ever map `-ffast-math` to `wasm-opt --fast-math` then
306-
# then we should enable that too here.
304+
# -Ofast typically includes -ffast-math semantics
305+
settings.FAST_MATH = 1
307306
requested_level = 3
308307
settings.SHRINK_LEVEL = 0
309308
else:
@@ -552,6 +551,8 @@ def consume_arg_file():
552551
settings.WASM_EXCEPTIONS = 1
553552
elif arg == '-fignore-exceptions':
554553
settings.DISABLE_EXCEPTION_CATCHING = 1
554+
elif arg == '-ffast-math':
555+
settings.FAST_MATH = 1
555556
elif check_arg('--default-obj-ext'):
556557
exit_with_error('--default-obj-ext is no longer supported by emcc')
557558
elif arg.startswith('-fsanitize=cfi'):

tools/link.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ def get_binaryen_passes():
392392
passes += ['--pass-arg=post-emscripten-side-module']
393393
if optimizing:
394394
passes += [building.opt_level_to_str(settings.OPT_LEVEL, settings.SHRINK_LEVEL)]
395+
if settings.FAST_MATH:
396+
passes += ['--fast-math']
395397
# when optimizing, use the fact that low memory is never used (1024 is a
396398
# hardcoded value in the binaryen pass). we also cannot do it when the stack
397399
# is first, as then the stack is in the low memory that should be unused.

0 commit comments

Comments
 (0)