<div dir="ltr"><div>Awesome, thanks! <br></div><div><br></div><div>Now I have another question. I have a matrix multiplication code. This is my code:</div><div><br></div><div>#include <stdio.h><br>#include <stdlib.h><br><br>#define n 4<br><br>int main(int argc, char *argv[]) {<br> int i, j, k;<br> <br> int A[n][n], B[n][n], C[n][n]; <br> for(i=0;i<n;i++){<br> for(j=0;j<n;j++){<br> A[i][j] = 1;<br> B[i][j] = 2;<br> C[i][j] = 0;<br> }<br> } <br> <br> for(i=0;i<n;i++){<br> for(j=0;j<n;j++){<br> for(k=0;k<n;k++){<br> C[i][j]=(C[i][j]+(A[i][k]*B[k][j]));<br> }<br> }<br> }<br> <br> return 0;<br>}</div><div><br></div><div><br></div><div>I tried over them the loop-unroll-and-jam pass. I run:</div><div><br></div><div>$ clang -O0 -Xclang -disable-O0-optnone -emit-llvm mult.c -S -o mult.ll<br>$ opt -O0 -S -mem2reg -simplifycfg -view-cfg mult.ll -o multopt.ll<br>$ opt -O0 -S -mem2reg -simplifycfg --loop-unroll-and-jam --unroll-and-jam-count=4 -simplifycfg -view-cfg mult.ll -o mult-opt00-unroll4.ll</div><div><br></div><div>I get the same CFG graph in two case (I attach it). Also, I tried with -O1 level (opt -01 -loop-unroll-and-jam --unroll-and-jam-count=4), but I didn't get any differece. <br></div><div><br></div><div>Why the pass --loop-unroll-and-jam does not work?</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El dom., 24 may. 2020 a las 14:36, Florian Hahn (<<a href="mailto:florian_hahn@apple.com">florian_hahn@apple.com</a>>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><br><div><br><blockquote type="cite"><div>On May 23, 2020, at 17:15, legend xx <<a href="mailto:legendaryxx7slh@gmail.com" target="_blank">legendaryxx7slh@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>This is my example (for.c):</div><div><br></div><div>#include <stdio.h><br><br>int add(int a, int b) {<br> return a + b;<br>}<br><br>int main() {<br> int a, b, c, d;<br> a = 5;<br> b = 15;<br> c = add(a, b);<br> d = 0;<br> for(int i=0;i<16;i++)<br> d = add(c, d);<br>}</div><div><br></div><div>I run:</div><div>$ clang -O0 -Xclang -disable-O0-optnone -emit-llvm for.c -S -o forO0.ll<br>$ opt -O0 -S --loop-unroll --unroll-count=4 -view-cfg forO0.ll -o for-opt00-unroll4.ll</div><div><br></div><div>And this is the LLVM IR code that I get: <br></div><div><br></div><div>; ModuleID = 'forO0.ll'<br>source_filename = "for.c"<br>target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"<br>target triple = "x86_64-unknown-linux-gnu"<br><br>; Function Attrs: noinline nounwind uwtable<br>define dso_local i32 @add(i32 %a, i32 %b) #0 {<br>entry:<br> %a.addr = alloca i32, align 4<br> %b.addr = alloca i32, align 4<br> store i32 %a, i32* %a.addr, align 4<br> store i32 %b, i32* %b.addr, align 4<br> %0 = load i32, i32* %a.addr, align 4<br> %1 = load i32, i32* %b.addr, align 4<br> %add = add nsw i32 %0, %1<br> ret i32 %add<br>}<br><br>; Function Attrs: noinline nounwind uwtable<br>define dso_local i32 @main() #0 {<br>entry:<br> %retval = alloca i32, align 4<br> %a = alloca i32, align 4<br> %b = alloca i32, align 4<br> %c = alloca i32, align 4<br> %d = alloca i32, align 4<br> %i = alloca i32, align 4<br> store i32 0, i32* %retval, align 4<br> store i32 5, i32* %a, align 4<br> store i32 15, i32* %b, align 4<br> %0 = load i32, i32* %a, align 4<br> %1 = load i32, i32* %b, align 4<br> %call = call i32 @add(i32 %0, i32 %1)<br> store i32 %call, i32* %c, align 4<br> store i32 0, i32* %d, align 4<br> store i32 0, i32* %i, align 4<br> br label %for.cond<br><br>for.cond: ; preds = %for.inc.3, %entry<br> %2 = load i32, i32* %i, align 4<br> %cmp = icmp slt i32 %2, 16<br> br i1 %cmp, label %for.body, label %for.end<br><br>for.body: ; preds = %for.cond<br> %3 = load i32, i32* %c, align 4<br> %4 = load i32, i32* %d, align 4<br> %call1 = call i32 @add(i32 %3, i32 %4)<br> store i32 %call1, i32* %d, align 4<br> br label %for.inc<br><br>for.inc: ; preds = %for.body<br> %5 = load i32, i32* %i, align 4<br> %inc = add nsw i32 %5, 1<br> store i32 %inc, i32* %i, align 4<br> %6 = load i32, i32* %i, align 4<br> %cmp.1 = icmp slt i32 %6, 16<br> br i1 %cmp.1, label %for.body.1, label %for.end<br><br>for.end: ; preds = %for.inc.2, %for.inc.1, %for.inc, %for.cond<br> %7 = load i32, i32* %d, align 4<br> %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7)<br> %8 = load i32, i32* %retval, align 4<br> ret i32 %8<br><br>for.body.1: ; preds = %for.inc<br> %9 = load i32, i32* %c, align 4<br> %10 = load i32, i32* %d, align 4<br> %call1.1 = call i32 @add(i32 %9, i32 %10)<br> store i32 %call1.1, i32* %d, align 4<br> br label %for.inc.1<br><br>for.inc.1: ; preds = %for.body.1<br> %11 = load i32, i32* %i, align 4<br> %inc.1 = add nsw i32 %11, 1<br> store i32 %inc.1, i32* %i, align 4<br> %12 = load i32, i32* %i, align 4<br> %cmp.2 = icmp slt i32 %12, 16<br> br i1 %cmp.2, label %for.body.2, label %for.end<br><br>for.body.2: ; preds = %for.inc.1<br> %13 = load i32, i32* %c, align 4<br> %14 = load i32, i32* %d, align 4<br> %call1.2 = call i32 @add(i32 %13, i32 %14)<br> store i32 %call1.2, i32* %d, align 4<br> br label %for.inc.2<br><br>for.inc.2: ; preds = %for.body.2<br> %15 = load i32, i32* %i, align 4<br> %inc.2 = add nsw i32 %15, 1<br> store i32 %inc.2, i32* %i, align 4<br> %16 = load i32, i32* %i, align 4<br> %cmp.3 = icmp slt i32 %16, 16<br> br i1 %cmp.3, label %for.body.3, label %for.end<br><br>for.body.3: ; preds = %for.inc.2<br> %17 = load i32, i32* %c, align 4<br> %18 = load i32, i32* %d, align 4<br> %call1.3 = call i32 @add(i32 %17, i32 %18)<br> store i32 %call1.3, i32* %d, align 4<br> br label %for.inc.3<br><br>for.inc.3: ; preds = %for.body.3<br> %19 = load i32, i32* %i, align 4<br> %inc.3 = add nsw i32 %19, 1<br> store i32 %inc.3, i32* %i, align 4<br> br label %for.cond, !llvm.loop !2<br>}<br><br>declare dso_local i32 @printf(i8*, ...) #1<br><br>attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }<br>attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }<br><br>!llvm.module.flags = !{!0}<br>!llvm.ident = !{!1}<br><br>!0 = !{i32 1, !"wchar_size", i32 4}<br>!1 = !{!"clang version 11.0.0 (<a href="https://github.com/llvm/llvm-project.git" target="_blank">https://github.com/llvm/llvm-project.git</a> a3485301d4870f57590d7b69eed7959134a694ab)"}<br>!2 = distinct !{!2, !3}<br>!3 = !{!"llvm.loop.unroll.disable"}</div><div><br></div><div><br></div><div>So my problem is:</div><div>With unroll 4 on the loop with 16 bounds I should see one single
block for the incrementation i=i+4, then 4 instructions for each
previous one instruction, and the condition should check if i<16. This is the intuitive code. However, the incrementation that I get is i=i+1 and there are only 4 blocks. <br>
</div><div> </div><div><br></div><div>Do you know why this happen?</div></div></div></blockquote><br></div><div>I think loop-unroll works as expected in your example, as you can see the copies of the unrolled loop blocks (for.body.X, for.inc.X). The reason this is not simplified to the single block you are expecting is the input for -loop-unroll: -loop-unroll gets the IR without any optimizations (-O0). </div><div><br></div><div>For the expected result, you need to run a few additional passes before -loop-unroll to promote some of the loads/stores to registers and simplify the CFG of the input. Running `opt -mem2reg -simplifycfg -loop-unroll -unroll-count=4 forO0.ll -S` should give you something like</div><div><br></div><div><div>define i32 @main() #0 {</div><div>entry:</div><div> %call = call i32 @add(i32 5, i32 15)</div><div> br label %for.cond</div><div><br></div><div>for.cond: ; preds = %for.body.3, %entry</div><div> %d.0 = phi i32 [ 0, %entry ], [ %call1.3, %for.body.3 ]</div><div> %i.0 = phi i32 [ 0, %entry ], [ %inc.3, %for.body.3 ]</div><div> %cmp = icmp ult i32 %i.0, 16</div><div> br i1 %cmp, label %for.body, label %for.end</div><div><br></div><div>for.body: ; preds = %for.cond</div><div> %call1 = call i32 @add(i32 %call, i32 %d.0)</div><div> %inc = add nuw nsw i32 %i.0, 1</div><div> br label %for.body.1</div><div><br></div><div>for.end: ; preds = %for.cond</div><div> ret i32 0</div><div><br></div><div>for.body.1: ; preds = %for.body</div><div> %call1.1 = call i32 @add(i32 %call, i32 %call1)</div><div> %inc.1 = add nuw nsw i32 %inc, 1</div><div> br label %for.body.2</div><div><br></div><div>for.body.2: ; preds = %for.body.1</div><div> %call1.2 = call i32 @add(i32 %call, i32 %call1.1)</div><div> %inc.2 = add nuw nsw i32 %inc.1, 1</div><div> br label %for.body.3</div><div><br></div><div>for.body.3: ; preds = %for.body.2</div><div> %call1.3 = call i32 @add(i32 %call, i32 %call1.2)</div><div> %inc.3 = add nuw nsw i32 %inc.2, 1</div><div> br label %for.cond, !llvm.loop !4</div><div>}</div><div><br></div><div>Note that there are still 4 copies of the body instead of a single one. Like many passes in LLVM, the loop-unroll pass focuses on performing one transformation (duplicating the loop body a number of times) and relies on other passes to clean-up/simplify the result. To fold the 4 copies of the body into a single block, you need another round of CFG simplifications. Running `opt -mem2reg -simplifycfg -loop-unroll -unroll-count=4 -simplifycfg forO0.ll -S` produces the code below, which is what you are looking for IIUC.</div><div><br></div><div><div>define i32 @main() #0 {</div><div>entry:</div><div> %call = call i32 @add(i32 5, i32 15)</div><div> br label %for.cond</div><div><br></div><div>for.cond: ; preds = %for.body, %entry</div><div> %d.0 = phi i32 [ 0, %entry ], [ %call1.3, %for.body ]</div><div> %i.0 = phi i32 [ 0, %entry ], [ %inc.3, %for.body ]</div><div> %cmp = icmp ult i32 %i.0, 16</div><div> br i1 %cmp, label %for.body, label %for.end</div><div><br></div><div>for.body: ; preds = %for.cond</div><div> %call1 = call i32 @add(i32 %call, i32 %d.0)</div><div> %inc = add nuw nsw i32 %i.0, 1</div><div> %call1.1 = call i32 @add(i32 %call, i32 %call1)</div><div> %inc.1 = add nuw nsw i32 %inc, 1</div><div> %call1.2 = call i32 @add(i32 %call, i32 %call1.1)</div><div> %inc.2 = add nuw nsw i32 %inc.1, 1</div><div> %call1.3 = call i32 @add(i32 %call, i32 %call1.2)</div><div> %inc.3 = add nuw nsw i32 %inc.2, 1</div><div> br label %for.cond, !llvm.loop !4</div><div><br></div><div>for.end: ; preds = %for.cond</div><div> ret i32 0</div><div>}</div></div></div><br></div></blockquote></div>