myunbin opened PR #12948 from myunbin:add-rules-040326 to bytecodealliance:main:
This PR adds several simplification rules:
arithmetic.isle
(x - y) == x --> y == 0;; (x + y) == y --> x == 0-x == -y --> x == y-((-y) * x) --> x * yireduce(xext(a) + xext(b)) --> a + bireduce(xext(a) - xext(b)) --> a - bmax(x, y) >= x --> 1x >= min(x, y) --> 1
bitops.isle
(x & ~y) == ~y --> (x | y) == -1x >= (x & y) --> 1and its dual
myunbin requested cfallin for a review on PR #12948.
myunbin requested wasmtime-compiler-reviewers for a review on PR #12948.
myunbin edited PR #12948:
This PR adds several simplification rules:
arithmetic.isle
(x - y) == x --> y == 0(x + y) == y --> x == 0-x == -y --> x == y-((-y) * x) --> x * yireduce(xext(a) + xext(b)) --> a + bireduce(xext(a) - xext(b)) --> a - bmax(x, y) >= x --> 1x >= min(x, y) --> 1
bitops.isle
(x & ~y) == ~y --> (x | y) == -1x >= (x & y) --> 1and its dual
github-actions[bot] added the label isle on PR #12948.
github-actions[bot] added the label cranelift on PR #12948.
github-actions[bot] commented on PR #12948:
Subscribe to Label Action
cc @cfallin, @fitzgen
<details>
This issue or pull request has been labeled: "cranelift", "isle"Thus the following users have been cc'd because of the following labels:
- cfallin: isle
- fitzgen: isle
To subscribe or unsubscribe from this label, edit the <code>.github/subscribe-to-label.json</code> configuration file.
Learn more.
</details>
cfallin submitted PR review:
Thanks! These all look right to me; just one comment about a potential redundancy below. Happy to merge once that's resolved.
cfallin created PR review comment:
Isn't this rule the same as the rule two lines up, with
xandyswapped? In general I like that we're doing commutativity explicitly here but I think that in this case it's actually just a redundant match -- we'll match the same patterns again with renamed bindings, so the effect is that we'll always add twoiadds to the egraph (which is not what we want).Same pattern in the rest of this rule's variants I believe.
fitzgen submitted PR review.
fitzgen created PR review comment:
I would have expected the overlap checker to find true redundancies, do you know why it isn't in this case?
cfallin submitted PR review.
cfallin created PR review comment:
These are in the mid-end so
simplifyis a multi-constructor (e-graphs!) -- we disable the overlap checker completely for this case.I guess in theory a static check might still be possible but it would have to be something like: LHSes both fire and RHSes are exactly the same, which is much harder (need to see into external ctor semantics in the general case).
cfallin submitted PR review.
cfallin created PR review comment:
(The driver should dedup the same
Valuecoming back twice though -- see here)
myunbin updated PR #12948.
myunbin submitted PR review.
myunbin created PR review comment:
Thanks for pointing this out. I’ve fixed the duplicated rules.
bongjunj submitted PR review.
bongjunj created PR review comment:
I think the "redundancy" is somewhat confusing to me, and I think it'd better if we can clarify here.
The README says that commutative cases are better explicitly coded, not assumed by the compiler or overly generalized with other meta-rules. I see the redundant rules here are to exploit the commutativity, following the doc. Let me know if I'm missing something. Thanks!
cfallin submitted PR review.
cfallin created PR review comment:
@bongjunj I think the issue here is that the left-hand sides are not even different commutative variants but literally the same (just alpha-renamed, i.e. with different binding spellings). With the right-hand sides the same, the effect of this is that matching with a particular
xandywill produce both(iadd x y)and(iadd y x)as terms (so I was wrong above: this won't actually dedup -- and there's no reason to have both those forms produced when we don't have further information aboutxandy).Said another way: what the README is describing is that we should have all commutative variants as left-hand sides explicitly in variations of a rule. But what the rules are doing here is not moving around pieces on the left-hand side, but just matching the same thing (always exactly equivalently) with different names.
I think "don't write the same left-hand side twice" is probably already implied by our rules but I'm happy to review a PR if you think it could be made more explicit!
cfallin submitted PR review:
LGTM!
cfallin added PR #12948 [Cranelift] add simplification rules to the merge queue.
cfallin merged PR #12948.
cfallin removed PR #12948 [Cranelift] add simplification rules from the merge queue.
bongjunj submitted PR review.
bongjunj created PR review comment:
Ah, now I see what the rules are doing. Thanks for the detailed comment!!
Last updated: Apr 12 2026 at 23:10 UTC