162 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s2 3// Check that passing -fno-unroll-loops does not impact the decision made using pragmas.4// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - -O1 -disable-llvm-optzns -fno-unroll-loops %s | FileCheck %s5 6// Verify while loop is recognized after unroll pragma.7void while_test(int *List, int Length) {8 // CHECK: define {{.*}} @_Z10while_test9 int i = 0;10 11#pragma unroll12 while (i < Length) {13 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_1:.*]]14 List[i] = i * 2;15 i++;16 }17}18 19// Verify do loop is recognized after multi-option pragma clang loop directive.20void do_test(int *List, int Length) {21 // CHECK: define {{.*}} @_Z7do_test22 int i = 0;23 24#pragma nounroll25 do {26 // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]27 List[i] = i * 2;28 i++;29 } while (i < Length);30}31 32// Verify for loop is recognized after unroll pragma.33void for_test(int *List, int Length) {34// CHECK: define {{.*}} @_Z8for_test35#pragma unroll 836 for (int i = 0; i < Length; i++) {37 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_3:.*]]38 List[i] = i * 2;39 }40}41 42// Verify c++11 for range loop is recognized after unroll pragma.43void for_range_test() {44 // CHECK: define {{.*}} @_Z14for_range_test45 double List[100];46 47#pragma unroll(4)48 for (int i : List) {49 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_4:.*]]50 List[i] = i;51 }52}53 54#define UNROLLCOUNT 855 56// Verify defines are correctly resolved in unroll pragmas.57void for_define_test(int *List, int Length, int Value) {58// CHECK: define {{.*}} @_Z15for_define_test59#pragma unroll(UNROLLCOUNT)60 for (int i = 0; i < Length; i++) {61 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_5:.*]]62 List[i] = i * Value;63 }64}65 66// Verify metadata is generated when template is used.67template <typename A>68void for_template_test(A *List, int Length, A Value) {69// CHECK: define {{.*}} @_Z13template_test70#pragma unroll 871 for (int i = 0; i < Length; i++) {72 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_6:.*]]73 List[i] = i * Value;74 }75}76 77// Verify define is resolved correctly when template is used.78template <typename A>79void for_template_define_test(A *List, int Length, A Value) {80// CHECK: define {{.*}} @_Z24for_template_define_test81 82#pragma unroll(UNROLLCOUNT)83 for (int i = 0; i < Length; i++) {84 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_7:.*]]85 List[i] = i * Value;86 }87}88 89#undef UNROLLCOUNT90 91// Use templates defined above. Test verifies metadata is generated correctly.92void template_test(double *List, int Length) {93 double Value = 10;94 95 for_template_test<double>(List, Length, Value);96 for_template_define_test<double>(List, Length, Value);97}98 99void for_unroll_zero_test(int *List, int Length) {100 // CHECK: define {{.*}} @_Z20for_unroll_zero_testPii101 #pragma unroll 0102 for (int i = 0; i < Length; i++) {103 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_14:.*]]104 List[i] = i * 2;105 }106}107 108void while_unroll_zero_test(int *List, int Length) {109 // CHECK: define {{.*}} @_Z22while_unroll_zero_testPii110 int i = 0;111#pragma unroll(0)112 while (i < Length) {113 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_15:.*]]114 List[i] = i * 2;115 i++;116 }117}118 119using size_t = unsigned long long;120 121template <bool Flag>122int value_dependent(int n) {123 // CHECK: define {{.*}} @_Z15value_dependentILb1EEii124 constexpr int N = 100;125 auto init = [=]() { return Flag ? n : 0UL; };126 auto cond = [=](size_t ix) { return Flag ? ix != 0 : ix < 10; };127 auto iter = [=](size_t ix) {128 return Flag ? ix & ~(1ULL << __builtin_clzll(ix)) : ix + 1;129 };130#pragma unroll Flag ? 1 : N131 for (size_t ix = init(); cond(ix); ix = iter(ix)) {132 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]133 n *= n;134 }135#pragma unroll Flag ? 0 : N136 for (size_t ix = init(); cond(ix); ix = iter(ix)) {137 // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]138 n *= n;139 }140 return n;141}142 143void test_value_dependent(int n) {144 value_dependent<true>(n);145}146 147// CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], [[MP:![0-9]+]], ![[UNROLL_ENABLE:.*]]}148// CHECK: ![[UNROLL_ENABLE]] = !{!"llvm.loop.unroll.enable"}149// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]]}150// CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}151// CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], [[MP]], ![[UNROLL_8:.*]]}152// CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}153// CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[UNROLL_4:.*]]}154// CHECK: ![[UNROLL_4]] = !{!"llvm.loop.unroll.count", i32 4}155// CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_8:.*]]}156// CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[UNROLL_8:.*]]}157// CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[UNROLL_8:.*]]}158// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], [[MP]], ![[UNROLL_DISABLE:.*]]}159// CHECK: ![[LOOP_15]] = distinct !{![[LOOP_15]], [[MP]], ![[UNROLL_DISABLE:.*]]}160// CHECK: ![[LOOP_16]] = distinct !{![[LOOP_16]], [[MP]], ![[UNROLL_DISABLE:.*]]}161// CHECK: ![[LOOP_17]] = distinct !{![[LOOP_17]], [[MP]], ![[UNROLL_DISABLE:.*]]}162