https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113581
Bug ID: 113581
Summary: Ignoring GCC unroll loop annotation for loops with
increment in condition
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: janschultke at googlemail dot com
Target Milestone: ---
Code to reproduce (https://godbolt.org/z/r6EcW7ocW)
===================================================
template <typename T>
int times_three(T x) {
int sum = 0;
#pragma GCC unroll 16
for (int i = 0; i++ < 3;) {
sum += x;
}
return sum;
}
int main() {
times_three(123u);
}
Output
======
> <source>: In function 'int times_three(T) [with T = unsigned int]':
> <source>:5:19: warning: ignoring loop annotation
> 5 | for (int i = 0; i++ < 3;) {
> | ^
Explanation
===========
It looks like GCC doesn't like loops that have an increment in the condition.
Changing the loop to the following solves this issue:
> for (int i = 0; i < 3; i++)
For increment, this is perhaps not a big deal. I've run into this bug when
working with a loop of the form:
> for (int i = N; i-- > N;)
This is a popular idiom for decrementing with i in range [0, N).