A generic lambda inside a generic function is (currently) instantiated twice. Thus the argument expression of a loop hint inside such a lambda may still be value dependent after the first instantiation and we must protect against this in `TransformLoopHintAttr`. We also fix a bug in this function where the special handling of an unroll(0|1) hint could be applied to *other* loop hints, if they receive an argument 1 after instantiation.
195 lines
6.3 KiB
C++
195 lines
6.3 KiB
C++
// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck --check-prefixes=CHECK,OTHER %s
|
|
|
|
// Check that passing -fno-unroll-loops does not impact the decision made using pragmas.
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - -O1 -disable-llvm-optzns -fno-unroll-loops %s | FileCheck %s
|
|
|
|
// Verify while loop is recognized after unroll pragma.
|
|
void while_test(int *List, int Length) {
|
|
// CHECK: define {{.*}} @_Z10while_test
|
|
int i = 0;
|
|
|
|
#pragma unroll
|
|
while (i < Length) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
|
|
List[i] = i * 2;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
// Verify do loop is recognized after multi-option pragma clang loop directive.
|
|
void do_test(int *List, int Length) {
|
|
// CHECK: define {{.*}} @_Z7do_test
|
|
int i = 0;
|
|
|
|
#pragma nounroll
|
|
do {
|
|
// CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
|
|
List[i] = i * 2;
|
|
i++;
|
|
} while (i < Length);
|
|
}
|
|
|
|
// Verify for loop is recognized after unroll pragma.
|
|
void for_test(int *List, int Length) {
|
|
// CHECK: define {{.*}} @_Z8for_test
|
|
#pragma unroll 8
|
|
for (int i = 0; i < Length; i++) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
|
|
List[i] = i * 2;
|
|
}
|
|
}
|
|
|
|
// Verify c++11 for range loop is recognized after unroll pragma.
|
|
void for_range_test() {
|
|
// CHECK: define {{.*}} @_Z14for_range_test
|
|
double List[100];
|
|
|
|
#pragma unroll(4)
|
|
for (int i : List) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
|
|
List[i] = i;
|
|
}
|
|
}
|
|
|
|
#define UNROLLCOUNT 8
|
|
|
|
// Verify defines are correctly resolved in unroll pragmas.
|
|
void for_define_test(int *List, int Length, int Value) {
|
|
// CHECK: define {{.*}} @_Z15for_define_test
|
|
#pragma unroll(UNROLLCOUNT)
|
|
for (int i = 0; i < Length; i++) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
|
|
List[i] = i * Value;
|
|
}
|
|
}
|
|
|
|
// Verify metadata is generated when template is used.
|
|
template <typename A>
|
|
void for_template_test(A *List, int Length, A Value) {
|
|
// CHECK: define {{.*}} @_Z13template_test
|
|
#pragma unroll 8
|
|
for (int i = 0; i < Length; i++) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
|
|
List[i] = i * Value;
|
|
}
|
|
}
|
|
|
|
// Verify define is resolved correctly when template is used.
|
|
template <typename A>
|
|
void for_template_define_test(A *List, int Length, A Value) {
|
|
// CHECK: define {{.*}} @_Z24for_template_define_test
|
|
|
|
#pragma unroll(UNROLLCOUNT)
|
|
for (int i = 0; i < Length; i++) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_7:.*]]
|
|
List[i] = i * Value;
|
|
}
|
|
}
|
|
|
|
#undef UNROLLCOUNT
|
|
|
|
// Use templates defined above. Test verifies metadata is generated correctly.
|
|
void template_test(double *List, int Length) {
|
|
double Value = 10;
|
|
|
|
for_template_test<double>(List, Length, Value);
|
|
for_template_define_test<double>(List, Length, Value);
|
|
}
|
|
|
|
void for_unroll_zero_test(int *List, int Length) {
|
|
// CHECK: define {{.*}} @_Z20for_unroll_zero_testPii
|
|
#pragma unroll 0
|
|
for (int i = 0; i < Length; i++) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_14:.*]]
|
|
List[i] = i * 2;
|
|
}
|
|
}
|
|
|
|
void while_unroll_zero_test(int *List, int Length) {
|
|
// CHECK: define {{.*}} @_Z22while_unroll_zero_testPii
|
|
int i = 0;
|
|
#pragma unroll(0)
|
|
while (i < Length) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_15:.*]]
|
|
List[i] = i * 2;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
using size_t = unsigned long long;
|
|
|
|
template <bool Flag>
|
|
int value_dependent(int n) {
|
|
// CHECK: define {{.*}} @_Z15value_dependentILb1EEii
|
|
constexpr int N = 100;
|
|
auto init = [=]() { return Flag ? n : 0UL; };
|
|
auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };
|
|
auto iter = [=](size_t ix) {
|
|
return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;
|
|
};
|
|
#pragma unroll Flag ? 1 : N
|
|
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]
|
|
n *= n;
|
|
}
|
|
#pragma unroll Flag ? 0 : N
|
|
for (size_t ix = init(); cond(ix); ix = iter(ix)) {
|
|
// CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]
|
|
n *= n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
void test_value_dependent(int n) {
|
|
value_dependent<true>(n);
|
|
}
|
|
|
|
// Verify that the special treatment of #pragma unroll 0|1 does not affect
|
|
// other loop pragmas with an integer argument, such as vectorize_width or
|
|
// interleave_count.
|
|
|
|
void for_vectorize_one_test(int *List, int Length) {
|
|
// OTHER: define {{.*}} @_Z22for_vectorize_one_testPii
|
|
#pragma clang loop vectorize_width(1)
|
|
for (int i = 0; i < Length; i++) {
|
|
// OTHER: br label {{.*}}, !llvm.loop ![[LOOP_18:.*]]
|
|
List[i] = i * 2;
|
|
}
|
|
}
|
|
|
|
template <int V>
|
|
void for_vectorize_value_dependent(int *List, int Length) {
|
|
// OTHER: define {{.*}} @_Z29for_vectorize_value_dependentILi1EEvPii
|
|
#pragma clang loop vectorize_width(V)
|
|
for (int i = 0; i < Length; i++) {
|
|
// OTHER: br label {{.*}}, !llvm.loop ![[LOOP_19:.*]]
|
|
List[i] = i * 2;
|
|
}
|
|
}
|
|
|
|
template void for_vectorize_value_dependent<1>(int *List, int Length);
|
|
|
|
// CHECK-DAG: ![[MP:[0-9]+]] = !{!"llvm.loop.mustprogress"}
|
|
//
|
|
// CHECK-DAG: ![[UNROLL_ENABLE:[0-9]+]] = !{!"llvm.loop.unroll.enable"}
|
|
// CHECK-DAG: ![[UNROLL_DISABLE:[0-9]+]] = !{!"llvm.loop.unroll.disable"}
|
|
// CHECK-DAG: ![[UNROLL_4:[0-9]+]] = !{!"llvm.loop.unroll.count", i32 4}
|
|
// CHECK-DAG: ![[UNROLL_8:[0-9]+]] = !{!"llvm.loop.unroll.count", i32 8}
|
|
//
|
|
// OTHER-DAG: ![[VEC_1:[0-9]+]] = !{!"llvm.loop.vectorize.width", i32 1}
|
|
// OTHER-DAG: ![[VEC_FIXED:[0-9]+]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
|
|
//
|
|
// CHECK-DAG: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[MP]], ![[UNROLL_ENABLE]]}
|
|
// CHECK-DAG: ![[LOOP_2]] = distinct !{![[LOOP_2]], ![[MP]], ![[UNROLL_DISABLE]]}
|
|
// CHECK-DAG: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[MP]], ![[UNROLL_8]]}
|
|
// CHECK-DAG: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[UNROLL_4]]}
|
|
// CHECK-DAG: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[MP]], ![[UNROLL_8]]}
|
|
// CHECK-DAG: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[MP]], ![[UNROLL_8]]}
|
|
// CHECK-DAG: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[MP]], ![[UNROLL_8]]}
|
|
// CHECK-DAG: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[MP]], ![[UNROLL_DISABLE]]}
|
|
// CHECK-DAG: ![[LOOP_15]] = distinct !{![[LOOP_15]], ![[MP]], ![[UNROLL_DISABLE]]}
|
|
// CHECK-DAG: ![[LOOP_16]] = distinct !{![[LOOP_16]], ![[MP]], ![[UNROLL_DISABLE]]}
|
|
// CHECK-DAG: ![[LOOP_17]] = distinct !{![[LOOP_17]], ![[MP]], ![[UNROLL_DISABLE]]}
|
|
// OTHER-DAG: ![[LOOP_18]] = distinct !{![[LOOP_18]], ![[MP]], ![[VEC_1]], ![[VEC_FIXED]]}
|
|
// OTHER-DAG: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[MP]], ![[VEC_1]], ![[VEC_FIXED]]}
|