Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test
  • Loading branch information
kripken committed Feb 8, 2024
commit eb9128a5197d0c32473bff81e47c2c09a3781c02
5 changes: 3 additions & 2 deletions src/passes/SimplifyGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,12 @@ struct SimplifyGlobals : public Pass {
// go, as well as applying them where possible.
for (auto& global : module->globals) {
if (!global->imported()) {
// Apply globals to this value, which may turn it into a constant we can
// further propagate, or it may already have been one.
applyGlobals(global->init);
Comment on lines +676 to +678
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a bug fix. Which test case does it affect?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not a bugfix, see the second paragraph above: This makes us slightly more efficient in SimplifyGlobals, but we always did it.

Copy link
Member Author

Choose a reason for hiding this comment

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

if (Properties::isConstantExpression(global->init)) {
constantGlobals[global->name] =
getLiteralsFromConstExpression(global->init);
} else {
applyGlobals(global->init);
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions test/lit/passes/propagate-globals-globally.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
;; The global constants should be applied to other globals.

(module
;; CHECK: (type $0 (func))
;; CHECK: (type $struct (struct (field stringref) (field stringref)))
(type $struct (struct stringref stringref))

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

;; CHECK: (global $A i32 (i32.const 42))
(global $A i32 (i32.const 42))
Expand All @@ -15,21 +18,31 @@
(global $B i32 (global.get $A))

;; CHECK: (global $C i32 (i32.add
;; CHECK-NEXT: (global.get $B)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: ))
(global $C i32 (i32.add
;; Both of these can be optimized, including $B which reads from $A.
(global.get $B)
(global.get $A)
))

;; CHECK: (global $D (ref string) (string.const "foo"))
(global $D (ref string) (string.const "foo"))

;; CHECK: (global $E (ref string) (string.const "foo"))
(global $E (ref string) (global.get $D))
;; CHECK: (global $E (ref string) (string.const "bar"))
(global $E (ref string) (string.const "bar"))

;; CHECK: (global $G (ref $struct) (struct.new $struct
;; CHECK-NEXT: (string.const "foo")
;; CHECK-NEXT: (string.const "bar")
;; CHECK-NEXT: ))
(global $G (ref $struct) (struct.new $struct
(global.get $D)
(global.get $E)
))

;; CHECK: (func $test (type $0)
;; CHECK: (func $test (type $1)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $A)
;; CHECK-NEXT: )
Expand Down
9 changes: 7 additions & 2 deletions test/lit/passes/simplify-globals-nested.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
;; 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: (type $struct (struct (field i32) (field i32) (field i32)))
(type $struct (struct i32 i32 i32))

;; CHECK: (import "x" "y" (global $no i32))
(import "x" "y" (global $no i32))

;; CHECK: (global $a i32 (i32.const 42))
(global $a i32 (i32.const 42))
Expand All @@ -17,10 +20,12 @@

;; CHECK: (global $struct (ref $struct) (struct.new $struct
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: (global.get $no)
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: ))
(global $struct (ref $struct) (struct.new $struct
(global.get $a)
(global.get $no) ;; the middle item cannot be optimized
(global.get $b)
))
)
Expand Down