Skip to content

Add peephole optimization to reduce MaskToVec and VecToMask conversions#130895

Open
abhishekdahad-1 wants to merge 1 commit into
dotnet:mainfrom
abhishekdahad-1:IndexOfMaxInt32_OptPass
Open

Add peephole optimization to reduce MaskToVec and VecToMask conversions#130895
abhishekdahad-1 wants to merge 1 commit into
dotnet:mainfrom
abhishekdahad-1:IndexOfMaxInt32_OptPass

Conversation

@abhishekdahad-1

Copy link
Copy Markdown

On x64, specifically when supporting AVX512 there is an issue in the code gen which leads to extraneous MaskToVec and VecToMask conversions. This can be observed specifically on the System.Numerics.Tensors.IndexOfMax<Int32> benchmark.

The generated assembly will look like this:
image

This can be fixed by examining every VecToMask conversion and seeing if it fits this form:

MASK_2 = VecToMask(NOT(LCL_VAR))
and LCL_VAR is defined earlier in the program as the following:
LCL_VAR = MaskToVec(MASK_1)

Then we can reduce this expression to become:

MASK_2 = NOT(MASK_1)

The fixed assembly looks like this:
image

This fix causes a ~47 reduction for 128 BufferLength and ~55% reduction in runtime for 3079 BufferLength. The runtime on System.Numerics.Tensors.IndexOfMax<Int32> is compared below.

Before changes:
image

After changes:
image

@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 Jul 16, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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.

@abhishekdahad-1

Copy link
Copy Markdown
Author

@abhishekdahad-1 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree company="AMD"

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.

1 participant