Skip to content

Commit bf6972c

Browse files
authored
Fix i64 select lowering. (#1773)
1 parent 39411cc commit bf6972c

File tree

5 files changed

+116
-17
lines changed

5 files changed

+116
-17
lines changed

src/passes/I64ToI32Lowering.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
235235
setOutParam(curr, std::move(highBits));
236236
}
237237

238-
// If and Select have identical code
239-
template<typename T>
240-
void visitBranching(T* curr) {
238+
void visitIf(If* curr) {
241239
if (!hasOutParam(curr->ifTrue)) return;
242240
assert(curr->ifFalse != nullptr && "Nullable ifFalse found");
243241
TempVar highBits = fetchOutParam(curr->ifTrue);
@@ -255,10 +253,6 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
255253
setOutParam(curr, std::move(highBits));
256254
}
257255

258-
void visitIf(If* curr) {
259-
visitBranching<If>(curr);
260-
}
261-
262256
void visitLoop(Loop* curr) {
263257
assert(labelHighBitVars.find(curr->name) == labelHighBitVars.end());
264258
if (curr->type != i64) return;
@@ -1526,7 +1520,36 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
15261520
}
15271521

15281522
void visitSelect(Select* curr) {
1529-
visitBranching<Select>(curr);
1523+
if (!hasOutParam(curr->ifTrue)) {
1524+
assert(!hasOutParam(curr->ifFalse));
1525+
return;
1526+
}
1527+
assert(hasOutParam(curr->ifFalse));
1528+
TempVar highBits = getTemp();
1529+
TempVar lowBits = getTemp();
1530+
TempVar cond = getTemp();
1531+
Block* result = builder->blockify(
1532+
builder->makeSetLocal(cond, curr->condition),
1533+
builder->makeSetLocal(
1534+
lowBits,
1535+
builder->makeSelect(
1536+
builder->makeGetLocal(cond, i32),
1537+
curr->ifTrue,
1538+
curr->ifFalse
1539+
)
1540+
),
1541+
builder->makeSetLocal(
1542+
highBits,
1543+
builder->makeSelect(
1544+
builder->makeGetLocal(cond, i32),
1545+
builder->makeGetLocal(fetchOutParam(curr->ifTrue), i32),
1546+
builder->makeGetLocal(fetchOutParam(curr->ifFalse), i32)
1547+
)
1548+
),
1549+
builder->makeGetLocal(lowBits, i32)
1550+
);
1551+
setOutParam(result, std::move(highBits));
1552+
replaceCurrent(result);
15301553
}
15311554

15321555
void visitDrop(Drop* curr) {

test/wasm2js/i64-select.2asm.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function asmFunc(global, env, buffer) {
2+
"use asm";
3+
var HEAP8 = new global.Int8Array(buffer);
4+
var HEAP16 = new global.Int16Array(buffer);
5+
var HEAP32 = new global.Int32Array(buffer);
6+
var HEAPU8 = new global.Uint8Array(buffer);
7+
var HEAPU16 = new global.Uint16Array(buffer);
8+
var HEAPU32 = new global.Uint32Array(buffer);
9+
var HEAPF32 = new global.Float32Array(buffer);
10+
var HEAPF64 = new global.Float64Array(buffer);
11+
var Math_imul = global.Math.imul;
12+
var Math_fround = global.Math.fround;
13+
var Math_abs = global.Math.abs;
14+
var Math_clz32 = global.Math.clz32;
15+
var Math_min = global.Math.min;
16+
var Math_max = global.Math.max;
17+
var Math_floor = global.Math.floor;
18+
var Math_ceil = global.Math.ceil;
19+
var Math_sqrt = global.Math.sqrt;
20+
var abort = env.abort;
21+
var nan = global.NaN;
22+
var infinity = global.Infinity;
23+
var i64toi32_i32$HIGH_BITS = 0;
24+
function p(i) {
25+
i = i | 0;
26+
return i | 0;
27+
}
28+
29+
function $1($0) {
30+
$0 = $0 | 0;
31+
var wasm2js_i32$0 = 0, wasm2js_i32$1 = 0, wasm2js_i32$2 = 0;
32+
return (wasm2js_i32$0 = p(4294967295 | 0) | 0, wasm2js_i32$1 = p(0 | 0) | 0, wasm2js_i32$2 = 1, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1) | 0;
33+
}
34+
35+
function $2() {
36+
var i64toi32_i32$3 = 0, i64toi32_i32$2 = 0, i64toi32_i32$4 = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0, wasm2js_i32$2 = 0;
37+
i64toi32_i32$4 = 1;
38+
i64toi32_i32$3 = (wasm2js_i32$0 = 4294967295, wasm2js_i32$1 = 0, wasm2js_i32$2 = i64toi32_i32$4, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1);
39+
i64toi32_i32$2 = (wasm2js_i32$0 = 4294967295, wasm2js_i32$1 = 0, wasm2js_i32$2 = i64toi32_i32$4, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1);
40+
i64toi32_i32$2 = i64toi32_i32$2;
41+
i64toi32_i32$3 = i64toi32_i32$3;
42+
i64toi32_i32$HIGH_BITS = i64toi32_i32$2;
43+
return i64toi32_i32$3 | 0;
44+
}
45+
46+
return {
47+
48+
};
49+
}
50+
51+
const memasmFunc = new ArrayBuffer(65536);
52+
const retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc);

test/wasm2js/i64-select.wast

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
;; Testing i64 select
2+
3+
(module
4+
(func $p (param $i i32) (result i32) (get_local $i))
5+
(func (param i32) (result i64)
6+
(return
7+
(select
8+
(call $p (i32.const -1))
9+
(call $p (i32.const 0))
10+
(i32.const 1)
11+
)
12+
)
13+
)
14+
(func (result i64)
15+
(return
16+
(select
17+
(i64.const -1)
18+
(i64.const 0)
19+
(i32.const 1)
20+
)
21+
)
22+
)
23+
)

test/wasm2js/left-to-right.2asm.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ function asmFunc(global, env, buffer) {
10611061
}
10621062

10631063
function $93() {
1064-
var i64toi32_i32$0 = 0, i64toi32_i32$1 = 0, $0 = 0, $0$hi = 0, $1 = 0, $1$hi = 0;
1064+
var i64toi32_i32$0 = 0, i64toi32_i32$1 = 0, i64toi32_i32$4 = 0, $0 = 0, $0$hi = 0, $1 = 0, $1$hi = 0;
10651065
reset();
10661066
i64toi32_i32$0 = i64_left() | 0;
10671067
i64toi32_i32$1 = i64toi32_i32$HIGH_BITS;
@@ -1071,11 +1071,9 @@ function asmFunc(global, env, buffer) {
10711071
i64toi32_i32$0 = i64toi32_i32$HIGH_BITS;
10721072
$1 = i64toi32_i32$1;
10731073
$1$hi = i64toi32_i32$0;
1074+
i64toi32_i32$4 = i64_bool() | 0;
10741075
i64toi32_i32$0 = $0$hi;
10751076
i64toi32_i32$1 = $1$hi;
1076-
i64toi32_i32$0 = i64toi32_i32$1;
1077-
i64toi32_i32$0 = i64toi32_i32$1;
1078-
i64_bool() | 0;
10791077
return get() | 0 | 0;
10801078
}
10811079

test/wasm2js/select.2asm.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ function asmFunc(global, env, buffer) {
3535
rhs = rhs | 0;
3636
rhs$hi = rhs$hi | 0;
3737
cond = cond | 0;
38-
var i64toi32_i32$0 = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0, wasm2js_i32$2 = 0;
38+
var i64toi32_i32$0 = 0, i64toi32_i32$3 = 0, i64toi32_i32$2 = 0, i64toi32_i32$4 = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0, wasm2js_i32$2 = 0;
3939
i64toi32_i32$0 = lhs$hi;
4040
i64toi32_i32$0 = rhs$hi;
41+
i64toi32_i32$4 = cond;
4142
i64toi32_i32$0 = lhs$hi;
42-
i64toi32_i32$0 = rhs$hi;
43-
i64toi32_i32$0 = i64toi32_i32$0;
44-
i64toi32_i32$HIGH_BITS = i64toi32_i32$0;
45-
return (wasm2js_i32$0 = lhs, wasm2js_i32$1 = rhs, wasm2js_i32$2 = cond, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1) | 0;
43+
i64toi32_i32$3 = (wasm2js_i32$0 = lhs, wasm2js_i32$1 = rhs, wasm2js_i32$2 = i64toi32_i32$4, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1);
44+
i64toi32_i32$2 = (wasm2js_i32$0 = i64toi32_i32$0, wasm2js_i32$1 = rhs$hi, wasm2js_i32$2 = i64toi32_i32$4, wasm2js_i32$2 ? wasm2js_i32$0 : wasm2js_i32$1);
45+
i64toi32_i32$2 = i64toi32_i32$2;
46+
i64toi32_i32$3 = i64toi32_i32$3;
47+
i64toi32_i32$HIGH_BITS = i64toi32_i32$2;
48+
return i64toi32_i32$3 | 0;
4649
}
4750

4851
function $2(lhs, rhs, cond) {

0 commit comments

Comments
 (0)