Skip to content

Commit f127025

Browse files
committed
Implement -ffast-math flag mapping to wasm-opt --fast-math
1 parent 9bc3ffa commit f127025

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/settings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ var ASSERTIONS = 1;
6666
// [link]
6767
var STACK_OVERFLOW_CHECK = 0;
6868

69+
// Enable fast math optimizations in wasm-opt when -ffast-math is passed.
70+
// This enables aggressive floating-point optimizations that may violate
71+
// IEEE 754 semantics but can improve performance.
72+
// [link]
73+
var FAST_MATH = 0;
74+
6975
// When STACK_OVERFLOW_CHECK is enabled we also check writes to address zero.
7076
// This can help detect NULL pointer usage. If you want to skip this extra
7177
// check (for example, if you want reads from the address zero to always return

tools/building.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,12 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, debug_info):
789789
# get the flags to pass into the very last binaryen tool invocation, that runs
790790
# the final set of optimizations
791791
def get_last_binaryen_opts():
792-
return [f'--optimize-level={settings.OPT_LEVEL}',
792+
opts = [f'--optimize-level={settings.OPT_LEVEL}',
793793
f'--shrink-level={settings.SHRINK_LEVEL}',
794794
'--optimize-stack-ir']
795+
if settings.FAST_MATH:
796+
opts.append('--fast-math')
797+
return opts
795798

796799

797800
# run binaryen's wasm-metadce to dce both js and wasm

tools/cmdline.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@ def consume_arg_file():
294294
settings.SHRINK_LEVEL = 0
295295
settings.DEBUG_LEVEL = max(settings.DEBUG_LEVEL, 1)
296296
elif requested_level == 'fast':
297-
# TODO(https://github.com/emscripten-core/emscripten/issues/21497):
298-
# If we ever map `-ffast-math` to `wasm-opt --fast-math` then
299-
# then we should enable that too here.
297+
# -Ofast typically includes -ffast-math semantics, so enable fast math optimizations
298+
settings.FAST_MATH = 1
300299
requested_level = 3
301300
settings.SHRINK_LEVEL = 0
302301
else:
@@ -547,6 +546,8 @@ def consume_arg_file():
547546
settings.WASM_EXCEPTIONS = 1
548547
elif arg == '-fignore-exceptions':
549548
settings.DISABLE_EXCEPTION_CATCHING = 1
549+
elif arg == '-ffast-math':
550+
settings.FAST_MATH = 1
550551
elif check_arg('--default-obj-ext'):
551552
exit_with_error('--default-obj-ext is no longer supported by emcc')
552553
elif arg.startswith('-fsanitize=cfi'):

0 commit comments

Comments
 (0)