Skip to content

JIT: preserve null check for unused GT_BLK arg when inlining - #130417

Merged
JulieLeeMSFT merged 4 commits into
mainfrom
copilot/fix-uninline-null-reference-exception
Jul 16, 2026
Merged

JulieLeeMSFT merged 4 commits into
mainfrom
copilot/fix-uninline-null-reference-exception

Conversation

Copilot AI commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

When inlining a callee that accepts a struct argument by value but never uses it, the JIT was silently dropping the GT_BLK dereference, swallowing a NullReferenceException that should have been thrown:

Problem(null); // should throw NRE, but silently succeeds

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static unsafe void Problem(S* pA) { Use(*pA); }

static void Use(S s) { } // inlined, never uses 's'
struct S { }

Root cause: In fgInsertInlineeArgument, when a GT_BLK argument is unused (argIsUsed == false) but has side effects, the code stripped it to just the address operand and wrapped it in a gtUnusedValNode. The bare address has no side effects, so the optimizer discarded it entirely.

Fix (fginline.cpp): Check IndirMayFault on the GT_BLK. If the indirection can fault, emit a GT_NULLCHECK instead of a dead value expression — preserving the null-dereference semantics without placing a GT_BLK under GT_COMMA (which codegen can't handle).

GenTree* addr = argNode->AsOp()->gtOp1;
if (argNode->IndirMayFault(this))
    *newStmt = gtNewStmt(gtNewNullCheck(addr), callDI);
else
    *newStmt = gtNewStmt(gtUnusedValNode(addr), callDI);

Test: Added src/tests/JIT/Regression/JitBlue/Runtime_72808 — calls Problem(null) under AggressiveOptimization and expects NullReferenceException.

Copilot AI requested review from Copilot and removed request for Copilot July 9, 2026 16:30
Copilot AI linked an issue Jul 9, 2026 that may be closed by this pull request
@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 9, 2026
Copilot AI requested review from Copilot and removed request for Copilot July 9, 2026 17:17
Copilot AI changed the title [WIP] Fix unused struct argument loads by improving inlining JIT: preserve null check for unused GT_BLK arg when inlining Jul 9, 2026
Copilot AI requested a review from JulieLeeMSFT July 9, 2026 17:19
@EgorBo
EgorBo marked this pull request as ready for review July 9, 2026 18:58
Copilot AI review requested due to automatic review settings July 9, 2026 18:58

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 updates the CoreCLR JIT inliner’s handling of unused struct-by-value arguments so that required faulting semantics (null dereference) are preserved when the argument is represented as a GT_BLK and the inlinee never consumes it.

Changes:

  • In fgInsertInlineeArgument, when an unused GT_BLK argument may fault, emit a GT_NULLCHECK instead of reducing the tree to a dead address expression.
  • Add a new JIT regression test Runtime_72808 intended to validate that Problem(null) still throws NullReferenceException in the inlining scenario.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/fginline.cpp Preserves null-check semantics for unused GT_BLK inlinee arguments by emitting GT_NULLCHECK when the indirection may fault.
src/tests/JIT/Regression/JitBlue/Runtime_72808/Runtime_72808.cs Adds a regression test covering the inline-time elimination of a faulting struct load from a null pointer.
src/tests/JIT/Regression/JitBlue/Runtime_72808/Runtime_72808.csproj Adds the project file to build the new regression test (optimized, unsafe enabled).

@JulieLeeMSFT

Copy link
Copy Markdown
Member

Rerunning failed tests.

@JulieLeeMSFT

Copy link
Copy Markdown
Member

/ba-g test failures are on mono.

Copilot AI temporarily deployed to copilot-pat-pool July 14, 2026 17:24 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 14, 2026 17:24 Inactive
@JulieLeeMSFT

JulieLeeMSFT commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

@EgorBo, mono pipelines constantly fail even after a few retries. Can you check SPMI diffs?

@EgorBo

EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member

@copilot please remove Runtime_72808.csproj and add this to the shared csproj up-level. Also fix the build error with a missing unsafe {} over null passing to S*

Copilot AI requested review from Copilot and removed request for Copilot July 15, 2026 00:20

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please remove Runtime_72808.csproj and add this to the shared csproj up-level. Also fix the build error with a missing unsafe {} over null passing to S*

Done. Removed Runtime_72808.csproj and added the .cs file to Regression_o_3.csproj (between Runtime_72768 and Runtime_73559). Also added unsafe { } block around Problem(null) in TestEntryPoint. (AllowUnsafeBlocks is already set globally in src/tests/Directory.Build.props so no changes needed to the shared csproj.)

Copilot AI requested a review from EgorBo July 15, 2026 00:20
Copilot AI temporarily deployed to copilot-pat-pool July 15, 2026 13:42 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 15, 2026 13:42 Inactive
@JulieLeeMSFT
JulieLeeMSFT enabled auto-merge (squash) July 15, 2026 17:14
Copilot AI review requested due to automatic review settings July 15, 2026 17:14

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

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

@JulieLeeMSFT
JulieLeeMSFT merged commit 49110af into main Jul 16, 2026
147 checks passed
@JulieLeeMSFT
JulieLeeMSFT deleted the copilot/fix-uninline-null-reference-exception branch July 16, 2026 00:57
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 16, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — ac9ffd3d Deployed Jul 15, 2026 by JulieLeeMSFT via pat_pool #15212
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.

Unused struct argument loads can be dropped by inlining

4 participants