Skip to content

Improve IntegralRange::ForCastOutput in assertion prop#128612

Draft
MihaZupan wants to merge 1 commit into
dotnet:mainfrom
MihaZupan:assertprop-cast
Draft

Improve IntegralRange::ForCastOutput in assertion prop#128612
MihaZupan wants to merge 1 commit into
dotnet:mainfrom
MihaZupan:assertprop-cast

Conversation

@MihaZupan
Copy link
Copy Markdown
Member

https://gist.github.com/MihuBot/dea35dcc7a03aa6982f9cb187965a762

The diff in BigInteger is for a pattern like this

int Foo(ulong x) => BitOperations.LeadingZeroCount(x) / 8

where the JIT didn't see BitOperations.LeadingZeroCount as non-negative since it's implemented as (int)Lzcnt.X64.LeadingZeroCount(value).

E.g.

-       mov      edx, edi
-       sar      edx, 31
-       and      edx, 7
-       add      edi, edx
-       sar      edi, 3
+       shr      edi, 3

@MihaZupan MihaZupan added this to the 11.0.0 milestone May 26, 2026
Copilot AI review requested due to automatic review settings May 26, 2026 23:13
@MihaZupan MihaZupan added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 26, 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.

Copy link
Copy Markdown
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 refines the JIT’s integral range analysis for certain non-overflowing casts by allowing IntegralRange::ForCastOutput to return a tighter range derived from the cast operand when it is provably within the cast’s default output range. This improves downstream assertion/range reasoning and can enable simpler codegen when the JIT can preserve non-negativity (and other range constraints) through casts.

Changes:

  • Compute the cast’s default output range (typeRange) for the relevant non-overflowing cast cases.
  • In non-MinOpts, compute the operand’s range and, when it’s contained within typeRange, return the tighter operandRange instead of the broader default.

Comment on lines +539 to +546
if (!compiler->opts.MinOpts())
{
return {SymbolicIntegerValue::Zero, SymbolicIntegerValue::UIntMax};
// If the operand's range is tighter than the cast's range, use it instead.
IntegralRange operandRange = ForNode(cast->CastOp(), compiler);
if (typeRange.Contains(operandRange))
{
return operandRange;
}
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