Skip to content

feat: lower i64.extend*_s through wasm dialect#1008

Merged
mooori merged 3 commits intonextfrom
mooori/wasm-i64extend
Mar 13, 2026
Merged

feat: lower i64.extend*_s through wasm dialect#1008
mooori merged 3 commits intonextfrom
mooori/wasm-i64extend

Conversation

@mooori
Copy link
Contributor

@mooori mooori commented Mar 13, 2026

Generalize I32ExtendS to ExtendS to support both i32.extend*_s and i64.extend*_s sign-extension instructions:

  • Rename I32ExtendSExtendS
  • Generalize the operation to handle sign-extension for both i32 and i64 destination types
  • Add dst_ty attribute to explicitly track the destination type
  • Support i64.extend(8|16|32)_s in the Wasm frontend
  • Lowering, evaluation, and folding of ExtendS now handle variable destination types

Changes in generated code

None as the i64.extend(8|16|32)_s were not supported previously.

@mooori mooori marked this pull request as ready for review March 13, 2026 15:41
@mooori mooori requested review from bitwalker and greenhat March 13, 2026 15:42
implements(UnaryOp, InferTypeOpInterface, MemoryEffectOpInterface, Foldable, OpPrinter)
)]
pub struct I32ExtendS {
pub struct ExtendS {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's rename this SignExtend since the correlation to the actual Wasm opcode is basically gone, and that name is clearer than ExtendS/extend_s IMO.

In general, I would prefer to have more precise ops, and factor out the common parts with functions/traits/whatever, rather than create this kind of "mega" ops that handle all sorts of variations, since it complicates handling of those ops in many ways, and sometimes precludes implementing traits for them (not really in this case though). We do have legacy ops like this (namely the various generalized cast operations), but we can likely remove some of those once the Wasm dialect is complete (at least hir.cast, since that existed primarily to handle cases that will now be handled with more precise ops).

Anyway, it's not necessary to change this op, since it is still reasonably narrowly scoped - but just something to bear in mind going forward. Typically it is easier to write canonicalizations and optimizations on more precise ops, rather than on general ops, and more efficient to match as well when those rules can be trivially excluded/included based on the opcode, rather than needing to evaluate properties of the operation in detail. Basically, it isn't essential to factor out duplication in opcodes, just in the implementation of their behavior (i.e. if we had distinct opcodes for every pair of source and target type like Wasm does, we could still factor out the implementation details of those ops using generic functions).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed it to SignExtend. I see your point and will bear it in mind!

I wouldn't mind splitting it up by restoring I32ExtendS and adding I64ExtendS. Either as a follow-up or once SignExtend being too general becomes an issue.

Copy link
Collaborator

@bitwalker bitwalker left a comment

Choose a reason for hiding this comment

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

Looks good, just one change (renaming the op to SignExtend), and we can merge this

@mooori mooori merged commit d3cd8cd into next Mar 13, 2026
15 checks passed
@mooori mooori deleted the mooori/wasm-i64extend branch March 13, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants