@@ -357,45 +357,57 @@ 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 explicitly disabled on the command line,
368+ # or when targeting an VM/engine that does not support the feature.
369+
370+ # List of [<feature_name>, <lowering_flag>] pairs.
371+ features = [
372+ [feature_matrix .Feature .SIGN_EXT , '--signext-lowering' ],
373+ [feature_matrix .Feature .NON_TRAPPING_FPTOINT , '--llvm-nontrapping-fptoint-lowering' ],
374+ [feature_matrix .Feature .BULK_MEMORY , '--llvm-memory-copy-fill-lowering' ],
375+ ]
376+
377+ for feature , lowering_flag in features :
378+ if not feature_matrix .caniuse (feature ):
379+ logger .debug (f'lowering { feature .name } feature due to incompatible target browser engines' )
380+ passes .append (lowering_flag )
381+
382+ return passes
383+
384+
385+ def get_binaryen_passes (options ):
386+ passes = get_binaryen_lowering_passes ()
362387 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' ]
388+
371389 # safe heap must run before post-emscripten, so post-emscripten can apply the sbrk ptr
372390 if settings .SAFE_HEAP :
373391 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' ]
386392 if optimizing :
393+ # wasm-emscripten-finalize will strip the features section for us
394+ # automatically, but if we did not modify the wasm then we didn't run it,
395+ # and in an optimized build we strip it manually here. (note that in an
396+ # unoptimized build we might end up with the features section, if we neither
397+ # optimize nor run wasm-emscripten-finalize, but a few extra bytes in the
398+ # binary don't matter in an unoptimized build)
399+ passes += ['--strip-target-features' ]
387400 passes += ['--post-emscripten' ]
388401 if settings .SIDE_MODULE :
389402 passes += ['--pass-arg=post-emscripten-side-module' ]
390- if optimizing :
391403 passes += [building .opt_level_to_str (settings .OPT_LEVEL , settings .SHRINK_LEVEL )]
404+ # when optimizing, use the fact that low memory is never used (1024 is a
405+ # hardcoded value in the binaryen pass). we also cannot do it when the stack
406+ # is first, as then the stack is in the low memory that should be unused.
407+ if settings .GLOBAL_BASE >= 1024 and not settings .STACK_FIRST :
408+ passes += ['--low-memory-unused' ]
392409 if options .fast_math :
393410 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' ]
399411 if settings .AUTODEBUG :
400412 # adding '--flatten' here may make these even more effective
401413 passes += ['--instrument-locals' ]
0 commit comments