Skip to content

Fix discarding of NRE's when unboxing nullables in Mono and JIT - #133351

Open
jakobbotsch wants to merge 3 commits into
dotnet:mainfrom
jakobbotsch:fix-133207
Open

Fix discarding of NRE's when unboxing nullables in Mono and JIT#133351
jakobbotsch wants to merge 3 commits into
dotnet:mainfrom
jakobbotsch:fix-133207

Conversation

@jakobbotsch

@jakobbotschjakobbotsch commented Sep 7, 2026

Copy link
Copy Markdown
Member

VN-based constant folding did not preserve exceptions from the root node. For GetType folding, that results in discarding an explicit nullcheck.

Retain throwing roots when their VN exception sets are not covered by their operands. Fold unused GetType operations to null checks through shared side-effect extraction, and add a targeted nullable GetType regression test.

Mono had a similar issue for nullables only, which was fixed in #132732 but later reverted. This PR includes that fix.

Fix#133207

Retain throwing roots when their VN exception sets are not covered by their operands. Fold unused GetType operations to null checks through shared side-effect extraction, and add a targeted nullable GetType regression test.
Fixesdotnet#133207.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f9c8af88-4173-4ead-99f9-50375606ef4a
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 7, 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.

Restore the Mono interpreter GetType fix without duplicating the JitBlue regression. Remove debug destruction of roots that assertion propagation may retain for side effects.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f9c8af88-4173-4ead-99f9-50375606ef4a
@jakobbotschjakobbotsch changed the title JIT: preserve root exceptions during VN constant foldingFix discarding of NRE's when unboxing nullables in Mono and JITSep 7, 2026
@jakobbotsch

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-coreclr superpmi-diffs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jakobbotsch

Copy link
Copy Markdown
MemberAuthor

/ba-g Failure is #130439

@jakobbotsch
jakobbotsch marked this pull request as ready for review September 8, 2026 11:38
CopilotAI lite review requested due to automatic review settings September 8, 2026 11:38
@jakobbotsch

Copy link
Copy Markdown
MemberAuthor

cc @dotnet/jit-contrib PTAL @EgorBo

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

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

🟡 Changes recommended

The new exception-set subset logic unions operand VNs without guarding against undefined VN pairs, which can trigger debug asserts/crashes in some loop/PHI scenarios.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes a JIT and Mono interpreter optimization bug where VN-based folding (notably GetType folding on boxed nullables) could drop required faulting behavior, leading to missing NullReferenceExceptions. It also adds a targeted JIT regression test to prevent recurrence.

Changes:

  • Update JIT VN-based constant folding to retain “throwing roots” when their exception sets aren’t covered by operands, and improve side-effect extraction so unused GetType can be reduced to a null-check.
  • Reapply the Mono interpreter fix to avoid optimizing box Nullable<T>; call GetType() into a constant type lookup.
  • Add a regression test for Nullable<T>.GetType() behavior (throws on null; returns underlying type when non-null).
File summaries
FileDescription
src/coreclr/jit/assertionprop.cppAdjust VN-based folding to decide when to preserve root side effects based on exception-set coverage.
src/coreclr/jit/gentree.cppAdd helper to turn unused GetType into a NULLCHECK via shared side-effect extraction.
src/coreclr/jit/compiler.hDeclare the new side-effect extraction helper on Compiler.
src/mono/mono/mini/interp/transform.cDisable the box; GetType optimization for nullable types in the interpreter.
src/tests/JIT/Regression/JitBlue/Runtime_133207/Runtime_133207.csAdd xUnit regression coverage for nullable GetType throwing/returning behavior.
src/tests/JIT/Regression/Regression_ro_2.csprojInclude the new regression test in the JIT regression project.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment threadsrc/coreclr/jit/assertionprop.cpp
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
CopilotAI review requested due to automatic review settings September 8, 2026 12:04

CopilotAI 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

It changes JIT optimization and side-effect extraction behavior in broadly-used code paths, so it warrants final maintainer review despite the added regression coverage.

Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclrCLR 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.

[ci-scan] Test failure: JitTest_Directed_nullabletypes_gettype.P.BoxedNullableGetType

3 participants

@jakobbotsch@jkotas