Skip to content

Unnecessary overflow check with checked on 64-bit ryujit #111567

Open

Description

@hamarb123

Description

Elidable checked is not elided.

/cc @EgorBo who asked me to open an issue for it

Configuration

64-bit on sharplab.com

Regression?

Probably not.

Data

public class C {
    public nuint M(uint a) {
        return checked((nuint)a * 8);
    }
}

gives

C.M(UInt32)
    L0000: sub rsp, 0x28
    L0004: mov eax, edx
    L0006: mov ecx, 8
    L000b: mul rcx
    L000e: jb short L0015
    L0010: add rsp, 0x28
    L0014: ret
    L0015: call 0x0000027c46350090
    L001a: int3

Sharplab link

Analysis

The checked can be elided on 64-bit, since a fits within 32 bits & a * 8 fits within 35 bits, which means no overflow can occur with 64-bits of storage.

Seemingly doesn't reproduce on 32-bit when nuint is replaced with ulong (but still does on 64-bit)? Sharplab link

Activity

  1. added
    tenet-performance
    on Jan 18, 2025
  2. ghost added
    area-CodeGen-coreclr
    on Jan 18, 2025
  3. added
    untriaged
    on Jan 18, 2025
  4. dotnet-policy-service commented on Jan 18, 2025

    @dotnet-policy-service
    Contributor

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

  5. added this to the Future milestone on Jan 18, 2025
  6. removed
    untriaged
    on Jan 18, 2025
  7. changed the title [-]Unnecessary overflow check with checked[/-] [+]Unnecessary overflow check with checked on 64-bit ryujit[/+] on Jan 18, 2025
  8. xtqqczze commented on Jan 18, 2025

    @xtqqczze
    Contributor

    Elidable checked is not elided.

    The code generation issue is actually worse, using checked forces use of mul instead of shl:

    C.Unchecked(UInt32)
        L0000: mov eax, edx
        L0002: shl rax, 3
        L0006: ret
    
    C.Checked(UInt32)
        L0000: sub rsp, 0x28
        L0004: mov eax, edx
        L0006: mov ecx, 8
        L000b: mul rcx
        L000e: jb short L0015
        L0010: add rsp, 0x28
        L0014: ret
        L0015: call 0x0000027c464a00b0
        L001a: int3

    SharpLab

  9. huoyaoyuan commented on Jan 18, 2025

    @huoyaoyuan
    Member

    Also #105333

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-coreclrtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions