File: BuildIR.h

package info (click to toggle)
intel-graphics-compiler2 2.24.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 113,504 kB
  • sloc: cpp: 812,849; lisp: 288,219; ansic: 102,423; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 318; sh: 162; makefile: 38
file content (2469 lines) | stat: -rw-r--r-- 109,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#ifndef _BUILDIR_H_
#define _BUILDIR_H_

#include <cstdarg>
#include <list>
#include <map>
#include <optional>
#include <set>
#include <string>

#include "Assertions.h"
#include "BitSet.h"
#include "Common_ISA_util.h"
#include "G4_IR.hpp"
#include "G4_Kernel.hpp"
#include "InstSplit.h"
#include "PreDefinedVars.h"
#include "RT_Jitter_Interface.h"
#include "inc/common/sku_wa.h"
#include "visa_igc_common_header.h"
#include "visa_wa.h"
#include "FrequencyInfo.h"

#define MAX_DWORD_VALUE 0x7fffffff
#define MIN_DWORD_VALUE 0x80000000
#define MAX_UDWORD_VALUE 0xffffffff
#define MIN_UDWORD_VALUE 0
#define MAX_WORD_VALUE 32767
#define MIN_WORD_VALUE -32768
#define MAX_UWORD_VALUE 65535
#define MIN_UWORD_VALUE 0
#define MAX_CHAR_VALUE 127
#define MIN_CHAR_VALUE -128
#define MAX_UCHAR_VALUE 255
#define MIN_UCHAR_VALUE 0

typedef struct FCCalls {
  // callOffset is in inst number units
  unsigned int callOffset;
  const char *calleeLabelString;
} FCCalls;

enum DeclareType {
  Regular = 0,
  Fill = 1,
  Spill = 2,
  Tmp = 3,
  AddrSpill = 4,
  CoalescedSpillFill = 5,
};

// forward declaration
// FIXME: our #include is a mess, need to clean it up
class CISA_IR_Builder;

namespace vISA {
// IR_Builder class has a member of type FCPatchingInfo
// as a member. This class is expected to hold all FC
// related information.
class FCPatchingInfo {
private:
  // Flag to tell if this instance has any fast-composite
  // type calls. Callees for such call instructions are not available
  // in same compilation unit.
  bool hasFCCalls;

  // Set to true if kernel has "Callable" attribute set in VISA
  // stream.
  bool isFCCallableKernel;

  // Set to true if kernel was compiled with /mCM_composable_kernel
  // FE flag.
  bool isFCComposableKernel;

  // Set to true if kernel has "Entry" attribute set in VISA
  // stream.
  bool isFCEntryKernel;

  std::vector<FCCalls *> FCCallsToPatch;
  std::vector<unsigned int> FCReturnOffsetsToPatch;

public:
  FCPatchingInfo() {
    hasFCCalls = false;
    isFCCallableKernel = false;
    isFCComposableKernel = false;
    isFCEntryKernel = false;
  }

  void setHasFCCalls(bool hasFC) { hasFCCalls = hasFC; }
  bool getHasFCCalls() const { return hasFCCalls; }
  void setIsCallableKernel(bool value) { isFCCallableKernel = value; }
  bool getIsCallableKernel() const { return isFCCallableKernel; }
  void setFCComposableKernel(bool value) { isFCComposableKernel = value; }
  bool getFCComposableKernel() const { return isFCComposableKernel; }
  void setIsEntryKernel(bool value) { isFCEntryKernel = value; }
  bool getIsEntryKernel() const { return isFCEntryKernel; }
  std::vector<FCCalls *> &getFCCallsToPatch() { return FCCallsToPatch; }
  std::vector<unsigned int> &getFCReturnsToPatch() {
    return FCReturnOffsetsToPatch;
  }

  enum RegAccessType : unsigned char {
    Fully_Use = 0,
    Partial_Use = 1,
    Fully_Def = 2,
    Partial_Def = 3
  };

  enum RegAccessPipe : unsigned char {
    Pipe_ALU = 0,
    Pipe_Math = 1,
    Pipe_Send = 2,
    Pipe_Dpas = 3
  };

  struct RegAccess {
    RegAccess *Next;    // The next access on the same GRF.
    RegAccessType Type; // 'def' or 'use' of that GRF.
    unsigned RegNo;     // GRF.
    unsigned Pipe;      // Pipe.
    // where that access is issued.
    G4_INST *Inst;   // Where that GRF is accessed.
    unsigned Offset; // Instruction offset populated finally.
    // Token associated with that access.
    unsigned Token; // Optional token allocation associated to 'def'.

    RegAccess()
        : Next(nullptr), Type(Fully_Use), RegNo(0), Pipe(0), Inst(nullptr),
          Offset(0), Token(0) {}
  };

  // FIXME: Need to consider the pipeline ID together with GRF since
  // different pipe will use/def out-of-order. Need to synchronize all of
  // them to resolve the dependency.

  std::list<RegAccess> RegFirstAccessList;
  std::list<RegAccess> RegLastAccessList;
  // Per GRF, the first access.
  std::map<unsigned, RegAccess *> RegFirstAccessMap;
  // Per GRF, the last access.
  std::map<unsigned, RegAccess *> RegLastAccessMap;
  // Note that tokens recorded are tokens allocated but not used in last
  // access list.
  std::set<unsigned> AllocatedToken; // Allocated token.
};
} // namespace vISA

namespace vISA {
//
// hash table for holding reg vars and reg region
//

class OperandHashTable {
  Mem_Manager &mem;

  struct ImmKey {
    int64_t val;
    G4_Type valType;
    ImmKey(int64_t imm, G4_Type type) : val(imm), valType(type) {}
    bool operator==(const ImmKey &imm) const {
      return val == imm.val && valType == imm.valType;
    }
  };

  struct ImmKeyHash {
    std::size_t operator()(const ImmKey &imm) const {
      return (std::size_t)(imm.val ^ imm.valType);
    }
  };

  struct stringCompare {
    bool operator()(const char *s1, const char *s2) const {
      return strcmp(s1, s2) == 0;
    }
  };

  std::unordered_map<ImmKey, G4_Imm *, ImmKeyHash> immTable;
  std::unordered_map<const char *, G4_Label *, std::hash<const char *>,
                     stringCompare>
      labelTable;

public:
  OperandHashTable(Mem_Manager &m) : mem(m) {}

  // Generic methods that work on both integer and floating-point types.
  // For floating-point types, 'imm' needs to be G4_Imm(<float-value>.getImm().
  G4_Imm *lookupImm(int64_t imm, G4_Type ty);
  G4_Imm *createImm(int64_t imm, G4_Type ty);
};

//
// place for holding all region descriptions
//
class RegionPool {
  Mem_Manager &mem;
  std::vector<RegionDesc *> rgnlist;

public:
  RegionPool(Mem_Manager &m) : mem(m) {}
  const RegionDesc *createRegion(uint16_t vstride, uint16_t width,
                                 uint16_t hstride);
};

//
// place for holding all declares.
//
class DeclarePool {
  Mem_Manager &mem;
  const IR_Builder &irb;
  std::vector<G4_Declare *> dcllist;
  int addrSpillLocCount; // incremented in G4_RegVarAddrSpillLoc()
public:
  DeclarePool(Mem_Manager &m, const IR_Builder &builder)
      : mem(m), irb(builder), addrSpillLocCount(0) {
    dcllist.reserve(2048);
  }
  ~DeclarePool();
  DeclarePool(const DeclarePool&) = delete;
  DeclarePool& operator=(const DeclarePool&) = delete;

  G4_Declare *cloneDeclare(G4_Kernel &kernel,
                           std::map<G4_Declare *, G4_Declare *> &dclMap,
                           const char *newDclName, G4_Declare *dcl);

  G4_Declare *createDeclare(const char *name, G4_RegFileKind regFile,
                            unsigned short nElems, unsigned short nRows,
                            G4_Type ty, DeclareType kind = Regular,
                            G4_RegVar *base = nullptr,
                            G4_Operand *repRegion = nullptr,
                            G4_ExecSize execSize = G4_ExecSize(0));

  G4_Declare *createPreVarDeclare(PreDefinedVarsInternal index,
                                  unsigned short n_elems, unsigned short n_rows,
                                  G4_Type ty) {

    G4_Declare *dcl =
        new (mem) G4_Declare(irb, getPredefinedVarString(index), G4_INPUT,
                             n_elems * n_rows, ty, dcllist);
    G4_RegVar *regVar;
    regVar = new (mem) G4_RegVar(dcl, G4_RegVar::RegVarType::Default);
    dcl->setRegVar(regVar);

    return dcl;
  }

  std::vector<G4_Declare *> &getDeclareList() { return dcllist; }
};

//
// interface for creating operands and instructions
//
class IR_Builder {
public:
  const char *curFile = nullptr;
  unsigned int curLine = 0;
  int curCISAOffset = -1;

  static const int OrphanVISAIndex = 0xffffffff;
  int debugInfoPlaceholder =
      OrphanVISAIndex; // used for debug info, catch all VISA offset for orphan
                       // instructions

private:
  class GlobalImmPool {
    struct ImmVal {
      G4_Imm *imm;
      int numElt;

      bool operator==(const ImmVal &v) const {
        return imm == v.imm && numElt == v.numElt;
      }
    };
    static const int MAX_POOL_SIZE =
        8; // reg pressure control, for now just do naive first-come first-serve
    std::array<ImmVal, MAX_POOL_SIZE> immArray = {};
    std::array<G4_Declare *, MAX_POOL_SIZE> dclArray = {};
    int curSize = 0;
    IR_Builder &builder;

  public:
    GlobalImmPool(IR_Builder &b) : builder(b) {}
    G4_Declare *addImmVal(G4_Imm *imm, int numElt);
    int size() const { return curSize; }
    const ImmVal &getImmVal(int i) { return immArray[i]; }
    G4_Declare *getImmDcl(int i) { return dclArray[i]; }
  };

  GlobalImmPool immPool;

  // allocator pools
  USE_DEF_ALLOCATOR useDefAllocator;

  FINALIZER_INFO *metaData = nullptr;
  // Use a BitSet to track the barrier IDs used
  BitSet usedBarriers;

  int subroutineId = -1;     // the kernel itself has id 0, as we always emit a
                             // subroutine label for kernel too
  enum VISA_BUILD_TYPE type = VISA_BUILD_TYPE::KERNEL; // as opposed to what?

  uint32_t m_next_local_label_id = 0;

  // pre-defined declare that binds to R0 (the entire GRF)
  // when pre-emption is enabled, builtinR0 is replaced by a temp,
  // and a move is inserted at kernel entry
  // mov (8) builtinR0 realR0
  G4_Declare *builtinR0 =
      nullptr; // this is either r0 or the temp if pre-emption is enabled
  G4_Declare *realR0 = nullptr; // this always refers to r0

  // pre-defined declare that binds to A0.0:ud
  G4_Declare *builtinA0 = nullptr;
  // pre-defined declare that binds to A0.2:ud
  G4_Declare *builtinA0Dot2 = nullptr; // used for splitsend's ext msg
                                       // descriptor
  // pre-defined declare that binds to HWTid (R0.5:ud)
  G4_Declare *builtinHWTID = nullptr;
  // pre-defined declare that binds to SR0.1:ud
  G4_Declare *builtinSR0Dot1 = nullptr;

  // pre-defined QWs of S0
  // 0 and 1 are reserved for Gather (indirect) Send usage.
  G4_Declare *builtinS0 = nullptr;

  // for rotating through the high subregisters of s0 in QW
  // we require QWs even for things like SLM or URB (which are a32 bases)
  // since the sendg instruction requires QW s0 subregs
  static const unsigned FIRST_SURFACE_S0_QW = 2; // s0.{0,1}:uq for ind. send
  unsigned nextSurfaceS0QW = FIRST_SURFACE_S0_QW;
  // pre-defined bindless surface index (252, 1 UD)
  G4_Declare *builtinT252 = nullptr;
  // pre-defined bindless sampler index (31, 1 UD)
  G4_Declare *builtinBindlessSampler = nullptr;
  // pre-defined sampler header
  G4_Declare *builtinSamplerHeader = nullptr;

  // common message header for spill/fill intrinsics
  // We put them here instead of spillManager since there may be multiple rounds
  // of spill, and we want to use a common header
  G4_Declare *spillFillHeader = nullptr;

  // for scatter spills
  G4_Declare *scatterSpillBaseAddress = nullptr;
  G4_Declare *scatterSpillAddress = nullptr;

  G4_Declare *oldA0Dot2Temp = nullptr;

  G4_Declare *builtinScratchSurface = nullptr;
  // if scratch surface is used, this will be initialized once at entry
  G4_Declare *scratchSurfaceOffset = nullptr;


  // The temp var for eu fusion W/A
  G4_Declare *euFusionWATmpVar = nullptr;

  // Indicates that sampler header cache (builtinSamplerHeader) is correctly
  // initialized with r0 contents.
  // Used only when vISA_cacheSamplerHeader option is set.
  bool builtinSamplerHeaderInitialized = false;
  // for PVC send WAR WA
  bool hasDF = false;
  // function call related declares
  G4_Declare *be_sp = nullptr;
  G4_Declare *be_fp = nullptr;
  // Part FDE inst is the move that stores r125.[0-3] to a temp.
  // This is used to restore ret %ip, ret EM, and BE ptrs.
  G4_INST *savePartFDInst = nullptr;
  // FDE spill inst is first spill instruction that writes frame
  // descriptor to stack.
  G4_INST *FDSpillInst = nullptr;
  // Instruction with ScratchSurfaceOffset
  G4_INST* instSSO = nullptr;
  G4_Declare *tmpFCRet = nullptr;
  // Used to store implicit arg, local id buffer ptrs for stackcall
  G4_Declare *implArgBufferPtr = nullptr;
  G4_Declare *localIdBufferPtr = nullptr;

  unsigned short arg_size;
  unsigned short return_var_size;

  unsigned int sampler8x8_group_id;

  // input declare of R1.
  G4_Declare *inputR1 = nullptr;

  // Populate this data structure so after compiling all kernels
  // in file, we can emit out patch file using this up-levelled
  // information.
  FCPatchingInfo *fcPatchInfo = nullptr;

  const WA_TABLE *m_pWaTable;
  Options *m_options = nullptr;

  std::map<const G4_INST *, G4_FCALL> m_fcallInfo;

  // Basic region descriptors.
  RegionDesc CanonicalRegionStride0, // <0; 1, 0>
      CanonicalRegionStride1,        // <1; 1, 0>
      CanonicalRegionStride2,        // <2; 1, 0>
      CanonicalRegionStride4;        // <4; 1, 0>

  // map of all stack functioncs ever invoked by this builder's kernel/function
  std::map<std::string, G4_Label *> m_fcallLabels;

  G4_Label *getFcallLabel(const std::string &str) {
    auto it = m_fcallLabels.find(str);
    if (it == m_fcallLabels.end()) {
      auto label = createLabel(str, LABEL_FUNCTION);
      m_fcallLabels[str] = label;
      return label;
    }
    return it->second;
  }

  class PreDefinedVars {
  public:
    void setHasPredefined(PreDefinedVarsInternal id, bool val) {
      vASSERT(id < PreDefinedVarsInternal::VAR_LAST);
      hasPredefined[static_cast<int>(id)] = val;
    }

    bool isHasPredefined(PreDefinedVarsInternal id) const {
      vASSERT(id < PreDefinedVarsInternal::VAR_LAST);
      return hasPredefined[static_cast<int>(id)];
    }

    void setPredefinedVar(PreDefinedVarsInternal id, G4_Declare *dcl) {
      vASSERT(id < PreDefinedVarsInternal::VAR_LAST);
      predefinedVars[static_cast<int>(id)] = dcl;
    }

    G4_Declare *getPreDefinedVar(PreDefinedVarsInternal id) const {
      vASSERT(id < PreDefinedVarsInternal::VAR_LAST);
      if (id >= PreDefinedVarsInternal::VAR_LAST) {
        return nullptr;
      }
      return predefinedVars[static_cast<int>(id)];
    }

  private:
    // records whether a vISA pre-defined var is used by the kernel
    // some predefined need to be expanded (e.g., HWTid, color)
    bool hasPredefined[static_cast<int>(PreDefinedVarsInternal::VAR_LAST)]{};
    G4_Declare
        *predefinedVars[static_cast<int>(PreDefinedVarsInternal::VAR_LAST)]{};
  };

  bool hasNullReturnSampler = false;

  const CISA_IR_Builder *parentBuilder = nullptr;

  // stores all metadata ever allocated
  Mem_Manager metadataMem = 4096;
  std::vector<Metadata *> allMDs;
  std::vector<MDNode *> allMDNodes;

  FrequencyInfo freqInfoManager;

  // bump pointer allocator for variable and label names, used for IR dump only.
  Mem_Manager debugNameMem = 4096;

  // Whether the kernel should avoid clobbering or even reading R0, this
  // is generally due to mid-thread preemption.
  // If mode is read-only, we mark it as forbidden in various RA algorithms so
  // that it will not be re-assigned to other variables.
  // If mode is none, we copy it to a different register at kernel entry and use
  // that in the body instead. r0 should not be used outside the prolog.
  enum class R0_ACCESS { READ_WRITE, READ_ONLY, NONE };
  // This unfortunately can't be const due to some context-dependent WA that can
  // only be set after all instructions are read.
  R0_ACCESS r0AccessMode;
  R0_ACCESS getR0AccessFromOptions() const;

public:
  PreDefinedVars preDefVars;
  Mem_Manager &mem;           // memory for all operands and insts
  PhyRegPool phyregpool;      // all physical regs
  OperandHashTable hashtable; // all created region operands
  RegionPool rgnpool;         // all region description
  DeclarePool dclpool;        // all created decalres
  INST_LIST instList;         // all created insts
  // list of instructions ever allocated
  // This list may only grow and is freed when IR_Builder is destroyed
  std::vector<G4_INST *> instAllocList;
  G4_Kernel &kernel;
  // the following fileds are used for dcl name when a new dcl is created.
  // number of predefined variables are included.
  unsigned num_temp_dcl;
  // number of temp GRF vars created to hold spilled addr/flag
  uint32_t numAddrFlagSpillLoc = 0;
  std::vector<input_info_t *> m_inputVect;
  BitSet src1FirstGRFOfLastDpas;

  const Options *getOptions() const { return m_options; }
  bool getOption(vISAOptions opt) const { return m_options->getOption(opt); }
  uint32_t getuint32Option(vISAOptions opt) const {
    return m_options->getuInt32Option(opt);
  }
  void getOption(vISAOptions opt, const char *&str) const {
    return m_options->getOption(opt, str);
  }
  void addInputArg(input_info_t *inpt);
  input_info_t *getInputArg(unsigned int index) const;
  unsigned int getInputCount() const;
  input_info_t *getRetIPArg() const;

  const CISA_IR_Builder *getParent() const { return parentBuilder; }

  void dump(std::ostream &os); // not const because G4_INST::emit isn't :(

  std::stringstream &criticalMsgStream();

  const USE_DEF_ALLOCATOR &getAllocator() const { return useDefAllocator; }

  // Getter/setter for be_sp and be_fp
  G4_Declare *getBESP() {
    if (be_sp == NULL) {
      be_sp = createDeclare("be_sp", G4_GRF, 1, 1, Type_UD);
      be_sp->getRegVar()->setPhyReg(phyregpool.getGreg(kernel.stackCall.getFPSPGRF()),
                                    kernel.stackCall.subRegs.BE_SP);
    }

    return be_sp;
  }

  G4_Declare *getBEFP() {
    if (be_fp == NULL) {
      be_fp = createDeclare("be_fp", G4_GRF, 1, 1, Type_UD);
      be_fp->getRegVar()->setPhyReg(phyregpool.getGreg(kernel.stackCall.getFPSPGRF()),
                                    kernel.stackCall.subRegs.BE_FP);
    }

    return be_fp;
  }

  G4_INST *getPartFDSaveInst() const { return savePartFDInst; }
  void setPartFDSaveInst(G4_INST *i) { savePartFDInst = i; }

  G4_INST *getFDSpillInst() const { return FDSpillInst; }
  void setFDSpillInst(G4_INST *i) { FDSpillInst = i; }

  // Getter/settter for instruction with ScratchSurfaceOffset
  G4_INST* getSSOInst() const { return instSSO; }
  void setSSOInst(G4_INST* inst) { instSSO = inst; }

  G4_Declare *getStackCallArg() const {
    return preDefVars.getPreDefinedVar(PreDefinedVarsInternal::ARG);
  }
  G4_Declare *getStackCallRet() const {
    return preDefVars.getPreDefinedVar(PreDefinedVarsInternal::RET);
  }

  G4_Declare *getFE_SP() const {
    return preDefVars.getPreDefinedVar(PreDefinedVarsInternal::FE_SP);
  }

  G4_Declare *getFE_FP() const {
    return preDefVars.getPreDefinedVar(PreDefinedVarsInternal::FE_FP);
  }

  G4_Declare *getImplArgBufPtr() const {
    return preDefVars.getPreDefinedVar(
        PreDefinedVarsInternal::IMPL_ARG_BUF_PTR);
  }

  G4_Declare *getLocalIdBufPtr() const {
    return preDefVars.getPreDefinedVar(
        PreDefinedVarsInternal::LOCAL_ID_BUF_PTR);
  }

  bool isPreDefArg(G4_Declare *dcl) const { return dcl == getStackCallArg(); }

  bool isPreDefRet(G4_Declare *dcl) const { return dcl == getStackCallRet(); }

  bool isPreDefFEStackVar(G4_Declare *dcl) const {
    return dcl == getFE_SP() || dcl == getFE_FP();
  }

  bool isPreDefSpillHeader(G4_Declare *dcl) const {
    return dcl == getImplArgBufPtr() || dcl == getLocalIdBufPtr();
  }

  // this refers to vISA's internal stack for spill and caller/callee-save
  // Note that this is only valid after CFG is constructed
  // ToDo: make this a pass?
  bool usesStack() const {
    return kernel.fg.getHasStackCalls() || kernel.fg.getIsStackCallFunc();
  }

  void bindInputDecl(G4_Declare *dcl, int grfOffset);

  uint32_t getPerThreadInputSize() const {
    return kernel.getInt32KernelAttr(Attributes::ATTR_PerThreadInputSize);
  }

  int32_t getCrossThreadInputSize() const {
    return kernel.getInt32KernelAttr(Attributes::ATTR_CrossThreadInputSize);
  }

  bool getLTOInvokeOptTarget() const {
    return kernel.getBoolKernelAttr(Attributes::ATTR_LTOInvokeOptTarget);
  }

  bool isGRFDstAligned(G4_Operand *dst, int alignByte) const;

  //
  // Check if opnd is or can be made "alignByte"-byte aligned.
  // These functions will change the underlying variable's alignment
  // (e.g., make a scalar variable GRF-aligned) when possible to satisfy
  // the alignment
  bool tryToAlignOperand(G4_Operand *opnd, int alignByte) const;
  bool tryToAlignOperand(G4_Operand *opnd, unsigned short &offset,
                         int alignByte) const;

  void setType(enum VISA_BUILD_TYPE _type) { type = _type; }
  bool getIsKernel() const { return type == VISA_BUILD_TYPE::KERNEL; }
  bool getIsFunction() const { return type == VISA_BUILD_TYPE::FUNCTION; }
  bool getIsPayload() const { return type == VISA_BUILD_TYPE::PAYLOAD; }
  enum VISA_BUILD_TYPE getType() const { return type; }
  void predefinedVarRegAssignment(uint8_t inputSize);
  void expandPredefinedVars();
  void setArgSize(unsigned short size) { arg_size = size; }
  unsigned short getArgSize() const { return arg_size; }
  void setRetVarSize(unsigned short size) { return_var_size = size; }
  unsigned short getRetVarSize() const { return return_var_size; }
  unsigned int getCallRetOpndSize() const;

  FCPatchingInfo *getFCPatchInfo();

  const WA_TABLE *getPWaTable() const { return m_pWaTable; }

  const char *getNameString(size_t size, const char *format, ...);

  G4_Predicate_Control
  vISAPredicateToG4Predicate(VISA_PREDICATE_CONTROL control,
                             G4_ExecSize execSize);

  std::optional<G4_FCALL> getFcallInfo(const G4_INST *inst) const;
  void addFcallInfo(const G4_INST *FcallInst, uint16_t ArgSize,
                    uint16_t RetSize, bool isUniform) {
    m_fcallInfo.emplace(FcallInst, G4_FCALL(ArgSize, RetSize, isUniform));
  }

  // If this is true (detected in TranslateInterface.cpp), we need a sampler
  // flush before EOT
  bool getHasNullReturnSampler() const { return hasNullReturnSampler; }

  //    Initializes predefined vars for all the vISA versions
  void createPreDefinedVars();

  void createBuiltinDecls();

  G4_Declare *getSpillFillHeader();
  bool hasValidSpillFillHeader() { return spillFillHeader; }

  G4_Declare *getScatterSpillBaseAddress();
  G4_Declare *getScatterSpillAddress();

  G4_Declare *getEUFusionWATmpVar();

  G4_Declare *getOldA0Dot2Temp();
  bool hasValidOldA0Dot2() { return oldA0Dot2Temp; }
  bool hasDFInst() const { return hasDF; }

  IR_Builder(INST_LIST_NODE_ALLOCATOR &alloc, G4_Kernel &k, Mem_Manager &m,
             Options *options, CISA_IR_Builder *parent, FINALIZER_INFO *jitInfo,
             const WA_TABLE *pWaTable);

  ~IR_Builder();
  IR_Builder(const IR_Builder&) = delete;
  IR_Builder& operator=(const IR_Builder&) = delete;

  void setR0ReadOnly() {
    // NONE is stronger than READ_ONLY, so don't change it
    if (r0AccessMode == R0_ACCESS::READ_WRITE)
      r0AccessMode = R0_ACCESS::READ_ONLY;
  }
  bool canReadR0() const { return r0AccessMode != R0_ACCESS::NONE; }
  bool canWriteR0() const { return r0AccessMode == R0_ACCESS::READ_WRITE; }
  bool mustReserveR1() const;

  void rebuildPhyRegPool(unsigned int numRegisters) {
    phyregpool.rebuildRegPool(mem, numRegisters);
  }

  TARGET_PLATFORM getPlatform() const { return kernel.getPlatform(); }
  PlatformGen getPlatformGeneration() const {
    return kernel.getPlatformGeneration();
  }
  const char *getGenxPlatformString() const {
    return kernel.getGenxPlatformString();
  }
  unsigned getACCSize() const { return getGRFSize(); }
  unsigned char getGRFSize() const { return kernel.getGRFSize(); }
  template <G4_Type T> unsigned numEltPerGRF() const {
    return kernel.numEltPerGRF<T>();
  }
  unsigned numEltPerGRF(G4_Type t) const { return kernel.numEltPerGRF(t); }
  unsigned getMaxVariableSize() const { return kernel.getMaxVariableSize(); }
  G4_SubReg_Align getGRFAlign() const { return kernel.getGRFAlign(); }
  G4_SubReg_Align getHalfGRFAlign() const { return kernel.getHalfGRFAlign(); }
  unsigned getGenxDataportIOSize() const {
    return kernel.getGenxDataportIOSize();
  }
  unsigned getGenxSamplerIOSize() const {
    return kernel.getGenxSamplerIOSize();
  }
  FINALIZER_INFO *getJitInfo() { return metaData; }
  BitSet &usedBarries() { return usedBarriers; }
  // Return the max id set + 1 as the number of barriers used. Ideally the
  // number of bits set can be used to represent the number of barriers.
  // However, In current programming model the barriers should be allocated
  // sequentially, so here we return max id + 1 to make sure of that.
  unsigned numBarriers() const {
    int maxId = usedBarriers.findLastIn(0, kernel.getMaxNumOfBarriers());
    // maxId + 1 would also cover the case, which returns -1, that no bits
    // are set.
    return maxId + 1;
  }

  void updateNamedBarrier(G4_Operand *barrierId);

  G4_Declare *cloneDeclare(std::map<G4_Declare *, G4_Declare *> &dclMap,
                           G4_Declare *dcl);

  G4_Declare *createDeclare(const char *name, G4_RegFileKind regFile,
                            unsigned short n_elems, unsigned short n_rows,
                            G4_Type ty, DeclareType kind = Regular,
                            G4_RegVar *base = NULL,
                            G4_Operand *repRegion = NULL,
                            G4_ExecSize execSize = G4_ExecSize(0));

  G4_Declare *createPreVarDeclareNoLookup(PreDefinedVarsInternal index,
                                          unsigned short n_elems,
                                          unsigned short n_rows, G4_Type ty) {
    G4_Declare *dcl = dclpool.createPreVarDeclare(index, n_elems, n_rows, ty);
    kernel.Declares.push_back(dcl);
    return dcl;
  }

  G4_Declare *getBuiltinR0() { return builtinR0; }
  void setBuiltInR0(G4_Declare *dcl) { builtinR0 = dcl; }
  G4_Declare *getRealR0() const {
    return realR0;
  } // undefined terminology: what's "real" here (vs "builtin" above)?
  void setRealR0(G4_Declare *dcl) { realR0 = dcl; }
  G4_Declare *getBuiltinA0() { return builtinA0; }
  G4_Declare *getBuiltinA0Dot2() { return builtinA0Dot2; }
  G4_Declare *getBuiltinHWTID() const { return builtinHWTID; }
  G4_Declare *getBuiltinSR0Dot1() const { return builtinSR0Dot1; }

  // The first part of s0 is reserved for Xe3+ Gather Send (indirect send)
  // This tests if an operand refers to Gather Send or something else
  bool isBuiltinSendIndirectS0(G4_Operand *op) const;

  G4_Declare *getBuiltinT252() const { return builtinT252; }
  G4_Declare *getBuiltinBindlessSampler() const {
    return builtinBindlessSampler;
  }
  G4_Declare *getBuiltinSamplerHeader() const { return builtinSamplerHeader; }
  G4_Declare *getBuiltinS0() const { return builtinS0;}
  G4_Declare *getOldA0Dot2Temp() const { return oldA0Dot2Temp; }
  void setHasDFInst(bool b) { hasDF = b; }
  G4_Declare *getInputR1() { return inputR1; }
  void setInputR1(G4_Declare *r1) { inputR1 = r1; }

  bool isBindlessSampler(const G4_Operand *sampler) const {
    return sampler->isSrcRegRegion() &&
           sampler->getTopDcl() == getBuiltinBindlessSampler();
  }

  bool isBindlessSurface(const G4_Operand *bti) const {
    return bti->isSrcRegRegion() && bti->getTopDcl() == getBuiltinT252();
  }

  // IsSLMSurface - Check whether the given surface is SLM surface.
  static bool IsSLMSurface(const G4_Operand *surface) {
    // So far, it's only reliable to check an immediate surface.
    return surface->isImm() && surface->asImm()->getImm() == PREDEF_SURF_0;
  }

  static const G4_Declare *getDeclare(const G4_Operand *opnd) {
    const G4_Declare *dcl = opnd->getBase()->asRegVar()->getDeclare();

    while (const G4_Declare *parentDcl = dcl->getAliasDeclare())
      dcl = parentDcl;

    return dcl;
  }
  static G4_Declare *getDeclare(G4_Operand *opnd) {
    return const_cast<G4_Declare *>(getDeclare((const G4_Operand *)opnd));
  }

  bool shouldForceSplitSend(const G4_Operand *surface) const {
    return surface->isSrcRegRegion() &&
           (surface->getTopDcl() == getBuiltinT252() ||
            surface->getTopDcl() == getBuiltinScratchSurface());
  }

  /// getSplitEMask() calculates the new mask after splitting from the current
  /// execution mask at the given execution size.
  /// It only works with masks covering whole GRF and thus won't
  /// generate/consume nibbles.
  static uint32_t getSplitEMask(unsigned execSize, uint32_t eMask, bool isLo);
  static uint32_t getSplitLoEMask(unsigned execSize, uint32_t eMask) {
    return getSplitEMask(execSize, eMask, true);
  }
  static uint32_t getSplitHiEMask(unsigned execSize, uint32_t eMask) {
    return getSplitEMask(execSize, eMask, false);
  }

  bool isScratchSpace(G4_Operand *bti) const {
    return bti->isSrcRegRegion() && bti->getTopDcl() == builtinScratchSurface;
  }
  G4_Declare *getBuiltinScratchSurface() const { return builtinScratchSurface; }

  G4_SrcRegRegion *createScratchExDesc(uint32_t exdesc);

  void initScratchSurfaceOffset();
  void initAddressesForScatterSpills();

  G4_Declare *getSpillSurfaceOffset() { return scratchSurfaceOffset; }

  // create a new temp GRF with the specified type/size and undefined regions
  G4_Declare *createTempVar(unsigned int numElements, G4_Type type,
                            G4_SubReg_Align subAlign, const char *prefix = "TV",
                            bool appendIdToName = true);

  // create a new temp GRF as the home location of a spilled addr/flag dcl
  G4_Declare *createAddrFlagSpillLoc(G4_Declare *dcl);

  // like the above, but also mark the variable as don't spill
  // this is used for temp variables in macro sequences where spilling woul not
  // help
  // FIXME: can we somehow merge this with G4_RegVarTmp/G4_RegVarTransient?
  G4_Declare *createTempVarWithNoSpill(unsigned int numElements, G4_Type type,
                                       G4_SubReg_Align subAlign,
                                       const char *prefix = "TV") {
    G4_Declare *dcl = createTempVar(numElements, type, subAlign, prefix);
    dcl->setDoNotSpill();
    return dcl;
  }

  //
  // Create a declare that is hardwired to some phyiscal GRF.
  // It is useful to implement various workarounds post RA where we want to
  // directly address some physical GRF. regOff is in unit of the declare type.
  // caller is responsible for ensuring the resulting variable does not violate
  // any HW restrictions (e.g., operand does not cross two GRF)
  G4_Declare *createHardwiredDeclare(uint32_t numElements, G4_Type type,
                                     uint32_t regNum, uint32_t regOff);

  void createPseudoKills(std::initializer_list<G4_Declare *> dcls,
                         PseudoKillType ty);

  G4_INST *createPseudoKill(G4_Declare *dcl, PseudoKillType ty,
                            bool addToInstList);

  G4_INST *createEUWASpill(bool addToInstList);

  // numRows is in hword units
  // offset is in hword units
  G4_INST *createSpill(G4_DstRegRegion *dst, G4_SrcRegRegion *header,
                       G4_SrcRegRegion *payload, G4_ExecSize execSize,
                       uint16_t numRows, uint32_t offset, G4_Declare *fp,
                       G4_InstOption option, bool addToInstList,
                       bool isScatter = false);

  G4_INST *createSpill(G4_DstRegRegion *dst, G4_SrcRegRegion *payload,
                       G4_ExecSize execSize, uint16_t numRows, uint32_t offset,
                       G4_Declare *fp, G4_InstOption option, bool addToInstList,
                       bool isScatter = false);

  G4_INST *createFill(G4_SrcRegRegion *header, G4_DstRegRegion *dstData,
                      G4_ExecSize execSize, uint16_t numRows, uint32_t offset,
                      G4_Declare *fp, G4_InstOption option, bool addToInstList);
  G4_INST *createFill(G4_DstRegRegion *dstData, G4_ExecSize execSize,
                      uint16_t numRows, uint32_t offset, G4_Declare *fp,
                      G4_InstOption option, bool addToInstList);

  // numberOfFlags MEANS NUMBER OF WORDS (e.g., 1 means 16-bit), not number of
  // bits or number of data elements in operands.
  G4_Declare *createTempFlag(unsigned short numberOfFlags,
                             const char *prefix = "TEMP_FLAG_");

  // like above, but pass numFlagElements instead. This allows us to distinguish
  // between 1/8/16-bit flags, which are all allocated as a UW. name is
  // allocated by caller
  G4_Declare *createFlag(uint16_t numFlagElements, const char *name);

  G4_Declare *createTempScalar(uint16_t numFlagElements, const char *prefix);

  G4_Declare *createScalar(uint16_t numFlagElements, const char *name);

  G4_Declare *createTempAddress(uint16_t numAddressElements,
                                const char *prefix = "TEMP_ADDR");

  G4_Declare *createPreVar(PreDefinedVarsInternal preDefVar_index,
                           unsigned short numElements, G4_Type type);

  //
  // create <vstride; width, hstride>
  //
  // PLEASE use getRegion* interface to get regions if possible!
  // This function will be mostly used for external regions.
  const RegionDesc *createRegionDesc(uint16_t vstride, uint16_t width,
                                     uint16_t hstride) {
    return rgnpool.createRegion(vstride, width, hstride);
  }

  // Given the execution size and region parameters, create a region
  // descriptor.
  //
  // PLEASE use getRegion* interface to get regions if possible!
  // This function will be mostly used for external regions.
  const RegionDesc *createRegionDesc(uint16_t execSize, uint16_t vstride,
                                     uint16_t width, uint16_t hstride) {
    // Performs normalization for commonly used regions.
    switch (RegionDesc::getRegionDescKind(execSize, vstride, width, hstride)) {
    case RegionDesc::RK_Stride0:
      return getRegionScalar();
    case RegionDesc::RK_Stride1:
      return getRegionStride1();
    case RegionDesc::RK_Stride2:
      return getRegionStride2();
    case RegionDesc::RK_Stride4:
      return getRegionStride4();
    default:
      break;
    }
    return rgnpool.createRegion(vstride, width, hstride);
  }

  /// Helper to normalize an existing region descriptor.
  const RegionDesc *getNormalizedRegion(uint16_t execSize,
                                        const RegionDesc *rd) {
    return createRegionDesc(execSize, rd->vertStride, rd->width,
                            rd->horzStride);
  }

  /// Get the predefined region descriptors.
  const RegionDesc *getRegionScalar() const { return &CanonicalRegionStride0; }
  const RegionDesc *getRegionStride1() const { return &CanonicalRegionStride1; }
  const RegionDesc *getRegionStride2() const { return &CanonicalRegionStride2; }
  const RegionDesc *getRegionStride4() const { return &CanonicalRegionStride4; }

  // ToDo: get rid of this version and use the message type specific ones below
  // instead, so we can avoid having to explicitly create extDesc bits
  G4_SendDescRaw *createGeneralMsgDesc(uint32_t desc, uint32_t extDesc,
                                       SendAccess access,
                                       G4_Operand *bti = nullptr,
                                       G4_Operand *sti = nullptr);

  G4_SendDescRaw *createReadMsgDesc(SFID sfid, uint32_t desc,
                                    G4_Operand *bti = nullptr);

  G4_SendDescRaw *createWriteMsgDesc(SFID sfid, uint32_t desc, int src1Len,
                                     G4_Operand *bti = nullptr);

  G4_SendDescRaw *createSyncMsgDesc(SFID sfid, uint32_t desc);

  G4_SendDescRaw *createSampleMsgDesc(uint32_t desc, bool cps, int src1Len,
                                      G4_Operand *bti, G4_Operand *sti);

  static SendAccess getSendAccessType(bool isRead, bool isWrite) {
    if (isRead && isWrite) {
      return SendAccess::READ_WRITE;
    }
    return isRead ? SendAccess::READ_ONLY : SendAccess::WRITE_ONLY;
  }

  G4_SendDescRaw *createSendMsgDesc(SFID sfid, uint32_t desc, uint32_t extDesc,
                                    int src1Len, SendAccess access,
                                    G4_Operand *bti, G4_ExecSize execSize,
                                    bool isValidFuncCtrl = true);

  G4_SendDescRaw *createSendMsgDesc(SFID sfid, uint32_t desc, uint32_t extDesc,
                                    int src1Len, SendAccess access,
                                    G4_Operand *bti,
                                    bool isValidFuncCtrl = true);

  G4_SendDescRaw *createSendMsgDesc(unsigned funcCtrl, unsigned regs2rcv,
                                    unsigned regs2snd, SFID funcID,
                                    unsigned extMsgLength, uint16_t extFuncCtrl,
                                    SendAccess access,
                                    G4_Operand *bti = nullptr,
                                    G4_Operand *sti = nullptr);

  G4_Operand *emitSampleIndexGE16(G4_Operand *sampler, G4_Declare *headerDecl);

  //
  // deprecated, please use the one below
  //
  G4_SrcRegRegion *createSrcRegRegion(G4_SrcRegRegion &src) {
    G4_SrcRegRegion *rgn = new (mem) G4_SrcRegRegion(src);
    return rgn;
  }

  // Create a new srcregregion allocated in mem
  G4_SrcRegRegion *createSrc(G4_VarBase *b, short roff, short sroff,
                             const RegionDesc *rd, G4_Type ty,
                             G4_AccRegSel regSel = ACC_UNDEFINED) {
    return createSrcRegRegion(G4_SrcModifier::Mod_src_undef,
                              G4_RegAccess::Direct, b, roff, sroff, rd, ty,
                              regSel);
  }

  // deprecated, either use createSrc or createIndirectSrc
  G4_SrcRegRegion *createSrcRegRegion(G4_SrcModifier m, G4_RegAccess a,
                                      G4_VarBase *b, short roff, short sroff,
                                      const RegionDesc *rd, G4_Type ty,
                                      G4_AccRegSel regSel = ACC_UNDEFINED) {
    G4_SrcRegRegion *rgn =
        new (mem) G4_SrcRegRegion(*this, m, a, b, roff, sroff, rd, ty, regSel);
    return rgn;
  }

  G4_SrcRegRegion *createSrcWithNewRegOff(G4_SrcRegRegion *old,
                                          short newRegOff);
  G4_SrcRegRegion *createSrcWithNewSubRegOff(G4_SrcRegRegion *old,
                                             short newSubRegOff);
  G4_SrcRegRegion *createSrcWithNewBase(G4_SrcRegRegion *old,
                                        G4_VarBase *newBase);

  G4_SrcRegRegion *createIndirectSrc(G4_SrcModifier m, G4_VarBase *b,
                                     short roff, short sroff,
                                     const RegionDesc *rd, G4_Type ty,
                                     short immAddrOff) {
    G4_SrcRegRegion *rgn = new (mem) G4_SrcRegRegion(
        *this, m, IndirGRF, b, roff, sroff, rd, ty, ACC_UNDEFINED);
    rgn->setImmAddrOff(immAddrOff);
    return rgn;
  }

  //
  // deprecated, please use the version below
  //
  G4_DstRegRegion *createDstRegRegion(G4_DstRegRegion &dst) {
    G4_DstRegRegion *rgn = new (mem) G4_DstRegRegion(dst);
    return rgn;
  }

  // create a direct DstRegRegion
  G4_DstRegRegion *createDst(G4_VarBase *b, short roff, short sroff,
                             unsigned short hstride, G4_Type ty,
                             G4_AccRegSel regSel = ACC_UNDEFINED) {
    return createDstRegRegion(Direct, b, roff, sroff, hstride, ty, regSel);
  }
  // create a direct DstRegRegion
  G4_DstRegRegion *createDst(G4_VarBase *b, G4_Type ty) {
    return createDstRegRegion(Direct, b, 0, 0, 1, ty, ACC_UNDEFINED);
  }

  // create a indirect DstRegRegion
  // b is the address variable, which only supports subreg offset
  G4_DstRegRegion *createIndirectDst(G4_VarBase *b, short sroff,
                                     uint16_t hstride, G4_Type ty,
                                     int16_t immOff) {
    auto dst = createDstRegRegion(IndirGRF, b, 0, sroff, hstride, ty);
    dst->setImmAddrOff(immOff);
    return dst;
  }

  G4_DstRegRegion *createDstWithNewSubRegOff(G4_DstRegRegion *old,
                                             short newSubRegOff);

  //
  // return the imm operand; create one if not yet created
  //
  G4_Imm *createImm(int64_t imm, G4_Type ty) {
    G4_Imm *i = hashtable.lookupImm(imm, ty);
    return (i != NULL) ? i : hashtable.createImm(imm, ty);
  }

  //
  // return the float operand; create one if not yet created
  //
  G4_Imm *createImm(float fp);

  //
  // return the double operand; create one if not yet created
  //
  G4_Imm *createDFImm(double fp);

  // For integer immediates use a narrower type if possible
  // also change byte type to word type since HW does not support byte imm
  G4_Type getNewType(int64_t imm, G4_Type ty);

  //
  // return the imm operand with its lowest type(W or above); create one if not
  // yet created
  //
  G4_Imm *createImmWithLowerType(int64_t imm, G4_Type ty) {
    G4_Type new_type = getNewType(imm, ty);
    G4_Imm *i = hashtable.lookupImm(imm, new_type);
    return (i != NULL) ? i : hashtable.createImm(imm, new_type);
  }

  //
  // Create immediate operand without looking up hash table. This operand
  // is a relocatable immediate type.
  //
  G4_Reloc_Imm *createRelocImm(
    GenRelocType st, const std::string &sym, G4_Type ty)
  {
    return createRelocImm(st, sym, G4_Reloc_Imm::DEFAULT_MAGIC, ty);
  }

  //
  // Create immediate operand without looking up hash table. This operand
  // is a relocatable immediate type. Specify the value of this imm field,
  // which will present in the output instruction's imm value.
  //
  G4_Reloc_Imm *createRelocImm(
    GenRelocType st, const std::string &sym, int64_t immval, G4_Type ty)
  {
    size_t len = sym.size() + 1;
    char *symCopy = (char *)mem.alloc(len); // +1 for null that ends the string
    memcpy_s(symCopy, len, sym.c_str(), len);
    G4_Reloc_Imm *newImm = new (mem) G4_Reloc_Imm(st, symCopy, immval, ty);
    return newImm;
  }

  //
  // a new null-terminated copy of "lab" is created for the new label, so
  // caller does not have to allocate memory for lab
  // Note that "lab" should be unique within CISA_IR_BUILDER.
  G4_Label *createLabel(std::string_view lab, VISA_Label_Kind kind) {
    auto labStr = lab.data();
    size_t len = lab.size() + 1;
    char *new_str = (char *)mem.alloc(len); // +1 for null that ends the string
    memcpy_s(new_str, len, labStr, len);
    return new (mem) G4_Label(new_str, kind);
  }

  uint32_t getAndUpdateNextLabelId() { return m_next_local_label_id++; }
  //
  // createLocalBlockLabel() creates a unique label of type LABEL_BLOCK.
  //
  // Return a new G4_Label whose name is a null-terminated local label that
  // always starts with "_". This label is guaranteed to be unique within a
  // kernel and all its functions (within CISA_IR_BUILDER).
  // It is in the form:
  //
  //    label name:
  //    _[<kernelName>|L]_[k|f]<functionId>_<func_local_label_id>_<lab>
  //
  //    where optional <lab> is used for annotating the label for readability;
  //    and 'k' is for kernel entry and 'f' is for function.
  // If no kernel name or kernel name is too long, the label will start with
  // "_L".
  //
  G4_Label *createLocalBlockLabel(const std::string &lab = "") {
    const char *cstr_kname = kernel.getName();
    std::string kname("L");
    if (cstr_kname) {
      std::string tName = sanitizeLabelString(cstr_kname);
      // cstr_kname is just for readability. If it is too long, don't use it.
      // Also, make sure cstr_kname does not start with "_L_" to make sure it
      // would never be the same as any internal label (starts with "_L_").
      if (tName.size() != 0 && tName.size() <= 30 && tName.find("_L_") != 0) {
        kname = std::move(tName);
      }
    }

    std::stringstream ss;
    uint32_t lbl_id = getAndUpdateNextLabelId();
    // kernel and function (getFunctionId()) are numbered independently.
    // Make sure to use "k" for kernel entry and "f" for function to avoid
    // the same label in kernel entry and the first function.
    ss << "_" << kname << (getIsKernel() ? "_k" : "_f")
       << kernel.getFunctionId() << "_" << lbl_id << "_" << lab;
    size_t len = ss.str().size() + 1;
    char *new_str = (char *)mem.alloc(len); // +1 for null that ends the string
    memcpy_s(new_str, len, ss.str().c_str(), len);
    return new (mem) G4_Label(new_str, LABEL_BLOCK);
  }

  G4_Predicate *createPredicate(G4_PredState s, G4_VarBase *flag,
                                unsigned short srOff,
                                G4_Predicate_Control ctrl = PRED_DEFAULT) {
    G4_Predicate *pred = new (mem) G4_Predicate(s, flag, srOff, ctrl);
    return pred;
  }

  G4_Predicate *createPredicate(G4_Predicate &prd) {
    G4_Predicate *p = new (mem) G4_Predicate(prd);
    return p;
  }

  G4_CondMod *createCondMod(G4_CondModifier m, G4_VarBase *flag,
                            unsigned short off) {
    G4_CondMod *p = new (mem) G4_CondMod(m, flag, off);
    return p;
  }

  //
  // return the condition modifier; create one if not yet created
  //
  G4_CondMod *createCondMod(G4_CondMod &mod) {
    G4_CondMod *p = new (mem) G4_CondMod(mod);
    return p;
  }

  //
  // create register address expression normalized to (&reg +/- exp)
  //
  G4_AddrExp *createAddrExp(G4_RegVar *reg, int offset, G4_Type ty) {
    return new (mem) G4_AddrExp(reg, offset, ty);
  }

private:
  // Avoid calling this directly, use createDst and createIndirectDst instead
  G4_DstRegRegion *createDstRegRegion(G4_RegAccess a, G4_VarBase *b, short roff,
                                      short sroff, unsigned short hstride,
                                      G4_Type ty,
                                      G4_AccRegSel regSel = ACC_UNDEFINED) {
    G4_DstRegRegion *rgn = new (mem)
        G4_DstRegRegion(*this, a, b, roff, sroff, hstride, ty, regSel);
    return rgn;
  }

  // please leave all createInst() as private and use the public wrappers below

  // cond+sat+binary+line
  G4_INST *createInst(G4_Predicate *prd, G4_opcode op, G4_CondMod *mod,
                      G4_Sat sat, G4_ExecSize size, G4_DstRegRegion *dst,
                      G4_Operand *src0, G4_Operand *src1, G4_InstOpts options,
                      bool addToInstList);

  // TODO: remove
  // old template:
  // cond+sat+binary+line
  G4_INST *createInst(G4_Predicate *prd, G4_opcode op, G4_CondMod *mod,
                      G4_Sat sat, int execSize, G4_DstRegRegion *dst,
                      G4_Operand *src0, G4_Operand *src1, G4_InstOpts options,
                      bool addToInstList) {
    G4_ExecSize sz((unsigned char)execSize);
    return createInst(prd, op, mod, sat, sz, dst, src0, src1, options,
                      addToInstList);
  }

  // cond+sat+ternary
  G4_INST *createInst(G4_Predicate *prd, G4_opcode op, G4_CondMod *mod,
                      G4_Sat sat, G4_ExecSize execSize, G4_DstRegRegion *dst,
                      G4_Operand *src0, G4_Operand *src1, G4_Operand *src2,
                      G4_InstOpts options, bool addToInstList);

public:
  G4_INST *createIf(G4_Predicate *prd, G4_ExecSize execSize,
                    G4_InstOpts options);
  G4_INST *createElse(G4_ExecSize execSize, G4_InstOpts options);
  G4_INST *createEndif(G4_ExecSize execSize, G4_InstOpts options);
  G4_INST *createLabelInst(G4_Label *label, bool appendToInstList);
  G4_INST *createJmp(G4_Predicate *pred, G4_Operand *jmpTarget,
                     G4_InstOpts options, bool appendToInstList);
  G4_INST *createGoto(G4_Predicate *pred, G4_ExecSize execSize,
                      G4_Label *target, G4_InstOpts options,
                      bool addToInstList) {
    // jip is computed later during CFG construction
    return createCFInst(pred, G4_goto, execSize, nullptr, target, options,
                        addToInstList);
  }

  // ToDo: make createInternalInst() private as well and add wraper for them
  G4_INST *createInternalInst(G4_Predicate *prd, G4_opcode op, G4_CondMod *mod,
                              G4_Sat sat, G4_ExecSize execSize,
                              G4_DstRegRegion *dst, G4_Operand *src0,
                              G4_Operand *src1, G4_InstOpts options);

  G4_INST *createInternalInst(G4_Predicate *prd, G4_opcode op, G4_CondMod *mod,
                              G4_Sat sat, G4_ExecSize execSize,
                              G4_DstRegRegion *dst, G4_Operand *src0,
                              G4_Operand *src1, G4_Operand *src2,
                              G4_InstOpts options);

  G4_INST *createCFInst(G4_Predicate *prd, G4_opcode op, G4_ExecSize execSize,
                        G4_Label *jip, G4_Label *uip, G4_InstOpts options,
                        bool addToInstList);

  G4_INST *createInternalCFInst(G4_Predicate *prd, G4_opcode op,
                                G4_ExecSize execSize, G4_Label *jip,
                                G4_Label *uip, G4_InstOpts options);

  G4_InstSend *createSendInst(
      G4_Predicate *prd, G4_opcode op, G4_ExecSize execSize,
      G4_DstRegRegion *postDst, G4_SrcRegRegion *payload, G4_Operand *msg,
      G4_InstOpts options,
      G4_SendDescRaw *msgDesc, bool addToInstList);


  G4_InstSend *createInternalSendInst(
      G4_Predicate *prd, G4_opcode op, G4_ExecSize execSize,
      G4_DstRegRegion *postDst, G4_SrcRegRegion *payload, G4_Operand *msg,
      G4_InstOpts options,
      G4_SendDescRaw *msgDescs);

  G4_InstSend *createSplitSendInst(G4_Predicate *prd, G4_opcode op,
                                   G4_ExecSize execSize, G4_DstRegRegion *dst,
                                   G4_SrcRegRegion *src1, G4_SrcRegRegion *src2,
                                   G4_Operand *msg, G4_InstOpts options,
                                   G4_SendDescRaw *msgDesc, G4_Operand *src3,
                                   bool addToInstList);

  G4_InstSend *
  createInternalSplitSendInst(G4_ExecSize execSize, G4_DstRegRegion *dst,
                              G4_SrcRegRegion *src1, G4_SrcRegRegion *src2,
                              G4_Operand *msg, G4_InstOpts options,
                              G4_SendDescRaw *msgDesc, G4_Operand *src3);

  G4_INST *createMathInst(G4_Predicate *prd, G4_Sat sat, G4_ExecSize execSize,
                          G4_DstRegRegion *dst, G4_Operand *src0,
                          G4_Operand *src1, G4_MathOp mathOp,
                          G4_InstOpts options, bool addToInstList);

  G4_INST *createInternalMathInst(G4_Predicate *prd, G4_Sat sat,
                                  G4_ExecSize execSize, G4_DstRegRegion *dst,
                                  G4_Operand *src0, G4_Operand *src1,
                                  G4_MathOp mathOp, G4_InstOpts options);

  G4_INST *createIntrinsicInst(G4_Predicate *prd, Intrinsic intrinId,
                               G4_ExecSize execSize, G4_DstRegRegion *dst,
                               G4_Operand *src0, G4_Operand *src1,
                               G4_Operand *src2, G4_InstOpts options,
                               bool addToInstList);

  G4_INST *createInternalIntrinsicInst(G4_Predicate *prd, Intrinsic intrinId,
                                       G4_ExecSize execSize,
                                       G4_DstRegRegion *dst, G4_Operand *src0,
                                       G4_Operand *src1, G4_Operand *src2,
                                       G4_InstOpts options);

  G4_INST *createIntrinsicAddrMovInst(Intrinsic intrinId, G4_DstRegRegion *dst,
                                      G4_Operand *src0, G4_Operand *src1,
                                      G4_Operand *src2, G4_Operand *src3,
                                      G4_Operand *src4, G4_Operand *src5,
                                      G4_Operand *src6, G4_Operand *src7,
                                      bool addToInstList);

  G4_INST *createNop(G4_InstOpts options);
  G4_INST *createSync(G4_opcode syncOp, G4_Operand *src);

  G4_INST *createMov(G4_ExecSize execSize, G4_DstRegRegion *dst,
                     G4_Operand *src0, G4_InstOpts options,
                     bool appendToInstList);
  G4_INST *createMov(G4_Predicate *pred, G4_ExecSize execSize,
                     G4_DstRegRegion *dst, G4_Operand *src0,
                     G4_InstOpts options, bool appendToInstList);

  G4_INST *createBinOp(G4_opcode op, G4_ExecSize execSize, G4_DstRegRegion *dst,
                       G4_Operand *src0, G4_Operand *src1, G4_InstOpts options,
                       bool appendToInstList) {
    return createBinOp(nullptr, op, execSize, dst, src0, src1, options,
                       appendToInstList);
  }

  G4_INST *createBinOp(G4_Predicate *pred, G4_opcode op, G4_ExecSize execSize,
                       G4_DstRegRegion *dst, G4_Operand *src0, G4_Operand *src1,
                       G4_InstOpts options, bool appendToInstList = true);

  G4_INST *createMach(G4_ExecSize execSize, G4_DstRegRegion *dst,
                      G4_Operand *src0, G4_Operand *src1, G4_InstOpts options,
                      G4_Type accType);

  G4_INST *createMacl(G4_ExecSize execSize, G4_DstRegRegion *dst,
                      G4_Operand *src0, G4_Operand *src1, G4_InstOpts options,
                      G4_Type accType);

  G4_INST *createMadm(G4_ExecSize execSize, G4_DstRegRegion *dst,
                      G4_SrcRegRegion *src0, G4_SrcRegRegion *src1,
                      G4_SrcRegRegion *src2, G4_InstOpts options) {
    return createMadm(nullptr, execSize, dst, src0, src1, src2, options);
  }

  G4_INST *createMadm(G4_Predicate *pred, G4_ExecSize execSize,
                      G4_DstRegRegion *dst, G4_SrcRegRegion *src0,
                      G4_SrcRegRegion *src1, G4_SrcRegRegion *src2,
                      G4_InstOpts options);

  static G4_MathOp Get_MathFuncCtrl(ISA_Opcode op, G4_Type type);

  void resizePredefinedStackVars();

  template <typename T> T *duplicateOperand(T *opnd) {
    return static_cast<T *>(duplicateOpndImpl(opnd));
  }
  G4_Operand *duplicateOpndImpl(G4_Operand *opnd);

  G4_DstRegRegion *createSubDstOperand(G4_DstRegRegion *dst, uint16_t start,
                                       uint8_t size);
  G4_SrcRegRegion *createSubSrcOperand(G4_SrcRegRegion *src, uint16_t start,
                                       uint8_t size, uint16_t newVs,
                                       uint16_t newWd);
  G4_INST *makeSplittingInst(G4_INST *inst, G4_ExecSize execSize);

  G4_InstSend *createSendInst(G4_Predicate *pred, G4_DstRegRegion *postDst,
                              G4_SrcRegRegion *payload, G4_ExecSize execSize,
                              G4_SendDescRaw *msgDesc, G4_InstOpts options,
                              bool is_sendc);

  G4_InstSend *createSplitSendInst(G4_Predicate *pred, G4_DstRegRegion *dst,
                                   G4_SrcRegRegion *src1, G4_SrcRegRegion *src2,
                                   G4_ExecSize execSize,
                                   G4_SendDescRaw *msgDesc, G4_InstOpts options,
                                   bool is_sendc);

  // TODO: move to TranslateSendLdStLsc or elide VISA_Exec_Size (pre-convert)
  G4_SendDescRaw *createLscMsgDesc(LSC_OP op, LSC_SFID lscSfid,
                                   VISA_Exec_Size execSizeEnum,
                                   LSC_CACHE_OPTS cacheOpts, LSC_ADDR addr,
                                   LSC_DATA_SHAPE shape, G4_Operand *surface,
                                   uint32_t dstLen, uint32_t addrRegs,
                                   LdStAttrs otherAttrs);

  // ToDo: unify this with above function
  G4_SendDescRaw *createLscDesc(SFID sfid, uint32_t desc, uint32_t extDesc,
                                int src1Len, SendAccess access, G4_Operand *bti,
                                LdStAttrs otherAttrs);


  G4_InstSend *createLscSendInst(G4_Predicate *pred, G4_DstRegRegion *dst,
                                 G4_SrcRegRegion *src0, G4_SrcRegRegion *src1,
                                 G4_ExecSize execsize,
                                 G4_SendDescRaw *msgDesc,
                                 G4_InstOpts option, LSC_ADDR_TYPE addrType,
                                 unsigned surfOff, bool emitA0RegDef);


  G4_SrcRegRegion *getScratchSurfaceStatusIndex();

  void RestoreA0();

  G4_InstSend *
  createLscSendInstToScratch(G4_Predicate *pred, G4_DstRegRegion *dst,
                             G4_SrcRegRegion *src0, G4_SrcRegRegion *src1,
                             G4_ExecSize execSize, G4_SendDescRaw *msgDesc,
                             G4_InstOpts option, bool usesBti);

  G4_InstSend *
  createSplitSendToRenderTarget(G4_Predicate *pred, G4_DstRegRegion *dst,
                                G4_SrcRegRegion *src1, G4_SrcRegRegion *src2,
                                G4_SrcRegRegion *extDesc, G4_ExecSize execSize,
                                G4_SendDescRaw *msgDesc, G4_InstOpts option);

  G4_InstSend *createSendInst(G4_Predicate *pred, G4_DstRegRegion *postDst,
                              G4_SrcRegRegion *payload, unsigned regs2snd,
                              unsigned regs2rcv, G4_ExecSize execsize,
                              unsigned fc, SFID tf_id, bool head_present,
                              SendAccess access, G4_Operand *bti,
                              G4_Operand *sti, G4_InstOpts options,
                              bool is_sendc);

  G4_InstSend *createSplitSendInst(G4_Predicate *pred, G4_DstRegRegion *dst,
                                   G4_SrcRegRegion *src1, unsigned regs2snd1,
                                   G4_SrcRegRegion *src2, unsigned regs2snd2,
                                   unsigned regs2rcv, G4_ExecSize execSize,
                                   unsigned fc, SFID tf_id, bool head_present,
                                   SendAccess access, G4_Operand *bti,
                                   G4_Operand *sti, G4_InstOpts option,
                                   bool is_sendc);

  // helper functions
  G4_Declare *createSendPayloadDcl(unsigned num_elt, G4_Type type);

  void createMovR0Inst(G4_Declare *dcl, short refOff, short subregOff,
                       bool use_nomask = false,
                       G4_InstOpts options = InstOpt_NoOpt);

  void createMovInst(G4_Declare *dcl, short refOff, short subregOff,
                     G4_ExecSize execsize, G4_Predicate *pred,
                     G4_CondMod *condMod, G4_Operand *src_opnd,
                     bool use_nomask = false,
                     G4_InstOpts options = InstOpt_NoOpt);
  void createAddInst(G4_Declare *dcl, short regOff, short subregOff,
                     G4_ExecSize execSize, G4_Predicate *pred,
                     G4_CondMod *condMod, G4_Operand *src0_opnd,
                     G4_Operand *src1_opnd, G4_InstOption options);
  void createMovSendSrcInst(G4_Declare *dcl, short refOff, short subregOff,
                            unsigned num_dword, G4_Operand *src_opnd,
                            G4_InstOpts options);

  // short hand for creating a dstRegRegion
  G4_DstRegRegion *createDstRegRegion(G4_Declare *dcl, unsigned short hstride);
  G4_SrcRegRegion *createSrcRegRegion(G4_Declare *dcl, const RegionDesc *rd);

  // Create a null dst with scalar region and the given type
  G4_DstRegRegion *createNullDst(G4_Type dstType);

  // Create a null src with scalar region and the given type
  G4_SrcRegRegion *createNullSrc(G4_Type dstType);

  G4_DstRegRegion *checkSendDst(G4_DstRegRegion *dst_opnd);

  G4_INST *createDpasInst(G4_opcode opc, G4_ExecSize execSize,
                          G4_DstRegRegion *dst, G4_Operand *src0,
                          G4_Operand *src1, G4_Operand *src2,
                          G4_Operand *src3, G4_Operand *src4,
                          G4_InstOpts options, GenPrecision A, GenPrecision W,
                          uint8_t D, uint8_t C, bool addToInstList);

  G4_INST *createInternalDpasInst(G4_opcode opc, G4_ExecSize execSize,
                                  G4_DstRegRegion *dst, G4_Operand *src0,
                                  G4_Operand *src1, G4_Operand *src2,
                                  G4_InstOpts options, GenPrecision A,
                                  GenPrecision W, uint8_t D, uint8_t C,
                                  G4_Operand *src3 = nullptr,
                                  G4_Operand *src4 = nullptr);

  G4_INST *createBfnInst(uint8_t booleanFuncCtrl, G4_Predicate *prd,
                         G4_CondMod *mod, G4_Sat sat, G4_ExecSize execSize,
                         G4_DstRegRegion *dst, G4_Operand *src0,
                         G4_Operand *src1, G4_Operand *src2,
                         G4_InstOpts options, bool addToInstLis);

  G4_INST *createInternalBfnInst(uint8_t booleanFuncCtrl, G4_Predicate *prd,
                                 G4_CondMod *mod, G4_Sat sat,
                                 G4_ExecSize execSize, G4_DstRegRegion *dst,
                                 G4_Operand *src0, G4_Operand *src1,
                                 G4_Operand *src2, G4_InstOpts options);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // translateXXXXXXX functions translate specific vISA instructions into
  // sequences of G4 IR that implement the operation
  //
  // Implementations are split amongst various files based on category.
  // Comments below will point the user to the correct implementation file.
  // Please keep implementations the same sequential order as in the header.
  // (Feel free to re-order, but reflect the re-order here.)
  //
  // During refactor anything that was in TranslationInterface.cpp was moved
  // to one of this VisaToG4/Translate* files, but some methods have nothing
  // to do with vISA and make sense in the BuildIRImpl.cpp and could be
  // moved there.  Please move the declaration (prototype) upwards in
  // that case.

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Members related to general arithmetic/logic/shift ops are in
  // VisaToG4/TranslateALU.cpp
  int translateVISAAddrInst(ISA_Opcode opcode, VISA_Exec_Size execSize,
                            VISA_EMask_Ctrl emask, G4_DstRegRegion *dst_opnd,
                            G4_Operand *src0_opnd, G4_Operand *src1_opnd);

  int translateVISAArithmeticInst(ISA_Opcode opcode, VISA_Exec_Size execSize,
                                  VISA_EMask_Ctrl emask, G4_Predicate *predOpnd,
                                  G4_Sat saturate, G4_CondMod *condMod,
                                  G4_DstRegRegion *dstOpnd,
                                  G4_Operand *src0Opnd, G4_Operand *src1Opnd,
                                  G4_Operand *src2Opnd,
                                  G4_DstRegRegion *carryBorrow);

  int translateVISADpasInst(VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask,
                            G4_opcode opc, G4_DstRegRegion *dstOpnd,
                            G4_SrcRegRegion *src0Opnd,
                            G4_SrcRegRegion *src1Opnd,
                            G4_SrcRegRegion *src2Opnd,
                            G4_SrcRegRegion *src3Opnd,
                            G4_SrcRegRegion *src4Opnd, GenPrecision A,
                            GenPrecision W, uint8_t D, uint8_t C);
  int translateVISABfnInst(uint8_t booleanFuncCtrl,
                           VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask,
                           G4_Predicate *predOpnd, G4_Sat saturate,
                           G4_CondMod *condMod, G4_DstRegRegion *dstOpnd,
                           G4_Operand *src0Opnd, G4_Operand *src1Opnd,
                           G4_Operand *src2Opnd);

  int translateVISACompareInst(ISA_Opcode opcode, VISA_Exec_Size execSize,
                               VISA_EMask_Ctrl emask, VISA_Cond_Mod relOp,
                               G4_Declare *predDst, G4_Operand *src0_opnd,
                               G4_Operand *src1_opnd);

  int translateVISACompareInst(ISA_Opcode opcode, VISA_Exec_Size execSize,
                               VISA_EMask_Ctrl emask, VISA_Cond_Mod relOp,
                               G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd,
                               G4_Operand *src1Opnd);

  int translateVISALogicInst(ISA_Opcode opcode, G4_Predicate *pred_opnd,
                             G4_Sat saturate, VISA_Exec_Size executionSize,
                             VISA_EMask_Ctrl emask, G4_DstRegRegion *dst,
                             G4_Operand *src0, G4_Operand *src1,
                             G4_Operand *src2, G4_Operand *src3);

  int translateVISADataMovementInst(ISA_Opcode opcode,
                                    CISA_MIN_MAX_SUB_OPCODE subOpcode,
                                    G4_Predicate *pred_opnd,
                                    VISA_Exec_Size executionSize,
                                    VISA_EMask_Ctrl emask, G4_Sat saturate,
                                    G4_DstRegRegion *dst, G4_Operand *src0,
                                    G4_Operand *src1);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Control flow, function call, and branching ops are located in
  // VisaToG4/TranslateBranch.cpp
  int translateVISACFSwitchInst(G4_Operand *indexOpnd, uint8_t numLabels,
                                G4_Label **lab);

  int translateVISACFLabelInst(G4_Label *lab);

  int translateVISACFCallInst(VISA_Exec_Size execsize, VISA_EMask_Ctrl emask,
                              G4_Predicate *predOpnd, G4_Label *lab);

  int translateVISACFJumpInst(G4_Predicate *predOpnd, G4_Label *lab);

  int translateVISACFFCallInst(VISA_Exec_Size execsize, VISA_EMask_Ctrl emask,
                               G4_Predicate *predOpnd, std::string funcName,
                               uint8_t argSize, uint8_t returnSize);

  int translateVISACFIFCallInst(VISA_Exec_Size execsize, VISA_EMask_Ctrl emask,
                                G4_Predicate *predOpnd, bool isUniform,
                                G4_Operand *funcAddr, uint8_t argSize,
                                uint8_t returnSize);

  int translateVISACFSymbolInst(const std::string &symbolName,
                                G4_DstRegRegion *dst);

  int translateVISACFFretInst(VISA_Exec_Size execsize, VISA_EMask_Ctrl emask,
                              G4_Predicate *predOpnd);

  int translateVISACFRetInst(VISA_Exec_Size execsize, VISA_EMask_Ctrl emask,
                             G4_Predicate *predOpnd);

  int translateVISAGotoInst(G4_Predicate *predOpnd,
                            VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask,
                            G4_Label *label);

  ///////////////////////////////////////////////////////////////////////////
  // members related to special math sequences VisaToG4/TranslateMath.cpp
  void expandFdiv(G4_ExecSize exsize, G4_Predicate *predOpnd, G4_Sat saturate,
                  G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd,
                  G4_Operand *src1Opnd, uint32_t instOpt);

  void expandPow(G4_ExecSize exsize, G4_Predicate *predOpnd, G4_Sat saturate,
                 G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd,
                 G4_Operand *src1Opnd, uint32_t instOpt);

  int translateVISAArithmeticDoubleInst(
      ISA_Opcode opcode, VISA_Exec_Size execSize, VISA_EMask_Ctrl emask,
      G4_Predicate *predOpnd, G4_Sat saturate, G4_DstRegRegion *dstOpnd,
      G4_Operand *src0Opnd, G4_Operand *src1Opnd);

  int translateVISAArithmeticSingleDivideIEEEInst(
      ISA_Opcode opcode, VISA_Exec_Size execSize, VISA_EMask_Ctrl emask,
      G4_Predicate *predOpnd, G4_Sat saturate, G4_CondMod *condMod,
      G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd, G4_Operand *src1Opnd);

  int translateVISAArithmeticSingleSQRTIEEEInst(
      ISA_Opcode opcode, VISA_Exec_Size execSize, VISA_EMask_Ctrl emask,
      G4_Predicate *predOpnd, G4_Sat saturate, G4_CondMod *condMod,
      G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd);

  int translateVISAArithmeticDoubleSQRTInst(
      ISA_Opcode opcode, VISA_Exec_Size execSize, VISA_EMask_Ctrl emask,
      G4_Predicate *predOpnd, G4_Sat saturate, G4_CondMod *condMod,
      G4_DstRegRegion *dstOpnd, G4_Operand *src0Opnd);

  int translateVISAInvmRsqtmInst(ISA_Opcode opcode,
      VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask,
      G4_Predicate *predOpnd, G4_Sat saturate, G4_DstRegRegion *dstOpnd,
      VISA_PredVar *predDst, G4_Operand *src0Opnd, G4_Operand *Src1Opnd);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Members related miscellaneous instructions that don't fit any other
  // category are in VisaToG4/TranslateMisc.cpp
  //
  // (As stated above some of this could move to BuildIRImpl.cpp.)
  static G4_ExecSize toExecSize(VISA_Exec_Size execSize);

  VISA_Exec_Size roundUpExecSize(VISA_Exec_Size execSize);

  G4_Declare *getImmDcl(G4_Imm *val, int numElt);

  //
  // 'copyExecSize' and preparePayload's batchExSize together provide
  // the execSize of copying instruction.
  //     'copyExecSize' of PayloadSource is used for header only for now.
  //     If 'copyExecSize' is present, use it; otherwise, use batchExSize
  //     of preparePayload for copying.
  //
  //  For example,
  //     send(4|M0)   nullptr  addr:a32 data:ud ...
  //  will be changed to
  //     mov(8|M0)    msgPayload(0,0) <1;1,0> header
  //     mov(4|M0)    msgPayload(1,0) <1;1,0> addr
  //     mov(4|M0)    msgPayload(2,0) <1;1,0> data
  //     send(4|M0)   nullptr   msgPayload ...
  //  where 'copyExecSize' will be 8 and batchExSize = 4.
  //
  struct PayloadSource {
    G4_SrcRegRegion *opnd = nullptr;
    uint32_t numElts = 0; // 'opnd's size in msg payload
    G4_InstOpts instOpt = {};
    G4_ExecSize copyExecSize = {}; // used for copy if given.

    PayloadSource() : copyExecSize(g4::SIMD_UNDEFINED) {}
  };

  /// preparePayload - This method prepares payload from the specified header
  /// and sources.
  ///
  /// \param msgs             Message(s) prepared. That 2-element array must
  ///                         be cleared before calling preparePayload().
  /// \param sizes            Size(s) (in GRF) of each message prepared. That
  ///                         2-element array must be cleared before calling
  ///                         preparePayload().
  /// \param batchExSize      When it's required to copy sources, batchExSize
  ///                         specifies the SIMD width of copy except when
  ///                         'copyExecSize' of PayloadSource is defined. And
  ///                         in the case 'copyExecSize is defined, it's used as
  ///                         execsize for copy.
  /// \param splitSendEnabled Whether feature split-send is available. When
  ///                         feature split-send is available, this function
  ///                         will check whether two consecutive regions
  ///                         could be prepared instead of one to take
  ///                         advantage of split-send.
  /// \param srcs             The array of sources (including header if
  ///                         present).
  /// \param len              The length of the array of sources.
  ///
  void preparePayload(G4_SrcRegRegion *msgs[2], unsigned sizes[2],
                      G4_ExecSize batchExSize, bool splitSendEnabled,
                      PayloadSource sources[], unsigned len);

  // Coalesce multiple payloads into a single region.  Pads each region with
  // an optional alignment argument (e.g. a GRF size).  The source region
  // sizes are determined by source dimension, so use an alias if you are
  // using a subregion.  All copies are made under no mask semantics using
  // the maximal SIMD width for the current device.
  //
  // A second alignment option allows a caller to align the full payload
  // to some total.
  //
  // If all parameters are nullptr or the null register, we return the null
  // register.
  //
  // Some examples:
  //
  // 1. coalescePayloads(GRF_SIZE,GRF_SIZE,...);
  //    Coalesces each source into a single region.  Each source is padded
  //    out to a full GRF, and the sum total result is also padded out to
  //    a full GRF.
  //
  // 2. coalescePayloads(1,GRF_SIZE,...);
  //    Coalesces each source into a single region packing each source
  //    together, but padding the result.  E.g. one could copy a QW and then
  //    a DW and pad the result out to a GRF.
  //
  G4_SrcRegRegion *
  coalescePayload(G4_Predicate *pred,
                  unsigned alignSourcesTo, unsigned alignPayloadTo,
                  uint32_t payloadSize, uint32_t srcSize,
                  std::initializer_list<G4_SrcRegRegion *> srcs,
                  VISA_EMask_Ctrl emask);

  // emask is InstOption
  void Copy_SrcRegRegion_To_Payload(G4_Declare *payload, unsigned int &regOff,
                                    G4_SrcRegRegion *src, G4_ExecSize execSize,
                                    uint32_t emask,
                                    G4_Predicate *pred = nullptr);
  unsigned int getByteOffsetSrcRegion(G4_SrcRegRegion *srcRegion);

  // only used in TranslateSend3D, maybe consider moving there if no
  // one else uses them.
  bool checkIfRegionsAreConsecutive(G4_SrcRegRegion *first,
                                    G4_SrcRegRegion *second,
                                    G4_ExecSize execSize);
  bool checkIfRegionsAreConsecutive(G4_SrcRegRegion *first,
                                    G4_SrcRegRegion *second,
                                    G4_ExecSize execSize, G4_Type type);

  int generateDebugInfoPlaceholder(); // TODO: move to BuildIRImpl.cpp?
                                      //
  // handle breakpoint instruction
  int translateBreakpointInstruction();

  // legitimiately belongs in Misc
  int translateVISALifetimeInst(bool isStart, G4_Operand *var);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // members related to 3D and sampler ops are in VisaToG4/TranslateSend3D.cpp
  int translateVISASampleInfoInst(VISA_Exec_Size executionSize,
                                  VISA_EMask_Ctrl emask, ChannelMask chMask,
                                  G4_Operand *surface, G4_DstRegRegion *dst);

  int translateVISAResInfoInst(VISA_Exec_Size executionSize,
                               VISA_EMask_Ctrl emask, ChannelMask chMask,
                               G4_Operand *surface, G4_SrcRegRegion *lod,
                               G4_DstRegRegion *dst);

  int translateVISAURBWrite3DInst(
      G4_Predicate *pred, VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask,
      uint8_t numOut, uint16_t globalOffset, G4_SrcRegRegion *channelMask,
      G4_SrcRegRegion *urbHandle, G4_SrcRegRegion *perSlotOffset,
      G4_SrcRegRegion *vertexData);

  int translateVISARTWrite3DInst(G4_Predicate *pred,
                                 VISA_Exec_Size executionSize,
                                 VISA_EMask_Ctrl emask, G4_Operand *surface,
                                 G4_SrcRegRegion *r1HeaderOpnd,
                                 G4_Operand *rtIndex, vISA_RT_CONTROLS cntrls,
                                 G4_SrcRegRegion *sampleIndexOpnd,
                                 G4_Operand *cpsCounter, unsigned int numParms,
                                 G4_SrcRegRegion **msgOpnds);



  int translateLscTypedBlock2DInst(LSC_OP op, LSC_CACHE_OPTS cacheOpts,
                                   LSC_ADDR_TYPE addrModel,
                                   LSC_DATA_SHAPE_TYPED_BLOCK2D shape,
                                   G4_Operand *surface,
                                   unsigned ssIdx,
                                   G4_DstRegRegion *dstData,
                                   G4_Operand *xOffset, // block start offset x
                                   G4_Operand *yOffset, // block start offset y
                                   int xImmOffset, // x immediate offset
                                   int yImmOffset, // y immediate offset
                                   G4_SrcRegRegion *src1Data);

  G4_SrcRegRegion *
  lscBuildTypedBlock2DPayload(LSC_DATA_SHAPE_TYPED_BLOCK2D dataShape2D,
                              G4_Operand *xOffset, G4_Operand *yOffset,
                              int immOffX, int immOffY);

  int translateLscUntypedAppendCounterAtomicInst(
      LSC_OP op, G4_Predicate *pred, VISA_Exec_Size visaExecSize,
      VISA_EMask_Ctrl execCtrl, LSC_CACHE_OPTS cacheOpts,
      LSC_ADDR_TYPE addrType, LSC_DATA_SHAPE dataShape,
      G4_Operand *surface, unsigned ssIdx,
      G4_DstRegRegion *dst, G4_SrcRegRegion *srcData);

  int splitSampleInst(VISASampler3DSubOpCode actualop, bool pixelNullMask,
                      bool cpsEnable, G4_Predicate *pred,
                      ChannelMask srcChannel, int numChannels,
                      G4_Operand *aoffimmi, G4_Operand *sampler,
                      G4_Operand *surface,
                      G4_Operand *pairedResource,
                      G4_DstRegRegion *dst, VISA_EMask_Ctrl emask,
                      bool useHeader,
                      unsigned numRows, // msg length for each simd8
                      unsigned int numParms, G4_SrcRegRegion **params,
                      bool uniformSampler = true);

  void doSamplerHeaderMove(G4_Declare *header, G4_Operand *sampler);
  void doPairedResourceHeaderMove(G4_Declare *header,
                                  G4_Operand *pairedResource);
  G4_Declare *getSamplerHeader(bool isBindlessSampler, bool samplerIndexGE16);
  uint32_t getSamplerResponseLength(int numChannels, bool isFP16, int execSize,
                                    bool pixelNullMask, bool nullDst);

  int translateVISASampler3DInst(
      VISASampler3DSubOpCode actualop, bool pixelNullMask, bool cpsEnable,
      bool uniformSampler, G4_Predicate *pred, VISA_Exec_Size executionSize,
      VISA_EMask_Ctrl emask, ChannelMask srcChannel, G4_Operand *aoffimmi,
      G4_Operand *sampler, G4_Operand *surface,
      G4_Operand *pairedSurface,
      G4_DstRegRegion *dst, unsigned int numParms, G4_SrcRegRegion **params);


  int translateVISALoad3DInst(VISASampler3DSubOpCode actualop,
                              bool pixelNullMask, G4_Predicate *pred,
                              VISA_Exec_Size exeuctionSize, VISA_EMask_Ctrl em,
                              ChannelMask channelMask, G4_Operand *aoffimmi,
                              G4_Operand *surface, G4_Operand *pairedSurface,
                              G4_DstRegRegion *dst, uint8_t numOpnds,
                              G4_SrcRegRegion **opndArray);

  int translateVISAGather3dInst(VISASampler3DSubOpCode actualop,
                                bool pixelNullMask, G4_Predicate *pred,
                                VISA_Exec_Size exeuctionSize,
                                VISA_EMask_Ctrl em, ChannelMask channelMask,
                                G4_Operand *aoffimmi, G4_Operand *sampler,
                                G4_Operand *surface, G4_Operand *pairedSurface,
                                G4_DstRegRegion *dst, unsigned int numOpnds,
                                G4_SrcRegRegion **opndArray);

  int translateVISASamplerNormInst(G4_Operand *surface, G4_Operand *sampler,
                                   ChannelMask channel,
                                   unsigned numEnabledChannels,
                                   G4_Operand *deltaUOpnd, G4_Operand *uOffOpnd,
                                   G4_Operand *deltaVOpnd, G4_Operand *vOffOpnd,
                                   G4_DstRegRegion *dst_opnd);

  int translateVISASamplerInst(unsigned simdMode, G4_Operand *surface,
                               G4_Operand *sampler, ChannelMask channel,
                               unsigned numEnabledChannels,
                               G4_Operand *uOffOpnd, G4_Operand *vOffOpnd,
                               G4_Operand *rOffOpnd, G4_DstRegRegion *dstOpnd);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // basic load and store (type and untyped) are located in
  // VisaToG4/TranslateSendLdStLegacy.cpp

  // IsHeaderOptional - Check whether the message header is optional.
  bool isMessageHeaderOptional(G4_Operand *surface, G4_Operand *Offset) const;

  // IsStatelessSurface - Check whether the give surface is statelesss surface.
  static bool isStatelessSurface(const G4_Operand *surface) {
    // So far, it's only reliable to check an immediate surface.
    return surface->isImm() && (surface->asImm()->getImm() == PREDEF_SURF_255 ||
                                surface->asImm()->getImm() == PREDEF_SURF_253);
  }

  std::tuple<G4_SrcRegRegion *, uint32_t, uint32_t>
  constructSrcPayloadRenderTarget(vISA_RT_CONTROLS cntrls,
                                  G4_SrcRegRegion **msgOpnds,
                                  unsigned int numMsgOpnds,
                                  G4_ExecSize execSize, G4_InstOpts instOpt);

  std::tuple<G4_SrcRegRegion *, uint32_t, uint32_t>
  constructSrcPayloadDualRenderTarget(vISA_RT_CONTROLS cntrls,
                                      G4_SrcRegRegion **msgOpnds,
                                      unsigned int numMsgOpnds,
                                      G4_ExecSize execSize,
                                      G4_InstOpts instOpt);

  int translateVISAQWGatherInst(VISA_Exec_Size executionSize,
                                VISA_EMask_Ctrl emask, G4_Predicate *pred,
                                VISA_SVM_Block_Num numBlocks,
                                G4_SrcRegRegion *surface,
                                G4_SrcRegRegion *addresses,
                                G4_DstRegRegion *dst);

  int translateVISAQWScatterInst(VISA_Exec_Size executionSize,
                                 VISA_EMask_Ctrl emask, G4_Predicate *pred,
                                 VISA_SVM_Block_Num numBlocks,
                                 G4_SrcRegRegion *surface,
                                 G4_SrcRegRegion *addresses,
                                 G4_SrcRegRegion *src);

  uint32_t setOwordForDesc(uint32_t desc, int numOword,
                           bool isSLM = false) const;
  int translateVISAOwordLoadInst(ISA_Opcode opcode, bool modified,
                                 G4_Operand *surface, VISA_Oword_Num size,
                                 G4_Operand *offOpnd, G4_DstRegRegion *dstOpnd);

  int translateVISAOwordStoreInst(G4_Operand *surface, VISA_Oword_Num size,
                                  G4_Operand *offOpnd,
                                  G4_SrcRegRegion *srcOpnd);

  int translateVISAGatherInst(VISA_EMask_Ctrl emask, bool modified,
                              GATHER_SCATTER_ELEMENT_SIZE eltSize,
                              VISA_Exec_Size executionSize, G4_Operand *surface,
                              G4_Operand *gOffOpnd, G4_SrcRegRegion *eltOFfOpnd,
                              G4_DstRegRegion *dstOpnd);

  int translateVISAScatterInst(VISA_EMask_Ctrl emask,
                               GATHER_SCATTER_ELEMENT_SIZE eltSize,
                               VISA_Exec_Size executionSize,
                               G4_Operand *surface, G4_Operand *gOffOpnd,
                               G4_SrcRegRegion *eltOffOpnd,
                               G4_SrcRegRegion *srcOpnd);

  int translateVISAGather4Inst(VISA_EMask_Ctrl emask, bool modified,
                               ChannelMask chMask, VISA_Exec_Size executionSize,
                               G4_Operand *surface, G4_Operand *gOffOpnd,
                               G4_SrcRegRegion *eltOffOpnd,
                               G4_DstRegRegion *dstOpnd);

  int translateVISAScatter4Inst(VISA_EMask_Ctrl emask, ChannelMask chMask,
                                VISA_Exec_Size executionSize,
                                G4_Operand *surface, G4_Operand *gOffOpnd,
                                G4_SrcRegRegion *eltOffOpnd,
                                G4_SrcRegRegion *srcOpnd);

  int translateVISADwordAtomicInst(VISAAtomicOps subOpc, bool is16Bit,
                                   G4_Predicate *pred, VISA_Exec_Size execSize,
                                   VISA_EMask_Ctrl eMask, G4_Operand *surface,
                                   G4_SrcRegRegion *offsets,
                                   G4_SrcRegRegion *src0, G4_SrcRegRegion *src1,
                                   G4_DstRegRegion *dst);

  void buildTypedSurfaceAddressPayload(G4_SrcRegRegion *u, G4_SrcRegRegion *v,
                                       G4_SrcRegRegion *r, G4_SrcRegRegion *lod,
                                       G4_ExecSize execSize,
                                       G4_InstOpts instOpt,
                                       PayloadSource sources[], uint32_t &len);

  int translateVISAGather4TypedInst(G4_Predicate *pred, VISA_EMask_Ctrl emask,
                                    ChannelMask chMask, G4_Operand *surfaceOpnd,
                                    VISA_Exec_Size executionSize,
                                    G4_SrcRegRegion *uOffsetOpnd,
                                    G4_SrcRegRegion *vOffsetOpnd,
                                    G4_SrcRegRegion *rOffsetOpnd,
                                    G4_SrcRegRegion *lodOpnd,
                                    G4_DstRegRegion *dstOpnd);

  int translateVISAScatter4TypedInst(
      G4_Predicate *pred, VISA_EMask_Ctrl emask, ChannelMask chMask,
      G4_Operand *surfaceOpnd, VISA_Exec_Size executionSize,
      G4_SrcRegRegion *uOffsetOpnd, G4_SrcRegRegion *vOffsetOpnd,
      G4_SrcRegRegion *rOffsetOpnd, G4_SrcRegRegion *lodOpnd,
      G4_SrcRegRegion *srcOpnd);

  int translateVISATypedAtomicInst(
      VISAAtomicOps atomicOp, bool is16Bit, G4_Predicate *pred,
      VISA_EMask_Ctrl emask, VISA_Exec_Size execSize, G4_Operand *surface,
      G4_SrcRegRegion *uOffsetOpnd, G4_SrcRegRegion *vOffsetOpnd,
      G4_SrcRegRegion *rOffsetOpnd, G4_SrcRegRegion *lodOpnd,
      G4_SrcRegRegion *src0, G4_SrcRegRegion *src1, G4_DstRegRegion *dst);

  void applySideBandOffset(G4_Operand *sideBand,
                           const G4_SendDescRaw *sendMsgDesc);

  int translateVISAGather4ScaledInst(
      G4_Predicate *pred, VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
      ChannelMask chMask, G4_Operand *surface, G4_Operand *globalOffset,
      G4_SrcRegRegion *offsets, G4_DstRegRegion *dst);

  int translateVISAScatter4ScaledInst(
      G4_Predicate *pred, VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
      ChannelMask chMask, G4_Operand *surface, G4_Operand *globalOffset,
      G4_SrcRegRegion *offsets, G4_SrcRegRegion *src);

  int translateGather4Inst(G4_Predicate *pred, VISA_Exec_Size execSize,
                           VISA_EMask_Ctrl eMask, ChannelMask chMask,
                           G4_Operand *surface, G4_Operand *globalOffset,
                           G4_SrcRegRegion *offsets, G4_DstRegRegion *dst);

  int translateScatter4Inst(G4_Predicate *pred, VISA_Exec_Size execSize,
                            VISA_EMask_Ctrl eMask, ChannelMask chMask,
                            G4_Operand *surface, G4_Operand *globalOffset,
                            G4_SrcRegRegion *offsets, G4_SrcRegRegion *src);

  int translateVISAGatherScaledInst(
      G4_Predicate *pred, VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
      VISA_SVM_Block_Num numBlocks, G4_Operand *surface,
      G4_Operand *globalOffset, G4_SrcRegRegion *offsets, G4_DstRegRegion *dst);

  int translateVISAScatterScaledInst(
      G4_Predicate *pred, VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
      VISA_SVM_Block_Num numBlocks, G4_Operand *surface,
      G4_Operand *globalOffset, G4_SrcRegRegion *offsets, G4_SrcRegRegion *src);

  int translateByteGatherInst(G4_Predicate *pred, VISA_Exec_Size execSize,
                              VISA_EMask_Ctrl eMask,
                              VISA_SVM_Block_Num numBlocks, G4_Operand *surface,
                              G4_Operand *globalOffset,
                              G4_SrcRegRegion *offsets, G4_DstRegRegion *dst);

  int translateByteScatterInst(G4_Predicate *pred, VISA_Exec_Size execSize,
                               VISA_EMask_Ctrl eMask,
                               VISA_SVM_Block_Num numBlocks,
                               G4_Operand *surface, G4_Operand *globalOffset,
                               G4_SrcRegRegion *offsets, G4_SrcRegRegion *src);

  int translateVISASVMBlockReadInst(VISA_Oword_Num numOword, bool unaligned,
                                    G4_Operand *address, G4_DstRegRegion *dst);

  int translateVISASVMBlockWriteInst(VISA_Oword_Num numOword,
                                     G4_Operand *address, G4_SrcRegRegion *src);

  int translateVISASVMScatterReadInst(VISA_Exec_Size executionSize,
                                      VISA_EMask_Ctrl emask, G4_Predicate *pred,
                                      VISA_SVM_Block_Type blockSize,
                                      VISA_SVM_Block_Num numBlocks,
                                      G4_SrcRegRegion *addresses,
                                      G4_DstRegRegion *dst);

  int translateVISASVMScatterWriteInst(
      VISA_Exec_Size executionSize, VISA_EMask_Ctrl emask, G4_Predicate *pred,
      VISA_SVM_Block_Type blockSize, VISA_SVM_Block_Num numBlocks,
      G4_SrcRegRegion *addresses, G4_SrcRegRegion *src);

  int translateVISASVMAtomicInst(VISAAtomicOps op, unsigned short bitwidth,
                                 VISA_Exec_Size executionSize,
                                 VISA_EMask_Ctrl emask, G4_Predicate *pred,
                                 G4_SrcRegRegion *addresses,
                                 G4_SrcRegRegion *src0, G4_SrcRegRegion *src1,
                                 G4_DstRegRegion *dst);

  // return globalOffset + offsets as a contiguous operand
  G4_SrcRegRegion *getSVMOffset(G4_Operand *globalOffset,
                                G4_SrcRegRegion *offsets, uint16_t exSize,
                                G4_Predicate *pred, uint32_t mask);

  int translateSVMGather4Inst(VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
                              ChannelMask chMask, G4_Predicate *pred,
                              G4_Operand *address, G4_SrcRegRegion *offsets,
                              G4_DstRegRegion *dst);

  int translateSVMScatter4Inst(VISA_Exec_Size execSize, VISA_EMask_Ctrl eMask,
                               ChannelMask chMask, G4_Predicate *pred,
                               G4_Operand *address, G4_SrcRegRegion *offsets,
                               G4_SrcRegRegion *src);

  int translateVISASVMGather4ScaledInst(VISA_Exec_Size execSize,
                                        VISA_EMask_Ctrl eMask,
                                        ChannelMask chMask, G4_Predicate *pred,
                                        G4_Operand *address,
                                        G4_SrcRegRegion *offsets,
                                        G4_DstRegRegion *dst);

  int translateVISASVMScatter4ScaledInst(VISA_Exec_Size execSize,
                                         VISA_EMask_Ctrl eMask,
                                         ChannelMask chMask, G4_Predicate *pred,
                                         G4_Operand *address,
                                         G4_SrcRegRegion *offsets,
                                         G4_SrcRegRegion *src);

  // Minimum execution size for LSC on this platform
  // Minimum is generally half the full size except rare cases.
  // Full LSC SIMD size for PVC derivatives is 32, and 16 for DG2 derivatives
  G4_ExecSize lscMinExecSize(LSC_SFID lscSfid) const;

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // New LSC-based load and store (type and untyped) are located in
  // VisaToG4/TranslateSendLdStLsc.cpp
  int translateLscUntypedInst(
      LSC_OP op, LSC_SFID lscSfid, G4_Predicate *pred, VISA_Exec_Size execSize,
      VISA_EMask_Ctrl emask, LSC_CACHE_OPTS cacheOpts, LSC_ADDR addrInfo,
      LSC_DATA_SHAPE shape,
      G4_Operand *surface, // surface/bti
      unsigned ssIdx, // optional BSSO index
      G4_DstRegRegion *dstData, G4_SrcRegRegion *src0AddrOrBlockY,
      G4_Operand *src0AddrStrideOrBlockX, // only for strided and block2d
      G4_SrcRegRegion *src1Data,          // store data/extra atomic operands
      G4_SrcRegRegion *src2Data           // only for fcas/icas
  );
  int translateLscUntypedBlock2DInst(
      LSC_OP op, LSC_SFID lscSfid, G4_Predicate *pred, VISA_Exec_Size execSize,
      VISA_EMask_Ctrl emask, LSC_CACHE_OPTS cacheOpts,
      LSC_DATA_SHAPE_BLOCK2D shape, G4_DstRegRegion *dstData,
      G4_Operand *src0Addrs[LSC_BLOCK2D_ADDR_PARAMS],
      G4_SrcRegRegion *src1Data, int xImmOff, int yImmOff);
  int translateLscUntypedBlock2DInst(
      LSC_OP op, LSC_SFID lscSfid, G4_Predicate *pred, VISA_Exec_Size execSize,
      VISA_EMask_Ctrl emask, LSC_CACHE_OPTS cacheOpts,
      LSC_DATA_SHAPE_BLOCK2D shape, G4_DstRegRegion *dstData,
      G4_Operand *src0Addr, G4_SrcRegRegion *src1Data, int xImmOff, int yImmOff);
  int translateLscTypedInst(
      LSC_OP op, G4_Predicate *pred, VISA_Exec_Size execSize,
      VISA_EMask_Ctrl emask, LSC_CACHE_OPTS cacheOpts, LSC_ADDR_TYPE addrModel,
      LSC_ADDR_SIZE addrSize, LSC_DATA_SHAPE shape,
      G4_Operand *surface,      // surface/bti
      unsigned surfaceIndex,
      G4_DstRegRegion *dstData, // dst on load/atomic
      G4_SrcRegRegion *src0AddrUs, int uOff,
      G4_SrcRegRegion *src0AddrVs, int vOff,
      G4_SrcRegRegion *src0AddrRs, int rOff,
      G4_SrcRegRegion *src0AddrLODs,
      G4_SrcRegRegion *src1Data, // store data/extra atomic operands
      G4_SrcRegRegion *src2Data  // icas/fcas only
  );

  LSC_DATA_ELEMS lscGetElementNum(unsigned eNum) const;
  int lscEncodeAddrSize(LSC_ADDR_SIZE addr_size, uint32_t &desc,
                        int &status) const;
  uint32_t lscComputeAddrSizeBits(LSC_ADDR_SIZE addr_size, int &status) const;
  int lscEncodeDataSize(LSC_DATA_SIZE data_size, uint32_t &desc,
                        int &status) const;
  uint32_t lscComputeDataSize(LSC_DATA_SIZE data_size, int &status) const;
  int lscEncodeDataElems(LSC_DATA_ELEMS data_elems, uint32_t &desc,
                         int &status) const;
  void lscEncodeDataOrder(LSC_DATA_ORDER t, uint32_t &desc, int &status) const;
  void lscEncodeCachingOpts(const LscOpInfo &opInfo, LSC_CACHE_OPTS cacheOpts,
                            uint32_t &desc, int &status) const;
  void lscEncodeAddrType(LSC_ADDR_TYPE at, uint32_t &desc, int &status) const;


  G4_SrcRegRegion *lscBuildStridedPayload(G4_Predicate *pred,
                                          G4_SrcRegRegion *src0AddrBase,
                                          G4_Operand *src0AddrStride,
                                          int dataSizeBytes, int vecSize,
                                          bool transposed);
  G4_SrcRegRegion *lscBuildBlock2DPayload(LSC_DATA_SHAPE_BLOCK2D dataShape2D,
                                          G4_Predicate *pred,
                                          G4_Operand *src0Addrs[6],
                                          int immOffX, int immOffY);
  G4_SrcRegRegion *emulateImmOffsetBlock2D(G4_Predicate *pred,
                                           G4_Operand *addrPayload,
                                           int immOffX,
                                           int immOffY);

  //
  // LSC allows users to pass an immediate scale and immediate addend.
  // Hardware may be able to take advantage of that if they satisfy
  // various constraints.  This also broadcasts if needed.
  G4_SrcRegRegion *lscLoadEffectiveAddress(
      LSC_OP lscOp, LSC_SFID lscSfid, G4_Predicate *pred, G4_ExecSize execSize,
      VISA_EMask_Ctrl execCtrl, LSC_ADDR addrInfo, int bytesPerDataElem,
      const G4_Operand *surface, G4_SrcRegRegion *addr, uint32_t &exDesc,
      uint32_t &exDescImmOff);

  // try and promote an immediate offset to LSC descriptor
  // (doesn't work for block2d)
  bool lscTryPromoteImmOffToExDesc(LSC_OP lscOp, LSC_SFID lscSfid,
                                   LSC_ADDR addrInfo, int bytesPerDataElem,
                                   const G4_Operand *surface,
                                   uint32_t &exDescImm, uint32_t &exDescImmOff);

  G4_SrcRegRegion *lscCheckRegion(G4_Predicate *pred, G4_ExecSize execSize,
                                  VISA_EMask_Ctrl execCtrl,
                                  G4_SrcRegRegion *src);

  G4_SrcRegRegion *lscMulAdd(G4_Predicate *pred, G4_ExecSize execSize,
                             VISA_EMask_Ctrl execCtrl, G4_SrcRegRegion *src,
                             int16_t mulImm16, int64_t addImm64);
  G4_SrcRegRegion *lscMul(G4_Predicate *pred, G4_ExecSize execSize,
                          VISA_EMask_Ctrl execCtrl, G4_SrcRegRegion *src,
                          int16_t mulImm16);
  G4_SrcRegRegion *lscAdd(G4_Predicate *pred, G4_ExecSize execSize,
                          VISA_EMask_Ctrl execCtrl, G4_SrcRegRegion *src,
                          int64_t addImm64);
  G4_SrcRegRegion *lscAdd64AosNative(G4_Predicate *pred, G4_ExecSize execSize,
                                     VISA_EMask_Ctrl execCtrl,
                                     G4_SrcRegRegion *src, int64_t addImm64);
  G4_SrcRegRegion *lscAdd64AosEmu(G4_Predicate *pred, G4_ExecSize execSize,
                                  VISA_EMask_Ctrl execCtrl,
                                  G4_SrcRegRegion *src, int64_t addImm64);
  G4_SrcRegRegion *lscMul64Aos(G4_Predicate *pred, G4_ExecSize execSize,
                               VISA_EMask_Ctrl execCtrl, G4_SrcRegRegion *src,
                               int16_t mulImm16);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // members related to media and VME are in VisaToG4/TranslateSendMedia.cpp
  int translateVISAMediaLoadInst(MEDIA_LD_mod mod, G4_Operand *surface,
                                 unsigned planeID, unsigned blockWidth,
                                 unsigned blockHeight, G4_Operand *xOffOpnd,
                                 G4_Operand *yOffOpnd,
                                 G4_DstRegRegion *dst_opnd);

  int translateVISAMediaStoreInst(MEDIA_ST_mod mod, G4_Operand *surface,
                                  unsigned planeID, unsigned blockWidth,
                                  unsigned blockHeight, G4_Operand *xOffOpnd,
                                  G4_Operand *yOffOpnd,
                                  G4_SrcRegRegion *srcOpnd);

  int translateVISAVmeImeInst(uint8_t stream_mode, uint8_t search_ctrl,
                              G4_Operand *surfaceOpnd, G4_Operand *uniInputOpnd,
                              G4_Operand *imeInputOpnd, G4_Operand *ref0Opnd,
                              G4_Operand *ref1Opnd, G4_Operand *costCenterOpnd,
                              G4_DstRegRegion *outputOpnd);

  int translateVISAVmeSicInst(G4_Operand *surfaceOpnd, G4_Operand *uniInputOpnd,
                              G4_Operand *sicInputOpnd,
                              G4_DstRegRegion *outputOpnd);

  int translateVISAVmeFbrInst(G4_Operand *surfaceOpnd,
                              G4_Operand *unitInputOpnd,
                              G4_Operand *fbrInputOpnd,
                              G4_Operand *fbrMbModOpnd,
                              G4_Operand *fbrSubMbShapeOpnd,
                              G4_Operand *fbrSubPredModeOpnd,
                              G4_DstRegRegion *outputOpnd);

  int translateVISAVmeIdmInst(G4_Operand *surfaceOpnd,
                              G4_Operand *unitInputOpnd,
                              G4_Operand *idmInputOpnd,
                              G4_DstRegRegion *outputOpnd);

  int translateVISASamplerVAGenericInst(
      G4_Operand *surface, G4_Operand *sampler, G4_Operand *uOffOpnd,
      G4_Operand *vOffOpnd, G4_Operand *vSizeOpnd, G4_Operand *hSizeOpnd,
      G4_Operand *mmfMode, unsigned char cntrl, unsigned char msgSeq,
      VA_fopcode fopcode, G4_DstRegRegion *dstOpnd, G4_Type dstType,
      unsigned dstSize, bool isBigKernel = false);

  int translateVISAAvsInst(G4_Operand *surface, G4_Operand *sampler,
                           ChannelMask channel, unsigned numEnabledChannels,
                           G4_Operand *deltaUOpnd, G4_Operand *uOffOpnd,
                           G4_Operand *deltaVOpnd, G4_Operand *vOffOpnd,
                           G4_Operand *u2dOpnd, G4_Operand *groupIDOpnd,
                           G4_Operand *verticalBlockNumberOpnd,
                           unsigned char cntrl, G4_Operand *v2dOpnd,
                           unsigned char execMode, G4_Operand *eifbypass,
                           G4_DstRegRegion *dstOpnd);

  int translateVISAVaSklPlusGeneralInst(
      ISA_VA_Sub_Opcode sub_opcode, G4_Operand *surface, G4_Operand *sampler,
      unsigned char mode, unsigned char functionality, G4_Operand *uOffOpnd,
      G4_Operand *vOffOpnd,
      // 1pixel convolve
      G4_Operand *offsetsOpnd,

      // FloodFill
      G4_Operand *loopCountOpnd, G4_Operand *pixelHMaskOpnd,
      G4_Operand *pixelVMaskLeftOpnd, G4_Operand *pixelVMaskRightOpnd,

      // LBP Correlation
      G4_Operand *disparityOpnd,

      // Correlation Search
      G4_Operand *verticalOriginOpnd, G4_Operand *horizontalOriginOpnd,
      G4_Operand *xDirectionSizeOpnd, G4_Operand *yDirectionSizeOpnd,
      G4_Operand *xDirectionSearchSizeOpnd,
      G4_Operand *yDirectionSearchSizeOpnd,

      G4_DstRegRegion *dstOpnd, G4_Type dstType, unsigned dstSize,

      // HDC
      unsigned char pixelSize, G4_Operand *dstSurfaceOpnd, G4_Operand *dstXOpnd,
      G4_Operand *dstYOpnd, bool hdcMode);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Raw send related members are in VisaToG4/TranslateSendRaw.cpp
  int translateVISARawSendInst(G4_Predicate *predOpnd,
                               VISA_Exec_Size executionSize,
                               VISA_EMask_Ctrl emask, uint8_t modifiers,
                               unsigned int exDesc, uint8_t numSrc,
                               uint8_t numDst, G4_Operand *msgDescOpnd,
                               G4_SrcRegRegion *msgOpnd,
                               G4_DstRegRegion *dstOpnd);

  int translateVISARawSendsInst(G4_Predicate *predOpnd,
                                VISA_Exec_Size executionSize,
                                VISA_EMask_Ctrl emask, uint8_t modifiers,
                                G4_Operand *exDesc, uint8_t numSrc0,
                                uint8_t numSrc1, uint8_t numDst,
                                G4_Operand *msgDescOpnd, G4_Operand *msgOpnd0,
                                G4_Operand *msgOpnd1, G4_DstRegRegion *dstOpnd,
                                unsigned ffid, bool hasEOT = false);

  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Send sync related members are in VisaToG4/TranslateSendSync.cpp
  G4_INST *translateLscFence(G4_Predicate *pred, SFID sfid,
                             LSC_FENCE_OP fenceOp, LSC_SCOPE scope,
                             int &status);

  G4_INST *translateLscFence(G4_Predicate *pred, SFID sfid,
                             LSC_FENCE_OP fenceOp, LSC_SCOPE scope) {
    int status = VISA_SUCCESS;
    return translateLscFence(pred, sfid, fenceOp, scope, status);
  }

  ////////////////////////////////////////////////////////////////////////
  // default barrier functions
  void generateSingleBarrier(G4_Predicate *prd, uint32_t id);
  void generateBarrierSend(G4_Predicate *prd, uint32_t id);
  void generateBarrierWait(G4_Predicate *prd, uint32_t id);
  int translateVISASplitBarrierInst(G4_Predicate *prd, bool isSignal);

  ////////////////////////////////////////////////////////////////////////
  // named barrier functions
  int translateVISANamedBarrierSignal(G4_Predicate *prd, G4_Operand *barrierId,
                                      G4_Operand *barrierType,
                                      G4_Operand *numProducers,
                                      G4_Operand *numConsumers);
  int translateVISANamedBarrierWait(G4_Predicate *prd, G4_Operand *barrierId);

  ////////////////////////////////////////////////////////////////////////
  // fence etc

  // this is the old fence op
  // post-LSC platforms should use LSC fences
  G4_INST *createFenceInstructionPreLSC(G4_Predicate *prd, uint8_t flushParam,
                                        bool commitEnable, bool globalMemFence,
                                        bool isSendc);

  G4_INST *createSLMFence(G4_Predicate *prd);

  ////////////////////////////////////////////////////////////////////////
  // other mem sync ops
  int translateVISAWaitInst(G4_Operand *mask);

  int translateVISASyncInst(ISA_Opcode opcode, unsigned int mask);


  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////

  // return either 253 or 255 for A64 messages, depending on whether we want I/A
  // coherency or not
  uint8_t getA64BTI() const {
    return m_options->getOption(vISA_noncoherentStateless) ? 0xFD : 0xFF;
  }

  bool useSends() const {
    return getPlatform() >= GENX_SKL &&
           !(VISA_WA_CHECK(m_pWaTable, WaDisableSendsSrc0DstOverlap));
  }

  Metadata *allocateMD() {
    Metadata *newMD = new (metadataMem) Metadata();
    allMDs.push_back(newMD);
    return newMD;
  }

  MDNode *allocateMDString(const std::string &str) {
    auto newNode = new (metadataMem) MDString(str);
    allMDNodes.push_back(newNode);
    return newNode;
  }

  MDLocation *allocateMDLocation(int line, const char *file) {
    auto newNode = new (metadataMem) MDLocation(line, file);
    allMDNodes.push_back(newNode);
    return newNode;
  }

  MDTokenLocation *allocateMDTokenLocation(unsigned short token,
                                           unsigned globalID) {
    auto newNode = new (metadataMem) MDTokenLocation(token, globalID);
    allMDNodes.push_back(newNode);
    return newNode;
  }

  //FrequencyInfo API
  FrequencyInfo& getFreqInfoManager() { return freqInfoManager;}
  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////
  // Generic IR simplification tasks
  G4_Imm *foldConstVal(G4_Imm* opnd, G4_INST *op);
  G4_Imm *foldConstVal(G4_Imm *const1, G4_Imm *const2, G4_opcode op,
                       bool qwordMode = false);
  void doConsFolding(G4_INST *inst);
  G4_INST *doMathConsFolding(INST_LIST_ITER &iter);
  void doSimplification(G4_INST *inst);

  static G4_Type findConstFoldCommonType(G4_Type type1, G4_Type type2);
  ///////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////

  void materializeGlobalImm(G4_BB *entryBB); // why is in FlowGraph.cpp???

  bool canPromoteToMovi(G4_INST *);

#include "HWCaps.inc"

private:
  G4_INST* getSingleDefInst(G4_INST* UI, Gen4_Operand_Number OpndNum) const;
  G4_SrcRegRegion *createBindlessExDesc(uint32_t exdesc);
  uint32_t createSamplerMsgDesc(VISASampler3DSubOpCode samplerOp,
                                bool isNativeSIMDSize, bool isFP16Return,
                                bool isFP16Input) const;
};

constexpr VISALscImmOffOpts getLscImmOffOpt(LSC_ADDR_TYPE addrType) {
  switch (addrType) {
  case LSC_ADDR_TYPE_FLAT:
    return VISA_LSC_IMMOFF_ADDR_TYPE_FLAT;
  case LSC_ADDR_TYPE_BSS:
    return VISA_LSC_IMMOFF_ADDR_TYPE_BSS;
  case LSC_ADDR_TYPE_SS:
    return VISA_LSC_IMMOFF_ADDR_TYPE_SS;
  case LSC_ADDR_TYPE_BTI:
  case LSC_ADDR_TYPE_ARG:
    return VISA_LSC_IMMOFF_ADDR_TYPE_BTI;
  default:
    break;
  }
  return VISA_LSC_IMMOFF_INVALID;
}
} // namespace vISA

// G4IR instructions added by JIT that do not result from lowering
// any CISA bytecode will be assigned CISA offset = 0xffffffff.
// This includes pseudo nodes, G4_labels, mov introduced for copying
// r0 for pre-emption support.
constexpr int UNMAPPABLE_VISA_INDEX = vISA::IR_Builder::OrphanVISAIndex;

#endif