Skip to content

Commit 4be7ec0

Browse files
committed
handle unary and binary nodes that have implicit traps in vacuum
1 parent 45f8fed commit 4be7ec0

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/passes/Vacuum.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,26 @@ struct Vacuum : public WalkerPass<PostWalker<Vacuum>> {
9393
if (resultUsed) {
9494
return curr; // used, keep it
9595
}
96-
// for unary, binary, and select, we need to check their arguments for side effects
96+
// for unary, binary, and select, we need to check their arguments for side effects,
97+
// as well as the node itself, as some unaries and binaries have implicit traps
9798
if (auto* unary = curr->dynCast<Unary>()) {
99+
EffectAnalyzer tester(getPassOptions());
100+
tester.visitUnary(unary);
101+
if (tester.hasSideEffects()) {
102+
return curr;
103+
}
98104
if (EffectAnalyzer(getPassOptions(), unary->value).hasSideEffects()) {
99105
curr = unary->value;
100106
continue;
101107
} else {
102108
return nullptr;
103109
}
104110
} else if (auto* binary = curr->dynCast<Binary>()) {
111+
EffectAnalyzer tester(getPassOptions());
112+
tester.visitBinary(binary);
113+
if (tester.hasSideEffects()) {
114+
return curr;
115+
}
105116
if (EffectAnalyzer(getPassOptions(), binary->left).hasSideEffects()) {
106117
if (EffectAnalyzer(getPassOptions(), binary->right).hasSideEffects()) {
107118
return curr; // leave them

test/passes/vacuum.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,17 @@
260260
(i64.const 0)
261261
)
262262
)
263+
(func $unary-binary-may-trap (type $0)
264+
(drop
265+
(i64.div_s
266+
(i64.const -1)
267+
(i64.const 729618461987467893)
268+
)
269+
)
270+
(drop
271+
(i64.trunc_u/f32
272+
(f32.const 70847791997969805621592064)
273+
)
274+
)
275+
)
263276
)

test/passes/vacuum.wast

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,17 @@
580580
(i64.const 0)
581581
)
582582
)
583+
(func $unary-binary-may-trap
584+
(drop
585+
(i64.div_s
586+
(i64.const 70847791997969805621592064)
587+
(i64.const 729618461987467893)
588+
)
589+
)
590+
(drop
591+
(i64.trunc_u/f32
592+
(f32.const 70847791997969805621592064)
593+
)
594+
)
595+
)
583596
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
(module
22
(type $0 (func (result i32)))
3+
(type $1 (func))
34
(memory $0 0)
45
(func $load-would-normally-have-side-effects (type $0) (result i32)
56
(i64.ge_s
67
(i64.const 2912825531628789796)
78
(i64.const 0)
89
)
910
)
11+
(func $unary-binary-may-trap (type $1)
12+
(nop)
13+
)
1014
)

test/passes/vacuum_ignore-implicit-traps.wast

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,18 @@
1515
(i64.const 0)
1616
)
1717
)
18+
(func $unary-binary-may-trap
19+
(drop
20+
(i64.div_s
21+
(i64.const 70847791997969805621592064)
22+
(i64.const 729618461987467893)
23+
)
24+
)
25+
(drop
26+
(i64.trunc_u/f32
27+
(f32.const 70847791997969805621592064)
28+
)
29+
)
30+
)
1831
)
1932

0 commit comments

Comments
 (0)