Skip to content

[JIT] Recognize when length is never negative for Span<T>::.ctor(void*, int32) #83248

@xtqqczze

Description

@xtqqczze

I noticed this for the JIT intrinsic BitOperations.PopCount, for which the return value should be treated as never negative (since #64951).

unsafe Span<byte> M0(byte* p, byte b) => new(p, b);
unsafe Span<byte> M2(byte* p, int b) => new(p, (byte)b);
unsafe Span<byte> M3(byte* p, uint mask) => new(p, BitOperations.PopCount(mask));
unsafe Span<byte> M4(byte* p, uint mask) => new(p, (byte)BitOperations.PopCount(mask));
// crossgen2 8.0.0-preview.3.23159.99+1b2eb12a711410a358bdfa93af017a34a929d4cf

C:M0(ulong,ubyte):System.Span`1[ubyte]:this:
; Emitting BLENDED_CODE for X64 CPU with AVX - Unix
       push     rax
						;; size=1 bbWeight=1 PerfScore 1.00
       mov      rax, rsi
       movzx    rdx, dl
						;; size=6 bbWeight=1 PerfScore 0.50
       add      rsp, 8
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25

C:M02(ulong,int):System.Span`1[ubyte]:this:
; Emitting BLENDED_CODE for X64 CPU with AVX - Unix
       push     rax
						;; size=1 bbWeight=1 PerfScore 1.00
       movzx    rdx, dl
       test     edx, edx
       jl       SHORT G_M62596_IG04
       mov      rax, rsi
						;; size=10 bbWeight=1 PerfScore 1.75
       add      rsp, 8
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M62596_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
						;; size=7 bbWeight=0 PerfScore 0.00

C:M1(ulong,uint):System.Span`1[ubyte]:this:
; Emitting BLENDED_CODE for X64 CPU with AVX - Unix
       push     rax
						;; size=1 bbWeight=1 PerfScore 1.00
       popcnt   edx, edx
       jl       SHORT G_M53218_IG04
       mov      rax, rsi
						;; size=9 bbWeight=1 PerfScore 3.25
       add      rsp, 8
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M53218_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
						;; size=7 bbWeight=0 PerfScore 0.00

C:M2(ulong,uint):System.Span`1[ubyte]:this:
; Emitting BLENDED_CODE for X64 CPU with AVX - Unix
       push     rax
						;; size=1 bbWeight=1 PerfScore 1.00
       popcnt   edx, edx
       jl       SHORT G_M40097_IG04
       mov      rax, rsi
						;; size=9 bbWeight=1 PerfScore 3.25
       add      rsp, 8
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
G_M40097_IG04:
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
						;; size=7 bbWeight=0 PerfScore 0.00
``

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions