Skip to content

Preserve contained-load exceptions during embedded masking - #134366

Merged
tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-embedded-masking-fault-preservation
Sep 22, 2026
Merged

tannergooding merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-embedded-masking-fault-preservation

Conversation

@tannergooding

@tannergooding tannergooding commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Prevent AVX-512 masking from suppressing eagerly evaluated loads and losing required exceptions.

There are two affected paths: embedding an arithmetic operation that already contains a faulting load, and containing a load directly in a masked blend. Both can suppress the load when the mask is empty. Disabling embedded masking avoids the arithmetic fusion but does not prevent the direct-blend case.

Use NodeOrContainedOperandsMayThrow in the compiler-aware isEmbeddedMaskingCompatible check and in direct masked-blend containment analysis. The check follows contained operands through broadcasts and leaves potentially throwing loads unconditional. Lowering rechecks compatibility after containment is established. Register operands and proven-nonfaulting memory operands remain eligible; this preserves exceptions modeled by the JIT, not arbitrary hardware access violations. The compiler-aware overload is non-const to reuse the existing exception queries without a broader const-correctness refactor.

The regression covers five representative shapes: arithmetic with a vector load, arithmetic with a scalar broadcast, a direct vector load, a direct broadcast, and an aligned-load intrinsic. Widths are distributed across 128/256/512 bits. Each shape checks empty, partial, and full masks with valid and null inputs, for 15 theory cases.

Validation

  • Built Checked and Release x64 JITs and ran on Windows with AVX-512F/VL active.
  • All five empty-mask cases fail with the original Checked and Release JITs; all 15 cases pass with the complete fix. The intermediate ALU-only fix still fails the three direct-load/broadcast/intrinsic-load cases.
  • The fixed JIT passes embedded-masking-disabled and register-stress controls. AVX-512-disabled, AVX2-disabled, and software-fallback configurations each pass 12 portable cases and skip the three AVX-512-specific cases.
  • The complete related regression runner passes 356 tests both normally and with DOTNET_JitStress=2.
  • Disassembly confirms the affected masked memory operands are evaluated unconditionally with the fix, while safe masking remains available.
  • JIT formatting passed for Windows x64.

Codegen and performance

Representative safe arithmetic field loads, constant broadcasts, and explicitly null-checked loads retain identical instructions and code sizes (63, 63, and 118 bytes). Preserving potentially faulting vector and broadcast loads in unconditional arithmetic adds six bytes in the measured examples (67 to 73 and 64 to 70).

For the direct-blend follow-up, the representative safe cases also retain identical codegen. The affected direct-broadcast example grows from 54 to 60 bytes; its local BenchmarkDotNet means were 1.551 ns before and 1.553 ns after. Other tiny benchmarks varied despite identical instructions, and an identical-JIT control also showed substantial timing variation. No speedup or precise performance bound is claimed.

x86, ARM64, non-Windows systems, NativeAOT execution, and servicing branches were not exercised.

Resolves #134197

Note

This PR description was drafted by GitHub Copilot.

Copilot AI lite review requested due to automatic review settings September 21, 2026 19:24
@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 Sep 21, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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.

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 review overview

🟢 Approval recommended

The JIT change is small and targeted, leverages existing exception analysis, and is backed by clear, focused regression tests covering the fault-suppression scenarios.

Review effort: Lite
Findings: None

What changed in this PR

This PR prevents AVX-512 embedded-masking formation from suppressing exceptions (notably NullReferenceException) when the masked operation would otherwise execute a potentially-faulting contained operand load. It does this by reusing existing JIT exception analysis to block embedded masking when the candidate operand (or its contained children, including broadcast chains) may throw, and adds focused regression coverage for the relevant codegen shapes.

Changes:

  • In x64 lowering, gate embedded-masking eligibility on !NodeOrContainedOperandsMayThrow(...) for the embedded-masking candidate operand.
  • Add a new JIT regression test file covering multiple intrinsic shapes (128/256/512, broadcast, zeroing, and aligned-load), validating empty/partial/full masks with null vs valid inputs.
File Description
src/​coreclr/​jit/​lowerxarch.cpp Prevents forming embedded-masked operations when the embedded operand (or contained children) may throw, preserving required exceptions.
src/​tests/​JIT/​Regression_ro_2/​Runtime_134197.cs Adds regression tests ensuring empty-mask cases still throw NullReferenceException and non-empty masks produce correct results across several shapes.

Comment thread src/coreclr/jit/lowerxarch.cpp Resolved
Centralize embedded-mask exception checks and cover direct blend containment with focused regression tests.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings September 21, 2026 21:32

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 review overview

🔵 Needs a closer look

It changes core JIT codegen/containment behavior around exception modeling for AVX-512 masking, which has a broad blast radius and warrants final human review.

Review effort: Lite
Findings: None

@tannergooding
tannergooding merged commit e29c15e into dotnet:main Sep 22, 2026
144 of 145 checks passed
@tannergooding
tannergooding deleted the tannergooding-embedded-masking-fault-preservation branch September 22, 2026 15:55
@tannergooding

Copy link
Copy Markdown
Member Author

/backport to release/11.0

Note

Backport requested by GitHub Copilot at the maintainer's direction.

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 2026
JulieLeeMSFT pushed a commit that referenced this pull request Oct 12, 2026
…king (#134430)

Backport of #134366 to `release/11.0`.

Preserve required exceptions when AVX-512 masking would otherwise
suppress an eagerly evaluated load. The fix covers both arithmetic
operations with contained loads and direct masked-blend memory operands,
including broadcasts and intrinsic loads. It reuses the JIT's exception
analysis and leaves proven-nonfaulting operands eligible for masking.

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Reported in #134197. On AVX-512-capable x64 systems,
optimized vector conditional selections can silently return the fallback
value instead of throwing the required `NullReferenceException` when an
eagerly evaluated input dereferences null. The behavior depends on the
mask and generated instruction shape, so callers can observe incorrect
exception behavior only on certain hardware or under optimization.
Disabling embedded masking avoids the arithmetic-fusion case but does
not fix the direct masked-blend case.

## Regression

- [ ] Yes
- [x] No

Not a regression unique to .NET 11. The issue explicitly reports
reproduction on released .NET 10.0.12, and the discussion lists affected
SDK versions 9.0.318, 10.0.401, and 11.0.100-rc.2.26467.112. The
original introducing change has not been established; the older-version
reports were not independently reproduced during this investigation.

## Testing

Validation completed for the main-branch fix on Windows x64 with
AVX-512F/VL active:

- Built Checked and Release JITs. All five empty-mask regression shapes
fail with the original JITs; all 15 theory cases pass with the complete
fix. The intermediate arithmetic-only fix still fails the three
direct-load/broadcast/intrinsic-load shapes.
- The focused test covers arithmetic loads, nested scalar broadcasts,
direct vector loads, direct broadcasts, and aligned-load intrinsics,
distributing 128/256/512-bit widths across the shapes. Each checks
empty, partial, and full masks with valid and null inputs. This
specifically covers the interaction between eager operand evaluation,
containment, and hardware fault suppression.
- Embedded-masking-disabled and register-stress controls pass.
AVX-512-disabled, AVX2-disabled, and software-fallback configurations
pass all 12 portable cases and skip the three AVX-512-specific cases.
- The full related regression runner passes 356 tests normally and with
`DOTNET_JitStress=2`.
- Disassembly verifies that potentially faulting loads remain
unconditional and representative safe optimizations remain available.
Windows x64 JIT formatting passed.

The bot applied the backport without manual conflict resolution, and its
diff was inspected. The `release/11.0` backport has not been built or
executed locally; release-branch CI validation is still required. x86,
ARM64, non-Windows systems, and NativeAOT execution were not tested
locally. The exact reason earlier test coverage missed the issue is not
established.

## Risk

**Low.** The product change is confined to xarch masked-operation
eligibility and masked-blend containment. It conservatively declines
optimizations only when existing JIT exception analysis identifies an
operation or contained operand that may throw. It does not add a new
optimization, alter public APIs or data layouts, or refactor the
exception-analysis/traversal infrastructure. Proven-nonfaulting loads
remain eligible, and explicit masked-load APIs retain their
intentionally predicated semantics.

The main tradeoff is code quality for affected loads: keeping evaluation
unconditional can require another instruction/register and slightly
larger code. Measured affected examples grew by six bytes.
Representative safe cases retained identical instructions and sizes; a
local direct-broadcast benchmark measured 1.551 ns before and 1.553 ns
after. Tiny-benchmark timings varied substantially even with identical
JITs, so these measurements are not a precise performance bound.
Main-branch regression, fallback, stress, and codegen checks reduce
risk, but release-branch CI remains necessary.

**IMPORTANT**: If this backport is for a servicing release, please
verify that:

- For .NET 8 and .NET 9: The PR target branch is `release/X.0-staging`,
not `release/X.0`.
- For .NET 10+: The PR target branch is `release/X.0` (no `-staging`
suffix).

Verified target: `dotnet/runtime:release/11.0`.

## Package authoring no longer needed in .NET 9

**IMPORTANT**: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.

No package-authoring changes are required for this .NET 11 backport.

Resolves #134197

> [!NOTE]
> This backport description was drafted by GitHub Copilot.

Co-authored-by: Tanner Gooding <[email protected]>
Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 23, 2026
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

Development

Successfully merging this pull request may close these issues.

JIT: (bug) Embedded masking suppresses a contained faulting load, losing NullReferenceException (AVX-512)

3 participants