summaryrefslogtreecommitdiffstats
path: root/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
blob: f33942b3c7c02d9b7f91a082f7764a2fab9c4c51 (plain)
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
//===-- OpenMPOps.td - OpenMP dialect operation definitions *- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the basic operations for the OpenMP dialect.
//
//===----------------------------------------------------------------------===//


#ifndef OPENMP_OPS
#define OPENMP_OPS

include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.td"
include "mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td"
include "mlir/Dialect/OpenMP/OpenMPTypeInterfaces.td"

def OpenMP_Dialect : Dialect {
  let name = "omp";
  let cppNamespace = "::mlir::omp";
  let dependentDialects = ["::mlir::LLVM::LLVMDialect, ::mlir::func::FuncDialect"];
  let useDefaultAttributePrinterParser = 1;
  let useDefaultTypePrinterParser = 1;
}

// OmpCommon requires definition of OpenACC_Dialect.
include "mlir/Dialect/OpenMP/OmpCommon.td"

//===----------------------------------------------------------------------===//
//  OpenMP Attributes
//===----------------------------------------------------------------------===//

class OpenMP_Attr<string name, string attrMnemonic,
                list<Trait> traits = [],
                string baseCppClass = "::mlir::Attribute">
    : AttrDef<OpenMP_Dialect, name, traits, baseCppClass> {
  let mnemonic = attrMnemonic;
}

def VersionAttr : OpenMP_Attr<"Version", "version"> {
  let parameters = (ins
    "uint32_t":$version
  );

  let assemblyFormat = "`<` struct(params) `>`";
}

//===----------------------------------------------------------------------===//
// Runtime library flag's attribute that holds information for lowering to LLVM
//===----------------------------------------------------------------------===//

def FlagsAttr : OpenMP_Attr<"Flags", "flags"> {
  let parameters = (ins
    DefaultValuedParameter<"uint32_t", "0">:$debug_kind,
    DefaultValuedParameter<"bool", "false">:$assume_teams_oversubscription,
    DefaultValuedParameter<"bool", "false">:$assume_threads_oversubscription,
    DefaultValuedParameter<"bool", "false">:$assume_no_thread_state,
    DefaultValuedParameter<"bool", "false">:$assume_no_nested_parallelism,
    DefaultValuedParameter<"bool", "false">:$no_gpu_lib,
    DefaultValuedParameter<"uint32_t", "50">:$openmp_device_version
  );

  let assemblyFormat = "`<` struct(params) `>`";
}


class OpenMP_Op<string mnemonic, list<Trait> traits = []> :
      Op<OpenMP_Dialect, mnemonic, traits>;

// Type which can be constraint accepting standard integers and indices.
def IntLikeType : AnyTypeOf<[AnyInteger, Index]>;

def OpenMP_PointerLikeType : TypeAlias<OpenMP_PointerLikeTypeInterface,
	"OpenMP-compatible variable type">;

class OpenMP_Type<string name, string typeMnemonic> : TypeDef<OpenMP_Dialect, name> {
  let mnemonic = typeMnemonic;
}

//===----------------------------------------------------------------------===//
//  2.12.7 Declare Target Directive
//===----------------------------------------------------------------------===//

def DeviceTypeAny : I32EnumAttrCase<"any", 0>;
def DeviceTypeHost   : I32EnumAttrCase<"host", 1>;
def DeviceTypeNoHost   : I32EnumAttrCase<"nohost", 2>;

def DeclareTargetDeviceType : I32EnumAttr<
    "DeclareTargetDeviceType",
    "device_type clause",
    [DeviceTypeAny, DeviceTypeHost, DeviceTypeNoHost]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}

def DeclareTargetDeviceTypeAttr : EnumAttr<OpenMP_Dialect, DeclareTargetDeviceType,
                                    "device_type"> {
  let assemblyFormat = "`(` $value `)`";
}

def CaptureClauseTo : I32EnumAttrCase<"to", 0>;
def CaptureClauseLink   : I32EnumAttrCase<"link", 1>;
def CaptureClauseEnter   : I32EnumAttrCase<"enter", 2>;

def DeclareTargetCaptureClause : I32EnumAttr<
    "DeclareTargetCaptureClause",
    "capture clause",
    [CaptureClauseTo, CaptureClauseLink, CaptureClauseEnter]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}

def DeclareTargetCaptureClauseAttr : EnumAttr<OpenMP_Dialect, DeclareTargetCaptureClause,
                                    "capture_clause"> {
  let assemblyFormat = "`(` $value `)`";
}

def DeclareTargetAttr : OpenMP_Attr<"DeclareTarget", "declaretarget"> {
  let parameters = (ins
    OptionalParameter<"DeclareTargetDeviceTypeAttr">:$device_type,
    OptionalParameter<"DeclareTargetCaptureClauseAttr">:$capture_clause
  );

  let assemblyFormat = "`<` struct(params) `>`";
}

//===----------------------------------------------------------------------===//
// 2.19.4 Data-Sharing Attribute Clauses
//===----------------------------------------------------------------------===//

def DataSharingTypePrivate      : I32EnumAttrCase<"Private", 0, "private">;
def DataSharingTypeFirstPrivate : I32EnumAttrCase<"FirstPrivate", 1, "firstprivate">;

def DataSharingClauseType : I32EnumAttr<
    "DataSharingClauseType",
    "Type of a data-sharing clause",
    [DataSharingTypePrivate, DataSharingTypeFirstPrivate]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}

def DataSharingClauseTypeAttr : EnumAttr<
    OpenMP_Dialect, DataSharingClauseType, "data_sharing_type"> {
  let assemblyFormat = "`{` `type` `=` $value `}`";
}

def PrivateClauseOp : OpenMP_Op<"private", [IsolatedFromAbove]> {
  let summary = "Provides declaration of [first]private logic.";
  let description = [{
    This operation provides a declaration of how to implement the
    [first]privatization of a variable. The dialect users should provide
    information about how to create an instance of the type in the alloc region
    and how to initialize the copy from the original item in the copy region.

    Examples:

    * `private(x)` would be emitted as:
    ```mlir
    omp.private {type = private} @x.privatizer : !fir.ref<i32> alloc {
    ^bb0(%arg0: !fir.ref<i32>):
    %0 = ... allocate proper memory for the private clone ...
    omp.yield(%0 : !fir.ref<i32>)
    }
    ```

    * `firstprivate(x)` would be emitted as:
    ```mlir
    omp.private {type = firstprivate} @x.privatizer : !fir.ref<i32> alloc {
    ^bb0(%arg0: !fir.ref<i32>):
    %0 = ... allocate proper memory for the private clone ...
    omp.yield(%0 : !fir.ref<i32>)
    } copy {
    ^bb0(%arg0: !fir.ref<i32>, %arg1: !fir.ref<i32>):
    // %arg0 is the original host variable. Same as for `alloc`.
    // %arg1 represents the memory allocated in `alloc`.
    ... copy from host to the privatized clone ....
    omp.yield(%arg1 : !fir.ref<i32>)
    }
    ```

    There are no restrictions on the body except for:
    - The `alloc` region has a single argument.
    - The `copy` region has 2 arguments.
    - Both regions are terminated by `omp.yield` ops.
    The above restrictions and other obvious restrictions (e.g. verifying the
    type of yielded values) are verified by the custom op verifier. The actual
    contents of the blocks inside both regions are not verified.

    Instances of this op would then be used by ops that model directives that
    accept data-sharing attribute clauses.

    The $sym_name attribute provides a symbol by which the privatizer op can be
    referenced by other dialect ops.

    The $type attribute is the type of the value being privatized.

    The $data_sharing_type attribute specifies whether privatizer corresponds
    to a `private` or a `firstprivate` clause.
  }];

  let arguments = (ins SymbolNameAttr:$sym_name,
                       TypeAttrOf<AnyType>:$type,
                       DataSharingClauseTypeAttr:$data_sharing_type);

  let regions = (region MinSizedRegion<1>:$alloc_region,
                        AnyRegion:$copy_region);

  let assemblyFormat = [{
    $data_sharing_type $sym_name `:` $type
      `alloc` $alloc_region
      (`copy` $copy_region^)?
      attr-dict
  }];

  let builders = [
    OpBuilder<(ins CArg<"TypeRange">:$result,
                   CArg<"StringAttr">:$sym_name,
                   CArg<"TypeAttr">:$type)>
  ];

  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.6 parallel Construct
//===----------------------------------------------------------------------===//

def ParallelOp : OpenMP_Op<"parallel", [
                 AutomaticAllocationScope, AttrSizedOperandSegments,
                 DeclareOpInterfaceMethods<OutlineableOpenMPOpInterface>,
                 RecursiveMemoryEffects, ReductionClauseInterface]> {
  let summary = "parallel construct";
  let description = [{
    The parallel construct includes a region of code which is to be executed
    by a team of threads.

    The optional $if_expr_var parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the parallel
    region runs as normal, if it is 0 then the parallel region is executed with
    one thread.

    The optional $num_threads_var parameter specifies the number of threads which
    should be used to execute the parallel region.

    The $allocators_vars and $allocate_vars parameters are a variadic list of values
    that specify the memory allocator to be used to obtain storage for private values.

    Reductions can be performed in a parallel construct by specifying reduction
    accumulator variables in `reduction_vars` and symbols referring to reduction
    declarations in the `reductions` attribute. Each reduction is identified
    by the accumulator it uses and accumulators must not be repeated in the same
    reduction. The `omp.reduction` operation accepts the accumulator and a
    partial value which is considered to be produced by the thread for the
    given reduction. If multiple values are produced for the same accumulator,
    i.e. there are multiple `omp.reduction`s, the last value is taken. The
    reduction declaration specifies how to combine the values from each thread
    into the final value, which is available in the accumulator after all the
    threads complete.

    The optional $proc_bind_val attribute controls the thread affinity for the execution
    of the parallel region.

    The optional byref attribute controls whether reduction arguments are passed by
    reference or by value.
  }];

  let arguments = (ins Optional<I1>:$if_expr_var,
             Optional<IntLikeType>:$num_threads_var,
             Variadic<AnyType>:$allocate_vars,
             Variadic<AnyType>:$allocators_vars,
             Variadic<OpenMP_PointerLikeType>:$reduction_vars,
             OptionalAttr<SymbolRefArrayAttr>:$reductions,
             OptionalAttr<ProcBindKindAttr>:$proc_bind_val,
             Variadic<AnyType>:$private_vars,
             OptionalAttr<SymbolRefArrayAttr>:$privatizers,
             UnitAttr:$byref);

  let regions = (region AnyRegion:$region);

  let builders = [
    OpBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "{}">:$attributes)>
  ];
  let extraClassDeclaration = [{
    /// Returns the number of reduction variables.
    unsigned getNumReductionVars() { return getReductionVars().size(); }
  }];
  let assemblyFormat = [{
    oilist(
          `if` `(` $if_expr_var `:` type($if_expr_var) `)`
          | `num_threads` `(` $num_threads_var `:` type($num_threads_var) `)`
          | `allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
          | `proc_bind` `(` custom<ClauseAttr>($proc_bind_val) `)`
          | `byref` $byref
    ) custom<ParallelRegion>($region, $reduction_vars, type($reduction_vars),
                             $reductions, $private_vars, type($private_vars),
                             $privatizers) attr-dict
  }];
  let hasVerifier = 1;
}

def TerminatorOp : OpenMP_Op<"terminator", [Terminator, Pure]> {
  let summary = "terminator for OpenMP regions";
  let description = [{
    A terminator operation for regions that appear in the body of OpenMP
    operation.  These regions are not expected to return any value so the
    terminator takes no operands. The terminator op returns control to the
    enclosing op.
  }];

  let assemblyFormat = "attr-dict";
}

//===----------------------------------------------------------------------===//
// 2.7 teams Construct
//===----------------------------------------------------------------------===//
def TeamsOp : OpenMP_Op<"teams", [
              AttrSizedOperandSegments, RecursiveMemoryEffects,
              ReductionClauseInterface]> {
  let summary = "teams construct";
  let description = [{
    The teams construct defines a region of code that triggers the creation of a
    league of teams. Once created, the number of teams remains constant for the
    duration of its code region.

    The optional $num_teams_upper and $num_teams_lower specify the limit on the
    number of teams to be created. If only the upper bound is specified, it acts
    as if the lower bound was set to the same value. It is not supported to set
    $num_teams_lower if $num_teams_upper is not specified. They define a closed
    range, where both the lower and upper bounds are included.

    If the $if_expr is present and it evaluates to `false`, the number of teams
    created is one.

    The optional $thread_limit specifies the limit on the number of threads.

    The $allocators_vars and $allocate_vars parameters are a variadic list of
    values that specify the memory allocator to be used to obtain storage for
    private values.
  }];

  let arguments = (ins Optional<AnyInteger>:$num_teams_lower,
                       Optional<AnyInteger>:$num_teams_upper,
                       Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$thread_limit,
                       Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars,
                       Variadic<OpenMP_PointerLikeType>:$reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$reductions);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(
      `num_teams` `(` ( $num_teams_lower^ `:` type($num_teams_lower) )? `to`
                        $num_teams_upper `:` type($num_teams_upper) `)`
    | `if` `(` $if_expr `)`
    | `thread_limit` `(` $thread_limit `:` type($thread_limit) `)`
    | `reduction` `(`
        custom<ReductionVarList>(
          $reduction_vars, type($reduction_vars), $reductions
        ) `)`
    | `allocate` `(`
        custom<AllocateAndAllocator>(
          $allocate_vars, type($allocate_vars),
          $allocators_vars, type($allocators_vars)
        ) `)`
    ) $region attr-dict
  }];

  let hasVerifier = 1;
}

def OMP_ScheduleModNone         : I32EnumAttrCase<"none", 0>;
def OMP_ScheduleModMonotonic    : I32EnumAttrCase<"monotonic", 1>;
def OMP_ScheduleModNonmonotonic : I32EnumAttrCase<"nonmonotonic", 2>;
// FIXME: remove this value for the modifier because this is handled using a
// separate attribute
def OMP_ScheduleModSIMD         : I32EnumAttrCase<"simd", 3>;

def ScheduleModifier
    : I32EnumAttr<"ScheduleModifier", "OpenMP Schedule Modifier",
                  [OMP_ScheduleModNone, OMP_ScheduleModMonotonic,
                   OMP_ScheduleModNonmonotonic, OMP_ScheduleModSIMD]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}
def ScheduleModifierAttr : EnumAttr<OpenMP_Dialect, ScheduleModifier,
                                    "sched_mod">;

//===----------------------------------------------------------------------===//
// 2.8.1 Sections Construct
//===----------------------------------------------------------------------===//

def SectionOp : OpenMP_Op<"section", [HasParent<"SectionsOp">]> {
  let summary = "section directive";
  let description = [{
    A section operation encloses a region which represents one section in a
    sections construct. A section op should always be surrounded by an
    `omp.sections` operation.
  }];
  let regions = (region AnyRegion:$region);
  let assemblyFormat = "$region attr-dict";
}

def SectionsOp : OpenMP_Op<"sections", [AttrSizedOperandSegments,
                           ReductionClauseInterface]> {
  let summary = "sections construct";
  let description = [{
    The sections construct is a non-iterative worksharing construct that
    contains `omp.section` operations. The `omp.section` operations are to be
    distributed among and executed by the threads in a team. Each `omp.section`
    is executed once by one of the threads in the team in the context of its
    implicit task.

    Reductions can be performed in a sections construct by specifying reduction
    accumulator variables in `reduction_vars` and symbols referring to reduction
    declarations in the `reductions` attribute. Each reduction is identified
    by the accumulator it uses and accumulators must not be repeated in the same
    reduction. The `omp.reduction` operation accepts the accumulator and a
    partial value which is considered to be produced by the section for the
    given reduction. If multiple values are produced for the same accumulator,
    i.e. there are multiple `omp.reduction`s, the last value is taken. The
    reduction declaration specifies how to combine the values from each section
    into the final value, which is available in the accumulator after all the
    sections complete.

    The $allocators_vars and $allocate_vars parameters are a variadic list of values
    that specify the memory allocator to be used to obtain storage for private values.

    The `nowait` attribute, when present, signifies that there should be no
    implicit barrier at the end of the construct.
  }];
  let arguments = (ins Variadic<OpenMP_PointerLikeType>:$reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$reductions,
                       Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars,
                       UnitAttr:$nowait);

  let regions = (region SizedRegion<1>:$region);

  let assemblyFormat = [{
    oilist( `reduction` `(`
              custom<ReductionVarList>(
                $reduction_vars, type($reduction_vars), $reductions
              ) `)`
          | `allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
          | `nowait` $nowait
    ) $region attr-dict
  }];

  let hasVerifier = 1;
  let hasRegionVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.8.2 Single Construct
//===----------------------------------------------------------------------===//

def SingleOp : OpenMP_Op<"single", [AttrSizedOperandSegments]> {
  let summary = "single directive";
  let description = [{
    The single construct specifies that the associated structured block is
    executed by only one of the threads in the team (not necessarily the
    master thread), in the context of its implicit task. The other threads
    in the team, which do not execute the block, wait at an implicit barrier
    at the end of the single construct unless a nowait clause is specified.

    If copyprivate variables and functions are specified, then each thread
    variable is updated with the variable value of the thread that executed
    the single region, using the specified copy functions.
  }];

  let arguments = (ins Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars,
                       Variadic<OpenMP_PointerLikeType>:$copyprivate_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$copyprivate_funcs,
                       UnitAttr:$nowait);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(`allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
          |`nowait` $nowait
          |`copyprivate` `(`
              custom<CopyPrivateVarList>(
                $copyprivate_vars, type($copyprivate_vars), $copyprivate_funcs
              ) `)`
    ) $region attr-dict
  }];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.9.2 Workshare Loop Construct
//===----------------------------------------------------------------------===//

def WsloopOp : OpenMP_Op<"wsloop", [AttrSizedOperandSegments,
                         AllTypesMatch<["lowerBound", "upperBound", "step"]>,
                         RecursiveMemoryEffects, ReductionClauseInterface]> {
  let summary = "worksharing-loop construct";
  let description = [{
    The worksharing-loop construct specifies that the iterations of the loop(s)
    will be executed in parallel by threads in the current context. These
    iterations are spread across threads that already exist in the enclosing
    parallel region. The lower and upper bounds specify a half-open range: the
    range includes the lower bound but does not include the upper bound. If the
    `inclusive` attribute is specified then the upper bound is also included.

    The body region can contain any number of blocks. The region is terminated
    by "omp.yield" instruction without operands.

    ```
    omp.wsloop <clauses>
    for (%i1, %i2) : index = (%c0, %c0) to (%c10, %c10) step (%c1, %c1) {
      %a = load %arrA[%i1, %i2] : memref<?x?xf32>
      %b = load %arrB[%i1, %i2] : memref<?x?xf32>
      %sum = arith.addf %a, %b : f32
      store %sum, %arrC[%i1, %i2] : memref<?x?xf32>
      omp.yield
    }
    ```

    The `linear_step_vars` operand additionally specifies the step for each
    associated linear operand. Note that the `linear_vars` and
    `linear_step_vars` variadic lists should contain the same number of
    elements.

    Reductions can be performed in a worksharing-loop by specifying reduction
    accumulator variables in `reduction_vars` and symbols referring to reduction
    declarations in the `reductions` attribute. Each reduction is identified
    by the accumulator it uses and accumulators must not be repeated in the same
    reduction. A private variable corresponding to the accumulator is used in
    place of the accumulator inside the body of the worksharing-loop. The
    reduction declaration specifies how to combine the values from each
    iteration into the final value, which is available in the accumulator after
    the loop completes.

    The optional `schedule_val` attribute specifies the loop schedule for this
    loop, determining how the loop is distributed across the parallel threads.
    The optional `schedule_chunk_var` associated with this determines further
    controls this distribution.

    Collapsed loops are represented by the worksharing-loop having a list of
    indices, bounds and steps where the size of the list is equal to the
    collapse value.

    The `nowait` attribute, when present, signifies that there should be no
    implicit barrier at the end of the loop.

    The optional `ordered_val` attribute specifies how many loops are associated
    with the worksharing-loop construct. The value of zero refers to the ordered
    clause specified without parameter.

    The optional `order` attribute specifies which order the iterations of the
    associate loops are executed in. Currently the only option for this
    attribute is "concurrent".

    The optional `byref` attribute indicates that reduction arguments should be
    passed by reference.
  }];

  let arguments = (ins Variadic<IntLikeType>:$lowerBound,
             Variadic<IntLikeType>:$upperBound,
             Variadic<IntLikeType>:$step,
             Variadic<AnyType>:$linear_vars,
             Variadic<I32>:$linear_step_vars,
             Variadic<OpenMP_PointerLikeType>:$reduction_vars,
             OptionalAttr<SymbolRefArrayAttr>:$reductions,
             OptionalAttr<ScheduleKindAttr>:$schedule_val,
             Optional<AnyType>:$schedule_chunk_var,
             OptionalAttr<ScheduleModifierAttr>:$schedule_modifier,
             UnitAttr:$simd_modifier,
             UnitAttr:$nowait,
             UnitAttr:$byref,
             ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$ordered_val,
             OptionalAttr<OrderKindAttr>:$order_val,
             UnitAttr:$inclusive);

  let builders = [
    OpBuilder<(ins "ValueRange":$lowerBound, "ValueRange":$upperBound,
               "ValueRange":$step,
               CArg<"ArrayRef<NamedAttribute>", "{}">:$attributes)>,
  ];

  let regions = (region AnyRegion:$region);

  let extraClassDeclaration = [{
    /// Returns the number of loops in the worksharing-loop nest.
    unsigned getNumLoops() { return getLowerBound().size(); }

    /// Returns the number of reduction variables.
    unsigned getNumReductionVars() { return getReductionVars().size(); }
  }];
  let hasCustomAssemblyFormat = 1;
  let assemblyFormat = [{
    oilist(`linear` `(`
              custom<LinearClause>($linear_vars, type($linear_vars),
                                   $linear_step_vars) `)`
          |`schedule` `(`
              custom<ScheduleClause>(
                $schedule_val, $schedule_modifier, $simd_modifier,
                $schedule_chunk_var, type($schedule_chunk_var)) `)`
          |`nowait` $nowait
          |`byref` $byref
          |`ordered` `(` $ordered_val `)`
          |`order` `(` custom<ClauseAttr>($order_val) `)`
    ) custom<Wsloop>($region, $lowerBound, $upperBound, $step, type($step),
                     $reduction_vars, type($reduction_vars), $reductions,
                     $inclusive) attr-dict
  }];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// Simd construct [2.9.3.1]
//===----------------------------------------------------------------------===//

def SimdLoopOp : OpenMP_Op<"simdloop", [AttrSizedOperandSegments,
                         AllTypesMatch<["lowerBound", "upperBound", "step"]>]> {
 let summary = "simd loop construct";
  let description = [{
    The simd construct can be applied to a loop to indicate that the loop can be
    transformed into a SIMD loop (that is, multiple iterations of the loop can
    be executed concurrently using SIMD instructions).. The lower and upper
    bounds specify a half-open range: the range includes the lower bound but
    does not include the upper bound. If the `inclusive` attribute is specified
    then the upper bound is also included.

    The body region can contain any number of blocks. The region is terminated
    by "omp.yield" instruction without operands.

    Collapsed loops are represented by the simd-loop having a list of indices,
    bounds and steps where the size of the list is equal to the collapse value.

    The `alignment_values` attribute additionally specifies alignment of each
    corresponding aligned operand. Note that `$aligned_vars` and
    `alignment_values` should contain the same number of elements.

    When an if clause is present and evaluates to false, the preferred number of
    iterations to be executed concurrently is one, regardless of whether
    a simdlen clause is specified.

    The optional `nontemporal` attribute specifies variables which have low
    temporal locality across the iterations where they are accessed.

    The optional `order` attribute specifies which order the iterations of the
    associate loops are executed in. Currently the only option for this
    attribute is "concurrent".

    When a simdlen clause is present, the preferred number of iterations to be
    executed concurrently is the value provided to the simdlen clause.

    The safelen clause specifies that no two concurrent iterations within a
    SIMD chunk can have a distance in the logical iteration space that is
    greater than or equal to the value given in the clause.
    ```
    omp.simdloop <clauses>
    for (%i1, %i2) : index = (%c0, %c0) to (%c10, %c10) step (%c1, %c1) {
      // block operations
      omp.yield
    }
    ```
  }];

  // TODO: Add other clauses
  let arguments = (ins Variadic<IntLikeType>:$lowerBound,
             Variadic<IntLikeType>:$upperBound,
             Variadic<IntLikeType>:$step,
             Variadic<OpenMP_PointerLikeType>:$aligned_vars,
             OptionalAttr<I64ArrayAttr>:$alignment_values,
             Optional<I1>:$if_expr,
             Variadic<OpenMP_PointerLikeType>:$nontemporal_vars,
             OptionalAttr<OrderKindAttr>:$order_val,
             ConfinedAttr<OptionalAttr<I64Attr>, [IntPositive]>:$simdlen,
             ConfinedAttr<OptionalAttr<I64Attr>, [IntPositive]>:$safelen,
             UnitAttr:$inclusive
     );

  let regions = (region AnyRegion:$region);
  let assemblyFormat = [{
    oilist(`aligned` `(`
              custom<AlignedClause>($aligned_vars, type($aligned_vars),
                                   $alignment_values) `)`
          |`if` `(` $if_expr `)`
          |`nontemporal` `(`  $nontemporal_vars `:` type($nontemporal_vars) `)`
          |`order` `(` custom<ClauseAttr>($order_val) `)`
          |`simdlen` `(` $simdlen  `)`
          |`safelen` `(` $safelen  `)`
    ) `for` custom<LoopControl>($region, $lowerBound, $upperBound, $step,
                                  type($step), $inclusive) attr-dict
  }];

  let extraClassDeclaration = [{
    /// Returns the number of loops in the simd loop nest.
    unsigned getNumLoops() { return getLowerBound().size(); }

  }];

  let hasCustomAssemblyFormat = 1;
  let hasVerifier = 1;
}


def YieldOp : OpenMP_Op<"yield",
    [Pure, ReturnLike, Terminator,
     ParentOneOf<["WsloopOp", "DeclareReductionOp",
     "AtomicUpdateOp", "SimdLoopOp", "PrivateClauseOp"]>]> {
  let summary = "loop yield and termination operation";
  let description = [{
    "omp.yield" yields SSA values from the OpenMP dialect op region and
    terminates the region. The semantics of how the values are yielded is
    defined by the parent operation.
  }];

  let arguments = (ins Variadic<AnyType>:$results);

  let builders = [
    OpBuilder<(ins), [{ build($_builder, $_state, {}); }]>
  ];

  let assemblyFormat = [{ ( `(` $results^ `:` type($results) `)` )? attr-dict}];
}

//===----------------------------------------------------------------------===//
// Distribute construct [2.9.4.1]
//===----------------------------------------------------------------------===//
def DistributeOp : OpenMP_Op<"distribute", [AttrSizedOperandSegments,
                              MemoryEffects<[MemWrite]>]> {
  let summary = "distribute construct";
  let description = [{
    The distribute construct specifies that the iterations of one or more loops
    (optionally specified using collapse clause) will be executed by the
    initial teams in the context of their implicit tasks. The loops that the
    distribute op is associated with starts with the outermost loop enclosed by
    the distribute op region and going down the loop nest toward the innermost
    loop. The iterations are distributed across the initial threads of all
    initial teams that execute the teams region to which the distribute region
    binds.

    The distribute loop construct specifies that the iterations of the loop(s)
    will be executed in parallel by threads in the current context. These
    iterations are spread across threads that already exist in the enclosing
    region. The lower and upper bounds specify a half-open range: the
    range includes the lower bound but does not include the upper bound. If the
    `inclusive` attribute is specified then the upper bound is also included.

    The `dist_schedule_static` attribute specifies the  schedule for this
    loop, determining how the loop is distributed across the parallel threads.
    The optional `schedule_chunk` associated with this determines further
    controls this distribution.

    // TODO: private_var, firstprivate_var, lastprivate_var, collapse
  }];
  let arguments = (ins
             UnitAttr:$dist_schedule_static,
             Optional<IntLikeType>:$chunk_size,
             Variadic<AnyType>:$allocate_vars,
             Variadic<AnyType>:$allocators_vars,
             OptionalAttr<OrderKindAttr>:$order_val);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(`dist_schedule_static` $dist_schedule_static
          |`chunk_size` `(` $chunk_size `:` type($chunk_size) `)`
          |`order` `(` custom<ClauseAttr>($order_val) `)`
          |`allocate` `(`
             custom<AllocateAndAllocator>(
               $allocate_vars, type($allocate_vars),
               $allocators_vars, type($allocators_vars)
             ) `)`
    ) $region attr-dict
  }];

  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.10.1 task Construct
//===----------------------------------------------------------------------===//

def ClauseTaskDependIn    : I32EnumAttrCase<"taskdependin",    0>;
def ClauseTaskDependOut   : I32EnumAttrCase<"taskdependout",   1>;
def ClauseTaskDependInOut : I32EnumAttrCase<"taskdependinout", 2>;

def ClauseTaskDepend : I32EnumAttr<
    "ClauseTaskDepend",
    "depend clause in a target or task construct",
    [ClauseTaskDependIn, ClauseTaskDependOut, ClauseTaskDependInOut]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}
def ClauseTaskDependAttr :
  EnumAttr<OpenMP_Dialect, ClauseTaskDepend, "clause_task_depend"> {
  let assemblyFormat = "`(` $value `)`";
}
def TaskDependArrayAttr :
  TypedArrayAttrBase<ClauseTaskDependAttr, "clause_task_depend array attr"> {
    let constBuilderCall = ?;
  }

def TaskOp : OpenMP_Op<"task", [AttrSizedOperandSegments,
                       OutlineableOpenMPOpInterface, AutomaticAllocationScope,
                       ReductionClauseInterface]> {
  let summary = "task construct";
  let description = [{
    The task construct defines an explicit task.

    For definitions of "undeferred task", "included task", "final task" and
    "mergeable task", please check OpenMP Specification.

    When an `if` clause is present on a task construct, and the value of
    `if_expr` evaluates to `false`, an "undeferred task" is generated, and the
    encountering thread must suspend the current task region, for which
    execution cannot be resumed until execution of the structured block that is
    associated with the generated task is completed.

    When a `final` clause is present on a task construct and the `final_expr`
    evaluates to `true`, the generated task will be a "final task". All task
    constructs encountered during execution of a final task will generate final
    and included tasks.

    If the `untied` clause is present on a task construct, any thread in the
    team can resume the task region after a suspension. The `untied` clause is
    ignored if a `final` clause is present on the same task construct and the
    `final_expr` evaluates to `true`, or if a task is an included task.

    When the `mergeable` clause is present on a task construct, the generated
    task is a "mergeable task".

    The `in_reduction` clause specifies that this particular task (among all the
    tasks in current taskgroup, if any) participates in a reduction.

    The `priority` clause is a hint for the priority of the generated task.
    The `priority` is a non-negative integer expression that provides a hint for
    task execution order. Among all tasks ready to be executed, higher priority
    tasks (those with a higher numerical value in the priority clause
    expression) are recommended to execute before lower priority ones. The
    default priority-value when no priority clause is specified should be
    assumed to be zero (the lowest priority).

    The `depends` and `depend_vars` arguments are variadic lists of values
    that specify the dependencies of this particular task in relation to
    other tasks.

    The `allocators_vars` and `allocate_vars` arguments are a variadic list of
    values that specify the memory allocator to be used to obtain storage for
    private values.

  }];

  // TODO: depend, affinity and detach clauses
  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<I1>:$final_expr,
                       UnitAttr:$untied,
                       UnitAttr:$mergeable,
                       Variadic<OpenMP_PointerLikeType>:$in_reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$in_reductions,
                       Optional<I32>:$priority,
                       OptionalAttr<TaskDependArrayAttr>:$depends,
                       Variadic<OpenMP_PointerLikeType>:$depend_vars,
                       Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars);
  let regions = (region AnyRegion:$region);
  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `)`
          |`final` `(` $final_expr `)`
          |`untied` $untied
          |`mergeable` $mergeable
          |`in_reduction` `(`
              custom<ReductionVarList>(
                $in_reduction_vars, type($in_reduction_vars), $in_reductions
              ) `)`
          |`priority` `(` $priority `)`
          |`allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
          |`depend` `(`
              custom<DependVarList>(
                $depend_vars, type($depend_vars), $depends
              ) `)`
    ) $region attr-dict
  }];
  let extraClassDeclaration = [{
    /// Returns the reduction variables
    SmallVector<Value> getReductionVars() {
      return SmallVector<Value>(getInReductionVars().begin(),
                                getInReductionVars().end());
    }
  }];
  let hasVerifier = 1;
}

def TaskloopOp : OpenMP_Op<"taskloop", [AttrSizedOperandSegments,
                           AutomaticAllocationScope, RecursiveMemoryEffects,
                           AllTypesMatch<["lowerBound", "upperBound", "step"]>,
                           ReductionClauseInterface]> {
  let summary = "taskloop construct";
  let description = [{
    The taskloop construct specifies that the iterations of one or more
    associated loops will be executed in parallel using explicit tasks. The
    iterations are distributed across tasks generated by the construct and
    scheduled to be executed.

    The `lowerBound` and `upperBound` specify a half-open range: the range
    includes the lower bound but does not include the upper bound. If the
    `inclusive` attribute is specified then the upper bound is also included.
    The `step` specifies the loop step.

    The body region can contain any number of blocks.

    ```
    omp.taskloop <clauses>
    for (%i1, %i2) : index = (%c0, %c0) to (%c10, %c10) step (%c1, %c1) {
      %a = load %arrA[%i1, %i2] : memref<?x?xf32>
      %b = load %arrB[%i1, %i2] : memref<?x?xf32>
      %sum = arith.addf %a, %b : f32
      store %sum, %arrC[%i1, %i2] : memref<?x?xf32>
      omp.terminator
    }
    ```

    For definitions of "undeferred task", "included task", "final task" and
    "mergeable task", please check OpenMP Specification.

    When an `if` clause is present on a taskloop construct, and if the `if`
    clause expression evaluates to `false`, undeferred tasks are generated. The
    use of a variable in an `if` clause expression of a taskloop construct
    causes an implicit reference to the variable in all enclosing constructs.

    When a `final` clause is present on a taskloop construct and the `final`
    clause expression evaluates to `true`, the generated tasks will be final
    tasks. The use of a variable in a `final` clause expression of a taskloop
    construct causes an implicit reference to the variable in all enclosing
    constructs.

    If the `untied` clause is specified, all tasks generated by the taskloop
    construct are untied tasks.

    When the `mergeable` clause is present on a taskloop construct, each
    generated task is a mergeable task.

    Reductions can be performed in a loop by specifying reduction accumulator
    variables in `reduction_vars` or `in_reduction_vars` and symbols referring
    to reduction declarations in the `reductions` or `in_reductions` attribute.
    Each reduction is identified by the accumulator it uses and accumulators
    must not be repeated in the same reduction. The `omp.reduction` operation
    accepts the accumulator and a partial value which is considered to be
    produced by the current loop iteration for the given reduction. If multiple
    values are produced for the same accumulator, i.e. there are multiple
    `omp.reduction`s, the last value is taken. The reduction declaration
    specifies how to combine the values from each iteration into the final
    value, which is available in the accumulator after the loop completes.

    If an `in_reduction` clause is present on the taskloop construct, the
    behavior is as if each generated task was defined by a task construct on
    which an `in_reduction` clause with the same reduction operator and list
    items is present. Thus, the generated tasks are participants of a reduction
    previously defined by a reduction scoping clause.

    If a `reduction` clause is present on the taskloop construct, the behavior
    is as if a `task_reduction` clause with the same reduction operator and list
    items was applied to the implicit taskgroup construct enclosing the taskloop
    construct. The taskloop construct executes as if each generated task was
    defined by a task construct on which an `in_reduction` clause with the same
    reduction operator and list items is present. Thus, the generated tasks are
    participants of the reduction defined by the `task_reduction` clause that
    was applied to the implicit taskgroup construct.

    When a `priority` clause is present on a taskloop construct, the generated
    tasks use the `priority-value` as if it was specified for each individual
    task. If the `priority` clause is not specified, tasks generated by the
    taskloop construct have the default task priority (zero).

    The `allocators_vars` and `allocate_vars` arguments are a variadic list of
    values that specify the memory allocator to be used to obtain storage for
    private values.

    If a `grainsize` clause is present on the taskloop construct, the number of
    logical loop iterations assigned to each generated task is greater than or
    equal to the minimum of the value of the grain-size expression and the
    number of logical loop iterations, but less than two times the value of the
    grain-size expression.

    If `num_tasks` is specified, the taskloop construct creates as many tasks as
    the minimum of the num-tasks expression and the number of logical loop
    iterations. Each task must have at least one logical loop iteration.

    By default, the taskloop construct executes as if it was enclosed in a
    taskgroup construct with no statements or directives outside of the taskloop
    construct. Thus, the taskloop construct creates an implicit taskgroup
    region. If the `nogroup` clause is present, no implicit taskgroup region is
    created.
  }];

  let arguments = (ins Variadic<IntLikeType>:$lowerBound,
                       Variadic<IntLikeType>:$upperBound,
                       Variadic<IntLikeType>:$step,
                       UnitAttr:$inclusive,
                       Optional<I1>:$if_expr,
                       Optional<I1>:$final_expr,
                       UnitAttr:$untied,
                       UnitAttr:$mergeable,
                       Variadic<OpenMP_PointerLikeType>:$in_reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$in_reductions,
                       Variadic<OpenMP_PointerLikeType>:$reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$reductions,
                       Optional<IntLikeType>:$priority,
                       Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars,
                       Optional<IntLikeType>: $grain_size,
                       Optional<IntLikeType>: $num_tasks,
                       UnitAttr: $nogroup);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `)`
          |`final` `(` $final_expr `)`
          |`untied` $untied
          |`mergeable` $mergeable
          |`in_reduction` `(`
              custom<ReductionVarList>(
                $in_reduction_vars, type($in_reduction_vars), $in_reductions
              ) `)`
          |`reduction` `(`
              custom<ReductionVarList>(
                $reduction_vars, type($reduction_vars), $reductions
              ) `)`
          |`priority` `(` $priority `:` type($priority) `)`
          |`allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
          |`grain_size` `(` $grain_size `:` type($grain_size) `)`
          |`num_tasks` `(` $num_tasks `:` type($num_tasks) `)`
          |`nogroup` $nogroup
    ) `for` custom<LoopControl>($region, $lowerBound, $upperBound, $step,
                                  type($step), $inclusive) attr-dict
  }];

  let extraClassDeclaration = [{
    /// Returns the reduction variables
    SmallVector<Value> getAllReductionVars();
    void getEffects(SmallVectorImpl<MemoryEffects::EffectInstance> &effects);
  }];

  let hasVerifier = 1;
}

def TaskgroupOp : OpenMP_Op<"taskgroup", [AttrSizedOperandSegments,
                            ReductionClauseInterface,
                            AutomaticAllocationScope]> {
  let summary = "taskgroup construct";
  let description = [{
    The taskgroup construct specifies a wait on completion of child tasks of the
    current task and their descendent tasks.

    When a thread encounters a taskgroup construct, it starts executing the
    region. All child tasks generated in the taskgroup region and all of their
    descendants that bind to the same parallel region as the taskgroup region
    are part of the taskgroup set associated with the taskgroup region. There is
    an implicit task scheduling point at the end of the taskgroup region. The
    current task is suspended at the task scheduling point until all tasks in
    the taskgroup set complete execution.

    The `task_reduction` clause specifies a reduction among tasks. For each list
    item, the number of copies is unspecified. Any copies associated with the
    reduction are initialized before they are accessed by the tasks
    participating in the reduction. After the end of the region, the original
    list item contains the result of the reduction.

    The `allocators_vars` and `allocate_vars` arguments are a variadic list of
    values that specify the memory allocator to be used to obtain storage for
    private values.
  }];

  let arguments = (ins Variadic<OpenMP_PointerLikeType>:$task_reduction_vars,
                       OptionalAttr<SymbolRefArrayAttr>:$task_reductions,
                       Variadic<AnyType>:$allocate_vars,
                       Variadic<AnyType>:$allocators_vars);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(`task_reduction` `(`
              custom<ReductionVarList>(
                $task_reduction_vars, type($task_reduction_vars), $task_reductions
              ) `)`
          |`allocate` `(`
              custom<AllocateAndAllocator>(
                $allocate_vars, type($allocate_vars),
                $allocators_vars, type($allocators_vars)
              ) `)`
    ) $region attr-dict
  }];

  let extraClassDeclaration = [{
    /// Returns the reduction variables
    operand_range getAllReductionVars() { return getTaskReductionVars(); }
  }];

  let hasVerifier = 1;

}

//===----------------------------------------------------------------------===//
// 2.10.4 taskyield Construct
//===----------------------------------------------------------------------===//

def TaskyieldOp : OpenMP_Op<"taskyield"> {
  let summary = "taskyield construct";
  let description = [{
    The taskyield construct specifies that the current task can be suspended
    in favor of execution of a different task.
  }];

  let assemblyFormat = "attr-dict";
}

//===----------------------------------------------------------------------===//
// 2.13.7 flush Construct
//===----------------------------------------------------------------------===//
def FlushOp : OpenMP_Op<"flush"> {
  let summary = "flush construct";
  let description = [{
    The flush construct executes the OpenMP flush operation. This operation
    makes a thread’s temporary view of memory consistent with memory and
    enforces an order on the memory operations of the variables explicitly
    specified or implied.
  }];

  let arguments = (ins Variadic<OpenMP_PointerLikeType>:$varList);

  let assemblyFormat = [{ ( `(` $varList^ `:` type($varList) `)` )? attr-dict}];
  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      return getOperation()->getNumOperands();
    }
    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      return getOperand(i);
    }
  }];
}

//===----------------------------------------------------------------------===//
// Map related constructs
//===----------------------------------------------------------------------===//

def CaptureThis : I32EnumAttrCase<"This", 0>;
def CaptureByRef : I32EnumAttrCase<"ByRef", 1>;
def CaptureByCopy : I32EnumAttrCase<"ByCopy", 2>;
def CaptureVLAType : I32EnumAttrCase<"VLAType", 3>;

def VariableCaptureKind : I32EnumAttr<
    "VariableCaptureKind",
    "variable capture kind",
    [CaptureThis, CaptureByRef, CaptureByCopy, CaptureVLAType]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}

def VariableCaptureKindAttr : EnumAttr<OpenMP_Dialect, VariableCaptureKind,
                                       "variable_capture_kind"> {
  let assemblyFormat = "`(` $value `)`";
}

def MapBoundsType : OpenMP_Type<"MapBounds", "map_bounds_ty"> {
  let summary = "Type for representing omp map clause bounds information";
}

def MapBoundsOp : OpenMP_Op<"map.bounds",
    [AttrSizedOperandSegments, NoMemoryEffect]> {
  let summary = "Represents normalized bounds information for map clauses.";

  let description = [{
    This operation is a variation on the OpenACC dialects DataBoundsOp. Within
    the OpenMP dialect it stores the bounds/range of data to be mapped to a
    device specified by map clauses on target directives. Within
    the OpenMP dialect, the MapBoundsOp is associated with MapInfoOp,
    helping to store bounds information for the mapped variable.

    It is used to support OpenMP array sectioning, Fortran pointer and
    allocatable mapping and pointer/allocatable member of derived types.
    In all cases the MapBoundsOp holds information on the section of
    data to be mapped. Such as the upper bound and lower bound of the
    section of data to be mapped. This information is currently
    utilised by the LLVM-IR lowering to help generate instructions to
    copy data to and from the device when processing target operations.

    The example below copys a section of a 10-element array; all except the
    first element, utilising OpenMP array sectioning syntax where array
    subscripts are provided to specify the bounds to be mapped to device.
    To simplify the examples, the constants are used directly, in reality
    they will be MLIR SSA values.

    C++:
    ```
    int array[10];
    #pragma target map(array[1:9])
    ```
    =>
    ```mlir
    omp.map.bounds lower_bound(1) upper_bound(9) extent(9) start_idx(0)
    ```

    Fortran:
    ```
    integer :: array(1:10)
    !$target map(array(2:10))
    ```
    =>
    ```mlir
    omp.map.bounds lower_bound(1) upper_bound(9) extent(9) start_idx(1)
    ```

    For Fortran pointers and allocatables (as well as those that are
    members of derived types) the bounds information is provided by
    the Fortran compiler and runtime through descriptor information.

    A basic pointer example can be found below (constants again
    provided for simplicity, where in reality SSA values will be
    used, in this case that point to data yielded by Fortran's
    descriptors):

    Fortran:
    ```
    integer, pointer :: ptr(:)
    allocate(ptr(10))
    !$target map(ptr)
    ```
    =>
    ```mlir
    omp.map.bounds lower_bound(0) upper_bound(9) extent(10) start_idx(1)
    ```

    This operation records the bounds information in a normalized fashion
    (zero-based). This works well with the `PointerLikeType`
    requirement in data clauses - since a `lower_bound` of 0 means looking
    at data at the zero offset from pointer.

    This operation must have an `upper_bound` or `extent` (or both are allowed -
    but not checked for consistency). When the source language's arrays are
    not zero-based, the `start_idx` must specify the zero-position index.
  }];

  let arguments = (ins Optional<IntLikeType>:$lower_bound,
                       Optional<IntLikeType>:$upper_bound,
                       Optional<IntLikeType>:$extent,
                       Optional<IntLikeType>:$stride,
                       DefaultValuedAttr<BoolAttr, "false">:$stride_in_bytes,
                       Optional<IntLikeType>:$start_idx);
  let results = (outs MapBoundsType:$result);

  let assemblyFormat = [{
    oilist(
        `lower_bound` `(` $lower_bound `:` type($lower_bound) `)`
      | `upper_bound` `(` $upper_bound `:` type($upper_bound) `)`
      | `extent` `(` $extent `:` type($extent) `)`
      | `stride` `(` $stride `:` type($stride) `)`
      | `start_idx` `(` $start_idx `:` type($start_idx) `)`
    ) attr-dict
  }];

  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      return getNumOperands();
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      return getOperands()[i];
    }
  }];

  let hasVerifier = 1;
}

def MapInfoOp : OpenMP_Op<"map.info", [AttrSizedOperandSegments]> {
  let arguments = (ins OpenMP_PointerLikeType:$var_ptr,
                       TypeAttr:$var_type,
                       Optional<OpenMP_PointerLikeType>:$var_ptr_ptr,
                       Variadic<OpenMP_PointerLikeType>:$members,
                       Variadic<MapBoundsType>:$bounds, /* rank-0 to rank-{n-1} */
                       OptionalAttr<UI64Attr>:$map_type,
                       OptionalAttr<VariableCaptureKindAttr>:$map_capture_type,
                       OptionalAttr<StrAttr>:$name);
  let results = (outs OpenMP_PointerLikeType:$omp_ptr);

  let description = [{
    The MapInfoOp captures information relating to individual OpenMP map clauses
    that are applied to certain OpenMP directives such as Target and Target Data.

    For example, the map type modifier; such as from, tofrom and to, the variable
    being captured or the bounds of an array section being mapped.

    It can be used to capture both implicit and explicit map information, where
    explicit is an argument directly specified to an OpenMP map clause or implicit
    where a variable is utilised in a target region but is defined externally to
    the target region.

    This map information is later used to aid the lowering of the target operations
    they are attached to providing argument input and output context for kernels
    generated or the target data mapping environment.

    Example (Fortran):

    ```
    integer :: index
    !$target map(to: index)
    ```
    =>
    ```mlir
    omp.map.info var_ptr(%index_ssa) map_type(to) map_capture_type(ByRef)
      name(index)
    ```

    Description of arguments:
    - `var_ptr`: The address of variable to copy.
    - `var_type`: The type of the variable to copy.
    - `var_ptr_ptr`: Used when the variable copied is a member of a class, structure
      or derived type and refers to the originating struct.
    - `members`:  Used to indicate mapped child members for the current MapInfoOp,
       represented as other MapInfoOp's, utilised in cases where a parent structure
       type and members of the structure type are being mapped at the same time.
       For example: map(to: parent, parent->member, parent->member2[:10])
    - `bounds`: Used when copying slices of array's, pointers or pointer members of
       objects (e.g. derived types or classes), indicates the bounds to be copied
       of the variable. When it's an array slice it is in rank order where rank 0
       is the inner-most dimension.
    - 'map_clauses': OpenMP map type for this map capture, for example: from, to and
       always. It's a bitfield composed of the OpenMP runtime flags stored in
       OpenMPOffloadMappingFlags.
    - 'map_capture_type': Capture type for the variable e.g. this, byref, byvalue, byvla
       this can affect how the variable is lowered.
    - `name`: Holds the name of variable as specified in user clause (including bounds).
  }];

  let assemblyFormat = [{
    `var_ptr` `(` $var_ptr `:` type($var_ptr) `,` $var_type `)`
    oilist(
        `var_ptr_ptr` `(` $var_ptr_ptr `:` type($var_ptr_ptr) `)`
      | `map_clauses` `(` custom<MapClause>($map_type) `)`
      | `capture` `(` custom<CaptureType>($map_capture_type) `)`
      | `members` `(` $members `:` type($members) `)`
      | `bounds` `(` $bounds `)`
    ) `->` type($omp_ptr) attr-dict
  }];

  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      return getNumOperands();
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      return getOperands()[i];
    }
  }];
}

//===---------------------------------------------------------------------===//
// 2.14.2 target data Construct
//===---------------------------------------------------------------------===//

def TargetDataOp: OpenMP_Op<"target_data", [AttrSizedOperandSegments,
                                            MapClauseOwningOpInterface]>{
  let summary = "target data construct";
  let description = [{
    Map variables to a device data environment for the extent of the region.

    The omp target data directive maps variables to a device data
    environment, and defines the lexical scope of the data environment
    that is created. The omp target data directive can reduce data copies
    to and from the offloading device when multiple target regions are using
    the same data.

    The optional $if_expr parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the target
    region runs on a device, if it is 0 then the target region is executed
    on the host device.

    The optional $device parameter specifies the device number for the target
    region.

    The optional $use_device_ptr specifies the device pointers to the
    corresponding list items in the device data environment.

    The optional $use_device_addr specifies the address of the objects in the
    device data enviornment.

    The $map_operands specifies the locator-list operands of the map clause.

    The $map_types specifies the types and modifiers for the map clause.

    TODO:  depend clause and map_type_modifier values iterator and mapper.
  }];

  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$device,
                       Variadic<OpenMP_PointerLikeType>:$use_device_ptr,
                       Variadic<OpenMP_PointerLikeType>:$use_device_addr,
                       Variadic<AnyType>:$map_operands);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `:` type($if_expr) `)`
    | `device` `(` $device `:` type($device) `)`
    | `map_entries` `(` $map_operands `:` type($map_operands) `)`
    | `use_device_ptr` `(` $use_device_ptr `:` type($use_device_ptr) `)`
    | `use_device_addr` `(` $use_device_addr `:` type($use_device_addr) `)`)
    $region attr-dict
  }];

  let hasVerifier = 1;
}

//===---------------------------------------------------------------------===//
// 2.14.3 target enter data Construct
//===---------------------------------------------------------------------===//

def TargetEnterDataOp: OpenMP_Op<"target_enter_data",
                                                 [AttrSizedOperandSegments,
                                                  MapClauseOwningOpInterface]>{
  let  summary = "target enter data construct";
  let description = [{
    The target enter data directive specifies that variables are mapped to
    a device data environment. The target enter data directive is a
    stand-alone directive.

    The optional $if_expr parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the target
    region runs on a device, if it is 0 then the target region is executed on
    the host device.

    The optional $device parameter specifies the device number for the
    target region.

    The optional $nowait eliminates the implicit barrier so the parent task
    can make progress even if the target task is not yet completed.

    The $map_operands specifies the locator-list operands of the map clause.

    The $map_types specifies the types and modifiers for the map clause.

    The `depends` and `depend_vars` arguments are variadic lists of values
    that specify the dependencies of this particular target task in relation to
    other tasks.

    TODO:  map_type_modifier values iterator and mapper.
  }];

  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$device,
                       OptionalAttr<TaskDependArrayAttr>:$depends,
                       Variadic<OpenMP_PointerLikeType>:$depend_vars,
                       UnitAttr:$nowait,
                       Variadic<AnyType>:$map_operands);

  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `:` type($if_expr) `)`
    | `device` `(` $device `:` type($device) `)`
    | `nowait` $nowait
    | `map_entries` `(` $map_operands `:` type($map_operands) `)`
    | `depend` `(` custom<DependVarList>($depend_vars, type($depend_vars), $depends) `)`
    ) attr-dict
   }];

  let hasVerifier = 1;
}

//===---------------------------------------------------------------------===//
// 2.14.4 target exit data Construct
//===---------------------------------------------------------------------===//

def TargetExitDataOp: OpenMP_Op<"target_exit_data",
                                                 [AttrSizedOperandSegments,
                                                  MapClauseOwningOpInterface]>{
  let  summary = "target exit data construct";
  let description = [{
    The target exit data directive specifies that variables are mapped to a
    device data environment. The target exit data directive is
    a stand-alone directive.

    The optional $if_expr parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the target
    region runs on a device, if it is 0 then the target region is executed
    on the host device.

    The optional $device parameter specifies the device number for the
    target region.

    The optional $nowait eliminates the implicit barrier so the parent
    task can make progress even if the target task is not yet completed.

    The $map_operands specifies the locator-list operands of the map clause.

    The $map_types specifies the types and modifiers for the map clause.

    The `depends` and `depend_vars` arguments are variadic lists of values
    that specify the dependencies of this particular target task in relation to
    other tasks.

    TODO: map_type_modifier values iterator and mapper.
  }];

  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$device,
                       OptionalAttr<TaskDependArrayAttr>:$depends,
                       Variadic<OpenMP_PointerLikeType>:$depend_vars,
                       UnitAttr:$nowait,
                       Variadic<AnyType>:$map_operands);

  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `:` type($if_expr) `)`
    | `device` `(` $device `:` type($device) `)`
    | `nowait` $nowait
    | `map_entries` `(` $map_operands `:` type($map_operands) `)`
    | `depend` `(` custom<DependVarList>($depend_vars, type($depend_vars), $depends) `)`
    ) attr-dict
   }];

  let hasVerifier = 1;
}

//===---------------------------------------------------------------------===//
// 2.14.6 target update Construct
//===---------------------------------------------------------------------===//

def TargetUpdateOp: OpenMP_Op<"target_update", [AttrSizedOperandSegments,
                                                MapClauseOwningOpInterface]>{
  let  summary = "target update construct";
  let description = [{
    The target update directive makes the corresponding list items in the device
    data environment consistent with their original list items, according to the
    specified motion clauses. The target update construct is a stand-alone
    directive.

    The optional $if_expr parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the target
    region runs on a device, if it is 0 then the target region is executed
    on the host device.

    The optional $device parameter specifies the device number for the
    target region.

    The optional $nowait eliminates the implicit barrier so the parent
    task can make progress even if the target task is not yet completed.

    We use `MapInfoOp` to model the motion clauses and their modifiers. Even
    though the spec differentiates between map-types & map-type-modifiers vs.
    motion-clauses & motion-modifiers, the motion clauses and their modifiers are
    a subset of map types and their modifiers. The subset relation is handled in
    during verification to make sure the restrictions for target update are
    respected.

    The `depends` and `depend_vars` arguments are variadic lists of values
    that specify the dependencies of this particular target task in relation to
    other tasks.

  }];

  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$device,
                       OptionalAttr<TaskDependArrayAttr>:$depends,
                       Variadic<OpenMP_PointerLikeType>:$depend_vars,
                       UnitAttr:$nowait,
                       Variadic<OpenMP_PointerLikeType>:$map_operands);

  let assemblyFormat = [{
    oilist(`if` `(` $if_expr `:` type($if_expr) `)`
    | `device` `(` $device `:` type($device) `)`
    | `nowait` $nowait
    | `motion_entries` `(` $map_operands `:` type($map_operands) `)`
    | `depend` `(` custom<DependVarList>($depend_vars, type($depend_vars), $depends) `)`
    ) attr-dict
   }];

  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.14.5 target construct
//===----------------------------------------------------------------------===//

def TargetOp : OpenMP_Op<"target", [IsolatedFromAbove, MapClauseOwningOpInterface,
                                   OutlineableOpenMPOpInterface, AttrSizedOperandSegments]> {
  let summary = "target construct";
  let description = [{
    The target construct includes a region of code which is to be executed
    on a device.

    The optional $if_expr parameter specifies a boolean result of a
    conditional check. If this value is 1 or is not provided then the target
    region runs on a device, if it is 0 then the target region is executed on the
    host device.

    The optional $device parameter specifies the device number for the target region.

    The optional $thread_limit specifies the limit on the number of threads

    The optional $nowait elliminates the implicit barrier so the parent task can make progress
    even if the target task is not yet completed.

    The `depends` and `depend_vars` arguments are variadic lists of values
    that specify the dependencies of this particular target task in relation to
    other tasks.

    TODO:  is_device_ptr, defaultmap, in_reduction

  }];

  let arguments = (ins Optional<I1>:$if_expr,
                       Optional<AnyInteger>:$device,
                       Optional<AnyInteger>:$thread_limit,
                       OptionalAttr<TaskDependArrayAttr>:$depends,
                       Variadic<OpenMP_PointerLikeType>:$depend_vars,
                       UnitAttr:$nowait,
                       Variadic<AnyType>:$map_operands);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    oilist( `if` `(` $if_expr `)`
    | `device` `(` $device `:` type($device) `)`
    | `thread_limit` `(` $thread_limit `:` type($thread_limit) `)`
    | `nowait` $nowait
    | `map_entries` `(` custom<MapEntries>($map_operands, type($map_operands)) `)`
    | `depend` `(` custom<DependVarList>($depend_vars, type($depend_vars), $depends) `)`
    ) $region attr-dict
  }];

  let hasVerifier = 1;
}


//===----------------------------------------------------------------------===//
// 2.16 master Construct
//===----------------------------------------------------------------------===//
def MasterOp : OpenMP_Op<"master"> {
  let summary = "master construct";
  let description = [{
    The master construct specifies a structured block that is executed by
    the master thread of the team.
  }];

  let regions = (region AnyRegion:$region);

  let assemblyFormat = "$region attr-dict";
}

//===----------------------------------------------------------------------===//
// 2.17.1 critical Construct
//===----------------------------------------------------------------------===//
def CriticalDeclareOp : OpenMP_Op<"critical.declare", [Symbol]> {
  let summary = "declares a named critical section.";

  let description = [{
    Declares a named critical section.

    The name can be used in critical constructs in the dialect.
  }];

  let arguments = (ins SymbolNameAttr:$sym_name,
                       DefaultValuedAttr<I64Attr, "0">:$hint_val);

  let assemblyFormat = [{
    $sym_name oilist(`hint` `(` custom<SynchronizationHint>($hint_val) `)`)
    attr-dict
  }];
  let hasVerifier = 1;
}


def CriticalOp : OpenMP_Op<"critical",
    [DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
  let summary = "critical construct";
  let description = [{
    The critical construct imposes a restriction on the associated structured
    block (region) to be executed by only a single thread at a time.
  }];

  let arguments = (ins OptionalAttr<FlatSymbolRefAttr>:$name);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{
    (`(` $name^ `)`)? $region attr-dict
  }];
}

//===----------------------------------------------------------------------===//
// 2.17.2 barrier Construct
//===----------------------------------------------------------------------===//

def BarrierOp : OpenMP_Op<"barrier"> {
  let summary = "barrier construct";
  let description = [{
    The barrier construct specifies an explicit barrier at the point at which
    the construct appears.
  }];

  let assemblyFormat = "attr-dict";
}

//===----------------------------------------------------------------------===//
// [5.1] 2.19.9 ordered Construct
//===----------------------------------------------------------------------===//

def ClauseDependSource : I32EnumAttrCase<"dependsource", 0>;
def ClauseDependSink   : I32EnumAttrCase<"dependsink",   1>;

def ClauseDepend : I32EnumAttr<
    "ClauseDepend",
    "depend clause",
    [ClauseDependSource, ClauseDependSink]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}
def ClauseDependAttr : EnumAttr<OpenMP_Dialect, ClauseDepend, "clause_depend"> {
  let assemblyFormat = "`(` $value `)`";
}

def OrderedOp : OpenMP_Op<"ordered"> {
  let summary = "ordered construct without region";
  let description = [{
    The ordered construct without region is a stand-alone directive that
    specifies cross-iteration dependences in a doacross loop nest.

    The `depend_type_val` attribute refers to either the DEPEND(SOURCE) clause
    or the DEPEND(SINK: vec) clause.

    The `num_loops_val` attribute specifies the number of loops in the doacross
    nest.

    The `depend_vec_vars` is a variadic list of operands that specifies the index
    of the loop iterator in the doacross nest for the DEPEND(SOURCE) clause or
    the index of the element of "vec" for the DEPEND(SINK: vec) clause. It
    contains the operands in multiple "vec" when multiple DEPEND(SINK: vec)
    clauses exist in one ORDERED directive.
  }];

  let arguments = (ins OptionalAttr<ClauseDependAttr>:$depend_type_val,
             ConfinedAttr<OptionalAttr<I64Attr>, [IntMinValue<0>]>:$num_loops_val,
             Variadic<AnyType>:$depend_vec_vars);

  let assemblyFormat = [{
    ( `depend_type` `` $depend_type_val^ )?
    ( `depend_vec` `(` $depend_vec_vars^ `:` type($depend_vec_vars) `)` )?
    attr-dict
  }];
  let hasVerifier = 1;
}

def OrderedRegionOp : OpenMP_Op<"ordered.region"> {
  let summary = "ordered construct with region";
  let description = [{
    The ordered construct with region specifies a structured block in a
    worksharing-loop, SIMD, or worksharing-loop SIMD region that is executed in
    the order of the loop iterations.

    The `simd` attribute corresponds to the SIMD clause specified. If it is not
    present, it behaves as if the THREADS clause is specified or no clause is
    specified.
  }];

  let arguments = (ins UnitAttr:$simd);

  let regions = (region AnyRegion:$region);

  let assemblyFormat = [{ ( `simd` $simd^ )? $region attr-dict}];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.17.5 taskwait Construct
//===----------------------------------------------------------------------===//

def TaskwaitOp : OpenMP_Op<"taskwait"> {
  let summary = "taskwait construct";
  let description = [{
    The taskwait construct specifies a wait on the completion of child tasks
    of the current task.
  }];

  let assemblyFormat = "attr-dict";
}

//===----------------------------------------------------------------------===//
// 2.17.7 atomic construct
//===----------------------------------------------------------------------===//

// In the OpenMP Specification, atomic construct has an `atomic-clause` which
// can take the values `read`, `write`, `update` and `capture`. These four
// kinds of atomic constructs are fundamentally independent and are handled
// separately while lowering. Having four separate operations (one for each
// value of the clause) here decomposes handling of this construct into a
// two-step process.

def AtomicReadOp : OpenMP_Op<"atomic.read", [AllTypesMatch<["x", "v"]>,
                                            AtomicReadOpInterface]> {

  let summary = "performs an atomic read";

  let description = [{
    This operation performs an atomic read.

    The operand `x` is the address from where the value is atomically read.
    The operand `v` is the address where the value is stored after reading.

    `hint` is the value of hint (as specified in the hint clause). It is a
    compile time constant. As the name suggests, this is just a hint for
    optimization.

    `memory_order` indicates the memory ordering behavior of the construct. It
    can be one of `seq_cst`, `acquire` or `relaxed`.
  }];

  let arguments = (ins OpenMP_PointerLikeType:$x,
                       OpenMP_PointerLikeType:$v,
                       TypeAttr:$element_type,
                       DefaultValuedOptionalAttr<I64Attr, "0">:$hint_val,
                       OptionalAttr<MemoryOrderKindAttr>:$memory_order_val);
  let assemblyFormat = [{
    $v `=` $x
    oilist( `memory_order` `(` custom<ClauseAttr>($memory_order_val) `)`
          | `hint` `(` custom<SynchronizationHint>($hint_val) `)`)
    `:` type($x) `,` $element_type attr-dict
  }];
  let hasVerifier = 1;
  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      assert(getX() && "expected 'x' operand");
      assert(getV() && "expected 'v' operand");
      return 2;
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      assert(i < 2 && "invalid index position for an operand");
      return i == 0 ? getX() : getV();
    }
  }];
}

def AtomicWriteOp : OpenMP_Op<"atomic.write", [AtomicWriteOpInterface]> {

  let summary = "performs an atomic write";

  let description = [{
    This operation performs an atomic write.

    The operand `x` is the address to where the `expr` is atomically
    written w.r.t. multiple threads. The evaluation of `expr` need not be
    atomic w.r.t. the write to address. In general, the type(x) must
    dereference to type(expr).

    `hint` is the value of hint (as specified in the hint clause). It is a
    compile time constant. As the name suggests, this is just a hint for
    optimization.

    `memory_order` indicates the memory ordering behavior of the construct. It
    can be one of `seq_cst`, `release` or `relaxed`.
  }];

  let arguments = (ins OpenMP_PointerLikeType:$x,
                       AnyType:$expr,
                       DefaultValuedOptionalAttr<I64Attr, "0">:$hint_val,
                       OptionalAttr<MemoryOrderKindAttr>:$memory_order_val);
  let assemblyFormat = [{
    $x `=` $expr
    oilist( `hint` `(` custom<SynchronizationHint>($hint_val) `)`
          | `memory_order` `(` custom<ClauseAttr>($memory_order_val) `)`)
    `:` type($x) `,` type($expr)
    attr-dict
  }];
  let hasVerifier = 1;
  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      assert(getX() && "expected address operand");
      assert(getExpr() && "expected value operand");
      return 2;
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      assert(i < 2 && "invalid index position for an operand");
      return i == 0 ? getX() : getExpr();
    }
  }];
}

def AtomicUpdateOp : OpenMP_Op<"atomic.update",
                               [SingleBlockImplicitTerminator<"YieldOp">,
                                RecursiveMemoryEffects,
                                AtomicUpdateOpInterface]> {

  let summary = "performs an atomic update";

  let description = [{
    This operation performs an atomic update.

    The operand `x` is exactly the same as the operand `x` in the OpenMP
    Standard (OpenMP 5.0, section 2.17.7). It is the address of the variable
    that is being updated. `x` is atomically read/written.

    `hint` is the value of hint (as used in the hint clause). It is a compile
    time constant. As the name suggests, this is just a hint for optimization.

    `memory_order` indicates the memory ordering behavior of the construct. It
    can be one of `seq_cst`, `release` or `relaxed`.

    The region describes how to update the value of `x`. It takes the value at
    `x` as an input and must yield the updated value. Only the update to `x` is
    atomic. Generally the region must have only one instruction, but can
    potentially have more than one instructions too. The update is sematically
    similar to a compare-exchange loop based atomic update.

    The syntax of atomic update operation is different from atomic read and
    atomic write operations. This is because only the host dialect knows how to
    appropriately update a value. For example, while generating LLVM IR, if
    there are no special `atomicrmw` instructions for the operation-type
    combination in atomic update, a compare-exchange loop is generated, where
    the core update operation is directly translated like regular operations by
    the host dialect. The front-end must handle semantic checks for allowed
    operations.
  }];

  let arguments = (ins Arg<OpenMP_PointerLikeType,
                           "Address of variable to be updated",
                           [MemRead, MemWrite]>:$x,
                       DefaultValuedOptionalAttr<I64Attr, "0">:$hint_val,
                       OptionalAttr<MemoryOrderKindAttr>:$memory_order_val);
  let regions = (region SizedRegion<1>:$region);
  let assemblyFormat = [{
    oilist( `memory_order` `(` custom<ClauseAttr>($memory_order_val) `)`
          | `hint` `(` custom<SynchronizationHint>($hint_val) `)`)
    $x `:` type($x) $region attr-dict
  }];
  let hasVerifier = 1;
  let hasRegionVerifier = 1;
  let hasCanonicalizeMethod = 1;
  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      assert(getX() && "expected 'x' operand");
      return 1;
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      assert(i == 0 && "invalid index position for an operand");
      return getX();
    }
  }];
}

def AtomicCaptureOp : OpenMP_Op<"atomic.capture",
    [SingleBlockImplicitTerminator<"TerminatorOp">,
     RecursiveMemoryEffects, AtomicCaptureOpInterface]> {
  let summary = "performs an atomic capture";
  let description = [{
    This operation performs an atomic capture.

    `hint` is the value of hint (as used in the hint clause). It is a compile
    time constant. As the name suggests, this is just a hint for optimization.

    `memory_order` indicates the memory ordering behavior of the construct. It
    can be one of `seq_cst`, `acq_rel`, `release`, `acquire` or `relaxed`.

    The region has the following allowed forms:

    ```
      omp.atomic.capture {
        omp.atomic.update ...
        omp.atomic.read ...
        omp.terminator
      }

      omp.atomic.capture {
        omp.atomic.read ...
        omp.atomic.update ...
        omp.terminator
      }

      omp.atomic.capture {
        omp.atomic.read ...
        omp.atomic.write ...
        omp.terminator
      }
    ```

  }];

  let arguments = (ins DefaultValuedOptionalAttr<I64Attr, "0">:$hint_val,
                       OptionalAttr<MemoryOrderKindAttr>:$memory_order_val);
  let regions = (region SizedRegion<1>:$region);
  let assemblyFormat = [{
    oilist(`memory_order` `(` custom<ClauseAttr>($memory_order_val) `)`
          |`hint` `(` custom<SynchronizationHint>($hint_val) `)`)
    $region attr-dict
  }];
  let hasRegionVerifier = 1;
  let hasVerifier = 1;
  let extraClassDeclaration = [{
    /// Returns the `atomic.read` operation inside the region, if any.
    /// Otherwise, it returns nullptr.
    AtomicReadOp getAtomicReadOp();

    /// Returns the `atomic.write` operation inside the region, if any.
    /// Otherwise, it returns nullptr.
    AtomicWriteOp getAtomicWriteOp();

    /// Returns the `atomic.update` operation inside the region, if any.
    /// Otherwise, it returns nullptr.
    AtomicUpdateOp getAtomicUpdateOp();
  }];
}

//===----------------------------------------------------------------------===//
// [5.1] 2.21.2 threadprivate Directive
//===----------------------------------------------------------------------===//

def ThreadprivateOp : OpenMP_Op<"threadprivate",
                                [AllTypesMatch<["sym_addr", "tls_addr"]>]> {
  let summary = "threadprivate directive";
  let description = [{
    The threadprivate directive specifies that variables are replicated, with
    each thread having its own copy.

    The current implementation uses the OpenMP runtime to provide thread-local
    storage (TLS). Using the TLS feature of the LLVM IR will be supported in
    future.

    This operation takes in the address of a symbol that represents the original
    variable and returns the address of its TLS. All occurrences of
    threadprivate variables in a parallel region should use the TLS returned by
    this operation.

    The `sym_addr` refers to the address of the symbol, which is a pointer to
    the original variable.
  }];

  let arguments = (ins OpenMP_PointerLikeType:$sym_addr);
  let results = (outs OpenMP_PointerLikeType:$tls_addr);
  let assemblyFormat = [{
    $sym_addr `:` type($sym_addr) `->` type($tls_addr) attr-dict
  }];
  let extraClassDeclaration = [{
    /// The number of variable operands.
    unsigned getNumVariableOperands() {
      assert(getSymAddr() && "expected one variable operand");
      return 1;
    }

    /// The i-th variable operand passed.
    Value getVariableOperand(unsigned i) {
      assert(i == 0 && "invalid index position for an operand");
      return getSymAddr();
    }
  }];
}

//===----------------------------------------------------------------------===//
// 2.18.1 Cancel Construct
//===----------------------------------------------------------------------===//
def CancelOp : OpenMP_Op<"cancel"> {
  let summary = "cancel directive";
  let description = [{
    The cancel construct activates cancellation of the innermost enclosing
    region of the type specified.
  }];
  let arguments = (ins CancellationConstructTypeAttr:$cancellation_construct_type_val,
                       Optional<I1>:$if_expr);
  let assemblyFormat = [{ `cancellation_construct_type` `(`
                          custom<ClauseAttr>($cancellation_construct_type_val) `)`
                          ( `if` `(` $if_expr^ `)` )? attr-dict}];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.18.2 Cancellation Point Construct
//===----------------------------------------------------------------------===//
def CancellationPointOp : OpenMP_Op<"cancellation_point"> {
  let summary = "cancellation point directive";
  let description = [{
    The cancellation point construct introduces a user-defined cancellation
    point at which implicit or explicit tasks check if cancellation of the
    innermost enclosing region of the type specified has been activated.
  }];
  let arguments = (ins CancellationConstructTypeAttr:$cancellation_construct_type_val);
  let assemblyFormat = [{ `cancellation_construct_type` `(`
                           custom<ClauseAttr>($cancellation_construct_type_val) `)`
                           attr-dict}];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.19.5.7 declare reduction Directive
//===----------------------------------------------------------------------===//

def DeclareReductionOp : OpenMP_Op<"declare_reduction", [Symbol,
                                                         IsolatedFromAbove]> {
  let summary = "declares a reduction kind";

  let description = [{
    Declares an OpenMP reduction kind. This requires two mandatory and one
    optional region.

      1. The initializer region specifies how to initialize the thread-local
         reduction value. This is usually the neutral element of the reduction.
         For convenience, the region has an argument that contains the value
         of the reduction accumulator at the start of the reduction. It is
         expected to `omp.yield` the new value on all control flow paths.
      2. The reduction region specifies how to combine two values into one, i.e.
         the reduction operator. It accepts the two values as arguments and is
         expected to `omp.yield` the combined value on all control flow paths.
      3. The atomic reduction region is optional and specifies how two values
         can be combined atomically given local accumulator variables. It is
         expected to store the combined value in the first accumulator variable.

    Note that the MLIR type system does not allow for type-polymorphic
    reductions. Separate reduction declarations should be created for different
    element and accumulator types.

    For initializer and reduction regions, the operand to `omp.yield` must
    match the parent operation's results.
  }];

  let arguments = (ins SymbolNameAttr:$sym_name,
                       TypeAttr:$type);

  let regions = (region AnyRegion:$initializerRegion,
                        AnyRegion:$reductionRegion,
                        AnyRegion:$atomicReductionRegion);

  let assemblyFormat = "$sym_name `:` $type attr-dict-with-keyword "
                       "`init` $initializerRegion "
                       "`combiner` $reductionRegion "
                       "custom<AtomicReductionRegion>($atomicReductionRegion)";

  let extraClassDeclaration = [{
    PointerLikeType getAccumulatorType() {
      if (getAtomicReductionRegion().empty())
        return {};

      return cast<PointerLikeType>(getAtomicReductionRegion().front().getArgument(0).getType());
    }
  }];
  let hasRegionVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 2.19.5.4 reduction clause
//===----------------------------------------------------------------------===//

def ReductionOp : OpenMP_Op<"reduction"> {
  let summary = "reduction construct";
  let description = [{
    Indicates the value that is produced by the current reduction-participating
    entity for a reduction requested in some ancestor. The reduction is
    identified by the accumulator, but the value of the accumulator may not be
    updated immediately.
  }];

  let arguments= (ins AnyType:$operand, OpenMP_PointerLikeType:$accumulator);
  let assemblyFormat = [{
    $operand `,` $accumulator attr-dict `:` type($operand) `,` type($accumulator)
  }];
  let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// 8.2 requires directive
//===----------------------------------------------------------------------===//

// atomic_default_mem_order clause values not defined here because they can be
// represented by the OMPC_MemoryOrder enumeration instead.
def ClauseRequiresNone : I32BitEnumAttrCaseNone<"none">;
def ClauseRequiresReverseOffload : I32BitEnumAttrCaseBit<"reverse_offload", 0>;
def ClauseRequiresUnifiedAddress : I32BitEnumAttrCaseBit<"unified_address", 1>;
def ClauseRequiresUnifiedSharedMemory
    : I32BitEnumAttrCaseBit<"unified_shared_memory", 2>;
def ClauseRequiresDynamicAllocators
    : I32BitEnumAttrCaseBit<"dynamic_allocators", 3>;

def ClauseRequires : I32BitEnumAttr<
    "ClauseRequires",
    "requires clauses",
    [
      ClauseRequiresNone,
      ClauseRequiresReverseOffload,
      ClauseRequiresUnifiedAddress,
      ClauseRequiresUnifiedSharedMemory,
      ClauseRequiresDynamicAllocators
    ]> {
  let genSpecializedAttr = 0;
  let cppNamespace = "::mlir::omp";
}
def ClauseRequiresAttr :
  EnumAttr<OpenMP_Dialect, ClauseRequires, "clause_requires"> {
}

#endif // OPENMP_OPS