File: ByteCodeReaderNG.cpp

package info (click to toggle)
intel-graphics-compiler2 2.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 107,552 kB
  • sloc: cpp: 807,012; lisp: 287,936; ansic: 16,397; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 37
file content (3263 lines) | stat: -rw-r--r-- 129,540 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
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

/*
 * ISA Byte Code Reader
 *
 * This library is designed to be extremely reusable and general in nature, and
 * as a result the following byte code reader code primarily uses the following
 * IR and data types:
 *
 * - common_isa_header
 * - kernel_format_t
 * - attribute_info_t
 * - CISA_opnd
 * - vector_opnd
 * - raw_opnd
 * - CISA_INST
 * - std::list<CISA_INST*>
 * - primitives (please use stdint.h types)
 *
 * which are used to load the byte code from a buffer into a simple structured
 * IR.
 *
 * Use of any other data types should be discussed by several members of the CM
 * jitter team before hand.
 *
 */

#include "Assertions.h"
#include "Attributes.hpp"
#include "Common_ISA.h"
#include "Common_ISA_framework.h"
#include "Common_ISA_util.h"
#include "G4_Kernel.hpp"
#include "IsaDisassembly.h"
#include "JitterDataStruct.h"
#include "Mem_Manager.h"
#include "common.h"
#include "visa_igc_common_header.h"

#include "VISAKernel.h"

#include <list>

// bfi can have 7 operands
#define COMMON_ISA_MAX_NUM_OPND_ARITH_LOGIC 7
#define STRING_LEN 1024

using namespace vISA;

struct RoutineContainer {
  RoutineContainer()
      : generalVarDecls(NULL), generalVarsCount(0), addressVarDecls(NULL),
        addressVarsCount(0), predicateVarDecls(NULL), predicateVarsCount(0),
        samplerVarDecls(NULL), samplerVarsCount(0), surfaceVarDecls(NULL),
        surfaceVarsCount(0), labelVarDecls(NULL), labelVarsCount(0),
        inputVarDecls(NULL), inputVarsCount(0), majorVersion(0),
        minorVersion(0) {}

  ~RoutineContainer() { stringPool.clear(); }
  RoutineContainer(const RoutineContainer&) = delete;
  RoutineContainer& operator=(const RoutineContainer&) = delete;
  VISA_GenVar **generalVarDecls;
  unsigned generalVarsCount;
  VISA_AddrVar **addressVarDecls;
  unsigned addressVarsCount;
  VISA_PredVar **predicateVarDecls;
  unsigned predicateVarsCount;
  VISA_SamplerVar **samplerVarDecls;
  unsigned samplerVarsCount;
  VISA_SurfaceVar **surfaceVarDecls;
  unsigned surfaceVarsCount;
  VISA_LabelOpnd **labelVarDecls;
  unsigned labelVarsCount;
  CISA_GEN_VAR **inputVarDecls;
  unsigned inputVarsCount;

  std::vector<std::string> stringPool;

  CISA_IR_Builder *builder = nullptr;
  VISAKernel *kernelBuilder = nullptr;
  uint8_t majorVersion;
  uint8_t minorVersion;
};

/// Assumming buf is start of the CISA byte code.
#define GET_MAJOR_VERSION(buf) (*((unsigned char *)&buf[4]))
#define GET_MINOR_VERSION(buf) (*((unsigned char *)&buf[5]))

#define READ_CISA_FIELD(dst, type, bytePos, buf)                               \
  do {                                                                         \
    dst = *((type *)&buf[bytePos]);                                            \
    bytePos += sizeof(type);                                                   \
  } while (0)

#define PEAK_CISA_FIELD(dst, type, bytePos, buf)                               \
  do {                                                                         \
    dst = *((type *)&buf[bytePos]);                                            \
  } while (0)

typedef enum {
  CISA_EMASK_M0,
  CISA_EMASK_M1,
  CISA_EMASK_M2,
  CISA_EMASK_M3,
  CISA_EMASK_M4,
  CISA_EMASK_M5,
  CISA_EMASK_M6,
  CISA_EMASK_M7,
  CISA_NO_EMASK,
  CISA_DEF_EMASK
} Common_ISA_EMask_Ctrl_3_0;

enum class FIELD_TYPE { DECL, INPUT };

// vISA 3.4+ supports 32-bit general variable IDs
// vISA 3.5+ supports 32-bit input count
template <typename T>
inline void readVarBytes(uint8_t major, uint8_t minor, T &dst,
                         uint32_t &bytePos, const char *buf,
                         FIELD_TYPE field = FIELD_TYPE::DECL) {
  vISA_ASSERT(std::is_integral<T>::value &&
                    (sizeof(T) == 2 || sizeof(T) == 4),
                "T should be short or int");
  uint32_t version = getVersionAsInt(major, minor);
  uintptr_t ptrval = reinterpret_cast<uintptr_t>(&buf[bytePos]);
  bool get4Bytes = false;
  if (field == FIELD_TYPE::DECL) {
    get4Bytes = (version >= getVersionAsInt(3, 4));
  } else if (field == FIELD_TYPE::INPUT) {
    get4Bytes = (version >= getVersionAsInt(3, 5));
  }

  if (get4Bytes) {
    dst = *(reinterpret_cast<uint32_t *>(ptrval));
    bytePos += sizeof(uint32_t);
  } else if (field == FIELD_TYPE::INPUT) {
    dst = *(reinterpret_cast<uint8_t *>(ptrval));
    bytePos += sizeof(uint8_t);
  } else {
    dst = *(reinterpret_cast<uint16_t *>(ptrval));
    bytePos += sizeof(uint16_t);
  }
}

static VISA_EMask_Ctrl transformMask(RoutineContainer &container,
                                     uint8_t maskVal) {
  VISA_EMask_Ctrl mask = vISA_EMASK_M1;
  if (container.majorVersion == 3 && container.minorVersion == 0) {
    Common_ISA_EMask_Ctrl_3_0 tMask = Common_ISA_EMask_Ctrl_3_0(maskVal);
    switch (tMask) {
    case CISA_EMASK_M0: {
      mask = vISA_EMASK_M1;
      break;
    }
    case CISA_EMASK_M1: {
      mask = vISA_EMASK_M2;
      break;
    }
    case CISA_EMASK_M2: {
      mask = vISA_EMASK_M3;
      break;
    }
    case CISA_EMASK_M3: {
      mask = vISA_EMASK_M4;
      break;
    }
    case CISA_EMASK_M4: {
      mask = vISA_EMASK_M5;
      break;
    }
    case CISA_EMASK_M5: {
      mask = vISA_EMASK_M6;
      break;
    }
    case CISA_EMASK_M6: {
      mask = vISA_EMASK_M7;
      break;
    }
    case CISA_EMASK_M7: {
      mask = vISA_EMASK_M8;
      break;
    }
    case CISA_NO_EMASK: {
      mask = vISA_EMASK_M1_NM;
      break;
    }
    case CISA_DEF_EMASK: {
      mask = vISA_EMASK_M1;
      break;
    }
    default:
      break;
    }
  } else {
    mask = VISA_EMask_Ctrl(maskVal);
  }

  return mask;
}
static void readExecSizeNG(unsigned &bytePos, const char *buf,
                           VISA_Exec_Size &size, VISA_EMask_Ctrl &mask,
                           RoutineContainer &container) {
  uint8_t execSize = 0;
  READ_CISA_FIELD(execSize, uint8_t, bytePos, buf);
  uint8_t maskVal = (execSize >> 0x4) & 0xF;

  mask = transformMask(container, maskVal);

  size = (VISA_Exec_Size)((execSize)&0xF);
}

template <typename T>
T readPrimitiveOperandNG(unsigned &bytePos, const char *buf) {
  vISA_ASSERT(buf != nullptr, "Argument Exception: argument buf  is NULL.");
  T data = 0;
  READ_CISA_FIELD(data, T, bytePos, buf);
  return data;
}

static VISA_PredOpnd *readPredicateOperandNG(unsigned &bytePos, const char *buf,
                                             RoutineContainer &container) {
  uint16_t predOpnd = 0;
  READ_CISA_FIELD(predOpnd, uint16_t, bytePos, buf);

  if (0 == predOpnd)
    return nullptr;

  VISAKernel *kernelBuilder = container.kernelBuilder;
  unsigned predID = (predOpnd & 0xfff);
  VISA_PREDICATE_CONTROL control =
      (VISA_PREDICATE_CONTROL)((predOpnd & 0x6000) >> 13);
  VISA_PREDICATE_STATE state =
      (VISA_PREDICATE_STATE)((predOpnd & 0x8000) >> 15);
  VISA_PredVar *decl = container.predicateVarDecls[predID];
  VISA_PredOpnd *opnd = nullptr;

  kernelBuilder->CreateVISAPredicateOperand(opnd, decl, state, control);

  return opnd;
}

static VISA_RawOpnd *readRawOperandNG(unsigned &bytePos, const char *buf,
                                      RoutineContainer &container) {
  vISA_ASSERT_INPUT(buf != nullptr, "Argument Exception: argument buf  is NULL.");
  uint8_t majorVersion = container.majorVersion;
  uint8_t minorVersion = container.minorVersion;

  uint32_t index = 0;
  uint16_t offset = 0;
  readVarBytes(majorVersion, minorVersion, index, bytePos, buf);
  READ_CISA_FIELD(offset, uint16_t, bytePos, buf);

  VISAKernelImpl *kernelBuilderImpl =
      ((VISAKernelImpl *)container.kernelBuilder);

  unsigned numPreDefinedVars = Get_CISA_PreDefined_Var_Count();
  VISA_GenVar *decl = NULL;
  VISA_RawOpnd *opnd = NULL;

  /**
      Null register is treated differently now. There is special operand NullReg
     created. In it field isNullReg is set to true.

      TODO:? To make things more generic need to mark decl created during
     initialization as null register, then when CreateVisaRawOperand is called
     check that decl passed in is null register decl, and mark operand is
     nullReg, also create region <0;1,0>
  */
  if (index == 0) {
    kernelBuilderImpl->CreateVISANullRawOperand(opnd, true); // dst
  } else {
    if (index >= numPreDefinedVars)
      decl = container.generalVarDecls[index];
    else
      kernelBuilderImpl->GetPredefinedVar(decl, (PreDefined_Vars)index);

    kernelBuilderImpl->CreateVISARawOperand(opnd, decl, offset);
  }

  return opnd;
}

static VISA_PredVar *readPreVarNG(unsigned &bytePos, const char *buf,
                                  RoutineContainer &container) {
  vISA_ASSERT_INPUT(buf != nullptr, "Argument Exception: argument buf  is NULL.");

  [[maybe_unused]] uint8_t tag = 0;
  READ_CISA_FIELD(tag, uint8_t, bytePos, buf);

  uint16_t index = 0;
  READ_CISA_FIELD(index, uint16_t, bytePos, buf);

  uint16_t predIndex = index & 0xfff;
  VISA_PredVar *decl = NULL;

  if (predIndex >= COMMON_ISA_NUM_PREDEFINED_PRED)
    decl = container.predicateVarDecls[predIndex];
  return decl;
}

static uint32_t readOtherOperandNG(unsigned &bytePos, const char *buf,
                                   VISA_Type visatype) {
  union {
    uint32_t other_opnd;
    struct {
      uint8_t b[4];
    };
  } v;

  unsigned bsize = CISATypeTable[visatype].typeSize;
  vISA_ASSERT_INPUT(bsize <= 4, " Unsupported other_opnd whose size > 4 bytes!");
  v.other_opnd = 0;
  for (int i = 0; i < (int)bsize; ++i) {
    v.b[i] = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  }
  return v.other_opnd;
}

static VISA_VectorOpnd *readVectorOperandNG(unsigned &bytePos, const char *buf,
                                            uint8_t &tag,
                                            RoutineContainer &container,
                                            unsigned int size, bool isDst,
                                            bool isAddressoff = false) {
  vISA_ASSERT_INPUT(buf != nullptr, "Argument Exception: argument buf  is NULL.");

  VISAKernelImpl *kernelBuilderImpl =
      ((VISAKernelImpl *)container.kernelBuilder);

  uint8_t majorVersion = container.majorVersion;
  uint8_t minorVersion = container.minorVersion;

  READ_CISA_FIELD(tag, uint8_t, bytePos, buf);
  VISA_Modifier modifier = ((VISA_Modifier)((tag >> 3) & 0x7));

  switch ((Common_ISA_Operand_Class)(tag & 0x7)) /// getOperandClass
  {
  case OPERAND_GENERAL: {
    uint32_t index = 0;
    uint8_t rowOffset = 0;
    uint8_t colOffset = 0;
    uint16_t region = 0;

    readVarBytes(majorVersion, minorVersion, index, bytePos, buf);
    READ_CISA_FIELD(rowOffset, uint8_t, bytePos, buf);
    READ_CISA_FIELD(colOffset, uint8_t, bytePos, buf);
    READ_CISA_FIELD(region, uint16_t, bytePos, buf);

    uint16_t v_stride =
        Get_Common_ISA_Region_Value((Common_ISA_Region_Val)(region & 0xF));
    uint16_t width = Get_Common_ISA_Region_Value(
        (Common_ISA_Region_Val)((region >> 4) & 0xF));
    uint16_t h_stride = Get_Common_ISA_Region_Value(
        (Common_ISA_Region_Val)((region >> 8) & 0xF));

    unsigned numPreDefinedVars = Get_CISA_PreDefined_Var_Count();

    VISA_Modifier mod = modifier;
    VISA_VectorOpnd *opnd = NULL;
    VISA_GenVar *decl = NULL;

    if (index >= numPreDefinedVars)
      decl = container.generalVarDecls[index];
    else
      kernelBuilderImpl->GetPredefinedVar(decl, (PreDefined_Vars)index);

    if (isDst)
      kernelBuilderImpl->CreateVISADstOperand(opnd, decl, h_stride, rowOffset,
                                              colOffset);
    else if (isAddressoff) {
      VISA_Type vType = decl->genVar.getType();
      G4_Type gType = GetGenTypeFromVISAType(vType);
      unsigned int offset =
          colOffset * TypeSize(gType) +
          rowOffset * kernelBuilderImpl->getKernel()->numEltPerGRF<Type_UB>();
      kernelBuilderImpl->CreateVISAAddressOfOperand(opnd, decl, offset);
    } else {
      kernelBuilderImpl->CreateVISASrcOperand(opnd, decl, mod, v_stride, width,
                                              h_stride, rowOffset, colOffset);
    }

    return opnd;
  }
  case OPERAND_ADDRESS: {
    uint16_t index = 0;
    uint8_t offset = 0;
    uint16_t width = 0;

    READ_CISA_FIELD(index, uint16_t, bytePos, buf);
    READ_CISA_FIELD(offset, uint8_t, bytePos, buf);
    READ_CISA_FIELD(width, uint8_t, bytePos, buf);

    VISA_VectorOpnd *opnd = NULL;
    VISA_AddrVar *decl = container.addressVarDecls[index];
    kernelBuilderImpl->CreateVISAAddressOperand(
        opnd, decl, offset, Get_VISA_Exec_Size((VISA_Exec_Size)width), isDst);

    return opnd;
  }
  case OPERAND_PREDICATE: {
    uint16_t index = 0;
    READ_CISA_FIELD(index, uint16_t, bytePos, buf);

    uint16_t predIndex = index & 0xfff;
    vASSERT(predIndex >= COMMON_ISA_NUM_PREDEFINED_PRED);

    VISA_PredVar *decl = container.predicateVarDecls[predIndex];
    VISA_VectorOpnd *opnd = nullptr;
    if (isDst) {
      kernelBuilderImpl->CreateVISAPredicateDstOperand(opnd, decl, size);
      return opnd;
    } else {
      kernelBuilderImpl->CreateVISAPredicateSrcOperand(opnd, decl, size);
      return opnd;
    }
  }
  case OPERAND_INDIRECT: {
    uint16_t index = 0;
    uint8_t addr_offset = 0;
    int16_t indirect_offset = 0;
    uint8_t bit_property = 0;
    uint16_t region = 0;

    READ_CISA_FIELD(index, uint16_t, bytePos, buf);
    READ_CISA_FIELD(addr_offset, uint8_t, bytePos, buf);
    READ_CISA_FIELD(indirect_offset, int16_t, bytePos, buf);
    READ_CISA_FIELD(bit_property, uint8_t, bytePos, buf);
    READ_CISA_FIELD(region, uint16_t, bytePos, buf);

    uint16_t v_stride =
        Get_Common_ISA_Region_Value((Common_ISA_Region_Val)((region)&0xF));
    uint16_t width = Get_Common_ISA_Region_Value(
        (Common_ISA_Region_Val)((region >> 4) & 0xF));
    uint16_t h_stride = Get_Common_ISA_Region_Value(
        (Common_ISA_Region_Val)((region >> 8) & 0xF));

    VISA_Modifier mod = modifier;
    VISA_VectorOpnd *opnd = NULL;
    VISA_AddrVar *decl = container.addressVarDecls[index];

    kernelBuilderImpl->CreateVISAIndirectGeneralOperand(
        opnd, decl, mod, addr_offset, indirect_offset, v_stride, width,
        h_stride, (VISA_Type)(bit_property & 0xF), isDst);

    return opnd;
  }
  case OPERAND_IMMEDIATE: {
    uint8_t type = 0;
    READ_CISA_FIELD(type, uint8_t, bytePos, buf);
    VISA_Type immedType = (VISA_Type)(type & 0xF);

    VISA_VectorOpnd *opnd = NULL;

    if (immedType == ISA_TYPE_DF) {
      double val = 0;
      READ_CISA_FIELD(val, double, bytePos, buf);
      kernelBuilderImpl->CreateVISAImmediate(opnd, &val, immedType);
    } else if (immedType == ISA_TYPE_Q || immedType == ISA_TYPE_UQ) {
      uint64_t val = 0;
      READ_CISA_FIELD(val, uint64_t, bytePos, buf);
      kernelBuilderImpl->CreateVISAImmediate(opnd, &val, immedType);
    } else /// Immediate operands are at least 4 bytes.
    {
      unsigned val = 0;
      READ_CISA_FIELD(val, unsigned, bytePos, buf);
      kernelBuilderImpl->CreateVISAImmediate(opnd, &val, immedType);
    }

    return opnd;
  }
  case OPERAND_STATE: {
    uint8_t opnd_class = 0;
    uint16_t index = 0;
    uint8_t offset = 0;

    READ_CISA_FIELD(opnd_class, uint8_t, bytePos, buf);
    READ_CISA_FIELD(index, uint16_t, bytePos, buf);
    READ_CISA_FIELD(offset, uint8_t, bytePos, buf);

    VISA_VectorOpnd *opnd = NULL;

    switch ((Common_ISA_State_Opnd_Class)opnd_class) {
    case STATE_OPND_SURFACE: {
      if (isAddressoff) {
        VISA_SurfaceVar *decl = container.surfaceVarDecls[index];
        unsigned int offsetB = offset * TypeSize(Type_UW);
        kernelBuilderImpl->CreateVISAAddressOfOperand(opnd, decl, offsetB);
      } else {
        VISA_SurfaceVar *decl = container.surfaceVarDecls[index];
        kernelBuilderImpl->CreateVISAStateOperand(opnd, decl, (uint8_t)size,
                                                  offset, isDst);
      }
      break;
    }
    case STATE_OPND_SAMPLER: {
      VISA_SamplerVar *decl = container.samplerVarDecls[index];
      if (isAddressoff) {
        unsigned int offsetB = offset * TypeSize(Type_UW);
        kernelBuilderImpl->CreateVISAAddressOfOperandGeneric(opnd, decl,
                                                             offsetB);
      } else {
        kernelBuilderImpl->CreateVISAStateOperand(opnd, decl, (uint8_t)size,
                                                  offset, isDst);
      }
      break;
    }
    default: {
      vISA_ASSERT_UNREACHABLE("Invalid state operand class: only surface and "
                          "sampler are supported.");
      break;
    }
    }

    return opnd;
  }
  default:
    vISA_ASSERT_UNREACHABLE("Operand class not recognized");
    return NULL;
  }
}

static VISA_VectorOpnd *readVectorOperandNG(unsigned &bytePos, const char *buf,
                                            RoutineContainer &container,
                                            unsigned int size) {
  uint8_t tag = 0;
  bool isDst = false;
  return readVectorOperandNG(bytePos, buf, tag, container, size, isDst);
}

static VISA_VectorOpnd *readVectorOperandNG(unsigned &bytePos, const char *buf,
                                            RoutineContainer &container,
                                            bool isDst) {
  uint8_t tag = 0;
  return readVectorOperandNG(bytePos, buf, tag, container, 1, isDst);
}

static VISA_VectorOpnd *
readVectorOperandNGAddressOf(unsigned &bytePos, const char *buf,
                             RoutineContainer &container) {
  uint8_t tag = 0;
  bool isDst = false;
  bool isAddressOff = true;
  return readVectorOperandNG(bytePos, buf, tag, container, 1, isDst,
                             isAddressOff);
}

static void readInstructionCommonNG(unsigned &bytePos, const char *buf,
                                    ISA_Opcode opcode,
                                    RoutineContainer &container) {
  VISA_EMask_Ctrl emask = vISA_EMASK_M1;
  VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;

  VISA_INST_Desc *inst_desc = &CISA_INST_table[opcode];
  unsigned opnd_count = inst_desc->opnd_num;
  unsigned opnd_skip = 0;

  for (unsigned i = 0; i < 2; i++) {
    if ((opnd_count - opnd_skip) > 0 &&
        (inst_desc->opnd_desc[i].opnd_type == OPND_EXECSIZE ||
         inst_desc->opnd_desc[i].opnd_type == OPND_PRED)) {
      opnd_skip++;
    }
  }

  VISAKernel *kernelBuilder = container.kernelBuilder;

  switch (ISA_Inst_Table[opcode].type) {
  case ISA_Inst_Mov:
  case ISA_Inst_Arith:
  case ISA_Inst_Logic:
  case ISA_Inst_Address:
  case ISA_Inst_Compare: {
    VISA_VectorOpnd *opnds[COMMON_ISA_MAX_NUM_OPND_ARITH_LOGIC];
    vISA_ASSERT_INPUT(
        opnd_count <= COMMON_ISA_MAX_NUM_OPND_ARITH_LOGIC,
        "Insturction operand count exceeds maximum supported operands.");
    memset(opnds, 0,
           sizeof(VISA_VectorOpnd *) * COMMON_ISA_MAX_NUM_OPND_ARITH_LOGIC);

    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = hasPredicate(opcode)
                              ? readPredicateOperandNG(bytePos, buf, container)
                              : NULL;

    uint8_t opSpec = 0;
    if (ISA_FMINMAX == opcode || ISA_CMP == opcode) {
      opSpec =
          readPrimitiveOperandNG<uint8_t>(bytePos, buf); /// rel_Op or opext
      opnd_skip++;
    }
    uint8_t bfn_func_ctrl = 0;

    uint32_t exSize = Get_VISA_Exec_Size(esize);
    uint8_t tag = 0;
    VISA_PredVar *dstDcl = NULL;
    bool cmpHasDst = false;
    for (unsigned i = 0; i < opnd_count - opnd_skip; i++) {
      bool isDst = i == 0;

      if ((OPND_DST_GEN & inst_desc->opnd_desc[i + opnd_skip].opnd_type) != 0) {
        isDst = true;
      }

      if (isDst) {
        if (ISA_Inst_Table[opcode].type == ISA_Inst_Compare) {
          opnds[i] = NULL;
          PEAK_CISA_FIELD(tag, uint8_t, bytePos, buf);

          if ((tag & 0x7) == OPERAND_GENERAL) {
            opnds[i] = readVectorOperandNG(bytePos, buf, tag, container,
                                           Get_VISA_Exec_Size(esize), true);
            cmpHasDst = true;

          } else {
            dstDcl = readPreVarNG(bytePos, buf, container);
          }
        } else
          opnds[i] =
              readVectorOperandNG(bytePos, buf, tag, container, exSize, isDst);
      } else if (ISA_Inst_Table[opcode].type == ISA_Inst_Address && i == 1) {
        // for first source of address add instruction.
        opnds[i] = readVectorOperandNGAddressOf(bytePos, buf, container);
      } else if (i == 4 && opcode == ISA_BFN) {
        // read bfn booleanFuncCtrl from the last opnd
        int opnd_ix = i + opnd_skip;
        vISA_ASSERT_INPUT(inst_desc->opnd_desc[opnd_ix].opnd_type == OPND_OTHER,
               "BFN: FuncCtrl opnd_desc's type should be OPND_OTHER!");
        VISA_Type visatype = (VISA_Type)inst_desc->opnd_desc[opnd_ix].data_type;
        bfn_func_ctrl = readOtherOperandNG(bytePos, buf, visatype);
      } else {
        opnds[i] = readVectorOperandNG(bytePos, buf, container, exSize);
      }
    }

    opnd_count -= opnd_skip;

    // skip booleanFuncCtrl, which is the last opnd
    if (opcode == ISA_BFN)
      --opnd_count;

    bool saturate = (((VISA_Modifier)((tag >> 3) & 0x7)) == MODIFIER_SAT);
    VISA_VectorOpnd *dst = opnds[0];
    VISA_VectorOpnd *src0 = opnds[1];
    VISA_VectorOpnd *src1 = opnd_count > 2 ? opnds[2] : NULL;
    VISA_VectorOpnd *src2 = opnd_count > 3 ? opnds[3] : NULL;
    VISA_VectorOpnd *src3 = opnd_count > 4 ? opnds[4] : NULL;

    switch (ISA_Inst_Table[opcode].type) {
    case ISA_Inst_Mov:
      if (opcode == ISA_FMINMAX)
        kernelBuilder->AppendVISAMinMaxInst((CISA_MIN_MAX_SUB_OPCODE)opSpec,
                                            saturate, emask, esize, dst, src0,
                                            src1);
      else
        kernelBuilder->AppendVISADataMovementInst(opcode, pred, saturate, emask,
                                                  esize, dst, src0, src1);
      break;
    case ISA_Inst_Arith:
      if (opcode == ISA_ADDC || opcode == ISA_SUBB) {
        kernelBuilder->AppendVISATwoDstArithmeticInst(
            opcode, pred, emask, esize, dst, src0, src1, src2);
      } else {
        kernelBuilder->AppendVISAArithmeticInst(opcode, pred, saturate, emask,
                                                esize, dst, src0, src1, src2);
      }
      break;
    case ISA_Inst_Logic:
      if (opcode == ISA_BFN)
        kernelBuilder->AppendVISABfnInst(bfn_func_ctrl, pred, saturate, emask,
                                         esize, dst, src0, src1, src2);
      else
        kernelBuilder->AppendVISALogicOrShiftInst(
            opcode, pred, saturate, emask, esize, dst, src0, src1, src2, src3);
      break;
    case ISA_Inst_Address:
      kernelBuilder->AppendVISAAddrAddInst(emask, esize, dst, src0, src1);
      break;
    case ISA_Inst_Compare:
      if (dstDcl)
        kernelBuilder->AppendVISAComparisonInst(
            (VISA_Cond_Mod)(opSpec & 0x7), emask, esize, dstDcl, src0, src1);
      else if (cmpHasDst)
        kernelBuilder->AppendVISAComparisonInst((VISA_Cond_Mod)(opSpec & 0x7),
                                                emask, esize, dst, src0, src1);
      break;
    default:
      break;
    }

    break;
  }
  case ISA_Inst_SIMD_Flow: {
    vISA_ASSERT_INPUT(opcode == ISA_GOTO, "expect goto instruction");
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = hasPredicate(opcode)
                              ? readPredicateOperandNG(bytePos, buf, container)
                              : nullptr;
    VISA_LabelOpnd *label =
        opcode == ISA_GOTO
            ? container
                  .labelVarDecls[readPrimitiveOperandNG<uint16_t>(bytePos, buf)]
            : nullptr;
    kernelBuilder->AppendVISACFGotoInst(pred, emask, esize, label);
    break;
  }
  case ISA_Inst_Sync: {
    if (opcode == ISA_WAIT) {
      VISA_VectorOpnd *mask = NULL;
      if (getVersionAsInt(container.majorVersion, container.minorVersion) >=
          getVersionAsInt(3, 1)) {
        // additional vector operand
        mask = readVectorOperandNG(bytePos, buf, container, false);
      } else {
        // set mask to 0
        uint16_t value = 0;
        kernelBuilder->CreateVISAImmediate(mask, &value, ISA_TYPE_UW);
      }
      kernelBuilder->AppendVISAWaitInst(mask);
    } else if (opcode == ISA_SBARRIER) {
      uint32_t mode = readOtherOperandNG(bytePos, buf, ISA_TYPE_UB);
      kernelBuilder->AppendVISASplitBarrierInst(mode != 0);
    } else if (opcode == ISA_NBARRIER) {
      // Still support reading visa binary ?
      uint32_t mode = readOtherOperandNG(bytePos, buf, ISA_TYPE_UB);
      auto barrierId = readVectorOperandNG(bytePos, buf, container, false);
      VISA_VectorOpnd *barrierType =
          readVectorOperandNG(bytePos, buf, container, false);
      VISA_VectorOpnd *numProds =
          readVectorOperandNG(bytePos, buf, container, false);
      VISA_VectorOpnd *numCons =
          readVectorOperandNG(bytePos, buf, container, false);
      bool isWait = (mode == 0);
      if (isWait) {
        kernelBuilder->AppendVISANamedBarrierWait(barrierId);
      } else {
        const auto &vo = barrierType->_opnd.v_opnd;
        if ((vo.tag & 0x7) == OPERAND_IMMEDIATE &&
            vo.opnd_val.const_opnd._val.lval == 0 && numProds == numCons) {
          kernelBuilder->AppendVISANamedBarrierSignal(barrierId, numProds);
        } else {
          kernelBuilder->AppendVISANamedBarrierSignal(barrierId, barrierType,
                                                      numProds, numCons);
        }
      }
    } else {
      bool hasMask = (opcode == ISA_FENCE);
      uint8_t mask =
          hasMask ? readPrimitiveOperandNG<uint8_t>(bytePos, buf) : 0;
      kernelBuilder->AppendVISASyncInst(opcode, mask);
    }
    break;
  }
  default: {
    vISA_ASSERT_UNREACHABLE("Invalid common instruction type.");
  }
  }
}

/// Read a byte which encodes the atomic opcode and a flag indicating whether
/// this is a 16bit atomic operation.
std::tuple<VISAAtomicOps, unsigned short>
getAtomicOpAndBitwidth(unsigned &bytePos, const char *buf) {
  // bits 0-4 atomic op and bit 5-6 encode the bitwidth
  uint8_t data = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  VISAAtomicOps op = static_cast<VISAAtomicOps>(data & 0x1F);
  unsigned short bitwidth;
  if (data >> 5 == 1)
    bitwidth = 16;
  else if (data >> 6 == 1)
    bitwidth = 64;
  else
    bitwidth = 32;
  return std::tie(op, bitwidth);
}

static void readInstructionDataportNG(unsigned &bytePos, const char *buf,
                                      ISA_Opcode opcode,
                                      RoutineContainer &container) {
  VISAKernel *kernelBuilder = container.kernelBuilder;
  VISAKernelImpl *kernelBuilderImpl = ((VISAKernelImpl *)kernelBuilder);

  switch (opcode) {
  case ISA_MEDIA_ST:
  case ISA_MEDIA_LD: {
    uint8_t modifier = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t plane = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t width = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t height = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_opnd *xoffset = readVectorOperandNG(bytePos, buf, container, false);
    VISA_opnd *yoffset = readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *msg = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    kernelBuilder->AppendVISASurfAccessMediaLoadStoreInst(
        opcode, (MEDIA_LD_mod)modifier, surfaceHnd, width, height,
        (VISA_VectorOpnd *)xoffset, (VISA_VectorOpnd *)yoffset, msg,
        (CISA_PLANE_ID)plane);
    break;
  }
  case ISA_OWORD_ST:
  case ISA_OWORD_LD:
  case ISA_OWORD_LD_UNALIGNED: {
    uint8_t size = readPrimitiveOperandNG<uint8_t>(bytePos, buf) & 0x7;
    if (ISA_OWORD_ST != opcode) {
      readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    } // modifier
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *offset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *msg = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilder->AppendVISASurfAccessOwordLoadStoreInst(
        opcode, vISA_EMASK_M1, surfaceHnd, (VISA_Oword_Num)size,
        (VISA_VectorOpnd *)offset, msg);

    break;
  }
  case ISA_GATHER:
  case ISA_SCATTER: {
    uint8_t elt_size = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    if (ISA_GATHER == opcode) {
      readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    } // modifier
    uint8_t num_elts = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_VectorOpnd *globalOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *elementOffset = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *msg = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;

    /// TODO: Conversions like these make using vISA builder cumbersome.
    switch (num_elts & 0x3) {
    case 0:
      esize = EXEC_SIZE_8;
      break;
    case 1:
      esize = EXEC_SIZE_16;
      break;
    case 2:
      esize = EXEC_SIZE_1;
      break;
    default:
      vISA_ASSERT_UNREACHABLE("Invalid Number of Elements for Gather/Scatter.");
    }

    emask = transformMask(container, num_elts >> 4);

    kernelBuilderImpl->AppendVISASurfAccessGatherScatterInst(
        opcode, emask, (GATHER_SCATTER_ELEMENT_SIZE)(elt_size & 0x3), esize,
        surfaceHnd, globalOffset, elementOffset, msg);
    break;
  }
  case ISA_GATHER4_TYPED:
  case ISA_SCATTER4_TYPED: {
    if (getVersionAsInt(container.majorVersion, container.minorVersion) >=
        getVersionAsInt(3, 2)) {
      VISA_EMask_Ctrl emask = vISA_EMASK_M1;
      VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
      readExecSizeNG(bytePos, buf, esize, emask, container);

      VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
      unsigned ch_mask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
      uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      VISA_RawOpnd *uOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *vOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *rOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *lod = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *msg = readRawOperandNG(bytePos, buf, container);

      VISA_StateOpndHandle *surfaceHnd = NULL;
      kernelBuilderImpl->CreateVISAStateOperandHandle(
          surfaceHnd, container.surfaceVarDecls[surface]);
      kernelBuilderImpl->AppendVISASurfAccessGather4Scatter4TypedInst(
          opcode, pred, ChannelMask::createAPIFromBinary(opcode, ch_mask),
          emask, esize, surfaceHnd, uOffset, vOffset, rOffset, lod, msg);
    } else {
      uint8_t ch_mask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      VISA_EMask_Ctrl emask = vISA_EMASK_M1;
      VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
      readExecSizeNG(bytePos, buf, esize, emask, container);

      vISA_ASSERT_INPUT(esize == 0, "Unsupported number of elements for "
                               "ISA_SCATTER4_TYPED/ISA_GATHER4_TYPED.");
      esize = EXEC_SIZE_8;

      uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      VISA_RawOpnd *uOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *vOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *rOffset = readRawOperandNG(bytePos, buf, container);
      VISA_RawOpnd *lod = NULL;
      kernelBuilderImpl->CreateVISANullRawOperand(lod, false);
      VISA_RawOpnd *msg = readRawOperandNG(bytePos, buf, container);

      VISA_StateOpndHandle *surfaceHnd = NULL;
      kernelBuilderImpl->CreateVISAStateOperandHandle(
          surfaceHnd, container.surfaceVarDecls[surface]);
      ch_mask = ~ch_mask;
      kernelBuilderImpl->AppendVISASurfAccessGather4Scatter4TypedInst(
          opcode, NULL, ChannelMask::createAPIFromBinary(opcode, ch_mask),
          emask, esize, surfaceHnd, uOffset, vOffset, rOffset, lod, msg);
    }
    break;
  }
  case ISA_3D_RT_WRITE: {
    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    uint16_t mode = readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *r1HeaderOpnd = readRawOperandNG(bytePos, buf, container);

    vISA_RT_CONTROLS cntrls;
    cntrls.s0aPresent = (mode & (0x1 << 3)) ? true : false;
    cntrls.oMPresent = (mode & (0x1 << 4)) ? true : false;
    cntrls.zPresent = (mode & (0x1 << 5)) ? true : false;
    cntrls.isStencil = (mode & (0x1 << 6)) ? true : false;
    cntrls.isLastWrite = (mode & (0x1 << 7)) ? true : false;
    bool CPSEnable = (mode & (0x1 << 8)) ? true : false;
    cntrls.isPerSample = (mode & (0x1 << 9)) ? true : false;
    cntrls.isCoarseMode = (mode & (0x1 << 10)) ? true : false;
    cntrls.isSampleIndex = (mode & (0x1 << 11)) ? true : false;
    cntrls.RTIndexPresent = (mode & (0x1 << 2)) ? true : false;
    cntrls.isNullRT = (mode & (0x1 << 12)) ? true : false;
    cntrls.isHeaderMaskfromCe0 = 0;

    VISA_VectorOpnd *sampleIndex =
        cntrls.isSampleIndex
            ? readVectorOperandNG(bytePos, buf, container, false)
            : NULL;
    VISA_VectorOpnd *cpsCounter =
        CPSEnable ? readVectorOperandNG(bytePos, buf, container, false) : NULL;
    VISA_VectorOpnd *rti =
        cntrls.RTIndexPresent
            ? readVectorOperandNG(bytePos, buf, container, false)
            : NULL;
    VISA_RawOpnd *s0a =
        cntrls.s0aPresent ? readRawOperandNG(bytePos, buf, container) : NULL;
    VISA_RawOpnd *oM =
        cntrls.oMPresent ? readRawOperandNG(bytePos, buf, container) : NULL;
    VISA_RawOpnd *R = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *G = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *B = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *A = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *Z =
        cntrls.zPresent ? readRawOperandNG(bytePos, buf, container) : NULL;
    VISA_RawOpnd *S =
        cntrls.isStencil ? readRawOperandNG(bytePos, buf, container) : NULL;

    std::vector<VISA_RawOpnd *> rawOpndVector;
    if (s0a)
      rawOpndVector.push_back(s0a);
    if (oM)
      rawOpndVector.push_back(oM);
    if (R)
      rawOpndVector.push_back(R);
    if (G)
      rawOpndVector.push_back(G);
    if (B)
      rawOpndVector.push_back(B);
    if (A)
      rawOpndVector.push_back(A);
    if (Z)
      rawOpndVector.push_back(Z);
    if (S)
      rawOpndVector.push_back(S);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISA3dRTWriteCPS(
        pred, emask, esize, rti, cntrls, surfaceHnd, r1HeaderOpnd, sampleIndex,
        cpsCounter, (uint8_t)rawOpndVector.size(), rawOpndVector.data());
    break;
  }
  case ISA_GATHER4_SCALED:
  case ISA_SCATTER4_SCALED: {
    VISA_EMask_Ctrl eMask = vISA_EMASK_M1;
    VISA_Exec_Size exSize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, exSize, eMask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    unsigned channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    // scale is ignored and must be zero
    (void)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *globalOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dstOrSrc = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISASurfAccessGather4Scatter4ScaledInst(
        opcode, pred, eMask, exSize,
        ChannelMask::createAPIFromBinary(opcode, channelMask), surfaceHnd,
        globalOffset, offsets, dstOrSrc);
    break;
  }
  case ISA_GATHER_SCALED:
  case ISA_SCATTER_SCALED: {
    VISA_EMask_Ctrl eMask = vISA_EMASK_M1;
    VISA_Exec_Size exSize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, exSize, eMask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    // block size is ignored (MBZ)
    (void)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_SVM_Block_Num numBlocks =
        VISA_SVM_Block_Num(readPrimitiveOperandNG<uint8_t>(bytePos, buf));
    // scale is ignored (MBZ)
    (void)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *globalOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dstOrSrc = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISASurfAccessScatterScaledInst(
        opcode, pred, eMask, exSize, numBlocks, surfaceHnd, globalOffset,
        offsets, dstOrSrc);
    break;
  }
  case ISA_DWORD_ATOMIC: {
    VISAAtomicOps subOpc;
    unsigned short bitwidth;
    std::tie(subOpc, bitwidth) = getAtomicOpAndBitwidth(bytePos, buf);

    VISA_EMask_Ctrl eMask = vISA_EMASK_M1;
    VISA_Exec_Size exSize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, exSize, eMask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    unsigned surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISASurfAccessDwordAtomicInst(
        pred, subOpc, bitwidth == 16, eMask, exSize, surfaceHnd, offsets, src0,
        src1, dst);
    break;
  }
  case ISA_3D_TYPED_ATOMIC: {
    VISAAtomicOps subOpc;
    unsigned short bitwidth;
    std::tie(subOpc, bitwidth) = getAtomicOpAndBitwidth(bytePos, buf);

    VISA_EMask_Ctrl eMask = vISA_EMASK_M1;
    VISA_Exec_Size exSize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, exSize, eMask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    unsigned surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *u = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *v = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *r = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *lod = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISA3dTypedAtomic(subOpc, bitwidth == 16, pred,
                                               eMask, exSize, surfaceHnd, u, v,
                                               r, lod, src0, src1, dst);
    break;
  }
  case ISA_QW_GATHER:
  case ISA_QW_SCATTER: {
    VISA_EMask_Ctrl eMask = vISA_EMASK_M1;
    VISA_Exec_Size exSize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, exSize, eMask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    VISA_SVM_Block_Num numBlocks =
        VISA_SVM_Block_Num(readPrimitiveOperandNG<uint8_t>(bytePos, buf));
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dstOrSrc = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = nullptr;
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    if (opcode == ISA_QW_GATHER) {
      kernelBuilderImpl->AppendVISAQwordGatherInst(
          pred, eMask, exSize, numBlocks, surfaceHnd, offsets, dstOrSrc);
    } else {
      kernelBuilderImpl->AppendVISAQwordScatterInst(
          pred, eMask, exSize, numBlocks, surfaceHnd, offsets, dstOrSrc);
    }
    break;
  }
  default: {
    vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal DataPort Opcode.");
  }
  }
}

static void readInstructionControlFlow(unsigned &bytePos, const char *buf,
                                       ISA_Opcode opcode,
                                       RoutineContainer &container) {
  VISAKernel *kernelBuilder = container.kernelBuilder;

  VISA_EMask_Ctrl emask = vISA_EMASK_M1;
  VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;

  switch (opcode) {
  case ISA_SUBROUTINE:
  case ISA_LABEL: {
    uint16_t labelId = readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    VISA_LabelOpnd *label = container.labelVarDecls[labelId];
    kernelBuilder->AppendVISACFLabelInst(label);
    return;
  }
  case ISA_JMP:
  case ISA_RET:
  case ISA_CALL:
  case ISA_FRET:
  case ISA_FCALL: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = hasPredicate(opcode)
                              ? readPredicateOperandNG(bytePos, buf, container)
                              : NULL;

    uint16_t labelId =
        (opcode == ISA_JMP || opcode == ISA_CALL || opcode == ISA_FCALL)
            ? readPrimitiveOperandNG<uint16_t>(bytePos, buf)
            : 0;

    if (opcode == ISA_FCALL) {
      uint8_t argSize = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
      uint8_t retSize = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
      kernelBuilder->AppendVISACFFunctionCallInst(
          pred, emask, esize, container.stringPool[labelId], argSize, retSize);
      return;
    }

    switch (opcode) {
    case ISA_JMP:
      kernelBuilder->AppendVISACFJmpInst(pred,
                                         container.labelVarDecls[labelId]);
      return;
    case ISA_CALL:
      kernelBuilder->AppendVISACFCallInst(pred, emask, esize,
                                          container.labelVarDecls[labelId]);
      return;

    case ISA_RET:
      kernelBuilder->AppendVISACFRetInst(pred, emask, esize);
      return;
    case ISA_FRET:
      kernelBuilder->AppendVISACFFunctionRetInst(pred, emask, esize);
      return;

    default:
      vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal Control Flow Opcode.");
      return;
    }

    return;
  }
  case ISA_IFCALL: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = hasPredicate(opcode)
                              ? readPredicateOperandNG(bytePos, buf, container)
                              : nullptr;

    bool isUniform = false;
    VISA_VectorOpnd *funcAddr =
        readVectorOperandNG(bytePos, buf, container, false);
    uint8_t argSize = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t retSize = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    kernelBuilder->AppendVISACFIndirectFuncCallInst(
        pred, emask, esize, isUniform, funcAddr, argSize, retSize);
    return;
  }
  case ISA_FADDR: {
    uint16_t sym_name_idx = readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    VISA_VectorOpnd *dst = readVectorOperandNG(bytePos, buf, container, true);
    kernelBuilder->AppendVISACFSymbolInst(container.stringPool[sym_name_idx],
                                          dst);
    return;
  }
  case ISA_SWITCHJMP: {
    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    uint8_t numLabels = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    vISA_ASSERT_INPUT(0 < numLabels && numLabels < 33,
                 "Number of labels in SWITCHJMP must be between 1 and 32.");

    VISA_VectorOpnd *index =
        readVectorOperandNG(bytePos, buf, container, false);

    VISA_LabelOpnd *labels[32]; /// 32 is max
    for (unsigned i = 0; i < numLabels; i++)
      labels[i] =
          container
              .labelVarDecls[readPrimitiveOperandNG<uint16_t>(bytePos, buf)];

    kernelBuilder->AppendVISACFSwitchJMPInst(index, numLabels, labels);
    break;
  }
  default:
    vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal Control Flow Opcode.");
  }
}

static void readInstructionMisc(unsigned &bytePos, const char *buf,
                                ISA_Opcode opcode,
                                RoutineContainer &container) {
  VISAKernel *kernelBuilder = container.kernelBuilder;

  switch (opcode) {
  case ISA_FILE: {
    uint32_t versionInt =
        getVersionAsInt(container.majorVersion, container.minorVersion);
    bool is3Dot4Plus = versionInt >= getVersionAsInt(3, 4);
    uint32_t filenameIndex =
        is3Dot4Plus ? readPrimitiveOperandNG<uint32_t>(bytePos, buf)
                    : readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    const char *filename = container.stringPool[filenameIndex].c_str();
    kernelBuilder->AppendVISAMiscFileInst((char *)filename);
    break;
  }
  case ISA_LOC: {
    unsigned lineNumber = readPrimitiveOperandNG<unsigned>(bytePos, buf);
    kernelBuilder->AppendVISAMiscLOC(lineNumber);
    break;
  }
  case ISA_RAW_SEND: {
    uint8_t modifier = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    unsigned exMsgDesc = readPrimitiveOperandNG<unsigned>(bytePos, buf);
    uint8_t numSrc = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t numDst = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_VectorOpnd *desc = readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *src = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    kernelBuilder->AppendVISAMiscRawSend(pred, emask, esize, modifier,
                                         exMsgDesc, numSrc, numDst, desc, src,
                                         dst);
    break;
  }
  case ISA_RAW_SENDS: {
    uint8_t modifier = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    bool hasEOT = modifier & 0x2;

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    uint8_t numSrc0 = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t numSrc1 = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t numDst = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t ffid = 0;
    if (getVersionAsInt(container.majorVersion, container.minorVersion) >
        getVersionAsInt(3, 5)) {
      ffid = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    }
    VISA_VectorOpnd *exMsgDesc =
        readVectorOperandNG(bytePos, buf, container, false);

    VISA_VectorOpnd *desc = readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    kernelBuilder->AppendVISAMiscRawSends(pred, emask, esize, modifier, ffid,
                                          exMsgDesc, numSrc0, numSrc1, numDst,
                                          desc, src0, src1, dst, hasEOT);
    break;
  }
  case ISA_VME_FBR: {
    VISA_RawOpnd *UNIInput = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *FBRInput = readRawOperandNG(bytePos, buf, container);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *FBRMbMode =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *FBRSubMbShape =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *FBRSubPredMode =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *output = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilder->AppendVISAMiscVME_FBR(surfaceHnd, UNIInput, FBRInput,
                                         FBRMbMode, FBRSubMbShape,
                                         FBRSubPredMode, output);
    break;
  }
  case ISA_VME_IME: {
    uint8_t streamMode = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t searchCtrl = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *UNIInput = readRawOperandNG(bytePos, buf, container);

    VISA_RawOpnd *IMEInput = readRawOperandNG(bytePos, buf, container);

    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *ref0 = readRawOperandNG(bytePos, buf, container); //, 2);
    VISA_RawOpnd *ref1 = readRawOperandNG(bytePos, buf, container); //, 2);
    VISA_RawOpnd *costCenter =
        readRawOperandNG(bytePos, buf, container); //, 4);
    VISA_RawOpnd *output = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilder->AppendVISAMiscVME_IME(surfaceHnd, streamMode, searchCtrl,
                                         UNIInput, IMEInput, ref0, ref1,
                                         costCenter, output);
    break;
  }
  case ISA_VME_SIC: {
    VISA_RawOpnd *UNIInput = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *SICInput =
        readRawOperandNG(bytePos, buf, container); //, 128); //SICInput
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *output = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    kernelBuilder->AppendVISAMiscVME_SIC(surfaceHnd, UNIInput, SICInput,
                                         output);
    break;
  }
  case ISA_VME_IDM: {
    VISA_RawOpnd *UNIInput =
        readRawOperandNG(bytePos, buf, container); //, 4*32);
    VISA_RawOpnd *IDMInput =
        readRawOperandNG(bytePos, buf, container); //, 32); //DMInput
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *output = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    kernelBuilder->AppendVISAMiscVME_IDM(surfaceHnd, UNIInput, IDMInput,
                                         output);
    break;
  }
  case ISA_3D_URB_WRITE: {
    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    uint8_t numOut = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_RawOpnd *channelMask = readRawOperandNG(bytePos, buf, container);
    uint16_t globalOffset = readPrimitiveOperandNG<uint16_t>(bytePos, buf);

    VISA_RawOpnd *urbHandle = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *perSlotOffset = readRawOperandNG(bytePos, buf, container);

    VISA_RawOpnd *vertexData = readRawOperandNG(bytePos, buf, container);

    kernelBuilder->AppendVISA3dURBWrite(pred, emask, esize, numOut, channelMask,
                                        globalOffset, urbHandle, perSlotOffset,
                                        vertexData);
    break;
  }
  case ISA_DPAS:
  case ISA_DPASW: {
    GenPrecision A = GenPrecision::INVALID, W = GenPrecision::INVALID;
    uint8_t D = 0;
    uint8_t C = 0;

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);

    uint8_t tag = 0;
    VISA_VectorOpnd *src2 = readVectorOperandNG(
        bytePos, buf, tag, container, Get_VISA_Exec_Size(esize), false);

    VISA_INST_Desc *inst_desc = &CISA_INST_table[opcode];

    // sanity check
    vISA_ASSERT_INPUT(inst_desc->opnd_desc[5].opnd_type == OPND_OTHER,
           "opnd_desc's type at index = 5 should be OPND_OTHER!");

    VISA_Type visatype = (VISA_Type)inst_desc->opnd_desc[5].data_type;
    uint32_t dpasOtherOpnd = readOtherOperandNG(bytePos, buf, visatype);
    UI32ToDpasInfo(dpasOtherOpnd, A, W, D, C);

    kernelBuilder->AppendVISADpasInst(opcode, emask, esize, dst, src0, src1,
                                      src2, A, W, D, C);
    break;
  }
  case ISA_LIFETIME: {
    VISA_VectorOpnd *opnd = NULL;
    Common_ISA_Operand_Class opndClass;
    VISAVarLifetime lifetime;

    unsigned char properties = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint32_t versionInt =
        getVersionAsInt(container.majorVersion, container.minorVersion);
    uint32_t varId = versionInt >= getVersionAsInt(3, 4)
                         ? readPrimitiveOperandNG<uint32_t>(bytePos, buf)
                         : readPrimitiveOperandNG<uint16_t>(bytePos, buf);

    opndClass = (Common_ISA_Operand_Class)(properties >> 4);
    lifetime = (VISAVarLifetime)(properties & 0x1);

    if (opndClass == OPERAND_GENERAL) {
      VISA_GenVar *decl;

      decl = container.generalVarDecls[varId];

      if (lifetime == LIFETIME_START) {
        kernelBuilder->CreateVISADstOperand(opnd, decl, 1, 0, 0);
      } else {
        kernelBuilder->CreateVISASrcOperand(opnd, decl, MODIFIER_NONE, 0, 1, 0,
                                            0, 0);
      }
    } else if (opndClass == OPERAND_ADDRESS) {
      VISA_AddrVar *decl;
      decl = container.addressVarDecls[varId];

      if (lifetime == LIFETIME_START) {
        kernelBuilder->CreateVISAAddressDstOperand(opnd, decl, 0);
      } else {
        kernelBuilder->CreateVISAAddressSrcOperand(opnd, decl, 0, 1);
      }
    } else if (opndClass == OPERAND_PREDICATE) {
      VISA_PredVar *decl;

      decl = container.predicateVarDecls[varId];
      VISA_PredOpnd *predOpnd;
      kernelBuilder->CreateVISAPredicateOperand(
          predOpnd, decl, PredState_NO_INVERSE, PRED_CTRL_NON);
      opnd = (VISA_VectorOpnd *)predOpnd;
    } else {
      vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal Operand Class");
    }

    kernelBuilder->AppendVISALifetime(lifetime, opnd);
    break;
  }
  default: {
    vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal Misc Opcode.");
  }
  }
}

static void readInstructionSVM(unsigned &bytePos, const char *buf,
                               ISA_Opcode opcode, RoutineContainer &container) {
  VISA_EMask_Ctrl emask = vISA_EMASK_M1;
  VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;

  VISAKernel *kernelBuilder = container.kernelBuilder;

  SVMSubOpcode subOpcode =
      (SVMSubOpcode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  switch (subOpcode) {
  case SVM_BLOCK_ST:
  case SVM_BLOCK_LD: {
    uint8_t numOWords = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    bool unaligned = (numOWords & 8) != 0;
    numOWords &= 7;
    VISA_VectorOpnd *address =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    if (subOpcode == SVM_BLOCK_LD) {
      kernelBuilder->AppendVISASvmBlockLoadInst(VISA_Oword_Num(numOWords),
                                                unaligned, address, dst);
    } else {
      kernelBuilder->AppendVISASvmBlockStoreInst(VISA_Oword_Num(numOWords),
                                                 unaligned, address, dst);
    }
    break;
  }
  case SVM_GATHER:
  case SVM_SCATTER: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    VISA_SVM_Block_Type blockSize = VISA_SVM_Block_Type(
        readPrimitiveOperandNG<uint8_t>(bytePos, buf) & 0x3);
    VISA_SVM_Block_Num numBlocks =
        VISA_SVM_Block_Num(readPrimitiveOperandNG<uint8_t>(bytePos, buf) & 0x3);
    VISA_RawOpnd *addresses = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    if (subOpcode == SVM_GATHER) {
      kernelBuilder->AppendVISASvmGatherInst(pred, emask, esize, blockSize,
                                             numBlocks, addresses, dst);
    } else {
      kernelBuilder->AppendVISASvmScatterInst(pred, emask, esize, blockSize,
                                              numBlocks, addresses, dst);
    }
    break;
  }
  case SVM_ATOMIC: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    VISAAtomicOps op;
    unsigned short bitwidth;
    std::tie(op, bitwidth) = getAtomicOpAndBitwidth(bytePos, buf);

    VISA_RawOpnd *addresses = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    kernelBuilder->AppendVISASvmAtomicInst(pred, emask, esize, op, bitwidth,
                                           addresses, src0, src1, dst);
    break;
  }
  case SVM_GATHER4SCALED: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    unsigned channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    // scale is ignored and MBZ
    (void)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    VISA_VectorOpnd *address =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    kernelBuilder->AppendVISASvmGather4ScaledInst(
        pred, emask, esize,
        ChannelMask::createAPIFromBinary(ISA_SVM, channelMask), address,
        offsets, dst);
    break;
  }
  case SVM_SCATTER4SCALED: {
    readExecSizeNG(bytePos, buf, esize, emask, container);
    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    unsigned channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    // scale is ignored and MBZ
    (void)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    VISA_VectorOpnd *address =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *offsets = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    kernelBuilder->AppendVISASvmScatter4ScaledInst(
        pred, emask, esize,
        ChannelMask::createAPIFromBinary(ISA_SVM, channelMask), address,
        offsets, dst);
    break;
  }
  default:
    vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal SVM Sub Opcode.");
  }
}

template <class T>
static VISA3DSamplerOp readSubOpcodeByteNG(unsigned &bytePos, const char *buf) {
  T val = 0;
  READ_CISA_FIELD(val, T, bytePos, buf);
  return VISA3DSamplerOp::extractSamplerOp<T>(val);
}

static VISA_VectorOpnd *readAoffimmi(uint32_t &bytePos, const char *buf,
                                     RoutineContainer &container) {
  VISAKernel *kernelBuilder = container.kernelBuilder;
  uint32_t versionInt =
      getVersionAsInt(container.majorVersion, container.minorVersion);
  bool is3Dot4Plus = versionInt >= getVersionAsInt(3, 4);
  VISA_VectorOpnd *aoffimmi = nullptr;
  if (is3Dot4Plus) {
    aoffimmi = readVectorOperandNG(bytePos, buf, container, false);
  } else {
    uint16_t aoffimmiVal = readPrimitiveOperandNG<uint16_t>(bytePos, buf);
    kernelBuilder->CreateVISAImmediate(aoffimmi, &aoffimmiVal, ISA_TYPE_UW);
  }
  return aoffimmi;
}

static void readInstructionSampler(unsigned &bytePos, const char *buf,
                                   ISA_Opcode opcode,
                                   RoutineContainer &container) {
  VISAKernel *kernelBuilder = container.kernelBuilder;
  VISAKernelImpl *kernelBuilderImpl = ((VISAKernelImpl *)kernelBuilder);

  switch (opcode) {
  case ISA_AVS: {
    uint8_t chanelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISAChannelMask channel =
        ChannelMask::createAPIFromBinary(ISA_AVS, chanelMask);
    uint8_t sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_VectorOpnd *uOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *vOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *deltaU =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *deltaV =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *u2d = readVectorOperandNG(bytePos, buf, container, false);

    VISA_VectorOpnd *groupID =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *verticalBlockNumber =
        readVectorOperandNG(bytePos, buf, container, false);
    uint8_t cntrl = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *v2d = readVectorOperandNG(bytePos, buf, container, false);
    uint8_t execMode = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *IEFBypass =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilder->CreateVISAStateOperandHandle(
        samplerHnd, container.samplerVarDecls[sampler]);

    kernelBuilder->AppendVISAMEAVS(
        surfaceHnd, samplerHnd, channel, uOffset, vOffset, deltaU, deltaV, u2d,
        v2d, groupID, verticalBlockNumber, (OutputFormatControl)cntrl,
        (AVSExecMode)execMode, IEFBypass, dst);
    break;
  }
  case ISA_LOAD:
  case ISA_SAMPLE: {
    uint8_t mode = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t sampler = (ISA_SAMPLE == opcode)
                          ? readPrimitiveOperandNG<uint8_t>(bytePos, buf)
                          : 0;
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *uOffset = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *vOffset = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *roffset = readRawOperandNG(bytePos, buf, container);
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    uint8_t channel = mode & 0xF;
    bool isSimd16 = 0x0 != ((mode >> 4) & 0x3);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    if (opcode == ISA_SAMPLE) {
      kernelBuilderImpl->CreateVISAStateOperandHandle(
          samplerHnd, container.samplerVarDecls[sampler]);
      kernelBuilderImpl->AppendVISASISample(
          vISA_EMASK_M1, surfaceHnd, samplerHnd,
          ChannelMask::createAPIFromBinary(opcode, channel), isSimd16, uOffset,
          vOffset, roffset, dst);
    } else
      kernelBuilderImpl->AppendVISASILoad(
          surfaceHnd, ChannelMask::createAPIFromBinary(opcode, channel),
          isSimd16, uOffset, vOffset, roffset, dst);
    break;
  }
  case ISA_SAMPLE_UNORM: {
    uint8_t channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    CHANNEL_OUTPUT_FORMAT channelOutput =
        (CHANNEL_OUTPUT_FORMAT)ChannelMask::getChannelOutputFormat(channelMask);

    VISA_VectorOpnd *uOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *vOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *deltaU =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *deltaV =
        readVectorOperandNG(bytePos, buf, container, false);

    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        samplerHnd, container.samplerVarDecls[sampler]);
    kernelBuilderImpl->AppendVISASISampleUnorm(
        surfaceHnd, samplerHnd,
        ChannelMask::createAPIFromBinary(opcode, channelMask), uOffset, vOffset,
        deltaU, deltaV, dst, channelOutput);
    break;
  }
  case ISA_3D_SAMPLE: {
    // 0x6D <op> <pixel_null_mask> <cps_enable> <exec_size> <pred>
    // <channels> <aoffimmi> <sampler> <surface> <dst> <numParams> <params>
    // <paired_surface>
       // The vISA version that widens the opcode field is 4
       // the below check is necessary to pick the correct
       // width during decoding based on input binary version
    const int WIDE_OPCODE_ISA_VER = 4;
    auto op = (container.majorVersion >= WIDE_OPCODE_ISA_VER)
                  ? readSubOpcodeByteNG<uint16_t>(bytePos, buf)
                  : readSubOpcodeByteNG<uint8_t>(bytePos, buf);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    uint8_t channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *aoffimmi = readAoffimmi(bytePos, buf, container);
    uint8_t sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    bool isPairedSurfaceAvailable =
        getVersionAsInt(container.majorVersion, container.minorVersion) >=
        getVersionAsInt(3, 8);
    VISA_RawOpnd *pairedSurface =
        isPairedSurfaceAvailable ? readRawOperandNG(bytePos, buf, container)
                                 : nullptr;
    if (pairedSurface == nullptr) {
      kernelBuilder->CreateVISANullRawOperand(pairedSurface, false /*isDst*/);
    }

    uint8_t numParams = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    vISA_ASSERT_INPUT(numParams < 16,
                 "number of parameters for 3D_Sample should be < 16");

    VISA_RawOpnd *params[16];
    for (int i = 0; i < numParams; ++i) {
      params[i] = readRawOperandNG(bytePos, buf, container);
    }
    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        samplerHnd, container.samplerVarDecls[sampler]);
    kernelBuilderImpl->AppendVISA3dSampler(
        op.opcode, op.pixelNullMask, op.cpsEnable, !op.nonUniformSampler, pred,
        emask, esize, ChannelMask::createAPIFromBinary(opcode, channelMask),
        aoffimmi, samplerHnd, 0, surfaceHnd, 0, pairedSurface, dst, numParams,
        params);
    break;
  }
  case ISA_3D_LOAD: {
    // 0x6E <op> <pixel_null_mask> <exec_size> <pred> <channels>
    // <aoffimmi> <surface> <dst> <numParams> <params>
    // <paired_surface>
       // same as 3D_SAMPLE, except that sampler is missing.

    // The vISA version that widens the opcode field is 4
    // the below check is necessary to pick the correct
    // width during decoding based on input binary version
    const int WIDE_OPCODE_ISA_VER = 4;
    auto op = (container.majorVersion >= WIDE_OPCODE_ISA_VER)
                  ? readSubOpcodeByteNG<uint16_t>(bytePos, buf)
                  : readSubOpcodeByteNG<uint8_t>(bytePos, buf);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
    uint8_t channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *aoffimmi = readAoffimmi(bytePos, buf, container);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    bool isPairedSurfaceAvailable =
        getVersionAsInt(container.majorVersion, container.minorVersion) >=
        getVersionAsInt(3, 8);
    VISA_RawOpnd *pairedSurface =
        isPairedSurfaceAvailable ? readRawOperandNG(bytePos, buf, container)
                                 : nullptr;
    if (pairedSurface == nullptr) {
      kernelBuilder->CreateVISANullRawOperand(pairedSurface, false /*isDst*/);
    }

    uint8_t numParams = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    vISA_ASSERT_INPUT(numParams < 16,
                 "number of parameters for 3D_Load should be < 16");

    VISA_RawOpnd *params[16];
    for (int i = 0; i < numParams; ++i) {
      params[i] = readRawOperandNG(bytePos, buf, container);
    }
    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->AppendVISA3dLoad(
        op.opcode, op.pixelNullMask, pred, emask, esize,
        ChannelMask::createAPIFromBinary(opcode, channelMask), aoffimmi,
        surfaceHnd, 0,
        pairedSurface,
        dst, numParams, params);
    break;
  }
  case ISA_3D_GATHER4: {
    // The vISA version that widens the opcode field is 4
    // the below check is necessary to pick the correct
    // width during decoding based on input binary version
    const int WIDE_OPCODE_ISA_VER = 4;
    auto op = (container.majorVersion >= WIDE_OPCODE_ISA_VER)
                  ? readSubOpcodeByteNG<uint16_t>(bytePos, buf)
                  : readSubOpcodeByteNG<uint8_t>(bytePos, buf);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);

    VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

    uint8_t channel = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    VISA_VectorOpnd *aoffimmi = readAoffimmi(bytePos, buf, container);
    uint8_t sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
    bool isPairedSurfaceAvailable =
        getVersionAsInt(container.majorVersion, container.minorVersion) >=
        getVersionAsInt(3, 8);
    VISA_RawOpnd *pairedSurface =
        isPairedSurfaceAvailable ? readRawOperandNG(bytePos, buf, container)
                                 : nullptr;
    if (pairedSurface == nullptr) {
      kernelBuilder->CreateVISANullRawOperand(pairedSurface, false /*isDst*/);
    }

    uint8_t numParams = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    vISA_ASSERT_INPUT(numParams < 8,
                 "number of parameters for 3D_Gather4 should be < 8");

    VISA_RawOpnd *params[16];
    for (int i = 0; i < numParams; ++i) {
      params[i] = readRawOperandNG(bytePos, buf, container);
    }
    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilderImpl->CreateVISAStateOperandHandle(
        samplerHnd, container.samplerVarDecls[sampler]);
    kernelBuilder->AppendVISA3dGather4(op.opcode, op.pixelNullMask, pred, emask,
                                       esize, (VISASourceSingleChannel)channel,
                                       aoffimmi, samplerHnd, 0, surfaceHnd, 0,
                                       pairedSurface,
                                       dst, numParams, params);
    break;
  }
  case ISA_3D_INFO: {
    VISASampler3DSubOpCode subOpcode =
        (VISASampler3DSubOpCode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_EMask_Ctrl emask = vISA_EMASK_M1;
    VISA_Exec_Size esize = EXEC_SIZE_ILLEGAL;
    readExecSizeNG(bytePos, buf, esize, emask, container);
    uint8_t channelMask = 0xF;
    channelMask = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_RawOpnd *lod = subOpcode == VISA_3D_RESINFO
                            ? readRawOperandNG(bytePos, buf, container)
                            : NULL;
    VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);
    kernelBuilder->AppendVISA3dInfo(
        subOpcode, emask, esize,
        ChannelMask::createAPIFromBinary(opcode, channelMask), surfaceHnd, 0, lod,
        dst);

    break;
  }
  case ISA_VA: {
    /// subOpcode
    ISA_VA_Sub_Opcode subOpcode =
        (ISA_VA_Sub_Opcode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    VISA_StateOpndHandle *surfaceHnd = NULL;
    VISA_StateOpndHandle *samplerHnd = NULL;
    uint8_t sampler = 0;

    switch (subOpcode) {
    case MINMAXFILTER_FOPCODE:
    case Convolve_FOPCODE:
    case Dilate_FOPCODE:
    case ERODE_FOPCODE: {
      sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
      kernelBuilderImpl->CreateVISAStateOperandHandle(
          samplerHnd, container.samplerVarDecls[sampler]);
      break;
    }
    default:
      break; // Prevent gcc warning
    }

    uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    kernelBuilder->CreateVISAStateOperandHandle(
        surfaceHnd, container.surfaceVarDecls[surface]);

    VISA_VectorOpnd *uOffset =
        readVectorOperandNG(bytePos, buf, container, false);
    VISA_VectorOpnd *vOffset =
        readVectorOperandNG(bytePos, buf, container, false);

    switch (subOpcode) {
    case MINMAX_FOPCODE: {
      /// mmf mode
      VISA_VectorOpnd *mmfMode =
          readVectorOperandNG(bytePos, buf, container, false);

      /// dst
      VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
      kernelBuilder->AppendVISAVAMinMax(surfaceHnd, uOffset, vOffset, mmfMode,
                                        dst);
      break;
    }
    case MINMAXFILTER_FOPCODE: {

      /// cntrl
      OutputFormatControl cntrl =
          (OutputFormatControl)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      /// execMode
      MMFExecMode execMode =
          (MMFExecMode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      /// mmf mode
      VISA_VectorOpnd *mmfMode =
          readVectorOperandNG(bytePos, buf, container, false);

      /// dst
      VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
      kernelBuilder->AppendVISAVAMinMaxFilter(samplerHnd, surfaceHnd, uOffset,
                                              vOffset, cntrl, execMode, mmfMode,
                                              dst);
      break;
    }
    case BoolCentroid_FOPCODE:
    case Centroid_FOPCODE: {
      /// v size
      VISA_VectorOpnd *vSize =
          readVectorOperandNG(bytePos, buf, container, false);

      if (subOpcode == BoolCentroid_FOPCODE) {
        /// h size
        VISA_VectorOpnd *hSize =
            readVectorOperandNG(bytePos, buf, container, false);
        VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
        kernelBuilder->AppendVISAVABooleanCentroid(surfaceHnd, uOffset, vOffset,
                                                   vSize, hSize, dst);
      } else {
        VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
        kernelBuilder->AppendVISAVACentroid(surfaceHnd, uOffset, vOffset, vSize,
                                            dst);
      }

      break;
    }
    case Convolve_FOPCODE: {
      /// size for convolve, execMode for erode/dilate
      uint8_t properties = readPrimitiveOperandNG<uint8_t>(bytePos, buf);

      bool isBigKernel = (bool)((properties >> 4) & 0x1);
      CONVExecMode execMode = (CONVExecMode)(properties & 0x3);

      /// dst
      VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
      kernelBuilder->AppendVISAVAConvolve(samplerHnd, surfaceHnd, uOffset,
                                          vOffset, execMode, isBigKernel, dst);
      break;
    }
    case Dilate_FOPCODE:
    case ERODE_FOPCODE: {
      /// size for convolve, execMode for erode/dilate
      EDExecMode execMode =
          (EDExecMode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
      EDMode mode = (subOpcode == Dilate_FOPCODE) ? VA_DILATE : VA_ERODE;
      /// dst
      VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
      kernelBuilder->AppendVISAVAErodeDilate(mode, samplerHnd, surfaceHnd,
                                             uOffset, vOffset, execMode, dst);
      break;
    }
    default:
      break; // Prevent gcc warning
    }

    break;
  }
  case ISA_VA_SKL_PLUS: {
    VISA_INST_Desc *instDesc = &CISA_INST_table[opcode];

    /// subOpcode
    ISA_VA_Sub_Opcode subOpcode =
        (ISA_VA_Sub_Opcode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

    if ((subOpcode < VA_OP_CODE_1D_CONVOLVE_VERTICAL) ||
        (subOpcode >= VA_OP_CODE_UNDEFINED)) {
      vISA_ASSERT_INPUT(false, "Invalid VA sub-opcode");
      return;
    }

#define MAX_NUM_VOPNDS 10
    VISA_VectorOpnd *vOpnds[MAX_NUM_VOPNDS];
    uint8_t numVSrcs = 0;
    memset(vOpnds, 0, sizeof(VISA_VectorOpnd *) * MAX_NUM_VOPNDS);

#define MAX_NUM_MOPNDS 10
    unsigned int miscOpnds[MAX_NUM_MOPNDS];
    uint8_t numMiscOpnds = 0;
    memset(miscOpnds, 0, sizeof(uint32_t) * MAX_NUM_MOPNDS);

    VISA_RawOpnd *dst = NULL;

#define MAX_NUM_RSRCS 5
    VISA_RawOpnd *rawSrcs[MAX_NUM_RSRCS];
    uint8_t numRawSrcs = 0;
    memset(rawSrcs, 0, sizeof(VISA_RawOpnd *) * MAX_NUM_RSRCS);

#define MAX_NUM_SOPNDS 4
    VISA_StateOpndHandle *stateOpnds[MAX_NUM_SOPNDS];
    uint8_t numStateOpnds = 0;
    memset(stateOpnds, 0, sizeof(VISA_StateOpndHandle *) * MAX_NUM_SOPNDS);

    int numTotalOperands = instDesc->getSubInstDesc(subOpcode).opnd_num;

    for (int i = 0; i < numTotalOperands; i++) {
      const OpndDesc *opndDesc =
          &(instDesc->getSubInstDesc(subOpcode).opnd_desc[i]);

      if (opndDesc->opnd_type == OPND_SAMPLE) {
        uint8_t sampler = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
        kernelBuilderImpl->CreateVISAStateOperandHandle(
            stateOpnds[numStateOpnds++], container.samplerVarDecls[sampler]);
      } else if (opndDesc->opnd_type == OPND_SURFACE) {
        uint8_t surface = readPrimitiveOperandNG<uint8_t>(bytePos, buf);
        kernelBuilderImpl->CreateVISAStateOperandHandle(
            stateOpnds[numStateOpnds++], container.surfaceVarDecls[surface]);
      } else if ((opndDesc->opnd_type & OPND_SRC_GEN) == OPND_SRC_GEN) {
        vOpnds[numVSrcs++] =
            readVectorOperandNG(bytePos, buf, container, false);
      } else if (opndDesc->opnd_type == OPND_RAW_SRC) {
        rawSrcs[numRawSrcs++] = readRawOperandNG(bytePos, buf, container);
      } else if (opndDesc->opnd_type == OPND_RAW_DST) {
        dst = readRawOperandNG(bytePos, buf, container);
      } else if (opndDesc->opnd_type == OPND_OTHER) {
        // in theory this is not necessary since all of them will be UB
        // but to demonstrate usage model
        if (opndDesc->data_type == ISA_TYPE_UB) {
          miscOpnds[numMiscOpnds++] =
              readPrimitiveOperandNG<uint8_t>(bytePos, buf);
        } else if (opndDesc->data_type == ISA_TYPE_UW) {
          miscOpnds[numMiscOpnds++] =
              readPrimitiveOperandNG<uint16_t>(bytePos, buf);
        } else if (opndDesc->data_type == ISA_TYPE_UD) {
          miscOpnds[numMiscOpnds++] =
              readPrimitiveOperandNG<unsigned int>(bytePos, buf);
        } else {
          vISA_ASSERT_INPUT(false, "Invalid misc opnd data type");
          return;
        }
      } else {
        vISA_ASSERT_INPUT(false, "Invalid opnd type");
        return;
      }
    }
    numVSrcs = 0;
    numRawSrcs = 0;
    numMiscOpnds = 0;
    numStateOpnds = 0;

    if (subOpcode >= 8 && subOpcode <= 15 && subOpcode != 14)
      vASSERT(dst);

    switch (subOpcode) {
    case VA_OP_CODE_FLOOD_FILL:
      kernelBuilderImpl->AppendVISAVAFloodFill(
          miscOpnds[0] != 0, rawSrcs[0], vOpnds[0], vOpnds[1], vOpnds[2], dst);
      break;
    case VA_OP_CODE_1D_CONVOLVE_VERTICAL:
      kernelBuilderImpl->AppendVISAVAConvolve1D(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (CONVExecMode)miscOpnds[0], VA_V_DIRECTION, dst);
      break;
    case VA_OP_CODE_1D_CONVOLVE_HORIZONTAL:
      kernelBuilderImpl->AppendVISAVAConvolve1D(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (CONVExecMode)miscOpnds[0], VA_H_DIRECTION, dst);
      break;
    case VA_OP_CODE_1PIXEL_CONVOLVE:
      kernelBuilderImpl->AppendVISAVAConvolve1Pixel(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (CONV1PixelExecMode)miscOpnds[0], rawSrcs[0], dst);
      break;
    case VA_OP_CODE_LBP_CORRELATION:
      kernelBuilderImpl->AppendVISAVALBPCorrelation(stateOpnds[0], vOpnds[0],
                                                    vOpnds[1], vOpnds[2], dst);
      break;
    case VA_OP_CODE_LBP_CREATION:
      kernelBuilderImpl->AppendVISAVALBPCreation(
          stateOpnds[0], vOpnds[0], vOpnds[1], (LBPCreationMode)miscOpnds[0],
          dst);
      break;
    case VA_OP_CODE_CORRELATION_SEARCH:
      kernelBuilderImpl->AppendVISAVACorrelationSearch(
          stateOpnds[0], vOpnds[0], vOpnds[1], vOpnds[2], vOpnds[3], vOpnds[4],
          vOpnds[5], vOpnds[6], vOpnds[7], dst);
      break;
    case ISA_HDC_ERODE:
      kernelBuilderImpl->AppendVISAVAHDCErodeDilate(
          VA_ERODE, stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          stateOpnds[2], vOpnds[2], vOpnds[3]);
      break;
    case ISA_HDC_DILATE:
      kernelBuilderImpl->AppendVISAVAHDCErodeDilate(
          VA_DILATE, stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          stateOpnds[2], vOpnds[2], vOpnds[3]);
      break;
    case ISA_HDC_LBPCORRELATION:
      kernelBuilderImpl->AppendVISAVAHDCLBPCorrelation(
          stateOpnds[0], vOpnds[0], vOpnds[1], vOpnds[2], stateOpnds[1],
          vOpnds[3], vOpnds[4]);
      break;
    case ISA_HDC_LBPCREATION:
      kernelBuilderImpl->AppendVISAVAHDCLBPCreation(
          stateOpnds[0], vOpnds[0], vOpnds[1], (LBPCreationMode)miscOpnds[0],
          stateOpnds[1], vOpnds[2], vOpnds[3]);
      break;
    case ISA_HDC_MMF:
      kernelBuilderImpl->AppendVISAVAHDCMinMaxFilter(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (HDCReturnFormat)miscOpnds[0], (MMFEnableMode)miscOpnds[1],
          stateOpnds[2], vOpnds[2], vOpnds[3]);
      break;
    case ISA_HDC_1PIXELCONV:
      kernelBuilderImpl->AppendVISAVAHDCConvolve1Pixel(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (HDCReturnFormat)miscOpnds[0], rawSrcs[0], stateOpnds[2], vOpnds[2],
          vOpnds[3]);
      break;
    case ISA_HDC_CONV:
      kernelBuilderImpl->AppendVISAVAHDCConvolve(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (HDCReturnFormat)(miscOpnds[0] & 0xF),
          (CONVHDCRegionSize)(miscOpnds[0] >> 4), stateOpnds[2], vOpnds[2],
          vOpnds[3]);
      break;
    case ISA_HDC_1DCONV_H:
      kernelBuilderImpl->AppendVISAVAHDCConvolve1D(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (HDCReturnFormat)miscOpnds[0], VA_H_DIRECTION, stateOpnds[2],
          vOpnds[2], vOpnds[3]);
      break;
    case ISA_HDC_1DCONV_V:
      kernelBuilderImpl->AppendVISAVAHDCConvolve1D(
          stateOpnds[0], stateOpnds[1], vOpnds[0], vOpnds[1],
          (HDCReturnFormat)miscOpnds[0], VA_V_DIRECTION, stateOpnds[2],
          vOpnds[2], vOpnds[3]);
      break;
    default:
      vISA_ASSERT_UNREACHABLE("Invalid VA sub-opcode");
    }

    break;
  }
  default: {
    vISA_ASSERT_UNREACHABLE("Unimplemented or Illegal Sampler Opcode.");
  }
  }
}

static LSC_CACHE_OPTS readLscCacheOpts(unsigned &bytePos, const char *buf) {
  LSC_CACHE_OPTS caching{};
  caching.l1 = (LSC_CACHE_OPT)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  caching.l3 = (LSC_CACHE_OPT)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  return caching;
}

static LSC_ADDR readLscAddr(unsigned &bytePos, const char *buf) {
  LSC_ADDR addrInfo{};
  addrInfo.type = (LSC_ADDR_TYPE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  addrInfo.immScale = readPrimitiveOperandNG<uint16_t>(bytePos, buf);
  addrInfo.immOffset = readPrimitiveOperandNG<int32_t>(bytePos, buf);
  addrInfo.size = (LSC_ADDR_SIZE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  return addrInfo;
}

static LSC_DATA_SHAPE readLscDataShape(unsigned &bytePos, const char *buf,
                                       bool useChMask) {
  LSC_DATA_SHAPE dataInfo{};
  dataInfo.size = (LSC_DATA_SIZE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  dataInfo.order =
      (LSC_DATA_ORDER)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  // even though both chmask and elems are a sum type, the binary holds both
  auto dataElems =
      (LSC_DATA_ELEMS)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  int chMask = (int)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  if (useChMask) {
    dataInfo.chmask = chMask;
  } else {
    dataInfo.elems = dataElems;
  }
  return dataInfo;
}

static void readInstructionLscUntyped(LSC_OP subOpcode, unsigned &bytePos,
                                      const char *buf,
                                      RoutineContainer &container) {
  const LscOpInfo opInfo = LscOpInfoGet(subOpcode);

  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_ILLEGAL;

  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

  auto lscSfid = (LSC_SFID)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  LSC_CACHE_OPTS caching = readLscCacheOpts(bytePos, buf);
  LSC_ADDR addrInfo = readLscAddr(bytePos, buf);
  LSC_DATA_SHAPE dataInfo = readLscDataShape(
      bytePos, buf, subOpcode == LSC_LOAD_QUAD || subOpcode == LSC_STORE_QUAD);

  VISA_VectorOpnd *surface =
      readVectorOperandNG(bytePos, buf, container, false);
  auto ssIdx = readPrimitiveOperandNG<uint32_t>(bytePos, buf);
  VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *src0 = readRawOperandNG(bytePos, buf, container);
  VISA_VectorOpnd *src0Pitch = nullptr;
  if (opInfo.isStrided()) {
    src0Pitch = readVectorOperandNG(bytePos, buf, container, false);
  }
  VISA_RawOpnd *src1 = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *src2 = nullptr;
  if (!opInfo.isStrided()) {
    src2 = readRawOperandNG(bytePos, buf, container);
  }

  if (opInfo.isStrided()) {
    container.kernelBuilder->AppendVISALscUntypedStridedInst(
        subOpcode, lscSfid, pred, execSize, execMask, caching, addrInfo,
        dataInfo, surface, ssIdx, dst, src0, src0Pitch, src1);
  } else {
    container.kernelBuilder->AppendVISALscUntypedInst(
        subOpcode, lscSfid, pred, execSize, execMask, caching, false, addrInfo,
        dataInfo, surface, ssIdx, dst, src0, src1, src2);
  }
}

static void readInstructionLscUntypedBlock2D(LSC_OP subOpcode,
                                             unsigned &bytePos, const char *buf,
                                             RoutineContainer &container) {
  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_ILLEGAL;

  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
  //
  auto sfid = (LSC_SFID)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  //
  LSC_CACHE_OPTS caching = readLscCacheOpts(bytePos, buf);
  //
  LSC_DATA_SHAPE_BLOCK2D dataShape2D{};
  dataShape2D.size =
      (LSC_DATA_SIZE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  dataShape2D.order =
      (LSC_DATA_ORDER)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  dataShape2D.blocks = (int)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  dataShape2D.width = (int)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
  dataShape2D.height = (int)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
  dataShape2D.vnni = readPrimitiveOperandNG<uint8_t>(bytePos, buf) != 0;
  //
  VISA_RawOpnd *dstData = readRawOperandNG(bytePos, buf, container);
  VISA_VectorOpnd *src0Addrs[LSC_BLOCK2D_ADDR_PARAMS]{};
  for (size_t i = 0; i < LSC_BLOCK2D_ADDR_PARAMS; i++) {
    src0Addrs[i] = readVectorOperandNG(bytePos, buf, container, false);
  }
  VISA_RawOpnd *src1Data = readRawOperandNG(bytePos, buf, container);
  //
  container.kernelBuilder->AppendVISALscUntypedBlock2DInst(
      subOpcode, sfid, pred, execSize, execMask, caching, dataShape2D, dstData,
      src0Addrs, 0, 0, src1Data);
}

static void readInstructionLscTyped(LSC_OP subOpcode, unsigned &bytePos,
                                    const char *buf,
                                    RoutineContainer &container) {
  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_ILLEGAL;

  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

  // LSC_SFID lscSfid = ... always TGM
  LSC_CACHE_OPTS caching = readLscCacheOpts(bytePos, buf);
  auto addrModel = (LSC_ADDR_TYPE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  auto addrSize = (LSC_ADDR_SIZE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  //
  auto dataSize = (LSC_DATA_SIZE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  auto dataOrder =
      (LSC_DATA_ORDER)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  auto dataElems =
      (LSC_DATA_ELEMS)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  auto chMask = (int)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  LSC_DATA_SHAPE dataInfo{};
  dataInfo.size = dataSize;
  dataInfo.order = dataOrder;
  if (subOpcode == LSC_LOAD_QUAD || subOpcode == LSC_STORE_QUAD) {
    dataInfo.chmask = chMask;
  } else {
    dataInfo.elems = dataElems;
  }

  VISA_VectorOpnd *surface =
      readVectorOperandNG(bytePos, buf, container, false);
  auto ssIdx = readPrimitiveOperandNG<uint32_t>(bytePos, buf);
  VISA_RawOpnd *dstData = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *src0AddrsU = readRawOperandNG(bytePos, buf, container);
  auto uOff = readPrimitiveOperandNG<int32_t>(bytePos, buf);
  VISA_RawOpnd *src0AddrsV = readRawOperandNG(bytePos, buf, container);
  auto vOff = readPrimitiveOperandNG<int32_t>(bytePos, buf);
  VISA_RawOpnd *src0AddrsR = readRawOperandNG(bytePos, buf, container);
  auto rOff = readPrimitiveOperandNG<int32_t>(bytePos, buf);
  VISA_RawOpnd *src0AddrsLOD = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *src1Data = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *src2Data = readRawOperandNG(bytePos, buf, container);

  container.kernelBuilder->AppendVISALscTypedInst(
      subOpcode, pred, execSize, execMask, caching, addrModel, addrSize,
      dataInfo,
      surface, ssIdx,
      dstData,
      src0AddrsU, uOff,
      src0AddrsV, vOff,
      src0AddrsR, rOff,
      src0AddrsLOD, src1Data, src2Data);
}
static void readInstructionLscFence(unsigned &bytePos, const char *buf,
                                    RoutineContainer &container) {
  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_1;

  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  (void)readPredicateOperandNG(bytePos, buf, container);
  LSC_SFID sfid = (LSC_SFID)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  LSC_FENCE_OP fenceOp =
      (LSC_FENCE_OP)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  LSC_SCOPE scope = (LSC_SCOPE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

  container.kernelBuilder->AppendVISALscFence(sfid, fenceOp, scope);
}

static void readInstructionLscTypedBlock2D(LSC_OP subOpcode, unsigned &bytePos,
                                           const char *buf,
                                           RoutineContainer &container) {
  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_ILLEGAL;
  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);
  (void)pred;

  LSC_CACHE_OPTS caching = readLscCacheOpts(bytePos, buf);

  auto addrModel = (LSC_ADDR_TYPE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);

  LSC_DATA_SHAPE_TYPED_BLOCK2D dataShape2D{};
  dataShape2D.width = (int)readPrimitiveOperandNG<uint16_t>(bytePos, buf);
  dataShape2D.height = (int)readPrimitiveOperandNG<uint16_t>(bytePos, buf);

  VISA_VectorOpnd *surface =
      readVectorOperandNG(bytePos, buf, container, false);
  unsigned surfaceIndex = readPrimitiveOperandNG<uint32_t>(bytePos, buf);

  VISA_RawOpnd *dstData = readRawOperandNG(bytePos, buf, container);

  VISA_VectorOpnd *xOffset =
      readVectorOperandNG(bytePos, buf, container, false);
  VISA_VectorOpnd *yOffset =
      readVectorOperandNG(bytePos, buf, container, false);

  VISA_RawOpnd *src1Data = readRawOperandNG(bytePos, buf, container);

  container.kernelBuilder->AppendVISALscTypedBlock2DInst(
      subOpcode, caching, addrModel, dataShape2D,
      surface, surfaceIndex,
      dstData,
      xOffset, yOffset, 0, 0, src1Data);
}

static void
readInstructionLscUntypedAppendCounterAtomic(LSC_OP subOpcode,
                                             unsigned &bytePos, const char *buf,
                                             RoutineContainer &container) {

  VISA_EMask_Ctrl execMask = vISA_EMASK_M1;
  VISA_Exec_Size execSize = EXEC_SIZE_ILLEGAL;

  readExecSizeNG(bytePos, buf, execSize, execMask, container);
  VISA_PredOpnd *pred = readPredicateOperandNG(bytePos, buf, container);

  auto lscSfid = (LSC_SFID)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  (void)lscSfid;

  LSC_CACHE_OPTS caching = readLscCacheOpts(bytePos, buf);
  auto addrModel = (LSC_ADDR_TYPE)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  LSC_DATA_SHAPE dataInfo = readLscDataShape(bytePos, buf, false);

  VISA_VectorOpnd *surface =
      readVectorOperandNG(bytePos, buf, container, false);
  auto surfaceIndex = readPrimitiveOperandNG<uint32_t>(bytePos, buf);
  VISA_RawOpnd *dst = readRawOperandNG(bytePos, buf, container);
  VISA_RawOpnd *srcData = readRawOperandNG(bytePos, buf, container);

  container.kernelBuilder->AppendVISALscUntypedAppendCounterAtomicInst(
      subOpcode, pred, execSize, execMask, caching, addrModel, dataInfo,
      surface, surfaceIndex, dst, srcData);
}

static void readInstructionLSC(unsigned &bytePos, const char *buf,
                               ISA_Opcode opcode, RoutineContainer &container) {
  if (opcode == ISA_Opcode::ISA_LSC_FENCE) {
    readInstructionLscFence(bytePos, buf, container);
  } else if (opcode == ISA_Opcode::ISA_LSC_UNTYPED) {
    auto subOpcode = (LSC_OP)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    if (subOpcode == LSC_LOAD_BLOCK2D || subOpcode == LSC_STORE_BLOCK2D) {
      readInstructionLscUntypedBlock2D(subOpcode, bytePos, buf, container);
    } else if (subOpcode == LSC_APNDCTR_ATOMIC_ADD ||
               subOpcode == LSC_APNDCTR_ATOMIC_SUB) {
      readInstructionLscUntypedAppendCounterAtomic(subOpcode, bytePos, buf,
                                                   container);
    } else {
      readInstructionLscUntyped(subOpcode, bytePos, buf, container);
    }
  } else if (opcode == ISA_Opcode::ISA_LSC_TYPED) {
    auto subOpcode = (LSC_OP)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
    if (subOpcode == LSC_LOAD_BLOCK2D || subOpcode == LSC_STORE_BLOCK2D) {
      readInstructionLscTypedBlock2D(subOpcode, bytePos, buf, container);
    } else {
      readInstructionLscTyped(subOpcode, bytePos, buf, container);
    }
  } else {
    vISA_ASSERT_INPUT(false, "invalid LSC op");
  }
}

void readInstructionNG(unsigned &bytePos, const char *buf,
                       RoutineContainer &container, unsigned instID) {
  ISA_Opcode opcode = (ISA_Opcode)readPrimitiveOperandNG<uint8_t>(bytePos, buf);
  vISA_ASSERT_INPUT(opcode < ISA_NUM_OPCODE,
               "Illegal or unimplemented CISA opcode.");

  // cout << "Opcode: " << ISA_Inst_Table[opcode].str << endl;

  switch (ISA_Inst_Table[opcode].type) {
  case ISA_Inst_Mov:
  case ISA_Inst_Sync:
  case ISA_Inst_Arith:
  case ISA_Inst_Logic:
  case ISA_Inst_Address:
  case ISA_Inst_Compare:
  case ISA_Inst_SIMD_Flow:
    readInstructionCommonNG(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_Data_Port:
    readInstructionDataportNG(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_Flow:
    readInstructionControlFlow(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_Misc:
    readInstructionMisc(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_SVM:
    readInstructionSVM(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_Sampler:
    readInstructionSampler(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  case ISA_Inst_LSC:
    readInstructionLSC(bytePos, buf, (ISA_Opcode)opcode, container);
    break;
  default: {
    std::stringstream sstr;
    sstr << "Illegal or unimplemented ISA opcode " << ISA_Inst_Table[opcode].str
         << " (" << (unsigned)opcode << ")"
         << " at byte position " << bytePos - 1 << "(0x" << std::hex
         << bytePos - 1 << ")"
         << ".";
    vISA_ASSERT_UNREACHABLE(sstr.str());
  }
  }
}

static void readAttributesNG(uint8_t major, uint8_t minor, unsigned &bytePos,
                             const char *buf, kernel_format_t &header,
                             attribute_info_t *attributes, int numAttributes,
                             vISA::Mem_Manager &mem) {
  vISA_ASSERT_INPUT(buf != nullptr, "Argument Exception: argument buf    is NULL.");

  for (int i = 0; i < numAttributes; i++) {
    vISA_ASSERT_INPUT(attributes,
                "Argument Exception: argument 'attributes' is NULL");

    readVarBytes(major, minor, attributes[i].nameIndex, bytePos, buf);
    READ_CISA_FIELD(attributes[i].size, uint8_t, bytePos, buf);

    const char *attrName = header.strings[attributes[i].nameIndex];
    char *valueBuffer =
        (char *)mem.alloc(sizeof(char) * (attributes[i].size + 1));
    memcpy_s(valueBuffer, attributes[i].size, &buf[bytePos],
             attributes[i].size);
    bytePos += attributes[i].size;
    vISA::Attributes::ID attrID = vISA::Attributes::getAttributeID(attrName);
    if (vISA::Attributes::isInt32(attrID) || vISA::Attributes::isBool(attrID)) {
      attributes[i].isInt = true;
      switch (attributes[i].size) {
      case 0:
        // No value attribute, treat it as boolean internally.
        attributes[i].value.intVal = 1;
        break;
      case 1:
        attributes[i].value.intVal = *valueBuffer;
        break;
      case 2:
        attributes[i].value.intVal = *((short *)valueBuffer);
        break;
      case 4:
        attributes[i].value.intVal = *((int *)valueBuffer);
        break;
      default:
        vISA_ASSERT_UNREACHABLE("Unsupported attribute size.");
        break;
      }
    } else if (vISA::Attributes::isCStr(attrID)) {
      valueBuffer[attributes[i].size] = '\0';
      attributes[i].isInt =
          false; // by default assume attributes have string value
      attributes[i].value.stringVal = valueBuffer;
    } else {
      std::string errMsg(attrName);
      vISA_ASSERT_INPUT(false, "%s: unsupported attribute!", errMsg.c_str());
    }
  }
}

static std::string getDeclLabelString(const char *prefix, uint32_t index,
                                      kernel_format_t &header,
                                      VISA_Label_Kind kind) {
  if (index) {
    if (kind == LABEL_FC)
      return header.strings[index];
    std::stringstream sstr;
    sstr << header.strings[index] << "_" << index;
    return sstr.str();
  }
  return prefix;
}

static void addAllAttributesNG(kernel_format_t &Header, VISAKernelImpl *KBImpl,
                               CISA_GEN_VAR *GenVar, const uint32_t AttrsCount,
                               attribute_info_t *Attrs) {
  if (AttrsCount > 0) {
    KBImpl->resizeAttribute(GenVar, AttrsCount);

    for (unsigned ai = 0; ai < AttrsCount; ai++) {
      attribute_info_t *attribute = &Attrs[ai];
      KBImpl->AddAttributeToVarGeneric(
          GenVar, Header.strings[attribute->nameIndex], attribute->size,
          attribute->isInt ? (const char *)&attribute->value.intVal
                           : attribute->value.stringVal);
    }
  }
}

static void readRoutineNG(unsigned &bytePos, const char *buf,
                          vISA::Mem_Manager &mem, RoutineContainer &container) {
  kernel_format_t header;
  uint8_t majorVersion = container.majorVersion;
  uint8_t minorVersion = container.minorVersion;

  VISAKernelImpl *kernelBuilderImpl =
      ((VISAKernelImpl *)container.kernelBuilder);
  bool isKernel = kernelBuilderImpl->getIsKernel();

  unsigned kernelStart = bytePos;

  readVarBytes(majorVersion, minorVersion, header.string_count, bytePos, buf);
  header.strings =
      (const char **)mem.alloc(header.string_count * sizeof(char *));
  container.stringPool.resize(header.string_count);
  for (unsigned i = 0; i < header.string_count; i++) {
    char *str = (char *)mem.alloc(STRING_LEN);
    unsigned j = 0;
    while (buf[bytePos] != '\0' && j < STRING_LEN) {
      str[j++] = buf[bytePos++];
    }
    vISA_ASSERT_INPUT(j < STRING_LEN, "string exceeds the maximum length allowed");
    str[j] = '\0';
    bytePos++;
    header.strings[i] = str;
    container.stringPool[i] = str;
  }
  readVarBytes(majorVersion, minorVersion, header.name_index, bytePos, buf);

  /// read general variables
  unsigned numPreDefinedVars = Get_CISA_PreDefined_Var_Count();
  readVarBytes(majorVersion, minorVersion, header.variable_count, bytePos, buf);
  header.variables = (var_info_t *)mem.alloc(
      sizeof(var_info_t) * (header.variable_count + numPreDefinedVars));
  container.generalVarDecls = (VISA_GenVar **)mem.alloc(
      sizeof(VISA_GenVar *) * (header.variable_count + numPreDefinedVars));
  container.generalVarsCount = (header.variable_count + numPreDefinedVars);

  for (unsigned i = numPreDefinedVars;
       i < header.variable_count + numPreDefinedVars; i++) {
    unsigned declID = i;
    readVarBytes(majorVersion, minorVersion,
                 header.variables[declID].name_index, bytePos, buf);
    READ_CISA_FIELD(header.variables[declID].bit_properties, uint8_t, bytePos,
                    buf);
    READ_CISA_FIELD(header.variables[declID].num_elements, uint16_t, bytePos,
                    buf);
    readVarBytes(majorVersion, minorVersion,
                 header.variables[declID].alias_index, bytePos, buf);
    READ_CISA_FIELD(header.variables[declID].alias_offset, uint16_t, bytePos,
                    buf);

    READ_CISA_FIELD(header.variables[declID].alias_scope_specifier, uint8_t,
                    bytePos, buf);

    READ_CISA_FIELD(header.variables[declID].attribute_count, uint8_t, bytePos,
                    buf);

    header.variables[declID].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.variables[declID].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.variables[declID].attributes,
                     header.variables[declID].attribute_count, mem);
    header.variables[declID].dcl = NULL;

    /// VISA Builder Call
    var_info_t *var = &header.variables[declID];
    VISA_GenVar *decl = NULL;
    VISA_Type varType = (VISA_Type)((var->bit_properties) & 0xF);
    VISA_Align varAlign = (VISA_Align)((var->bit_properties >> 4) & 0xF);
    [[maybe_unused]] uint8_t aliasScopeSpecifier =
        header.variables[declID].alias_scope_specifier;
    [[maybe_unused]] int status = VISA_SUCCESS;

    vISA_ASSERT(aliasScopeSpecifier == 0,
           "file scope variables are no longer supported");

    {
      VISA_GenVar *parentDecl = NULL;
      uint16_t aliasOffset = 0;
      uint32_t aliasIndex = header.variables[declID].alias_index;
      if (aliasIndex > 0) {
        if (aliasIndex < numPreDefinedVars) {
          status = kernelBuilderImpl->GetPredefinedVar(
              parentDecl, (PreDefined_Vars)aliasIndex);
          vISA_ASSERT(status == VISA_SUCCESS,
                      "Invalid index for pre-defined variables");
        } else {
          parentDecl = container.generalVarDecls[aliasIndex];
        }
        aliasOffset = header.variables[declID].alias_offset;
      }

      status = kernelBuilderImpl->CreateVISAGenVar(
          decl, header.strings[var->name_index], var->num_elements, varType,
          varAlign, parentDecl, aliasOffset);
      vISA_ASSERT(VISA_SUCCESS == status,
                  "Failed to add VISA general variable.");
    }

    addAllAttributesNG(header, kernelBuilderImpl, decl, var->attribute_count,
                       var->attributes);
    container.generalVarDecls[i] = decl;
  }

  /// read address variables
  READ_CISA_FIELD(header.address_count, uint16_t, bytePos, buf);
  header.addresses =
      (addr_info_t *)mem.alloc(sizeof(addr_info_t) * header.address_count);
  container.addressVarDecls = (VISA_AddrVar **)mem.alloc(
      sizeof(VISA_AddrVar *) * (header.address_count));
  container.addressVarsCount = (header.address_count);
  for (unsigned i = 0; i < header.address_count; i++) {
    unsigned declID = i;
    readVarBytes(majorVersion, minorVersion,
                 header.addresses[declID].name_index, bytePos, buf);
    READ_CISA_FIELD(header.addresses[declID].num_elements, uint16_t, bytePos,
                    buf);
    READ_CISA_FIELD(header.addresses[declID].attribute_count, uint8_t, bytePos,
                    buf);
    header.addresses[declID].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.addresses[declID].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.addresses[declID].attributes,
                     header.addresses[declID].attribute_count, mem);
    header.addresses[declID].dcl = NULL;

    /// VISA Builder Call
    addr_info_t *var = &header.addresses[declID];
    VISA_AddrVar *decl = NULL;
    [[maybe_unused]] int status = kernelBuilderImpl->CreateVISAAddrVar(
        decl, header.strings[var->name_index], var->num_elements);
    vISA_ASSERT(VISA_SUCCESS == status, "Failed to add VISA address variable.");

    addAllAttributesNG(header, kernelBuilderImpl, decl, var->attribute_count,
                       var->attributes);
    container.addressVarDecls[i] = decl;
  }

  // read predicate variables
  READ_CISA_FIELD(header.predicate_count, uint16_t, bytePos, buf);
  header.predicates = (pred_info_t *)mem.alloc(
      sizeof(pred_info_t) *
      (header.predicate_count + COMMON_ISA_NUM_PREDEFINED_PRED));
  container.predicateVarDecls = (VISA_PredVar **)mem.alloc(
      sizeof(VISA_PredVar *) *
      (header.predicate_count + COMMON_ISA_NUM_PREDEFINED_PRED));
  container.predicateVarsCount =
      (header.predicate_count + COMMON_ISA_NUM_PREDEFINED_PRED);
  for (unsigned i = COMMON_ISA_NUM_PREDEFINED_PRED;
       i < (unsigned)(header.predicate_count + COMMON_ISA_NUM_PREDEFINED_PRED);
       i++) {
    unsigned declID = i;
    readVarBytes(majorVersion, minorVersion,
                 header.predicates[declID].name_index, bytePos, buf);
    READ_CISA_FIELD(header.predicates[declID].num_elements, uint16_t, bytePos,
                    buf);
    READ_CISA_FIELD(header.predicates[declID].attribute_count, uint8_t, bytePos,
                    buf);
    header.predicates[declID].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.predicates[declID].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.predicates[declID].attributes,
                     header.predicates[declID].attribute_count, mem);
    header.predicates[declID].dcl = NULL;

    /// VISA Builder Call
    pred_info_t *var = &header.predicates[declID];
    VISA_PredVar *decl = NULL;
    [[maybe_unused]] int status = kernelBuilderImpl->CreateVISAPredVar(
        decl, header.strings[var->name_index], var->num_elements);
    vISA_ASSERT(VISA_SUCCESS == status,
                "Failed to add VISA predicate vairable.");

    addAllAttributesNG(header, kernelBuilderImpl, decl, var->attribute_count,
                       var->attributes);
    container.predicateVarDecls[i] = decl;
  }

  // read label variables
  READ_CISA_FIELD(header.label_count, uint16_t, bytePos, buf);
  header.labels =
      (label_info_t *)mem.alloc(sizeof(label_info_t) * header.label_count);
  container.labelVarDecls = (VISA_LabelOpnd **)mem.alloc(
      sizeof(VISA_LabelOpnd *) * (header.label_count));
  container.labelVarsCount = header.label_count;
  for (unsigned i = 0; i < header.label_count; i++) {
    readVarBytes(majorVersion, minorVersion, header.labels[i].name_index,
                 bytePos, buf);
    READ_CISA_FIELD(header.labels[i].kind, uint8_t, bytePos, buf);
    READ_CISA_FIELD(header.labels[i].attribute_count, uint8_t, bytePos, buf);
    header.labels[i].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.labels[i].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.labels[i].attributes,
                     header.labels[i].attribute_count, mem);

    /// VISA Builder Call
    unsigned declID = i;
    label_info_t *var = &header.labels[declID];
    VISA_LabelOpnd *decl = NULL;
    [[maybe_unused]] int status = kernelBuilderImpl->CreateVISALabelVar(
        decl,
        getDeclLabelString("L", var->name_index, header,
                           VISA_Label_Kind(var->kind))
            .c_str(),
        VISA_Label_Kind(var->kind));
    vISA_ASSERT(VISA_SUCCESS == status, "Failed to add VISA label variable.");

    for (unsigned ai = 0; ai < var->attribute_count; ai++) {
      /// TODO: How to Add label decls and attributes correctly.
      /// kernelBuilderImpl->AddAttributeToVar(decl,
      /// header.strings[attribute->nameIndex], attribute->size,
      /// attribute->value.stringVal);
      vISA_ASSERT(false, "Currently the builder API does not support label "
                         "attributes. Please file a bug.");
    }

    container.labelVarDecls[i] = decl;
  }

  // read sampler variables
  READ_CISA_FIELD(header.sampler_count, uint8_t, bytePos, buf);
  // up to 31 pre-defined samplers are allowed
  vISA_ASSERT(header.sampler_count < COMMON_ISA_MAX_NUM_SAMPLERS,
               "number of vISA samplers exceeds the max");
  header.samplers = (state_info_t *)mem.alloc(sizeof(state_info_t) *
                                              COMMON_ISA_MAX_NUM_SAMPLERS);
  container.samplerVarDecls = (VISA_SamplerVar **)mem.alloc(
      sizeof(VISA_SamplerVar *) * COMMON_ISA_MAX_NUM_SAMPLERS);
  container.samplerVarsCount = header.sampler_count;
  for (unsigned i = 0; i < header.sampler_count; i++) {
    readVarBytes(majorVersion, minorVersion, header.samplers[i].name_index,
                 bytePos, buf);
    READ_CISA_FIELD(header.samplers[i].num_elements, uint16_t, bytePos, buf);
    READ_CISA_FIELD(header.samplers[i].attribute_count, uint8_t, bytePos, buf);
    header.samplers[i].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.samplers[i].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.samplers[i].attributes,
                     header.samplers[i].attribute_count, mem);

    /// VISA Builder Call
    unsigned declID = i;
    state_info_t *var = &header.samplers[declID];
    VISA_SamplerVar *decl = NULL;
    [[maybe_unused]] int status = kernelBuilderImpl->CreateVISASamplerVar(
        decl, header.strings[var->name_index], var->num_elements);
    vISA_ASSERT(VISA_SUCCESS == status, "Failed to add VISA sampler variable.");

    addAllAttributesNG(header, kernelBuilderImpl, decl, var->attribute_count,
                       var->attributes);
    container.samplerVarDecls[i] = decl;
  }
  // bindless sampler field
  constexpr int BINDLESS_SAMPLER_ID = 31;
  kernelBuilderImpl->GetBindlessSampler(
      container.samplerVarDecls[BINDLESS_SAMPLER_ID]);

  // read surface variables
  READ_CISA_FIELD(header.surface_count, uint8_t, bytePos, buf);
  unsigned num_pred_surf = Get_CISA_PreDefined_Surf_Count();
  header.surface_count += (uint8_t)num_pred_surf;
  header.surface_attrs = (bool *)mem.alloc(sizeof(bool) * header.surface_count);
  memset(header.surface_attrs, 0, sizeof(bool) * header.surface_count);
  header.surfaces =
      (state_info_t *)mem.alloc(sizeof(state_info_t) * header.surface_count);
  container.surfaceVarDecls = (VISA_SurfaceVar **)mem.alloc(
      sizeof(VISA_SurfaceVar *) * (header.surface_count));
  container.surfaceVarsCount = header.surface_count;

  /// Populate the predefined surfaces.
  for (unsigned i = 0; i < num_pred_surf; i++) {
    VISA_SurfaceVar *surfaceHnd = NULL;
    kernelBuilderImpl->GetPredefinedSurface(surfaceHnd, (PreDefined_Surface)i);
    container.surfaceVarDecls[i] = surfaceHnd;
  }

  /// Populate the rest of the surfaces.
  for (unsigned i = num_pred_surf; i < header.surface_count; i++) {
    readVarBytes(majorVersion, minorVersion, header.surfaces[i].name_index,
                 bytePos, buf);
    READ_CISA_FIELD(header.surfaces[i].num_elements, uint16_t, bytePos, buf);
    READ_CISA_FIELD(header.surfaces[i].attribute_count, uint8_t, bytePos, buf);
    header.surfaces[i].attributes = (attribute_info_t *)mem.alloc(
        sizeof(attribute_info_t) * header.surfaces[i].attribute_count);
    readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                     header.surfaces[i].attributes,
                     header.surfaces[i].attribute_count, mem);

    /// VISA Builder Call
    unsigned declID = i;
    state_info_t *var = &header.surfaces[declID];
    VISA_SurfaceVar *decl = NULL;
    [[maybe_unused]] int status = kernelBuilderImpl->CreateVISASurfaceVar(
        decl, header.strings[var->name_index], var->num_elements);
    vISA_ASSERT(VISA_SUCCESS == status, "Failed to add VISA surface variable.");

    addAllAttributesNG(header, kernelBuilderImpl, decl, var->attribute_count,
                       var->attributes);

    for (unsigned ai = 0; ai < var->attribute_count; ai++) {
      attribute_info_t *attribute = &var->attributes[ai];

      /// TODO: Does this code even make sense anymore???
      const char *attrName = header.strings[attribute->nameIndex];
      if (Attributes::isAttribute(Attributes::ATTR_SurfaceUsage, attrName)) {
        header.surface_attrs[i] = (attribute->value.intVal == 2);
        break;
      }
    }

    container.surfaceVarDecls[i] = decl;
  }

  [[maybe_unused]] int vmeCount = 0;
  READ_CISA_FIELD(vmeCount, uint8_t, bytePos, buf);
  vISA_ASSERT(vmeCount == 0, "VME variable is no longer supported");
  header.vme_count = 0;

  // read input variables
  if (isKernel) {
    readVarBytes(container.majorVersion, container.minorVersion,
                 header.input_count, bytePos, buf, FIELD_TYPE::INPUT);

    header.inputs =
        (input_info_t *)mem.alloc(sizeof(input_info_t) * header.input_count);
    container.inputVarDecls = (CISA_GEN_VAR **)mem.alloc(
        sizeof(CISA_GEN_VAR *) * (header.input_count));
    container.inputVarsCount = header.input_count;
    for (unsigned i = 0; i < header.input_count; i++) {
      READ_CISA_FIELD(header.inputs[i].kind, uint8_t, bytePos, buf);
      readVarBytes(majorVersion, minorVersion, header.inputs[i].index, bytePos,
                   buf);
      READ_CISA_FIELD(header.inputs[i].offset, int16_t, bytePos, buf);
      READ_CISA_FIELD(header.inputs[i].size, uint16_t, bytePos, buf);

      unsigned declID = i;
      input_info_t *var = &header.inputs[declID];
      CISA_GEN_VAR *decl = NULL;

      switch (var->getInputClass()) {
      case INPUT_GENERAL:
        decl = container.generalVarDecls[var->index];
        break;
      case INPUT_SAMPLER:
        decl = container.samplerVarDecls[var->index];
        break;
      case INPUT_SURFACE:
        decl = container.surfaceVarDecls[var->index];
        break;
      default:
        vISA_ASSERT_UNREACHABLE("Incorrect input variable type.");
      }

      [[maybe_unused]] int status = kernelBuilderImpl->CreateVISAInputVar(
          decl, var->offset, var->size, var->getImplicitKind());
      vISA_ASSERT(VISA_SUCCESS == status, "Failed to add VISA input variable.");

      container.inputVarDecls[i] = decl;
    }
  }

  READ_CISA_FIELD(header.size, unsigned, bytePos, buf);
  READ_CISA_FIELD(header.entry, unsigned, bytePos, buf);

  if (!isKernel) {
    READ_CISA_FIELD(header.input_size, uint8_t, bytePos, buf);
    READ_CISA_FIELD(header.return_value_size, uint8_t, bytePos, buf);

    // Store size of arg/ret registers for stack call functions
    kernelBuilderImpl->setInputSize(header.input_size);
    kernelBuilderImpl->setReturnSize(header.return_value_size);
  }

  /// read kernel attributes
  READ_CISA_FIELD(header.attribute_count, uint16_t, bytePos, buf);
  header.attributes = (attribute_info_t *)mem.alloc(sizeof(attribute_info_t) *
                                                    header.attribute_count);
  readAttributesNG(majorVersion, minorVersion, bytePos, buf, header,
                   header.attributes, header.attribute_count, mem);

  for (unsigned ai = 0; ai < header.attribute_count; ai++) {
    attribute_info_t *attribute = &header.attributes[ai];
    /// TODO: This parameter ordering is inconsistent.
    kernelBuilderImpl->AddKernelAttribute(
        header.strings[attribute->nameIndex], attribute->size,
        attribute->isInt ? (char *)&attribute->value.intVal
                         : attribute->value.stringVal);
  }
  if (!kernelBuilderImpl->getKernelAttributes()->isKernelAttrSet(
          vISA::Attributes::ATTR_Target)) {
    VISATarget target = kernelBuilderImpl->getOptions()->getTarget();
    kernelBuilderImpl->AddKernelAttribute("Target", sizeof(target), &target);
  }

  unsigned kernelEntry = kernelStart + header.entry;
  unsigned kernelEnd = kernelEntry + header.size;

  bytePos = kernelEntry;

  for (unsigned i = 0; bytePos < kernelEnd; i++) {
    readInstructionNG(bytePos, buf, container, i);
  }
}

#define READ_FIELD_FROM_BUF(dst, type)                                         \
  dst = *((type *)&buf[byte_pos]);                                             \
  byte_pos += sizeof(type);

static int processCommonISAHeader(common_isa_header &cisaHdr,
                                  unsigned &byte_pos, const void *cisaBuffer,
                                  vISA::Mem_Manager *mem) {
  const char *buf = (const char *)cisaBuffer;
  READ_FIELD_FROM_BUF(cisaHdr.magic_number, uint32_t);
  READ_FIELD_FROM_BUF(cisaHdr.major_version, uint8_t);
  READ_FIELD_FROM_BUF(cisaHdr.minor_version, uint8_t);
  READ_FIELD_FROM_BUF(cisaHdr.num_kernels, uint16_t);

  vISA_ASSERT(cisaHdr.major_version >= 3,
              "only vISA version 3.0 and above are supported");

  if (cisaHdr.num_kernels) {
    cisaHdr.kernels = (kernel_info_t *)mem->alloc(sizeof(kernel_info_t) *
                                                  cisaHdr.num_kernels);
    vASSERT(cisaHdr.kernels != nullptr);
  } else {
    cisaHdr.kernels = NULL;
  }

  for (int i = 0; i < cisaHdr.num_kernels; i++) {
    if (cisaHdr.major_version == 3 && cisaHdr.minor_version < 7) {
      READ_FIELD_FROM_BUF(cisaHdr.kernels[i].name_len, uint8_t);
    } else {
      READ_FIELD_FROM_BUF(cisaHdr.kernels[i].name_len, uint16_t);
    }
    cisaHdr.kernels[i].name =
        (char *)mem->alloc(cisaHdr.kernels[i].name_len + 1);
    memcpy_s(cisaHdr.kernels[i].name,
             cisaHdr.kernels[i].name_len * sizeof(uint8_t), &buf[byte_pos],
             cisaHdr.kernels[i].name_len * sizeof(uint8_t));
    cisaHdr.kernels[i].name[cisaHdr.kernels[i].name_len] = '\0';
    byte_pos += cisaHdr.kernels[i].name_len;
    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].offset, uint32_t);
    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].size, uint32_t);
    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].input_offset, uint32_t);

    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].variable_reloc_symtab.num_syms,
                        uint16_t);
    vISA_ASSERT(cisaHdr.kernels[i].variable_reloc_symtab.num_syms == 0,
                "relocation symbols not allowed");
    cisaHdr.kernels[i].variable_reloc_symtab.reloc_syms = nullptr;

    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].function_reloc_symtab.num_syms,
                        uint16_t);

    vISA_ASSERT(cisaHdr.kernels[i].function_reloc_symtab.num_syms == 0,
                "relocation symbols not allowed");
    cisaHdr.kernels[i].function_reloc_symtab.reloc_syms = nullptr;
    READ_FIELD_FROM_BUF(cisaHdr.kernels[i].num_gen_binaries, uint8_t);
    cisaHdr.kernels[i].gen_binaries = (gen_binary_info *)mem->alloc(
        cisaHdr.kernels[i].num_gen_binaries * sizeof(gen_binary_info));
    for (int j = 0; j < cisaHdr.kernels[i].num_gen_binaries; j++) {
      READ_FIELD_FROM_BUF(cisaHdr.kernels[i].gen_binaries[j].platform, uint8_t);
      READ_FIELD_FROM_BUF(cisaHdr.kernels[i].gen_binaries[j].binary_offset,
                          uint32_t);
      READ_FIELD_FROM_BUF(cisaHdr.kernels[i].gen_binaries[j].binary_size,
                          uint32_t);
    }

    cisaHdr.kernels[i].cisa_binary_buffer = NULL;
    cisaHdr.kernels[i].genx_binary_buffer = NULL;
  }

  READ_FIELD_FROM_BUF(cisaHdr.num_filescope_variables, uint16_t);
  vISA_ASSERT(cisaHdr.num_filescope_variables == 0,
              "file scope variables are no longer supported");

  READ_FIELD_FROM_BUF(cisaHdr.num_functions, uint16_t);

  if (cisaHdr.num_functions) {
    cisaHdr.functions = (function_info_t *)mem->alloc(sizeof(function_info_t) *
                                                      cisaHdr.num_functions);
    vASSERT(cisaHdr.functions != nullptr);
  } else {
    cisaHdr.functions = NULL;
  }

  for (int i = 0; i < cisaHdr.num_functions; i++) {
    // field is deprecated
    READ_FIELD_FROM_BUF(cisaHdr.functions[i].linkage, uint8_t);

    if (cisaHdr.major_version == 3 && cisaHdr.minor_version < 7) {
      READ_FIELD_FROM_BUF(cisaHdr.functions[i].name_len, uint8_t);
    } else {
      READ_FIELD_FROM_BUF(cisaHdr.functions[i].name_len, uint16_t);
    }
    cisaHdr.functions[i].name =
        (char *)mem->alloc(cisaHdr.functions[i].name_len + 1);
    memcpy_s(cisaHdr.functions[i].name,
             cisaHdr.functions[i].name_len * sizeof(uint8_t), &buf[byte_pos],
             cisaHdr.functions[i].name_len * sizeof(uint8_t));
    cisaHdr.functions[i].name[cisaHdr.functions[i].name_len] = '\0';
    byte_pos += cisaHdr.functions[i].name_len;
    READ_FIELD_FROM_BUF(cisaHdr.functions[i].offset, uint32_t);
    READ_FIELD_FROM_BUF(cisaHdr.functions[i].size, uint32_t);

    READ_FIELD_FROM_BUF(cisaHdr.functions[i].variable_reloc_symtab.num_syms,
                        uint16_t);
    vISA_ASSERT(cisaHdr.functions[i].variable_reloc_symtab.num_syms == 0,
                "variable relocation not supported");
    cisaHdr.functions[i].variable_reloc_symtab.reloc_syms = nullptr;

    READ_FIELD_FROM_BUF(cisaHdr.functions[i].function_reloc_symtab.num_syms,
                        uint16_t);
    vISA_ASSERT(cisaHdr.functions[i].function_reloc_symtab.num_syms == 0,
                "function relocation not supported");
    cisaHdr.functions[i].function_reloc_symtab.reloc_syms = nullptr;

    cisaHdr.functions[i].cisa_binary_buffer = NULL;
    cisaHdr.functions[i].genx_binary_buffer = NULL;
  }

  return 0;
}

//
// buf -- vISA binary to be processed.  For offline compile it's always the
// entire vISA object.
//     For JIT mode it's the entire isa file for 3.0, the kernel isa only
//     for 2.x
// builder -- the vISA builder
// kernels -- IR for the vISA kernel
//      if kernelName is specified, return that kernel only in kernels[0]
//      otherwise, all kernels in the isa are processed and returned in kernel
// kernelName -- name of the kernel to be processed.  If null, all kernels will
// be built majorVerion/minorVersion -- version of the vISA binary returns true
// if IR build succeeds, false otherwise
//
bool readIsaBinaryNG(const char *buf, CISA_IR_Builder *builder,
                     std::vector<VISAKernel *> &kernels, const char *kernelName,
                     unsigned int majorVersion, unsigned int minorVersion) {
  vISA_ASSERT(buf != nullptr, "Argument Exception: argument buf  is NULL.");

  unsigned bytePos = 0;
  vISA::Mem_Manager binaryReaderMem(4096);
  common_isa_header isaHeader;
  isaHeader.num_functions = 0;

  processCommonISAHeader(isaHeader, bytePos, buf, &binaryReaderMem);

  // we have to set the CISA builder version to the binary version,
  // or some instructions that behave differently based on vISA version (e.g.,
  // unaligned oword read) would not work correctly
  builder->CISA_IR_setVersion(isaHeader.major_version, isaHeader.minor_version);

  if (kernelName) {
    int kernelIndex = -1;
    for (unsigned i = 0; i < isaHeader.num_kernels; i++) {
      if (!strcmp(isaHeader.kernels[i].name, kernelName)) {
        kernelIndex = i;
        break;
      }
    }

    if (kernelIndex == -1) {
      return false;
    }

    bytePos = isaHeader.kernels[kernelIndex].offset;

    RoutineContainer container;
    container.builder = builder;
    container.kernelBuilder = NULL;
    container.majorVersion = isaHeader.major_version;
    container.minorVersion = isaHeader.minor_version;

    builder->AddKernel(container.kernelBuilder,
                       isaHeader.kernels[kernelIndex].name);
    kernels.push_back(container.kernelBuilder);

    readRoutineNG(bytePos, buf, binaryReaderMem, container);

    for (unsigned int i = 0; i < isaHeader.num_functions; i++) {
      bytePos = isaHeader.functions[i].offset;

      VISAFunction *funcPtr = NULL;
      builder->AddFunction(funcPtr, isaHeader.functions[i].name);

      container.kernelBuilder = (VISAKernel *)funcPtr;
      kernels.push_back(container.kernelBuilder);

      readRoutineNG(bytePos, buf, binaryReaderMem, container);
    }
  } else {
    for (unsigned int k = 0; k < isaHeader.num_kernels; k++) {
      bytePos = isaHeader.kernels[k].offset;

      RoutineContainer container;
      container.builder = builder;
      container.kernelBuilder = NULL;
      container.majorVersion = isaHeader.major_version;
      container.minorVersion = isaHeader.minor_version;

      builder->AddKernel(container.kernelBuilder, isaHeader.kernels[k].name);
      kernels.push_back(container.kernelBuilder);

      readRoutineNG(bytePos, buf, binaryReaderMem, container);
    }

    for (unsigned int i = 0; i < isaHeader.num_functions; i++) {
      RoutineContainer container;

      container.builder = builder;
      container.majorVersion = isaHeader.major_version;
      container.minorVersion = isaHeader.minor_version;

      bytePos = isaHeader.functions[i].offset;

      VISAFunction *funcPtr = NULL;
      builder->AddFunction(funcPtr, isaHeader.functions[i].name);

      container.kernelBuilder = (VISAKernel *)funcPtr;
      kernels.push_back(container.kernelBuilder);

      readRoutineNG(bytePos, buf, binaryReaderMem, container);
    }
  }

  return true;
}