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
6 changes: 4 additions & 2 deletions src/passes/SimplifyGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

As a separate cleanup it might be nice to use the template parameter on FindAllPointers in the type of its list to avoid the need for a cast below.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try, though I have a vague memory of that not working for a sad C++ey reason...

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);
}
}
};
Expand Down
27 changes: 27 additions & 0 deletions test/lit/passes/simplify-globals-nested.wast
Original file line number Diff line number Diff line change
@@ -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)
))
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

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

I'm sure this is already tested elsewhere, but it might be nice to see that a global.get where the value cannot be propagated (because it is a GC data allocation, for instance) does not interfere with propagation of the other global.gets.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by "does not interfere"? Like if it is in the middle between them?

Copy link
Member

Choose a reason for hiding this comment

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

Not in the middle, necessarily, but yeah, if this struct had another field initialized with another global.get, but that one couldn't be propagated.

)