Missed optimization: inliner penalizes std::array vs std::span in lambda
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider the following example:
```c++
#include
#include
#include
#include
struct Foo {
void foo(int total) {
int sum = 0;
auto loop = [&](auto& arr) {
for (int& x : arr) {
int n = std::min(total - sum, x);
sum += n;
x -= n;
if (sum >= total) {
return true;
}
}
return false;
};
if (loop(a_)) return;
if (loop(b_)) return;
if (loop(c_)) return;
}
std::array a_;
std::array b_;
std::array c_;
};
void foo(Foo& f, int total) {
f.foo(total);
}
```
I have two optimization questions:
1. Clang generates an subroutine for `loop(b_)`, which I would expect this to be inlined. Replacing `auto& arr` with `std::span arr` inlines the loop. https://godbolt.org/z/T3h5b3c8Y
2. When the arrays are adjacent in memory and sufficiently large to disable unrolling, would it not be possible to optimize them into a single loop?
Contributor guide
Assessment
This issue has not been assessed yet.