-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Simplify ireduce of modular operators, when it mean strictly fewer instructions #7719
Merged
Conversation
This file contains 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
github-actions
bot
added
cranelift
Issues related to the Cranelift code generator
isle
Related to the ISLE domain-specific language
labels
Dec 21, 2023
Subscribe to Label Action
This issue or pull request has been labeled: "cranelift", "isle"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
fitzgen
approved these changes
Jan 2, 2024
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.
Thanks!
github-merge-queue
bot
removed this pull request from the merge queue due to failed status checks
Jan 2, 2024
A bisection I just ran points to this PR as the cause of #7874 (cc'ing here to close the loop) |
cfallin
added a commit
to cfallin/wasmtime
that referenced
this pull request
Feb 28, 2024
…dance after bytecodealliance#7999. While debugging bytecodealliance#7999, we learned of a possible, extremely subtle, interaction between the design of our ISLE mid-end prelude and matching rules with a certain structure. Because a `Value` represents an entire eclass, separate left-hand sides (as in helper rules or if-let clauses) that match on the value may match against different enodes returned by the multi-match extractor's iterator. We then may infer some property of one enode, another property of another enode, and perform a rewrite that is only valid if both of those matches are on the same enode. The precise distinction is whether it is a property of the *value* -- e.g., nonzero, or even, or within a range -- or a property of the *operation* and matched subpieces. The former is fine, the latter runs into trouble. We found that bytecodealliance#7719 added a helper that determined whether a value "was a certain operator" -- actually, had any enode of a certain operator -- and separately, matched "the operator" and extracted its opcode and parameters (actually, *any* binary operator). The first half can match an opcode we support simplifying, and the second half can get the arguments and `op` and blindly use them in the rewrite. This PR adds new guidance to avoid complex helpers and be aware of multi-matching behavior, preferring to write patterns directly (as the fix in bytecodealliance#8005 does) instead. Longer-term, we also have other ideas, e.g. @jameysharp's suggestion to disallow at-patterns on multi-extractors in left hand sides to reduce the chance of hitting this footgun.
github-merge-queue bot
pushed a commit
that referenced
this pull request
Feb 28, 2024
…dance after #7999. (#8015) While debugging #7999, we learned of a possible, extremely subtle, interaction between the design of our ISLE mid-end prelude and matching rules with a certain structure. Because a `Value` represents an entire eclass, separate left-hand sides (as in helper rules or if-let clauses) that match on the value may match against different enodes returned by the multi-match extractor's iterator. We then may infer some property of one enode, another property of another enode, and perform a rewrite that is only valid if both of those matches are on the same enode. The precise distinction is whether it is a property of the *value* -- e.g., nonzero, or even, or within a range -- or a property of the *operation* and matched subpieces. The former is fine, the latter runs into trouble. We found that #7719 added a helper that determined whether a value "was a certain operator" -- actually, had any enode of a certain operator -- and separately, matched "the operator" and extracted its opcode and parameters (actually, *any* binary operator). The first half can match an opcode we support simplifying, and the second half can get the arguments and `op` and blindly use them in the rewrite. This PR adds new guidance to avoid complex helpers and be aware of multi-matching behavior, preferring to write patterns directly (as the fix in #8005 does) instead. Longer-term, we also have other ideas, e.g. @jameysharp's suggestion to disallow at-patterns on multi-extractors in left hand sides to reduce the chance of hitting this footgun.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is a follow-up to fitzgen's comment in #7693 (comment)
The version that was removed from that PR had some concerns because it added things to the egraph that might not be useful, since it added more instructions, potentially: going from
(small)(x OP y)
to(small)x OP (small)y
isn't always a win since it's one more CLIF instruction.This PR addresses that concern by restricting the pattern only to places where it's going to be strictly fewer total instructions. It does that by only applying when all the arguments to the inner operator are either extended or constants. In those cases, pushing the
ireduce
into the arguments will collapse with the instruction that's already there -- either because extend-then-reduce back is a nop, because cprop will simplify it, or thanks to #7711.The particular motivating example for this -- which is why the
mulhi
pattern is in here too -- is Rust'su64::widening_mul
. LLVM doesn't have an intrinsic for wide multiplication, so the way it's often written is by casting the arguments to a wider type, doing the multiplication, then reducing back again. But that means the obvious translation in CLIF ends up with an expensive 128-bit multiplication. Collapsing that down to a 64-bitimul
+umulhi
is much cheaper.