Skip to content

Commit 8d3bc3f

Browse files
authored
SimplifyGlobals: Constant-propagate constant values of immutable globals (#2234)
1 parent 9bdf713 commit 8d3bc3f

9 files changed

Lines changed: 138 additions & 12 deletions

src/passes/SimplifyGlobals.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
// globals immutable.
2222
// * If an immutable global is a copy of another, use the earlier one,
2323
// to allow removal of the copies later.
24+
// * Apply the constant values of immutable globals.
2425
//
2526

2627
#include <atomic>
2728

29+
#include "ir/utils.h"
2830
#include "pass.h"
2931
#include "wasm.h"
3032

@@ -54,6 +56,7 @@ struct GlobalUseScanner : public WalkerPass<PostWalker<GlobalUseScanner>> {
5456
};
5557

5658
using NameNameMap = std::map<Name, Name>;
59+
using NameSet = std::set<Name>;
5760

5861
struct GlobalUseModifier : public WalkerPass<PostWalker<GlobalUseModifier>> {
5962
bool isFunctionParallel() override { return true; }
@@ -76,6 +79,29 @@ struct GlobalUseModifier : public WalkerPass<PostWalker<GlobalUseModifier>> {
7679
NameNameMap* copiedParentMap;
7780
};
7881

82+
struct ConstantGlobalApplier
83+
: public WalkerPass<PostWalker<ConstantGlobalApplier>> {
84+
bool isFunctionParallel() override { return true; }
85+
86+
ConstantGlobalApplier(NameSet* constantGlobals)
87+
: constantGlobals(constantGlobals) {}
88+
89+
ConstantGlobalApplier* create() override {
90+
return new ConstantGlobalApplier(constantGlobals);
91+
}
92+
93+
void visitGlobalGet(GlobalGet* curr) {
94+
if (constantGlobals->count(curr->name)) {
95+
auto* global = getModule()->getGlobal(curr->name);
96+
assert(global->init->is<Const>());
97+
replaceCurrent(ExpressionManipulator::copy(global->init, *getModule()));
98+
}
99+
}
100+
101+
private:
102+
NameSet* constantGlobals;
103+
};
104+
79105
} // anonymous namespace
80106

81107
struct SimplifyGlobals : public Pass {
@@ -135,6 +161,23 @@ struct SimplifyGlobals : public Pass {
135161
subRunner.add<GlobalUseModifier>(&copiedParentMap);
136162
subRunner.run();
137163
}
164+
// If any immutable globals have constant values, we can just apply them
165+
// (the global itself will be removed by another pass, as it/ won't have
166+
// any uses).
167+
NameSet constantGlobals;
168+
for (auto& global : module->globals) {
169+
if (!global->mutable_ && !global->imported() &&
170+
global->init->is<Const>()) {
171+
constantGlobals.insert(global->name);
172+
}
173+
}
174+
if (!constantGlobals.empty()) {
175+
PassRunner subRunner(module, runner->options);
176+
subRunner.add<ConstantGlobalApplier>(&constantGlobals);
177+
subRunner.run();
178+
}
179+
// TODO a mutable global's initial value can be applied to another global
180+
// after it, as the mutable one can't mutate during instance startup
138181
}
139182
};
140183

test/min.fromasm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
(import "env" "memory" (memory $memory 256 256))
33
(data (global.get $__memory_base) "min.asm.js")
44
(import "env" "__memory_base" (global $__memory_base i32))
5-
(global $M i32 (i32.const 0))
65
(export "floats" (func $floats))
76
(export "getTempRet0" (func $ub))
87
(export "neg" (func $neg))
@@ -35,6 +34,6 @@
3534
(drop
3635
(call $ub)
3736
)
38-
(global.get $M)
37+
(i32.const 0)
3938
)
4039
)

test/min.fromasm.clamp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
(import "env" "memory" (memory $memory 256 256))
33
(data (global.get $__memory_base) "min.asm.js")
44
(import "env" "__memory_base" (global $__memory_base i32))
5-
(global $M i32 (i32.const 0))
65
(export "floats" (func $floats))
76
(export "getTempRet0" (func $ub))
87
(export "neg" (func $neg))
@@ -35,6 +34,6 @@
3534
(drop
3635
(call $ub)
3736
)
38-
(global.get $M)
37+
(i32.const 0)
3938
)
4039
)

test/min.fromasm.imprecise

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(module
22
(import "env" "memory" (memory $memory 256 256))
3-
(global $M i32 (i32.const 0))
43
(export "floats" (func $floats))
54
(export "getTempRet0" (func $ub))
65
(export "neg" (func $neg))
@@ -33,6 +32,6 @@
3332
(drop
3433
(call $ub)
3534
)
36-
(global.get $M)
35+
(i32.const 0)
3736
)
3837
)

test/passes/simplify-globals_enable-mutable-globals.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,62 @@
5151
(global $g2 (mut i32) (global.get $g1))
5252
(export "global-2" (global $g2))
5353
)
54+
(module
55+
(type $FUNCSIG$v (func))
56+
(global $g1 i32 (i32.const 1))
57+
(global $g2 i32 (global.get $g1))
58+
(global $g3 f64 (f64.const -3.4))
59+
(global $g4 f64 (f64.const -2.8))
60+
(global $g5 i32 (i32.const 2))
61+
(global $g6 i32 (global.get $g5))
62+
(global $g7 i32 (i32.const 3))
63+
(global $g8 i32 (global.get $g7))
64+
(global $g9 i32 (i32.const 4))
65+
(global $ga (mut i32) (global.get $g9))
66+
(global $gb (mut i32) (i32.const 5))
67+
(global $gc i32 (global.get $gb))
68+
(func $foo (; 0 ;) (type $FUNCSIG$v)
69+
(drop
70+
(i32.const 1)
71+
)
72+
(drop
73+
(i32.const 1)
74+
)
75+
(drop
76+
(f64.const -3.4)
77+
)
78+
(drop
79+
(f64.const -2.8)
80+
)
81+
(drop
82+
(i32.const 2)
83+
)
84+
(drop
85+
(i32.const 2)
86+
)
87+
(drop
88+
(i32.const 3)
89+
)
90+
(drop
91+
(i32.const 3)
92+
)
93+
(drop
94+
(i32.const 4)
95+
)
96+
(drop
97+
(global.get $ga)
98+
)
99+
(drop
100+
(global.get $gb)
101+
)
102+
(drop
103+
(global.get $gc)
104+
)
105+
(global.set $ga
106+
(i32.const 6)
107+
)
108+
(global.set $gb
109+
(i32.const 7)
110+
)
111+
)
112+
)

test/passes/simplify-globals_enable-mutable-globals.wast

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,34 @@
3434
(global $g2 (mut i32) (global.get $g1))
3535
(export "global-2" (global $g2))
3636
)
37+
(module
38+
(global $g1 i32 (i32.const 1))
39+
(global $g2 i32 (global.get $g1))
40+
(global $g3 f64 (f64.const -3.4))
41+
(global $g4 (mut f64) (f64.const -2.8))
42+
(global $g5 i32 (i32.const 2))
43+
(global $g6 (mut i32) (global.get $g5))
44+
(global $g7 (mut i32) (i32.const 3))
45+
(global $g8 i32 (global.get $g7))
46+
(global $g9 i32 (i32.const 4))
47+
(global $ga (mut i32) (global.get $g9))
48+
(global $gb (mut i32) (i32.const 5))
49+
(global $gc i32 (global.get $gb))
50+
(func $foo
51+
(drop (global.get $g1))
52+
(drop (global.get $g2))
53+
(drop (global.get $g3))
54+
(drop (global.get $g4))
55+
(drop (global.get $g5))
56+
(drop (global.get $g6))
57+
(drop (global.get $g7))
58+
(drop (global.get $g8))
59+
(drop (global.get $g9))
60+
(drop (global.get $ga))
61+
(drop (global.get $gb))
62+
(drop (global.get $gc))
63+
(global.set $ga (i32.const 6))
64+
(global.set $gb (i32.const 7))
65+
)
66+
)
3767

test/unit.fromasm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
2525
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2626
(global $Int (mut i32) (i32.const 0))
27-
(global $Double f64 (f64.const 0))
2827
(global $nonZero (mut i32) (i32.const 1337))
2928
(global $exportedNumber i32 (i32.const 42))
3029
(export "big_negative" (func $big_negative))
@@ -92,7 +91,7 @@
9291
)
9392
(if
9493
(f64.gt
95-
(global.get $Double)
94+
(f64.const 0)
9695
(f64.const 0)
9796
)
9897
(return

test/unit.fromasm.clamp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
(import "env" "emscripten_log" (func $emscripten_log))
2323
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2424
(global $Int (mut i32) (i32.const 0))
25-
(global $Double f64 (f64.const 0))
2625
(global $nonZero (mut i32) (i32.const 1337))
2726
(global $exportedNumber i32 (i32.const 42))
2827
(export "big_negative" (func $big_negative))
@@ -90,7 +89,7 @@
9089
)
9190
(if
9291
(f64.gt
93-
(global.get $Double)
92+
(f64.const 0)
9493
(f64.const 0)
9594
)
9695
(return

test/unit.fromasm.imprecise

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
(import "env" "emscripten_log" (func $emscripten_log))
2121
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2222
(global $Int (mut i32) (i32.const 0))
23-
(global $Double f64 (f64.const 0))
2423
(global $nonZero (mut i32) (i32.const 1337))
2524
(global $exportedNumber i32 (i32.const 42))
2625
(export "big_negative" (func $big_negative))
@@ -88,7 +87,7 @@
8887
)
8988
(if
9089
(f64.gt
91-
(global.get $Double)
90+
(f64.const 0)
9291
(f64.const 0)
9392
)
9493
(return

0 commit comments

Comments
 (0)