Skip to content

Unnecessary sign-extension for (nuint)span.Length #84564

@xtqqczze

Description

@xtqqczze

@EgorBo In theory, the length in (nuint)span.Length should be recognized as never negative, and the sign-extension elided, but doesn't appear to be the case (at least in R2R), see https://csharp.godbolt.org/z/14oW68fqz.

Originally posted by @xtqqczze in #84524 (comment)

static nuint Span0(ReadOnlySpan<byte> span) => (nuint)span.Length;
static nuint Span1(ReadOnlySpan<byte> span) => (uint)span.Length;
// crossgen2 8.0.0-preview.4.23208.99+07f00dc2f624946c54dbf85fea79c600a690f634

C:Span0(System.ReadOnlySpan`1[ubyte]):ulong:
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
						;; size=0 bbWeight=1 PerfScore 0.00
       movsxd   rax, dword ptr [rcx+08H]
						;; size=4 bbWeight=1 PerfScore 4.00
       ret      
						;; size=1 bbWeight=1 PerfScore 1.00

C:Span1(System.ReadOnlySpan`1[ubyte]):ulong:
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
						;; size=0 bbWeight=1 PerfScore 0.00
       mov      eax, dword ptr [rcx+08H]
						;; size=3 bbWeight=1 PerfScore 2.00
       ret      
						;; size=1 bbWeight=1 PerfScore 1.00

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions