Skip to content

Fix three xarch lowering correctness/invariant issues - #130843

Merged
tannergooding merged 3 commits into
dotnet:mainfrom
tannergooding:tannergooding-xarch-lowering-correctness
Jul 20, 2026
Merged

tannergooding merged 3 commits into
dotnet:mainfrom
tannergooding:tannergooding-xarch-lowering-correctness

Conversation

@tannergooding

@tannergooding tannergooding commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

Three independent xarch lowering fixes, each as its own commit so they can be reviewed (or split) individually. Each has a standalone JitBlue regression test named after its tracking issue.


Fix incorrect base type passed to TryInvertMask for mask inversion (test: Runtime_130844)

Fixes #130844.

LowerHWIntrinsicCmpOp passed the node's simdBaseType to TryInvertMask, but the const-mask inversion path computes the number of valid mask bits from that base type (GetBitMask(simdSize / genTypeSize(base))). The mask's own base type (maskBaseType) can legitimately differ from the comparison's declared base type (e.g. an op_Equality declared TYP_INT over a TYP_DOUBLE mask), which would produce the wrong bit count and spurious high bits in the inverted mask. Pass maskBaseType, consistent with every other consumer in the block (element count, NotMask/ShiftLeftMask/ShiftRightMask). The other caller (BlendVariableMask) is unaffected since its base type already matches the mask.

Only observable on AVX512 (EVEX mask + KORTEST) and only in the IsCnsMsk() const-mask branch; correctness/consistency fix.


Ensure the correct node is checked for flags in TryLowerAndOpToExtractLowestSetBit (test: Runtime_130845)

Fixes #130845.

The GTF_SET_FLAGS guard tested opNode (the surviving leaf operand -- a dead check) rather than andNode, the GT_AND root that is removed and replaced by the BLSI. AND and BLSI set the CPU flags differently, so if a downstream consumer reads the flags produced by the AND the transform is unsound. Check andNode, matching the sibling TryLowerAndOpToResetLowestSetBit, and drop the dead opNode sub-check.

Latent by lowering order today (the AND's GTF_SET_FLAGS is only set later, when the consuming compare-to-zero is lowered), so this is a consistency/hardening change.


Don't mark INSERTPS op2 both contained and reg-optional (test: Runtime_130846)

Fixes #130846.

For NI_X86Base_Insert (FLOAT), when op2 is a zero vector it is already MakeSrcContained (the zero is fully elided via the INSERTPS zmask), but TryMakeSrcContainedOrRegOptional then ran unconditionally and additionally marked it reg-optional -- violating the invariant that a node is not both contained and reg-optional. Guard the call on !op2->isContained(), making the op2 path symmetric with the op1->IsVectorZero() branch just above (which contains and never retries).

The reg-optional flag is inert at runtime (a contained zero CNS_VEC produces no operand uses), so no default-build assert fires; hardening fix confirmed via temporary instrumentation.


All three come with JitBlue regression tests (proper ISA guards, scalar reference oracles). On non-AVX512 hardware all three are latent, so the tests are execution/coverage guards; #130844 specifically exercises the buggy EVEX path only on AVX512 CI.

Note

This PR description and the accompanying changes were generated with the assistance of GitHub Copilot.

Copilot AI review requested due to automatic review settings July 16, 2026 04:47
@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
@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.

@tannergooding

Copy link
Copy Markdown
Member Author

CC. @dotnet/jit-contrib, @EgorBo. 3x small bug fixes in lowering

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.

Pull request overview

This PR makes three targeted correctness/invariant fixes in xarch lowering and adds three new JitBlue regression tests to cover the affected scenarios.

Changes:

  • Fixes mask inversion in LowerHWIntrinsicCmpOp by passing the mask’s base type to TryInvertMask.
  • Fixes TryLowerAndOpToExtractLowestSetBit to check GTF_SET_FLAGS on the GT_AND root that would be replaced.
  • Fixes an invariant violation in containment for NI_X86Base_Insert by not marking op2 reg-optional when it is already contained.
  • Adds three new JitBlue regression tests with per-test csproj configuration.
Show a summary per file
File Description
src/coreclr/jit/lowerxarch.cpp Correctness/invariant fixes in xarch lowering for mask inversion, flags-guarding, and INSERTPS containment.
src/tests/JIT/Regression/JitBlue/Runtime_MaskCompareAllBitsSet/Runtime_MaskCompareAllBitsSet.csproj New JitBlue test project for mask inversion scenario.
src/tests/JIT/Regression/JitBlue/Runtime_MaskCompareAllBitsSet/Runtime_MaskCompareAllBitsSet.cs New regression test covering mask comparisons vs AllBitsSet with differing element interpretations.
src/tests/JIT/Regression/JitBlue/Runtime_InsertpsZeroContainment/Runtime_InsertpsZeroContainment.csproj New JitBlue test project for INSERTPS containment invariant.
src/tests/JIT/Regression/JitBlue/Runtime_InsertpsZeroContainment/Runtime_InsertpsZeroContainment.cs New regression test validating INSERTPS behavior when op2 is a contained zero vector.
src/tests/JIT/Regression/JitBlue/Runtime_ExtractLowestSetBitFlags/Runtime_ExtractLowestSetBitFlags.csproj New JitBlue test project for AND/NEG lowering flags-guard scenario.
src/tests/JIT/Regression/JitBlue/Runtime_ExtractLowestSetBitFlags/Runtime_ExtractLowestSetBitFlags.cs New regression test exercising x & (-x) patterns feeding branches/value uses.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 2

tannergooding and others added 2 commits July 15, 2026 22:00
The const-mask inversion path in TryInvertMask computes the number of valid mask bits from the base type, but LowerHWIntrinsicCmpOp was passing the node's simdBaseType instead of maskBaseType. These can legitimately differ (e.g. an op_Equality declared TYP_INT over a TYP_DOUBLE mask), which would yield the wrong bit count and spurious high bits in the inverted mask. Use maskBaseType, consistent with every other consumer in the block.

Co-authored-by: Copilot App <[email protected]>
…tLowestSetBit

The flags guard tested opNode (the surviving leaf operand) rather than andNode (the GT_AND root that is removed and replaced by the BLSI). AND and BLSI set the CPU flags differently, so if a downstream consumer reads the flags produced by the AND the transform is unsound. Check andNode, matching the sibling TryLowerAndOpToResetLowestSetBit, and drop the dead opNode sub-check.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 05:01
@tannergooding
tannergooding force-pushed the tannergooding-xarch-lowering-correctness branch from c6e8e3b to ae818a3 Compare July 16, 2026 05:01

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.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new

Copilot AI review requested due to automatic review settings July 16, 2026 05:05
For NI_X86Base_Insert (FLOAT), when op2 is a zero vector it is already MakeSrcContained, but TryMakeSrcContainedOrRegOptional then ran unconditionally and additionally marked it reg-optional, violating the invariant that a node is not both contained and reg-optional. Guard the call on !op2->isContained().

Co-authored-by: Copilot App <[email protected]>
@tannergooding
tannergooding force-pushed the tannergooding-xarch-lowering-correctness branch from ae818a3 to a972ceb Compare July 16, 2026 05:07

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.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new

Copilot AI review requested due to automatic review settings July 16, 2026 05:10

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.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new

@tannergooding
tannergooding requested a review from EgorBo July 18, 2026 16:45
@tannergooding
tannergooding enabled auto-merge (squash) July 18, 2026 16:46
@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "a972cebe72e8e8547c012ec73923400a41d9d279",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "f2281d59e62aa6a75e53055b8f842bac108b9887",
  "last_reviewed_commit": "a972cebe72e8e8547c012ec73923400a41d9d279",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "f2281d59e62aa6a75e53055b8f842bac108b9887",
  "last_recorded_worker_run_id": "29688132271",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "a972cebe72e8e8547c012ec73923400a41d9d279",
      "review_id": 4730810105
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: The PR bundles three independent x64 lowering fixes in the JIT, each addressing a distinct correctness or invariant issue in lowerxarch.cpp: (1) a wrong base type passed to TryInvertMask, (2) a dead/incorrect GTF_SET_FLAGS guard node in TryLowerAndOpToExtractLowestSetBit, and (3) an INSERTPS operand marked both contained and reg-optional. Each is a real invariant/correctness concern even if latent on current codegen paths.

Approach: Each change is a minimal, surgical edit that aligns the affected code with the established convention in its own vicinity. (1) simdBaseType -> maskBaseType matches every other consumer in the block (count at line 2892, NotMask/ShiftLeftMask/ShiftRightMask), correctly deriving the valid mask-bit count when the mask element type differs from the declared comparison base type. (2) Checking andNode->gtFlags instead of opNode->gtFlags matches the sibling TryLowerAndOpToResetLowestSetBit; the old guard tested a surviving leaf rather than the removed GT_AND whose flags semantics differ from BLSI. (3) Guarding TryMakeSrcContainedOrRegOptional on !op2->isContained() makes the op2-zero path symmetric with the op1-zero branch above and preserves the invariant that a node is not simultaneously contained and reg-optional. Each fix ships a standalone JitBlue regression test with a scalar reference oracle.

Summary: LGTM. The three fixes are correct, independently reviewable (one commit each), and consistent with surrounding conventions. The regression tests are reasonable: Runtime_130846 uses ConditionalFact(typeof(Sse41)) with a proper scalar oracle covering varied control bytes; Runtime_130845 exercises both value and flags-consumer uses; Runtime_130844 exercises mask/base-type mismatch cases and acts as a coverage guard on non-AVX512 hardware while exercising the buggy EVEX path on AVX512 CI. As the author notes, #130845 and #130846 are latent hardening changes today and #130844 is only observable on AVX512, so real behavioral coverage depends on AVX512 CI legs; this is acknowledged and acceptable. No actionable findings.

Detailed Findings

None.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 50.1 AIC · ⌖ 10.2 AIC · ⊞ 10K

@tannergooding
tannergooding merged commit c74f3fb into dotnet:main Jul 20, 2026
146 checks passed
@tannergooding
tannergooding deleted the tannergooding-xarch-lowering-correctness branch July 21, 2026 00:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

3 participants