Skip to content

JIT: fold CAST<small>(COMMA(se, IND<small-same-size>))#129387

Open
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-69472-fold-cast-ind-through-comma
Open

JIT: fold CAST<small>(COMMA(se, IND<small-same-size>))#129387
AndyAyersMS wants to merge 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-69472-fold-cast-ind-through-comma

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Extend fgOptimizeCast to look through GT_COMMA wrappers when matching the existing small-int IND retyping. This lets (sbyte)span[i] collapse from a zero-extending load followed by a sign-extending cast into a single movsx load, instead of the previous movzx; movsx sequence.

Fixes #69472.

Extend fgOptimizeCast to look through GT_COMMA wrappers when matching
the existing small-int IND retyping. This lets `(sbyte)span[i]`
collapse from a zero-extending load followed by a sign-extending cast
into a single `movsx` load, instead of the previous
`movzx; movsx` sequence.

Fixes dotnet#69472.
Copilot AI review requested due to automatic review settings June 14, 2026 14:51
@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 Jun 14, 2026
@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.

Pull request overview

This PR updates the CoreCLR JIT’s fgOptimizeCast to look through GT_COMMA wrappers when applying the existing “small-int indirection retyping” optimization, and adds a JIT regression test under src/tests/JIT/Regression/JitBlue to exercise signed/unsigned small-int cast scenarios (including the ReadOnlySpan<T> indexer shape that commonly introduces a COMMA).

Changes:

  • Extend fgOptimizeCast to use gtEffectiveVal() so it can match and retype comma-wrapped GT_IND / GT_LCL_FLD nodes.
  • Add new JitBlue regression test Runtime_69472 and include it in Regression_ro_2.csproj.

Reviewed changes

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

File Description
src/coreclr/jit/morph.cpp Teach cast folding to look through GT_COMMA and retype the effective indirection.
src/tests/JIT/Regression/Regression_ro_2.csproj Include the new regression test source file in the merged runner project.
src/tests/JIT/Regression/JitBlue/Runtime_69472/Runtime_69472.cs Add regression coverage for small-int cast folding shapes and signed/unsigned combinations.

Comment thread src/coreclr/jit/morph.cpp
Comment on lines +8586 to 8597
effectiveSrc->ChangeType(castToType);

// Propagate the new type up through any COMMA wrappers so that the
// tree's type accurately describes the value being produced.
for (GenTree* cur = src; cur != effectiveSrc; cur = cur->AsOp()->gtOp2)
{
cur->ChangeType(castToType);
}

src->SetVNsFromNode(cast);

return src;
Comment on lines +13 to +15
// `(sbyte)span[i]` which previously emitted a zero-extending load followed by
// a sign-extending cast and should now collapse to a single `movsx` (or
// equivalently `movzx`) load.
Comment on lines +90 to +96
for (int v = 0; v <= 0xFFFF; v++)
{
ushort uw = (ushort)v;
short sw = unchecked((short)v);
ushort[] ua = new[] { uw };
short[] sa = new[] { sw };

Comment thread src/coreclr/jit/morph.cpp
for (GenTree* cur = src; cur != effectiveSrc; cur = cur->AsOp()->gtOp2)
{
cur->ChangeType(castToType);
}

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.

Doesn't ChangeType do this already if called on the right node?

<Compile Include="JitBlue\Runtime_128801\Runtime_128801.cs" />
<Compile Include="JitBlue\Runtime_129076\Runtime_129076.cs" />
<Compile Include="JitBlue\Runtime_129176\Runtime_129176.cs" />
<Compile Include="JitBlue\Runtime_69472\Runtime_69472.cs" />

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.

Should this be a regression test?

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 perf: ldind.u1 followed by conv.i1 results in suboptimal codegen

3 participants