Skip to content

Commit 7179572

Browse files
authored
Untee pass (#1053)
1 parent 8aa91ec commit 7179572

6 files changed

Lines changed: 126 additions & 3 deletions

File tree

src/passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SET(passes_SOURCES
3434
ReorderFunctions.cpp
3535
SimplifyLocals.cpp
3636
SSAify.cpp
37+
Untee.cpp
3738
Vacuum.cpp
3839
)
3940
ADD_LIBRARY(passes STATIC ${passes_SOURCES})

src/passes/Untee.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2017 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+
// Removes tee_locals, replacing them with gets and sets.
19+
//
20+
// This makes the code "flatter", with less nested side
21+
// effects. That can make some passes, like CodePushing,
22+
// more effective.
23+
//
24+
25+
#include <wasm.h>
26+
#include <pass.h>
27+
#include <wasm-builder.h>
28+
29+
namespace wasm {
30+
31+
struct Untee : public WalkerPass<PostWalker<Untee>> {
32+
bool isFunctionParallel() override { return true; }
33+
34+
Pass* create() override { return new Untee; }
35+
36+
void visitSetLocal(SetLocal *curr) {
37+
if (curr->isTee()) {
38+
if (curr->value->type == unreachable) {
39+
// we don't reach the tee, just remove it
40+
replaceCurrent(curr->value);
41+
} else {
42+
// a normal tee. replace with set and get
43+
Builder builder(*getModule());
44+
replaceCurrent(
45+
builder.makeSequence(
46+
curr,
47+
builder.makeGetLocal(curr->index, curr->value->type)
48+
)
49+
);
50+
curr->setTee(false);
51+
}
52+
}
53+
}
54+
};
55+
56+
Pass *createUnteePass() {
57+
return new Untee();
58+
}
59+
60+
} // namespace wasm
61+

src/passes/pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void PassRegistry::registerPasses() {
8585
registerPass("optimize-instructions", "optimizes instruction combinations", createOptimizeInstructionsPass);
8686
registerPass("pick-load-signs", "pick load signs based on their uses", createPickLoadSignsPass);
8787
registerPass("post-emscripten", "miscellaneous optimizations for Emscripten-generated code", createPostEmscriptenPass);
88+
registerPass("precompute", "computes compile-time evaluatable expressions", createPrecomputePass);
8889
registerPass("print", "print in s-expression format", createPrinterPass);
8990
registerPass("print-minified", "print in minified s-expression format", createMinifiedPrinterPass);
9091
registerPass("print-full", "print in full s-expression format", createFullPrinterPass);
@@ -103,8 +104,8 @@ void PassRegistry::registerPasses() {
103104
registerPass("simplify-locals-nostructure", "miscellaneous locals-related optimizations", createSimplifyLocalsNoStructurePass);
104105
registerPass("simplify-locals-notee-nostructure", "miscellaneous locals-related optimizations", createSimplifyLocalsNoTeeNoStructurePass);
105106
registerPass("ssa", "ssa-ify variables so that they have a single assignment", createSSAifyPass);
107+
registerPass("untee", "removes tee_locals, replacing them with sets and gets", createUnteePass);
106108
registerPass("vacuum", "removes obviously unneeded code", createVacuumPass);
107-
registerPass("precompute", "computes compile-time evaluatable expressions", createPrecomputePass);
108109
// registerPass("lower-i64", "lowers i64 into pairs of i32s", createLowerInt64Pass);
109110
}
110111

src/passes/passes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Pass *createNameManagerPass();
4545
Pass *createOptimizeInstructionsPass();
4646
Pass *createPickLoadSignsPass();
4747
Pass *createPostEmscriptenPass();
48+
Pass *createPrecomputePass();
4849
Pass *createPrinterPass();
4950
Pass *createPrintCallGraphPass();
5051
Pass *createRelooperJumpThreadingPass();
@@ -61,9 +62,8 @@ Pass *createSimplifyLocalsNoTeePass();
6162
Pass *createSimplifyLocalsNoStructurePass();
6263
Pass *createSimplifyLocalsNoTeeNoStructurePass();
6364
Pass *createSSAifyPass();
65+
Pass *createUnteePass();
6466
Pass *createVacuumPass();
65-
Pass *createPrecomputePass();
66-
//Pass *createLowerInt64Pass();
6767

6868
}
6969

test/passes/untee.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
(module
2+
(type $0 (func))
3+
(memory $0 0)
4+
(func $tee (type $0)
5+
(local $x i32)
6+
(local $y f64)
7+
(drop
8+
(block (result i32)
9+
(set_local $x
10+
(i32.const 1)
11+
)
12+
(get_local $x)
13+
)
14+
)
15+
(drop
16+
(block (result f64)
17+
(set_local $y
18+
(f64.const 2)
19+
)
20+
(get_local $y)
21+
)
22+
)
23+
(set_local $x
24+
(block (result i32)
25+
(set_local $x
26+
(i32.const 3)
27+
)
28+
(get_local $x)
29+
)
30+
)
31+
(set_local $x
32+
(block (result i32)
33+
(set_local $x
34+
(block (result i32)
35+
(set_local $x
36+
(i32.const 3)
37+
)
38+
(get_local $x)
39+
)
40+
)
41+
(get_local $x)
42+
)
43+
)
44+
(drop
45+
(unreachable)
46+
)
47+
)
48+
)

test/passes/untee.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(module
2+
(func $tee
3+
(local $x i32)
4+
(local $y f64)
5+
(drop (tee_local $x (i32.const 1)))
6+
(drop (tee_local $y (f64.const 2)))
7+
(set_local $x (tee_local $x (i32.const 3)))
8+
(set_local $x (tee_local $x (tee_local $x (i32.const 3))))
9+
(drop (tee_local $x (unreachable)))
10+
)
11+
)
12+

0 commit comments

Comments
 (0)