Skip to content

JIT: Spill stfld data if nullcheck on object interferes#125126

Draft
jakobbotsch wants to merge 2 commits intodotnet:mainfrom
jakobbotsch:stfld-reverse-op
Draft

JIT: Spill stfld data if nullcheck on object interferes#125126
jakobbotsch wants to merge 2 commits intodotnet:mainfrom
jakobbotsch:stfld-reverse-op

Conversation

@jakobbotsch
Copy link
Member

No description provided.

Copilot AI review requested due to automatic review settings March 3, 2026 15:07
@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 Mar 3, 2026
@dotnet-policy-service
Copy link
Contributor

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

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 addresses a JIT ordering issue where the explicit nullcheck introduced for large instance field offsets can interfere with the required IL evaluation order for stfld, potentially causing the store’s RHS to be evaluated after the nullcheck (and therefore not executed if the nullcheck throws). A new regression test is added to validate the intended ordering.

Changes:

  • Add a JIT regression test ensuring RHS evaluation is not skipped when a large-offset stfld throws NullReferenceException.
  • Update the importer to spill the stfld value when it cannot be safely reordered with the nullcheck.
  • Remove an explicit GTF_EXCEPT flag assignment for String.Length intrinsic, relying on standard indirection exception annotation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tests/JIT/Regression/Regression_ro_1.csproj Includes the new regression test source file in the test project.
src/tests/JIT/Regression/JitBlue/Runtime_125124/Runtime_125124.cs Adds a regression test covering stfld RHS ordering vs. explicit nullcheck insertion for large offsets.
src/coreclr/jit/importercalls.cpp Removes a manual GTF_EXCEPT flag set for String.get_Length intrinsic.
src/coreclr/jit/importer.cpp Adds impCanReorderWithNullCheck and spills stfld data when ordering could be violated by a nullcheck.
src/coreclr/jit/compiler.h Declares impCanReorderWithNullCheck.

Comment on lines +3908 to +3916
if ((tree->gtFlags & GTF_PERSISTENT_SIDE_EFFECTS) != 0)
{
return false;
}

if (((tree->gtFlags & GTF_EXCEPT) != 0) && (gtCollectExceptions(tree) != ExceptionSetFlags::NullReferenceException))
{
return false;
}
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impCanReorderWithNullCheck currently ignores GTF_ORDER_SIDEEFF. If the stfld value has an ordering side effect (e.g., a volatile indirection sets GTF_ORDER_SIDEEFF), allowing it to be reordered with the explicit nullcheck introduced for large field offsets can violate IL evaluation order by skipping the volatile operation when the nullcheck throws. Consider returning false when tree->gtFlags includes GTF_ORDER_SIDEEFF (and possibly other non-persistent ordering-only effects) so the value is spilled in these cases.

Copilot uses AI. Check for mistakes.
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.

2 participants