Skip to content

perf(x64): optimize i8 multiplication by constants - #14796

Open
shrayjaiswal050-design wants to merge 1 commit into
bytecodealliance:mainfrom
shrayjaiswal050-design:fix/x64-i8-imul-imm
Open

shrayjaiswal050-design wants to merge 1 commit into
bytecodealliance:mainfrom
shrayjaiswal050-design:fix/x64-i8-imul-imm

Conversation

@shrayjaiswal050-design

Copy link
Copy Markdown

Fixes #14660.

On x64, multiplying an i8 value by a constant currently uses mulb
and can load the constant from memory. This uses a 32-bit immediate
imul instead, which gives the same low-byte result.

Byte loads remain byte-sized. Added tests for both operand orders,
wrapping results, negative constants, and inputs with unused upper bits.

Tested on Windows x64:

  • 623 x64/runtime filetests completed successfully
  • 204 codegen unit tests passed
  • Codegen formatting and git diff --check passed

Use 32-bit immediate imul for wrapping i8 constant multiplication on either side. Keep byte loads in registers to avoid widening memory accesses, and add codegen and execution regression coverage.

Fixes bytecodealliance#14660
@shrayjaiswal050-design
shrayjaiswal050-design requested a review from a team as a code owner October 17, 2026 17:56
@shrayjaiswal050-design
shrayjaiswal050-design requested review from alexcrichton and removed request for a team October 17, 2026 17:56
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen labels Oct 17, 2026
@@ -0,0 +1,82 @@
test compile

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.

Can this use the precise-output mode to get the full golden output listed here?

Comment on lines +1116 to +1122
;; i8 multiplication can use a 32-bit immediate multiply: only the low byte of
;; each operand affects the low byte of the result. Force the other operand into
;; a register so that a sinkable i8 load is never widened to a 32-bit load.
(rule -8 (lower (imul $I8 x (i32_from_iconst y)))
(x64_imul_imm $I32 (put_in_gpr x) y))
(rule -7 (lower (imul $I8 (i32_from_iconst x) y))
(x64_imul_imm $I32 (put_in_gpr y) x))

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.

Could this perhaps have rules for $I16 as well since it seems equally applicable? Also -- are you sure this doesn't need to extend the x and y arguments appropriately? I forget the rules around multiplication and bits so I'm not sure myself

This branch has not been deployed

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

Labels

cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cranelift: x86_64 lowering i8 multiply with mulb could be optimized

2 participants