Skip to content

[Cranelift] add type-aware imm64 constant folding operations#12826

Merged
fitzgen merged 7 commits intobytecodealliance:mainfrom
prosyslab:feat-imm64-const-folding
Apr 14, 2026
Merged

[Cranelift] add type-aware imm64 constant folding operations#12826
fitzgen merged 7 commits intobytecodealliance:mainfrom
prosyslab:feat-imm64-const-folding

Conversation

@bongjunj
Copy link
Copy Markdown
Contributor

@bongjunj bongjunj commented Mar 23, 2026

This is a followup of #12764

I'm adding rules that perform constant foldings. However, currently, Cranelift mainly uses u64/i64_* operations that are meta-generated by https://github.com/bytecodealliance/wasmtime/blob/main/cranelift/codegen/meta/src/gen_isle.rs

However, those u64/i64 operations are not aware bitwidth specified by type specifier (usually captured by ty) and signedness of a constant. Thus one cannot easily handle overflow, shift/rotation crossing bitwidth boundaries. Therefore, one has to carefully reason about the bitwidth boundary conditions, but can occasionally fail to write a correct rule. On the other hand, imm64-based constant operations perform computations considering those bitwidth-related semantics. This advantage is observed in the current implementation of imm64_sdiv as it requires the precise view of Cranelift constants to correctly perform "signedness"-aware computation. In addition, using imm64-based constant operations, the implementation (writing simplify rules and more) is more straightforward and convenient since the primary representation of constants in Cranelift is imm64. (u64/i64 are Rust representation of constant literals).

For these reasons, I'm proposing that we should prioritize imm64-based approach rather than uN/iN one to make it easier to develop safe constant folding operations. This PR prepares such imm64 operations for further inclusion of various constant expressions.

@bongjunj bongjunj requested a review from a team as a code owner March 23, 2026 13:40
@bongjunj bongjunj requested review from fitzgen and removed request for a team March 23, 2026 13:40
@bongjunj bongjunj marked this pull request as draft March 23, 2026 14:04
@github-actions github-actions bot added the cranelift Issues related to the Cranelift code generator label Mar 23, 2026
@bongjunj bongjunj force-pushed the feat-imm64-const-folding branch from a10f67c to 2722fd3 Compare March 24, 2026 04:54
@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Mar 24, 2026

For these reasons, I'm proposing that we should prioritize imm64-based approach rather than uN/iN one to make it easier to develop safe constant folding operations.

This makes sense to me.

I wonder if we can or should generate and/or enumerate these imm64_* constructors in gen_isle.rs. (And stop generating the u64_*/i64_* versions?)

@bongjunj
Copy link
Copy Markdown
Contributor Author

@fitzgen I think we should implement imm64_* by hand rather than meta-generating them. For uN/iN, it was relatively straightforward to meta-generate operations because they were backed by Rust uN/iN built-ins. However, imm64_* needs Cranelift specific semantics involving bitshifts/rotates/overflows; therefore, meta-generating them could make the implementation unnecessarily complex.

@fitzgen
Copy link
Copy Markdown
Member

fitzgen commented Mar 25, 2026

Makes sense to me 👍

@bongjunj bongjunj force-pushed the feat-imm64-const-folding branch 3 times, most recently from a04de21 to e652a32 Compare April 13, 2026 11:35
@bongjunj
Copy link
Copy Markdown
Contributor Author

  • Implemented imm64_* operations for constant foldings, and
  • updated constant folding rules to use the operations.
  • In addition, added a missing rule (-X) * C = X * (-C) using imm64_neg

@bongjunj bongjunj marked this pull request as ready for review April 13, 2026 11:35
@github-actions github-actions bot added the isle Related to the ISLE domain-specific language label Apr 13, 2026
@github-actions
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.

Copy link
Copy Markdown
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@fitzgen fitzgen added this pull request to the merge queue Apr 13, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Apr 13, 2026
@bongjunj bongjunj force-pushed the feat-imm64-const-folding branch from e652a32 to e70124e Compare April 14, 2026 00:30
@bongjunj
Copy link
Copy Markdown
Contributor Author

@fitzgen conflicts are now resolved, and the filetests are re-blessed (register numbering was changed). thanks!

Copy link
Copy Markdown
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks!

@fitzgen fitzgen added this pull request to the merge queue Apr 14, 2026
Merged via the queue into bytecodealliance:main with commit c6a9443 Apr 14, 2026
48 checks passed
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