Skip to content

Commit 3e33605

Browse files
authored
SimplifyGlobals: Do not switch a get to use a global of another type (#6605)
If we wanted to switch types in such cases we'd need to refinalize (which is likely worth doing, though other passes should refine globals anyhow).
1 parent 5d64afa commit 3e33605

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/passes/SimplifyGlobals.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,14 @@ struct GlobalUseModifier : public WalkerPass<PostWalker<GlobalUseModifier>> {
317317
void visitGlobalGet(GlobalGet* curr) {
318318
auto iter = copiedParentMap->find(curr->name);
319319
if (iter != copiedParentMap->end()) {
320-
curr->name = iter->second;
320+
auto original = iter->second;
321+
// Only apply this optimization if the global we are switching to has the
322+
// right type for us.
323+
// TODO: We could also allow it to be more refined, but would then need to
324+
// refinalize.
325+
if (getModule()->getGlobal(original)->type == curr->type) {
326+
curr->name = original;
327+
}
321328
}
322329
}
323330

test/lit/passes/simplify-globals-gc.wast

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,27 @@
3131
)
3232
)
3333

34+
(module
35+
;; CHECK: (type $struct (struct ))
36+
(type $struct (struct ))
37+
38+
;; CHECK: (type $1 (func (result anyref)))
39+
40+
;; CHECK: (global $a (ref $struct) (struct.new_default $struct))
41+
(global $a (ref $struct) (struct.new_default $struct))
42+
;; CHECK: (global $b (ref $struct) (global.get $a))
43+
(global $b (ref $struct) (global.get $a))
44+
;; CHECK: (global $c (ref null $struct) (global.get $a))
45+
(global $c (ref null $struct) (global.get $a))
46+
47+
;; CHECK: (func $get-c (type $1) (result anyref)
48+
;; CHECK-NEXT: (global.get $c)
49+
;; CHECK-NEXT: )
50+
(func $get-c (result anyref)
51+
;; $c has a less-refined type than the other two. We do not switch this to
52+
;; get from either $a or $b because of that, but we could if we also
53+
;; refinalized TODO
54+
(global.get $c)
55+
)
56+
)
57+

0 commit comments

Comments
 (0)