[Cranelift] add select simplification rules#12742
[Cranelift] add select simplification rules#12742myunbin wants to merge 3 commits intobytecodealliance:mainfrom
Conversation
Subscribe to Label ActionDetailsThis 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 |
|
Thanks for this! Most of these seem like reasonable rewrites to add. I do wonder about the origin and use-cases for some of them, though; for example,
seems "oddly specific" -- it is a kind of generalization / the reverse of conditional constant propagation, but it only works for the value In general we have the principle that rewrites are cheap (due to the ISLE compiler's LHS combining) and so it's fine to add a bunch of rewrite rules for corner-cases, as long as they rewrite in the correct direction. But they do carry a nonzero cost (code-size complexity, generated Rust size and Cranelift compile time, a few extra checks and branches during the mid-end pass) so it's worth ensuring that they are "actually real" in some sense. So: did you find this LHS in a program? I see in #10979 @bongjunj describes your project as using LLVM optimizers as a source; but per my reading at least, the optimizer gives you the right-hand side but it's unclear how you're harvesting left-hand sides. Could you clarify? |
And to be clear, because it was left implicit above, we can express the general form of this rule in isle, and that seems like a good rule to add. |
|
Thanks for the thoughtful question.
This exact LHS was not mined directly from an application program pattern. It started from an LLVM unit test, specifically this test case: Because this test is anchored on the fixed constant -2147483648, we summarized it to: Your point is very helpful, and your proposed commit looks excellent. We’d be happy to adopt it. Additionally: I updated the latest commit to apply the generalized version you suggested. Thank you! cc @bongjunj |
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
This PR adds a batch of select simplification rules.
Added rewrites:
(x == y) ? x : y => y(x | (y != z)) ? y : z => y(x == y) ? x : (x | y) => (x | y)(x != y) ? (x & y) : y => (x & y)(x <_u y) ? 1 : (x == y) => (x <=_u y)(x < y) ? 1 : (x > y) => (x != y)(x ? 1 : (x | y)) => (x ? 1 : y)if x == 1 then (y - 1) else (y - x) => (y - x)select(cond, eq(z, y), eq(p, y)) => eq(y, select(cond, z, p))These rewrites come from a synthesis project with @bongjunj (details: #10979 ).
I grouped these into one PR to reduce overhead from many small PRs.
If you'd prefer splitting by rule family, I'm happy to do that.
cc @bongjunj