Skip to content

Commit f98e77e

Browse files
committed
Separate out binaryen lowering passes. NFC
1 parent 094e5bd commit f98e77e

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

tools/link.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -357,45 +357,56 @@ def should_run_binaryen_optimizer():
357357
return settings.OPT_LEVEL >= 2
358358

359359

360-
def get_binaryen_passes(options):
360+
def get_binaryen_lowering_passes():
361361
passes = []
362+
363+
# The following features are all enabled in llvm by default and therefore
364+
# enabled in the emscripten system libraries. This means that we need to
365+
# lower them away using binaryen passes, if they are not enabled in the
366+
# feature matrix.
367+
# This can happen if the feature is expliictly disabled on the command line,
368+
# or when targetig an VM/engine that does not support the feature.
369+
370+
features = [
371+
[feature_matrix.Feature.SIGN_EXT, '--signext-lowering'],
372+
[feature_matrix.Feature.NON_TRAPPING_FPTOINT, '--llvm-nontrapping-fptoint-lowering'],
373+
[feature_matrix.Feature.BULK_MEMORY, '--llvm-memory-copy-fill-lowering'],
374+
]
375+
376+
for feature, flag in features:
377+
if not feature_matrix.caniuse(feature):
378+
logger.debug(f'lowering {feature.name} feature due to incompatible target browser engines')
379+
passes.append(flag)
380+
381+
return passes
382+
383+
384+
def get_binaryen_passes(options):
385+
passes = get_binaryen_lowering_passes()
362386
optimizing = should_run_binaryen_optimizer()
363-
# wasm-emscripten-finalize will strip the features section for us
364-
# automatically, but if we did not modify the wasm then we didn't run it,
365-
# and in an optimized build we strip it manually here. (note that in an
366-
# unoptimized build we might end up with the features section, if we neither
367-
# optimize nor run wasm-emscripten-finalize, but a few extra bytes in the
368-
# binary don't matter in an unoptimized build)
369-
if optimizing:
370-
passes += ['--strip-target-features']
387+
371388
# safe heap must run before post-emscripten, so post-emscripten can apply the sbrk ptr
372389
if settings.SAFE_HEAP:
373390
passes += ['--safe-heap']
374-
# sign-ext is enabled by default by llvm. If the target browser settings don't support
375-
# this we lower it away here using a binaryen pass.
376-
if not feature_matrix.caniuse(feature_matrix.Feature.SIGN_EXT):
377-
logger.debug('lowering sign-ext feature due to incompatible target browser engines')
378-
passes += ['--signext-lowering']
379-
# nontrapping-fp is enabled by default in llvm. Lower it away if requested.
380-
if not feature_matrix.caniuse(feature_matrix.Feature.NON_TRAPPING_FPTOINT):
381-
logger.debug('lowering nontrapping-fp feature due to incompatible target browser engines')
382-
passes += ['--llvm-nontrapping-fptoint-lowering']
383-
if not feature_matrix.caniuse(feature_matrix.Feature.BULK_MEMORY):
384-
logger.debug('lowering bulk-memory feature due to incompatible target browser engines')
385-
passes += ['--llvm-memory-copy-fill-lowering']
386391
if optimizing:
392+
# wasm-emscripten-finalize will strip the features section for us
393+
# automatically, but if we did not modify the wasm then we didn't run it,
394+
# and in an optimized build we strip it manually here. (note that in an
395+
# unoptimized build we might end up with the features section, if we neither
396+
# optimize nor run wasm-emscripten-finalize, but a few extra bytes in the
397+
# binary don't matter in an unoptimized build)
398+
passes += ['--strip-target-features']
387399
passes += ['--post-emscripten']
388400
if settings.SIDE_MODULE:
389401
passes += ['--pass-arg=post-emscripten-side-module']
390-
if optimizing:
391402
passes += [building.opt_level_to_str(settings.OPT_LEVEL, settings.SHRINK_LEVEL)]
403+
# when optimizing, use the fact that low memory is never used (1024 is a
404+
# hardcoded value in the binaryen pass). we also cannot do it when the stack
405+
# is first, as then the stack is in the low memory that should be unused.
406+
if settings.GLOBAL_BASE >= 1024 and not settings.STACK_FIRST:
407+
passes += ['--low-memory-unused']
392408
if options.fast_math:
393409
passes += ['--fast-math']
394-
# when optimizing, use the fact that low memory is never used (1024 is a
395-
# hardcoded value in the binaryen pass). we also cannot do it when the stack
396-
# is first, as then the stack is in the low memory that should be unused.
397-
if optimizing and settings.GLOBAL_BASE >= 1024 and not settings.STACK_FIRST:
398-
passes += ['--low-memory-unused']
399410
if settings.AUTODEBUG:
400411
# adding '--flatten' here may make these even more effective
401412
passes += ['--instrument-locals']

0 commit comments

Comments
 (0)