Skip to content

[Cranelift] add simplification rules#12948

Open
myunbin wants to merge 1 commit intobytecodealliance:mainfrom
myunbin:add-rules-040326
Open

[Cranelift] add simplification rules#12948
myunbin wants to merge 1 commit intobytecodealliance:mainfrom
myunbin:add-rules-040326

Conversation

@myunbin
Copy link
Copy Markdown
Contributor

@myunbin myunbin commented Apr 3, 2026

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 * y
  • ireduce(xext(a) + xext(b)) --> a + b
  • ireduce(xext(a) - xext(b)) --> a - b
  • max(x, y) >= x --> 1
  • x >= min(x, y) --> 1

bitops.isle

  • (x & ~y) == ~y --> (x | y) == -1
  • x >= (x & y) --> 1 and its dual

@myunbin myunbin requested a review from a team as a code owner April 3, 2026 08:28
@myunbin myunbin requested review from cfallin and removed request for a team April 3, 2026 08:28
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Apr 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 3, 2026

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 .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! These all look right to me; just one comment about a potential redundancy below. Happy to merge once that's resolved.

;; ireduce(xext(a) + xext(b)) --> a + b
(rule (simplify (ireduce ty (iadd cty (sextend cty x) (sextend cty y)))) (iadd ty x y))
(rule (simplify (ireduce ty (iadd cty (sextend cty x) (uextend cty y)))) (iadd ty x y))
(rule (simplify (ireduce ty (iadd cty (sextend cty y) (sextend cty x)))) (iadd ty x y))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this rule the same as the rule two lines up, with x and y swapped? 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 two iadds to the egraph (which is not what we want).

Same pattern in the rest of this rule's variants I believe.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected the overlap checker to find true redundancies, do you know why it isn't in this case?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are in the mid-end so simplify is 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The driver should dedup the same Value coming back twice though -- see here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants