diff options
-rw-r--r-- | src/passes/SimplifyGlobals.cpp | 39 | ||||
-rw-r--r-- | src/passes/pass.cpp | 1 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm | 15 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm.clamp | 15 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm.imprecise | 15 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm | 10 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm.clamp | 10 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm.imprecise | 10 | ||||
-rw-r--r-- | test/memorygrowth.fromasm | 15 | ||||
-rw-r--r-- | test/memorygrowth.fromasm.clamp | 15 | ||||
-rw-r--r-- | test/memorygrowth.fromasm.imprecise | 15 | ||||
-rw-r--r-- | test/passes/O1.txt | 4 | ||||
-rw-r--r-- | test/passes/O4_disable-bulk-memory.txt | 4 | ||||
-rw-r--r-- | test/passes/simplify-globals-optimizing_enable-mutable-globals.txt | 11 | ||||
-rw-r--r-- | test/passes/simplify-globals_all-features.txt | 19 | ||||
-rw-r--r-- | test/passes/simplify-globals_all-features.wast | 7 | ||||
-rw-r--r-- | test/wasm2js/global_i64.2asm.js.opt | 5 |
17 files changed, 80 insertions, 130 deletions
diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 5fa1686fc..71aafa491 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -51,6 +51,7 @@ struct GlobalInfo { bool imported = false; bool exported = false; std::atomic<bool> written; + std::atomic<bool> read; }; using GlobalInfoMap = std::map<Name, GlobalInfo>; @@ -64,6 +65,8 @@ struct GlobalUseScanner : public WalkerPass<PostWalker<GlobalUseScanner>> { void visitGlobalSet(GlobalSet* curr) { (*infos)[curr->name].written = true; } + void visitGlobalGet(GlobalGet* curr) { (*infos)[curr->name].read = true; } + private: GlobalInfoMap* infos; }; @@ -163,6 +166,23 @@ private: std::map<Name, Literals> currConstantGlobals; }; +struct GlobalSetRemover : public WalkerPass<PostWalker<GlobalSetRemover>> { + GlobalSetRemover(NameSet* toRemove) : toRemove(toRemove) {} + + bool isFunctionParallel() override { return true; } + + GlobalSetRemover* create() override { return new GlobalSetRemover(toRemove); } + + void visitGlobalSet(GlobalSet* curr) { + if (toRemove->count(curr->name) != 0) { + ExpressionManipulator::nop(curr); + } + } + +private: + NameSet* toRemove; +}; + } // anonymous namespace struct SimplifyGlobals : public Pass { @@ -180,6 +200,8 @@ struct SimplifyGlobals : public Pass { analyze(); + removeWritesToUnreadGlobals(); + preferEarlierImports(); propagateConstantsToGlobals(); @@ -211,6 +233,23 @@ struct SimplifyGlobals : public Pass { } } + void removeWritesToUnreadGlobals() { + // Globals that are not exports and not read from can be eliminated + // (even if they are written to). + NameSet unreadGlobals; + for (auto& global : module->globals) { + auto& info = map[global->name]; + if (!info.imported && !info.exported && !info.read) { + unreadGlobals.insert(global->name); + // We can now mark this global as immutable, and un-written, since we + // are about to remove all the `.set` operations on it. + global->mutable_ = false; + info.written = false; + } + } + GlobalSetRemover(&unreadGlobals).run(runner, module); + } + void preferEarlierImports() { // Optimize uses of immutable globals, prefer the earlier import when // there is a copy. diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index 733b61467..4da870ae9 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -483,6 +483,7 @@ void PassRunner::addDefaultGlobalOptimizationPostPasses() { } else { add("simplify-globals"); } + add("vacuum"); // simplify-globals can generate extra nops add("remove-unused-module-elements"); // may allow more inlining/dae/etc., need --converge for that add("directize"); diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm index ddcb90a43..bd9af676b 100644 --- a/test/emcc_O2_hello_world.fromasm +++ b/test/emcc_O2_hello_world.fromasm @@ -13,7 +13,6 @@ (elem (global.get $__table_base) $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $STACK_MAX$asm2wasm$import i32)) (import "env" "abort" (func $abort (param i32))) (import "env" "_pthread_cleanup_pop" (func $_pthread_cleanup_pop (param i32))) (import "env" "_pthread_self" (func $_pthread_self (result i32))) @@ -30,9 +29,7 @@ (import "env" "___syscall140" (func $___syscall140 (param i32 i32) (result i32))) (import "env" "___syscall146" (func $___syscall146 (param i32 i32) (result i32))) (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) - (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_free" (func $_free)) (export "_main" (func $_main)) @@ -8972,13 +8969,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) @@ -9002,9 +8994,6 @@ (global.set $STACKTOP (local.get $0) ) - (global.set $STACK_MAX - (local.get $1) - ) ) (func $dynCall_vi (; has Stack IR ;) (param $0 i32) (param $1 i32) (call_indirect (type $i32_=>_none) diff --git a/test/emcc_O2_hello_world.fromasm.clamp b/test/emcc_O2_hello_world.fromasm.clamp index ddcb90a43..bd9af676b 100644 --- a/test/emcc_O2_hello_world.fromasm.clamp +++ b/test/emcc_O2_hello_world.fromasm.clamp @@ -13,7 +13,6 @@ (elem (global.get $__table_base) $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $STACK_MAX$asm2wasm$import i32)) (import "env" "abort" (func $abort (param i32))) (import "env" "_pthread_cleanup_pop" (func $_pthread_cleanup_pop (param i32))) (import "env" "_pthread_self" (func $_pthread_self (result i32))) @@ -30,9 +29,7 @@ (import "env" "___syscall140" (func $___syscall140 (param i32 i32) (result i32))) (import "env" "___syscall146" (func $___syscall146 (param i32 i32) (result i32))) (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) - (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_free" (func $_free)) (export "_main" (func $_main)) @@ -8972,13 +8969,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) @@ -9002,9 +8994,6 @@ (global.set $STACKTOP (local.get $0) ) - (global.set $STACK_MAX - (local.get $1) - ) ) (func $dynCall_vi (; has Stack IR ;) (param $0 i32) (param $1 i32) (call_indirect (type $i32_=>_none) diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise index f08e59565..82edc64b9 100644 --- a/test/emcc_O2_hello_world.fromasm.imprecise +++ b/test/emcc_O2_hello_world.fromasm.imprecise @@ -12,7 +12,6 @@ (elem (global.get $__table_base) $b0 $___stdio_close $b1 $b1 $___stdout_write $___stdio_seek $b1 $___stdio_write $b1 $b1 $b2 $b2 $b2 $b2 $_cleanup_418 $b2 $b2 $b2) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $STACK_MAX$asm2wasm$import i32)) (import "env" "abort" (func $abort (param i32))) (import "env" "_pthread_cleanup_pop" (func $_pthread_cleanup_pop (param i32))) (import "env" "_pthread_self" (func $_pthread_self (result i32))) @@ -29,9 +28,7 @@ (import "env" "___syscall140" (func $___syscall140 (param i32 i32) (result i32))) (import "env" "___syscall146" (func $___syscall146 (param i32 i32) (result i32))) (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) - (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_free" (func $_free)) (export "_main" (func $_main)) @@ -8950,13 +8947,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) @@ -8976,9 +8968,6 @@ (global.set $STACKTOP (local.get $0) ) - (global.set $STACK_MAX - (local.get $1) - ) ) (func $dynCall_vi (; has Stack IR ;) (param $0 i32) (param $1 i32) (call_indirect (type $i32_=>_none) diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm index c4462eb4e..774740dc1 100644 --- a/test/emcc_hello_world.fromasm +++ b/test/emcc_hello_world.fromasm @@ -42,7 +42,6 @@ (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_i64Subtract" (func $_i64Subtract)) (export "_free" (func $_free)) @@ -117,13 +116,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) diff --git a/test/emcc_hello_world.fromasm.clamp b/test/emcc_hello_world.fromasm.clamp index e524ddd07..fecd5a564 100644 --- a/test/emcc_hello_world.fromasm.clamp +++ b/test/emcc_hello_world.fromasm.clamp @@ -41,7 +41,6 @@ (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_i64Subtract" (func $_i64Subtract)) (export "_free" (func $_free)) @@ -116,13 +115,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise index 89edb7f44..4c5d7afd2 100644 --- a/test/emcc_hello_world.fromasm.imprecise +++ b/test/emcc_hello_world.fromasm.imprecise @@ -39,7 +39,6 @@ (global $STACKTOP (mut i32) (global.get $STACKTOP$asm2wasm$import)) (global $STACK_MAX (mut i32) (global.get $STACK_MAX$asm2wasm$import)) (global $__THREW__ (mut i32) (i32.const 0)) - (global $threwValue (mut i32) (i32.const 0)) (global $tempRet0 (mut i32) (i32.const 0)) (export "_i64Subtract" (func $_i64Subtract)) (export "_free" (func $_free)) @@ -114,13 +113,8 @@ (i32.eqz (global.get $__THREW__) ) - (block - (global.set $__THREW__ - (local.get $0) - ) - (global.set $threwValue - (local.get $1) - ) + (global.set $__THREW__ + (local.get $0) ) ) ) diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm index 220bba783..9c909afcf 100644 --- a/test/memorygrowth.fromasm +++ b/test/memorygrowth.fromasm @@ -14,7 +14,6 @@ (import "env" "__memory_base" (global $__memory_base i32)) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $r$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $s$asm2wasm$import i32)) (import "env" "abort" (func $ja (param i32))) (import "env" "_pthread_cleanup_pop" (func $oa (param i32))) (import "env" "___lock" (func $pa (param i32))) @@ -28,9 +27,7 @@ (import "env" "___unlock" (func $xa (param i32))) (import "env" "___syscall146" (func $ya (param i32 i32) (result i32))) (global $r (mut i32) (global.get $r$asm2wasm$import)) - (global $s (mut i32) (global.get $s$asm2wasm$import)) (global $v (mut i32) (i32.const 0)) - (global $w (mut i32) (i32.const 0)) (global $K (mut i32) (i32.const 0)) (export "__growWasmMemory" (func $__growWasmMemory)) (export "_free" (func $fb)) @@ -9012,13 +9009,8 @@ (i32.eqz (global.get $v) ) - (block - (global.set $v - (local.get $0) - ) - (global.set $w - (local.get $1) - ) + (global.set $v + (local.get $0) ) ) ) @@ -9054,9 +9046,6 @@ (global.set $r (local.get $0) ) - (global.set $s - (local.get $1) - ) ) (func $nb (; has Stack IR ;) (param $0 i32) (result i32) (call $ja diff --git a/test/memorygrowth.fromasm.clamp b/test/memorygrowth.fromasm.clamp index 220bba783..9c909afcf 100644 --- a/test/memorygrowth.fromasm.clamp +++ b/test/memorygrowth.fromasm.clamp @@ -14,7 +14,6 @@ (import "env" "__memory_base" (global $__memory_base i32)) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $r$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $s$asm2wasm$import i32)) (import "env" "abort" (func $ja (param i32))) (import "env" "_pthread_cleanup_pop" (func $oa (param i32))) (import "env" "___lock" (func $pa (param i32))) @@ -28,9 +27,7 @@ (import "env" "___unlock" (func $xa (param i32))) (import "env" "___syscall146" (func $ya (param i32 i32) (result i32))) (global $r (mut i32) (global.get $r$asm2wasm$import)) - (global $s (mut i32) (global.get $s$asm2wasm$import)) (global $v (mut i32) (i32.const 0)) - (global $w (mut i32) (i32.const 0)) (global $K (mut i32) (i32.const 0)) (export "__growWasmMemory" (func $__growWasmMemory)) (export "_free" (func $fb)) @@ -9012,13 +9009,8 @@ (i32.eqz (global.get $v) ) - (block - (global.set $v - (local.get $0) - ) - (global.set $w - (local.get $1) - ) + (global.set $v + (local.get $0) ) ) ) @@ -9054,9 +9046,6 @@ (global.set $r (local.get $0) ) - (global.set $s - (local.get $1) - ) ) (func $nb (; has Stack IR ;) (param $0 i32) (result i32) (call $ja diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise index cec88c90f..b90edfcd6 100644 --- a/test/memorygrowth.fromasm.imprecise +++ b/test/memorygrowth.fromasm.imprecise @@ -12,7 +12,6 @@ (elem (global.get $__table_base) $nb $Oa $ob $Va $Ua $Ra $pb $Sa) (import "env" "__table_base" (global $__table_base i32)) (import "env" "STACKTOP" (global $r$asm2wasm$import i32)) - (import "env" "STACK_MAX" (global $s$asm2wasm$import i32)) (import "env" "abort" (func $ja (param i32))) (import "env" "_pthread_cleanup_pop" (func $oa (param i32))) (import "env" "___lock" (func $pa (param i32))) @@ -26,9 +25,7 @@ (import "env" "___unlock" (func $xa (param i32))) (import "env" "___syscall146" (func $ya (param i32 i32) (result i32))) (global $r (mut i32) (global.get $r$asm2wasm$import)) - (global $s (mut i32) (global.get $s$asm2wasm$import)) (global $v (mut i32) (i32.const 0)) - (global $w (mut i32) (i32.const 0)) (global $K (mut i32) (i32.const 0)) (export "__growWasmMemory" (func $__growWasmMemory)) (export "_free" (func $fb)) @@ -8986,13 +8983,8 @@ (i32.eqz (global.get $v) ) - (block - (global.set $v - (local.get $0) - ) - (global.set $w - (local.get $1) - ) + (global.set $v + (local.get $0) ) ) ) @@ -9024,9 +9016,6 @@ (global.set $r (local.get $0) ) - (global.set $s - (local.get $1) - ) ) (func $nb (; has Stack IR ;) (param $0 i32) (result i32) (call $ja diff --git a/test/passes/O1.txt b/test/passes/O1.txt index f8bad3bb6..11352bab2 100644 --- a/test/passes/O1.txt +++ b/test/passes/O1.txt @@ -1,12 +1,8 @@ (module (type $none_=>_i32 (func (result i32))) (memory $0 1 1) - (global $global$0 (mut i32) (i32.const 10)) (export "foo" (func $0)) (func $0 (result i32) - (global.set $global$0 - (i32.const 0) - ) (i32.load align=1 (i32.const 4) ) diff --git a/test/passes/O4_disable-bulk-memory.txt b/test/passes/O4_disable-bulk-memory.txt index 71fcdf9e8..78c130ffb 100644 --- a/test/passes/O4_disable-bulk-memory.txt +++ b/test/passes/O4_disable-bulk-memory.txt @@ -1,11 +1,7 @@ (module (type $none_=>_none (func)) - (global $global$0 (mut i32) (i32.const 10)) (export "func_59_invoker" (func $0)) (func $0 (; has Stack IR ;) - (global.set $global$0 - (i32.const 0) - ) (unreachable) ) ) diff --git a/test/passes/simplify-globals-optimizing_enable-mutable-globals.txt b/test/passes/simplify-globals-optimizing_enable-mutable-globals.txt index e026f01e8..2876b4f37 100644 --- a/test/passes/simplify-globals-optimizing_enable-mutable-globals.txt +++ b/test/passes/simplify-globals-optimizing_enable-mutable-globals.txt @@ -39,11 +39,9 @@ (module (type $none_=>_none (func)) (import "env" "global-1" (global $g1 i32)) - (global $g2 (mut i32) (global.get $g1)) + (global $g2 i32 (global.get $g1)) (func $foo - (global.set $g2 - (unreachable) - ) + (nop) ) ) (module @@ -134,7 +132,7 @@ (module (type $i32_=>_i32 (func (param i32) (result i32))) (global $g1 (mut i32) (i32.const 1)) - (global $g2 (mut i32) (i32.const 1)) + (global $g2 i32 (i32.const 1)) (func $no (param $x i32) (result i32) (global.set $g1 (i32.const 100) @@ -159,9 +157,6 @@ (global.set $g1 (i32.const 100) ) - (global.set $g2 - (local.get $0) - ) (i32.const 100) ) ) diff --git a/test/passes/simplify-globals_all-features.txt b/test/passes/simplify-globals_all-features.txt index d52c74cc4..c851d67b5 100644 --- a/test/passes/simplify-globals_all-features.txt +++ b/test/passes/simplify-globals_all-features.txt @@ -39,11 +39,9 @@ (module (type $none_=>_none (func)) (import "env" "global-1" (global $g1 i32)) - (global $g2 (mut i32) (global.get $g1)) + (global $g2 i32 (global.get $g1)) (func $foo - (global.set $g2 - (unreachable) - ) + (nop) ) ) (module @@ -180,7 +178,7 @@ (module (type $i32_=>_i32 (func (param i32) (result i32))) (global $g1 (mut i32) (i32.const 1)) - (global $g2 (mut i32) (i32.const 1)) + (global $g2 i32 (i32.const 1)) (func $no (param $x i32) (result i32) (global.set $g1 (i32.const 100) @@ -205,9 +203,7 @@ (global.set $g1 (i32.const 100) ) - (global.set $g2 - (local.get $x) - ) + (nop) (i32.const 100) ) ) @@ -230,3 +226,10 @@ ) ) ) +(module + (type $none_=>_none (func)) + (global $write-only i32 (i32.const 1)) + (func $foo + (nop) + ) +) diff --git a/test/passes/simplify-globals_all-features.wast b/test/passes/simplify-globals_all-features.wast index f7470f959..8c93d1390 100644 --- a/test/passes/simplify-globals_all-features.wast +++ b/test/passes/simplify-globals_all-features.wast @@ -139,3 +139,10 @@ (drop (global.get $g3)) ) ) +;; Global is used by `set` but never `get` can be eliminated. +(module + (global $write-only (mut i32) (i32.const 1)) + (func $foo + (global.set $write-only (i32.const 2)) + ) +) diff --git a/test/wasm2js/global_i64.2asm.js.opt b/test/wasm2js/global_i64.2asm.js.opt index f4bbd539f..d57e44121 100644 --- a/test/wasm2js/global_i64.2asm.js.opt +++ b/test/wasm2js/global_i64.2asm.js.opt @@ -20,11 +20,8 @@ function asmFunc(global, env, buffer) { var abort = env.abort; var nan = global.NaN; var infinity = global.Infinity; - var f = -1412567121; - var f$hi = 305419896; function $1() { - f = 1432778632; - f$hi = 287454020; + } var FUNCTION_TABLE = []; |