-
Notifications
You must be signed in to change notification settings - Fork 1.7k
x64: Add a smattering of lowerings for shuffle specializations
#5930
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
Changes from all commits
9100445
1616011
30f60cf
5e40d36
36a65c7
29f7e7a
75c9933
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3529,16 +3529,98 @@ | |
|
|
||
| ;; Rules for `shuffle` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
|
||
| ;; Special case for the `punpckhbw` instruction which interleaves the upper | ||
| ;; lanes of the two input registers. | ||
| (rule 4 (lower (shuffle a b (u128_from_immediate 0x1f0f_1e0e_1d0d_1c0c_1b0b_1a0a_1909_1808))) | ||
| ;; Special case the `pshuf{l,h}w` instruction which shuffles four 16-bit | ||
| ;; integers within one value, preserving the other four 16-bit integers in that | ||
| ;; value (either the high or low half). The complicated logic is in the | ||
| ;; extractors here implemented in Rust and note that there's two cases for each | ||
| ;; instruction here to match when either the first or second shuffle operand is | ||
| ;; used. | ||
| (rule 12 (lower (shuffle x y (pshuflw_lhs_imm imm))) | ||
| (x64_pshuflw x imm)) | ||
| (rule 11 (lower (shuffle x y (pshuflw_rhs_imm imm))) | ||
| (x64_pshuflw y imm)) | ||
| (rule 10 (lower (shuffle x y (pshufhw_lhs_imm imm))) | ||
| (x64_pshufhw x imm)) | ||
| (rule 9 (lower (shuffle x y (pshufhw_rhs_imm imm))) | ||
| (x64_pshufhw y imm)) | ||
|
|
||
| (decl pshuflw_lhs_imm (u8) Immediate) | ||
| (extern extractor pshuflw_lhs_imm pshuflw_lhs_imm) | ||
| (decl pshuflw_rhs_imm (u8) Immediate) | ||
| (extern extractor pshuflw_rhs_imm pshuflw_rhs_imm) | ||
| (decl pshufhw_lhs_imm (u8) Immediate) | ||
| (extern extractor pshufhw_lhs_imm pshufhw_lhs_imm) | ||
| (decl pshufhw_rhs_imm (u8) Immediate) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wish the matching logic could be visible here in ISLE but that doesn't really change how I feel about this PR.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I get that it gets rather hairy in the helpers...
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree! I originally was wondering how bad it would be to put this all in ISLE but I ended up concluding that it would end up with even more extractors and not necessarily simplify the overall scheme, so I opted to leave the rules in ISLE but the extraction of immediates in Rust (for better or worse) |
||
| (extern extractor pshufhw_rhs_imm pshufhw_rhs_imm) | ||
|
|
||
| ;; Special case for the `pshufd` instruction which will permute 32-bit values | ||
| ;; within a single register. This is only applicable if the `imm` specified | ||
| ;; selects 32-bit values from either `x` or `y`, but not both. This means | ||
| ;; there's one rule for selecting from `x` and another rule for selecting from | ||
| ;; `y`. | ||
| (rule 8 (lower (shuffle x y (pshufd_lhs_imm imm))) | ||
| (x64_pshufd x imm)) | ||
| (rule 7 (lower (shuffle x y (pshufd_rhs_imm imm))) | ||
| (x64_pshufd y imm)) | ||
|
|
||
| (decl pshufd_lhs_imm (u8) Immediate) | ||
| (extern extractor pshufd_lhs_imm pshufd_lhs_imm) | ||
| (decl pshufd_rhs_imm (u8) Immediate) | ||
| (extern extractor pshufd_rhs_imm pshufd_rhs_imm) | ||
|
|
||
| ;; Special case for i8-level interleaving of upper/low bytes. | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1f0f_1e0e_1d0d_1c0c_1b0b_1a0a_1909_1808))) | ||
| (x64_punpckhbw a b)) | ||
|
|
||
| ;; Special case for the `punpcklbw` instruction which interleaves the lower | ||
| ;; lanes of the two input registers. | ||
| (rule 4 (lower (shuffle a b (u128_from_immediate 0x1707_1606_1505_1404_1303_1202_1101_1000))) | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1707_1606_1505_1404_1303_1202_1101_1000))) | ||
| (x64_punpcklbw a b)) | ||
|
|
||
| ;; Special case for i16-level interleaving of upper/low bytes. | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1f1e_0f0e_1d1c_0d0c_1b1a_0b0a_1918_0908))) | ||
| (x64_punpckhwd a b)) | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1716_0706_1514_0504_1312_0302_1110_0100))) | ||
| (x64_punpcklwd a b)) | ||
|
|
||
| ;; Special case for i32-level interleaving of upper/low bytes. | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1f1e1d1c_0f0e0d0c_1b1a1918_0b0a0908))) | ||
| (x64_punpckhdq a b)) | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x17161514_07060504_13121110_03020100))) | ||
| (x64_punpckldq a b)) | ||
|
|
||
| ;; Special case for i64-level interleaving of upper/low bytes. | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1f1e1d1c1b1a1918_0f0e0d0c0b0a0908))) | ||
| (x64_punpckhqdq a b)) | ||
| (rule 6 (lower (shuffle a b (u128_from_immediate 0x1716151413121110_0706050403020100))) | ||
| (x64_punpcklqdq a b)) | ||
|
|
||
| ;; If the vector shift mask is all 0s then that means the first byte of the | ||
| ;; first operand is broadcast to all bytes. Falling through would load an | ||
| ;; all-zeros constant from a rip-relative location but it should be slightly | ||
| ;; more efficient to execute the `pshufb` here-and-now with an xor'd-to-be-zero | ||
| ;; register. | ||
| (rule 6 (lower (shuffle a _ (u128_from_immediate 0))) | ||
| (x64_pshufb a (xmm_zero $I8X16))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there could be a similar version of this rule if all lanes specify to broadcast the first byte of the second operand. That's, what, if every byte in the mask is 0x10?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! I've been going back and forth a bit on how exhaustive all these rules should be. For example this rule: could also match the I added a bunch of lhs/rhs shuffles here for things like Some of this I've been feeling would be better implemented as a egraph-style optimization. For example if all the shuffles lanes are <16 then the In the end I figured I'd stick to doing the obvious lowerings for each instruction a platform has along with ensuring that some various simd binaries I've been seeing all have all their shuffles special-cased. We can always add more here in the future, and I was mainly hoping that by that point it's more obivous how to solve the problem of "could this shuffle be special-cased if the operands were swapped?" |
||
|
|
||
| ;; Special case for the `shufps` instruction which will select two 32-bit values | ||
| ;; from the first operand and two 32-bit values from the second operand. Note | ||
| ;; that there is a second case here as well for when the operands can be | ||
| ;; swapped. | ||
| ;; | ||
| ;; Note that the priority of this instruction is currently lower than the above | ||
| ;; special cases since `shufps` handles many of them and for now it's | ||
| ;; hypothesized that the dedicated instructions are better than `shufps`. | ||
| ;; Someone with more knowledge about x86 timings should perhaps reorder the | ||
| ;; rules here eventually though. | ||
| (rule 5 (lower (shuffle x y (shufps_imm imm))) | ||
| (x64_shufps x y imm)) | ||
| (rule 4 (lower (shuffle x y (shufps_rev_imm imm))) | ||
| (x64_shufps y x imm)) | ||
|
|
||
| (decl shufps_imm(u8) Immediate) | ||
| (extern extractor shufps_imm shufps_imm) | ||
| (decl shufps_rev_imm(u8) Immediate) | ||
| (extern extractor shufps_rev_imm shufps_rev_imm) | ||
|
|
||
|
|
||
| ;; If `lhs` and `rhs` are the same we can use a single PSHUFB to shuffle the XMM | ||
| ;; register. We statically build `constructed_mask` to zero out any unknown lane | ||
| ;; indices (may not be completely necessary: verification could fail incorrect | ||
|
|
||
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.
I started to wonder how many comparisons need to be made before one can emit a shuffle lowering: I think in the end it is worth it to spend a bit more time finding the right instruction here but I wonder if there is quicker way to do so. (Just wondering, no action neeeded).
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.
Same! My thinking though was that there's really not all that many
shuffleinstructions in a program so ifshuffleis 100x slower to translate thanloadit's probably not the end of the world. That being said I do think that these can be slightly optimized where instead of having an extractor-per-immediate we could have one extractor that returns anenumof all the possible shuffle instructions that could be used which is then pattern-matched on in ISLE. I'm not sure if it would be that much faster but it would prevent bouncing back and forth between generated code andisle.rsif it becomes an issue.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.
I was going to mention that V8 does this "match once, then emit" through separate opcodes like you mention below:
I like the
enumidea for some point in the future.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.
Somewhere in v8 though something is translating from the wasm shuffle to each individual
kX64*instruction though, right? I would naively expect that the translation there is on the same order-of-magnitude of complexity/time/etc for what these ISLE lowerings are generating.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.
Since ISLE will combine all the
u128_from_immediatecalls in the rules at priority 6 below, many of these rules are really cheap to evaluate together. This isn't as bad as it looks. 😆The
enumapproach would let ISLE do the same collapsing for a bunch more of these rules, so yeah, that should be good. Also, all the rules that would then use the same extractor but match on different returned enum variants should then be placed at the same priority if there aren't other rules at priorities in between.