diff options
-rw-r--r-- | src/passes/SimplifyGlobals.cpp | 6 | ||||
-rw-r--r-- | test/lit/passes/simplify-globals-nested.wast | 27 |
2 files changed, 31 insertions, 2 deletions
diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 3baec4409..323ebd6a7 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -41,6 +41,7 @@ #include <atomic> #include "ir/effects.h" +#include "ir/find_all.h" #include "ir/linear-execution.h" #include "ir/properties.h" #include "ir/utils.h" @@ -659,10 +660,11 @@ struct SimplifyGlobals : public Pass { // This is the init of a passive segment, which is null. return; } - if (auto* get = init->dynCast<GlobalGet>()) { + for (auto** getp : FindAllPointers<GlobalGet>(init).list) { + auto* get = (*getp)->cast<GlobalGet>(); auto iter = constantGlobals.find(get->name); if (iter != constantGlobals.end()) { - init = builder.makeConstantExpression(iter->second); + *getp = builder.makeConstantExpression(iter->second); } } }; diff --git a/test/lit/passes/simplify-globals-nested.wast b/test/lit/passes/simplify-globals-nested.wast new file mode 100644 index 000000000..925d71161 --- /dev/null +++ b/test/lit/passes/simplify-globals-nested.wast @@ -0,0 +1,27 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up. + +;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s + +;; Test that we propagate globals into nested children of other globals. + +(module + ;; CHECK: (type $struct (struct (field i32) (field i32))) + (type $struct (struct i32 i32)) + + ;; CHECK: (global $a i32 (i32.const 42)) + (global $a i32 (i32.const 42)) + + ;; CHECK: (global $b i32 (i32.const 1337)) + (global $b i32 (i32.const 1337)) + + ;; CHECK: (global $struct (ref $struct) (struct.new $struct + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: (i32.const 1337) + ;; CHECK-NEXT: )) + (global $struct (ref $struct) (struct.new $struct + (global.get $a) + (global.get $b) + )) +) + |