Skip to content

Commit 3a993f9

Browse files
committed
add a pass to drop return values in set_local and store (#539)
1 parent 57ec212 commit 3a993f9

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

src/passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SET(passes_SOURCES
22
pass.cpp
33
CoalesceLocals.cpp
44
DeadCodeElimination.cpp
5+
DropReturnValues.cpp
56
LowerIfElse.cpp
67
MergeBlocks.cpp
78
Metrics.cpp

src/passes/DropReturnValues.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2016 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//
18+
// Stops using return values from set_local and store nodes.
19+
//
20+
21+
#include <wasm.h>
22+
#include <pass.h>
23+
#include <ast_utils.h>
24+
#include <wasm-builder.h>
25+
26+
namespace wasm {
27+
28+
struct DropReturnValues : public WalkerPass<PostWalker<DropReturnValues, Visitor<DropReturnValues>>> {
29+
bool isFunctionParallel() { return true; }
30+
31+
std::vector<Expression*> expressionStack;
32+
33+
void visitSetLocal(SetLocal* curr) {
34+
if (ExpressionAnalyzer::isResultUsed(expressionStack, getFunction())) {
35+
Builder builder(*getModule());
36+
replaceCurrent(builder.makeSequence(
37+
curr,
38+
builder.makeGetLocal(curr->index, curr->type)
39+
));
40+
}
41+
}
42+
43+
void visitStore(Store* curr) {
44+
if (ExpressionAnalyzer::isResultUsed(expressionStack, getFunction())) {
45+
Index index = getFunction()->getNumLocals();
46+
getFunction()->vars.emplace_back(curr->type);
47+
Builder builder(*getModule());
48+
replaceCurrent(builder.makeSequence(
49+
builder.makeSequence(
50+
builder.makeSetLocal(index, curr->value),
51+
curr
52+
),
53+
builder.makeGetLocal(index, curr->type)
54+
));
55+
curr->value = builder.makeGetLocal(index, curr->type);
56+
}
57+
}
58+
59+
static void visitPre(DropReturnValues* self, Expression** currp) {
60+
self->expressionStack.push_back(*currp);
61+
}
62+
63+
static void visitPost(DropReturnValues* self, Expression** currp) {
64+
self->expressionStack.pop_back();
65+
}
66+
67+
static void scan(DropReturnValues* self, Expression** currp) {
68+
self->pushTask(visitPost, currp);
69+
70+
WalkerPass<PostWalker<DropReturnValues, Visitor<DropReturnValues>>>::scan(self, currp);
71+
72+
self->pushTask(visitPre, currp);
73+
}
74+
};
75+
76+
static RegisterPass<DropReturnValues> registerPass("drop-return-values", "stops relying on return values from set_local and store");
77+
78+
} // namespace wasm
79+

test/passes/drop-return-values.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(module
2+
(memory 10)
3+
(func $0
4+
(local $x i32)
5+
(local $1 i32)
6+
(i32.add
7+
(block
8+
(set_local $x
9+
(i32.const 10)
10+
)
11+
(get_local $x)
12+
)
13+
(i32.const 20)
14+
)
15+
(i32.add
16+
(block
17+
(block
18+
(set_local $1
19+
(i32.const 40)
20+
)
21+
(i32.store
22+
(i32.const 30)
23+
(get_local $1)
24+
)
25+
)
26+
(get_local $1)
27+
)
28+
(i32.const 50)
29+
)
30+
)
31+
)

test/passes/drop-return-values.wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module
2+
(memory 10)
3+
(func
4+
(local $x i32)
5+
(i32.add (set_local $x (i32.const 10)) (i32.const 20))
6+
(i32.add (i32.store (i32.const 30) (i32.const 40)) (i32.const 50))
7+
)
8+
)
9+

0 commit comments

Comments
 (0)