-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[flang] Add -f[no-]unroll-loops flag #122906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
c9b2e58
d66a7c6
ac03aad
f27462b
0141e0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
! RUN: %flang -### -funroll-loops %s 2>&1 | FileCheck %s -check-prefix UNROLL | ||
! RUN: %flang -### -fno-unroll-loops %s 2>&1 | FileCheck %s -check-prefix NO-UNROLL | ||
|
||
! UNROLL: "-funroll-loops" | ||
! NO-UNROLL: "-fno-unroll-loops" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: %flang_fc1 -emit-llvm -O1 -funroll-loops -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,UNROLL | ||
// RUN: %flang_fc1 -emit-llvm -O2 -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,UNROLL | ||
// RUN: %flang_fc1 -emit-llvm -O1 -fno-unroll-loops -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,NO-UNROLL | ||
// RUN: %flang_fc1 -emit-llvm -O1 -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,NO-UNROLL | ||
|
||
// CHECK-LABEL: @unroll | ||
// CHECK-SAME: (ptr nocapture writeonly %[[ARG0:.*]]) | ||
func.func @unroll(%arg0: !fir.ref<!fir.array<1000xf64>> {fir.bindc_name = "a"}) { | ||
%scope = fir.dummy_scope : !fir.dscope | ||
%c1000 = arith.constant 1000 : index | ||
%shape = fir.shape %c1000 : (index) -> !fir.shape<1> | ||
%a:2 = hlfir.declare %arg0(%shape) dummy_scope %scope {uniq_name = "unrollEa"} : (!fir.ref<!fir.array<1000xf64>>, !fir.shape<1>, !fir.dscope) -> (!fir.ref<!fir.array<1000xf64>>, !fir.ref<!fir.array<1000xf64>>) | ||
%c1 = arith.constant 1 : index | ||
fir.do_loop %arg1 = %c1 to %c1000 step %c1 { | ||
// CHECK: br label %[[BLK:.*]] | ||
// CHECK: [[BLK]]: | ||
// CHECK-NEXT: %[[IND:.*]] = phi i64 [ 0, %{{.*}} ], [ %[[NIV:.*]], %[[BLK]] ] | ||
// CHECK-NEXT: %[[VIND:.*]] = phi <2 x i64> [ <i64 1, i64 2>, %{{.*}} ], [ %[[NVIND:.*]], %[[BLK]] ] | ||
|
||
// NO-UNROLL-NEXT: %[[IV_D:.*]] = uitofp nneg <2 x i64> %[[VIND]] to <2 x double> | ||
// NO-UNROLL-NEXT: %[[GEP:.*]] = getelementptr double, ptr %[[ARG0]], i64 %[[IND]] | ||
// NO-UNROLL-NEXT: store <2 x double> %[[IV_D]], ptr %[[GEP]] | ||
// NO-UNROLL-NEXT: %[[NIV:.*]] = add nuw i64 %{{.*}}, 2 | ||
// NO-UNROLL-NEXT: %[[NVIND]] = add <2 x i64> %[[VIND]], splat (i64 2) | ||
|
||
// UNROLL-NEXT: %[[VIND1:.*]] = add <2 x i64> %[[VIND]], splat (i64 2) | ||
// UNROLL-NEXT: %[[IV0_D:.*]] = uitofp nneg <2 x i64> %[[VIND]] to <2 x double> | ||
// UNROLL-NEXT: %[[IV1_D:.*]] = uitofp nneg <2 x i64> %[[VIND1]] to <2 x double> | ||
// UNROLL-NEXT: %[[GEP0:.*]] = getelementptr double, ptr %[[ARG0]], i64 %[[IND]] | ||
// UNROLL-NEXT: %[[GEP1:.*]] = getelementptr i8, ptr %[[GEP0]], i64 16 | ||
// UNROLL-NEXT: store <2 x double> %[[IV0_D]], ptr %[[GEP0]] | ||
// UNROLL-NEXT: store <2 x double> %[[IV1_D]], ptr %[[GEP1]] | ||
// UNROLL-NEXT: %[[NIV:.*]] = add nuw i64 %[[IND]], 4 | ||
// UNROLL-NEXT: %[[NVIND:.*]] = add <2 x i64> %[[VIND]], splat (i64 4) | ||
|
||
// CHECK-NEXT: %[[EXIT:.*]] = icmp eq i64 %[[NIV]], 1000 | ||
// CHECK-NEXT: br i1 %[[EXIT]], label %{{.*}}, label %[[BLK]] | ||
%iv = fir.convert %arg1 : (index) -> f64 | ||
%ai = hlfir.designate %a#0 (%arg1) : (!fir.ref<!fir.array<1000xf64>>, index) -> !fir.ref<f64> | ||
hlfir.assign %iv to %ai : f64, !fir.ref<f64> | ||
} | ||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
! RUN: %flang_fc1 -emit-llvm -O1 -funroll-loops -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,UNROLL | ||
! RUN: %flang_fc1 -emit-llvm -O2 -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,UNROLL | ||
! RUN: %flang_fc1 -emit-llvm -O1 -fno-unroll-loops -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,NO-UNROLL | ||
! RUN: %flang_fc1 -emit-llvm -O1 -mllvm -force-vector-width=2 -o- %s | FileCheck %s --check-prefixes=CHECK,NO-UNROLL | ||
|
||
! CHECK-LABEL: @unroll | ||
! CHECK-SAME: (ptr nocapture writeonly %[[ARG0:.*]]) | ||
subroutine unroll(a) | ||
real(kind=8), intent(out) :: a(1000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be an integer array to avoid the |
||
integer(kind=8) :: i | ||
! CHECK: br label %[[BLK:.*]] | ||
! CHECK: [[BLK]]: | ||
! CHECK-NEXT: %[[IND:.*]] = phi i64 [ 0, %{{.*}} ], [ %[[NIV:.*]], %[[BLK]] ] | ||
! CHECK-NEXT: %[[VIND:.*]] = phi <2 x i64> [ <i64 1, i64 2>, %{{.*}} ], [ %[[NVIND:.*]], %[[BLK]] ] | ||
! | ||
! NO-UNROLL-NEXT: %[[IV_D:.*]] = uitofp nneg <2 x i64> %[[VIND]] to <2 x double> | ||
! NO-UNROLL-NEXT: %[[GEP:.*]] = getelementptr double, ptr %[[ARG0]], i64 %[[IND]] | ||
! NO-UNROLL-NEXT: store <2 x double> %[[IV_D]], ptr %[[GEP]] | ||
! NO-UNROLL-NEXT: %[[NIV:.*]] = add nuw i64 %{{.*}}, 2 | ||
! NO-UNROLL-NEXT: %[[NVIND]] = add <2 x i64> %[[VIND]], splat (i64 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unroll should ideally check for the branch back to the body. And nounroll should probably check that there is no such branch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For small loops, it will fully unroll if the body is small. The issue with checking for two iterations is that this could be due to interleaving during vectorisation as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually did try to only have (e.g.) 2 iterations of the loop; for some reason the full loop is still emitted, at least at -O1. What I ended up with here was essentially copied from the clang |
||
! | ||
! UNROLL-NEXT: %[[VIND1:.*]] = add <2 x i64> %[[VIND]], splat (i64 2) | ||
! UNROLL-NEXT: %[[IV0_D:.*]] = uitofp nneg <2 x i64> %[[VIND]] to <2 x double> | ||
! UNROLL-NEXT: %[[IV1_D:.*]] = uitofp nneg <2 x i64> %[[VIND1]] to <2 x double> | ||
! UNROLL-NEXT: %[[GEP0:.*]] = getelementptr double, ptr %[[ARG0]], i64 %[[IND]] | ||
! UNROLL-NEXT: %[[GEP1:.*]] = getelementptr i8, ptr %[[GEP0]], i64 16 | ||
! UNROLL-NEXT: store <2 x double> %[[IV0_D]], ptr %[[GEP0]] | ||
! UNROLL-NEXT: store <2 x double> %[[IV1_D]], ptr %[[GEP1]] | ||
! UNROLL-NEXT: %[[NIV:.*]] = add nuw i64 %[[IND]], 4 | ||
! UNROLL-NEXT: %[[NVIND:.*]] = add <2 x i64> %[[VIND]], splat (i64 4) | ||
! | ||
! CHECK-NEXT: %[[EXIT:.*]] = icmp eq i64 %[[NIV]], 1000 | ||
! CHECK-NEXT: br i1 %[[EXIT]], label %{{.*}}, label %[[BLK]] | ||
do i=1,1000 | ||
a(i) = i | ||
end do | ||
end subroutine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a forwarding test from the driver to the frontend driver.