Repository navigation
JIT: normalize promoted field reads copied into a scalar - #137775
AndyAyersMS wants to merge 3 commits into
Conversation
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]>
|
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. |
|
@EgorBo ptal |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
🟢 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
shortandsbytevalues, 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
left a comment
There was a problem hiding this comment.
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 -1A 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?
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]>
|
Confirmed and fixed -- thank you for the repro. Added a cast for that case too (with the operand retyped to Note This comment was generated with the help of GitHub Copilot. |
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 Releasepassedwith 0 warnings/errors.
src\tests\build.cmd -GenerateLayoutOnly x64 Checkedpassed.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-formatpassed.Resolves #135394
Note
This PR description was generated with the help of GitHub Copilot.