dotnet / dotnet/runtime

Bound checks not elided in loop

Open
#131,473 2 comments 0 reactions 1 assignee Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

Similar idea as #121262, but where the check is part of the loop condition

https://godbolt.org/z/Yf1nd4cof
```c#
public static int Sum1(ReadOnlySpan input)
{
int sum = 0;
for (int i = 0; (uint)(i + 2) < (uint)input.Length; i += 3)
{
sum += input[i] + input[i + 1] + input[i + 2]; // All 3 accesses are checked
}
return sum;
}
```
vs
```c#
public static int Sum2(ReadOnlySpan input)
{
int sum = 0;
for (int i = 0; i < input.Length && (uint)(i + 2) < (uint)input.Length; i += 3)
{
sum += input[i] + input[i + 1] + input[i + 2]; // No checks
}
return sum;
}
```

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.