FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[wasm2js] Support nontrapping float-to-int conversions directly in JS · WebAssembly/binaryen@f0a2110 · GitHub

Commit f0a2110

Browse files
committed
[wasm2js] Support nontrapping float-to-int conversions directly in JS
Now that 64-bit `trunc_sat` instructions are supported directly in `I64ToI32Lowering` (#9017), and 32-bit `trunc_sat` instructions are handled directly in `wasm2js` (`~~expr` and `~~expr >>> 0`), we no longer need to run `llvm-nontrapping-fptoint-lowering` when `--emscripten` is passed. In `wasm2js`, trapping float-to-int operations (`i32.trunc_*` and `i64.trunc_*`) are already lowered to non-trapping JS conversions (`~~expr` and float arithmetic in `I64ToI32Lowering`), which do not trap on out-of-range values or NaN/Infinity (matching C/C++ UB semantics rather than strict Wasm trapping semantics). Removing `llvm-nontrapping-fptoint-lowering` extends that same direct approach to `trunc_sat` instructions under `--emscripten`, eliminating branching overhead and reducing code size.
1 parent 0af42fa commit f0a2110

4 files changed

Lines changed: 64 additions & 169 deletions

File tree

‎src/passes/LLVMNontrappingFPToIntLowering.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <limits>
55
#include <memory>
66

7+
// Note: This pass is no longer used either in emscripten, or internally by any
8+
// other part of binaryen. Unless there are other uses that show up we may
9+
// consider this pass for removal.
10+
711
// By default LLVM emits nontrapping float-to-int instructions to implement its
812
// fptoui/fptosi conversion instructions. This pass replaces these instructions
913
// with code sequences which also implement LLVM's fptoui/fptosi, but which are

‎src/wasm2js.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,6 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
360360
// First, do the lowering to a JS-friendly subset.
361361
{
362362
PassRunner runner(wasm, options);
363-
if (flags.emscripten) {
364-
runner.add("llvm-nontrapping-fptoint-lowering");
365-
}
366363
// TODO: only legalize if necessary - emscripten would already do so, and
367364
// likely other toolchains. but spec test suite needs that.
368365
runner.add("legalize-js-interface");

‎test/wasm2js/conversions-emscripten-modified.2asm.js‎

Lines changed: 48 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -17,166 +17,102 @@ function asmFunc(imports) {
1717
;
1818
function $0(x) {
1919
x = Math_fround(x);
20-
var $1_1 = Math_fround(0), $8 = 0;
21-
$1_1 = x;
22-
if (Math_fround(Math_abs($1_1)) < Math_fround(2147483648.0)) {
23-
$8 = ~~$1_1
24-
} else {
25-
$8 = -2147483648
26-
}
27-
return $8 | 0;
20+
return ~~x | 0;
2821
}
2922

3023
function $1(x) {
3124
x = Math_fround(x);
32-
var $1_1 = Math_fround(0), $10 = 0;
33-
$1_1 = x;
34-
if ($1_1 < Math_fround(4294967296.0) & $1_1 >= Math_fround(0.0) | 0) {
35-
$10 = ~~$1_1 >>> 0
36-
} else {
37-
$10 = 0
38-
}
39-
return $10 | 0;
25+
return ~~x >>> 0 | 0;
4026
}
4127

4228
function $2(x) {
4329
x = +x;
44-
var $1_1 = 0.0, $8 = 0;
45-
$1_1 = x;
46-
if (Math_abs($1_1) < 2147483647.0) {
47-
$8 = ~~$1_1
48-
} else {
49-
$8 = -2147483648
50-
}
51-
return $8 | 0;
30+
return ~~x | 0;
5231
}
5332

5433
function $3(x) {
5534
x = +x;
56-
var $1_1 = 0.0, $10 = 0;
57-
$1_1 = x;
58-
if ($1_1 < 4294967295.0 & $1_1 >= 0.0 | 0) {
59-
$10 = ~~$1_1 >>> 0
60-
} else {
61-
$10 = 0
62-
}
63-
return $10 | 0;
35+
return ~~x >>> 0 | 0;
6436
}
6537

6638
function $4(x) {
6739
x = Math_fround(x);
68-
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;
69-
$1_1 = x;
70-
if (Math_fround(Math_abs($1_1)) < Math_fround(9223372036854775808.0)) {
71-
i64toi32_i32$0 = $1_1;
72-
if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) {
73-
if (i64toi32_i32$0 > Math_fround(0.0)) {
74-
$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
75-
} else {
76-
$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
77-
}
78-
$7_1 = $6_1;
40+
var i64toi32_i32$0 = Math_fround(0), $4_1 = 0, $5_1 = 0, i64toi32_i32$1 = 0, i64toi32_i32$2 = 0;
41+
i64toi32_i32$0 = x;
42+
if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) {
43+
if (i64toi32_i32$0 > Math_fround(0.0)) {
44+
$4_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
7945
} else {
80-
$7_1 = 0
46+
$4_1 = ~~Math_fround(Math_ceil(Math_fround(Math_fround(i64toi32_i32$0 - Math_fround(~~i64toi32_i32$0 >>> 0 >>> 0)) / Math_fround(4294967296.0)))) >>> 0
8147
}
82-
i64toi32_i32$1 = $7_1;
83-
$8 = ~~i64toi32_i32$0 >>> 0;
84-
$8$hi = i64toi32_i32$1;
48+
$5_1 = $4_1;
8549
} else {
86-
i64toi32_i32$1 = -2147483648;
87-
$8 = 0;
88-
$8$hi = i64toi32_i32$1;
50+
$5_1 = 0
8951
}
90-
i64toi32_i32$1 = $8$hi;
52+
i64toi32_i32$1 = $5_1;
53+
i64toi32_i32$2 = ~~i64toi32_i32$0 >>> 0;
9154
i64toi32_i32$HIGH_BITS = i64toi32_i32$1;
92-
return $8 | 0;
55+
return i64toi32_i32$2 | 0;
9356
}
9457

9558
function $5(x) {
9659
x = Math_fround(x);
97-
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;
98-
$1_1 = x;
99-
if ($1_1 < Math_fround(18446744073709551615.0) & $1_1 >= Math_fround(0.0) | 0) {
100-
i64toi32_i32$0 = $1_1;
101-
if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) {
102-
if (i64toi32_i32$0 > Math_fround(0.0)) {
103-
$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
104-
} else {
105-
$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
106-
}
107-
$7_1 = $6_1;
60+
var i64toi32_i32$0 = Math_fround(0), $4_1 = 0, $5_1 = 0, i64toi32_i32$1 = 0, i64toi32_i32$2 = 0;
61+
i64toi32_i32$0 = x;
62+
if (Math_fround(Math_abs(i64toi32_i32$0)) >= Math_fround(1.0)) {
63+
if (i64toi32_i32$0 > Math_fround(0.0)) {
64+
$4_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
10865
} else {
109-
$7_1 = 0
66+
$4_1 = ~~Math_fround(Math_ceil(Math_fround(Math_fround(i64toi32_i32$0 - Math_fround(~~i64toi32_i32$0 >>> 0 >>> 0)) / Math_fround(4294967296.0)))) >>> 0
11067
}
111-
i64toi32_i32$1 = $7_1;
112-
$10 = ~~i64toi32_i32$0 >>> 0;
113-
$10$hi = i64toi32_i32$1;
68+
$5_1 = $4_1;
11469
} else {
115-
i64toi32_i32$1 = 0;
116-
$10 = 0;
117-
$10$hi = i64toi32_i32$1;
70+
$5_1 = 0
11871
}
119-
i64toi32_i32$1 = $10$hi;
72+
i64toi32_i32$1 = $5_1;
73+
i64toi32_i32$2 = ~~i64toi32_i32$0 >>> 0;
12074
i64toi32_i32$HIGH_BITS = i64toi32_i32$1;
121-
return $10 | 0;
75+
return i64toi32_i32$2 | 0;
12276
}
12377

12478
function $6(x) {
12579
x = +x;
126-
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;
127-
$1_1 = x;
128-
if (Math_abs($1_1) < 9223372036854775808.0) {
129-
i64toi32_i32$0 = $1_1;
130-
if (Math_abs(i64toi32_i32$0) >= 1.0) {
131-
if (i64toi32_i32$0 > 0.0) {
132-
$6_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0
133-
} else {
134-
$6_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0
135-
}
136-
$7_1 = $6_1;
80+
var i64toi32_i32$0 = 0.0, $4_1 = 0, $5_1 = 0, i64toi32_i32$1 = 0, i64toi32_i32$2 = 0;
81+
i64toi32_i32$0 = x;
82+
if (Math_abs(i64toi32_i32$0) >= 1.0) {
83+
if (i64toi32_i32$0 > 0.0) {
84+
$4_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0
13785
} else {
138-
$7_1 = 0
86+
$4_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0
13987
}
140-
i64toi32_i32$1 = $7_1;
141-
$8 = ~~i64toi32_i32$0 >>> 0;
142-
$8$hi = i64toi32_i32$1;
88+
$5_1 = $4_1;
14389
} else {
144-
i64toi32_i32$1 = -2147483648;
145-
$8 = 0;
146-
$8$hi = i64toi32_i32$1;
90+
$5_1 = 0
14791
}
148-
i64toi32_i32$1 = $8$hi;
92+
i64toi32_i32$1 = $5_1;
93+
i64toi32_i32$2 = ~~i64toi32_i32$0 >>> 0;
14994
i64toi32_i32$HIGH_BITS = i64toi32_i32$1;
150-
return $8 | 0;
95+
return i64toi32_i32$2 | 0;
15196
}
15297

15398
function $7(x) {
15499
x = +x;
155-
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;
156-
$1_1 = x;
157-
if ($1_1 < 18446744073709551615.0 & $1_1 >= 0.0 | 0) {
158-
i64toi32_i32$0 = $1_1;
159-
if (Math_abs(i64toi32_i32$0) >= 1.0) {
160-
if (i64toi32_i32$0 > 0.0) {
161-
$6_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0
162-
} else {
163-
$6_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0
164-
}
165-
$7_1 = $6_1;
100+
var i64toi32_i32$0 = 0.0, $4_1 = 0, $5_1 = 0, i64toi32_i32$1 = 0, i64toi32_i32$2 = 0;
101+
i64toi32_i32$0 = x;
102+
if (Math_abs(i64toi32_i32$0) >= 1.0) {
103+
if (i64toi32_i32$0 > 0.0) {
104+
$4_1 = ~~Math_min(Math_floor(i64toi32_i32$0 / 4294967296.0), 4294967296.0 - 1.0) >>> 0
166105
} else {
167-
$7_1 = 0
106+
$4_1 = ~~Math_ceil((i64toi32_i32$0 - +(~~i64toi32_i32$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0
168107
}
169-
i64toi32_i32$1 = $7_1;
170-
$10 = ~~i64toi32_i32$0 >>> 0;
171-
$10$hi = i64toi32_i32$1;
108+
$5_1 = $4_1;
172109
} else {
173-
i64toi32_i32$1 = 0;
174-
$10 = 0;
175-
$10$hi = i64toi32_i32$1;
110+
$5_1 = 0
176111
}
177-
i64toi32_i32$1 = $10$hi;
112+
i64toi32_i32$1 = $5_1;
113+
i64toi32_i32$2 = ~~i64toi32_i32$0 >>> 0;
178114
i64toi32_i32$HIGH_BITS = i64toi32_i32$1;
179-
return $10 | 0;
115+
return i64toi32_i32$2 | 0;
180116
}
181117

182118
function legalstub$4($0_1) {

‎test/wasm2js/conversions-emscripten-modified.2asm.js.opt‎

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,29 @@ function asmFunc(imports) {
1717
;
1818
function $0($0_1) {
1919
$0_1 = Math_fround($0_1);
20-
return (Math_fround(Math_abs($0_1)) < Math_fround(2147483648.0) ? ~~$0_1 : -2147483648) | 0;
20+
return ~~$0_1 | 0;
2121
}
2222

2323
function $1($0_1) {
2424
$0_1 = Math_fround($0_1);
25-
return ($0_1 < Math_fround(4294967296.0) & $0_1 >= Math_fround(0.0) ? ~~$0_1 >>> 0 : 0) | 0;
25+
return ~~$0_1 >>> 0 | 0;
2626
}
2727

2828
function $2($0_1) {
2929
$0_1 = +$0_1;
30-
return (Math_abs($0_1) < 2147483647.0 ? ~~$0_1 : -2147483648) | 0;
30+
return ~~$0_1 | 0;
3131
}
3232

3333
function $3($0_1) {
3434
$0_1 = +$0_1;
35-
return ($0_1 < 4294967295.0 & $0_1 >= 0.0 ? ~~$0_1 >>> 0 : 0) | 0;
35+
return ~~$0_1 >>> 0 | 0;
3636
}
3737

3838
function legalstub$4($0_1) {
3939
var $1_1 = 0, $2_1 = 0;
40-
if (Math_fround(Math_abs($0_1)) < Math_fround(9223372036854775808.0)) {
41-
$2_1 = ~~$0_1 >>> 0;
42-
if (Math_fround(Math_abs($0_1)) >= Math_fround(1.0)) {
43-
$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
44-
} else {
45-
$1_1 = 0
46-
}
47-
} else {
48-
$1_1 = -2147483648
49-
}
50-
i64toi32_i32$HIGH_BITS = $1_1;
51-
setTempRet0(i64toi32_i32$HIGH_BITS | 0);
52-
return $2_1;
53-
}
54-
55-
function legalstub$5($0_1) {
56-
var $1_1 = 0, $2_1 = 0;
57-
if ($0_1 < Math_fround(18446744073709551615.0) & $0_1 >= Math_fround(0.0)) {
58-
$2_1 = ~~$0_1 >>> 0;
59-
if (Math_fround(Math_abs($0_1)) >= Math_fround(1.0)) {
60-
$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
61-
} else {
62-
$1_1 = 0
63-
}
40+
$2_1 = ~~$0_1 >>> 0;
41+
if (Math_fround(Math_abs($0_1)) >= Math_fround(1.0)) {
42+
$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
6443
} else {
6544
$1_1 = 0
6645
}
@@ -71,30 +50,9 @@ function asmFunc(imports) {
7150

7251
function legalstub$6($0_1) {
7352
var $1_1 = 0, $2_1 = 0;
74-
if (Math_abs($0_1) < 9223372036854775808.0) {
75-
$2_1 = ~~$0_1 >>> 0;
76-
if (Math_abs($0_1) >= 1.0) {
77-
$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
78-
} else {
79-
$1_1 = 0
80-
}
81-
} else {
82-
$1_1 = -2147483648
83-
}
84-
i64toi32_i32$HIGH_BITS = $1_1;
85-
setTempRet0(i64toi32_i32$HIGH_BITS | 0);
86-
return $2_1;
87-
}
88-
89-
function legalstub$7($0_1) {
90-
var $1_1 = 0, $2_1 = 0;
91-
if ($0_1 < 18446744073709551615.0 & $0_1 >= 0.0) {
92-
$2_1 = ~~$0_1 >>> 0;
93-
if (Math_abs($0_1) >= 1.0) {
94-
$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
95-
} else {
96-
$1_1 = 0
97-
}
53+
$2_1 = ~~$0_1 >>> 0;
54+
if (Math_abs($0_1) >= 1.0) {
55+
$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
9856
} else {
9957
$1_1 = 0
10058
}
@@ -111,9 +69,9 @@ function asmFunc(imports) {
11169
"i32_trunc_sat_f64_s": $2,
11270
"i32_trunc_sat_f64_u": $3,
11371
"i64_trunc_sat_f32_s": legalstub$4,
114-
"i64_trunc_sat_f32_u": legalstub$5,
72+
"i64_trunc_sat_f32_u": legalstub$4,
11573
"i64_trunc_sat_f64_s": legalstub$6,
116-
"i64_trunc_sat_f64_u": legalstub$7
74+
"i64_trunc_sat_f64_u": legalstub$6
11775
};
11876
}
11977

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL