-
Notifications
You must be signed in to change notification settings - Fork 860
GlobalStructInference: Optimize gets of immutable global struct data #8005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29c1c05
start
kripken 3239afb
start
kripken 3ab6d67
work
kripken 1759b36
work
kripken 8562d45
done
kripken 9887800
format
kripken 900e75f
format
kripken 36bfc96
format
kripken e914ce4
fix
kripken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| ;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. | ||
| ;; RUN: foreach %s %t wasm-opt --gsi -all --closed-world -S -o - | filecheck %s | ||
|
|
||
| ;; Non-type-based optimizations in --gsi | ||
|
|
||
| ;; Create an immutable vtable in an immutable global, which we can optimize | ||
| ;; with. | ||
| (module | ||
| ;; CHECK: (type $vtable (struct (field funcref))) | ||
| (type $vtable (struct funcref)) | ||
|
|
||
| ;; CHECK: (type $1 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported funcref)) | ||
| (import "a" "b" (global $imported funcref)) | ||
|
|
||
| ;; CHECK: (global $vtable (ref $vtable) (struct.new $vtable | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: )) | ||
| (global $vtable (ref $vtable) | ||
| (struct.new $vtable | ||
| (global.get $imported) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $test (type $1) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| ;; This get reads $import. | ||
| (drop | ||
| (struct.get $vtable 0 | ||
| (global.get $vtable) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; As above, but the global is not immutable, so we cannot optimize. | ||
| (module | ||
| ;; CHECK: (type $vtable (struct (field funcref))) | ||
| (type $vtable (struct funcref)) | ||
|
|
||
| ;; CHECK: (type $1 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported funcref)) | ||
| (import "a" "b" (global $imported funcref)) | ||
|
|
||
| ;; CHECK: (global $vtable (mut (ref $vtable)) (struct.new $vtable | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: )) | ||
| (global $vtable (mut (ref $vtable)) | ||
| (struct.new $vtable | ||
| (global.get $imported) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $test (type $1) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (struct.get $vtable 0 | ||
| ;; CHECK-NEXT: (global.get $vtable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| (drop | ||
| (struct.get $vtable 0 | ||
| (global.get $vtable) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; As above, but the global does not contain a struct.new, so we cannot | ||
| ;; optimize. | ||
| (module | ||
| ;; CHECK: (type $vtable (struct (field funcref))) | ||
| (type $vtable (struct funcref)) | ||
|
|
||
| ;; CHECK: (type $1 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported funcref)) | ||
| (import "a" "b" (global $imported funcref)) | ||
|
|
||
| ;; CHECK: (global $vtable (ref null $vtable) (ref.null none)) | ||
| (global $vtable (ref null $vtable) (ref.null $vtable)) | ||
|
|
||
| ;; CHECK: (func $test (type $1) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (struct.get $vtable 0 | ||
| ;; CHECK-NEXT: (global.get $vtable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| (drop | ||
| (struct.get $vtable 0 | ||
| (global.get $vtable) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; As above, but the value in the struct.new is not constant, so we cannot | ||
| ;; optimize. | ||
| (module | ||
| ;; CHECK: (type $table (struct (field anyref))) | ||
| (type $table (struct anyref)) | ||
|
|
||
| ;; CHECK: (type $1 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported funcref)) | ||
| (import "a" "b" (global $imported funcref)) | ||
|
|
||
| ;; CHECK: (global $table (ref $table) (struct.new $table | ||
| ;; CHECK-NEXT: (struct.new_default $table) | ||
| ;; CHECK-NEXT: )) | ||
| (global $table (ref $table) | ||
| (struct.new $table | ||
| (struct.new_default $table) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $test (type $1) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (struct.get $table 0 | ||
| ;; CHECK-NEXT: (global.get $table) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| (drop | ||
| (struct.get $table 0 | ||
| (global.get $table) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; Test we can optimize a descriptor read. | ||
| (module | ||
| (rec | ||
| ;; CHECK: (rec | ||
| ;; CHECK-NEXT: (type $struct (sub (descriptor $desc (struct)))) | ||
| (type $struct (sub (descriptor $desc (struct)))) | ||
| ;; CHECK: (type $desc (sub (describes $struct (struct)))) | ||
| (type $desc (sub (describes $struct (struct)))) | ||
| ) | ||
|
|
||
| ;; CHECK: (type $2 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported (ref (exact $desc)))) | ||
| (import "a" "b" (global $imported (ref (exact $desc)))) | ||
|
|
||
| ;; CHECK: (global $struct (ref $struct) (struct.new_default $struct | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: )) | ||
| (global $struct (ref $struct) | ||
| (struct.new $struct | ||
| (global.get $imported) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $test (type $2) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| ;; This get reads $import. | ||
| (drop | ||
| (ref.get_desc $struct | ||
| (global.get $struct) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; Check we do not error on a global.get of an imported global. | ||
| (module | ||
| ;; CHECK: (type $vtable (struct (field funcref))) | ||
| (type $vtable (struct funcref)) | ||
|
|
||
| ;; CHECK: (type $1 (func)) | ||
|
|
||
| ;; CHECK: (import "a" "b" (global $imported (ref $vtable))) | ||
| (import "a" "b" (global $imported (ref $vtable))) | ||
|
|
||
| ;; CHECK: (func $test (type $1) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (struct.get $vtable 0 | ||
| ;; CHECK-NEXT: (global.get $imported) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $test | ||
| (drop | ||
| (struct.get $vtable 0 | ||
| (global.get $imported) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be neat (and not too hard, I think) to expand this to support arbitrarily deep paths through struct.new, array.new, and global.get expressions in global initializers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is what the TODO about unnesting is about. (I think we need to unnest, like the other code here? Unless there is a simpler way I am missing.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be fairly straightforward to have a utility that is given the root of some sequence of accessors (kind of like a
gepin LLVM, except not all rolled into one instruction) and then follows the access path as long as it is known to remain constant, returning (a pointer to) the result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that be useful anywhere else, do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'd have to think about that. I saw that meanwhile you uploaded a solution that unnests the intermediate values into globals. The only benefit a path-walking utility would have over that would be fewer new globals to be optimized back out later. A downside would be potentially quadratic behavior, though.