xref: /llvm-project/openmp/runtime/test/transform/unroll/factor_foreach.cpp (revision 9120562dfcc09cb4caf3052c6744049b4d9c8481)
1*9120562dSMichael Kruse // RUN: %libomp-cxx20-compile-and-run | FileCheck %s --match-full-lines
2*9120562dSMichael Kruse 
3*9120562dSMichael Kruse #ifndef HEADER
4*9120562dSMichael Kruse #define HEADER
5*9120562dSMichael Kruse 
6*9120562dSMichael Kruse #include <cstdlib>
7*9120562dSMichael Kruse #include <cstdarg>
8*9120562dSMichael Kruse #include <cstdio>
9*9120562dSMichael Kruse #include <vector>
10*9120562dSMichael Kruse 
11*9120562dSMichael Kruse struct Reporter {
12*9120562dSMichael Kruse   const char *name;
13*9120562dSMichael Kruse 
ReporterReporter14*9120562dSMichael Kruse   Reporter(const char *name) : name(name) { print("ctor"); }
15*9120562dSMichael Kruse 
ReporterReporter16*9120562dSMichael Kruse   Reporter() : name("<anon>") { print("ctor"); }
17*9120562dSMichael Kruse 
ReporterReporter18*9120562dSMichael Kruse   Reporter(const Reporter &that) : name(that.name) { print("copy ctor"); }
19*9120562dSMichael Kruse 
ReporterReporter20*9120562dSMichael Kruse   Reporter(Reporter &&that) : name(that.name) { print("move ctor"); }
21*9120562dSMichael Kruse 
~ReporterReporter22*9120562dSMichael Kruse   ~Reporter() { print("dtor"); }
23*9120562dSMichael Kruse 
operator =Reporter24*9120562dSMichael Kruse   const Reporter &operator=(const Reporter &that) {
25*9120562dSMichael Kruse     print("copy assign");
26*9120562dSMichael Kruse     this->name = that.name;
27*9120562dSMichael Kruse     return *this;
28*9120562dSMichael Kruse   }
29*9120562dSMichael Kruse 
operator =Reporter30*9120562dSMichael Kruse   const Reporter &operator=(Reporter &&that) {
31*9120562dSMichael Kruse     print("move assign");
32*9120562dSMichael Kruse     this->name = that.name;
33*9120562dSMichael Kruse     return *this;
34*9120562dSMichael Kruse   }
35*9120562dSMichael Kruse 
36*9120562dSMichael Kruse   struct Iterator {
37*9120562dSMichael Kruse     const Reporter *owner;
38*9120562dSMichael Kruse     int pos;
39*9120562dSMichael Kruse 
IteratorReporter::Iterator40*9120562dSMichael Kruse     Iterator(const Reporter *owner, int pos) : owner(owner), pos(pos) {}
41*9120562dSMichael Kruse 
IteratorReporter::Iterator42*9120562dSMichael Kruse     Iterator(const Iterator &that) : owner(that.owner), pos(that.pos) {
43*9120562dSMichael Kruse       owner->print("iterator copy ctor");
44*9120562dSMichael Kruse     }
45*9120562dSMichael Kruse 
IteratorReporter::Iterator46*9120562dSMichael Kruse     Iterator(Iterator &&that) : owner(that.owner), pos(that.pos) {
47*9120562dSMichael Kruse       owner->print("iterator move ctor");
48*9120562dSMichael Kruse     }
49*9120562dSMichael Kruse 
~IteratorReporter::Iterator50*9120562dSMichael Kruse     ~Iterator() { owner->print("iterator dtor"); }
51*9120562dSMichael Kruse 
operator =Reporter::Iterator52*9120562dSMichael Kruse     const Iterator &operator=(const Iterator &that) {
53*9120562dSMichael Kruse       owner->print("iterator copy assign");
54*9120562dSMichael Kruse       this->owner = that.owner;
55*9120562dSMichael Kruse       this->pos = that.pos;
56*9120562dSMichael Kruse       return *this;
57*9120562dSMichael Kruse     }
58*9120562dSMichael Kruse 
operator =Reporter::Iterator59*9120562dSMichael Kruse     const Iterator &operator=(Iterator &&that) {
60*9120562dSMichael Kruse       owner->print("iterator move assign");
61*9120562dSMichael Kruse       this->owner = that.owner;
62*9120562dSMichael Kruse       this->pos = that.pos;
63*9120562dSMichael Kruse       return *this;
64*9120562dSMichael Kruse     }
65*9120562dSMichael Kruse 
operator ==Reporter::Iterator66*9120562dSMichael Kruse     bool operator==(const Iterator &that) const {
67*9120562dSMichael Kruse       owner->print("iterator %d == %d", 2 - this->pos, 2 - that.pos);
68*9120562dSMichael Kruse       return this->pos == that.pos;
69*9120562dSMichael Kruse     }
70*9120562dSMichael Kruse 
operator !=Reporter::Iterator71*9120562dSMichael Kruse     bool operator!=(const Iterator &that) const {
72*9120562dSMichael Kruse       owner->print("iterator %d != %d", 2 - this->pos, 2 - that.pos);
73*9120562dSMichael Kruse       return this->pos != that.pos;
74*9120562dSMichael Kruse     }
75*9120562dSMichael Kruse 
operator ++Reporter::Iterator76*9120562dSMichael Kruse     Iterator &operator++() {
77*9120562dSMichael Kruse       owner->print("iterator prefix ++");
78*9120562dSMichael Kruse       pos -= 1;
79*9120562dSMichael Kruse       return *this;
80*9120562dSMichael Kruse     }
81*9120562dSMichael Kruse 
operator ++Reporter::Iterator82*9120562dSMichael Kruse     Iterator operator++(int) {
83*9120562dSMichael Kruse       owner->print("iterator postfix ++");
84*9120562dSMichael Kruse       auto result = *this;
85*9120562dSMichael Kruse       pos -= 1;
86*9120562dSMichael Kruse       return result;
87*9120562dSMichael Kruse     }
88*9120562dSMichael Kruse 
operator *Reporter::Iterator89*9120562dSMichael Kruse     int operator*() const {
90*9120562dSMichael Kruse       int result = 2 - pos;
91*9120562dSMichael Kruse       owner->print("iterator deref: %i", result);
92*9120562dSMichael Kruse       return result;
93*9120562dSMichael Kruse     }
94*9120562dSMichael Kruse 
operator -Reporter::Iterator95*9120562dSMichael Kruse     size_t operator-(const Iterator &that) const {
96*9120562dSMichael Kruse       int result = (2 - this->pos) - (2 - that.pos);
97*9120562dSMichael Kruse       owner->print("iterator distance: %d", result);
98*9120562dSMichael Kruse       return result;
99*9120562dSMichael Kruse     }
100*9120562dSMichael Kruse 
operator +Reporter::Iterator101*9120562dSMichael Kruse     Iterator operator+(int steps) const {
102*9120562dSMichael Kruse       owner->print("iterator advance: %i += %i", 2 - this->pos, steps);
103*9120562dSMichael Kruse       return Iterator(owner, pos - steps);
104*9120562dSMichael Kruse     }
105*9120562dSMichael Kruse 
printReporter::Iterator106*9120562dSMichael Kruse     void print(const char *msg) const { owner->print(msg); }
107*9120562dSMichael Kruse   };
108*9120562dSMichael Kruse 
beginReporter109*9120562dSMichael Kruse   Iterator begin() const {
110*9120562dSMichael Kruse     print("begin()");
111*9120562dSMichael Kruse     return Iterator(this, 2);
112*9120562dSMichael Kruse   }
113*9120562dSMichael Kruse 
endReporter114*9120562dSMichael Kruse   Iterator end() const {
115*9120562dSMichael Kruse     print("end()");
116*9120562dSMichael Kruse     return Iterator(this, -1);
117*9120562dSMichael Kruse   }
118*9120562dSMichael Kruse 
printReporter119*9120562dSMichael Kruse   void print(const char *msg, ...) const {
120*9120562dSMichael Kruse     va_list args;
121*9120562dSMichael Kruse     va_start(args, msg);
122*9120562dSMichael Kruse     printf("[%s] ", name);
123*9120562dSMichael Kruse     vprintf(msg, args);
124*9120562dSMichael Kruse     printf("\n");
125*9120562dSMichael Kruse     va_end(args);
126*9120562dSMichael Kruse   }
127*9120562dSMichael Kruse };
128*9120562dSMichael Kruse 
main()129*9120562dSMichael Kruse int main() {
130*9120562dSMichael Kruse   printf("do\n");
131*9120562dSMichael Kruse #pragma omp unroll partial(2)
132*9120562dSMichael Kruse   for (Reporter c{"init-stmt"}; auto &&v : Reporter("range"))
133*9120562dSMichael Kruse     printf("v=%d\n", v);
134*9120562dSMichael Kruse   printf("done\n");
135*9120562dSMichael Kruse   return EXIT_SUCCESS;
136*9120562dSMichael Kruse }
137*9120562dSMichael Kruse 
138*9120562dSMichael Kruse #endif /* HEADER */
139*9120562dSMichael Kruse 
140*9120562dSMichael Kruse // CHECK:      do
141*9120562dSMichael Kruse // CHECK-NEXT: [init-stmt] ctor
142*9120562dSMichael Kruse // CHECK-NEXT: [range] ctor
143*9120562dSMichael Kruse // CHECK-NEXT: [range] begin()
144*9120562dSMichael Kruse // CHECK-NEXT: [range] end()
145*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator 0 != 3
146*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator deref: 0
147*9120562dSMichael Kruse // CHECK-NEXT: v=0
148*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator prefix ++
149*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator 1 != 3
150*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator deref: 1
151*9120562dSMichael Kruse // CHECK-NEXT: v=1
152*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator prefix ++
153*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator 2 != 3
154*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator deref: 2
155*9120562dSMichael Kruse // CHECK-NEXT: v=2
156*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator prefix ++
157*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator 3 != 3
158*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator dtor
159*9120562dSMichael Kruse // CHECK-NEXT: [range] iterator dtor
160*9120562dSMichael Kruse // CHECK-NEXT: [range] dtor
161*9120562dSMichael Kruse // CHECK-NEXT: [init-stmt] dtor
162*9120562dSMichael Kruse // CHECK-NEXT: done
163