dotnet / dotnet/runtime

Redundant bound check for VectorX.Create over span - cns - cns2

Open
#128,029 1 comment 0 reactions 0 assignees View on GitHub
area-CodeGen-coreclr reduce-unsafe
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

Blocking unsafe replacement in https://github.com/dotnet/runtime/pull/127847, minimal repro:
```cs
void Test(ReadOnlySpan data)
{
if (Vector128.IsHardwareAccelerated && data.Length >= Vector128.Count + 1)
{
int tailStart = data.Length - Vector128.Count;
Vector128 carries = Vector128.Create(data.Slice(tailStart - 1, Vector128.Count));
Console.WriteLine(carries.ToScalar());
}
}
```
currently emits:
```asm
; Method Program:<$>g__Foo|0_0(System.ReadOnlySpan`1[nuint]) (FullOpts)
G_M58534_IG01: ;; offset=0x0000
sub rsp, 40
;; size=4 bbWeight=1 PerfScore 0.25

G_M58534_IG02: ;; offset=0x0004
mov rax, bword ptr [rcx]
mov ecx, dword ptr [rcx+0x08]
cmp ecx, 3
jge SHORT G_M58534_IG04
;; size=11 bbWeight=1 PerfScore 5.25

G_M58534_IG03: ;; offset=0x000F
add rsp, 40
ret
;; size=5 bbWeight=0.50 PerfScore 0.62

G_M58534_IG04: ;; offset=0x0014
lea edx, [rcx-0x03]
lea r8, [rdx+0x02]
mov ecx, ecx
cmp r8, rcx
ja SHORT G_M58534_IG06
lea rcx, bword ptr [rax+8*rdx]
mov rcx, qword ptr [rcx]
;; size=21 bbWeight=0.50 PerfScore 2.50

G_M58534_IG05: ;; offset=0x0029
add rsp, 40
tail.jmp [System.Console:WriteLine(ulong)]
;; size=10 bbWeight=0.50 PerfScore 1.12

G_M58534_IG06: ;; offset=0x0033
call [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
int3
;; size=7 bbWeight=0 PerfScore 0.00
; Total bytes of code: 58
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.