Skip to content

JIT: Normalize optimizations for ConditionalSelect and BlendVariable#123146

Open
saucecontrol wants to merge 7 commits intodotnet:mainfrom
saucecontrol:blendvariablemask
Open

JIT: Normalize optimizations for ConditionalSelect and BlendVariable#123146
saucecontrol wants to merge 7 commits intodotnet:mainfrom
saucecontrol:blendvariablemask

Conversation

@saucecontrol
Copy link
Member

@saucecontrol saucecontrol commented Jan 13, 2026

This fills in some optimizations present for ConditionalSelect but not BlendVariable and vice-versa on xarch.


Enables folding for BlendVariable with constant mask.

static Vector512<int> BlendMaskAllOnes(Vector512<int> v, int n) =>
    Avx512F.BlendVariable(
        Vector512.Create(n),
        v,
        Vector512.Create(-1));
-       vpbroadcastd zmm0, r8d
-       kmovq    k1, qword ptr [reloc @RWD00]
-       vpblendmd zmm0 {k1}, zmm0, zmmword ptr [rdx]
+       vmovups  zmm0, zmmword ptr [rdx]
        vmovups  zmmword ptr [rcx], zmm0
        mov      rax, rcx
 
        vzeroupper 
        ret      
-RWD00  	dq	000000000000FFFFh
-; Total bytes of code: 34
+; Total bytes of code: 19

Folds ConvertVectorToMask(CNS_VEC) to CNS_MASK

static Vector512<int> MultiInsert(Vector512<int> v, int n) =>
    Vector512.ConditionalSelect(
        Vector512.Create(0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0),
        v,
        Vector512.Create(n));
-       vbroadcastf32x4 zmm0, xmmword ptr [reloc @RWD00]
-       vpbroadcastd zmm1, r8d
-       vpmovd2m k1, zmm0
-       vpblendmd zmm0 {k1}, zmm1, zmmword ptr [rdx]
+       vpbroadcastd zmm0, r8d
+       kmovq    k1, qword ptr [reloc @RWD00]
+       vpblendmd zmm0 {k1}, zmm0, zmmword ptr [rdx]
        vmovups  zmmword ptr [rcx], zmm0
        mov      rax, rcx

        vzeroupper 
        ret      
-RWD00  	dq	FFFFFFFF00000000h, 0000000000000000h
+RWD00  	dq	0000000000002222h
-; Total bytes of code: 41
+; Total bytes of code: 34
 

Enables EVEX embedded zero in more cases.

static Vector512<int> MultiInsertZero(Vector512<int> v) =>
    Avx512F.BlendVariable(
        v,
        Vector512<int>.Zero,
        Vector512.Create(0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0));
-       vmovups  zmm0, zmmword ptr [rdx]
-       vxorps   ymm1, ymm1, ymm1
        kmovq    k1, qword ptr [reloc @RWD00]
-       vpblendmd zmm0 {k1}, zmm0, zmm1
+       vpblendmd zmm0 {k1}{z}, zmm0, zmmword ptr [rdx]
        vmovups  zmmword ptr [rcx], zmm0
        mov      rax, rcx
 
        vzeroupper 
        ret      
-RWD00  	dq	0000000000002222h
+RWD00  	dq	000000000000DDDDh
-; Total bytes of code: 38
+; Total bytes of code: 28
 

Optimizes blend with zero to AND/ANDN in more cases

static Vector256<int> MultiInsertZero(Vector256<int> v) =>
    Avx2.BlendVariable(
        v,
        Vector256<int>.Zero,
        Vector256.Create(0, -1, 0, 0, 0, -1, 0, 0));
-       vmovups  ymm0, ymmword ptr [rdx]
-       vxorps   ymm1, ymm1, ymm1
-       vbroadcastf128 ymm2, xmmword ptr [reloc @RWD00]
-       vpblendvb ymm0, ymm0, ymm1, ymm2
+       vbroadcastf128 ymm0, xmmword ptr [reloc @RWD00]
+       vpandn   ymm0, ymm0, ymmword ptr [rdx]
        vmovups  ymmword ptr [rcx], ymm0
        mov      rax, rcx

        vzeroupper 
        ret      
 RWD00  	dq	FFFFFFFF00000000h, 0000000000000000h
-; Total bytes of code: 34
+; Total bytes of code: 24
  

More improvements in the full diffs

@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 Jan 13, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 13, 2026
@dotnet-policy-service
Copy link
Contributor

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

@saucecontrol saucecontrol force-pushed the blendvariablemask branch 3 times, most recently from 3e3d69d to 9fd14b8 Compare January 15, 2026 04:34
@saucecontrol saucecontrol changed the title JIT: Import ConditionalSelect as BlendVariableMask JIT: Normalize optimizations for ConditionalSelect and BlendVariable Jan 20, 2026
Copy link
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

This PR normalizes and consolidates optimizations for ConditionalSelect and BlendVariable intrinsics on x86/x64 architectures. The changes enable several new optimization patterns and refactor existing code to share common logic between similar operations.

Changes:

  • Adds constant mask folding for BlendVariable intrinsics (all zeros → select first operand, all ones → select second operand)
  • Enables folding of ConvertVectorToMask(CNS_VEC) to constant masks at lowering time
  • Consolidates blend-with-zero optimizations into AND/AND_NOT operations across both ConditionalSelect and BlendVariable
  • Refactors mask inversion logic into a reusable TryInvertMask helper function to reduce code duplication

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/coreclr/jit/valuenum.cpp Adds value numbering optimizations for BlendVariable intrinsics with constant masks
src/coreclr/jit/rationalize.cpp Removes obsolete small type normalization logic for BlendVariable
src/coreclr/jit/lowerxarch.cpp Adds lowering optimizations for BlendVariable/ConvertVectorToMask, refactors mask inversion into TryInvertMask helper, and consolidates ConditionalSelect lowering
src/coreclr/jit/lower.h Adds declaration for new TryInvertMask helper method
src/coreclr/jit/gentree.cpp Fixes comment typo and adds constant folding for BlendVariable in morph phase

Copilot AI review requested due to automatic review settings February 4, 2026 18:51
Copy link
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 5 out of 5 changed files in this pull request and generated 2 comments.

@saucecontrol
Copy link
Member Author

@dotnet/jit-contrib this is ready for review

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.

3 participants