Skip to content

JIT: normalize promoted field reads copied into a scalar - #137775

Open
AndyAyersMS wants to merge 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-135394-small-field-sign-extension
Open

AndyAyersMS wants to merge 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-135394-small-field-sign-extension

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

CopyFieldByField read a normalize-on-load promoted struct field directly
into a GT_LCL_VAR node without a cast. When the destination was a scalar
local that normalizes on store (for example a method's return temp), the raw
unnormalized bits propagated instead of a sign/zero-extended value, so an
optimized method returning a small signed field could return the wrong
(unsigned-looking) value.

Insert the normalizing cast only for that scalar-destination case. Copies
between two normalize-on-load locals (field-to-field, or scalar-to-field)
are left alone, since each side independently re-normalizes on its own next
read.

Validation

  • .\build.cmd clr+libs+host -rc Checked -lc Release -c Release passed
    with 0 warnings/errors.
  • src\tests\build.cmd -GenerateLayoutOnly x64 Checked passed.
  • The new regression test (short/sbyte field reads, field-to-field copies,
    and scalar-to-field copies) fails on the unfixed JIT with the originally
    reported wrong value, and with an assertion failure for an earlier,
    overly broad version of this fix. It passes on the fixed JIT.
  • jit-format passed.

Resolves #135394

Note

This PR description was generated with the help of GitHub Copilot.

CopyFieldByField read a normalize-on-load promoted field directly into
a GT_LCL_VAR node without a cast. When the destination was a scalar
local that normalizes on store (e.g. a method's return temp), the raw
unnormalized bits propagated instead of a sign/zero-extended value.

Insert a cast only for that scalar-destination case; copies between
two normalize-on-load locals (field-to-field, or scalar-to-field) are
left alone since each side re-normalizes independently on its own next
read.

Resolves dotnet#135394

Co-authored-by: Copilot <[email protected]>
@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 Nov 17, 2026
@AndyAyersMS
AndyAyersMS requested a balanced review from Copilot November 17, 2026 21:24
@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.

@AndyAyersMS
AndyAyersMS requested a review from EgorBo November 17, 2026 21:25
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo ptal
fyi @dotnet/jit-contrib

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

🟢 Approval recommended

No active review issues were identified for the JIT small-integer normalization change.

No open issues 🎉

What changed in this PR

This PR fixes incorrect small-integer values in the JIT when a promoted struct field is copied into a scalar local that normalizes on store.

Changes:

  • Adds a normalizing cast for the affected scalar-destination case, leaving copies between normalize-on-load locals unchanged.
  • Adds regression coverage for signed short and sbyte values, including field-to-field and scalar-to-field copies.
  • Configures the regression test for optimized execution with process isolation and platform restrictions.
File Description
src/​tests/​JIT/​Regression/​JitBlue/​Runtime_135394/​Runtime_135394.csproj Configures the regression test and its execution constraints.
src/​tests/​JIT/​Regression/​JitBlue/​Runtime_135394/​Runtime_135394.cs Tests signed field reads and related copy scenarios.
src/​coreclr/​jit/​morphblock.cpp Normalizes promoted field reads when copying into a scalar that normalizes on store.

Review effort: Balanced

@EgorBo EgorBo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think TryPrimitiveCopy has the same problem. When the source is a non-promoted small param, doRetypeNode (line 1025) turns it into a short LCL_VAR of a normalize-on-load local. The destination becomes STORE_LCL_VAR int of a normalize-on-store local, and nothing normalizes in between. Managed callers always sign-extend, so it takes a caller that doesn't (native code can do this) to see it:

[MethodImpl(MethodImplOptions.NoInlining)]
static int Test(short p) {
    short s = 0;
    Unsafe.As<short, W>(ref s) = Unsafe.As<short, W>(ref p);
    return s;
}

delegate*<int, int> f = (delegate*<int, int>)(delegate*<short, int>)&Test;
Console.WriteLine(f(0x1FFFF)); // 131071, expected -1

A plain short s = p; returns -1 for the same call, and .NET 11 behaves the same way. Maybe both paths should go through one small helper that produces the normalized read?

Comment thread src/coreclr/jit/morphblock.cpp Resolved
AndyAyersMS and others added 2 commits November 18, 2026 18:47
TryPrimitiveCopy's doRetypeNode had the same gap as CopyFieldByField: a
retyped small, normalize-on-load local (e.g. a parameter) can keep
undefined upper bits, and copying it directly into a scalar that
normalizes on store skipped the normalizing cast. A conforming managed
call always sign/zero-extends its arguments, which masked this in
practice, but a caller that doesn't (e.g. native code, or a mismatched
function pointer signature) exposes the raw bits.

Insert the cast for that case, matching the destination's existing
normalize-on-store contract. Add a regression case that calls through
a deliberately mismatched function-pointer signature to force dirty
argument bits.

Co-authored-by: Copilot <[email protected]>
The LCL_VAR under the normalizing cast must be retyped to TYP_INT
first, matching the invariant fgMorphLeafLocal establishes for a
normalize-on-load local under such a cast. Otherwise a later re-morph
could see the still-small-typed LCL_VAR, have IntegralRange::ForNode
treat it as already in range, and strip the cast via optAssertionProp_Cast
or fgOptimizeCast -- reintroducing the original bug.

Co-authored-by: Copilot <[email protected]>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

Confirmed and fixed -- thank you for the repro. TryPrimitiveCopy's doRetypeNode had the same gap: a retyped small-typed normalize-on-load local (your short param) copied directly into a normalize-on-store scalar without a normalizing cast. Your exact repro (delegate*<short, int> cast to delegate*<int, int>, called with 0x1FFFF) now returns -1 as expected; it returned 131071 before this fix.

Added a cast for that case too (with the operand retyped to TYP_INT first, per your other comment), plus a regression case using the same mismatched-function-pointer trick to force dirty argument bits.

Note

This comment was generated with the help of GitHub Copilot.

This branch has not been deployed

No deployments
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.

JIT: (bug) single-field struct copy into a short local skips normalization (assert 'srcFld->IsIntegralConst()' / wrong value)

3 participants