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
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
|
//===--- SemaOpenACC.cpp - Semantic Analysis for OpenACC constructs -------===//
//
// 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 constructs and
/// clauses.
///
//===----------------------------------------------------------------------===//
#include "clang/Sema/SemaOpenACC.h"
#include "clang/AST/StmtOpenACC.h"
#include "clang/Basic/DiagnosticSema.h"
#include "clang/Basic/OpenACCKinds.h"
#include "clang/Sema/Sema.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"
using namespace clang;
namespace {
bool diagnoseConstructAppertainment(SemaOpenACC &S, OpenACCDirectiveKind K,
SourceLocation StartLoc, bool IsStmt) {
switch (K) {
default:
case OpenACCDirectiveKind::Invalid:
// Nothing to do here, both invalid and unimplemented don't really need to
// do anything.
break;
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::HostData:
case OpenACCDirectiveKind::Wait:
if (!IsStmt)
return S.Diag(StartLoc, diag::err_acc_construct_appertainment) << K;
break;
}
return false;
}
bool doesClauseApplyToDirective(OpenACCDirectiveKind DirectiveKind,
OpenACCClauseKind ClauseKind) {
switch (ClauseKind) {
// FIXME: For each clause as we implement them, we can add the
// 'legalization' list here.
case OpenACCClauseKind::Default:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Data:
return true;
default:
return false;
}
case OpenACCClauseKind::If:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::HostData:
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
case OpenACCDirectiveKind::Update:
case OpenACCDirectiveKind::Wait:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Self:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Update:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::NumGangs:
case OpenACCClauseKind::NumWorkers:
case OpenACCClauseKind::VectorLength:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::FirstPrivate:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Private:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::NoCreate:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Present:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::Declare:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Copy:
case OpenACCClauseKind::PCopy:
case OpenACCClauseKind::PresentOrCopy:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::Declare:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::CopyIn:
case OpenACCClauseKind::PCopyIn:
case OpenACCClauseKind::PresentOrCopyIn:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::Declare:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::CopyOut:
case OpenACCClauseKind::PCopyOut:
case OpenACCClauseKind::PresentOrCopyOut:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::Declare:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Create:
case OpenACCClauseKind::PCreate:
case OpenACCClauseKind::PresentOrCreate:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Attach:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::DevicePtr:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::Declare:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Async:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::Set:
case OpenACCDirectiveKind::Update:
case OpenACCDirectiveKind::Wait:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Wait:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::Update:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Seq:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::Routine:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Independent:
case OpenACCClauseKind::Auto:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Reduction:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::DeviceType:
case OpenACCClauseKind::DType:
switch (DirectiveKind) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
case OpenACCDirectiveKind::Update:
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::Routine:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
case OpenACCClauseKind::Collapse: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Tile: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Gang: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Routine:
return true;
default:
return false;
}
case OpenACCClauseKind::Worker: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Routine:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Vector: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Routine:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Finalize: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::ExitData:
return true;
default:
return false;
}
}
case OpenACCClauseKind::IfPresent: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::HostData:
case OpenACCDirectiveKind::Update:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Delete: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::ExitData:
return true;
default:
return false;
}
}
case OpenACCClauseKind::Detach: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::ExitData:
return true;
default:
return false;
}
}
case OpenACCClauseKind::DeviceNum: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
return true;
default:
return false;
}
}
case OpenACCClauseKind::UseDevice: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::HostData:
return true;
default:
return false;
}
}
case OpenACCClauseKind::DefaultAsync: {
switch (DirectiveKind) {
case OpenACCDirectiveKind::Set:
return true;
default:
return false;
}
}
}
default:
// Do nothing so we can go to the 'unimplemented' diagnostic instead.
return true;
}
llvm_unreachable("Invalid clause kind");
}
bool checkAlreadyHasClauseOfKind(
SemaOpenACC &S, ArrayRef<const OpenACCClause *> ExistingClauses,
SemaOpenACC::OpenACCParsedClause &Clause) {
const auto *Itr = llvm::find_if(ExistingClauses, [&](const OpenACCClause *C) {
return C->getClauseKind() == Clause.getClauseKind();
});
if (Itr != ExistingClauses.end()) {
S.Diag(Clause.getBeginLoc(), diag::err_acc_duplicate_clause_disallowed)
<< Clause.getDirectiveKind() << Clause.getClauseKind();
S.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here);
return true;
}
return false;
}
bool checkValidAfterDeviceType(
SemaOpenACC &S, const OpenACCDeviceTypeClause &DeviceTypeClause,
const SemaOpenACC::OpenACCParsedClause &NewClause) {
// This is implemented for everything but 'routine', so treat as 'fine' for
// that.
if (NewClause.getDirectiveKind() == OpenACCDirectiveKind::Routine)
return false;
// 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;
}
}
S.Diag(NewClause.getBeginLoc(), diag::err_acc_clause_after_device_type)
<< NewClause.getClauseKind() << DeviceTypeClause.getClauseKind()
<< NewClause.getDirectiveKind();
S.Diag(DeviceTypeClause.getBeginLoc(), diag::note_acc_previous_clause_here);
return true;
}
// A temporary function that helps implement the 'not implemented' check at the
// top of each clause checking function. This should only be used in conjunction
// with the one being currently implemented/only updated after the entire
// construct has been implemented.
bool isDirectiveKindImplemented(OpenACCDirectiveKind DK) {
return isOpenACCComputeDirectiveKind(DK) ||
isOpenACCCombinedDirectiveKind(DK) || isOpenACCDataDirectiveKind(DK) ||
DK == OpenACCDirectiveKind::Loop || DK == OpenACCDirectiveKind::Wait ||
DK == OpenACCDirectiveKind::Init ||
DK == OpenACCDirectiveKind::Shutdown ||
DK == OpenACCDirectiveKind::Set;
}
class SemaOpenACCClauseVisitor {
SemaOpenACC &SemaRef;
ASTContext &Ctx;
ArrayRef<const OpenACCClause *> ExistingClauses;
bool NotImplemented = false;
OpenACCClause *isNotImplemented() {
NotImplemented = true;
return nullptr;
}
// OpenACC 3.3 2.9:
// A 'gang', 'worker', or 'vector' clause may not appear if a 'seq' clause
// appears.
bool DiagIfSeqClause(SemaOpenACC::OpenACCParsedClause &Clause) {
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);
return true;
}
return false;
}
public:
SemaOpenACCClauseVisitor(SemaOpenACC &S,
ArrayRef<const OpenACCClause *> ExistingClauses)
: SemaRef(S), Ctx(S.getASTContext()), ExistingClauses(ExistingClauses) {}
// Once we've implemented everything, we shouldn't need this infrastructure.
// But in the meantime, we use this to help decide whether the clause was
// handled for this directive.
bool diagNotImplemented() { return NotImplemented; }
OpenACCClause *Visit(SemaOpenACC::OpenACCParsedClause &Clause) {
switch (Clause.getClauseKind()) {
#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"
default:
return isNotImplemented();
}
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;
// OpenACC 3.3, Section 2.5.4:
// At most one 'default' clause may appear, and it must have a value of
// either 'none' or 'present'.
// Second half of the sentence is diagnosed during parsing.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
return OpenACCDefaultClause::Create(
Ctx, Clause.getDefaultClauseKind(), Clause.getBeginLoc(),
Clause.getLParenLoc(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitTileClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Duplicates here are not really sensible. We could possible permit
// multiples if they all had the same value, but there isn't really a good
// reason to do so. Also, this simplifies the suppression of duplicates, in
// that we know if we 'find' one after instantiation, that it is the same
// clause, which simplifies instantiation/checking/etc.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense. Prose DOES exist for 'data' and 'host_data', 'set', 'enter data' and
// 'exit data' both don't, but other implmementations do this. OpenACC issue
// 519 filed for the latter two. Prose also exists for 'update'.
// GCC allows this on init/shutdown, presumably for good reason, so we do too.
if (Clause.getDirectiveKind() != OpenACCDirectiveKind::Init &&
Clause.getDirectiveKind() != OpenACCDirectiveKind::Shutdown &&
checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
// 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);
}
}
return OpenACCIfClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getConditionExpr(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitSelfClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
// 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);
}
return OpenACCSelfClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getConditionExpr(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNumGangsClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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);
SemaRef.Diag((*GangClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here);
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);
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);
return nullptr;
}
}
}
return OpenACCNumGangsClause::Create(
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getIntExprs(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitNumWorkersClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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);
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) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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);
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) {
// There is no prose in the standard that says duplicates aren't allowed,
// but this diagnostic is present in other compilers, as well as makes
// sense.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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) {
// Restrictions only properly implemented on certain constructs, so skip/treat
// as unimplemented in those cases.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// OpenACC 3.3 2.14.3: Two instances of the same clause may not appear on the
// same directive.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Set &&
checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
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) {
// OpenACC 3.3 2.14.3: Two instances of the same clause may not appear on the
// same directive.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
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) {
// Restrictions only properly implemented on 'compute'/'combined'/'data'
// constructs, and 'compute'/'combined'/'data' constructs are the only
// construct that can do anything with this yet, so skip/treat as
// unimplemented in this case.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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 OpenACCPresentClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(),
Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Restrictions only properly implemented on 'compute'/'combined'/'data'
// constructs, and 'compute'/'combined'/'data' constructs are the only
// construct that can do anything with this yet, so skip/treat as
// unimplemented in this case.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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 OpenACCCopyClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyInClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Restrictions only properly implemented on 'compute'/'combined'/'data'
// constructs, and 'compute'/'combined'/'data' constructs are the only
// construct that can do anything with this yet, so skip/treat as
// unimplemented in this case.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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 OpenACCCopyInClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.isReadOnly(), Clause.getVarList(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitCopyOutClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// Restrictions only properly implemented on 'compute'/'combined'/'data'
// constructs, and 'compute'/'combined'/'data' constructs are the only
// construct that can do anything with this yet, so skip/treat as
// unimplemented in this case.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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 OpenACCCopyOutClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.isZero(), 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.
return OpenACCCreateClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.isZero(), 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,
/*IsReadOnly=*/false, /*IsZero=*/false);
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,
/*IsReadOnly=*/false, /*IsZero=*/false);
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) {
// Restrictions only properly implemented on 'compute'/'combined'/'data'
// constructs, and 'compute'/'combined'/'data' constructs are the only
// construct that can do anything with this yet, so skip/treat as
// unimplemented in this case.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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,
/*IsReadOnly=*/false, /*IsZero=*/false);
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) {
// Restrictions implemented properly on everything except 'routine'.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Routine)
return isNotImplemented();
// OpenACC 3.3 2.14.3: Two instances of the same clause may not appear on the
// same directive.
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::Set &&
checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
return nullptr;
// TODO OpenACC: Once we get enough of the CodeGen implemented that we have
// a source for the list of valid architectures, we need to warn on unknown
// identifiers here.
return OpenACCDeviceTypeClause::Create(
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
Clause.getDeviceTypeArchitectures(), Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitAutoClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// OpenACC 3.3 2.9:
// Only one of the seq, independent, and auto clauses may appear.
const auto *Itr =
llvm::find_if(ExistingClauses,
llvm::IsaPred<OpenACCIndependentClause, OpenACCSeqClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_loop_spec_conflict)
<< Clause.getClauseKind() << Clause.getDirectiveKind();
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here);
return nullptr;
}
return OpenACCAutoClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitIndependentClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
// OpenACC 3.3 2.9:
// Only one of the seq, independent, and auto clauses may appear.
const auto *Itr = llvm::find_if(
ExistingClauses, llvm::IsaPred<OpenACCAutoClause, OpenACCSeqClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_loop_spec_conflict)
<< Clause.getClauseKind() << Clause.getDirectiveKind();
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here);
return nullptr;
}
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 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: {
// 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.
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})};
}
}
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);
return ExprError();
}
return ExprResult{E};
}
case OpenACCGangKind::Static:
return CheckGangStaticExpr(S, E);
return ExprError();
}
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");
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitVectorClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagIfSeqClause(Clause))
return nullptr;
// Restrictions only properly implemented on 'loop'/'combined' constructs, and
// it is the only construct that can do anything with this, so skip/treat as
// unimplemented for the routine constructs.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
Expr *IntExpr =
Clause.getNumIntExprs() != 0 ? Clause.getIntExprs()[0] : nullptr;
if (IntExpr) {
if (!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
switch (SemaRef.getActiveComputeConstructInfo().Kind) {
case OpenACCDirectiveKind::Invalid:
case OpenACCDirectiveKind::Parallel:
// No restriction on when 'parallel' can contain an argument.
break;
case OpenACCDirectiveKind::Serial:
// 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: {
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);
IntExpr = nullptr;
}
break;
}
default:
llvm_unreachable("Non compute construct in active compute construct");
}
} else {
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::SerialLoop) {
DiagIntArgInvalid(SemaRef, IntExpr, "length", OpenACCClauseKind::Vector,
Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
} else if (Clause.getDirectiveKind() ==
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);
IntExpr = nullptr;
}
}
}
}
if (!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
// 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);
return nullptr;
}
}
return OpenACCVectorClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), IntExpr,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitWorkerClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagIfSeqClause(Clause))
return nullptr;
// Restrictions only properly implemented on 'loop'/'combined' constructs, and
// it is the only construct that can do anything with this, so skip/treat as
// unimplemented for the routine constructs.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
Expr *IntExpr =
Clause.getNumIntExprs() != 0 ? Clause.getIntExprs()[0] : nullptr;
if (IntExpr) {
if (!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
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);
IntExpr = nullptr;
}
break;
}
default:
llvm_unreachable("Non compute construct in active compute construct");
}
} else {
if (Clause.getDirectiveKind() == OpenACCDirectiveKind::ParallelLoop ||
Clause.getDirectiveKind() == OpenACCDirectiveKind::SerialLoop) {
DiagIntArgInvalid(SemaRef, IntExpr, OpenACCGangKind::Num,
OpenACCClauseKind::Worker, Clause.getDirectiveKind(),
SemaRef.getActiveComputeConstructInfo().Kind);
IntExpr = nullptr;
} else {
assert(Clause.getDirectiveKind() == OpenACCDirectiveKind::KernelsLoop &&
"Unknown combined directive kind?");
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);
IntExpr = nullptr;
}
}
}
}
if (!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
// 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);
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);
return nullptr;
}
}
return OpenACCWorkerClause::Create(Ctx, Clause.getBeginLoc(),
Clause.getLParenLoc(), IntExpr,
Clause.getEndLoc());
}
OpenACCClause *SemaOpenACCClauseVisitor::VisitGangClause(
SemaOpenACC::OpenACCParsedClause &Clause) {
if (DiagIfSeqClause(Clause))
return nullptr;
// Restrictions only properly implemented on 'loop' constructs, and it is
// the only construct that can do anything with this, so skip/treat as
// unimplemented for the combined constructs.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// 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);
SemaRef.Diag((*NumGangsClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here);
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 (!isOpenACCCombinedDirectiveKind(Clause.getDirectiveKind())) {
// 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);
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);
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);
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) {
// Restrictions only properly implemented on 'loop' constructs and combined ,
// and it is the only construct that can do anything with this, so skip/treat
// as unimplemented for the routine constructs.
if (!isDirectiveKindImplemented(Clause.getDirectiveKind()))
return isNotImplemented();
// OpenACC 3.3 2.9:
// Only one of the seq, independent, and auto clauses may appear.
const auto *Itr =
llvm::find_if(ExistingClauses,
llvm::IsaPred<OpenACCAutoClause, OpenACCIndependentClause>);
if (Itr != ExistingClauses.end()) {
SemaRef.Diag(Clause.getBeginLoc(), diag::err_acc_loop_spec_conflict)
<< Clause.getClauseKind() << Clause.getDirectiveKind();
SemaRef.Diag((*Itr)->getBeginLoc(), diag::note_acc_previous_clause_here);
return nullptr;
}
// OpenACC 3.3 2.9:
// A 'gang', 'worker', or 'vector' clause may not appear if a 'seq' clause
// appears.
Itr = llvm::find_if(ExistingClauses,
llvm::IsaPred<OpenACCGangClause, OpenACCWorkerClause,
OpenACCVectorClause>);
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);
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);
SemaRef.Diag((*NumGangsClauseItr)->getBeginLoc(),
diag::note_acc_previous_clause_here);
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);
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) {
// Duplicates here are not really sensible. We could possible permit
// multiples if they all had the same value, but there isn't really a good
// reason to do so. Also, this simplifies the suppression of duplicates, in
// that we know if we 'find' one after instantiation, that it is the same
// clause, which simplifies instantiation/checking/etc.
if (checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, 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());
}
void CollectActiveReductionClauses(
llvm::SmallVector<OpenACCReductionClause *> &ActiveClauses,
ArrayRef<OpenACCClause *> CurClauses) {
for (auto *CurClause : CurClauses) {
if (auto *RedClause = dyn_cast<OpenACCReductionClause>(CurClause);
RedClause && !RedClause->getVarList().empty())
ActiveClauses.push_back(RedClause);
}
}
// Depth needs to be preserved for all associated statements that aren't
// supposed to modify the compute/combined/loop construct information.
bool PreserveLoopRAIIDepthInAssociatedStmtRAII(OpenACCDirectiveKind DK) {
switch (DK) {
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Loop:
return false;
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::HostData:
return true;
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::Wait:
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
case OpenACCDirectiveKind::Update:
llvm_unreachable("Doesn't have an associated stmt");
default:
case OpenACCDirectiveKind::Invalid:
llvm_unreachable("Unhandled directive kind?");
}
llvm_unreachable("Unhandled directive kind?");
}
} // namespace
SemaOpenACC::SemaOpenACC(Sema &S) : SemaBase(S) {}
SemaOpenACC::AssociatedStmtRAII::AssociatedStmtRAII(
SemaOpenACC &S, OpenACCDirectiveKind DK, SourceLocation DirLoc,
ArrayRef<const OpenACCClause *> UnInstClauses,
ArrayRef<OpenACCClause *> Clauses)
: SemaRef(S), OldActiveComputeConstructInfo(S.ActiveComputeConstructInfo),
DirKind(DK), OldLoopGangClauseOnKernel(S.LoopGangClauseOnKernel),
OldLoopWorkerClauseLoc(S.LoopWorkerClauseLoc),
OldLoopVectorClauseLoc(S.LoopVectorClauseLoc),
OldLoopWithoutSeqInfo(S.LoopWithoutSeqInfo),
ActiveReductionClauses(S.ActiveReductionClauses),
LoopRAII(SemaRef, PreserveLoopRAIIDepthInAssociatedStmtRAII(DirKind)) {
// Compute constructs end up taking their 'loop'.
if (DirKind == OpenACCDirectiveKind::Parallel ||
DirKind == OpenACCDirectiveKind::Serial ||
DirKind == OpenACCDirectiveKind::Kernels) {
CollectActiveReductionClauses(S.ActiveReductionClauses, Clauses);
SemaRef.ActiveComputeConstructInfo.Kind = DirKind;
SemaRef.ActiveComputeConstructInfo.Clauses = Clauses;
// 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.
//
// Implement the 'unless within a nested compute region' part.
SemaRef.LoopGangClauseOnKernel = {};
SemaRef.LoopWorkerClauseLoc = {};
SemaRef.LoopVectorClauseLoc = {};
SemaRef.LoopWithoutSeqInfo = {};
} else if (DirKind == OpenACCDirectiveKind::ParallelLoop ||
DirKind == OpenACCDirectiveKind::SerialLoop ||
DirKind == OpenACCDirectiveKind::KernelsLoop) {
SemaRef.ActiveComputeConstructInfo.Kind = DirKind;
SemaRef.ActiveComputeConstructInfo.Clauses = Clauses;
CollectActiveReductionClauses(S.ActiveReductionClauses, Clauses);
SetCollapseInfoBeforeAssociatedStmt(UnInstClauses, Clauses);
SetTileInfoBeforeAssociatedStmt(UnInstClauses, Clauses);
SemaRef.LoopGangClauseOnKernel = {};
SemaRef.LoopWorkerClauseLoc = {};
SemaRef.LoopVectorClauseLoc = {};
// Set the active 'loop' location if there isn't a 'seq' on it, so we can
// diagnose the for loops.
SemaRef.LoopWithoutSeqInfo = {};
if (Clauses.end() ==
llvm::find_if(Clauses, llvm::IsaPred<OpenACCSeqClause>))
SemaRef.LoopWithoutSeqInfo = {DirKind, DirLoc};
// 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.
//
// We don't bother doing this when this is a template instantiation, as
// there is no reason to do these checks: the existance of a
// gang/kernels/etc cannot be dependent.
if (DirKind == OpenACCDirectiveKind::KernelsLoop && UnInstClauses.empty()) {
// This handles the 'outer loop' part of this.
auto *Itr = llvm::find_if(Clauses, llvm::IsaPred<OpenACCGangClause>);
if (Itr != Clauses.end())
SemaRef.LoopGangClauseOnKernel = {(*Itr)->getBeginLoc(), DirKind};
}
if (UnInstClauses.empty()) {
auto *Itr = llvm::find_if(Clauses, llvm::IsaPred<OpenACCWorkerClause>);
if (Itr != Clauses.end())
SemaRef.LoopWorkerClauseLoc = (*Itr)->getBeginLoc();
auto *Itr2 = llvm::find_if(Clauses, llvm::IsaPred<OpenACCVectorClause>);
if (Itr2 != Clauses.end())
SemaRef.LoopVectorClauseLoc = (*Itr2)->getBeginLoc();
}
} else if (DirKind == OpenACCDirectiveKind::Loop) {
CollectActiveReductionClauses(S.ActiveReductionClauses, Clauses);
SetCollapseInfoBeforeAssociatedStmt(UnInstClauses, Clauses);
SetTileInfoBeforeAssociatedStmt(UnInstClauses, Clauses);
// Set the active 'loop' location if there isn't a 'seq' on it, so we can
// diagnose the for loops.
SemaRef.LoopWithoutSeqInfo = {};
if (Clauses.end() ==
llvm::find_if(Clauses, llvm::IsaPred<OpenACCSeqClause>))
SemaRef.LoopWithoutSeqInfo = {DirKind, DirLoc};
// 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.
//
// We don't bother doing this when this is a template instantiation, as
// there is no reason to do these checks: the existance of a
// gang/kernels/etc cannot be dependent.
if (SemaRef.getActiveComputeConstructInfo().Kind ==
OpenACCDirectiveKind::Kernels &&
UnInstClauses.empty()) {
// This handles the 'outer loop' part of this.
auto *Itr = llvm::find_if(Clauses, llvm::IsaPred<OpenACCGangClause>);
if (Itr != Clauses.end())
SemaRef.LoopGangClauseOnKernel = {(*Itr)->getBeginLoc(),
OpenACCDirectiveKind::Kernels};
}
if (UnInstClauses.empty()) {
auto *Itr = llvm::find_if(Clauses, llvm::IsaPred<OpenACCWorkerClause>);
if (Itr != Clauses.end())
SemaRef.LoopWorkerClauseLoc = (*Itr)->getBeginLoc();
auto *Itr2 = llvm::find_if(Clauses, llvm::IsaPred<OpenACCVectorClause>);
if (Itr2 != Clauses.end())
SemaRef.LoopVectorClauseLoc = (*Itr2)->getBeginLoc();
}
}
}
void SemaOpenACC::AssociatedStmtRAII::SetCollapseInfoBeforeAssociatedStmt(
ArrayRef<const OpenACCClause *> UnInstClauses,
ArrayRef<OpenACCClause *> Clauses) {
// Reset this checking for loops that aren't covered in a RAII object.
SemaRef.LoopInfo.CurLevelHasLoopAlready = false;
SemaRef.CollapseInfo.CollapseDepthSatisfied = true;
SemaRef.TileInfo.TileDepthSatisfied = true;
// We make sure to take an optional list of uninstantiated clauses, so that
// we can check to make sure we don't 'double diagnose' in the event that
// the value of 'N' was not dependent in a template. We also ensure during
// Sema that there is only 1 collapse on each construct, so we can count on
// the fact that if both find a 'collapse', that they are the same one.
auto *CollapseClauseItr =
llvm::find_if(Clauses, llvm::IsaPred<OpenACCCollapseClause>);
auto *UnInstCollapseClauseItr =
llvm::find_if(UnInstClauses, llvm::IsaPred<OpenACCCollapseClause>);
if (Clauses.end() == CollapseClauseItr)
return;
OpenACCCollapseClause *CollapseClause =
cast<OpenACCCollapseClause>(*CollapseClauseItr);
SemaRef.CollapseInfo.ActiveCollapse = CollapseClause;
Expr *LoopCount = CollapseClause->getLoopCount();
// If the loop count is still instantiation dependent, setting the depth
// counter isn't necessary, so return here.
if (!LoopCount || LoopCount->isInstantiationDependent())
return;
// Suppress diagnostics if we've done a 'transform' where the previous version
// wasn't dependent, meaning we already diagnosed it.
if (UnInstCollapseClauseItr != UnInstClauses.end() &&
!cast<OpenACCCollapseClause>(*UnInstCollapseClauseItr)
->getLoopCount()
->isInstantiationDependent())
return;
SemaRef.CollapseInfo.CollapseDepthSatisfied = false;
SemaRef.CollapseInfo.CurCollapseCount =
cast<ConstantExpr>(LoopCount)->getResultAsAPSInt();
SemaRef.CollapseInfo.DirectiveKind = DirKind;
}
void SemaOpenACC::AssociatedStmtRAII::SetTileInfoBeforeAssociatedStmt(
ArrayRef<const OpenACCClause *> UnInstClauses,
ArrayRef<OpenACCClause *> Clauses) {
// We don't diagnose if this is during instantiation, since the only thing we
// care about is the number of arguments, which we can figure out without
// instantiation, so we don't want to double-diagnose.
if (UnInstClauses.size() > 0)
return;
auto *TileClauseItr =
llvm::find_if(Clauses, llvm::IsaPred<OpenACCTileClause>);
if (Clauses.end() == TileClauseItr)
return;
OpenACCTileClause *TileClause = cast<OpenACCTileClause>(*TileClauseItr);
SemaRef.TileInfo.ActiveTile = TileClause;
SemaRef.TileInfo.TileDepthSatisfied = false;
SemaRef.TileInfo.CurTileCount = TileClause->getSizeExprs().size();
SemaRef.TileInfo.DirectiveKind = DirKind;
}
SemaOpenACC::AssociatedStmtRAII::~AssociatedStmtRAII() {
if (DirKind == OpenACCDirectiveKind::Parallel ||
DirKind == OpenACCDirectiveKind::Serial ||
DirKind == OpenACCDirectiveKind::Kernels ||
DirKind == OpenACCDirectiveKind::Loop ||
DirKind == OpenACCDirectiveKind::ParallelLoop ||
DirKind == OpenACCDirectiveKind::SerialLoop ||
DirKind == OpenACCDirectiveKind::KernelsLoop) {
SemaRef.ActiveComputeConstructInfo = OldActiveComputeConstructInfo;
SemaRef.LoopGangClauseOnKernel = OldLoopGangClauseOnKernel;
SemaRef.LoopWorkerClauseLoc = OldLoopWorkerClauseLoc;
SemaRef.LoopVectorClauseLoc = OldLoopVectorClauseLoc;
SemaRef.LoopWithoutSeqInfo = OldLoopWithoutSeqInfo;
SemaRef.ActiveReductionClauses.swap(ActiveReductionClauses);
} else if (DirKind == OpenACCDirectiveKind::Data ||
DirKind == OpenACCDirectiveKind::HostData) {
// Intentionally doesn't reset the Loop, Compute Construct, or reduction
// effects.
}
}
OpenACCClause *
SemaOpenACC::ActOnClause(ArrayRef<const OpenACCClause *> ExistingClauses,
OpenACCParsedClause &Clause) {
if (Clause.getClauseKind() == OpenACCClauseKind::Invalid)
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,
[&](const OpenACCClause *C) {
return isa<OpenACCDeviceTypeClause>(C);
});
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?");
if (Visitor.diagNotImplemented())
Diag(Clause.getBeginLoc(), diag::warn_acc_clause_unimplemented)
<< Clause.getClauseKind();
return Result;
}
namespace {
// 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
/// 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);
return ExprError();
}
}
}
}
return VarExpr;
}
void SemaOpenACC::ActOnConstruct(OpenACCDirectiveKind K,
SourceLocation DirLoc) {
// Start an evaluation context to parse the clause arguments on.
SemaRef.PushExpressionEvaluationContext(
Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
switch (K) {
case OpenACCDirectiveKind::Invalid:
// Nothing to do here, an invalid kind has nothing we can check here. We
// want to continue parsing clauses as far as we can, so we will just
// ensure that we can still work and don't check any construct-specific
// rules anywhere.
break;
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::HostData:
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
case OpenACCDirectiveKind::Update:
// Nothing to do here, there is no real legalization that needs to happen
// here as these constructs do not take any arguments.
break;
case OpenACCDirectiveKind::Wait:
// Nothing really to do here, the arguments to the 'wait' should have
// already been handled by the time we get here.
break;
default:
Diag(DirLoc, diag::warn_acc_construct_unimplemented) << K;
break;
}
}
ExprResult SemaOpenACC::ActOnIntExpr(OpenACCDirectiveKind DK,
OpenACCClauseKind CK, SourceLocation Loc,
Expr *IntExpr) {
assert(((DK != OpenACCDirectiveKind::Invalid &&
CK == OpenACCClauseKind::Invalid) ||
(DK == OpenACCDirectiveKind::Invalid &&
CK != OpenACCClauseKind::Invalid) ||
(DK == OpenACCDirectiveKind::Invalid &&
CK == OpenACCClauseKind::Invalid)) &&
"Only one of directive or clause kind should be provided");
class IntExprConverter : public Sema::ICEConvertDiagnoser {
OpenACCDirectiveKind DirectiveKind;
OpenACCClauseKind ClauseKind;
Expr *IntExpr;
// gets the index into the diagnostics so we can use this for clauses,
// directives, and sub array.s
unsigned getDiagKind() const {
if (ClauseKind != OpenACCClauseKind::Invalid)
return 0;
if (DirectiveKind != OpenACCDirectiveKind::Invalid)
return 1;
return 2;
}
public:
IntExprConverter(OpenACCDirectiveKind DK, OpenACCClauseKind CK,
Expr *IntExpr)
: ICEConvertDiagnoser(/*AllowScopedEnumerations=*/false,
/*Suppress=*/false,
/*SuppressConversion=*/true),
DirectiveKind(DK), ClauseKind(CK), IntExpr(IntExpr) {}
bool match(QualType T) override {
// OpenACC spec just calls this 'integer expression' as having an
// 'integer type', so fall back on C99's 'integer type'.
return T->isIntegerType();
}
SemaBase::SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
QualType T) override {
return S.Diag(Loc, diag::err_acc_int_expr_requires_integer)
<< getDiagKind() << ClauseKind << DirectiveKind << T;
}
SemaBase::SemaDiagnosticBuilder
diagnoseIncomplete(Sema &S, SourceLocation Loc, QualType T) override {
return S.Diag(Loc, diag::err_acc_int_expr_incomplete_class_type)
<< T << IntExpr->getSourceRange();
}
SemaBase::SemaDiagnosticBuilder
diagnoseExplicitConv(Sema &S, SourceLocation Loc, QualType T,
QualType ConvTy) override {
return S.Diag(Loc, diag::err_acc_int_expr_explicit_conversion)
<< T << ConvTy;
}
SemaBase::SemaDiagnosticBuilder noteExplicitConv(Sema &S,
CXXConversionDecl *Conv,
QualType ConvTy) override {
return S.Diag(Conv->getLocation(), diag::note_acc_int_expr_conversion)
<< ConvTy->isEnumeralType() << ConvTy;
}
SemaBase::SemaDiagnosticBuilder
diagnoseAmbiguous(Sema &S, SourceLocation Loc, QualType T) override {
return S.Diag(Loc, diag::err_acc_int_expr_multiple_conversions) << T;
}
SemaBase::SemaDiagnosticBuilder
noteAmbiguous(Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
return S.Diag(Conv->getLocation(), diag::note_acc_int_expr_conversion)
<< ConvTy->isEnumeralType() << ConvTy;
}
SemaBase::SemaDiagnosticBuilder
diagnoseConversion(Sema &S, SourceLocation Loc, QualType T,
QualType ConvTy) override {
llvm_unreachable("conversion functions are permitted");
}
} IntExprDiagnoser(DK, CK, IntExpr);
if (!IntExpr)
return ExprError();
ExprResult IntExprResult = SemaRef.PerformContextualImplicitConversion(
Loc, IntExpr, IntExprDiagnoser);
if (IntExprResult.isInvalid())
return ExprError();
IntExpr = IntExprResult.get();
if (!IntExpr->isTypeDependent() && !IntExpr->getType()->isIntegerType())
return ExprError();
// TODO OpenACC: Do we want to perform usual unary conversions here? When
// doing codegen we might find that is necessary, but skip it for now.
return IntExpr;
}
bool SemaOpenACC::CheckVarIsPointerType(OpenACCClauseKind ClauseKind,
Expr *VarExpr) {
// We already know that VarExpr is a proper reference to a variable, so we
// should be able to just take the type of the expression to get the type of
// the referenced variable.
// We've already seen an error, don't diagnose anything else.
if (!VarExpr || VarExpr->containsErrors())
return false;
if (isa<ArraySectionExpr>(VarExpr->IgnoreParenImpCasts()) ||
VarExpr->hasPlaceholderType(BuiltinType::ArraySection)) {
Diag(VarExpr->getExprLoc(), diag::err_array_section_use) << /*OpenACC=*/0;
Diag(VarExpr->getExprLoc(), diag::note_acc_expected_pointer_var);
return true;
}
QualType Ty = VarExpr->getType();
Ty = Ty.getNonReferenceType().getUnqualifiedType();
// Nothing we can do if this is a dependent type.
if (Ty->isDependentType())
return false;
if (!Ty->isPointerType())
return Diag(VarExpr->getExprLoc(), diag::err_acc_var_not_pointer_type)
<< ClauseKind << Ty;
return false;
}
ExprResult SemaOpenACC::ActOnVar(OpenACCClauseKind CK, Expr *VarExpr) {
Expr *CurVarExpr = VarExpr->IgnoreParenImpCasts();
// 'use_device' doesn't allow array subscript or array sections.
// OpenACC3.3 2.8:
// A 'var' in a 'use_device' clause must be the name of a variable or array.
if (CK == OpenACCClauseKind::UseDevice &&
isa<ArraySectionExpr, ArraySubscriptExpr>(CurVarExpr)) {
Diag(VarExpr->getExprLoc(), diag::err_acc_not_a_var_ref_use_device);
return ExprError();
}
// Sub-arrays/subscript-exprs are fine as long as the base is a
// VarExpr/MemberExpr. So strip all of those off.
while (isa<ArraySectionExpr, ArraySubscriptExpr>(CurVarExpr)) {
if (auto *SubScrpt = dyn_cast<ArraySubscriptExpr>(CurVarExpr))
CurVarExpr = SubScrpt->getBase()->IgnoreParenImpCasts();
else
CurVarExpr =
cast<ArraySectionExpr>(CurVarExpr)->getBase()->IgnoreParenImpCasts();
}
// References to a VarDecl are fine.
if (const auto *DRE = dyn_cast<DeclRefExpr>(CurVarExpr)) {
if (isa<VarDecl, NonTypeTemplateParmDecl>(
DRE->getFoundDecl()->getCanonicalDecl()))
return VarExpr;
}
// If CK is a Reduction, this special cases for OpenACC3.3 2.5.15: "A var in a
// reduction clause must be a scalar variable name, an aggregate variable
// name, an array element, or a subarray.
// If CK is a 'use_device', this also isn't valid, as it isn' the name of a
// variable or array.
// A MemberExpr that references a Field is valid for other clauses.
if (CK != OpenACCClauseKind::Reduction &&
CK != OpenACCClauseKind::UseDevice) {
if (const auto *ME = dyn_cast<MemberExpr>(CurVarExpr)) {
if (isa<FieldDecl>(ME->getMemberDecl()->getCanonicalDecl()))
return VarExpr;
}
}
// Referring to 'this' is ok for the most part, but for 'use_device' doesn't
// fall into 'variable or array name'
if (CK != OpenACCClauseKind::UseDevice && isa<CXXThisExpr>(CurVarExpr))
return VarExpr;
// Nothing really we can do here, as these are dependent. So just return they
// are valid.
if (isa<DependentScopeDeclRefExpr>(CurVarExpr) ||
(CK != OpenACCClauseKind::Reduction &&
isa<CXXDependentScopeMemberExpr>(CurVarExpr)))
return VarExpr;
// There isn't really anything we can do in the case of a recovery expr, so
// skip the diagnostic rather than produce a confusing diagnostic.
if (isa<RecoveryExpr>(CurVarExpr))
return ExprError();
if (CK == OpenACCClauseKind::UseDevice)
Diag(VarExpr->getExprLoc(), diag::err_acc_not_a_var_ref_use_device);
else
Diag(VarExpr->getExprLoc(), diag::err_acc_not_a_var_ref)
<< (CK != OpenACCClauseKind::Reduction);
return ExprError();
}
ExprResult SemaOpenACC::ActOnArraySectionExpr(Expr *Base, SourceLocation LBLoc,
Expr *LowerBound,
SourceLocation ColonLoc,
Expr *Length,
SourceLocation RBLoc) {
ASTContext &Context = getASTContext();
// Handle placeholders.
if (Base->hasPlaceholderType() &&
!Base->hasPlaceholderType(BuiltinType::ArraySection)) {
ExprResult Result = SemaRef.CheckPlaceholderExpr(Base);
if (Result.isInvalid())
return ExprError();
Base = Result.get();
}
if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) {
ExprResult Result = SemaRef.CheckPlaceholderExpr(LowerBound);
if (Result.isInvalid())
return ExprError();
Result = SemaRef.DefaultLvalueConversion(Result.get());
if (Result.isInvalid())
return ExprError();
LowerBound = Result.get();
}
if (Length && Length->getType()->isNonOverloadPlaceholderType()) {
ExprResult Result = SemaRef.CheckPlaceholderExpr(Length);
if (Result.isInvalid())
return ExprError();
Result = SemaRef.DefaultLvalueConversion(Result.get());
if (Result.isInvalid())
return ExprError();
Length = Result.get();
}
// Check the 'base' value, it must be an array or pointer type, and not to/of
// a function type.
QualType OriginalBaseTy = ArraySectionExpr::getBaseOriginalType(Base);
QualType ResultTy;
if (!Base->isTypeDependent()) {
if (OriginalBaseTy->isAnyPointerType()) {
ResultTy = OriginalBaseTy->getPointeeType();
} else if (OriginalBaseTy->isArrayType()) {
ResultTy = OriginalBaseTy->getAsArrayTypeUnsafe()->getElementType();
} else {
return ExprError(
Diag(Base->getExprLoc(), diag::err_acc_typecheck_subarray_value)
<< Base->getSourceRange());
}
if (ResultTy->isFunctionType()) {
Diag(Base->getExprLoc(), diag::err_acc_subarray_function_type)
<< ResultTy << Base->getSourceRange();
return ExprError();
}
if (SemaRef.RequireCompleteType(Base->getExprLoc(), ResultTy,
diag::err_acc_subarray_incomplete_type,
Base))
return ExprError();
if (!Base->hasPlaceholderType(BuiltinType::ArraySection)) {
ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(Base);
if (Result.isInvalid())
return ExprError();
Base = Result.get();
}
}
auto GetRecovery = [&](Expr *E, QualType Ty) {
ExprResult Recovery =
SemaRef.CreateRecoveryExpr(E->getBeginLoc(), E->getEndLoc(), E, Ty);
return Recovery.isUsable() ? Recovery.get() : nullptr;
};
// Ensure both of the expressions are int-exprs.
if (LowerBound && !LowerBound->isTypeDependent()) {
ExprResult LBRes =
ActOnIntExpr(OpenACCDirectiveKind::Invalid, OpenACCClauseKind::Invalid,
LowerBound->getExprLoc(), LowerBound);
if (LBRes.isUsable())
LBRes = SemaRef.DefaultLvalueConversion(LBRes.get());
LowerBound =
LBRes.isUsable() ? LBRes.get() : GetRecovery(LowerBound, Context.IntTy);
}
if (Length && !Length->isTypeDependent()) {
ExprResult LenRes =
ActOnIntExpr(OpenACCDirectiveKind::Invalid, OpenACCClauseKind::Invalid,
Length->getExprLoc(), Length);
if (LenRes.isUsable())
LenRes = SemaRef.DefaultLvalueConversion(LenRes.get());
Length =
LenRes.isUsable() ? LenRes.get() : GetRecovery(Length, Context.IntTy);
}
// Length is required if the base type is not an array of known bounds.
if (!Length && (OriginalBaseTy.isNull() ||
(!OriginalBaseTy->isDependentType() &&
!OriginalBaseTy->isConstantArrayType() &&
!OriginalBaseTy->isDependentSizedArrayType()))) {
bool IsArray = !OriginalBaseTy.isNull() && OriginalBaseTy->isArrayType();
Diag(ColonLoc, diag::err_acc_subarray_no_length) << IsArray;
// Fill in a dummy 'length' so that when we instantiate this we don't
// double-diagnose here.
ExprResult Recovery = SemaRef.CreateRecoveryExpr(
ColonLoc, SourceLocation(), ArrayRef<Expr *>(), Context.IntTy);
Length = Recovery.isUsable() ? Recovery.get() : nullptr;
}
// Check the values of each of the arguments, they cannot be negative(we
// assume), and if the array bound is known, must be within range. As we do
// so, do our best to continue with evaluation, we can set the
// value/expression to nullptr/nullopt if they are invalid, and treat them as
// not present for the rest of evaluation.
// We don't have to check for dependence, because the dependent size is
// represented as a different AST node.
std::optional<llvm::APSInt> BaseSize;
if (!OriginalBaseTy.isNull() && OriginalBaseTy->isConstantArrayType()) {
const auto *ArrayTy = Context.getAsConstantArrayType(OriginalBaseTy);
BaseSize = ArrayTy->getSize();
}
auto GetBoundValue = [&](Expr *E) -> std::optional<llvm::APSInt> {
if (!E || E->isInstantiationDependent())
return std::nullopt;
Expr::EvalResult Res;
if (!E->EvaluateAsInt(Res, Context))
return std::nullopt;
return Res.Val.getInt();
};
std::optional<llvm::APSInt> LowerBoundValue = GetBoundValue(LowerBound);
std::optional<llvm::APSInt> LengthValue = GetBoundValue(Length);
// Check lower bound for negative or out of range.
if (LowerBoundValue.has_value()) {
if (LowerBoundValue->isNegative()) {
Diag(LowerBound->getExprLoc(), diag::err_acc_subarray_negative)
<< /*LowerBound=*/0 << toString(*LowerBoundValue, /*Radix=*/10);
LowerBoundValue.reset();
LowerBound = GetRecovery(LowerBound, LowerBound->getType());
} else if (BaseSize.has_value() &&
llvm::APSInt::compareValues(*LowerBoundValue, *BaseSize) >= 0) {
// Lower bound (start index) must be less than the size of the array.
Diag(LowerBound->getExprLoc(), diag::err_acc_subarray_out_of_range)
<< /*LowerBound=*/0 << toString(*LowerBoundValue, /*Radix=*/10)
<< toString(*BaseSize, /*Radix=*/10);
LowerBoundValue.reset();
LowerBound = GetRecovery(LowerBound, LowerBound->getType());
}
}
// Check length for negative or out of range.
if (LengthValue.has_value()) {
if (LengthValue->isNegative()) {
Diag(Length->getExprLoc(), diag::err_acc_subarray_negative)
<< /*Length=*/1 << toString(*LengthValue, /*Radix=*/10);
LengthValue.reset();
Length = GetRecovery(Length, Length->getType());
} else if (BaseSize.has_value() &&
llvm::APSInt::compareValues(*LengthValue, *BaseSize) > 0) {
// Length must be lessthan or EQUAL to the size of the array.
Diag(Length->getExprLoc(), diag::err_acc_subarray_out_of_range)
<< /*Length=*/1 << toString(*LengthValue, /*Radix=*/10)
<< toString(*BaseSize, /*Radix=*/10);
LengthValue.reset();
Length = GetRecovery(Length, Length->getType());
}
}
// Adding two APSInts requires matching sign, so extract that here.
auto AddAPSInt = [](llvm::APSInt LHS, llvm::APSInt RHS) -> llvm::APSInt {
if (LHS.isSigned() == RHS.isSigned())
return LHS + RHS;
unsigned Width = std::max(LHS.getBitWidth(), RHS.getBitWidth()) + 1;
return llvm::APSInt(LHS.sext(Width) + RHS.sext(Width), /*Signed=*/true);
};
// If we know all 3 values, we can diagnose that the total value would be out
// of range.
if (BaseSize.has_value() && LowerBoundValue.has_value() &&
LengthValue.has_value() &&
llvm::APSInt::compareValues(AddAPSInt(*LowerBoundValue, *LengthValue),
*BaseSize) > 0) {
Diag(Base->getExprLoc(),
diag::err_acc_subarray_base_plus_length_out_of_range)
<< toString(*LowerBoundValue, /*Radix=*/10)
<< toString(*LengthValue, /*Radix=*/10)
<< toString(*BaseSize, /*Radix=*/10);
LowerBoundValue.reset();
LowerBound = GetRecovery(LowerBound, LowerBound->getType());
LengthValue.reset();
Length = GetRecovery(Length, Length->getType());
}
// If any part of the expression is dependent, return a dependent sub-array.
QualType ArrayExprTy = Context.ArraySectionTy;
if (Base->isTypeDependent() ||
(LowerBound && LowerBound->isInstantiationDependent()) ||
(Length && Length->isInstantiationDependent()))
ArrayExprTy = Context.DependentTy;
return new (Context)
ArraySectionExpr(Base, LowerBound, Length, ArrayExprTy, VK_LValue,
OK_Ordinary, ColonLoc, RBLoc);
}
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::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?");
}
default:
// TODO: OpenACC: when we implement this on 'routine', we'll have to
// implement its checking here.
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) {
// 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);
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);
return nullptr;
}
}
}
}
auto *Ret = OpenACCReductionClause::Create(
getASTContext(), BeginLoc, LParenLoc, ReductionOp, Vars, EndLoc);
return Ret;
}
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})};
}
void SemaOpenACC::ActOnWhileStmt(SourceLocation WhileLoc) {
if (!getLangOpts().OpenACC)
return;
if (!LoopInfo.TopLevelLoopSeen)
return;
if (CollapseInfo.CurCollapseCount && *CollapseInfo.CurCollapseCount > 0) {
Diag(WhileLoc, diag::err_acc_invalid_in_loop)
<< /*while loop*/ 1 << CollapseInfo.DirectiveKind
<< OpenACCClauseKind::Collapse;
assert(CollapseInfo.ActiveCollapse && "Collapse count without object?");
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
// Remove the value so that we don't get cascading errors in the body. The
// caller RAII object will restore this.
CollapseInfo.CurCollapseCount = std::nullopt;
}
if (TileInfo.CurTileCount && *TileInfo.CurTileCount > 0) {
Diag(WhileLoc, diag::err_acc_invalid_in_loop)
<< /*while loop*/ 1 << TileInfo.DirectiveKind
<< OpenACCClauseKind::Tile;
assert(TileInfo.ActiveTile && "tile count without object?");
Diag(TileInfo.ActiveTile->getBeginLoc(), diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
// Remove the value so that we don't get cascading errors in the body. The
// caller RAII object will restore this.
TileInfo.CurTileCount = std::nullopt;
}
}
void SemaOpenACC::ActOnDoStmt(SourceLocation DoLoc) {
if (!getLangOpts().OpenACC)
return;
if (!LoopInfo.TopLevelLoopSeen)
return;
if (CollapseInfo.CurCollapseCount && *CollapseInfo.CurCollapseCount > 0) {
Diag(DoLoc, diag::err_acc_invalid_in_loop)
<< /*do loop*/ 2 << CollapseInfo.DirectiveKind
<< OpenACCClauseKind::Collapse;
assert(CollapseInfo.ActiveCollapse && "Collapse count without object?");
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
// Remove the value so that we don't get cascading errors in the body. The
// caller RAII object will restore this.
CollapseInfo.CurCollapseCount = std::nullopt;
}
if (TileInfo.CurTileCount && *TileInfo.CurTileCount > 0) {
Diag(DoLoc, diag::err_acc_invalid_in_loop)
<< /*do loop*/ 2 << TileInfo.DirectiveKind << OpenACCClauseKind::Tile;
assert(TileInfo.ActiveTile && "tile count without object?");
Diag(TileInfo.ActiveTile->getBeginLoc(), diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
// Remove the value so that we don't get cascading errors in the body. The
// caller RAII object will restore this.
TileInfo.CurTileCount = std::nullopt;
}
}
void SemaOpenACC::ForStmtBeginHelper(SourceLocation ForLoc,
ForStmtBeginChecker &C) {
assert(getLangOpts().OpenACC && "Check enabled when not OpenACC?");
// Enable the while/do-while checking.
LoopInfo.TopLevelLoopSeen = true;
if (CollapseInfo.CurCollapseCount && *CollapseInfo.CurCollapseCount > 0) {
C.check();
// OpenACC 3.3 2.9.1:
// Each associated loop, except the innermost, must contain exactly one loop
// or loop nest.
// This checks for more than 1 loop at the current level, the
// 'depth'-satisifed checking manages the 'not zero' case.
if (LoopInfo.CurLevelHasLoopAlready) {
Diag(ForLoc, diag::err_acc_clause_multiple_loops)
<< CollapseInfo.DirectiveKind << OpenACCClauseKind::Collapse;
assert(CollapseInfo.ActiveCollapse && "No collapse object?");
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
} else {
--(*CollapseInfo.CurCollapseCount);
// Once we've hit zero here, we know we have deep enough 'for' loops to
// get to the bottom.
if (*CollapseInfo.CurCollapseCount == 0)
CollapseInfo.CollapseDepthSatisfied = true;
}
}
if (TileInfo.CurTileCount && *TileInfo.CurTileCount > 0) {
C.check();
if (LoopInfo.CurLevelHasLoopAlready) {
Diag(ForLoc, diag::err_acc_clause_multiple_loops)
<< TileInfo.DirectiveKind << OpenACCClauseKind::Tile;
assert(TileInfo.ActiveTile && "No tile object?");
Diag(TileInfo.ActiveTile->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
} else {
--(*TileInfo.CurTileCount);
// Once we've hit zero here, we know we have deep enough 'for' loops to
// get to the bottom.
if (*TileInfo.CurTileCount == 0)
TileInfo.TileDepthSatisfied = true;
}
}
// Set this to 'false' for the body of this loop, so that the next level
// checks independently.
LoopInfo.CurLevelHasLoopAlready = false;
}
namespace {
bool isValidLoopVariableType(QualType LoopVarTy) {
// Just skip if it is dependent, it could be any of the below.
if (LoopVarTy->isDependentType())
return true;
// The loop variable must be of integer,
if (LoopVarTy->isIntegerType())
return true;
// C/C++ pointer,
if (LoopVarTy->isPointerType())
return true;
// or C++ random-access iterator type.
if (const auto *RD = LoopVarTy->getAsCXXRecordDecl()) {
// Note: Only do CXXRecordDecl because RecordDecl can't be a random access
// iterator type!
// We could either do a lot of work to see if this matches
// random-access-iterator, but it seems that just checking that the
// 'iterator_category' typedef is more than sufficient. If programmers are
// willing to lie about this, we can let them.
for (const auto *TD :
llvm::make_filter_range(RD->decls(), llvm::IsaPred<TypedefNameDecl>)) {
const auto *TDND = cast<TypedefNameDecl>(TD)->getCanonicalDecl();
if (TDND->getName() != "iterator_category")
continue;
// If there is no type for this decl, return false.
if (TDND->getUnderlyingType().isNull())
return false;
const CXXRecordDecl *ItrCategoryDecl =
TDND->getUnderlyingType()->getAsCXXRecordDecl();
// If the category isn't a record decl, it isn't the tag type.
if (!ItrCategoryDecl)
return false;
auto IsRandomAccessIteratorTag = [](const CXXRecordDecl *RD) {
if (RD->getName() != "random_access_iterator_tag")
return false;
// Checks just for std::random_access_iterator_tag.
return RD->getEnclosingNamespaceContext()->isStdNamespace();
};
if (IsRandomAccessIteratorTag(ItrCategoryDecl))
return true;
// We can also support types inherited from the
// random_access_iterator_tag.
for (CXXBaseSpecifier BS : ItrCategoryDecl->bases()) {
if (IsRandomAccessIteratorTag(BS.getType()->getAsCXXRecordDecl()))
return true;
}
return false;
}
}
return false;
}
} // namespace
void SemaOpenACC::ForStmtBeginChecker::check() {
if (SemaRef.LoopWithoutSeqInfo.Kind == OpenACCDirectiveKind::Invalid)
return;
if (AlreadyChecked)
return;
AlreadyChecked = true;
// OpenACC3.3 2.1:
// A loop associated with a loop construct that does not have a seq clause
// must be written to meet all the following conditions:
// - The loop variable must be of integer, C/C++ pointer, or C++ random-access
// iterator type.
// - The loop variable must monotonically increase or decrease in the
// direction of its termination condition.
// - The loop trip count must be computable in constant time when entering the
// loop construct.
//
// For a C++ range-based for loop, the loop variable
// identified by the above conditions is the internal iterator, such as a
// pointer, that the compiler generates to iterate the range. it is not the
// variable declared by the for loop.
if (IsRangeFor) {
// If the range-for is being instantiated and didn't change, don't
// re-diagnose.
if (!RangeFor.has_value())
return;
// For a range-for, we can assume everything is 'corect' other than the type
// of the iterator, so check that.
const DeclStmt *RangeStmt = (*RangeFor)->getBeginStmt();
// In some dependent contexts, the autogenerated range statement doesn't get
// included until instantiation, so skip for now.
if (!RangeStmt)
return;
const ValueDecl *InitVar = cast<ValueDecl>(RangeStmt->getSingleDecl());
QualType VarType = InitVar->getType().getNonReferenceType();
if (!isValidLoopVariableType(VarType)) {
SemaRef.Diag(InitVar->getBeginLoc(), diag::err_acc_loop_variable_type)
<< SemaRef.LoopWithoutSeqInfo.Kind << VarType;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc,
diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
}
return;
}
// Else we are in normal 'ForStmt', so we can diagnose everything.
// We only have to check cond/inc if they have changed, but 'init' needs to
// just suppress its diagnostics if it hasn't changed.
const ValueDecl *InitVar = checkInit();
if (Cond.has_value())
checkCond();
if (Inc.has_value())
checkInc(InitVar);
}
const ValueDecl *SemaOpenACC::ForStmtBeginChecker::checkInit() {
if (!Init) {
if (InitChanged) {
SemaRef.Diag(ForLoc, diag::err_acc_loop_variable)
<< SemaRef.LoopWithoutSeqInfo.Kind;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc,
diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
}
return nullptr;
}
auto DiagLoopVar = [&]() {
if (InitChanged) {
SemaRef.Diag(Init->getBeginLoc(), diag::err_acc_loop_variable)
<< SemaRef.LoopWithoutSeqInfo.Kind;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc,
diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
}
return nullptr;
};
if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
Init = ExprTemp->getSubExpr();
if (const auto *E = dyn_cast<Expr>(Init))
Init = E->IgnoreParenImpCasts();
const ValueDecl *InitVar = nullptr;
if (const auto *BO = dyn_cast<BinaryOperator>(Init)) {
// Allow assignment operator here.
if (!BO->isAssignmentOp())
return DiagLoopVar();
const Expr *LHS = BO->getLHS()->IgnoreParenImpCasts();
if (const auto *DRE = dyn_cast<DeclRefExpr>(LHS))
InitVar = DRE->getDecl();
} else if (const auto *DS = dyn_cast<DeclStmt>(Init)) {
// Allow T t = <whatever>
if (!DS->isSingleDecl())
return DiagLoopVar();
InitVar = dyn_cast<ValueDecl>(DS->getSingleDecl());
// Ensure we have an initializer, unless this is a record/dependent type.
if (InitVar) {
if (!isa<VarDecl>(InitVar))
return DiagLoopVar();
if (!InitVar->getType()->isRecordType() &&
!InitVar->getType()->isDependentType() &&
!cast<VarDecl>(InitVar)->hasInit())
return DiagLoopVar();
}
} else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Init)) {
// Allow assignment operator call.
if (CE->getOperator() != OO_Equal)
return DiagLoopVar();
const Expr *LHS = CE->getArg(0)->IgnoreParenImpCasts();
if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
InitVar = DRE->getDecl();
} else if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
InitVar = ME->getMemberDecl();
}
}
if (!InitVar)
return DiagLoopVar();
InitVar = cast<ValueDecl>(InitVar->getCanonicalDecl());
QualType VarType = InitVar->getType().getNonReferenceType();
// Since we have one, all we need to do is ensure it is the right type.
if (!isValidLoopVariableType(VarType)) {
if (InitChanged) {
SemaRef.Diag(InitVar->getBeginLoc(), diag::err_acc_loop_variable_type)
<< SemaRef.LoopWithoutSeqInfo.Kind << VarType;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc,
diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
}
return nullptr;
}
return InitVar;
}
void SemaOpenACC::ForStmtBeginChecker::checkCond() {
if (!*Cond) {
SemaRef.Diag(ForLoc, diag::err_acc_loop_terminating_condition)
<< SemaRef.LoopWithoutSeqInfo.Kind;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc, diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
}
// Nothing else to do here. we could probably do some additional work to look
// into the termination condition, but that error-prone. For now, we don't
// implement anything other than 'there is a termination condition', and if
// codegen/MLIR comes up with some necessary restrictions, we can implement
// them here.
}
void SemaOpenACC::ForStmtBeginChecker::checkInc(const ValueDecl *Init) {
if (!*Inc) {
SemaRef.Diag(ForLoc, diag::err_acc_loop_not_monotonic)
<< SemaRef.LoopWithoutSeqInfo.Kind;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc, diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
return;
}
auto DiagIncVar = [this] {
SemaRef.Diag((*Inc)->getBeginLoc(), diag::err_acc_loop_not_monotonic)
<< SemaRef.LoopWithoutSeqInfo.Kind;
SemaRef.Diag(SemaRef.LoopWithoutSeqInfo.Loc, diag::note_acc_construct_here)
<< SemaRef.LoopWithoutSeqInfo.Kind;
return;
};
if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(*Inc))
Inc = ExprTemp->getSubExpr();
if (const auto *E = dyn_cast<Expr>(*Inc))
Inc = E->IgnoreParenImpCasts();
auto getDeclFromExpr = [](const Expr *E) -> const ValueDecl * {
E = E->IgnoreParenImpCasts();
if (const auto *FE = dyn_cast<FullExpr>(E))
E = FE->getSubExpr();
E = E->IgnoreParenImpCasts();
if (!E)
return nullptr;
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
return dyn_cast<ValueDecl>(DRE->getDecl());
if (const auto *ME = dyn_cast<MemberExpr>(E))
if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
return ME->getMemberDecl();
return nullptr;
};
const ValueDecl *IncVar = nullptr;
// Here we enforce the monotonically increase/decrease:
if (const auto *UO = dyn_cast<UnaryOperator>(*Inc)) {
// Allow increment/decrement ops.
if (!UO->isIncrementDecrementOp())
return DiagIncVar();
IncVar = getDeclFromExpr(UO->getSubExpr());
} else if (const auto *BO = dyn_cast<BinaryOperator>(*Inc)) {
switch (BO->getOpcode()) {
default:
return DiagIncVar();
case BO_AddAssign:
case BO_SubAssign:
case BO_MulAssign:
case BO_DivAssign:
case BO_Assign:
// += -= *= /= should all be fine here, this should be all of the
// 'monotonical' compound-assign ops.
// Assignment we just give up on, we could do better, and ensure that it
// is a binary/operator expr doing more work, but that seems like a lot
// of work for an error prone check.
break;
}
IncVar = getDeclFromExpr(BO->getLHS());
} else if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(*Inc)) {
switch (CE->getOperator()) {
default:
return DiagIncVar();
case OO_PlusPlus:
case OO_MinusMinus:
case OO_PlusEqual:
case OO_MinusEqual:
case OO_StarEqual:
case OO_SlashEqual:
case OO_Equal:
// += -= *= /= should all be fine here, this should be all of the
// 'monotonical' compound-assign ops.
// Assignment we just give up on, we could do better, and ensure that it
// is a binary/operator expr doing more work, but that seems like a lot
// of work for an error prone check.
break;
}
IncVar = getDeclFromExpr(CE->getArg(0));
} else if (const auto *ME = dyn_cast<CXXMemberCallExpr>(*Inc)) {
IncVar = getDeclFromExpr(ME->getImplicitObjectArgument());
// We can't really do much for member expressions, other than hope they are
// doing the right thing, so give up here.
}
if (!IncVar)
return DiagIncVar();
// InitVar shouldn't be null unless there was an error, so don't diagnose if
// that is the case. Else we should ensure that it refers to the loop
// value.
if (Init && IncVar->getCanonicalDecl() != Init->getCanonicalDecl())
return DiagIncVar();
return;
}
void SemaOpenACC::ActOnForStmtBegin(SourceLocation ForLoc, const Stmt *OldFirst,
const Stmt *First, const Stmt *OldSecond,
const Stmt *Second, const Stmt *OldThird,
const Stmt *Third) {
if (!getLangOpts().OpenACC)
return;
std::optional<const Stmt *> S;
if (OldSecond == Second)
S = std::nullopt;
else
S = Second;
std::optional<const Stmt *> T;
if (OldThird == Third)
S = std::nullopt;
else
S = Third;
bool InitChanged = false;
if (OldFirst != First) {
InitChanged = true;
// VarDecls are always rebuild because they are dependent, so we can do a
// little work to suppress some of the double checking based on whether the
// type is instantiation dependent.
QualType OldVDTy;
QualType NewVDTy;
if (const auto *DS = dyn_cast<DeclStmt>(OldFirst))
if (const VarDecl *VD = dyn_cast_if_present<VarDecl>(
DS->isSingleDecl() ? DS->getSingleDecl() : nullptr))
OldVDTy = VD->getType();
if (const auto *DS = dyn_cast<DeclStmt>(First))
if (const VarDecl *VD = dyn_cast_if_present<VarDecl>(
DS->isSingleDecl() ? DS->getSingleDecl() : nullptr))
NewVDTy = VD->getType();
if (!OldVDTy.isNull() && !NewVDTy.isNull())
InitChanged = OldVDTy->isInstantiationDependentType() !=
NewVDTy->isInstantiationDependentType();
}
ForStmtBeginChecker FSBC{*this, ForLoc, First, InitChanged, S, T};
if (!LoopInfo.TopLevelLoopSeen) {
FSBC.check();
}
ForStmtBeginHelper(ForLoc, FSBC);
}
void SemaOpenACC::ActOnForStmtBegin(SourceLocation ForLoc, const Stmt *First,
const Stmt *Second, const Stmt *Third) {
if (!getLangOpts().OpenACC)
return;
ForStmtBeginChecker FSBC{*this, ForLoc, First, /*InitChanged=*/true,
Second, Third};
if (!LoopInfo.TopLevelLoopSeen) {
FSBC.check();
}
ForStmtBeginHelper(ForLoc, FSBC);
}
void SemaOpenACC::ActOnRangeForStmtBegin(SourceLocation ForLoc,
const Stmt *OldRangeFor,
const Stmt *RangeFor) {
if (!getLangOpts().OpenACC)
return;
std::optional<const CXXForRangeStmt *> RF;
if (OldRangeFor == RangeFor)
RF = std::nullopt;
else
RF = cast<CXXForRangeStmt>(RangeFor);
ForStmtBeginChecker FSBC{*this, ForLoc, RF};
if (!LoopInfo.TopLevelLoopSeen) {
FSBC.check();
}
ForStmtBeginHelper(ForLoc, FSBC);
}
void SemaOpenACC::ActOnRangeForStmtBegin(SourceLocation ForLoc,
const Stmt *RangeFor) {
if (!getLangOpts().OpenACC)
return;
ForStmtBeginChecker FSBC{*this, ForLoc, cast<CXXForRangeStmt>(RangeFor)};
if (!LoopInfo.TopLevelLoopSeen) {
FSBC.check();
}
ForStmtBeginHelper(ForLoc, FSBC);
}
namespace {
SourceLocation FindInterveningCodeInLoop(const Stmt *CurStmt) {
// We should diagnose on anything except `CompoundStmt`, `NullStmt`,
// `ForStmt`, `CXXForRangeStmt`, since those are legal, and `WhileStmt` and
// `DoStmt`, as those are caught as a violation elsewhere.
// For `CompoundStmt` we need to search inside of it.
if (!CurStmt ||
isa<ForStmt, NullStmt, ForStmt, CXXForRangeStmt, WhileStmt, DoStmt>(
CurStmt))
return SourceLocation{};
// Any other construct is an error anyway, so it has already been diagnosed.
if (isa<OpenACCConstructStmt>(CurStmt))
return SourceLocation{};
// Search inside the compound statement, this allows for arbitrary nesting
// of compound statements, as long as there isn't any code inside.
if (const auto *CS = dyn_cast<CompoundStmt>(CurStmt)) {
for (const auto *ChildStmt : CS->children()) {
SourceLocation ChildStmtLoc = FindInterveningCodeInLoop(ChildStmt);
if (ChildStmtLoc.isValid())
return ChildStmtLoc;
}
// Empty/not invalid compound statements are legal.
return SourceLocation{};
}
return CurStmt->getBeginLoc();
}
} // namespace
void SemaOpenACC::ActOnForStmtEnd(SourceLocation ForLoc, StmtResult Body) {
if (!getLangOpts().OpenACC)
return;
// Set this to 'true' so if we find another one at this level we can diagnose.
LoopInfo.CurLevelHasLoopAlready = true;
if (!Body.isUsable())
return;
bool IsActiveCollapse = CollapseInfo.CurCollapseCount &&
*CollapseInfo.CurCollapseCount > 0 &&
!CollapseInfo.ActiveCollapse->hasForce();
bool IsActiveTile = TileInfo.CurTileCount && *TileInfo.CurTileCount > 0;
if (IsActiveCollapse || IsActiveTile) {
SourceLocation OtherStmtLoc = FindInterveningCodeInLoop(Body.get());
if (OtherStmtLoc.isValid() && IsActiveCollapse) {
Diag(OtherStmtLoc, diag::err_acc_intervening_code)
<< OpenACCClauseKind::Collapse << CollapseInfo.DirectiveKind;
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
}
if (OtherStmtLoc.isValid() && IsActiveTile) {
Diag(OtherStmtLoc, diag::err_acc_intervening_code)
<< OpenACCClauseKind::Tile << TileInfo.DirectiveKind;
Diag(TileInfo.ActiveTile->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
}
}
}
namespace {
// Get a list of clause Kinds for diagnosing a list, joined by a commas and an
// 'or'.
std::string GetListOfClauses(llvm::ArrayRef<OpenACCClauseKind> Clauses) {
assert(!Clauses.empty() && "empty clause list not supported");
std::string Output;
llvm::raw_string_ostream OS{Output};
if (Clauses.size() == 1) {
OS << '\'' << Clauses[0] << '\'';
return Output;
}
llvm::ArrayRef<OpenACCClauseKind> AllButLast{Clauses.begin(),
Clauses.end() - 1};
llvm::interleave(
AllButLast, [&](OpenACCClauseKind K) { OS << '\'' << K << '\''; },
[&] { OS << ", "; });
OS << " or \'" << Clauses.back() << '\'';
return Output;
}
} // namespace
bool SemaOpenACC::ActOnStartStmtDirective(
OpenACCDirectiveKind K, SourceLocation StartLoc,
ArrayRef<const OpenACCClause *> Clauses) {
SemaRef.DiscardCleanupsInEvaluationContext();
SemaRef.PopExpressionEvaluationContext();
// OpenACC 3.3 2.9.1:
// Intervening code must not contain other OpenACC directives or calls to API
// routines.
//
// ALL constructs are ill-formed if there is an active 'collapse'
if (CollapseInfo.CurCollapseCount && *CollapseInfo.CurCollapseCount > 0) {
Diag(StartLoc, diag::err_acc_invalid_in_loop)
<< /*OpenACC Construct*/ 0 << CollapseInfo.DirectiveKind
<< OpenACCClauseKind::Collapse << K;
assert(CollapseInfo.ActiveCollapse && "Collapse count without object?");
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
}
if (TileInfo.CurTileCount && *TileInfo.CurTileCount > 0) {
Diag(StartLoc, diag::err_acc_invalid_in_loop)
<< /*OpenACC Construct*/ 0 << TileInfo.DirectiveKind
<< OpenACCClauseKind::Tile << K;
assert(TileInfo.ActiveTile && "Tile count without object?");
Diag(TileInfo.ActiveTile->getBeginLoc(), diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
}
// OpenACC3.3 2.6.5: At least one copy, copyin, copyout, create, no_create,
// present, deviceptr, attach, or default clause must appear on a 'data'
// construct.
if (K == OpenACCDirectiveKind::Data &&
llvm::find_if(Clauses,
llvm::IsaPred<OpenACCCopyClause, OpenACCCopyInClause,
OpenACCCopyOutClause, OpenACCCreateClause,
OpenACCNoCreateClause, OpenACCPresentClause,
OpenACCDevicePtrClause, OpenACCAttachClause,
OpenACCDefaultClause>) == Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K
<< GetListOfClauses(
{OpenACCClauseKind::Copy, OpenACCClauseKind::CopyIn,
OpenACCClauseKind::CopyOut, OpenACCClauseKind::Create,
OpenACCClauseKind::NoCreate, OpenACCClauseKind::Present,
OpenACCClauseKind::DevicePtr, OpenACCClauseKind::Attach,
OpenACCClauseKind::Default});
// OpenACC3.3 2.6.6: At least one copyin, create, or attach clause must appear
// on an enter data directive.
if (K == OpenACCDirectiveKind::EnterData &&
llvm::find_if(Clauses,
llvm::IsaPred<OpenACCCopyInClause, OpenACCCreateClause,
OpenACCAttachClause>) == Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K
<< GetListOfClauses({
OpenACCClauseKind::CopyIn,
OpenACCClauseKind::Create,
OpenACCClauseKind::Attach,
});
// OpenACC3.3 2.6.6: At least one copyout, delete, or detach clause must
// appear on an exit data directive.
if (K == OpenACCDirectiveKind::ExitData &&
llvm::find_if(Clauses,
llvm::IsaPred<OpenACCCopyOutClause, OpenACCDeleteClause,
OpenACCDetachClause>) == Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K
<< GetListOfClauses({
OpenACCClauseKind::CopyOut,
OpenACCClauseKind::Delete,
OpenACCClauseKind::Detach,
});
// OpenACC3.3 2.8: At least 'one use_device' clause must appear.
if (K == OpenACCDirectiveKind::HostData &&
llvm::find_if(Clauses, llvm::IsaPred<OpenACCUseDeviceClause>) ==
Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K << GetListOfClauses({OpenACCClauseKind::UseDevice});
// OpenACC3.3 2.14.3: At least one default_async, device_num, or device_type
// clause must appear.
if (K == OpenACCDirectiveKind::Set &&
llvm::find_if(
Clauses,
llvm::IsaPred<OpenACCDefaultAsyncClause, OpenACCDeviceNumClause,
OpenACCDeviceTypeClause, OpenACCIfClause>) ==
Clauses.end())
return Diag(StartLoc, diag::err_acc_construct_one_clause_of)
<< K
<< GetListOfClauses({OpenACCClauseKind::DefaultAsync,
OpenACCClauseKind::DeviceNum,
OpenACCClauseKind::DeviceType,
OpenACCClauseKind::If});
// TODO: OpenACC: 'Update' construct needs to have one of 'self', 'host', or
// 'device'. Implement here.
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/true);
}
StmtResult SemaOpenACC::ActOnEndStmtDirective(
OpenACCDirectiveKind K, SourceLocation StartLoc, SourceLocation DirLoc,
SourceLocation LParenLoc, SourceLocation MiscLoc, ArrayRef<Expr *> Exprs,
SourceLocation RParenLoc, SourceLocation EndLoc,
ArrayRef<OpenACCClause *> Clauses, StmtResult AssocStmt) {
switch (K) {
default:
return StmtEmpty();
case OpenACCDirectiveKind::Invalid:
return StmtError();
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels: {
return OpenACCComputeConstruct::Create(
getASTContext(), K, StartLoc, DirLoc, EndLoc, Clauses,
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop: {
return OpenACCCombinedConstruct::Create(
getASTContext(), K, StartLoc, DirLoc, EndLoc, Clauses,
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
case OpenACCDirectiveKind::Loop: {
return OpenACCLoopConstruct::Create(
getASTContext(), ActiveComputeConstructInfo.Kind, StartLoc, DirLoc,
EndLoc, Clauses, AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
case OpenACCDirectiveKind::Data: {
return OpenACCDataConstruct::Create(
getASTContext(), StartLoc, DirLoc, EndLoc, Clauses,
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
case OpenACCDirectiveKind::EnterData: {
return OpenACCEnterDataConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
case OpenACCDirectiveKind::ExitData: {
return OpenACCExitDataConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
case OpenACCDirectiveKind::HostData: {
return OpenACCHostDataConstruct::Create(
getASTContext(), StartLoc, DirLoc, EndLoc, Clauses,
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
case OpenACCDirectiveKind::Wait: {
return OpenACCWaitConstruct::Create(
getASTContext(), StartLoc, DirLoc, LParenLoc, Exprs.front(), MiscLoc,
Exprs.drop_front(), RParenLoc, EndLoc, Clauses);
}
case OpenACCDirectiveKind::Init: {
return OpenACCInitConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
case OpenACCDirectiveKind::Shutdown: {
return OpenACCShutdownConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
case OpenACCDirectiveKind::Set: {
return OpenACCSetConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
case OpenACCDirectiveKind::Update: {
return OpenACCUpdateConstruct::Create(getASTContext(), StartLoc, DirLoc,
EndLoc, Clauses);
}
}
llvm_unreachable("Unhandled case in directive handling?");
}
StmtResult SemaOpenACC::ActOnAssociatedStmt(
SourceLocation DirectiveLoc, OpenACCDirectiveKind K,
ArrayRef<const OpenACCClause *> Clauses, StmtResult AssocStmt) {
switch (K) {
default:
llvm_unreachable("Unimplemented associated statement application");
case OpenACCDirectiveKind::EnterData:
case OpenACCDirectiveKind::ExitData:
case OpenACCDirectiveKind::Wait:
case OpenACCDirectiveKind::Init:
case OpenACCDirectiveKind::Shutdown:
case OpenACCDirectiveKind::Set:
llvm_unreachable(
"these don't have associated statements, so shouldn't get here");
case OpenACCDirectiveKind::Parallel:
case OpenACCDirectiveKind::Serial:
case OpenACCDirectiveKind::Kernels:
case OpenACCDirectiveKind::Data:
case OpenACCDirectiveKind::HostData:
// There really isn't any checking here that could happen. As long as we
// have a statement to associate, this should be fine.
// OpenACC 3.3 Section 6:
// Structured Block: in C or C++, an executable statement, possibly
// compound, with a single entry at the top and a single exit at the
// bottom.
// FIXME: Should we reject DeclStmt's here? The standard isn't clear, and
// an interpretation of it is to allow this and treat the initializer as
// the 'structured block'.
return AssocStmt;
case OpenACCDirectiveKind::Loop:
case OpenACCDirectiveKind::ParallelLoop:
case OpenACCDirectiveKind::SerialLoop:
case OpenACCDirectiveKind::KernelsLoop:
if (!AssocStmt.isUsable())
return StmtError();
if (!isa<CXXForRangeStmt, ForStmt>(AssocStmt.get())) {
Diag(AssocStmt.get()->getBeginLoc(), diag::err_acc_loop_not_for_loop)
<< K;
Diag(DirectiveLoc, diag::note_acc_construct_here) << K;
return StmtError();
}
if (!CollapseInfo.CollapseDepthSatisfied || !TileInfo.TileDepthSatisfied) {
if (!CollapseInfo.CollapseDepthSatisfied) {
Diag(DirectiveLoc, diag::err_acc_insufficient_loops)
<< OpenACCClauseKind::Collapse;
assert(CollapseInfo.ActiveCollapse && "Collapse count without object?");
Diag(CollapseInfo.ActiveCollapse->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Collapse;
}
if (!TileInfo.TileDepthSatisfied) {
Diag(DirectiveLoc, diag::err_acc_insufficient_loops)
<< OpenACCClauseKind::Tile;
assert(TileInfo.ActiveTile && "Collapse count without object?");
Diag(TileInfo.ActiveTile->getBeginLoc(),
diag::note_acc_active_clause_here)
<< OpenACCClauseKind::Tile;
}
return StmtError();
}
return AssocStmt.get();
}
llvm_unreachable("Invalid associated statement application");
}
bool SemaOpenACC::ActOnStartDeclDirective(OpenACCDirectiveKind K,
SourceLocation StartLoc) {
// OpenCC3.3 2.1 (line 889)
// A program must not depend on the order of evaluation of expressions in
// clause arguments or on any side effects of the evaluations.
SemaRef.DiscardCleanupsInEvaluationContext();
SemaRef.PopExpressionEvaluationContext();
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/false);
}
DeclGroupRef SemaOpenACC::ActOnEndDeclDirective() { return DeclGroupRef{}; }
ExprResult
SemaOpenACC::BuildOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc) {
return OpenACCAsteriskSizeExpr::Create(getASTContext(), AsteriskLoc);
}
ExprResult
SemaOpenACC::ActOnOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc) {
return BuildOpenACCAsteriskSizeExpr(AsteriskLoc);
}
|