• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/Frontend/OpenACCTest.cpp - OpenACC Frontend tests ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/SmallSet.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Frontend/OpenACC/ACC.h.inc"
13 #include "gtest/gtest.h"
14 
15 using namespace llvm;
16 using namespace acc;
17 
18 namespace {
19 
20 static const Clause AllClauses[] = {ACCC_unknown,
21                                     ACCC_async,
22                                     ACCC_attach,
23                                     ACCC_auto,
24                                     ACCC_bind,
25                                     ACCC_capture,
26                                     ACCC_collapse,
27                                     ACCC_copy,
28                                     ACCC_copyin,
29                                     ACCC_copyout,
30                                     ACCC_create,
31                                     ACCC_default,
32                                     ACCC_default_async,
33                                     ACCC_delete,
34                                     ACCC_detach,
35                                     ACCC_device,
36                                     ACCC_device_num,
37                                     ACCC_deviceptr,
38                                     ACCC_device_resident,
39                                     ACCC_device_type,
40                                     ACCC_finalize,
41                                     ACCC_firstprivate,
42                                     ACCC_gang,
43                                     ACCC_host,
44                                     ACCC_if,
45                                     ACCC_if_present,
46                                     ACCC_independent,
47                                     ACCC_link,
48                                     ACCC_no_create,
49                                     ACCC_nohost,
50                                     ACCC_num_gangs,
51                                     ACCC_num_workers,
52                                     ACCC_present,
53                                     ACCC_private,
54                                     ACCC_read,
55                                     ACCC_reduction,
56                                     ACCC_self,
57                                     ACCC_seq,
58                                     ACCC_tile,
59                                     ACCC_unknown,
60                                     ACCC_use_device,
61                                     ACCC_vector,
62                                     ACCC_vector_length,
63                                     ACCC_wait,
64                                     ACCC_worker,
65                                     ACCC_write};
66 
TEST(OpenACCTest,DirectiveHelpers)67 TEST(OpenACCTest, DirectiveHelpers) {
68   EXPECT_EQ(getOpenACCDirectiveKind(""), ACCD_unknown);
69   EXPECT_EQ(getOpenACCDirectiveKind("dummy"), ACCD_unknown);
70   EXPECT_EQ(getOpenACCDirectiveKind("atomic"), ACCD_atomic);
71   EXPECT_EQ(getOpenACCDirectiveKind("cache"), ACCD_cache);
72   EXPECT_EQ(getOpenACCDirectiveKind("data"), ACCD_data);
73   EXPECT_EQ(getOpenACCDirectiveKind("declare"), ACCD_declare);
74   EXPECT_EQ(getOpenACCDirectiveKind("enter data"), ACCD_enter_data);
75   EXPECT_EQ(getOpenACCDirectiveKind("exit data"), ACCD_exit_data);
76   EXPECT_EQ(getOpenACCDirectiveKind("host_data"), ACCD_host_data);
77   EXPECT_EQ(getOpenACCDirectiveKind("init"), ACCD_init);
78   EXPECT_EQ(getOpenACCDirectiveKind("kernels"), ACCD_kernels);
79   EXPECT_EQ(getOpenACCDirectiveKind("kernels loop"), ACCD_kernels_loop);
80   EXPECT_EQ(getOpenACCDirectiveKind("loop"), ACCD_loop);
81   EXPECT_EQ(getOpenACCDirectiveKind("parallel"), ACCD_parallel);
82   EXPECT_EQ(getOpenACCDirectiveKind("parallel loop"), ACCD_parallel_loop);
83   EXPECT_EQ(getOpenACCDirectiveKind("routine"), ACCD_routine);
84   EXPECT_EQ(getOpenACCDirectiveKind("serial"), ACCD_serial);
85   EXPECT_EQ(getOpenACCDirectiveKind("serial loop"), ACCD_serial_loop);
86   EXPECT_EQ(getOpenACCDirectiveKind("set"), ACCD_set);
87   EXPECT_EQ(getOpenACCDirectiveKind("shutdown"), ACCD_shutdown);
88   EXPECT_EQ(getOpenACCDirectiveKind("unknown"), ACCD_unknown);
89   EXPECT_EQ(getOpenACCDirectiveKind("update"), ACCD_update);
90   EXPECT_EQ(getOpenACCDirectiveKind("wait"), ACCD_wait);
91 
92   EXPECT_EQ(getOpenACCDirectiveName(ACCD_atomic), "atomic");
93   EXPECT_EQ(getOpenACCDirectiveName(ACCD_cache), "cache");
94   EXPECT_EQ(getOpenACCDirectiveName(ACCD_data), "data");
95   EXPECT_EQ(getOpenACCDirectiveName(ACCD_declare), "declare");
96   EXPECT_EQ(getOpenACCDirectiveName(ACCD_enter_data), "enter data");
97   EXPECT_EQ(getOpenACCDirectiveName(ACCD_exit_data), "exit data");
98   EXPECT_EQ(getOpenACCDirectiveName(ACCD_host_data), "host_data");
99   EXPECT_EQ(getOpenACCDirectiveName(ACCD_init), "init");
100   EXPECT_EQ(getOpenACCDirectiveName(ACCD_kernels), "kernels");
101   EXPECT_EQ(getOpenACCDirectiveName(ACCD_kernels_loop), "kernels loop");
102   EXPECT_EQ(getOpenACCDirectiveName(ACCD_loop), "loop");
103   EXPECT_EQ(getOpenACCDirectiveName(ACCD_parallel), "parallel");
104   EXPECT_EQ(getOpenACCDirectiveName(ACCD_parallel_loop), "parallel loop");
105   EXPECT_EQ(getOpenACCDirectiveName(ACCD_routine), "routine");
106   EXPECT_EQ(getOpenACCDirectiveName(ACCD_serial), "serial");
107   EXPECT_EQ(getOpenACCDirectiveName(ACCD_serial_loop), "serial loop");
108   EXPECT_EQ(getOpenACCDirectiveName(ACCD_set), "set");
109   EXPECT_EQ(getOpenACCDirectiveName(ACCD_shutdown), "shutdown");
110   EXPECT_EQ(getOpenACCDirectiveName(ACCD_unknown), "unknown");
111   EXPECT_EQ(getOpenACCDirectiveName(ACCD_update), "update");
112   EXPECT_EQ(getOpenACCDirectiveName(ACCD_wait), "wait");
113 }
114 
TEST(OpenACCTest,ClauseHelpers)115 TEST(OpenACCTest, ClauseHelpers) {
116   EXPECT_EQ(getOpenACCClauseKind(""), ACCC_unknown);
117   EXPECT_EQ(getOpenACCClauseKind("dummy"), ACCC_unknown);
118   EXPECT_EQ(getOpenACCClauseKind("async"), ACCC_async);
119   EXPECT_EQ(getOpenACCClauseKind("attach"), ACCC_attach);
120   EXPECT_EQ(getOpenACCClauseKind("auto"), ACCC_auto);
121   EXPECT_EQ(getOpenACCClauseKind("bind"), ACCC_bind);
122   EXPECT_EQ(getOpenACCClauseKind("capture"), ACCC_capture);
123   EXPECT_EQ(getOpenACCClauseKind("collapse"), ACCC_collapse);
124   EXPECT_EQ(getOpenACCClauseKind("copy"), ACCC_copy);
125   EXPECT_EQ(getOpenACCClauseKind("copyin"), ACCC_copyin);
126   EXPECT_EQ(getOpenACCClauseKind("copyout"), ACCC_copyout);
127   EXPECT_EQ(getOpenACCClauseKind("create"), ACCC_create);
128   EXPECT_EQ(getOpenACCClauseKind("default"), ACCC_default);
129   EXPECT_EQ(getOpenACCClauseKind("default_async"), ACCC_default_async);
130   EXPECT_EQ(getOpenACCClauseKind("delete"), ACCC_delete);
131   EXPECT_EQ(getOpenACCClauseKind("detach"), ACCC_detach);
132   EXPECT_EQ(getOpenACCClauseKind("device"), ACCC_device);
133   EXPECT_EQ(getOpenACCClauseKind("device_num"), ACCC_device_num);
134   EXPECT_EQ(getOpenACCClauseKind("deviceptr"), ACCC_deviceptr);
135   EXPECT_EQ(getOpenACCClauseKind("device_resident"), ACCC_device_resident);
136   EXPECT_EQ(getOpenACCClauseKind("device_type"), ACCC_device_type);
137   EXPECT_EQ(getOpenACCClauseKind("finalize"), ACCC_finalize);
138   EXPECT_EQ(getOpenACCClauseKind("firstprivate"), ACCC_firstprivate);
139   EXPECT_EQ(getOpenACCClauseKind("gang"), ACCC_gang);
140   EXPECT_EQ(getOpenACCClauseKind("host"), ACCC_host);
141   EXPECT_EQ(getOpenACCClauseKind("if"), ACCC_if);
142   EXPECT_EQ(getOpenACCClauseKind("if_present"), ACCC_if_present);
143   EXPECT_EQ(getOpenACCClauseKind("independent"), ACCC_independent);
144   EXPECT_EQ(getOpenACCClauseKind("link"), ACCC_link);
145   EXPECT_EQ(getOpenACCClauseKind("no_create"), ACCC_no_create);
146   EXPECT_EQ(getOpenACCClauseKind("nohost"), ACCC_nohost);
147   EXPECT_EQ(getOpenACCClauseKind("num_gangs"), ACCC_num_gangs);
148   EXPECT_EQ(getOpenACCClauseKind("num_workers"), ACCC_num_workers);
149   EXPECT_EQ(getOpenACCClauseKind("present"), ACCC_present);
150   EXPECT_EQ(getOpenACCClauseKind("private"), ACCC_private);
151   EXPECT_EQ(getOpenACCClauseKind("read"), ACCC_read);
152   EXPECT_EQ(getOpenACCClauseKind("reduction"), ACCC_reduction);
153   EXPECT_EQ(getOpenACCClauseKind("self"), ACCC_self);
154   EXPECT_EQ(getOpenACCClauseKind("seq"), ACCC_seq);
155   EXPECT_EQ(getOpenACCClauseKind("tile"), ACCC_tile);
156   EXPECT_EQ(getOpenACCClauseKind("unknown"), ACCC_unknown);
157   EXPECT_EQ(getOpenACCClauseKind("use_device"), ACCC_use_device);
158   EXPECT_EQ(getOpenACCClauseKind("vector"), ACCC_vector);
159   EXPECT_EQ(getOpenACCClauseKind("vector_length"), ACCC_vector_length);
160   EXPECT_EQ(getOpenACCClauseKind("wait"), ACCC_wait);
161   EXPECT_EQ(getOpenACCClauseKind("worker"), ACCC_worker);
162   EXPECT_EQ(getOpenACCClauseKind("write"), ACCC_write);
163 
164   EXPECT_EQ(getOpenACCClauseName(ACCC_async), "async");
165   EXPECT_EQ(getOpenACCClauseName(ACCC_attach), "attach");
166   EXPECT_EQ(getOpenACCClauseName(ACCC_auto), "auto");
167   EXPECT_EQ(getOpenACCClauseName(ACCC_bind), "bind");
168   EXPECT_EQ(getOpenACCClauseName(ACCC_capture), "capture");
169   EXPECT_EQ(getOpenACCClauseName(ACCC_collapse), "collapse");
170   EXPECT_EQ(getOpenACCClauseName(ACCC_copy), "copy");
171   EXPECT_EQ(getOpenACCClauseName(ACCC_copyin), "copyin");
172   EXPECT_EQ(getOpenACCClauseName(ACCC_copyout), "copyout");
173   EXPECT_EQ(getOpenACCClauseName(ACCC_create), "create");
174   EXPECT_EQ(getOpenACCClauseName(ACCC_default), "default");
175   EXPECT_EQ(getOpenACCClauseName(ACCC_default_async), "default_async");
176   EXPECT_EQ(getOpenACCClauseName(ACCC_delete), "delete");
177   EXPECT_EQ(getOpenACCClauseName(ACCC_detach), "detach");
178   EXPECT_EQ(getOpenACCClauseName(ACCC_device), "device");
179   EXPECT_EQ(getOpenACCClauseName(ACCC_device_num), "device_num");
180   EXPECT_EQ(getOpenACCClauseName(ACCC_deviceptr), "deviceptr");
181   EXPECT_EQ(getOpenACCClauseName(ACCC_device_resident), "device_resident");
182   EXPECT_EQ(getOpenACCClauseName(ACCC_device_type), "device_type");
183   EXPECT_EQ(getOpenACCClauseName(ACCC_finalize), "finalize");
184   EXPECT_EQ(getOpenACCClauseName(ACCC_firstprivate), "firstprivate");
185   EXPECT_EQ(getOpenACCClauseName(ACCC_gang), "gang");
186   EXPECT_EQ(getOpenACCClauseName(ACCC_host), "host");
187   EXPECT_EQ(getOpenACCClauseName(ACCC_if), "if");
188   EXPECT_EQ(getOpenACCClauseName(ACCC_if_present), "if_present");
189   EXPECT_EQ(getOpenACCClauseName(ACCC_independent), "independent");
190   EXPECT_EQ(getOpenACCClauseName(ACCC_link), "link");
191   EXPECT_EQ(getOpenACCClauseName(ACCC_no_create), "no_create");
192   EXPECT_EQ(getOpenACCClauseName(ACCC_nohost), "nohost");
193   EXPECT_EQ(getOpenACCClauseName(ACCC_num_gangs), "num_gangs");
194   EXPECT_EQ(getOpenACCClauseName(ACCC_num_workers), "num_workers");
195   EXPECT_EQ(getOpenACCClauseName(ACCC_present), "present");
196   EXPECT_EQ(getOpenACCClauseName(ACCC_private), "private");
197   EXPECT_EQ(getOpenACCClauseName(ACCC_read), "read");
198   EXPECT_EQ(getOpenACCClauseName(ACCC_reduction), "reduction");
199   EXPECT_EQ(getOpenACCClauseName(ACCC_self), "self");
200   EXPECT_EQ(getOpenACCClauseName(ACCC_seq), "seq");
201   EXPECT_EQ(getOpenACCClauseName(ACCC_tile), "tile");
202   EXPECT_EQ(getOpenACCClauseName(ACCC_unknown), "unknown");
203   EXPECT_EQ(getOpenACCClauseName(ACCC_use_device), "use_device");
204   EXPECT_EQ(getOpenACCClauseName(ACCC_vector), "vector");
205   EXPECT_EQ(getOpenACCClauseName(ACCC_vector_length), "vector_length");
206   EXPECT_EQ(getOpenACCClauseName(ACCC_wait), "wait");
207   EXPECT_EQ(getOpenACCClauseName(ACCC_worker), "worker");
208   EXPECT_EQ(getOpenACCClauseName(ACCC_write), "write");
209 }
210 
expectAllowedClauses(Directive Dir,unsigned Version,const ArrayRef<Clause> & AllowedClauses)211 static void expectAllowedClauses(Directive Dir, unsigned Version,
212                                  const ArrayRef<Clause> &AllowedClauses) {
213   SmallSet<Clause, 30> AllowedClausesSet;
214   for (Clause Cl : AllowedClauses) {
215     EXPECT_TRUE(isAllowedClauseForDirective(Dir, Cl, Version));
216     AllowedClausesSet.insert(Cl);
217   }
218 
219   for (Clause Cl : AllClauses) {
220     if (!AllowedClausesSet.contains(Cl)) {
221       EXPECT_FALSE(isAllowedClauseForDirective(Dir, Cl, Version));
222     }
223   }
224 }
225 
TEST(OpenACCTest,AllowedClause)226 TEST(OpenACCTest, AllowedClause) {
227   expectAllowedClauses(ACCD_atomic, 3, {});
228   expectAllowedClauses(ACCD_cache, 3, {});
229   expectAllowedClauses(ACCD_unknown, 3, {});
230   expectAllowedClauses(ACCD_parallel, 0, {}); // Version starts at 1
231 
232   expectAllowedClauses(ACCD_data, 3,
233                        {ACCC_if, ACCC_attach, ACCC_copy, ACCC_copyin,
234                         ACCC_copyout, ACCC_create, ACCC_default, ACCC_deviceptr,
235                         ACCC_no_create, ACCC_present});
236 
237   expectAllowedClauses(ACCD_declare, 3,
238                        {ACCC_copy, ACCC_copyin, ACCC_copyout, ACCC_create,
239                         ACCC_present, ACCC_deviceptr, ACCC_device_resident,
240                         ACCC_link});
241 
242   expectAllowedClauses(
243       ACCD_enter_data, 3,
244       {ACCC_async, ACCC_if, ACCC_wait, ACCC_attach, ACCC_create, ACCC_copyin});
245 
246   expectAllowedClauses(ACCD_exit_data, 3,
247                        {ACCC_async, ACCC_if, ACCC_wait, ACCC_finalize,
248                         ACCC_copyout, ACCC_delete, ACCC_detach});
249 
250   expectAllowedClauses(ACCD_host_data, 3,
251                        {ACCC_if, ACCC_if_present, ACCC_use_device});
252 
253   expectAllowedClauses(ACCD_init, 3,
254                        {ACCC_device_num, ACCC_device_type, ACCC_if});
255 
256   expectAllowedClauses(ACCD_kernels, 3,
257                        {ACCC_attach, ACCC_copy, ACCC_copyin, ACCC_copyout,
258                         ACCC_create, ACCC_device_type, ACCC_no_create,
259                         ACCC_present, ACCC_deviceptr, ACCC_async, ACCC_default,
260                         ACCC_if, ACCC_num_gangs, ACCC_num_workers, ACCC_self,
261                         ACCC_vector_length, ACCC_wait});
262 
263   expectAllowedClauses(
264       ACCD_kernels_loop, 3,
265       {ACCC_copy,        ACCC_copyin,        ACCC_copyout, ACCC_create,
266        ACCC_device_type, ACCC_no_create,     ACCC_present, ACCC_private,
267        ACCC_deviceptr,   ACCC_attach,        ACCC_async,   ACCC_collapse,
268        ACCC_default,     ACCC_gang,          ACCC_if,      ACCC_num_gangs,
269        ACCC_num_workers, ACCC_reduction,     ACCC_self,    ACCC_tile,
270        ACCC_vector,      ACCC_vector_length, ACCC_wait,    ACCC_worker,
271        ACCC_auto,        ACCC_independent,   ACCC_seq});
272 
273   expectAllowedClauses(ACCD_loop, 3,
274                        {ACCC_device_type, ACCC_private, ACCC_collapse,
275                         ACCC_gang, ACCC_reduction, ACCC_tile, ACCC_vector,
276                         ACCC_worker, ACCC_auto, ACCC_independent, ACCC_seq});
277 
278   expectAllowedClauses(ACCD_parallel, 3,
279                        {ACCC_async,        ACCC_wait,          ACCC_num_gangs,
280                         ACCC_num_workers,  ACCC_vector_length, ACCC_device_type,
281                         ACCC_if,           ACCC_self,          ACCC_reduction,
282                         ACCC_copy,         ACCC_copyin,        ACCC_copyout,
283                         ACCC_create,       ACCC_no_create,     ACCC_present,
284                         ACCC_deviceptr,    ACCC_attach,        ACCC_private,
285                         ACCC_firstprivate, ACCC_default});
286 
287   expectAllowedClauses(
288       ACCD_parallel_loop, 3,
289       {ACCC_attach,    ACCC_copy,      ACCC_copyin,      ACCC_copyout,
290        ACCC_create,    ACCC_deviceptr, ACCC_device_type, ACCC_firstprivate,
291        ACCC_no_create, ACCC_present,   ACCC_private,     ACCC_tile,
292        ACCC_wait,      ACCC_async,     ACCC_collapse,    ACCC_default,
293        ACCC_gang,      ACCC_if,        ACCC_num_gangs,   ACCC_num_workers,
294        ACCC_reduction, ACCC_self,      ACCC_vector,      ACCC_vector_length,
295        ACCC_worker,    ACCC_auto,      ACCC_independent, ACCC_seq});
296 
297   expectAllowedClauses(ACCD_routine, 3,
298                        {ACCC_bind, ACCC_device_type, ACCC_nohost, ACCC_gang,
299                         ACCC_seq, ACCC_vector, ACCC_worker});
300 
301   expectAllowedClauses(ACCD_serial, 3,
302                        {ACCC_attach, ACCC_copy, ACCC_copyin, ACCC_copyout,
303                         ACCC_create, ACCC_deviceptr, ACCC_device_type,
304                         ACCC_firstprivate, ACCC_no_create, ACCC_present,
305                         ACCC_private, ACCC_wait, ACCC_async, ACCC_default,
306                         ACCC_if, ACCC_reduction, ACCC_self});
307 
308   expectAllowedClauses(
309       ACCD_serial_loop, 3,
310       {ACCC_attach,    ACCC_copy,      ACCC_copyin,      ACCC_copyout,
311        ACCC_create,    ACCC_deviceptr, ACCC_device_type, ACCC_firstprivate,
312        ACCC_no_create, ACCC_present,   ACCC_private,     ACCC_wait,
313        ACCC_async,     ACCC_collapse,  ACCC_default,     ACCC_gang,
314        ACCC_if,        ACCC_reduction, ACCC_self,        ACCC_tile,
315        ACCC_vector,    ACCC_worker,    ACCC_auto,        ACCC_independent,
316        ACCC_seq});
317 
318   expectAllowedClauses(
319       ACCD_set, 3,
320       {ACCC_default_async, ACCC_device_num, ACCC_device_type, ACCC_if});
321 
322   expectAllowedClauses(ACCD_shutdown, 3,
323                        {ACCC_device_num, ACCC_device_type, ACCC_if});
324 
325   expectAllowedClauses(ACCD_update, 3,
326                        {ACCC_async, ACCC_wait, ACCC_device_type, ACCC_if,
327                         ACCC_if_present, ACCC_self, ACCC_host, ACCC_device});
328 
329   expectAllowedClauses(ACCD_wait, 3, {ACCC_async, ACCC_if});
330 }
331 
332 } // namespace
333