Skip to content

CSE seems to fail when writing x / cns and x % cns for unsigned integers #119131

Open

Description

@hamarb123

Description

CSE seems to fail if I write something like:

uint Natural(uint index)
{
    uint arrIndex = index / 3;
    uint bitIndex = index % 3;

    return arrIndex + bitIndex;
}

(it does the division twice).

This seems to only be an issue for unsigned integers not using DivRem.

Godbolt.

Configuration

Seems to reproduce on any .NET version on godbolt - I've only checked the issue on x64.

Regression?

Probably not.

Analysis

@tannergooding was saying it seems that UMOD might be missing early expansion altogether & it's getting carried down to rationalisation.

Activity

  1. added
    tenet-performance
    on Aug 27, 2025
  2. added
    untriaged
    on Aug 27, 2025
  3. added
    needs-area-label
    on Aug 27, 2025
  4. added
    area-CodeGen-coreclr
    and removed
    needs-area-label
    on Aug 27, 2025
  5. dotnet-policy-service commented on Aug 27, 2025

    @dotnet-policy-service
    Contributor

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

  6. tannergooding commented on Aug 27, 2025

    @tannergooding
    Member

    Related to #32615, not necessarily a dupe though since this is specifically around a constant divisor.

    On xarch we end up preserving the UMOD node until rationalization, unlike with MOD where it's often replaced in morph: https://github.com/dotnet/runtime/blob/main/src/coreclr/jit/morph.cpp#L7405-L7417

    This is unlike for arm64 where we always do it.

    The general logic here is likely worth tuning a bit since the AMD/Intel optimization manuals also recommend such transforms when one of the inputs is a known constant and it allows the same general CSE opportunities.

    Inversely we may want to recognize tmp = a / b followed by a - (tmp * b) for a non-constant b and transform the latter back into GT_MOD for some cases to ensure #117215 is handled and optimal.

    If we handle this by doing what #32615 suggests and always doing the transform, we may want to also recognize tmp = a / b followed by a - (tmp * b) for the general xarch optimization to only do a single DIV instead.

  7. added this to the Future milestone on Aug 27, 2025
  8. removed
    untriaged
    on Aug 27, 2025
  9. added
    help wanted
    on Aug 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrhelp wantedtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions