Skip to content

Commit ad9152e

Browse files
authored
More #1678 fixes (#1685)
While debugging to fix the waterfall regressions I noticed that wasm-reduce regressed. We need to be more careful with visitFunction which now may visit an imported function - I found a few not-well-tested passes that also regressed that way.
1 parent 41ebb1b commit ad9152e

File tree

10 files changed

+115
-10
lines changed

10 files changed

+115
-10
lines changed

src/passes/I64ToI32Lowering.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
175175
}
176176

177177
void visitFunction(Function* func) {
178+
if (func->imported()) {
179+
return;
180+
}
178181
if (func->result == i64) {
179182
func->result = i32;
180183
// body may not have out param if it ends with control flow

src/passes/LogExecution.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ struct LogExecution : public WalkerPass<PostWalker<LogExecution>> {
4242
}
4343

4444
void visitFunction(Function* curr) {
45+
if (curr->imported()) {
46+
return;
47+
}
4548
curr->body = makeLogCall(curr->body);
4649
}
4750

src/tools/wasm-reduce.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,11 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
524524
}
525525

526526
void visitFunction(Function* curr) {
527-
// extra chance to work on the function toplevel element, as if it can
528-
// be reduced it's great
529-
visitExpression(curr->body);
527+
if (!curr->imported()) {
528+
// extra chance to work on the function toplevel element, as if it can
529+
// be reduced it's great
530+
visitExpression(curr->body);
531+
}
530532
// finish function
531533
funcsSeen++;
532534
static int last = 0;
@@ -677,7 +679,7 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
677679
if (module->functions.size() == 1 && module->exports.empty() && module->table.segments.empty()) {
678680
auto* func = module->functions[0].get();
679681
// We can't remove something that might have breaks to it.
680-
if (!Properties::isNamedControlFlow(func->body)) {
682+
if (!func->imported() && !Properties::isNamedControlFlow(func->body)) {
681683
auto funcType = func->type;
682684
auto funcResult = func->result;
683685
auto* funcBody = func->body;

src/wasm/wasm-emscripten.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ struct EmJsWalker : public PostWalker<EmJsWalker> {
489489
segmentOffsets(getSegmentOffsets(wasm)) { }
490490

491491
void visitFunction(Function* curr) {
492+
if (curr->imported()) {
493+
return;
494+
}
492495
if (!curr->name.startsWith(EM_JS_PREFIX.str)) {
493496
return;
494497
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
(module
2+
(type $FUNCSIG$j (func (result i32)))
3+
(import "env" "func" (func $import (result i32)))
4+
(global $i64toi32_i32$HIGH_BITS (mut i32) (i32.const 0))
5+
(func $defined (; 1 ;) (type $FUNCSIG$j) (result i32)
6+
(local $i64toi32_i32$0 i32)
7+
(local $i64toi32_i32$1 i32)
8+
(local $i64toi32_i32$2 i32)
9+
(local $i64toi32_i32$3 i32)
10+
(local $i64toi32_i32$4 i32)
11+
(local $i64toi32_i32$5 i32)
12+
(set_local $i64toi32_i32$2
13+
(block (result i32)
14+
(set_local $i64toi32_i32$2
15+
(block (result i32)
16+
(set_local $i64toi32_i32$0
17+
(i32.const 0)
18+
)
19+
(i32.const 1)
20+
)
21+
)
22+
(set_local $i64toi32_i32$3
23+
(block (result i32)
24+
(set_local $i64toi32_i32$1
25+
(i32.const 0)
26+
)
27+
(i32.const 2)
28+
)
29+
)
30+
(set_local $i64toi32_i32$4
31+
(i32.add
32+
(get_local $i64toi32_i32$2)
33+
(get_local $i64toi32_i32$3)
34+
)
35+
)
36+
(set_local $i64toi32_i32$5
37+
(i32.add
38+
(get_local $i64toi32_i32$0)
39+
(get_local $i64toi32_i32$1)
40+
)
41+
)
42+
(if
43+
(i32.lt_u
44+
(get_local $i64toi32_i32$4)
45+
(get_local $i64toi32_i32$3)
46+
)
47+
(set_local $i64toi32_i32$5
48+
(i32.add
49+
(get_local $i64toi32_i32$5)
50+
(i32.const 1)
51+
)
52+
)
53+
)
54+
(get_local $i64toi32_i32$4)
55+
)
56+
)
57+
(set_global $i64toi32_i32$HIGH_BITS
58+
(get_local $i64toi32_i32$5)
59+
)
60+
(get_local $i64toi32_i32$2)
61+
)
62+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(module
2+
(import "env" "func" (func $import (result i64)))
3+
(func $defined (result i64)
4+
(i64.add (i64.const 1) (i64.const 2))
5+
)
6+
)
7+

test/passes/log-execution.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
(module
2-
(type $0 (func))
2+
(type $FUNCSIG$v (func))
33
(type $1 (func (result i32)))
44
(type $FUNCSIG$vi (func (param i32)))
5+
(import "env" "func" (func $import))
56
(import "env" "log_execution" (func $log_execution (param i32)))
6-
(func $nopp (; 1 ;) (type $0)
7+
(func $nopp (; 2 ;) (type $FUNCSIG$v)
78
(call $log_execution
89
(i32.const 0)
910
)
1011
(nop)
1112
)
12-
(func $intt (; 2 ;) (type $1) (result i32)
13+
(func $intt (; 3 ;) (type $1) (result i32)
1314
(call $log_execution
1415
(i32.const 1)
1516
)
1617
(i32.const 10)
1718
)
18-
(func $workk (; 3 ;) (type $0)
19+
(func $workk (; 4 ;) (type $FUNCSIG$v)
1920
(call $log_execution
2021
(i32.const 2)
2122
)
@@ -29,7 +30,7 @@
2930
)
3031
)
3132
)
32-
(func $loops (; 4 ;) (type $0)
33+
(func $loops (; 5 ;) (type $FUNCSIG$v)
3334
(call $log_execution
3435
(i32.const 6)
3536
)
@@ -70,7 +71,7 @@
7071
)
7172
)
7273
)
73-
(func $loops-similar (; 5 ;) (type $0)
74+
(func $loops-similar (; 6 ;) (type $FUNCSIG$v)
7475
(call $log_execution
7576
(i32.const 8)
7677
)

test/passes/log-execution.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(module
2+
(import "env" "func" (func $import))
23
(func $nopp
34
(nop)
45
)

test/reduce/imports.wast

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(module
2+
(import "env" "func" (func $import))
3+
(export "x" (func $x))
4+
(func $x (result i32)
5+
(nop)
6+
(nop)
7+
(nop)
8+
(call $import)
9+
(drop (i32.const 1234))
10+
(i32.const 5678) ;; easily reducible
11+
)
12+
(func $not-exported
13+
(nop)
14+
(unreachable)
15+
)
16+
)
17+

test/reduce/imports.wast.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(module
2+
(type $0 (func))
3+
(type $1 (func (result i32)))
4+
(import "env" "func" (func $fimport$0))
5+
)
6+

0 commit comments

Comments
 (0)