Skip to content

JIT: Improve codegen for xarch vector byte multiply#126348

Open
saucecontrol wants to merge 3 commits intodotnet:mainfrom
saucecontrol:bytemul
Open

JIT: Improve codegen for xarch vector byte multiply#126348
saucecontrol wants to merge 3 commits intodotnet:mainfrom
saucecontrol:bytemul

Conversation

@saucecontrol
Copy link
Copy Markdown
Member

@saucecontrol saucecontrol commented Mar 31, 2026

Resolves #109775

In cases where we can widen to the next vector size up and multiply once, the current codegen is already optimal. When that's not possible, the current codegen falls back to a version that splits into two vectors and runs the same basic algorithm, which is not optimal.

This implements the suggestion made by @MineCake147E on #109775, which still requires two multiplications, but avoids double widening and narrowing. The result is a ~2x perf improvement.

Typical diff:

        vmovups  zmm0, zmmword ptr [r8]
        vpbroadcastb zmm1, byte  ptr [rcx]
        vpsubb   zmm0, zmm0, zmm1
-       vmovaps  zmm1, zmm0
-       vpmovzxbw zmm1, zmm1
-       vpmullw  zmm1, zmm1, zmm1
-       vpmovwb  zmm1, zmm1
-       vextracti32x8 ymm0, zmm0, 1
-       vpmovzxbw zmm0, zmm0
+       vpsrlw   zmm1, zmm0, 8
+       vpandd   zmm2, zmm0, dword ptr [reloc @RWD00] {1to16}
+       vpmullw  zmm1, zmm2, zmm1
        vpmullw  zmm0, zmm0, zmm0
-       vpmovwb  zmm0, zmm0
-       vinserti32x8 zmm0, zmm1, ymm0, 1
+       vpternlogd zmm0, zmm1, dword ptr [reloc @RWD04] {1to16}, -20
        vmovups  zmmword ptr [rdx], zmm0
        mov      rax, rdx
        vzeroupper 
        ret      

+RWD00  	dd	FF00FF00h
+RWD04  	dd	00FF00FFh
 
-; Total bytes of code 87
+; Total bytes of code 71

Full diffs

NB: codegen could actually be better, but currently JIT imports (and morphs) AND_NOT(x, y) as AND(x, NOT(y)), which in the case of constant y that is re-used, creates two constants where one would have sufficed.

Copilot AI review requested due to automatic review settings March 31, 2026 06:25
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 31, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 31, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves xarch JIT codegen for SIMD byte multiplication by using a more efficient two-multiply “odd/even byte” strategy when widening to the next vector size isn’t possible, reducing unnecessary widen/narrow work compared to the prior fallback.

Changes:

  • Adds a fast-path that widens to the next vector size (AVX2 for SIMD16, AVX512 for SIMD32) to perform a single multiply and then narrow.
  • Replaces the previous fallback (split/widen/mul/narrow twice) with an odd/even byte approach that uses two 16-bit multiplies and recombines bytes with masks/shifts.

Copilot AI review requested due to automatic review settings March 31, 2026 08:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Mar 31, 2026

vpmovzxbw zmm1, zmm1

the current codegen has an invalid operand for the instruction?

should be VPMOVZXBW zmm1 {k1}{z}, ymm2/m256?

and a similar issue for vpmovwb?

@saucecontrol
Copy link
Copy Markdown
Member Author

the current codegen has an invalid operand for the instruction?

Ha, I didn't notice that. Looks like that's just a bug in the JIT disasm. Running the code bytes through another disassembler shows it correctly.

@saucecontrol
Copy link
Copy Markdown
Member Author

cc @dotnet/jit-contrib

Diffs

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

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE/AVX byte multiplication could be improved

3 participants