Span index access not eliminating bounds checks depending on access order #116088
Labels
area-CodeGen-coreclr
CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone
Description
When accessing span items by Index from end, some patterns do not eliminate the bounds check.
Reproduction Steps
Access a span by both its' first and last item. Both accesses incur an index bounds check, despite the
s[0]
ensuring the span is non-empty. Accessings[^1]
first eliminates the second bounds check.IsEmpty
and an explicitLength != 0
checks seem to work properly, e.g.s.IsEmpty ? -1 : s[0]
.Expected behavior
Actual behavior
Both accesses cause a range check.
Regression?
No response
Known Workarounds
Swap the operations or use unsafe code for the second access (which generates roughly the same ASM as the swapped version). This is obviously not a huge deal unless you are writing high-performance code without resorting to
Unsafe
& friends.Configuration
Can also be seen on https://godbolt.org/z/4q66bo36n and on arm64 arch as well
Other information
No response
The text was updated successfully, but these errors were encountered: