diff options
author | Derek Schuff <dschuff@chromium.org> | 2024-11-26 14:11:06 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 14:11:06 -0800 |
commit | acb12584c277d9bfb0b1c78e65101fa54e9a7361 (patch) | |
tree | 310ab60622771a7d3092c4026e7c1178fb004c3c | |
parent | cc97853b5f03e8c8441159e68ecb2262beee166f (diff) | |
download | binaryen-acb12584c277d9bfb0b1c78e65101fa54e9a7361.tar.gz binaryen-acb12584c277d9bfb0b1c78e65101fa54e9a7361.tar.bz2 binaryen-acb12584c277d9bfb0b1c78e65101fa54e9a7361.zip |
[wasm2js] Run LLVM nontrapping fptoint lowering when running for emscripten (#7116)
Lower away saturating fptoint operations when we know we are using
emscripten.
-rw-r--r-- | scripts/test/wasm2js.py | 9 | ||||
-rw-r--r-- | src/wasm2js.h | 3 | ||||
-rw-r--r-- | test/wasm2js/conversions-emscripten-modified.2asm.js | 289 | ||||
-rw-r--r-- | test/wasm2js/conversions-emscripten-modified.2asm.js.opt | 121 | ||||
-rw-r--r-- | test/wasm2js/conversions-emscripten-modified.wast | 10 |
5 files changed, 428 insertions, 4 deletions
diff --git a/scripts/test/wasm2js.py b/scripts/test/wasm2js.py index 1f6ca4f8a..224dc01cb 100644 --- a/scripts/test/wasm2js.py +++ b/scripts/test/wasm2js.py @@ -162,10 +162,11 @@ def update_wasm2js_tests(): if not wasm.endswith('.wast'): continue - if os.path.basename(wasm) in wasm2js_skipped_tests: + basename = os.path.basename(wasm) + if basename in wasm2js_skipped_tests: continue - asm = os.path.basename(wasm).replace('.wast', '.2asm.js') + asm = basename.replace('.wast', '.2asm.js') expected_file = os.path.join(shared.get_test_dir('wasm2js'), asm) if opt: expected_file += '.opt' @@ -192,9 +193,9 @@ def update_wasm2js_tests(): '--disable-exception-handling'] if opt: cmd += ['-O'] - if 'emscripten' in wasm: + if 'emscripten' in basename: cmd += ['--emscripten'] - if 'deterministic' in t: + if 'deterministic' in basename: cmd += ['--deterministic'] out = support.run_command(cmd) all_out.append(out) diff --git a/src/wasm2js.h b/src/wasm2js.h index f81908948..7ed089448 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -361,6 +361,9 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { // First, do the lowering to a JS-friendly subset. { PassRunner runner(wasm, options); + if (flags.emscripten) { + runner.add("llvm-nontrapping-fptoint-lowering"); + } // TODO: only legalize if necessary - emscripten would already do so, and // likely other toolchains. but spec test suite needs that. runner.add("legalize-js-interface"); diff --git a/test/wasm2js/conversions-emscripten-modified.2asm.js b/test/wasm2js/conversions-emscripten-modified.2asm.js new file mode 100644 index 000000000..0bb42c142 --- /dev/null +++ b/test/wasm2js/conversions-emscripten-modified.2asm.js @@ -0,0 +1,289 @@ +function instantiate(info) { +function asmFunc(imports) { + var Math_imul = Math.imul; + var Math_fround = Math.fround; + var Math_abs = Math.abs; + var Math_clz32 = Math.clz32; + var Math_min = Math.min; + var Math_max = Math.max; + var Math_floor = Math.floor; + var Math_ceil = Math.ceil; + var Math_trunc = Math.trunc; + var Math_sqrt = Math.sqrt; + var env = imports.env; + var setTempRet0 = env.setTempRet0; + var i64toi32_i32$HIGH_BITS = 0; + // EMSCRIPTEN_START_FUNCS +; + function $0(x) { + x = Math_fround(x); + var $1_1 = Math_fround(0), $8 = 0; + $1_1 = x; + if (Math_fround(Math_abs($1_1)) < Math_fround(2147483648.0)) { + $8 = ~~$1_1 + } else { + $8 = -2147483648 + } + return $8 | 0; + } + + function $1(x) { + x = Math_fround(x); + var $1_1 = Math_fround(0), $10 = 0; + $1_1 = x; + if ($1_1 < Math_fround(4294967296.0) & $1_1 >= Math_fround(0.0) | 0) { + $10 = ~~$1_1 >>> 0 + } else { + $10 = 0 + } + return $10 | 0; + } + + function $2(x) { + x = +x; + var $1_1 = 0.0, $8 = 0; + $1_1 = x; + if (Math_abs($1_1) < 2147483647.0) { + $8 = ~~$1_1 + } else { + $8 = -2147483648 + } + return $8 | 0; + } + + function $3(x) { + x = +x; + var $1_1 = 0.0, $10 = 0; + $1_1 = x; + if ($1_1 < 4294967295.0 & $1_1 >= 0.0 | 0) { + $10 = ~~$1_1 >>> 0 + } else { + $10 = 0 + } + return $10 | 0; + } + + function $4(x) { + x = Math_fround(x); + var i64toi32_i32$0 = Math_fround(0), i64toi32_i32$1 = 0, $1_1 = Math_fround(0), $6_1 = 0, $7_1 = 0, $8 = 0, $8$hi = 0; + $1_1 = x; + if (Math_fround(Math_abs($1_1)) < Math_fround(9223372036854775808.0)) { + i64toi32_i32$0 = $1_1; + if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) { + if (i64toi32_i32$0 > Math_fround(0.0)) { + $6_1 = ~~Math_fround(Math_min(Math_fround(Math_floor(Math_fround(i64toi32_i32$0 / Math_fround(4294967296.0)))), Math_fround(Math_fround(4294967296.0) - Math_fround(1.0)))) >>> 0 + } else { + $6_1 = ~~Math_fround(Math_ceil(Math_fround(Math_fround(i64toi32_i32$0 - Math_fround(~~i64toi32_i32$0 >>> 0 >>> 0)) / Math_fround(4294967296.0)))) >>> 0 + } + $7_1 = $6_1; + } else { + $7_1 = 0 + } + i64toi32_i32$1 = $7_1; + $8 = ~~i64toi32_i32$0 >>> 0; + $8$hi = i64toi32_i32$1; + } else { + i64toi32_i32$1 = -2147483648; + $8 = 0; + $8$hi = i64toi32_i32$1; + } + i64toi32_i32$1 = $8$hi; + i64toi32_i32$HIGH_BITS = i64toi32_i32$1; + return $8 | 0; + } + + function $5(x) { + x = Math_fround(x); + var i64toi32_i32$0 = Math_fround(0), i64toi32_i32$1 = 0, $1_1 = Math_fround(0), $6_1 = 0, $7_1 = 0, $10 = 0, $10$hi = 0; + $1_1 = x; + if ($1_1 < Math_fround(18446744073709551615.0) & $1_1 >= Math_fround(0.0) | 0) { + i64toi32_i32$0 = $1_1; + if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) { + if (i64toi32_i32$0 > Math_fround(0.0)) { + $6_1 = ~~Math_fround(Math_min(Math_fround(Math_floor(Math_fround(i64toi32_i32$0 / Math_fround(4294967296.0)))), Math_fround(Math_fround(4294967296.0) - Math_fround(1.0)))) >>> 0 + } else { + $6_1 = ~~Math_fround(Math_ceil(Math_fround(Math_fround(i64toi32_i32$0 - Math_fround(~~i64toi32_i32$0 >>> 0 >>> 0)) / Math_fround(4294967296.0)))) >>> 0 + } + $7_1 = $6_1; + } else { + $7_1 = 0 + } + i64toi32_i32$1 = $7_1; + $10 = ~~i64toi32_i32$0 >>> 0; + $10$hi = i64toi32_i32$1; + } else { + i64toi32_i32$1 = 0; + $10 = 0; + $10$hi = i64toi32_i32$1; + } + i64toi32_i32$1 = $10$hi; + i64toi32_i32$HIGH_BITS = i64toi32_i32$1; + return $10 | 0; + } + + function $6(x) { + x = +x; + var i64toi32_i32$0 = 0.0, i64toi32_i32$1 = 0, $1_1 = 0.0, $6_1 = 0, $7_1 = 0, $8 = 0, $8$hi = 0; + $1_1 = x; + if (Math_abs($1_1) < 9223372036854775808.0) { + i64toi32_i32$0 = $1_1; + if (Math_abs(i64toi32_i32$0) >= 1.0) { + if (i64toi32_i32$0 > 0.0) { + $6_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0 + } else { + $6_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0 + } + $7_1 = $6_1; + } else { + $7_1 = 0 + } + i64toi32_i32$1 = $7_1; + $8 = ~~i64toi32_i32$0 >>> 0; + $8$hi = i64toi32_i32$1; + } else { + i64toi32_i32$1 = -2147483648; + $8 = 0; + $8$hi = i64toi32_i32$1; + } + i64toi32_i32$1 = $8$hi; + i64toi32_i32$HIGH_BITS = i64toi32_i32$1; + return $8 | 0; + } + + function $7(x) { + x = +x; + var i64toi32_i32$0 = 0.0, i64toi32_i32$1 = 0, $1_1 = 0.0, $6_1 = 0, $7_1 = 0, $10 = 0, $10$hi = 0; + $1_1 = x; + if ($1_1 < 18446744073709551615.0 & $1_1 >= 0.0 | 0) { + i64toi32_i32$0 = $1_1; + if (Math_abs(i64toi32_i32$0) >= 1.0) { + if (i64toi32_i32$0 > 0.0) { + $6_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0 + } else { + $6_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0 + } + $7_1 = $6_1; + } else { + $7_1 = 0 + } + i64toi32_i32$1 = $7_1; + $10 = ~~i64toi32_i32$0 >>> 0; + $10$hi = i64toi32_i32$1; + } else { + i64toi32_i32$1 = 0; + $10 = 0; + $10$hi = i64toi32_i32$1; + } + i64toi32_i32$1 = $10$hi; + i64toi32_i32$HIGH_BITS = i64toi32_i32$1; + return $10 | 0; + } + + function legalstub$4($0_1) { + $0_1 = Math_fround($0_1); + var i64toi32_i32$0 = 0, i64toi32_i32$4 = 0, i64toi32_i32$1 = 0, i64toi32_i32$3 = 0, $8 = 0, $1_1 = 0, $1$hi = 0, i64toi32_i32$2 = 0; + i64toi32_i32$0 = $4(Math_fround($0_1)) | 0; + i64toi32_i32$1 = i64toi32_i32$HIGH_BITS; + $1_1 = i64toi32_i32$0; + $1$hi = i64toi32_i32$1; + i64toi32_i32$2 = i64toi32_i32$0; + i64toi32_i32$0 = 0; + i64toi32_i32$3 = 32; + i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0; + if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) { + i64toi32_i32$0 = 0; + $8 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + } else { + i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + $8 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0; + } + setTempRet0($8 | 0); + i64toi32_i32$0 = $1$hi; + return $1_1 | 0; + } + + function legalstub$5($0_1) { + $0_1 = Math_fround($0_1); + var i64toi32_i32$0 = 0, i64toi32_i32$4 = 0, i64toi32_i32$1 = 0, i64toi32_i32$3 = 0, $8 = 0, $1_1 = 0, $1$hi = 0, i64toi32_i32$2 = 0; + i64toi32_i32$0 = $5(Math_fround($0_1)) | 0; + i64toi32_i32$1 = i64toi32_i32$HIGH_BITS; + $1_1 = i64toi32_i32$0; + $1$hi = i64toi32_i32$1; + i64toi32_i32$2 = i64toi32_i32$0; + i64toi32_i32$0 = 0; + i64toi32_i32$3 = 32; + i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0; + if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) { + i64toi32_i32$0 = 0; + $8 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + } else { + i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + $8 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0; + } + setTempRet0($8 | 0); + i64toi32_i32$0 = $1$hi; + return $1_1 | 0; + } + + function legalstub$6($0_1) { + $0_1 = +$0_1; + var i64toi32_i32$0 = 0, i64toi32_i32$4 = 0, i64toi32_i32$1 = 0, i64toi32_i32$3 = 0, $8 = 0, $1_1 = 0, $1$hi = 0, i64toi32_i32$2 = 0; + i64toi32_i32$0 = $6(+$0_1) | 0; + i64toi32_i32$1 = i64toi32_i32$HIGH_BITS; + $1_1 = i64toi32_i32$0; + $1$hi = i64toi32_i32$1; + i64toi32_i32$2 = i64toi32_i32$0; + i64toi32_i32$0 = 0; + i64toi32_i32$3 = 32; + i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0; + if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) { + i64toi32_i32$0 = 0; + $8 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + } else { + i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + $8 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0; + } + setTempRet0($8 | 0); + i64toi32_i32$0 = $1$hi; + return $1_1 | 0; + } + + function legalstub$7($0_1) { + $0_1 = +$0_1; + var i64toi32_i32$0 = 0, i64toi32_i32$4 = 0, i64toi32_i32$1 = 0, i64toi32_i32$3 = 0, $8 = 0, $1_1 = 0, $1$hi = 0, i64toi32_i32$2 = 0; + i64toi32_i32$0 = $7(+$0_1) | 0; + i64toi32_i32$1 = i64toi32_i32$HIGH_BITS; + $1_1 = i64toi32_i32$0; + $1$hi = i64toi32_i32$1; + i64toi32_i32$2 = i64toi32_i32$0; + i64toi32_i32$0 = 0; + i64toi32_i32$3 = 32; + i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0; + if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) { + i64toi32_i32$0 = 0; + $8 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + } else { + i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0; + $8 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0; + } + setTempRet0($8 | 0); + i64toi32_i32$0 = $1$hi; + return $1_1 | 0; + } + + // EMSCRIPTEN_END_FUNCS +; + return { + "i32_trunc_sat_f32_s": $0, + "i32_trunc_sat_f32_u": $1, + "i32_trunc_sat_f64_s": $2, + "i32_trunc_sat_f64_u": $3, + "i64_trunc_sat_f32_s": legalstub$4, + "i64_trunc_sat_f32_u": legalstub$5, + "i64_trunc_sat_f64_s": legalstub$6, + "i64_trunc_sat_f64_u": legalstub$7 + }; +} + + return asmFunc(info); +} diff --git a/test/wasm2js/conversions-emscripten-modified.2asm.js.opt b/test/wasm2js/conversions-emscripten-modified.2asm.js.opt new file mode 100644 index 000000000..d29f8f21f --- /dev/null +++ b/test/wasm2js/conversions-emscripten-modified.2asm.js.opt @@ -0,0 +1,121 @@ +function instantiate(info) { +function asmFunc(imports) { + var Math_imul = Math.imul; + var Math_fround = Math.fround; + var Math_abs = Math.abs; + var Math_clz32 = Math.clz32; + var Math_min = Math.min; + var Math_max = Math.max; + var Math_floor = Math.floor; + var Math_ceil = Math.ceil; + var Math_trunc = Math.trunc; + var Math_sqrt = Math.sqrt; + var env = imports.env; + var setTempRet0 = env.setTempRet0; + var i64toi32_i32$HIGH_BITS = 0; + // EMSCRIPTEN_START_FUNCS +; + function $0($0_1) { + $0_1 = Math_fround($0_1); + return (Math_fround(Math_abs($0_1)) < Math_fround(2147483648.0) ? ~~$0_1 : -2147483648) | 0; + } + + function $1($0_1) { + $0_1 = Math_fround($0_1); + return ($0_1 < Math_fround(4294967296.0) & $0_1 >= Math_fround(0.0) ? ~~$0_1 >>> 0 : 0) | 0; + } + + function $2($0_1) { + $0_1 = +$0_1; + return (Math_abs($0_1) < 2147483647.0 ? ~~$0_1 : -2147483648) | 0; + } + + function $3($0_1) { + $0_1 = +$0_1; + return ($0_1 < 4294967295.0 & $0_1 >= 0.0 ? ~~$0_1 >>> 0 : 0) | 0; + } + + function legalstub$4($0_1) { + var $1_1 = 0, $2_1 = 0; + if (Math_fround(Math_abs($0_1)) < Math_fround(9223372036854775808.0)) { + $2_1 = ~~$0_1 >>> 0; + if (Math_fround(Math_abs($0_1)) >= Math_fround(1.0)) { + $1_1 = ~~($0_1 > Math_fround(0.0) ? Math_fround(Math_min(Math_fround(Math_floor(Math_fround($0_1 * Math_fround(2.3283064365386963e-10)))), Math_fround(4294967296.0))) : Math_fround(Math_ceil(Math_fround(Math_fround($0_1 - Math_fround(~~$0_1 >>> 0 >>> 0)) * Math_fround(2.3283064365386963e-10))))) >>> 0 + } else { + $1_1 = 0 + } + } else { + $1_1 = -2147483648 + } + i64toi32_i32$HIGH_BITS = $1_1; + setTempRet0(i64toi32_i32$HIGH_BITS | 0); + return $2_1; + } + + function legalstub$5($0_1) { + var $1_1 = 0, $2_1 = 0; + if ($0_1 < Math_fround(18446744073709551615.0) & $0_1 >= Math_fround(0.0)) { + $2_1 = ~~$0_1 >>> 0; + if (Math_fround(Math_abs($0_1)) >= Math_fround(1.0)) { + $1_1 = ~~($0_1 > Math_fround(0.0) ? Math_fround(Math_min(Math_fround(Math_floor(Math_fround($0_1 * Math_fround(2.3283064365386963e-10)))), Math_fround(4294967296.0))) : Math_fround(Math_ceil(Math_fround(Math_fround($0_1 - Math_fround(~~$0_1 >>> 0 >>> 0)) * Math_fround(2.3283064365386963e-10))))) >>> 0 + } else { + $1_1 = 0 + } + } else { + $1_1 = 0 + } + i64toi32_i32$HIGH_BITS = $1_1; + setTempRet0(i64toi32_i32$HIGH_BITS | 0); + return $2_1; + } + + function legalstub$6($0_1) { + var $1_1 = 0, $2_1 = 0; + if (Math_abs($0_1) < 9223372036854775808.0) { + $2_1 = ~~$0_1 >>> 0; + if (Math_abs($0_1) >= 1.0) { + $1_1 = ~~($0_1 > 0.0 ? Math_min(Math_floor($0_1 * 2.3283064365386963e-10), 4294967295.0) : Math_ceil(($0_1 - +(~~$0_1 >>> 0 >>> 0)) * 2.3283064365386963e-10)) >>> 0 + } else { + $1_1 = 0 + } + } else { + $1_1 = -2147483648 + } + i64toi32_i32$HIGH_BITS = $1_1; + setTempRet0(i64toi32_i32$HIGH_BITS | 0); + return $2_1; + } + + function legalstub$7($0_1) { + var $1_1 = 0, $2_1 = 0; + if ($0_1 < 18446744073709551615.0 & $0_1 >= 0.0) { + $2_1 = ~~$0_1 >>> 0; + if (Math_abs($0_1) >= 1.0) { + $1_1 = ~~($0_1 > 0.0 ? Math_min(Math_floor($0_1 * 2.3283064365386963e-10), 4294967295.0) : Math_ceil(($0_1 - +(~~$0_1 >>> 0 >>> 0)) * 2.3283064365386963e-10)) >>> 0 + } else { + $1_1 = 0 + } + } else { + $1_1 = 0 + } + i64toi32_i32$HIGH_BITS = $1_1; + setTempRet0(i64toi32_i32$HIGH_BITS | 0); + return $2_1; + } + + // EMSCRIPTEN_END_FUNCS +; + return { + "i32_trunc_sat_f32_s": $0, + "i32_trunc_sat_f32_u": $1, + "i32_trunc_sat_f64_s": $2, + "i32_trunc_sat_f64_u": $3, + "i64_trunc_sat_f32_s": legalstub$4, + "i64_trunc_sat_f32_u": legalstub$5, + "i64_trunc_sat_f64_s": legalstub$6, + "i64_trunc_sat_f64_u": legalstub$7 + }; +} + + return asmFunc(info); +} diff --git a/test/wasm2js/conversions-emscripten-modified.wast b/test/wasm2js/conversions-emscripten-modified.wast new file mode 100644 index 000000000..05de07c47 --- /dev/null +++ b/test/wasm2js/conversions-emscripten-modified.wast @@ -0,0 +1,10 @@ +(module + (func (export "i32.trunc_sat_f32_s") (param $x f32) (result i32) (i32.trunc_sat_f32_s (local.get $x))) + (func (export "i32.trunc_sat_f32_u") (param $x f32) (result i32) (i32.trunc_sat_f32_u (local.get $x))) + (func (export "i32.trunc_sat_f64_s") (param $x f64) (result i32) (i32.trunc_sat_f64_s (local.get $x))) + (func (export "i32.trunc_sat_f64_u") (param $x f64) (result i32) (i32.trunc_sat_f64_u (local.get $x))) + (func (export "i64.trunc_sat_f32_s") (param $x f32) (result i64) (i64.trunc_sat_f32_s (local.get $x))) + (func (export "i64.trunc_sat_f32_u") (param $x f32) (result i64) (i64.trunc_sat_f32_u (local.get $x))) + (func (export "i64.trunc_sat_f64_s") (param $x f64) (result i64) (i64.trunc_sat_f64_s (local.get $x))) + (func (export "i64.trunc_sat_f64_u") (param $x f64) (result i64) (i64.trunc_sat_f64_u (local.get $x))) +) |