Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/passes/SimplifyGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,14 @@ struct GlobalUseModifier : public WalkerPass<PostWalker<GlobalUseModifier>> {
void visitGlobalGet(GlobalGet* curr) {
auto iter = copiedParentMap->find(curr->name);
if (iter != copiedParentMap->end()) {
curr->name = iter->second;
auto original = iter->second;
// Only apply this optimization if the global we are switching to has the
// right type for us.
// TODO: We could also allow it to be more refined, but would then need to
// refinalize.
if (getModule()->getGlobal(original)->type == curr->type) {
curr->name = original;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/lit/passes/simplify-globals-gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@
)
)

(module
;; CHECK: (type $struct (struct ))
(type $struct (struct ))

;; CHECK: (type $1 (func (result anyref)))

;; CHECK: (global $a (ref $struct) (struct.new_default $struct))
(global $a (ref $struct) (struct.new_default $struct))
;; CHECK: (global $b (ref $struct) (global.get $a))
(global $b (ref $struct) (global.get $a))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the role of $b in this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be comprehensive. $c is equal to both $a and $b but in different ways (it reads $a, but it has equal content to $b). I don't think we optimize both cases atm but this would handle things if we ever did.

;; CHECK: (global $c (ref null $struct) (global.get $a))
(global $c (ref null $struct) (global.get $a))

;; CHECK: (func $get-c (type $1) (result anyref)
;; CHECK-NEXT: (global.get $c)
;; CHECK-NEXT: )
(func $get-c (result anyref)
;; $c has a less-refined type than the other two. We do not switch this to
;; get from either $a or $b because of that, but we could if we also
;; refinalized TODO
(global.get $c)
)
)