summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ir/cost.h3
-rw-r--r--src/passes/LocalCSE.cpp16
-rw-r--r--test/example/cpp-unit.cpp18
-rw-r--r--test/example/cpp-unit.txt1
-rw-r--r--test/passes/licm.txt17
-rw-r--r--test/passes/licm.wast9
-rw-r--r--test/passes/local-cse.txt15
-rw-r--r--test/passes/local-cse.wast8
8 files changed, 85 insertions, 2 deletions
diff --git a/src/ir/cost.h b/src/ir/cost.h
index defd9413e..b64ca5110 100644
--- a/src/ir/cost.h
+++ b/src/ir/cost.h
@@ -17,6 +17,9 @@
#ifndef wasm_ir_cost_h
#define wasm_ir_cost_h
+#include <wasm.h>
+#include <wasm-traversal.h>
+
namespace wasm {
// Measure the execution cost of an AST. Very handwave-ey
diff --git a/src/passes/LocalCSE.cpp b/src/passes/LocalCSE.cpp
index 12f0d9d93..8faa5b47c 100644
--- a/src/passes/LocalCSE.cpp
+++ b/src/passes/LocalCSE.cpp
@@ -42,6 +42,7 @@
#include <wasm-traversal.h>
#include <pass.h>
#include <ir/effects.h>
+#include <ir/cost.h>
#include <ir/equivalent_sets.h>
#include <ir/hashed.h>
@@ -208,8 +209,19 @@ struct LocalCSE : public WalkerPass<LinearExecutionWalker<LocalCSE>> {
if (EffectAnalyzer(getPassOptions(), value).hasSideEffects()) {
return false; // we can't combine things with side effects
}
- // check what we care about TODO: use optimize/shrink levels?
- return Measurer::measure(value) > 1;
+ auto& options = getPassRunner()->options;
+ // If the size is at least 3, then if we have two of them we have 6,
+ // and so adding one set+two gets and removing one of the items itself
+ // is not detrimental, and may be beneficial.
+ if (options.shrinkLevel > 0 && Measurer::measure(value) >= 3) {
+ return true;
+ }
+ // If we focus on speed, any reduction in cost is beneficial, as the
+ // cost of a get is essentially free.
+ if (options.shrinkLevel == 0 && CostAnalyzer(value).cost > 0) {
+ return true;
+ }
+ return false;
}
};
diff --git a/test/example/cpp-unit.cpp b/test/example/cpp-unit.cpp
new file mode 100644
index 000000000..f51ca72b6
--- /dev/null
+++ b/test/example/cpp-unit.cpp
@@ -0,0 +1,18 @@
+// test multiple uses of the threadPool
+
+#include <assert.h>
+
+#include <wasm.h>
+#include <ir/cost.h>
+
+using namespace wasm;
+
+int main()
+{
+ // Some optimizations assume that the cost of a get is zero, e.g. local-cse.
+ GetLocal get;
+ assert(CostAnalyzer(&get).cost == 0);
+
+ std::cout << "Success.\n";
+ return 0;
+}
diff --git a/test/example/cpp-unit.txt b/test/example/cpp-unit.txt
new file mode 100644
index 000000000..a9d787cc5
--- /dev/null
+++ b/test/example/cpp-unit.txt
@@ -0,0 +1 @@
+Success.
diff --git a/test/passes/licm.txt b/test/passes/licm.txt
index e76a0d683..25c78c7a0 100644
--- a/test/passes/licm.txt
+++ b/test/passes/licm.txt
@@ -4,6 +4,7 @@
(type $2 (func (result i64)))
(type $3 (func (param i32)))
(type $4 (func (param i32) (result i32)))
+ (global $glob (mut i32) (i32.const 1))
(func $loop1 (; 0 ;) (type $0)
(drop
(i32.const 10)
@@ -674,4 +675,20 @@
)
)
)
+ (func $global (; 40 ;) (type $0)
+ (local $x i32)
+ (set_local $x
+ (get_global $glob)
+ )
+ (drop
+ (get_local $x)
+ )
+ (loop $loop
+ (nop)
+ (nop)
+ (br_if $loop
+ (get_local $x)
+ )
+ )
+ )
)
diff --git a/test/passes/licm.wast b/test/passes/licm.wast
index d4227525b..349739ce8 100644
--- a/test/passes/licm.wast
+++ b/test/passes/licm.wast
@@ -1,4 +1,5 @@
(module
+ (global $glob (mut i32) (i32.const 1))
(func $loop1
(loop $loop
(drop (i32.const 10))
@@ -388,5 +389,13 @@
(br_if $loop (i32.const 1))
)
)
+ (func $global
+ (local $x i32)
+ (loop $loop
+ (set_local $x (get_global $glob))
+ (drop (get_local $x))
+ (br_if $loop (get_local $x))
+ )
+ )
)
diff --git a/test/passes/local-cse.txt b/test/passes/local-cse.txt
index 970a13dd8..62395f73f 100644
--- a/test/passes/local-cse.txt
+++ b/test/passes/local-cse.txt
@@ -1,5 +1,7 @@
(module
(type $0 (func (result i64)))
+ (type $1 (func))
+ (global $glob (mut i32) (i32.const 1))
(func $i64-shifts (; 0 ;) (type $0) (result i64)
(local $temp i64)
(set_local $temp
@@ -19,4 +21,17 @@
)
(get_local $temp)
)
+ (func $global (; 1 ;) (type $1)
+ (local $x i32)
+ (local $y i32)
+ (set_local $x
+ (get_global $glob)
+ )
+ (set_local $y
+ (get_local $x)
+ )
+ (set_local $y
+ (get_local $x)
+ )
+ )
)
diff --git a/test/passes/local-cse.wast b/test/passes/local-cse.wast
index 23ea6ff68..09534be60 100644
--- a/test/passes/local-cse.wast
+++ b/test/passes/local-cse.wast
@@ -1,4 +1,5 @@
(module
+ (global $glob (mut i32) (i32.const 1))
(func $i64-shifts (result i64)
(local $temp i64)
(set_local $temp
@@ -18,4 +19,11 @@
)
(get_local $temp)
)
+ (func $global
+ (local $x i32)
+ (local $y i32)
+ (set_local $x (get_global $glob))
+ (set_local $y (get_global $glob))
+ (set_local $y (get_global $glob))
+ )
)