1 // RUN: %check_clang_tidy %s altera-id-dependent-backward-branch %t -- -header-filter=.* "--" -cl-std=CL1.2 -c --include opencl-c.h
2 
3 typedef struct ExampleStruct {
4   int IDDepField;
5 } ExampleStruct;
6 
error()7 void error() {
8   // ==== Conditional Expressions ====
9   int accumulator = 0;
10   for (int i = 0; i < get_local_id(0); i++) {
11     // CHECK-NOTES: :[[@LINE-1]]:19: warning: backward branch (for loop) is ID-dependent due to ID function call and may cause performance degradation [altera-id-dependent-backward-branch]
12     accumulator++;
13   }
14 
15   int j = 0;
16   while (j < get_local_id(0)) {
17     // CHECK-NOTES: :[[@LINE-1]]:10: warning: backward branch (while loop) is ID-dependent due to ID function call and may cause performance degradation [altera-id-dependent-backward-branch]
18     accumulator++;
19   }
20 
21   do {
22     accumulator++;
23   } while (j < get_local_id(0));
24   // CHECK-NOTES: :[[@LINE-1]]:12: warning: backward branch (do loop) is ID-dependent due to ID function call and may cause performance degradation [altera-id-dependent-backward-branch]
25 
26   // ==== Assignments ====
27   int ThreadID = get_local_id(0);
28 
29   while (j < ThreadID) {
30     // CHECK-NOTES: :[[@LINE-3]]:3: note: assignment of ID-dependent variable ThreadID
31     // CHECK-NOTES: :[[@LINE-2]]:10: warning: backward branch (while loop) is ID-dependent due to variable reference to 'ThreadID' and may cause performance degradation [altera-id-dependent-backward-branch]
32     accumulator++;
33   }
34 
35   ExampleStruct Example;
36   Example.IDDepField = get_local_id(0);
37 
38   // ==== Inferred Assignments ====
39   int ThreadID2 = ThreadID * get_local_size(0);
40 
41   int ThreadID3 = Example.IDDepField; // OK: not used in any loops
42 
43   ExampleStruct UnusedStruct = {
44       ThreadID * 2 // OK: not used in any loops
45   };
46 
47   for (int i = 0; i < ThreadID2; i++) {
48     // CHECK-NOTES: :[[@LINE-9]]:3: note: inferred assignment of ID-dependent value from ID-dependent variable ThreadID
49     // CHECK-NOTES: :[[@LINE-2]]:19: warning: backward branch (for loop) is ID-dependent due to variable reference to 'ThreadID2' and may cause performance degradation [altera-id-dependent-backward-branch]
50     accumulator++;
51   }
52 
53   do {
54     accumulator++;
55   } while (j < ThreadID);
56   // CHECK-NOTES: :[[@LINE-29]]:3: note: assignment of ID-dependent variable ThreadID
57   // CHECK-NOTES: :[[@LINE-2]]:12: warning: backward branch (do loop) is ID-dependent due to variable reference to 'ThreadID' and may cause performance degradation [altera-id-dependent-backward-branch]
58 
59   for (int i = 0; i < Example.IDDepField; i++) {
60     // CHECK-NOTES: :[[@LINE-24]]:3: note: assignment of ID-dependent field IDDepField
61     // CHECK-NOTES: :[[@LINE-2]]:19: warning: backward branch (for loop) is ID-dependent due to member reference to 'IDDepField' and may cause performance degradation [altera-id-dependent-backward-branch]
62     accumulator++;
63   }
64 
65   while (j < Example.IDDepField) {
66     // CHECK-NOTES: :[[@LINE-30]]:3: note: assignment of ID-dependent field IDDepField
67     // CHECK-NOTES: :[[@LINE-2]]:10: warning: backward branch (while loop) is ID-dependent due to member reference to 'IDDepField' and may cause performance degradation [altera-id-dependent-backward-branch]
68     accumulator++;
69   }
70 
71   do {
72     accumulator++;
73   } while (j < Example.IDDepField);
74   // CHECK-NOTES: :[[@LINE-38]]:3: note: assignment of ID-dependent field IDDepField
75   // CHECK-NOTES: :[[@LINE-2]]:12: warning: backward branch (do loop) is ID-dependent due to member reference to 'IDDepField' and may cause performance degradation [altera-id-dependent-backward-branch]
76 }
77 
success()78 void success() {
79   int accumulator = 0;
80 
81   for (int i = 0; i < 1000; i++) {
82     if (i < get_local_id(0)) {
83       accumulator++;
84     }
85   }
86 }
87