You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):pushrbpmovrbp,rspG_M44937_IG02: ;; offset=0x0004testesi,esije SHORT G_M44937_IG04leaeax,[rsi-0x01]movecx,eaxcmp word ptr [rdi+2*rcx],32jne SHORT G_M44937_IG04cmpeax,esi ; unnecessary checkja SHORT G_M44937_IG06 ;movesi,eaxjmp SHORT G_M44937_IG02G_M44937_IG04: ;; offset=0x001Cmoveax,esipoprbpretG_M44937_IG06: ;; offset=0x0020call[System.ThrowHelper:ThrowArgumentOutOfRangeException()]int3
Reacted by HighPerfDotNet and lindexi
Activity
ghost added
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
It's not a range check, rather an ArgumentOutOfRangeException which is correct here, as the length of the span could be 0, so slicing to Length - 1 = 0 - 1 = -1 isn't allowd, thus the exception.
The span's length cannot be 0 due to the loop condition s.Length != 0. Additionally, accessing s[^1] in the check s[^1] != ' ' further confirms s.Length is non-zero. Thus, slicing to s.Length - 1 is safe, and the ArgumentOutOfRangeException check is unnecessary.
Span is handled differently than array. For array, the range check is introduced by the JIT, and the JIT can decide to not introduce it. For span, the range check is present in IL, and the JIT needs to recognize the asserts and cancel it.
For this issue, the handling of Slice should be a duplicate of #112953.
But this check is redundant, even if we write it manually.
Of course, we could move this condition entirely into the loop condition, which would help in .NET 9. However, in the current build, even this optimization is broken for this case.
Activity
dotnet-policy-service commentedon Apr 27, 2025
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
huoyaoyuan commentedon Apr 27, 2025
Is this duplicate of #115090? The code snippets are the same.
rameel commentedon Apr 27, 2025
No, this is not a duplicate.
Yes, the code snippet is the same, but there are actually two separate issues.
#115090 is about a regression where an extra check being generated which does not exist in
.NET 9.And this issue is about an unnecessary bounds check being generated for the line
s = s.Slice(0, s.Length - 1).gfoidl commentedon Apr 27, 2025
It's not a range check, rather an
ArgumentOutOfRangeExceptionwhich is correct here, as the length of the span could be0, so slicing toLength - 1 = 0 - 1 = -1isn't allowd, thus the exception.rameel commentedon Apr 27, 2025
The span's length cannot be 0 due to the loop condition
s.Length != 0. Additionally, accessings[^1]in the checks[^1] != ' 'further confirmss.Lengthis non-zero. Thus, slicing tos.Length - 1is safe, and theArgumentOutOfRangeExceptioncheck is unnecessary.[-]Unnecessary range check[/-][+]Unnecessary conditional check[/+]huoyaoyuan commentedon Apr 27, 2025
Span is handled differently than array. For array, the range check is introduced by the JIT, and the JIT can decide to not introduce it. For span, the range check is present in IL, and the JIT needs to recognize the asserts and cancel it.
For this issue, the handling of
Sliceshould be a duplicate of #112953.rameel commentedon Apr 27, 2025
Exactly.
Currently, we can assist the JIT with something like:
But this check is redundant, even if we write it manually.
Of course, we could move this condition entirely into the loop condition, which would help in
.NET 9. However, in the current build, even this optimization is broken for this case.godbolt
JulieLeeMSFT commentedon May 6, 2025
CC @dotnet/jit-contrib.
rameel commentedon Oct 30, 2026
It looks like this is already fixed in .NET 11 RC 3