Skip to content

JIT: don't embed masks into narrowing conversions - #138230

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137901-narrowing-embmask
Nov 25, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-137901-narrowing-embmask

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Reject embedded masking for narrowing conversions. Their write masks apply to
narrower destination elements, so embedding a mask from a wider-lane
ConditionalSelect can produce incorrect results.

Validation

  • Built Checked CoreCLR and Release libraries, generated a matching Core_Root,
    and built the focused regression project on Windows x64.
  • With AVX-512F and VL enabled, all three new tests passed with the fixed JIT
    and failed with the unfixed JIT in the same runtime layout. A separate probe
    passed 2,481 checks with the fix and found 66 mismatches without it.
  • With AVX-512 disabled, two regression tests passed and the AVX-512-only test
    skipped; the probe passed 177 checks.
  • Disassembly confirms the affected masked conversions become unmasked
    conversions followed by blends. Sample same-lane conversions and a masked
    add retained identical codegen. No throughput benchmark or full SuperPMI
    run was performed.

Resolves #137901

Note

This description was generated with Copilot.

Narrowing conversions (cvt[t]pd2dq, cvtpd2ps, *qq2ps, *2ph, vpmov*, ...)
apply the EVEX write mask per narrower destination element, with the
element count set by the source vector length, and zero destination bits
above the converted elements. isEmbeddedMaskingCompatible assumed mask
lanes evenly cover the result type, so a ConditionalSelect over wider
lanes (e.g. Vector128<long> over a 256->128 vcvttpd2dq) was folded into
embedded masking and produced wrong results.

Reject embedded masking for these instructions. The only cases the old
size check accepted were incorrect, so no valid optimizations are lost.

Fixes dotnet#137901

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c747c31-c1bb-4378-b083-0bfdb399534a
@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 Nov 24, 2026
@AndyAyersMS
AndyAyersMS requested review from tannergooding and a balanced review from Copilot November 24, 2026 20:22
@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.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@tannergooding PTAL
fyi @dotnet/jit-contrib

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The cross-family AVX-512 eligibility changes need JIT maintainer review of instruction-list completeness and reported validation because independent hardware execution and differential codegen checks were unavailable.

No open issues 🎉

What changed in this PR

This PR fixes incorrect JIT code generation when a ConditionalSelect mask is embedded into a narrowing conversion whose destination elements use a different mask width.

Changes:

  • Rejects embedded masking for narrowing conversion instructions.
  • Adds three regression tests covering double-to-integer and double-to-single conversions with 128-, 256-, and 512-bit inputs.
File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_137901.cs Adds guarded regression tests for narrowing conversions followed by conditional selection.
src/​coreclr/​jit/​gentree.cpp Excludes narrowing conversions from embedded-mask compatibility.

Review effort: Balanced

Comment thread src/coreclr/jit/gentree.cpp Resolved

@tannergooding tannergooding left a comment

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.

Changes look correct to me, but left a suggestion on how to make this less error prone or risky for people forgetting with new instructions

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) wrong result when a masked vcvttpd2dq ymm -> xmm is embedded into a Vector128<long> ConditionalSelect (AVX-512)

3 participants