llvm / llvm/llvm-project

Missed optimization: inliner penalizes std::array vs std::span in lambda

Open
#187,968 2 comments 0 reactions 0 assignees View on GitHub
clang:codegen loopoptim missed-optimization
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

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.