<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/136028>136028</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Using basic loop unrolling concept
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mahmoodn
</td>
</tr>
</table>
<pre>
Hi
I have written a simple loop like this
```
#include <stdio.h>
int main() {
int sum = 0;
#pragma clang loop unroll(full)
for (int i = 0; i < 4; i++) {
sum += i;
}
printf("Sum is %d\n", sum);
return 0;
}
```
and used
```
clang -O0 -emit-llvm -S -Xclang -disable-O0-optnone loop.c -o loop.ll
```
to generate the IR format. The content of loop.ll file is
```
; ModuleID = 'loop.c'
source_filename = "loop.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@.str = private unnamed_addr constant [11 x i8] c"Sum is %d\0A\00", align 1
; Function Attrs: noinline nounwind uwtable
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%3 = alloca i32, align 4
store i32 0, ptr %1, align 4
store i32 0, ptr %2, align 4
store i32 0, ptr %3, align 4
br label %4
4: ; preds = %11, %0
%5 = load i32, ptr %3, align 4
%6 = icmp slt i32 %5, 4
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i32, ptr %3, align 4
%9 = load i32, ptr %2, align 4
%10 = add nsw i32 %9, %8
store i32 %10, ptr %2, align 4
br label %11
11: ; preds = %7
%12 = load i32, ptr %3, align 4
%13 = add nsw i32 %12, 1
store i32 %13, ptr %3, align 4
br label %4, !llvm.loop !6
14: ; preds = %4
%15 = load i32, ptr %2, align 4
%16 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %15)
ret i32 0
}
declare i32 @printf(ptr noundef, ...) #1
attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"clang version 21.0.0git (https://github.com/llvm/llvm-project d0c973a7a0149db3b71767d4c5a20a31e6a8ed5b)"}
!6 = distinct !{!6, !7, !8}
!7 = !{!"llvm.loop.mustprogress"}
!8 = !{!"llvm.loop.unroll.full"}
```
Then I used
```
opt -passes='loop-unroll' -S loop.ll -o loop_unrolled.ll
```
to use the unroll pass. I expect to see four add instruction without br because the loop has been unrolled. However, The content of loop.ll and loop_unrolled.ll are exactly the same and there is no sign of "unrolled" pass. I understand that maybe the loop is so small that may not reach the requirements for llvm unrolling. But on the other hand I had specified the `#pragma` directive, so I expect that llvm is forced to unroll the loop whatever it is.
I am using clang version 21.0.0git. Any idea about that?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWEtv4zjy_zT0pSCBop4--GAnE0yA_x-z2J4F9hZQUtnmtkRqScp276dfFPWIk056us8bGA4l_urJYj0snVMnjbhj-YHljxs5-rOxu16ee2NavalN-233u2J8_wxneUG4WuU9apDgVD90CJ0xA3TqK4I_K8f4nhV8_vA9E6nSTTe2CCx9cL5VJj6z9DfG90p76KXSTFRMbIGVB8b3AAC04cYeWPoInKXLaybSwcpTL6HppD5NckdtTdcxUR1H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