Skip to content

Cranelift: Add(and modify) simplification rules#13850

Open
myunbin wants to merge 1 commit into
bytecodealliance:mainfrom
myunbin:add-rules-20260709-012308
Open

Cranelift: Add(and modify) simplification rules#13850
myunbin wants to merge 1 commit into
bytecodealliance:mainfrom
myunbin:add-rules-20260709-012308

Conversation

@myunbin

@myunbin myunbin commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Add and modify simplification rules.

arithmetic.isle

  • -(~x) --> x + 1
  • ~(-x) --> x - 1
  • -1 - x --> ~x

bitops.isle

  • Modified rule x ^ x --> 0 to support vector types.

extends.isle

  • Keep only the narrowest type version.

icmp.isle

  • Add two rules: ~x == x --> 0, ~x != x --> 1
  • Modified rules for variants of x == x --> 1 to support vector types.

shifts.isle

  • All zeros/ones remain all zeros/ones for arithmetic right shifts or rotates.
    For example, 0 << x --> 0 or (-1) >>s x --> (-1).

@myunbin myunbin requested a review from a team as a code owner July 9, 2026 05:26
@myunbin myunbin requested review from fitzgen and removed request for a team July 9, 2026 05:26
@myunbin myunbin requested a review from a team as a code owner July 9, 2026 05:38
@myunbin myunbin force-pushed the add-rules-20260709-012308 branch 5 times, most recently from 8acf908 to b60f059 Compare July 9, 2026 07:27
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

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.

@fitzgen fitzgen left a comment

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.

LGTM, modulo one small concern below. Thanks!

Comment thread cranelift/codegen/src/opts/bitops.isle
Comment on lines +17 to +24
function %test_bxor_x_x_i64x2(i64x2) -> i64x2 fast {
block0(v0: i64x2):
v1 = bxor v0, v0
return v1
}

; run: %test_bxor_x_x_i64x2([0 0]) == [0 0]
; run: %test_bxor_x_x_i64x2([0x123456789abcdef0 0xffffffffffffffff]) == [0 0]

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.

Ah okay, here is the test.

Can we also exercise floats and a vector of floats? Thanks!

@myunbin myunbin force-pushed the add-rules-20260709-012308 branch from b60f059 to 8b4249a Compare July 10, 2026 05:49
@myunbin

myunbin commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

@fitzgen As you suspected, bxor did not work for floats or vectors of floats. I updated
the rule to support both cases and added tests. Thanks for pointing it out!

[Cranelift] add simplification rules
@myunbin myunbin force-pushed the add-rules-20260709-012308 branch from 8b4249a to 1999068 Compare July 10, 2026 06:27
(iconst_u ty 0)))
(subsume x))

;; Create an all-zero bit pattern for integer, float, and vector types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add an all-zero helper for float and vector types.

@fitzgen fitzgen left a comment

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.

Thanks! One last question below about the new all_zero helper

Comment on lines +22 to +23
(rule 3 (all_zero (ty_vec64 ty)) (splat ty (all_zero (lane_type ty))))
(rule 4 (all_zero (ty_vec128 ty)) (splat ty (all_zero (lane_type ty))))

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.

Is it more efficient to load zeros by splatting than via loads from the constant pool?

It seems like we don't have any terms in the mid-end's prelude for working with ir::Constants, but they would be easy to add if necessary.

But if splatting is expected to be more efficient, then we should add a comment to that effect 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.

2 participants