1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
|
//===--- SemaOpenACCClause.cpp - Semantic Analysis for OpenACC clause -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
/// This file implements semantic analysis for OpenACC clauses.
///
//===----------------------------------------------------------------------===//
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/OpenACCClause.h"
#include "clang/Basic/DiagnosticSema.h"
#include "clang/Basic/OpenACCKinds.h"
#include "clang/Sema/SemaOpenACC.h"
using namespace clang;
namespace {
bool checkValidAfterDeviceType(
SemaOpenACC &S, const OpenACCDeviceTypeClause &DeviceTypeClause,
const SemaOpenACC::OpenACCParsedClause &NewClause) {
// OpenACC3.3: Section 2.4: Clauses that precede any device_type clause are
// default clauses. Clauses that follow a device_type clause up to the end of
// the directive or up to the next device_type clause are device-specific
// clauses for the device types specified in the device_type argument.
//
// The above implies that despite what the individual text says, these are
// valid.
if (NewClause.getClauseKind() == OpenACCClauseKind::DType ||
NewClause.getClauseKind() == OpenACCClauseKind::DeviceType)
return false;
// Implement check from OpenACC3.3: section 2.5.4:
// Only the async, wait, num_gangs, num_workers, and vector_length clauses may
// follow a device_type clause.
if (isOpenACCComputeDirectiveKind(NewClause.getDirectiveKind())) {
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Async:
case OpenACCClauseKind::Wait:
case OpenACCClauseKind::NumGangs:
case OpenACCClauseKind::NumWorkers:
case OpenACCClauseKind::VectorLength:
return false;
default:
break;
}
} else if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Loop) {
// Implement check from OpenACC3.3: section 2.9:
// Only the collapse, gang, worker, vector, seq, independent, auto, and tile
// clauses may follow a device_type clause.
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Collapse:
case OpenACCClauseKind::Gang:
case OpenACCClauseKind::Worker:
case OpenACCClauseKind::Vector:
case OpenACCClauseKind::Seq:
case OpenACCClauseKind::Independent:
case OpenACCClauseKind::Auto:
case OpenACCClauseKind::Tile:
return false;
default:
break;
}
} else if (isOpenACCCombinedDirectiveKind(NewClause.getDirectiveKind())) {
// This seems like it should be the union of 2.9 and 2.5.4 from above.
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Async:
case OpenACCClauseKind::Wait:
case OpenACCClauseKind::NumGangs:
case OpenACCClauseKind::NumWorkers:
case OpenACCClauseKind::VectorLength:
case OpenACCClauseKind::Collapse:
case OpenACCClauseKind::Gang:
case OpenACCClauseKind::Worker:
case OpenACCClauseKind::Vector:
case OpenACCClauseKind::Seq:
case OpenACCClauseKind::Independent:
case OpenACCClauseKind::Auto:
case OpenACCClauseKind::Tile:
return false;
default:
break;
}
} else if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Data) {
// OpenACC3.3 section 2.6.5: Only the async and wait clauses may follow a
// device_type clause.
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Async:
case OpenACCClauseKind::Wait:
return false;
default:
break;
}
} else if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Set ||
NewClause.getDirectiveKind() == OpenACCDirectiveKind::Init ||
NewClause.getDirectiveKind() == OpenACCDirectiveKind::Shutdown) {
// There are no restrictions on 'set', 'init', or 'shutdown'.
return false;
} else if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Update) {
// OpenACC3.3 section 2.14.4: Only the async and wait clauses may follow a
// device_type clause.
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Async:
case OpenACCClauseKind::Wait:
return false;
default:
break;
}
} else if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Routine) {
// OpenACC 3.3 section 2.15: Only the 'gang', 'worker', 'vector', 'seq', and
// 'bind' clauses may follow a device_type clause.
switch (NewClause.getClauseKind()) {
case OpenACCClauseKind::Gang:
case OpenACCClauseKind::Worker:
case OpenACCClauseKind::Vector:
case OpenACCClauseKind::Seq:
case OpenACCClauseKind::Bind:
return false;
default:
break;
}
}
S.Diag(NewClause.getBeginLoc(), diag::err_acc_clause_after_device_type)
<< NewClause.getClauseKind() << DeviceTypeClause.getClauseKind()
<< NewClause.getDirectiveKind();
S.Diag(DeviceTypeClause.getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Active << DeviceTypeClause.getClauseKind();
return true;
}
// GCC looks through linkage specs, but not the other transparent declaration
// contexts for 'declare' restrictions, so this helper function helps get us
// through that.
const DeclContext *removeLinkageSpecDC(const DeclContext *DC) {
while (isa<LinkageSpecDecl>(DC))
DC = DC->getParent();
return DC;
}
class SemaOpenACCClauseVisitor {
SemaOpenACC &SemaRef;
ASTContext &Ctx;
ArrayRef<const OpenACCClause *> ExistingClauses;
// OpenACC 3.3 2.9:
// A 'gang', 'worker', or 'vector' clause may not appear if a 'seq' clause
// appears.
bool
DiagGangWorkerVectorSeqConflict(SemaOpenACC::OpenACCParsedClause &Clause) {
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::Loop &&
!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind()))
return false;
assert(Clause.getClauseKind() == OpenACCClauseKind::Gang ||
Clause.getClauseKind() == OpenACCClauseKind::Worker ||
Clause.getClauseKind() == OpenACCClauseKind::Vector);
const auto *Itr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCSeqClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_cannot_combine)
<< Clause.getClauseKind() << (*Itr)->getClauseKind()
<< Clause.getDirectiveKind();
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
return true;
}
return false;
}
OpenACCModifierKind
CheckModifierList(SemaOpenACC::OpenACCParsedClause &Clause,
OpenACCModifierKind Mods) {
auto CheckSingle = [=](OpenACCModifierKind CurMods,
OpenACCModifierKind ValidKinds,
OpenACCModifierKind Bit) {
if (!isOpenACCModifierBitSet(CurMods, Bit) ||
isOpenACCModifierBitSet(ValidKinds, Bit))
return CurMods;
SemaRef.Diag(Clause.getLParenLoc(), diag::err_acc_invalid_modifier)
<< Bit << Clause.getClauseKind();
return CurMods ^ Bit;
};
auto Check = [&](OpenACCModifierKind ValidKinds) {
if ((Mods | ValidKinds) == ValidKinds)
return Mods;
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::Always);
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::AlwaysIn);
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::AlwaysOut);
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::Readonly);
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::Zero);
Mods = CheckSingle(Mods, ValidKinds, OpenACCModifierKind::Capture);
return Mods;
};
// The 'capture' modifier is only valid on copyin, copyout, and create on
// structured data or compute constructs (which also includes combined).
bool IsStructuredDataOrCompute =
Clause.getDirectiveKind() == OpenACCDirectiveKind::Data ||
isOpenACCComputeDirectiveKind(Clause.getDirectiveKind()) ||
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind());
switch (Clause.getClauseKind()) {
default:
llvm_unreachable("Only for copy, copyin, copyout, create");
case OpenACCClauseKind::Copy:
case OpenACCClauseKind::PCopy:
case OpenACCClauseKind::PresentOrCopy:
// COPY: Capture always
return Check(OpenACCModifierKind::Always | OpenACCModifierKind::AlwaysIn |
OpenACCModifierKind::AlwaysOut |
OpenACCModifierKind::Capture);
case OpenACCClauseKind::CopyIn:
case OpenACCClauseKind::PCopyIn:
case OpenACCClauseKind::PresentOrCopyIn:
// COPYIN: Capture only struct.data & compute
return Check(OpenACCModifierKind::Always | OpenACCModifierKind::AlwaysIn |
OpenACCModifierKind::Readonly |
(IsStructuredDataOrCompute ? OpenACCModifierKind::Capture
: OpenACCModifierKind::Invalid));
case OpenACCClauseKind::CopyOut:
case OpenACCClauseKind::PCopyOut:
case OpenACCClauseKind::PresentOrCopyOut:
// COPYOUT: Capture only struct.data & compute
return Check(OpenACCModifierKind::Always |
OpenACCModifierKind::AlwaysOut | OpenACCModifierKind::Zero |
(IsStructuredDataOrCompute ? OpenACCModifierKind::Capture
: OpenACCModifierKind::Invalid));
case OpenACCClauseKind::Create:
case OpenACCClauseKind::PCreate:
case OpenACCClauseKind::PresentOrCreate:
// CREATE: Capture only struct.data & compute
return Check(OpenACCModifierKind::Zero |
(IsStructuredDataOrCompute ? OpenACCModifierKind::Capture
: OpenACCModifierKind::Invalid));
}
llvm_unreachable("didn't return from switch above?");
}
// Helper for the 'routine' checks during 'new' clause addition. Precondition
// is that we already know the new clause is one of the prohbiited ones.
template <typename Pred>
bool
CheckValidRoutineNewClauseHelper(Pred HasPredicate,
SemaOpenACC::OpenACCParsedClause &Clause) {
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::Routine)
return false;
auto *FirstDeviceType =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCDeviceTypeClause>);
if (FirstDeviceType == ExistingClauses.end()) {
// If there isn't a device type yet, ANY duplicate is wrong.
auto *ExistingProhibitedClause =
llvm::find_if(ExistingClauses, HasPredicate);
if (ExistingProhibitedClause == ExistingClauses.end())
return false;
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_cannot_combine)
<< Clause.getClauseKind()
<< (*ExistingProhibitedClause)->getClauseKind()
<< Clause.getDirectiveKind();
SemaRef.Diag((*ExistingProhibitedClause)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*ExistingProhibitedClause)->getClauseKind();
return true;
}
// At this point we know that this is 'after' a device type. So this is an
// error if: 1- there is one BEFORE the 'device_type' 2- there is one
// between this and the previous 'device_type'.
auto *BeforeDeviceType =
std::find_if(ExistingClauses.begin(), FirstDeviceType, HasPredicate);
// If there is one before the device_type (and we know we are after a
// device_type), than this is ill-formed.
if (BeforeDeviceType != FirstDeviceType) {
SemaRef.Diag(
Clause.getBeginLoc(),
diag::err_acc_clause_routine_cannot_combine_before_device_type)
<< Clause.getClauseKind() << (*BeforeDeviceType)->getClauseKind();
SemaRef.Diag((*BeforeDeviceType)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*BeforeDeviceType)->getClauseKind();
SemaRef.Diag((*FirstDeviceType)->getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Active
<< (*FirstDeviceType)->getClauseKind();
return true;
}
auto LastDeviceTypeItr =
std::find_if(ExistingClauses.rbegin(), ExistingClauses.rend(),
llvm::IsaPred<OpenACCDeviceTypeClause>);
// We already know there is one in the list, so it is nonsensical to not
// have one.
assert(LastDeviceTypeItr != ExistingClauses.rend());
// Get the device-type from-the-front (not reverse) iterator from the
// reverse iterator.
auto *LastDeviceType = LastDeviceTypeItr.base() - 1;
auto *ExistingProhibitedSinceLastDevice =
std::find_if(LastDeviceType, ExistingClauses.end(), HasPredicate);
// No prohibited ones since the last device-type.
if (ExistingProhibitedSinceLastDevice == ExistingClauses.end())
return false;
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_clause_routine_cannot_combine_same_device_type)
<< Clause.getClauseKind()
<< (*ExistingProhibitedSinceLastDevice)->getClauseKind();
SemaRef.Diag((*ExistingProhibitedSinceLastDevice)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*ExistingProhibitedSinceLastDevice)->getClauseKind();
SemaRef.Diag((*LastDeviceType)->getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Active << (*LastDeviceType)->getClauseKind();
return true;
}
// Routine has a pretty complicated set of rules for how device_type and the
// gang, worker, vector, and seq clauses work. So diagnose some of it here.
bool CheckValidRoutineGangWorkerVectorSeqNewClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (Clause.getClauseKind() != OpenACCClauseKind::Gang &&
Clause.getClauseKind() != OpenACCClauseKind::Vector &&
Clause.getClauseKind() != OpenACCClauseKind::Worker &&
Clause.getClauseKind() != OpenACCClauseKind::Seq)
return false;
auto ProhibitedPred = llvm::IsaPred<OpenACCGangClause, OpenACCWorkerClause,
OpenACCVectorClause, OpenACCSeqClause>;
return CheckValidRoutineNewClauseHelper(ProhibitedPred, Clause);
}
// Bind should have similar rules on a routine as gang/worker/vector/seq,
// except there is no 'must have 1' rule, so we can get all the checking done
// here.
bool
CheckValidRoutineBindNewClause(SemaOpenACC::OpenACCParsedClause &Clause) {
if (Clause.getClauseKind() != OpenACCClauseKind::Bind)
return false;
auto HasBindPred = llvm::IsaPred<OpenACCBindClause>;
return CheckValidRoutineNewClauseHelper(HasBindPred, Clause);
}
// For 'tile' and 'collapse', only allow 1 per 'device_type'.
// Also applies to num_worker, num_gangs, vector_length, and async.
// This does introspection into the actual device-types to prevent duplicates
// across device types as well.
template <typename TheClauseTy>
bool DisallowSinceLastDeviceType(SemaOpenACC::OpenACCParsedClause &Clause) {
auto LastDeviceTypeItr =
std::find_if(ExistingClauses.rbegin(), ExistingClauses.rend(),
llvm::IsaPred<OpenACCDeviceTypeClause>);
auto LastSinceDevTy =
std::find_if(ExistingClauses.rbegin(), LastDeviceTypeItr,
llvm::IsaPred<TheClauseTy>);
// In this case there is a duplicate since the last device_type/lack of a
// device_type. Diagnose these as duplicates.
if (LastSinceDevTy != LastDeviceTypeItr) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_clause_since_last_device_type)
<< Clause.getClauseKind() << Clause.getDirectiveKind()
<< (LastDeviceTypeItr != ExistingClauses.rend());
SemaRef.Diag((*LastSinceDevTy)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*LastSinceDevTy)->getClauseKind();
// Mention the last device_type as well.
if (LastDeviceTypeItr != ExistingClauses.rend())
SemaRef.Diag((*LastDeviceTypeItr)->getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Active
<< (*LastDeviceTypeItr)->getClauseKind();
return true;
}
// If this isn't in a device_type, and we didn't diagnose that there are
// dupes above, just give up, no sense in searching for previous device_type
// regions as they don't exist.
if (LastDeviceTypeItr == ExistingClauses.rend())
return false;
// The device-type that is active for us, so we can compare to the previous
// ones.
const auto &ActiveDeviceTypeClause =
cast<OpenACCDeviceTypeClause>(**LastDeviceTypeItr);
auto PrevDeviceTypeItr = LastDeviceTypeItr;
auto CurDevTypeItr = LastDeviceTypeItr;
while ((CurDevTypeItr = std::find_if(
std::next(PrevDeviceTypeItr), ExistingClauses.rend(),
llvm::IsaPred<OpenACCDeviceTypeClause>)) !=
ExistingClauses.rend()) {
// At this point, we know that we have a region between two device_types,
// as specified by CurDevTypeItr and PrevDeviceTypeItr.
auto CurClauseKindItr = std::find_if(PrevDeviceTypeItr, CurDevTypeItr,
llvm::IsaPred<TheClauseTy>);
// There are no clauses of the current kind between these device_types, so
// continue.
if (CurClauseKindItr == CurDevTypeItr) {
PrevDeviceTypeItr = CurDevTypeItr;
continue;
}
// At this point, we know that this device_type region has a collapse. So
// diagnose if the two device_types have any overlap in their
// architectures.
const auto &CurDeviceTypeClause =
cast<OpenACCDeviceTypeClause>(**CurDevTypeItr);
for (const DeviceTypeArgument &arg :
ActiveDeviceTypeClause.getArchitectures()) {
for (const DeviceTypeArgument &prevArg :
CurDeviceTypeClause.getArchitectures()) {
// This should catch duplicates * regions, duplicate same-text (thanks
// to identifier equiv.) and case insensitive dupes.
if (arg.getIdentifierInfo() == prevArg.getIdentifierInfo() ||
(arg.getIdentifierInfo() && prevArg.getIdentifierInfo() &&
StringRef{arg.getIdentifierInfo()->getName()}.equals_insensitive(
prevArg.getIdentifierInfo()->getName()))) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_clause_conflicts_prev_dev_type)
<< Clause.getClauseKind()
<< (arg.getIdentifierInfo() ? arg.getIdentifierInfo()->getName()
: "*");
// mention the active device type.
SemaRef.Diag(ActiveDeviceTypeClause.getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Active
<< ActiveDeviceTypeClause.getClauseKind();
// mention the previous clause.
SemaRef.Diag((*CurClauseKindItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*CurClauseKindItr)->getClauseKind();
// mention the previous device type.
SemaRef.Diag(CurDeviceTypeClause.getBeginLoc(),
diag::note_acc_active_applies_clause_here)
<< diag::ACCDeviceTypeApp::Applies
<< CurDeviceTypeClause.getClauseKind();
return true;
}
}
}
PrevDeviceTypeItr = CurDevTypeItr;
}
return false;
}
public:
SemaOpenACCClauseVisitor(SemaOpenACC &S,
ArrayRef<const OpenACCClause *> ExistingClauses)
: SemaRef(S), Ctx(S.getASTContext()), ExistingClauses(ExistingClauses) {}
OpenACCClause *Visit(SemaOpenACC::OpenACCParsedClause &Clause) {
if (SemaRef.DiagnoseAllowedOnceClauses(
Clause.getDirectiveKind(), Clause.getClauseKind(),
Clause.getBeginLoc(), ExistingClauses) ||
SemaRef.DiagnoseExclusiveClauses(Clause.getDirectiveKind(),
Clause.getClauseKind(),
Clause.getBeginLoc(), ExistingClauses))
return nullptr;
if (CheckValidRoutineGangWorkerVectorSeqNewClause(Clause) ||
CheckValidRoutineBindNewClause(Clause))
return nullptr;
switch (Clause.getClauseKind()) {
case OpenACCClauseKind::Shortloop:
llvm_unreachable("Shortloop shouldn't be generated in clang");
case OpenACCClauseKind::Invalid:
return nullptr;
#define VISIT_CLAUSE(CLAUSE_NAME) \
case OpenACCClauseKind::CLAUSE_NAME: \
return Visit##CLAUSE_NAME##Clause(Clause);
#define CLAUSE_ALIAS(ALIAS, CLAUSE_NAME, DEPRECATED) \
case OpenACCClauseKind::ALIAS: \
if (DEPRECATED) \
SemaRef.Diag(Clause.getBeginLoc(), diag::warn_acc_deprecated_alias_name) \
<< Clause.getClauseKind() << OpenACCClauseKind::CLAUSE_NAME; \
return Visit##CLAUSE_NAME##Clause(Clause);
#include "clang/Basic/OpenACCClauses.def"
}
llvm_unreachable("Invalid clause kind");
}
#define VISIT_CLAUSE(CLAUSE_NAME) \
OpenACCClause *Visit##CLAUSE_NAME##Clause( \
SemaOpenACC::OpenACCParsedClause &Clause);
#include "clang/Basic/OpenACCClauses.def"
};
OpenACCClause *SemaOpenACCClauseVisitor::VisitDefaultClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Don't add an invalid clause to the AST.
if (Clause.getDefaultClauseKind() == OpenACCDefaultClauseKind::Invalid)
return nullptr;
return OpenACCDefaultClause::Create(
Ctx, Clause.getDefaultClauseKind(), Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitTileClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCTileClause>(Clause))
return nullptr;
llvm::SmallVector<Expr *> NewSizeExprs;
// Make sure these are all positive constant expressions or *.
for (Expr *E : Clause.getIntExprs()) {
ExprResult Res = SemaRef.CheckTileSizeExpr(E);
if (!Res.isUsable())
return nullptr;
NewSizeExprs.push_back(Res.get());
}
return OpenACCTileClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), NewSizeExprs,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitIfClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// The parser has ensured that we have a proper condition expr, so there
// isn't really much to do here.
// If the 'if' clause is true, it makes the 'self' clause have no effect,
// diagnose that here. This only applies on compute/combined constructs.
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::Update) {
const auto *Itr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCSelfClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::warn_acc_if_self_conflict);
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
}
}
return OpenACCIfClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getConditionExpr(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitSelfClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// If the 'if' clause is true, it makes the 'self' clause have no effect,
// diagnose that here. This only applies on compute/combined constructs.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Update)
return OpenACCSelfClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
const auto *Itr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCIfClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::warn_acc_if_self_conflict);
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
}
return OpenACCSelfClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getConditionExpr(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNumGangsClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCNumGangsClause>(Clause))
return nullptr;
// num_gangs requires at least 1 int expr in all forms. Diagnose here, but
// allow us to continue, an empty clause might be useful for future
// diagnostics.
if (Clause.getIntExprs().empty())
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_num_gangs_num_args)
<< /*NoArgs=*/0;
unsigned MaxArgs =
(Clause.getDirectiveKind() == OpenACCDirectiveKind::Parallel ||
Clause.getDirectiveKind() == OpenACCDirectiveKind::ParallelLoop)
? 3
: 1;
// The max number of args differs between parallel and other constructs.
// Again, allow us to continue for the purposes of future diagnostics.
if (Clause.getIntExprs().size() > MaxArgs)
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_num_gangs_num_args)
<< /*NoArgs=*/1 << Clause.getDirectiveKind() << MaxArgs
<< Clause.getIntExprs().size();
// OpenACC 3.3 Section 2.9.11: A reduction clause may not appear on a loop
// directive that has a gang clause and is within a compute construct that has
// a num_gangs clause with more than one explicit argument.
if (Clause.getIntExprs().size() > 1 &&
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
auto *GangClauseItr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCGangClause>);
auto *ReductionClauseItr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCReductionClause>);
if (GangClauseItr != ExistingClauses.end() &&
ReductionClauseItr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_gang_reduction_numgangs_conflict)
<< OpenACCClauseKind::Reduction << OpenACCClauseKind::Gang
<< Clause.getDirectiveKind() << /*is on combined directive=*/1;
SemaRef.Diag((*ReductionClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*ReductionClauseItr)->getClauseKind();
SemaRef.Diag((*GangClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*GangClauseItr)->getClauseKind();
return nullptr;
}
}
// OpenACC 3.3 Section 2.5.4:
// A reduction clause may not appear on a parallel construct with a
// num_gangs clause that has more than one argument.
if ((Clause.getDirectiveKind() == OpenACCDirectiveKind::Parallel ||
Clause.getDirectiveKind() == OpenACCDirectiveKind::ParallelLoop) &&
Clause.getIntExprs().size() > 1) {
auto *Parallel =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCReductionClause>);
if (Parallel != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_reduction_num_gangs_conflict)
<< /*>1 arg in first loc=*/1 << Clause.getClauseKind()
<< Clause.getDirectiveKind() << OpenACCClauseKind::Reduction;
SemaRef.Diag((*Parallel)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*Parallel)->getClauseKind();
return nullptr;
}
}
// OpenACC 3.3 Section 2.9.2:
// An argument with no keyword or with the 'num' keyword is allowed only when
// the 'num_gangs' does not appear on the 'kernel' construct.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::KernelsLoop) {
auto GangClauses = llvm::make_filter_range(
ExistingClauses, llvm::IsaPred<OpenACCGangClause>);
for (auto *GC : GangClauses) {
if (cast<OpenACCGangClause>(GC)->hasExprOfKind(OpenACCGangKind::Num)) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_num_arg_conflict_reverse)
<< OpenACCClauseKind::NumGangs << OpenACCClauseKind::Gang
<< /*Num argument*/ 1;
SemaRef.Diag(GC->getBeginLoc(), diag::note_acc_previous_clause_here)
<< GC->getClauseKind();
return nullptr;
}
}
}
return OpenACCNumGangsClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNumWorkersClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCNumWorkersClause>(Clause))
return nullptr;
// OpenACC 3.3 Section 2.9.2:
// An argument is allowed only when the 'num_workers' does not appear on the
// kernels construct.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::KernelsLoop) {
auto WorkerClauses = llvm::make_filter_range(
ExistingClauses, llvm::IsaPred<OpenACCWorkerClause>);
for (auto *WC : WorkerClauses) {
if (cast<OpenACCWorkerClause>(WC)->hasIntExpr()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_num_arg_conflict_reverse)
<< OpenACCClauseKind::NumWorkers << OpenACCClauseKind::Worker
<< /*num argument*/ 0;
SemaRef.Diag(WC->getBeginLoc(), diag::note_acc_previous_clause_here)
<< WC->getClauseKind();
return nullptr;
}
}
}
assert(Clause.getIntExprs().size() == 1 &&
"Invalid number of expressions for NumWorkers");
return OpenACCNumWorkersClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs()[0],
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitVectorLengthClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCVectorLengthClause>(Clause))
return nullptr;
// OpenACC 3.3 Section 2.9.4:
// An argument is allowed only when the 'vector_length' does not appear on the
// 'kernels' construct.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::KernelsLoop) {
auto VectorClauses = llvm::make_filter_range(
ExistingClauses, llvm::IsaPred<OpenACCVectorClause>);
for (auto *VC : VectorClauses) {
if (cast<OpenACCVectorClause>(VC)->hasIntExpr()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_num_arg_conflict_reverse)
<< OpenACCClauseKind::VectorLength << OpenACCClauseKind::Vector
<< /*num argument*/ 0;
SemaRef.Diag(VC->getBeginLoc(), diag::note_acc_previous_clause_here)
<< VC->getClauseKind();
return nullptr;
}
}
}
assert(Clause.getIntExprs().size() == 1 &&
"Invalid number of expressions for NumWorkers");
return OpenACCVectorLengthClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs()[0],
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitAsyncClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCAsyncClause>(Clause))
return nullptr;
assert(Clause.getNumIntExprs() < 2 &&
"Invalid number of expressions for Async");
return OpenACCAsyncClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getNumIntExprs() != 0 ? Clause.getIntExprs()[0] : nullptr,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceNumClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
assert(Clause.getNumIntExprs() == 1 &&
"Invalid number of expressions for device_num");
return OpenACCDeviceNumClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs()[0],
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDefaultAsyncClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
assert(Clause.getNumIntExprs() == 1 &&
"Invalid number of expressions for default_async");
return OpenACCDefaultAsyncClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs()[0],
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitPrivateClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCPrivateClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitFirstPrivateClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCFirstPrivateClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNoCreateClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCNoCreateClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitPresentClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, OpenACCModifierKind::Invalid))
return nullptr;
return OpenACCPresentClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitHostClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCHostClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCDeviceClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
OpenACCModifierKind NewMods =
CheckModifierList(Clause, Clause.getModifierList());
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, NewMods))
return nullptr;
return OpenACCCopyClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getModifierList(), Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitLinkClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, OpenACCModifierKind::Invalid))
return nullptr;
Clause.setVarListDetails(SemaRef.CheckLinkClauseVarList(Clause.getVarList()),
OpenACCModifierKind::Invalid);
return OpenACCLinkClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceResidentClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, OpenACCModifierKind::Invalid))
return nullptr;
return OpenACCDeviceResidentClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyInClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
OpenACCModifierKind NewMods =
CheckModifierList(Clause, Clause.getModifierList());
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, NewMods))
return nullptr;
return OpenACCCopyInClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getModifierList(), Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyOutClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
OpenACCModifierKind NewMods =
CheckModifierList(Clause, Clause.getModifierList());
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, NewMods))
return nullptr;
return OpenACCCopyOutClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getModifierList(), Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCreateClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
OpenACCModifierKind NewMods =
CheckModifierList(Clause, Clause.getModifierList());
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, NewMods))
return nullptr;
return OpenACCCreateClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getModifierList(), Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitAttachClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, but we
// still have to make sure it is a pointer type.
llvm::SmallVector<Expr *> VarList{Clause.getVarList()};
llvm::erase_if(VarList, [&](Expr *E) {
return SemaRef.CheckVarIsPointerType(OpenACCClauseKind::Attach, E);
});
Clause.setVarListDetails(VarList, OpenACCModifierKind::Invalid);
return OpenACCAttachClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDetachClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, but we
// still have to make sure it is a pointer type.
llvm::SmallVector<Expr *> VarList{Clause.getVarList()};
llvm::erase_if(VarList, [&](Expr *E) {
return SemaRef.CheckVarIsPointerType(OpenACCClauseKind::Detach, E);
});
Clause.setVarListDetails(VarList, OpenACCModifierKind::Invalid);
return OpenACCDetachClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeleteClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, so there
// really isn't anything to do here. GCC does some duplicate-finding, though
// it isn't apparent in the standard where this is justified.
return OpenACCDeleteClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitUseDeviceClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable or array, so nothing
// left to do here.
return OpenACCUseDeviceClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDevicePtrClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// ActOnVar ensured that everything is a valid variable reference, but we
// still have to make sure it is a pointer type.
llvm::SmallVector<Expr *> VarList{Clause.getVarList()};
llvm::erase_if(VarList, [&](Expr *E) {
return SemaRef.CheckVarIsPointerType(OpenACCClauseKind::DevicePtr, E);
});
Clause.setVarListDetails(VarList, OpenACCModifierKind::Invalid);
// 'declare' has some restrictions that need to be enforced separately, so
// check it here.
if (SemaRef.CheckDeclareClause(Clause, OpenACCModifierKind::Invalid))
return nullptr;
return OpenACCDevicePtrClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getVarList(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitWaitClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
return OpenACCWaitClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getDevNumExpr(),
Clause.getQueuesLoc(), Clause.getQueueIdExprs(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceTypeClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Based on discussions, having more than 1 'architecture' on a 'set' is
// nonsensical, so we're going to fix the standard to reflect this. Implement
// the limitation, since the Dialect requires this.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Set &&
Clause.getDeviceTypeArchitectures().size() > 1) {
SemaRef.Diag(Clause.getDeviceTypeArchitectures()[1].getLoc(),
diag::err_acc_device_type_multiple_archs);
return nullptr;
}
// The list of valid device_type values. Flang also has these hardcoded in
// openacc_parsers.cpp, as there does not seem to be a reliable backend
// source. The list below is sourced from Flang, though NVC++ supports only
// 'nvidia', 'host', 'multicore', and 'default'.
const std::array<llvm::StringLiteral, 6> ValidValues{
"default", "nvidia", "acc_device_nvidia", "radeon", "host", "multicore"};
// As an optimization, we have a manually maintained list of valid values
// below, rather than trying to calculate from above. These should be kept in
// sync if/when the above list ever changes.
std::string ValidValuesString =
"'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'";
llvm::SmallVector<DeviceTypeArgument> Architectures{
Clause.getDeviceTypeArchitectures()};
// The parser has ensured that we either have a single entry of just '*'
// (represented by a nullptr IdentifierInfo), or a list.
bool Diagnosed = false;
auto FilterPred = [&](const DeviceTypeArgument &Arch) {
// The '*' case.
if (!Arch.getIdentifierInfo())
return false;
return llvm::find_if(ValidValues, [&](StringRef RHS) {
return Arch.getIdentifierInfo()->getName().equals_insensitive(RHS);
}) == ValidValues.end();
};
auto Diagnose = [&](const DeviceTypeArgument &Arch) {
Diagnosed = SemaRef.Diag(Arch.getLoc(), diag::err_acc_invalid_default_type)
<< Arch.getIdentifierInfo() << Clause.getClauseKind()
<< ValidValuesString;
};
// There aren't stable enumertor versions of 'for-each-then-erase', so do it
// here. We DO keep track of whether we diagnosed something to make sure we
// don't do the 'erase_if' in the event that the first list didn't find
// anything.
llvm::for_each(llvm::make_filter_range(Architectures, FilterPred), Diagnose);
if (Diagnosed)
llvm::erase_if(Architectures, FilterPred);
return OpenACCDeviceTypeClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Architectures, Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitAutoClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
return OpenACCAutoClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNoHostClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
return OpenACCNoHostClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitIndependentClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
return OpenACCIndependentClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
ExprResult CheckGangStaticExpr(SemaOpenACC &S, Expr *E) {
if (isa<OpenACCAsteriskSizeExpr>(E))
return E;
return S.ActOnIntExpr(OpenACCDirectiveKind::Invalid, OpenACCClauseKind::Gang,
E->getBeginLoc(), E);
}
bool IsOrphanLoop(OpenACCDirectiveKind DK, OpenACCDirectiveKind AssocKind) {
return DK == OpenACCDirectiveKind::Loop &&
AssocKind == OpenACCDirectiveKind::Invalid;
}
bool HasAssocKind(OpenACCDirectiveKind DK, OpenACCDirectiveKind AssocKind) {
return DK == OpenACCDirectiveKind::Loop &&
AssocKind != OpenACCDirectiveKind::Invalid;
}
ExprResult DiagIntArgInvalid(SemaOpenACC &S, Expr *E, OpenACCGangKind GK,
OpenACCClauseKind CK, OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind) {
S.Diag(E->getBeginLoc(), diag::err_acc_int_arg_invalid)
<< GK << CK << IsOrphanLoop(DK, AssocKind) << DK
<< HasAssocKind(DK, AssocKind) << AssocKind;
return ExprError();
}
ExprResult DiagIntArgInvalid(SemaOpenACC &S, Expr *E, StringRef TagKind,
OpenACCClauseKind CK, OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind) {
S.Diag(E->getBeginLoc(), diag::err_acc_int_arg_invalid)
<< TagKind << CK << IsOrphanLoop(DK, AssocKind) << DK
<< HasAssocKind(DK, AssocKind) << AssocKind;
return ExprError();
}
ExprResult CheckGangDimExpr(SemaOpenACC &S, Expr *E) {
// OpenACC 3.3 2.9.2: When the parent compute construct is a parallel
// construct, or an orphaned loop construct, the gang clause behaves as
// follows. ... The dim argument must be a constant positive integer value
// 1, 2, or 3.
// -also-
// OpenACC 3.3 2.15: The 'dim' argument must be a constant positive integer
// with value 1, 2, or 3.
if (!E)
return ExprError();
ExprResult Res = S.ActOnIntExpr(OpenACCDirectiveKind::Invalid,
OpenACCClauseKind::Gang, E->getBeginLoc(), E);
if (!Res.isUsable())
return Res;
if (Res.get()->isInstantiationDependent())
return Res;
std::optional<llvm::APSInt> ICE =
Res.get()->getIntegerConstantExpr(S.getASTContext());
if (!ICE || *ICE <= 0 || ICE > 3) {
S.Diag(Res.get()->getBeginLoc(), diag::err_acc_gang_dim_value)
<< ICE.has_value() << ICE.value_or(llvm::APSInt{}).getExtValue();
return ExprError();
}
return ExprResult{
ConstantExpr::Create(S.getASTContext(), Res.get(), APValue{*ICE})};
}
ExprResult CheckGangParallelExpr(SemaOpenACC &S, OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind,
OpenACCGangKind GK, Expr *E) {
switch (GK) {
case OpenACCGangKind::Static:
return CheckGangStaticExpr(S, E);
case OpenACCGangKind::Num:
// OpenACC 3.3 2.9.2: When the parent compute construct is a parallel
// construct, or an orphaned loop construct, the gang clause behaves as
// follows. ... The num argument is not allowed.
return DiagIntArgInvalid(S, E, GK, OpenACCClauseKind::Gang, DK, AssocKind);
case OpenACCGangKind::Dim:
return CheckGangDimExpr(S, E);
}
llvm_unreachable("Unknown gang kind in gang parallel check");
}
ExprResult CheckGangKernelsExpr(SemaOpenACC &S,
ArrayRef<const OpenACCClause *> ExistingClauses,
OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind,
OpenACCGangKind GK, Expr *E) {
switch (GK) {
// OpenACC 3.3 2.9.2: When the parent compute construct is a kernels
// construct, the gang clause behaves as follows. ... The dim argument is
// not allowed.
case OpenACCGangKind::Dim:
return DiagIntArgInvalid(S, E, GK, OpenACCClauseKind::Gang, DK, AssocKind);
case OpenACCGangKind::Num: {
// OpenACC 3.3 2.9.2: When the parent compute construct is a kernels
// construct, the gang clause behaves as follows. ... An argument with no
// keyword or with num keyword is only allowed when num_gangs does not
// appear on the kernels construct. ... The region of a loop with the gang
// clause may not contain another loop with a gang clause unless within a
// nested compute region.
// If this is a 'combined' construct, search the list of existing clauses.
// Else we need to search the containing 'kernel'.
auto Collection = isOpenACCCombinedDirectiveKind(DK)
? ExistingClauses
: S.getActiveComputeConstructInfo().Clauses;
const auto *Itr =
llvm::find_if(Collection, llvm::IsaPred<OpenACCNumGangsClause>);
if (Itr != Collection.end()) {
S.Diag(E->getBeginLoc(), diag::err_acc_num_arg_conflict)
<< "num" << OpenACCClauseKind::Gang << DK
<< HasAssocKind(DK, AssocKind) << AssocKind
<< OpenACCClauseKind::NumGangs;
S.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
return ExprError();
}
return ExprResult{E};
}
case OpenACCGangKind::Static:
return CheckGangStaticExpr(S, E);
}
llvm_unreachable("Unknown gang kind in gang kernels check");
}
ExprResult CheckGangSerialExpr(SemaOpenACC &S, OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind,
OpenACCGangKind GK, Expr *E) {
switch (GK) {
// 'dim' and 'num' don't really make sense on serial, and GCC rejects them
// too, so we disallow them too.
case OpenACCGangKind::Dim:
case OpenACCGangKind::Num:
return DiagIntArgInvalid(S, E, GK, OpenACCClauseKind::Gang, DK, AssocKind);
case OpenACCGangKind::Static:
return CheckGangStaticExpr(S, E);
}
llvm_unreachable("Unknown gang kind in gang serial check");
}
ExprResult CheckGangRoutineExpr(SemaOpenACC &S, OpenACCDirectiveKind DK,
OpenACCDirectiveKind AssocKind,
OpenACCGangKind GK, Expr *E) {
switch (GK) {
// Only 'dim' is allowed on a routine, so diallow num and static.
case OpenACCGangKind::Num:
case OpenACCGangKind::Static:
return DiagIntArgInvalid(S, E, GK, OpenACCClauseKind::Gang, DK, AssocKind);
case OpenACCGangKind::Dim:
return CheckGangDimExpr(S, E);
}
llvm_unreachable("Unknown gang kind in gang serial check");
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitVectorClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagGangWorkerVectorSeqConflict(Clause))
return nullptr;
Expr *IntExpr =
Clause.getNumIntExprs() != 0 ? Clause.getIntExprs()[0] : nullptr;
if (IntExpr) {
switch (Clause.getDirectiveKind()) {
default:
llvm_unreachable("Invalid directive kind for this clause");
case OpenACCDirectiveKind::Loop:
switch (SemaRef.getActiveComputeConstructInfo().Kind) {
case OpenACCDirectiveKind::Invalid:
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::ParallelLoop:
// No restriction on when 'parallel' can contain an argument.
break;
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::SerialLoop:
// GCC disallows this, and there is no real good reason for us to permit
// it, so disallow until we come up with a use case that makes sense.
DiagIntArgInvalid(SemaRef, IntExpr, "length", OpenACCClauseKind::Vector,
Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
break;
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::KernelsLoop: {
const auto *Itr =
llvm::find_if(SemaRef.getActiveComputeConstructInfo().Clauses,
llvm::IsaPred<OpenACCVectorLengthClause>);
if (Itr != SemaRef.getActiveComputeConstructInfo().Clauses.end()) {
SemaRef.Diag(IntExpr->getBeginLoc(), diag::err_acc_num_arg_conflict)
<< "length" << OpenACCClauseKind::Vector
<< Clause.getDirectiveKind()
<< HasAssocKind(Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind)
<< SemaRef.getActiveComputeConstructInfo().Kind
<< OpenACCClauseKind::VectorLength;
SemaRef.Diag((*Itr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
IntExpr = nullptr;
}
break;
}
default:
llvm_unreachable("Non compute construct in active compute construct");
}
break;
case OpenACCDirectiveKind::KernelsLoop: {
const auto *Itr = llvm::find_if(ExistingClauses,
llvm::IsaPred<OpenACCVectorLengthClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(IntExpr->getBeginLoc(), diag::err_acc_num_arg_conflict)
<< "length" << OpenACCClauseKind::Vector
<< Clause.getDirectiveKind()
<< HasAssocKind(Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind)
<< SemaRef.getActiveComputeConstructInfo().Kind
<< OpenACCClauseKind::VectorLength;
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
IntExpr = nullptr;
}
break;
}
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::Routine:
DiagIntArgInvalid(SemaRef, IntExpr, "length", OpenACCClauseKind::Vector,
Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
break;
case OpenACCDirectiveKind::ParallelLoop:
break;
case OpenACCDirectiveKind::Invalid:
// This can happen when the directive was not recognized, but we continued
// anyway. Since there is a lot of stuff that can happen (including
// 'allow anything' in the parallel loop case), just skip all checking and
// continue.
break;
}
}
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop) {
// OpenACC 3.3 2.9.4: The region of a loop with a 'vector' clause may not
// contain a loop with a gang, worker, or vector clause unless within a
// nested compute region.
if (SemaRef.LoopVectorClauseLoc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Vector << OpenACCClauseKind::Vector
<< /*skip kernels construct info*/ 0;
SemaRef.Diag(SemaRef.LoopVectorClauseLoc,
diag::note_acc_previous_clause_here)
<< "vector";
return nullptr;
}
}
return OpenACCVectorClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), IntExpr,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitWorkerClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagGangWorkerVectorSeqConflict(Clause))
return nullptr;
Expr *IntExpr =
Clause.getNumIntExprs() != 0 ? Clause.getIntExprs()[0] : nullptr;
if (IntExpr) {
switch (Clause.getDirectiveKind()) {
default:
llvm_unreachable("Invalid directive kind for this clause");
case OpenACCDirectiveKind::Invalid:
// This can happen in cases where the directive was not recognized but we
// continued anyway. Kernels allows kind of any integer argument, so we
// can assume it is that (rather than marking the argument invalid like
// with parallel/serial/routine), and just continue as if nothing
// happened. We'll skip the 'kernels' checking vs num-workers, since this
// MIGHT be something else.
break;
case OpenACCDirectiveKind::Loop:
switch (SemaRef.getActiveComputeConstructInfo().Kind) {
case OpenACCDirectiveKind::Invalid:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
DiagIntArgInvalid(SemaRef, IntExpr, OpenACCGangKind::Num,
OpenACCClauseKind::Worker, Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
break;
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Kernels: {
const auto *Itr =
llvm::find_if(SemaRef.getActiveComputeConstructInfo().Clauses,
llvm::IsaPred<OpenACCNumWorkersClause>);
if (Itr != SemaRef.getActiveComputeConstructInfo().Clauses.end()) {
SemaRef.Diag(IntExpr->getBeginLoc(), diag::err_acc_num_arg_conflict)
<< "num" << OpenACCClauseKind::Worker << Clause.getDirectiveKind()
<< HasAssocKind(Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind)
<< SemaRef.getActiveComputeConstructInfo().Kind
<< OpenACCClauseKind::NumWorkers;
SemaRef.Diag((*Itr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
IntExpr = nullptr;
}
break;
}
default:
llvm_unreachable("Non compute construct in active compute construct");
}
break;
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::Routine:
DiagIntArgInvalid(SemaRef, IntExpr, OpenACCGangKind::Num,
OpenACCClauseKind::Worker, Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
break;
case OpenACCDirectiveKind::KernelsLoop: {
const auto *Itr = llvm::find_if(ExistingClauses,
llvm::IsaPred<OpenACCNumWorkersClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(IntExpr->getBeginLoc(), diag::err_acc_num_arg_conflict)
<< "num" << OpenACCClauseKind::Worker << Clause.getDirectiveKind()
<< HasAssocKind(Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind)
<< SemaRef.getActiveComputeConstructInfo().Kind
<< OpenACCClauseKind::NumWorkers;
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
IntExpr = nullptr;
}
}
}
}
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop) {
// OpenACC 3.3 2.9.3: The region of a loop with a 'worker' clause may not
// contain a loop with a gang or worker clause unless within a nested
// compute region.
if (SemaRef.LoopWorkerClauseLoc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Worker << OpenACCClauseKind::Worker
<< /*skip kernels construct info*/ 0;
SemaRef.Diag(SemaRef.LoopWorkerClauseLoc,
diag::note_acc_previous_clause_here)
<< "worker";
return nullptr;
}
// OpenACC 3.3 2.9.4: The region of a loop with a 'vector' clause may not
// contain a loop with a gang, worker, or vector clause unless within a
// nested compute region.
if (SemaRef.LoopVectorClauseLoc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Worker << OpenACCClauseKind::Vector
<< /*skip kernels construct info*/ 0;
SemaRef.Diag(SemaRef.LoopVectorClauseLoc,
diag::note_acc_previous_clause_here)
<< "vector";
return nullptr;
}
}
return OpenACCWorkerClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), IntExpr,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitGangClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagGangWorkerVectorSeqConflict(Clause))
return nullptr;
// OpenACC 3.3 Section 2.9.11: A reduction clause may not appear on a loop
// directive that has a gang clause and is within a compute construct that has
// a num_gangs clause with more than one explicit argument.
if ((Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop &&
SemaRef.getActiveComputeConstructInfo().Kind !=
OpenACCDirectiveKind::Invalid) ||
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
// num_gangs clause on the active compute construct.
auto ActiveComputeConstructContainer =
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())
? ExistingClauses
: SemaRef.getActiveComputeConstructInfo().Clauses;
auto *NumGangsClauseItr = llvm::find_if(
ActiveComputeConstructContainer, llvm::IsaPred<OpenACCNumGangsClause>);
if (NumGangsClauseItr != ActiveComputeConstructContainer.end() &&
cast<OpenACCNumGangsClause>(*NumGangsClauseItr)->getIntExprs().size() >
1) {
auto *ReductionClauseItr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCReductionClause>);
if (ReductionClauseItr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_gang_reduction_numgangs_conflict)
<< OpenACCClauseKind::Gang << OpenACCClauseKind::Reduction
<< Clause.getDirectiveKind()
<< isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind());
SemaRef.Diag((*ReductionClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*ReductionClauseItr)->getClauseKind();
SemaRef.Diag((*NumGangsClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*NumGangsClauseItr)->getClauseKind();
return nullptr;
}
}
}
llvm::SmallVector<OpenACCGangKind> GangKinds;
llvm::SmallVector<Expr *> IntExprs;
// Store the existing locations, so we can do duplicate checking. Index is
// the int-value of the OpenACCGangKind enum.
SourceLocation ExistingElemLoc[3];
for (unsigned I = 0; I < Clause.getIntExprs().size(); ++I) {
OpenACCGangKind GK = Clause.getGangKinds()[I];
ExprResult ER =
SemaRef.CheckGangExpr(ExistingClauses, Clause.getDirectiveKind(), GK,
Clause.getIntExprs()[I]);
if (!ER.isUsable())
continue;
// OpenACC 3.3 2.9: 'gang-arg-list' may have at most one num, one dim, and
// one static argument.
if (ExistingElemLoc[static_cast<unsigned>(GK)].isValid()) {
SemaRef.Diag(ER.get()->getBeginLoc(), diag::err_acc_gang_multiple_elt)
<< static_cast<unsigned>(GK);
SemaRef.Diag(ExistingElemLoc[static_cast<unsigned>(GK)],
diag::note_acc_previous_expr_here);
continue;
}
ExistingElemLoc[static_cast<unsigned>(GK)] = ER.get()->getBeginLoc();
GangKinds.push_back(GK);
IntExprs.push_back(ER.get());
}
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop) {
// OpenACC 3.3 2.9.2: When the parent compute construct is a kernels
// construct, the gang clause behaves as follows. ... The region of a loop
// with a gang clause may not contain another loop with a gang clause unless
// within a nested compute region.
if (SemaRef.LoopGangClauseOnKernel.Loc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Gang << OpenACCClauseKind::Gang
<< /*kernels construct info*/ 1
<< SemaRef.LoopGangClauseOnKernel.DirKind;
SemaRef.Diag(SemaRef.LoopGangClauseOnKernel.Loc,
diag::note_acc_previous_clause_here)
<< "gang";
return nullptr;
}
// OpenACC 3.3 2.9.3: The region of a loop with a 'worker' clause may not
// contain a loop with a gang or worker clause unless within a nested
// compute region.
if (SemaRef.LoopWorkerClauseLoc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Gang << OpenACCClauseKind::Worker
<< /*!kernels construct info*/ 0;
SemaRef.Diag(SemaRef.LoopWorkerClauseLoc,
diag::note_acc_previous_clause_here)
<< "worker";
return nullptr;
}
// OpenACC 3.3 2.9.4: The region of a loop with a 'vector' clause may not
// contain a loop with a gang, worker, or vector clause unless within a
// nested compute region.
if (SemaRef.LoopVectorClauseLoc.isValid()) {
// This handles the 'inner loop' diagnostic, but we cannot set that we're
// on one of these until we get to the end of the construct.
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_in_clause_region)
<< OpenACCClauseKind::Gang << OpenACCClauseKind::Vector
<< /*!kernels construct info*/ 0;
SemaRef.Diag(SemaRef.LoopVectorClauseLoc,
diag::note_acc_previous_clause_here)
<< "vector";
return nullptr;
}
}
return SemaRef.CheckGangClause(Clause.getDirectiveKind(), ExistingClauses,
Clause.getBeginLoc(), Clause.getLParenLoc(),
GangKinds, IntExprs, Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitFinalizeClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// There isn't anything to do here, this is only valid on one construct, and
// has no associated rules.
return OpenACCFinalizeClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitIfPresentClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// There isn't anything to do here, this is only valid on one construct, and
// has no associated rules.
return OpenACCIfPresentClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitSeqClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// OpenACC 3.3 2.9:
// A 'gang', 'worker', or 'vector' clause may not appear if a 'seq' clause
// appears.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop ||
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
const auto *Itr = llvm::find_if(
ExistingClauses, llvm::IsaPred<OpenACCGangClause, OpenACCVectorClause,
OpenACCWorkerClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_clause_cannot_combine)
<< Clause.getClauseKind() << (*Itr)->getClauseKind()
<< Clause.getDirectiveKind();
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here)
<< (*Itr)->getClauseKind();
return nullptr;
}
}
return OpenACCSeqClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitReductionClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// OpenACC 3.3 Section 2.9.11: A reduction clause may not appear on a loop
// directive that has a gang clause and is within a compute construct that has
// a num_gangs clause with more than one explicit argument.
if ((Clause.getDirectiveKind() == OpenACCDirectiveKind::Loop &&
SemaRef.getActiveComputeConstructInfo().Kind !=
OpenACCDirectiveKind::Invalid) ||
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
// num_gangs clause on the active compute construct.
auto ActiveComputeConstructContainer =
isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())
? ExistingClauses
: SemaRef.getActiveComputeConstructInfo().Clauses;
auto *NumGangsClauseItr = llvm::find_if(
ActiveComputeConstructContainer, llvm::IsaPred<OpenACCNumGangsClause>);
if (NumGangsClauseItr != ActiveComputeConstructContainer.end() &&
cast<OpenACCNumGangsClause>(*NumGangsClauseItr)->getIntExprs().size() >
1) {
auto *GangClauseItr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCGangClause>);
if (GangClauseItr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_gang_reduction_numgangs_conflict)
<< OpenACCClauseKind::Reduction << OpenACCClauseKind::Gang
<< Clause.getDirectiveKind()
<< isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind());
SemaRef.Diag((*GangClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*GangClauseItr)->getClauseKind();
SemaRef.Diag((*NumGangsClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*NumGangsClauseItr)->getClauseKind();
return nullptr;
}
}
}
// OpenACC3.3 Section 2.9.11: If a variable is involved in a reduction that
// spans multiple nested loops where two or more of those loops have
// associated loop directives, a reduction clause containing that variable
// must appear on each of those loop directives.
//
// This can't really be implemented in the CFE, as this requires a level of
// rechability/useage analysis that we're not really wanting to get into.
// Additionally, I'm alerted that this restriction is one that the middle-end
// can just 'figure out' as an extension and isn't really necessary.
//
// OpenACC3.3 Section 2.9.11: Every 'var' in a reduction clause appearing on
// an orphaned loop construct must be private.
//
// This again is something we cannot really diagnose, as it requires we see
// all the uses/scopes of all variables referenced. The middle end/MLIR might
// be able to diagnose this.
// OpenACC 3.3 Section 2.5.4:
// A reduction clause may not appear on a parallel construct with a
// num_gangs clause that has more than one argument.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Parallel ||
Clause.getDirectiveKind() == OpenACCDirectiveKind::ParallelLoop) {
auto NumGangsClauses = llvm::make_filter_range(
ExistingClauses, llvm::IsaPred<OpenACCNumGangsClause>);
for (auto *NGC : NumGangsClauses) {
unsigned NumExprs =
cast<OpenACCNumGangsClause>(NGC)->getIntExprs().size();
if (NumExprs > 1) {
SemaRef.Diag(Clause.getBeginLoc(),
diag::err_acc_reduction_num_gangs_conflict)
<< /*>1 arg in first loc=*/0 << Clause.getClauseKind()
<< Clause.getDirectiveKind() << OpenACCClauseKind::NumGangs;
SemaRef.Diag(NGC->getBeginLoc(), diag::note_acc_previous_clause_here)
<< NGC->getClauseKind();
return nullptr;
}
}
}
SmallVector<Expr *> ValidVars;
for (Expr *Var : Clause.getVarList()) {
ExprResult Res = SemaRef.CheckReductionVar(Clause.getDirectiveKind(),
Clause.getReductionOp(), Var);
if (Res.isUsable())
ValidVars.push_back(Res.get());
}
return SemaRef.CheckReductionClause(
ExistingClauses, Clause.getDirectiveKind(), Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getReductionOp(), ValidVars,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCollapseClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DisallowSinceLastDeviceType<OpenACCCollapseClause>(Clause))
return nullptr;
ExprResult LoopCount = SemaRef.CheckCollapseLoopCount(Clause.getLoopCount());
if (!LoopCount.isUsable())
return nullptr;
return OpenACCCollapseClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.isForce(),
LoopCount.get(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitBindClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (std::holds_alternative<StringLiteral *>(Clause.getBindDetails()))
return OpenACCBindClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(),
std::get<StringLiteral *>(Clause.getBindDetails()), Clause.getEndLoc());
return OpenACCBindClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(),
std::get<IdentifierInfo *>(Clause.getBindDetails()), Clause.getEndLoc());
}
// Return true if the two vars refer to the same variable, for the purposes of
// equality checking.
bool areVarsEqual(Expr *VarExpr1, Expr *VarExpr2) {
if (VarExpr1->isInstantiationDependent() ||
VarExpr2->isInstantiationDependent())
return false;
VarExpr1 = VarExpr1->IgnoreParenCasts();
VarExpr2 = VarExpr2->IgnoreParenCasts();
// Legal expressions can be: Scalar variable reference, sub-array, array
// element, or composite variable member.
// Sub-array.
if (isa<ArraySectionExpr>(VarExpr1)) {
auto *Expr2AS = dyn_cast<ArraySectionExpr>(VarExpr2);
if (!Expr2AS)
return false;
auto *Expr1AS = cast<ArraySectionExpr>(VarExpr1);
if (!areVarsEqual(Expr1AS->getBase(), Expr2AS->getBase()))
return false;
// We could possibly check to see if the ranges aren't overlapping, but it
// isn't clear that the rules allow this.
return true;
}
// Array-element.
if (isa<ArraySubscriptExpr>(VarExpr1)) {
auto *Expr2AS = dyn_cast<ArraySubscriptExpr>(VarExpr2);
if (!Expr2AS)
return false;
auto *Expr1AS = cast<ArraySubscriptExpr>(VarExpr1);
if (!areVarsEqual(Expr1AS->getBase(), Expr2AS->getBase()))
return false;
// We could possibly check to see if the elements referenced aren't the
// same, but it isn't clear by reading of the standard that this is allowed
// (and that the 'var' refered to isn't the array).
return true;
}
// Scalar variable reference, or composite variable.
if (isa<DeclRefExpr>(VarExpr1)) {
auto *Expr2DRE = dyn_cast<DeclRefExpr>(VarExpr2);
if (!Expr2DRE)
return false;
auto *Expr1DRE = cast<DeclRefExpr>(VarExpr1);
return Expr1DRE->getDecl()->getMostRecentDecl() ==
Expr2DRE->getDecl()->getMostRecentDecl();
}
llvm_unreachable("Unknown variable type encountered");
}
} // namespace
OpenACCClause *
SemaOpenACC::ActOnClause(ArrayRef<const OpenACCClause *> ExistingClauses,
OpenACCParsedClause &Clause) {
if (Clause.getClauseKind() == OpenACCClauseKind::Invalid)
return nullptr;
if (DiagnoseAllowedClauses(Clause.getDirectiveKind(), Clause.getClauseKind(),
Clause.getBeginLoc()))
return nullptr;
//// Diagnose that we don't support this clause on this directive.
// if (!doesClauseApplyToDirective(Clause.getDirectiveKind(),
// Clause.getClauseKind())) {
// Diag(Clause.getBeginLoc(), diag::err_acc_clause_appertainment)
// << Clause.getDirectiveKind() << Clause.getClauseKind();
// return nullptr;
// }
if (const auto *DevTypeClause = llvm::find_if(
ExistingClauses, llvm::IsaPred<OpenACCDeviceTypeClause>);
DevTypeClause != ExistingClauses.end()) {
if (checkValidAfterDeviceType(
*this, *cast<OpenACCDeviceTypeClause>(*DevTypeClause), Clause))
return nullptr;
}
SemaOpenACCClauseVisitor Visitor{*this, ExistingClauses};
OpenACCClause *Result = Visitor.Visit(Clause);
assert((!Result || Result->getClauseKind() == Clause.getClauseKind()) &&
"Created wrong clause?");
return Result;
}
/// OpenACC 3.3 section 2.5.15:
/// At a mininmum, the supported data types include ... the numerical data types
/// in C, C++, and Fortran.
///
/// If the reduction var is a composite variable, each
/// member of the composite variable must be a supported datatype for the
/// reduction operation.
ExprResult SemaOpenACC::CheckReductionVar(OpenACCDirectiveKind DirectiveKind,
OpenACCReductionOperator ReductionOp,
Expr *VarExpr) {
VarExpr = VarExpr->IgnoreParenCasts();
auto TypeIsValid = [](QualType Ty) {
return Ty->isDependentType() || Ty->isScalarType();
};
if (isa<ArraySectionExpr>(VarExpr)) {
Expr *ASExpr = VarExpr;
QualType BaseTy = ArraySectionExpr::getBaseOriginalType(ASExpr);
QualType EltTy = getASTContext().getBaseElementType(BaseTy);
if (!TypeIsValid(EltTy)) {
Diag(VarExpr->getExprLoc(), diag::err_acc_reduction_type)
<< EltTy << /*Sub array base type*/ 1;
return ExprError();
}
} else if (auto *RD = VarExpr->getType()->getAsRecordDecl()) {
if (!RD->isStruct() && !RD->isClass()) {
Diag(VarExpr->getExprLoc(), diag::err_acc_reduction_composite_type)
<< /*not class or struct*/ 0 << VarExpr->getType();
return ExprError();
}
if (!RD->isCompleteDefinition()) {
Diag(VarExpr->getExprLoc(), diag::err_acc_reduction_composite_type)
<< /*incomplete*/ 1 << VarExpr->getType();
return ExprError();
}
if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
CXXRD && !CXXRD->isAggregate()) {
Diag(VarExpr->getExprLoc(), diag::err_acc_reduction_composite_type)
<< /*aggregate*/ 2 << VarExpr->getType();
return ExprError();
}
for (FieldDecl *FD : RD->fields()) {
if (!TypeIsValid(FD->getType())) {
Diag(VarExpr->getExprLoc(),
diag::err_acc_reduction_composite_member_type);
Diag(FD->getLocation(), diag::note_acc_reduction_composite_member_loc);
return ExprError();
}
}
} else if (!TypeIsValid(VarExpr->getType())) {
Diag(VarExpr->getExprLoc(), diag::err_acc_reduction_type)
<< VarExpr->getType() << /*Sub array base type*/ 0;
return ExprError();
}
// OpenACC3.3: 2.9.11: Reduction clauses on nested constructs for the same
// reduction 'var' must have the same reduction operator.
if (!VarExpr->isInstantiationDependent()) {
for (const OpenACCReductionClause *RClause : ActiveReductionClauses) {
if (RClause->getReductionOp() == ReductionOp)
break;
for (Expr *OldVarExpr : RClause->getVarList()) {
if (OldVarExpr->isInstantiationDependent())
continue;
if (areVarsEqual(VarExpr, OldVarExpr)) {
Diag(VarExpr->getExprLoc(), diag::err_reduction_op_mismatch)
<< ReductionOp << RClause->getReductionOp();
Diag(OldVarExpr->getExprLoc(), diag::note_acc_previous_clause_here)
<< RClause->getClauseKind();
return ExprError();
}
}
}
}
return VarExpr;
}
ExprResult SemaOpenACC::CheckTileSizeExpr(Expr *SizeExpr) {
if (!SizeExpr)
return ExprError();
assert((SizeExpr->isInstantiationDependent() ||
SizeExpr->getType()->isIntegerType()) &&
"size argument non integer?");
// If dependent, or an asterisk, the expression is fine.
if (SizeExpr->isInstantiationDependent() ||
isa<OpenACCAsteriskSizeExpr>(SizeExpr))
return ExprResult{SizeExpr};
std::optional<llvm::APSInt> ICE =
SizeExpr->getIntegerConstantExpr(getASTContext());
// OpenACC 3.3 2.9.8
// where each tile size is a constant positive integer expression or asterisk.
if (!ICE || *ICE <= 0) {
Diag(SizeExpr->getBeginLoc(), diag::err_acc_size_expr_value)
<< ICE.has_value() << ICE.value_or(llvm::APSInt{}).getExtValue();
return ExprError();
}
return ExprResult{
ConstantExpr::Create(getASTContext(), SizeExpr, APValue{*ICE})};
}
ExprResult SemaOpenACC::CheckCollapseLoopCount(Expr *LoopCount) {
if (!LoopCount)
return ExprError();
assert((LoopCount->isInstantiationDependent() ||
LoopCount->getType()->isIntegerType()) &&
"Loop argument non integer?");
// If this is dependent, there really isn't anything we can check.
if (LoopCount->isInstantiationDependent())
return ExprResult{LoopCount};
std::optional<llvm::APSInt> ICE =
LoopCount->getIntegerConstantExpr(getASTContext());
// OpenACC 3.3: 2.9.1
// The argument to the collapse clause must be a constant positive integer
// expression.
if (!ICE || *ICE <= 0) {
Diag(LoopCount->getBeginLoc(), diag::err_acc_collapse_loop_count)
<< ICE.has_value() << ICE.value_or(llvm::APSInt{}).getExtValue();
return ExprError();
}
return ExprResult{
ConstantExpr::Create(getASTContext(), LoopCount, APValue{*ICE})};
}
ExprResult
SemaOpenACC::CheckGangExpr(ArrayRef<const OpenACCClause *> ExistingClauses,
OpenACCDirectiveKind DK, OpenACCGangKind GK,
Expr *E) {
// There are two cases for the enforcement here: the 'current' directive is a
// 'loop', where we need to check the active compute construct kind, or the
// current directive is a 'combined' construct, where we have to check the
// current one.
switch (DK) {
case OpenACCDirectiveKind::ParallelLoop:
return CheckGangParallelExpr(*this, DK, ActiveComputeConstructInfo.Kind, GK,
E);
case OpenACCDirectiveKind::SerialLoop:
return CheckGangSerialExpr(*this, DK, ActiveComputeConstructInfo.Kind, GK,
E);
case OpenACCDirectiveKind::KernelsLoop:
return CheckGangKernelsExpr(*this, ExistingClauses, DK,
ActiveComputeConstructInfo.Kind, GK, E);
case OpenACCDirectiveKind::Routine:
return CheckGangRoutineExpr(*this, DK, ActiveComputeConstructInfo.Kind, GK,
E);
case OpenACCDirectiveKind::Loop:
switch (ActiveComputeConstructInfo.Kind) {
case OpenACCDirectiveKind::Invalid:
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::ParallelLoop:
return CheckGangParallelExpr(*this, DK, ActiveComputeConstructInfo.Kind,
GK, E);
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::Serial:
return CheckGangSerialExpr(*this, DK, ActiveComputeConstructInfo.Kind, GK,
E);
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Kernels:
return CheckGangKernelsExpr(*this, ExistingClauses, DK,
ActiveComputeConstructInfo.Kind, GK, E);
default:
llvm_unreachable("Non compute construct in active compute construct?");
}
case OpenACCDirectiveKind::Invalid:
// This can happen in cases where the the directive was not recognized but
// we continued anyway. Since the validity checking is all-over the place
// (it can be a star/integer, or a constant expr depending on the tag), we
// just give up and return an ExprError here.
return ExprError();
default:
llvm_unreachable("Invalid directive kind for a Gang clause");
}
llvm_unreachable("Compute construct directive not handled?");
}
OpenACCClause *
SemaOpenACC::CheckGangClause(OpenACCDirectiveKind DirKind,
ArrayRef<const OpenACCClause *> ExistingClauses,
SourceLocation BeginLoc, SourceLocation LParenLoc,
ArrayRef<OpenACCGangKind> GangKinds,
ArrayRef<Expr *> IntExprs, SourceLocation EndLoc) {
// Reduction isn't possible on 'routine' so we don't bother checking it here.
if (DirKind != OpenACCDirectiveKind::Routine) {
// OpenACC 3.3 2.9.11: A reduction clause may not appear on a loop directive
// that has a gang clause with a dim: argument whose value is greater
// than 1.
const auto *ReductionItr =
llvm::find_if(ExistingClauses, llvm::IsaPred<OpenACCReductionClause>);
if (ReductionItr != ExistingClauses.end()) {
const auto GangZip = llvm::zip_equal(GangKinds, IntExprs);
const auto GangItr = llvm::find_if(GangZip, [](const auto &Tuple) {
return std::get<0>(Tuple) == OpenACCGangKind::Dim;
});
if (GangItr != GangZip.end()) {
const Expr *DimExpr = std::get<1>(*GangItr);
assert((DimExpr->isInstantiationDependent() ||
isa<ConstantExpr>(DimExpr)) &&
"Improperly formed gang argument");
if (const auto *DimVal = dyn_cast<ConstantExpr>(DimExpr);
DimVal && DimVal->getResultAsAPSInt() > 1) {
Diag(DimVal->getBeginLoc(), diag::err_acc_gang_reduction_conflict)
<< /*gang/reduction=*/0 << DirKind;
Diag((*ReductionItr)->getBeginLoc(),
diag::note_acc_previous_clause_here)
<< (*ReductionItr)->getClauseKind();
return nullptr;
}
}
}
}
return OpenACCGangClause::Create(getASTContext(), BeginLoc, LParenLoc,
GangKinds, IntExprs, EndLoc);
}
OpenACCClause *SemaOpenACC::CheckReductionClause(
ArrayRef<const OpenACCClause *> ExistingClauses,
OpenACCDirectiveKind DirectiveKind, SourceLocation BeginLoc,
SourceLocation LParenLoc, OpenACCReductionOperator ReductionOp,
ArrayRef<Expr *> Vars, SourceLocation EndLoc) {
if (DirectiveKind == OpenACCDirectiveKind::Loop ||
isOpenACCCombinedDirectiveKind(DirectiveKind)) {
// OpenACC 3.3 2.9.11: A reduction clause may not appear on a loop directive
// that has a gang clause with a dim: argument whose value is greater
// than 1.
const auto GangClauses = llvm::make_filter_range(
ExistingClauses, llvm::IsaPred<OpenACCGangClause>);
for (auto *GC : GangClauses) {
const auto *GangClause = cast<OpenACCGangClause>(GC);
for (unsigned I = 0; I < GangClause->getNumExprs(); ++I) {
std::pair<OpenACCGangKind, const Expr *> EPair = GangClause->getExpr(I);
if (EPair.first != OpenACCGangKind::Dim)
continue;
if (const auto *DimVal = dyn_cast<ConstantExpr>(EPair.second);
DimVal && DimVal->getResultAsAPSInt() > 1) {
Diag(BeginLoc, diag::err_acc_gang_reduction_conflict)
<< /*reduction/gang=*/1 << DirectiveKind;
Diag(GangClause->getBeginLoc(), diag::note_acc_previous_clause_here)
<< GangClause->getClauseKind();
return nullptr;
}
}
}
}
auto *Ret = OpenACCReductionClause::Create(
getASTContext(), BeginLoc, LParenLoc, ReductionOp, Vars, EndLoc);
return Ret;
}
llvm::SmallVector<Expr *>
SemaOpenACC::CheckLinkClauseVarList(ArrayRef<Expr *> VarExprs) {
const DeclContext *DC = removeLinkageSpecDC(getCurContext());
// Link has no special restrictions on its var list unless it is not at NS/TU
// scope.
if (isa<NamespaceDecl, TranslationUnitDecl>(DC))
return llvm::SmallVector<Expr *>(VarExprs);
llvm::SmallVector<Expr *> NewVarList;
for (Expr *VarExpr : VarExprs) {
if (isa<DependentScopeDeclRefExpr, CXXDependentScopeMemberExpr>(VarExpr)) {
NewVarList.push_back(VarExpr);
continue;
}
// Field decls can't be global, nor extern, and declare can't refer to
// non-static fields in class-scope, so this always fails the scope check.
// BUT for now we add this so it gets diagnosed by the general 'declare'
// rules.
if (isa<MemberExpr>(VarExpr)) {
NewVarList.push_back(VarExpr);
continue;
}
const auto *DRE = cast<DeclRefExpr>(VarExpr);
const VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl());
if (!Var || !Var->hasExternalStorage())
Diag(VarExpr->getBeginLoc(), diag::err_acc_link_not_extern);
else
NewVarList.push_back(VarExpr);
}
return NewVarList;
}
bool SemaOpenACC::CheckDeclareClause(SemaOpenACC::OpenACCParsedClause &Clause,
OpenACCModifierKind Mods) {
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::Declare)
return false;
const DeclContext *DC = removeLinkageSpecDC(getCurContext());
// Whether this is 'create', 'copyin', 'deviceptr', 'device_resident', or
// 'link', which have 2 special rules.
bool IsSpecialClause =
Clause.getClauseKind() == OpenACCClauseKind::Create ||
Clause.getClauseKind() == OpenACCClauseKind::CopyIn ||
Clause.getClauseKind() == OpenACCClauseKind::DevicePtr ||
Clause.getClauseKind() == OpenACCClauseKind::DeviceResident ||
Clause.getClauseKind() == OpenACCClauseKind::Link;
// OpenACC 3.3 2.13:
// In C or C++ global or namespace scope, only 'create',
// 'copyin', 'deviceptr', 'device_resident', or 'link' clauses are
// allowed.
if (!IsSpecialClause && isa<NamespaceDecl, TranslationUnitDecl>(DC)) {
return Diag(Clause.getBeginLoc(), diag::err_acc_declare_clause_at_global)
<< Clause.getClauseKind();
}
llvm::SmallVector<Expr *> FilteredVarList;
const DeclaratorDecl *CurDecl = nullptr;
for (Expr *VarExpr : Clause.getVarList()) {
if (isa<DependentScopeDeclRefExpr, CXXDependentScopeMemberExpr>(VarExpr)) {
// There isn't really anything we can do here, so we add them anyway and
// we can check them again when we instantiate this.
} else if (const auto *MemExpr = dyn_cast<MemberExpr>(VarExpr)) {
FieldDecl *FD =
cast<FieldDecl>(MemExpr->getMemberDecl()->getCanonicalDecl());
CurDecl = FD;
if (removeLinkageSpecDC(
FD->getLexicalDeclContext()->getPrimaryContext()) != DC) {
Diag(MemExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
<< Clause.getClauseKind();
continue;
}
} else {
const auto *DRE = cast<DeclRefExpr>(VarExpr);
if (const auto *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
CurDecl = Var->getCanonicalDecl();
// OpenACC3.3 2.13:
// A 'declare' directive must be in the same scope as the declaration of
// any var that appears in the clauses of the directive or any scope
// within a C/C++ function.
// We can't really check 'scope' here, so we check declaration context,
// which is a reasonable approximation, but misses scopes inside of
// functions.
if (removeLinkageSpecDC(
Var->getLexicalDeclContext()->getPrimaryContext()) != DC) {
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_same_scope)
<< Clause.getClauseKind();
continue;
}
// OpenACC3.3 2.13:
// C and C++ extern variables may only appear in 'create',
// 'copyin', 'deviceptr', 'device_resident', or 'link' clauses on a
// 'declare' directive.
if (!IsSpecialClause && Var->hasExternalStorage()) {
Diag(VarExpr->getBeginLoc(), diag::err_acc_declare_extern)
<< Clause.getClauseKind();
continue;
}
}
// OpenACC3.3 2.13:
// A var may appear at most once in all the clauses of declare
// directives for a function, subroutine, program, or module.
if (CurDecl) {
auto [Itr, Inserted] = DeclareVarReferences.try_emplace(CurDecl);
if (!Inserted) {
Diag(VarExpr->getBeginLoc(), diag::err_acc_multiple_references)
<< Clause.getClauseKind();
Diag(Itr->second, diag::note_acc_previous_reference);
continue;
} else {
Itr->second = VarExpr->getBeginLoc();
}
}
}
FilteredVarList.push_back(VarExpr);
}
Clause.setVarListDetails(FilteredVarList, Mods);
return false;
}
|