Skip to content

JIT: Fix mask AND with zero incorrectly folded#123603

Merged
EgorBo merged 3 commits intomainfrom
copilot/fix-and-mask-zero-folding
Jan 26, 2026
Merged

JIT: Fix mask AND with zero incorrectly folded#123603
EgorBo merged 3 commits intomainfrom
copilot/fix-and-mask-zero-folding

Conversation

Copy link
Contributor

Copilot AI commented Jan 25, 2026

Description

AND between a mask and constant zero was incorrectly folded to return the mask value instead of zero in the JIT's gtFoldExprHWIntrinsic.

// Expected: <0, 0, 0, ...>  Actual (buggy): <-1, -1, -1, ...>
Vector512.Equals(v, v) & Vector512.LessThan(v.AsUInt32(), Vector512<uint>.Zero).AsInt32();

Changes

  • src/coreclr/jit/gentree.cpp: For mask types in GT_AND case, changed resultNode = otherNode to resultNode = gtWrapWithSideEffects(cnsNode, otherNode, GTF_ALL_EFFECT) — matching the non-mask vector case
  • src/tests/JIT/Regression/JitBlue/Runtime_123399/: Added regression test (requires AVX-512)
Original prompt

This section details on the original issue you should resolve

<issue_title>JIT: Mask AND with zero incorrectly folded</issue_title>
<issue_description>### Description

AND between a mask and a constant zero is incorrectly folded to the mask value in net10.0.

https://github.com/dotnet/runtime/blob/2075e8ffaa8cf598dac9c09f5f690778ef1a6c8e/src/coreclr/jit/gentree.cpp#L32988-L32992

Reproduction Steps

using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

Console.WriteLine(AndMaskZero(Vector512<int>.One));

[MethodImpl(MethodImplOptions.NoInlining)]
static Vector512<int> AndMaskZero(Vector512<int> v) =>
	Vector512.Equals(v, v) & Vector512.LessThan(v.AsUInt32(), Vector512<uint>.Zero).AsInt32();

Expected behavior

> dotnet run -c release -f net9.0

<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>

Actual behavior

> dotnet run -c release -f net10.0

<-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1>

Regression?

Yes, worked in 9.0

Configuration

Requires AVX-512

</issue_description>

<agent_instructions>Fix the bug in jit (see comments in the issue) + add a regression test (JitBlue)</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@JulieLeeMSFT Thanks, @saucecontrol, for the report. @anthonycanino, can your team please take a look? CC @kg.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 25, 2026 23:02
Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect folding of AND with zero in JIT JIT: Fix mask AND with zero incorrectly folded Jan 25, 2026
@stephentoub stephentoub added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 26, 2026
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo
Copy link
Member

EgorBo commented Jan 26, 2026

PTAL @dotnet/jit-contrib trivial bug-fix for x & 0 -> 0 (SIMD), found & suggested by @saucecontrol

No diffs

@EgorBo EgorBo requested a review from a team January 26, 2026 13:02
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 fixes a JIT optimization bug where AND operations between mask types and zero were incorrectly folded to return the mask value instead of zero. The issue occurred in gtFoldExprHWIntrinsic and has been regression since .NET 9.0. The fix makes mask type handling consistent with vector type handling for the same operation.

Changes:

  • Fixed incorrect constant folding for mask AND operations with zero
  • Added regression test to prevent future occurrences

Reviewed changes

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

File Description
src/coreclr/jit/gentree.cpp Fixed GT_AND case for mask types to use gtWrapWithSideEffects when ANDing with zero, making it consistent with vector type handling
src/tests/JIT/Regression/JitBlue/Runtime_123399/Runtime_123399.cs Added AVX512-gated regression test that verifies AND between an all-ones mask and a zero mask returns zero
src/tests/JIT/Regression/Regression_ro_1.csproj Added new test file to the project

@EgorBo EgorBo enabled auto-merge (squash) January 26, 2026 18:12
@EgorBo
Copy link
Member

EgorBo commented Jan 26, 2026

/ba-g deadletter

@EgorBo EgorBo merged commit 5d86fc4 into main Jan 26, 2026
110 of 139 checks passed
@EgorBo EgorBo deleted the copilot/fix-and-mask-zero-folding branch January 26, 2026 18:13
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: Mask AND with zero incorrectly folded

5 participants