<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/80586>80586</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang]: aggressive loop unrolling on different optimization level
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
rilysh
</td>
</tr>
</table>
<pre>
While testing some other things, I noticed that Clang aggressively unroll loops, when optimizations are enabled (-O{2, 3, fast}).
For example,
```c
#include <stdio.h>
#include <ctype.h>
static void f(const char *s)
{
const char *p;
for (p = s; *p; p++)
fputc(toupper(*p), stdout);
}
int main(void)
{
/* with -O3 */
f("hello world!hello worldworld!");
/* with -O2 */
f("hello world!12");
fputc('\n', stdout);
}
```
Compiling it with (-O3) shows that clang has unrolled that for loop, and filled it with fputc() function calls. However, compiling with (-O2) doesn't unroll the first loop but does for the second loop. It seems LLVM has some kind of threshold, and if the threshold is lower or equal it unrolls the loops.
Comparing with GCC, it doesn't unroll the loop in this case and generates almost identical code when Clang doesn't unroll. Is this intentional behavior?
Godbolt test: https://godbolt.org/z/qeM6c6Gfa
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVE1z4jgT_jXi0hXKkQHjA4cAL3lTNVNz2z0LqY20K0sedRs28-u3JENCUjO7S7kkpP56nlZ3KyJ3CogbsdyK5X6mRrYxbZLzr2Rnx2heN79b5xEYiV04AcUeIbLFBGxdOJGQO3iBENlpNMBWMey8CidQp1NCIndG_wpjSNF78DEOxeJiMUAc2PXuh2IXA4FKCBjU0aMBIdcP30SzlVm3zkuniEWzF7Kdi2ovqqdpPcQE-JfqB49C7q6SVTV9-nqWtQvajwZB1Dti4-Lcivp_P5Nqfh3wXlpWYsVOwzk6A52Qax0DMWirEgj5REK2V-1mO_2B6--j4iDq7b3bm1YXs3g9gKj3QKLe3pRhEHJbvvajRbEaRtZCrjmOw4BJyHWxkm1O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