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

// Copyright (c) 2020-2024, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 with LLVM Exceptions
//    (the "License"); you may not use this file except in compliance with
//    the License. You may obtain a copy of the License at
//
//         https://llvm.org/LICENSE.txt
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

// TOSA flatbuffer generation

#include "include/SerializationPasses.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
#include "mlir/Dialect/Quant/QuantTypes.h" // from @llvm-project
#include "mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
#include "mlir/Dialect/Tosa/IR/TosaOps.h"  // from @llvm-project
#include "mlir/IR/Matchers.h"              // from @llvm-project
#include "mlir/IR/Operation.h"
#include "mlir/Pass/Pass.h"             // from @llvm-project
#include "mlir/Support/LogicalResult.h" // from @llvm-project
#include "mlir/Support/TypeID.h"
#include "mlir/Transforms/DialectConversion.h" // from @llvm-project
#include "tosa_serialization_handler.h"
#include <algorithm>
#include <functional>
#include <map>
#include <unordered_map>

// The namespace might be confusing here. We have mlir::tosa:: defined in MLIR
// and tosa:: defined in serialization library
// TODO: align the namespace
using namespace tosa;

namespace cl = llvm::cl;

llvm::cl::opt<std::string> tosa_flatbuffer_filename(
    "tosa-flatbuffer-filename", llvm::cl::desc("<tosa flatbuffer filename>"),
    llvm::cl::init("tosa_dump.tosa"), llvm::cl::value_desc("filename"));

llvm::cl::opt<std::string> tosa_flatbuffer_schema(
    "tosa-flatbuffer-schema", llvm::cl::desc("<tosa flatbuffer schema file>"),
    llvm::cl::init(""), llvm::cl::value_desc("filename"));

// Specialize mlir::Value for std::hash<T> and std::equal_to<T> to be able to
// build std::unordered_map<mlir::Value, ...>
namespace std {

template <> struct hash<mlir::Value> {
  std::size_t operator()(const mlir::Value &val) const {
    return static_cast<std::size_t>(mlir::hash_value(val));
  }
};

template <> struct equal_to<mlir::Value> {
  bool operator()(const mlir::Value &lhs, const mlir::Value &rhs) const {
    return (lhs == rhs);
  }
};

} // namespace std

static ResizeMode ResizeModeStr2Enum(const std::string &mode_str) {
  if (mode_str == "NEAREST_NEIGHBOR")
    return ResizeMode_NEAREST;
  else if (mode_str == "BILINEAR")
    return ResizeMode_BILINEAR;
  else
    return ResizeMode_UNKNOWN;
}

static DType Type2DType(mlir::Type element_type) {
  if (element_type.isF64() || element_type.isF32()) {
    return DType_FP32;
  } else if (element_type.isFloat8E5M2()) {
    return DType_FP8E5M2;
  } else if (element_type.isFloat8E4M3FN()) {
    return DType_FP8E4M3;
  } else if (element_type.isF16()) {
    return DType_FP16;
  } else if (element_type.isBF16()) {
    return DType_BF16;
  } else if (element_type.isUnsignedInteger(8)) {
    return DType_UINT8;
  } else if (element_type.isInteger(4)) {
    return DType_INT4;
  } else if (element_type.isInteger(8)) {
    return DType_INT8;
  } else if (element_type.isInteger(16)) {
    return DType_INT16;
  } else if (element_type.isInteger(32)) {
    return DType_INT32;
  } else if (element_type.isInteger(48)) {
    return DType_INT48;
  }
  // boolean in MLIR treated as integer with bitwidth 1
  else if (element_type.isInteger(1)) {
    return DType_BOOL;
  }
  return DType_UNKNOWN;
}

static DType Type2AccDType(mlir::Type element_type) {
  // def Tosa_AccType : AnyTypeOf<[I<32>, I<48>, F16, F32]>;
  if (element_type.isF32()) {
    return DType_FP32;
  } else if (element_type.isF16()) {
    return DType_FP16;
  } else if (element_type.isInteger(32)) {
    return DType_INT32;
  } else if (element_type.isInteger(48)) {
    return DType_INT48;
  }
  return DType_UNKNOWN;
}
class TosaSerializationBlockBuilder;
class TosaSerializationRegionBuilder;

std::unordered_map<std::string, mlir::Operation *> variable_tensor_op_map;
std::unordered_map<std::string, std::string>
    variable_tensor_flatbuffer_name_map;
static int variable_tensor_index = 0;

namespace {

// for now, this is a global map of variables
void RegisterVariableOp(mlir::Operation &op) {
  std::string variable_tensor_flatbuffer_name =
      "Variable_" + std::to_string(variable_tensor_index++);
  std::string variable_tensor_mlir_name =
      op.getAttr("name").cast<mlir::StringAttr>().getValue().str();
  variable_tensor_op_map[variable_tensor_flatbuffer_name] = &op;
  variable_tensor_flatbuffer_name_map[variable_tensor_mlir_name] =
      variable_tensor_flatbuffer_name;
}

} // namespace

class TosaSerializationOperatorBuilder {
public:
  TosaSerializationOperatorBuilder(
      TosaSerializationBlockBuilder *_block_builder)
      : block_builder(_block_builder) {}

  template <typename T>
  TosaSerializationOperator *build(mlir::Operation &op) const;
  TosaSerializationHandler *GetTsh() const;
  TosaSerializationRegionBuilder *GetRegionBuilder() const;
  mlir::LogicalResult GetDataFromAttribute(mlir::Operation &op,
                                           mlir::Attribute &attr,
                                           mlir::Type element_type,
                                           std::vector<uint8_t> &u8_data) const;

  // populate u8_data with either int64_value or float_value depending on
  // element_type
  mlir::LogicalResult
  GetU8DataFromIntOrFloatValue(int64_t int64_value, float fp_value,
                               mlir::Type element_type,
                               std::vector<uint8_t> &u8_data) const;

  // populate u8_data with int_value depending on non-float element_type
  mlir::LogicalResult
  GetU8DataFromIntValues(const std::vector<int64_t> &int_values,
                         mlir::Type element_type,
                         std::vector<uint8_t> &u8_data) const;

  // populate u8_data with fp_value depending on float element_type
  mlir::LogicalResult
  GetU8DataFromFloatValues(const std::vector<float> &fp_values,
                           mlir::Type element_type,
                           std::vector<uint8_t> &u8_data) const;

private:
  std::string GetTensorName(mlir::Value val) const;
  std::string GetVariableTensorName(mlir::Operation *op) const;
  TosaSerializationOperator *BuildPoolOpFromMlirOp(mlir::Operation &op,
                                                   Op opcode) const;
  TosaSerializationOperator *BuildEwiseBinaryOpFromMlirOp(mlir::Operation &op,
                                                          Op opcode) const;
  TosaSerializationOperator *BuildEwiseUnaryOpFromMlirOp(mlir::Operation &op,
                                                         Op opcode) const;
  TosaSerializationOperator *BuildReductionOpFromMlirOp(mlir::Operation &op,
                                                        Op opcode) const;
  TosaSerializationBlockBuilder *block_builder;
};

// This builder assumes each region only has only one block
class TosaSerializationBlockBuilder {
public:
  // constructor
  TosaSerializationBlockBuilder(TosaSerializationBasicBlock *_ser_block,
                                TosaSerializationRegionBuilder *_region_builder,
                                mlir::Block *_block)
      : ser_block(_ser_block), region_builder(_region_builder), block(_block) {}

  mlir::LogicalResult
  BuildAllOpsInBlock(std::vector<mlir::Value> &return_values);
  TosaSerializationBasicBlock *GetBlock() const { return ser_block; }
  TosaSerializationRegionBuilder *GetRegionBuilder() const {
    return region_builder;
  }
  TosaSerializationHandler *GetTsh() const;
  std::unordered_map<mlir::Value, std::string> &GetTensorMap() {
    return tensor_map;
  }

private:
  TosaSerializationOperator *BuildTosaSerializationOperator(
      const TosaSerializationOperatorBuilder &op_builder, mlir::Operation &op);
  TosaSerializationTensor *
  BuildTosaSerializationVariableTensor(mlir::RankedTensorType tensor_type,
                                       const std::string &name,
                                       const std::string &variable_mlir_name);
  TosaSerializationTensor *
  BuildTosaSerializationTensor(mlir::Value val, const std::string &name);

  TosaSerializationBasicBlock *ser_block;
  TosaSerializationRegionBuilder *region_builder;
  mlir::Block *block;
  std::unordered_map<mlir::Value, std::string> tensor_map;
  std::unordered_map<mlir::Value, std::string> input_tensor_map;
};

class TosaSerializationRegionBuilder {
public:
  // Constructor
  TosaSerializationRegionBuilder(
      TosaSerializationRegion *_ser_region, mlir::Region *_region,
      TosaSerializationRegionBuilder *_parent_value_scope,
      TosaSerializationHandler *_tsh)
      : ser_region(_ser_region), region(_region),
        parent_value_scope(_parent_value_scope), tsh(_tsh) {}
  TosaSerializationHandler *GetTsh() const { return tsh; }
  mlir::LogicalResult
  BuildAllBlocksInRegion(bool is_top, std::vector<mlir::Value> &return_values);
  TosaSerializationRegionBuilder *GetParentValueScope() const {
    return parent_value_scope;
  }
  std::vector<TosaSerializationBlockBuilder *> &GetBlockBuilders() {
    return block_builders;
  }

private:
  TosaSerializationRegion *ser_region;
  mlir::Region *region;
  TosaSerializationRegionBuilder *parent_value_scope;
  TosaSerializationHandler *tsh;
  std::vector<TosaSerializationBlockBuilder *> block_builders;
};

TosaSerializationHandler *TosaSerializationOperatorBuilder::GetTsh() const {
  return block_builder->GetTsh();
}
TosaSerializationHandler *TosaSerializationBlockBuilder::GetTsh() const {
  return region_builder->GetTsh();
}
TosaSerializationRegionBuilder *
TosaSerializationOperatorBuilder::GetRegionBuilder() const {
  return block_builder->GetRegionBuilder();
}

std::string
TosaSerializationOperatorBuilder::GetTensorName(mlir::Value val) const {
  auto value_scope = GetRegionBuilder();
  while (value_scope) {
    // Traverse through each block builder in the region
    for (auto curr_block_builder : value_scope->GetBlockBuilders()) {
      const auto &tensor_map = curr_block_builder->GetTensorMap();
      if (tensor_map.count(val)) {
        return tensor_map.at(val);
      }
    }
    value_scope = value_scope->GetParentValueScope();
  }
  // Didn't find anything
  llvm::errs() << "ERROR: Failed to get mlir::Value from tensor_map\n";
  assert(0);
}

// Unpack 64-bit integer attribute element and pack into a std vector.
template <class T>
static std::vector<T> getDenseI64ArrayAttr(mlir::Attribute attr) {
  auto array_ref = attr.cast<mlir::DenseI64ArrayAttr>().asArrayRef();

  std::vector<T> vec;
  for (auto val : array_ref) {
    vec.push_back(val);
  }

  return vec;
}

// Unpack 8-bit integer attribute element and pack into a std vector.
template <class T>
static std::vector<T> getDenseI8ArrayAttr(mlir::Attribute attr) {
  auto array_ref = attr.cast<mlir::DenseI8ArrayAttr>().asArrayRef();

  std::vector<T> vec;
  for (auto val : array_ref) {
    vec.push_back(val);
  }

  return vec;
}

std::string TosaSerializationOperatorBuilder::GetVariableTensorName(
    mlir::Operation *op) const {
  std::string variable_tensor_mlir_name =
      op->getAttr("name").cast<mlir::StringAttr>().getValue().str();

  if (variable_tensor_flatbuffer_name_map.find(variable_tensor_mlir_name) ==
      variable_tensor_flatbuffer_name_map.end()) {
    llvm::errs() << "ERROR: Failed to find key " << variable_tensor_mlir_name
                 << " from variable_tensor_flatbuffer_name_map\n";
    assert(0);
  }
  return variable_tensor_flatbuffer_name_map[variable_tensor_mlir_name];
}

mlir::LogicalResult TosaSerializationOperatorBuilder::GetDataFromAttribute(
    mlir::Operation &op, mlir::Attribute &attr, mlir::Type element_type,
    std::vector<uint8_t> &u8_data) const {
  if (!element_type.isIntOrFloat()) {
    return mlir::failure();
  }
  auto dense_attr = attr.dyn_cast<mlir::DenseElementsAttr>();

  // handle float types
  if (element_type.isa<mlir::FloatType>()) {
    std::vector<float> fp_data;
    auto val_attr = attr.dyn_cast<mlir::FloatAttr>();

    if (dense_attr) {
      for (auto val : dense_attr.getValues<mlir::APFloat>()) {
        fp_data.push_back(val.convertToFloat());
      }
    } else if (val_attr) {
      fp_data.push_back((float)val_attr.getValueAsDouble());
    } else {
      op.emitOpError("Unknown const attribute");
      return mlir::failure();
    }

    return GetU8DataFromFloatValues(fp_data, element_type, u8_data);
  }

  // element_type is integer type

  bool isInt48 = element_type.isInteger(48);
  std::vector<int64_t> i64_data;

  auto val_attr = attr.dyn_cast<mlir::IntegerAttr>();
  if (dense_attr) {
    for (auto valueIt : dense_attr.getValues<mlir::APInt>()) {
      int64_t val = isInt48 ? static_cast<int64_t>(valueIt.getLimitedValue())
                            : valueIt.getSExtValue();
      i64_data.push_back(val);
    }
  } else if (val_attr) {
    i64_data.push_back(val_attr.getInt());
  } else {
    op.emitOpError("Unknown const attribute");
    return mlir::failure();
  }

  return GetU8DataFromIntValues(i64_data, element_type, u8_data);
}

mlir::LogicalResult TosaSerializationOperatorBuilder::GetU8DataFromIntValues(
    const std::vector<int64_t> &int64_values, mlir::Type element_type,
    std::vector<uint8_t> &u8_data) const {
  switch (element_type.getIntOrFloatBitWidth()) {
  case 1: {
    // bool use bool vec
    std::vector<bool> bool_values;
    for (auto v : int64_values) {
      bool bool_value = v == 0 ? false : true;
      bool_values.push_back(bool_value);
    }
    TosaSerializationHandler::ConvertBooltoU8(bool_values, u8_data);
    break;
  }
  case 4:
  case 8: {
    // I4 and I8 use int8_t vec
    std::vector<int8_t> i8_values;
    for (auto v : int64_values) {
      i8_values.push_back(static_cast<int8_t>(v));
    }
    if (element_type.isInteger(4)) {
      TosaSerializationHandler::ConvertI4toU8(i8_values, u8_data);
    } else {
      TosaSerializationHandler::ConvertI8toU8(i8_values, u8_data);
    }
    break;
  }
  case 16: {
    // I16 use int16_t vec
    std::vector<int16_t> i16_values;
    for (auto v : int64_values) {
      i16_values.push_back(static_cast<int16_t>(v));
    }
    TosaSerializationHandler::ConvertI16toU8(i16_values, u8_data);
    break;
  }
  case 32: {
    // I32 use int32_t vec
    std::vector<int32_t> i32_values;
    for (auto v : int64_values) {
      i32_values.push_back(static_cast<int32_t>(v));
    }
    TosaSerializationHandler::ConvertI32toU8(i32_values, u8_data);
    break;
  }
  case 48: {
    // I48 use int64_t vec
    TosaSerializationHandler::ConvertI48toU8(int64_values, u8_data);
    break;
  }
  default: {
    // unsupported bit widths
    return mlir::failure();
  }
  }
  return mlir::success();
}

mlir::LogicalResult TosaSerializationOperatorBuilder::GetU8DataFromFloatValues(
    const std::vector<float> &fp_values, mlir::Type element_type,
    std::vector<uint8_t> &u8_data) const {
  assert(
      element_type
          .isa<mlir::FloatType>()); // this should only be called for float type
  if (element_type.isF16()) {
    TosaSerializationHandler::ConvertF16toU8(fp_values, u8_data);
  } else if (element_type.isBF16()) {
    TosaSerializationHandler::ConvertBF16toU8(fp_values, u8_data);
  } else if (element_type.isFloat8E4M3FN()) {
    TosaSerializationHandler::ConvertFP8E4M3toU8(fp_values, u8_data);
  } else if (element_type.isFloat8E5M2()) {
    TosaSerializationHandler::ConvertFP8E5M2toU8(fp_values, u8_data);
  } else if (element_type.isF32()) {
    TosaSerializationHandler::ConvertF32toU8(fp_values, u8_data);
  } else {
    return mlir::failure();
  }
  return mlir::success();
}

mlir::LogicalResult
TosaSerializationOperatorBuilder::GetU8DataFromIntOrFloatValue(
    int64_t int64_value, float fp_value, mlir::Type element_type,
    std::vector<uint8_t> &u8_data) const {
  if (element_type.isa<mlir::FloatType>()) {
    return GetU8DataFromFloatValues({fp_value}, element_type, u8_data);
  } else {
    return GetU8DataFromIntValues({int64_value}, element_type, u8_data);
  }
}

// Main template to catch unimplemented translation.
template <typename T>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build(mlir::Operation &op) const {
  llvm::errs() << "Translation of operator " << op.getName().getStringRef()
               << " is not implemented yet\n";
  return nullptr;
}

/* Start translating TOSA operator */

#define ASSERT_VECTOR_LENGTH(VECTOR, LENGTH)                                   \
  if (VECTOR.size() != LENGTH) {                                               \
    std::string msg;                                                           \
    msg = std::string(#VECTOR) + " is [" + std::to_string(VECTOR.size()) +     \
          "], but expected to be [" + std::to_string(LENGTH) + "]\n";          \
    op.emitOpError(msg.c_str());                                               \
    return nullptr;                                                            \
  }

TosaSerializationOperator *
TosaSerializationOperatorBuilder::BuildPoolOpFromMlirOp(mlir::Operation &op,
                                                        Op opcode) const {
  auto pad = getDenseI64ArrayAttr<int>(op.getAttr("pad"));
  ASSERT_VECTOR_LENGTH(pad, 4);

  auto stride = getDenseI64ArrayAttr<int>(op.getAttr("stride"));
  ASSERT_VECTOR_LENGTH(stride, 2);

  auto kernel = getDenseI64ArrayAttr<int>(op.getAttr("kernel"));
  ASSERT_VECTOR_LENGTH(kernel, 2);

  DType acc_dtype = DType_FP32;
  // AvgPool has acc_type, MaxPool does not
  if (op.hasAttr("acc_type")) {
    auto acc_type = op.getAttr("acc_type").cast<mlir::TypeAttr>().getValue();
    acc_dtype = Type2AccDType(acc_type);
  }

  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? input_zp = op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t output_zp =
      op.hasAttr("output_zp")
          ? output_zp =
                op.getAttr("output_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  TosaPoolAttribute attribute(pad, kernel, stride, input_zp, output_zp,
                              acc_dtype);

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(opcode, Attribute_PoolAttribute, &attribute,
                                    std::vector<std::string>{input_name},
                                    std::vector<std::string>{output_name});

  return tyop;
}

TosaSerializationOperator *
TosaSerializationOperatorBuilder::BuildEwiseBinaryOpFromMlirOp(
    mlir::Operation &op, Op opcode) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      opcode, Attribute_NONE, nullptr,
      std::vector<std::string>{input0_name, input1_name},
      std::vector<std::string>{output_name});

  return tyop;
}

TosaSerializationOperator *
TosaSerializationOperatorBuilder::BuildEwiseUnaryOpFromMlirOp(
    mlir::Operation &op, Op opcode) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      opcode, Attribute_NONE, nullptr, std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

TosaSerializationOperator *
TosaSerializationOperatorBuilder::BuildReductionOpFromMlirOp(
    mlir::Operation &op, Op opcode) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t axis = op.getAttr("axis").dyn_cast<mlir::IntegerAttr>().getInt();
  TosaAxisAttribute attribute(axis);

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(opcode, Attribute_AxisAttribute, &attribute,
                                    std::vector<std::string>{input_name},
                                    std::vector<std::string>{output_name});

  return tyop;
}

#define BUILD_OP_POOL2D(MLIR_OP_NAME, SCHEMA_OP_NAME)                          \
  template <>                                                                  \
  TosaSerializationOperator *                                                  \
  TosaSerializationOperatorBuilder::build<mlir::tosa::MLIR_OP_NAME##Op>(       \
      mlir::Operation & op) const {                                            \
    return BuildPoolOpFromMlirOp(op, Op_##SCHEMA_OP_NAME);                     \
  }

#define BUILD_OP_ELEMENTWISE_BINARY(MLIR_OP_NAME, SCHEMA_OP_NAME)              \
  template <>                                                                  \
  TosaSerializationOperator *                                                  \
  TosaSerializationOperatorBuilder::build<mlir::tosa::MLIR_OP_NAME##Op>(       \
      mlir::Operation & op) const {                                            \
    return BuildEwiseBinaryOpFromMlirOp(op, Op_##SCHEMA_OP_NAME);              \
  }

#define BUILD_OP_ELEMENTWISE_UNARY(MLIR_OP_NAME, SCHEMA_OP_NAME)               \
  template <>                                                                  \
  TosaSerializationOperator *                                                  \
  TosaSerializationOperatorBuilder::build<mlir::tosa::MLIR_OP_NAME##Op>(       \
      mlir::Operation & op) const {                                            \
    return BuildEwiseUnaryOpFromMlirOp(op, Op_##SCHEMA_OP_NAME);               \
  }

#define BUILD_OP_REDUCTION(MLIR_OP_NAME, SCHEMA_OP_NAME)                       \
  template <>                                                                  \
  TosaSerializationOperator *                                                  \
  TosaSerializationOperatorBuilder::build<mlir::tosa::MLIR_OP_NAME##Op>(       \
      mlir::Operation & op) const {                                            \
    return BuildReductionOpFromMlirOp(op, Op_##SCHEMA_OP_NAME);                \
  }

BUILD_OP_POOL2D(MaxPool2d, MAX_POOL2D)
BUILD_OP_POOL2D(AvgPool2d, AVG_POOL2D)

BUILD_OP_ELEMENTWISE_BINARY(Add, ADD)
BUILD_OP_ELEMENTWISE_BINARY(BitwiseAnd, BITWISE_AND)
BUILD_OP_ELEMENTWISE_BINARY(BitwiseXor, BITWISE_XOR)
BUILD_OP_ELEMENTWISE_BINARY(BitwiseOr, BITWISE_OR)
BUILD_OP_ELEMENTWISE_BINARY(IntDiv, INTDIV)
BUILD_OP_ELEMENTWISE_BINARY(LogicalAnd, LOGICAL_AND)
BUILD_OP_ELEMENTWISE_BINARY(LogicalLeftShift, LOGICAL_LEFT_SHIFT)
BUILD_OP_ELEMENTWISE_BINARY(LogicalRightShift, LOGICAL_RIGHT_SHIFT)
BUILD_OP_ELEMENTWISE_BINARY(LogicalOr, LOGICAL_OR)
BUILD_OP_ELEMENTWISE_BINARY(LogicalXor, LOGICAL_XOR)
BUILD_OP_ELEMENTWISE_BINARY(Maximum, MAXIMUM)
BUILD_OP_ELEMENTWISE_BINARY(Minimum, MINIMUM)
BUILD_OP_ELEMENTWISE_BINARY(Pow, POW)
BUILD_OP_ELEMENTWISE_BINARY(Sub, SUB)

BUILD_OP_ELEMENTWISE_UNARY(Abs, ABS)
BUILD_OP_ELEMENTWISE_UNARY(BitwiseNot, BITWISE_NOT)
BUILD_OP_ELEMENTWISE_UNARY(Ceil, CEIL)
BUILD_OP_ELEMENTWISE_UNARY(Clz, CLZ)
BUILD_OP_ELEMENTWISE_UNARY(Cos, COS)
BUILD_OP_ELEMENTWISE_UNARY(Exp, EXP)
BUILD_OP_ELEMENTWISE_UNARY(Floor, FLOOR)
BUILD_OP_ELEMENTWISE_UNARY(Log, LOG)
BUILD_OP_ELEMENTWISE_UNARY(LogicalNot, LOGICAL_NOT)
BUILD_OP_ELEMENTWISE_UNARY(Reciprocal, RECIPROCAL)
BUILD_OP_ELEMENTWISE_UNARY(Rsqrt, RSQRT)
BUILD_OP_ELEMENTWISE_UNARY(Sin, SIN)

BUILD_OP_REDUCTION(ReduceAny, REDUCE_ANY)
BUILD_OP_REDUCTION(ReduceAll, REDUCE_ALL)
BUILD_OP_REDUCTION(ReduceMax, REDUCE_MAX)
BUILD_OP_REDUCTION(ReduceMin, REDUCE_MIN)
BUILD_OP_REDUCTION(ReduceProd, REDUCE_PRODUCT)
BUILD_OP_REDUCTION(ReduceSum, REDUCE_SUM)

BUILD_OP_ELEMENTWISE_BINARY(Equal, EQUAL)
BUILD_OP_ELEMENTWISE_BINARY(Greater, GREATER)
BUILD_OP_ELEMENTWISE_BINARY(GreaterEqual, GREATER_EQUAL)

BUILD_OP_ELEMENTWISE_UNARY(Erf, ERF)
BUILD_OP_ELEMENTWISE_UNARY(Sigmoid, SIGMOID)
BUILD_OP_ELEMENTWISE_UNARY(Tanh, TANH)
BUILD_OP_ELEMENTWISE_UNARY(Identity, IDENTITY)
BUILD_OP_ELEMENTWISE_UNARY(Cast, CAST)

BUILD_OP_ELEMENTWISE_BINARY(AddShape, ADD_SHAPE)
BUILD_OP_ELEMENTWISE_BINARY(SubShape, SUB_SHAPE)
BUILD_OP_ELEMENTWISE_BINARY(MulShape, MUL_SHAPE)
BUILD_OP_ELEMENTWISE_BINARY(DivShape, DIV_SHAPE)

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ConstShapeOp>(
    mlir::Operation &op) const {
  std::string output_name = GetTensorName(op.getResult(0));
  TosaSerializationTensor *ts =
      block_builder->GetBlock()->GetTensorByName(output_name);
  if (!ts) {
    op.emitOpError(
        "ERROR: serialization tensor must be built before building operator");
    return nullptr;
  }

  // Update tensor.data array with Const value attribute
  mlir::Attribute value_attr = op.getAttr("value");
  if (!value_attr) {
    op.emitOpError("ERROR: tosa.const_shape doesn't have value");
    return nullptr;
  }
  assert(ts->GetDtype() == DType::DType_SHAPE);
  std::vector<uint8_t> u8_data;

  std::vector<int64_t> data;
  auto dense_attr = op.getAttr(llvm::StringRef("value"))
                        .dyn_cast<mlir::DenseIntElementsAttr>();
  if (!dense_attr) {
    op.emitOpError("Unknown const attribute");
    return nullptr;
  }

  for (auto valueIt : dense_attr.getValues<mlir::APInt>()) {
    int64_t val = valueIt.getSExtValue();
    data.push_back(val);
  }

  TosaSerializationHandler::ConvertI64toU8(data, u8_data);

  ts->SetData(u8_data);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONST_SHAPE, Attribute_NONE, nullptr, std::vector<std::string>{},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ConstOp>(
    mlir::Operation &op) const {
  std::string output_name = GetTensorName(op.getResult(0));
  TosaSerializationTensor *ts =
      block_builder->GetBlock()->GetTensorByName(output_name);
  if (!ts) {
    op.emitOpError(
        "ERROR: serialization tensor must be built before building operator");
    return nullptr;
  }

  // Update tensor.data array with Const value attribute
  mlir::Attribute value_attr = op.getAttr("value");
  if (!value_attr) {
    op.emitOpError("ERROR: tosa.const doesn't have value");
    return nullptr;
  }
  std::vector<uint8_t> u8_data;
  mlir::Attribute attr = op.getAttr(llvm::StringRef("value"));
  mlir::Type element_type =
      llvm::cast<mlir::ShapedType>(op.getResult(0).getType()).getElementType();

  if (GetDataFromAttribute(op, attr, element_type, u8_data).failed()) {
    op.emitOpError("ERROR: GetDataFromAttribute() fails when building value of "
                   "const tensor");
    return nullptr;
  }

  ts->SetData(u8_data);
  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONST, Attribute_NONE, nullptr, std::vector<std::string>{},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::Conv2DOp>(
    mlir::Operation &op) const {
  auto pad = getDenseI64ArrayAttr<int>(op.getAttr("pad"));
  ASSERT_VECTOR_LENGTH(pad, 4);

  auto stride = getDenseI64ArrayAttr<int>(op.getAttr("stride"));
  ASSERT_VECTOR_LENGTH(stride, 2);

  auto dilation = getDenseI64ArrayAttr<int>(op.getAttr("dilation"));
  ASSERT_VECTOR_LENGTH(dilation, 2);

  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t weight_zp =
      op.hasAttr("weight_zp")
          ? op.getAttr("weight_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  auto acc_type = op.getAttr("acc_type").cast<mlir::TypeAttr>().getValue();
  auto acc_dtype = Type2AccDType(acc_type);

  TosaConvAttribute attribute(pad, stride, dilation, input_zp, weight_zp,
                              local_bound, acc_dtype);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONV2D, Attribute_ConvAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::Conv3DOp>(
    mlir::Operation &op) const {
  auto pad = getDenseI64ArrayAttr<int>(op.getAttr("pad"));
  ASSERT_VECTOR_LENGTH(pad, 6);

  auto stride = getDenseI64ArrayAttr<int>(op.getAttr("stride"));
  ASSERT_VECTOR_LENGTH(stride, 3);

  auto dilation = getDenseI64ArrayAttr<int>(op.getAttr("dilation"));
  ASSERT_VECTOR_LENGTH(dilation, 3);

  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t weight_zp =
      op.hasAttr("weight_zp")
          ? op.getAttr("weight_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  auto acc_type = op.getAttr("acc_type").cast<mlir::TypeAttr>().getValue();
  auto acc_dtype = Type2AccDType(acc_type);

  TosaConvAttribute attribute(pad, stride, dilation, input_zp, weight_zp,
                              local_bound, acc_dtype);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONV3D, Attribute_ConvAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::DepthwiseConv2DOp>(
    mlir::Operation &op) const {
  auto pad = getDenseI64ArrayAttr<int>(op.getAttr("pad"));
  ASSERT_VECTOR_LENGTH(pad, 4);

  auto stride = getDenseI64ArrayAttr<int>(op.getAttr("stride"));
  ASSERT_VECTOR_LENGTH(stride, 2);

  auto dilation = getDenseI64ArrayAttr<int>(op.getAttr("dilation"));
  ASSERT_VECTOR_LENGTH(dilation, 2);

  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t weight_zp =
      op.hasAttr("weight_zp")
          ? op.getAttr("weight_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  auto acc_type = op.getAttr("acc_type").cast<mlir::TypeAttr>().getValue();
  auto acc_dtype = Type2AccDType(acc_type);

  TosaConvAttribute attribute(pad, stride, dilation, input_zp, weight_zp,
                              local_bound, acc_dtype);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_DEPTHWISE_CONV2D, Attribute_ConvAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::TransposeConv2DOp>(
    mlir::Operation &op) const {
  auto out_pad = getDenseI64ArrayAttr<int>(op.getAttr("out_pad"));
  ASSERT_VECTOR_LENGTH(out_pad, 4);

  auto stride = getDenseI64ArrayAttr<int>(op.getAttr("stride"));
  ASSERT_VECTOR_LENGTH(stride, 2);

  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t weight_zp =
      op.hasAttr("weight_zp")
          ? op.getAttr("weight_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  mlir::RankedTensorType tensor =
      op.getOperand(0).getType().cast<mlir::RankedTensorType>();

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  auto acc_type = op.getAttr("acc_type").cast<mlir::TypeAttr>().getValue();
  auto acc_dtype = Type2AccDType(acc_type);

  TosaTransposeConvAttribute attribute(out_pad, stride, input_zp, weight_zp,
                                       local_bound, acc_dtype);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_TRANSPOSE_CONV2D, Attribute_TransposeConvAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::FullyConnectedOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input_zp =
      op.hasAttr("input_zp")
          ? op.getAttr("input_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t weight_zp =
      op.hasAttr("weight_zp")
          ? op.getAttr("weight_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  TosaFullyConnectedAttribute attribute(input_zp, weight_zp);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_FULLY_CONNECTED, Attribute_FullyConnectedAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::MatMulOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t A_zp = op.hasAttr("a_zp")
                     ? op.getAttr("a_zp").cast<mlir::IntegerAttr>().getInt()
                     : 0;
  int32_t B_zp = op.hasAttr("b_zp")
                     ? op.getAttr("b_zp").cast<mlir::IntegerAttr>().getInt()
                     : 0;

  TosaMatMulAttribute attribute(A_zp, B_zp);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_MATMUL, Attribute_MatMulAttribute, &attribute,
      std::vector<std::string>{input0_name, input1_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::SelectOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_SELECT, Attribute_NONE, nullptr,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ClampOp>(
    mlir::Operation &op) const {
  auto min_val_attr = op.getAttr("min_val");
  auto max_val_attr = op.getAttr("max_val");

  mlir::Type input_element_type =
      llvm::cast<mlir::ShapedType>(op.getOperand(0).getType()).getElementType();
  if (auto quantType = llvm::dyn_cast<mlir::quant::UniformQuantizedType>(
          input_element_type)) {
    input_element_type = quantType.getStorageType();
  }

  std::vector<uint8_t> min_val, max_val;
  float min_fp, max_fp;
  int64_t min_int, max_int;

  if (input_element_type.isa<mlir::FloatType>()) {
    min_fp =
        mlir::cast<mlir::FloatAttr>(min_val_attr).getValue().convertToFloat();
    max_fp =
        mlir::cast<mlir::FloatAttr>(max_val_attr).getValue().convertToFloat();
    min_int = max_int = 0;
  } else {
    assert(input_element_type.isa<mlir::IntegerType>());
    min_int = mlir::cast<mlir::IntegerAttr>(min_val_attr).getInt();
    max_int = mlir::cast<mlir::IntegerAttr>(max_val_attr).getInt();
    min_fp = max_fp = 0.f;
  }

  if (GetU8DataFromIntOrFloatValue(min_int, min_fp, input_element_type, min_val)
          .failed()) {
    op.emitOpError("Failed to serialize min value");
    return nullptr;
  }

  if (GetU8DataFromIntOrFloatValue(max_int, max_fp, input_element_type, max_val)
          .failed()) {
    op.emitOpError("Failed to serialize max value");
    return nullptr;
  }

  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaClampAttribute attribute(min_val, max_val);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CLAMP, Attribute_ClampAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ArgMaxOp>(
    mlir::Operation &op) const {
  int32_t axis = op.getAttr("axis").dyn_cast<mlir::IntegerAttr>().getInt();

  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaAxisAttribute attribute(axis);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_ARGMAX, Attribute_AxisAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ConcatOp>(
    mlir::Operation &op) const {
  int32_t axis = op.getAttr("axis").dyn_cast<mlir::IntegerAttr>().getInt();

  std::vector<std::string> inputs;
  for (uint32_t i = 0; i < op.getNumOperands(); i++) {
    std::string input_name = GetTensorName(op.getOperand(i));
    inputs.push_back(input_name);
  }

  std::string output_name = GetTensorName(op.getResult(0));

  TosaAxisAttribute attribute(axis);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONCAT, Attribute_AxisAttribute, &attribute, inputs,
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ConcatShapeOp>(
    mlir::Operation &op) const {
  std::vector<std::string> inputs;
  for (uint32_t i = 0; i < op.getNumOperands(); i++) {
    std::string input_name = GetTensorName(op.getOperand(i));
    inputs.push_back(input_name);
  }

  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CONCAT_SHAPE, Attribute_NONE, nullptr, inputs,
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::NegateOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t input1_zp =
      op.hasAttr("input1_zp")
          ? op.getAttr("input1_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;
  int32_t output_zp =
      op.hasAttr("output_zp")
          ? op.getAttr("output_zp").cast<mlir::IntegerAttr>().getInt()
          : 0;

  TosaNegateAttribute attribute(input1_zp, output_zp);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_NEGATE, Attribute_NegateAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});
  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ReshapeOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string shape_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_RESHAPE, Attribute_NONE, nullptr,
      std::vector<std::string>{input_name, shape_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::PadOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string padding_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));
  auto pad_op = llvm::cast<mlir::tosa::PadOp>(op);

  auto input_zp_attr = pad_op.getInputZpAttr();
  // pad_const includes the zero point if the tensor uses a zero point.
  int32_t pad_const_int = input_zp_attr ? input_zp_attr.getInt() : 0;
  float pad_const_fp = 0.f;

  if (auto tensor = pad_op.getPadConst()) {
    // Match pad_const tensor as compile-time constant attribute if present.
    mlir::DenseElementsAttr attr;
    if (!matchPattern(tensor, m_Constant(&attr)))
      return nullptr;

    assert(attr.getNumElements() == 1);
    auto elementTy = attr.getElementType();

    if (elementTy.isa<mlir::IntegerType>()) {
      pad_const_int = (attr.getValues<mlir::APInt>()[0]).getSExtValue();
    } else if (elementTy.isa<mlir::FloatType>()) {
      pad_const_fp = (attr.getValues<mlir::APFloat>()[0]).convertToFloat();
    } else {
      op.emitOpError("Unknown const attribute");
      return nullptr;
    }
  }

  std::vector<uint8_t> pad_const;
  mlir::Type input_element_type =
      llvm::cast<mlir::ShapedType>(op.getOperand(0).getType()).getElementType();

  if (GetU8DataFromIntOrFloatValue(pad_const_int, pad_const_fp,
                                   input_element_type, pad_const)
          .failed()) {
    op.emitOpError("Failed to serialize pad_const value");
    return nullptr;
  }

  TosaPadAttribute attribute(pad_const);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_PAD, Attribute_PadAttribute, &attribute,
      std::vector<std::string>{input_name, padding_name},
      std::vector<std::string>{output_name});
  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::DimOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t axis = op.getAttr("axis").dyn_cast<mlir::IntegerAttr>().getInt();
  TosaAxisAttribute attribute(axis);

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(Op_DIM, Attribute_AxisAttribute, &attribute,
                                    std::vector<std::string>{input_name},
                                    std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::TransposeOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  // Match perm tensor as compile-time constant attribute
  mlir::ElementsAttr perm_elems;
  if (!matchPattern(op.getOperand(1), m_Constant(&perm_elems)))
    return nullptr;

  std::vector<int> perm;
  for (auto value : perm_elems.getValues<mlir::IntegerAttr>()) {
    perm.push_back(value.getInt());
  }

  TosaTransposeAttribute attribute(perm);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_TRANSPOSE, Attribute_TransposeAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::SliceOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string start_name = GetTensorName(op.getOperand(1));
  std::string size_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_SLICE, Attribute_NONE, nullptr,
      std::vector<std::string>{input_name, start_name, size_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::TileOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_TILE, Attribute_NONE, nullptr,
      std::vector<std::string>{input0_name, input1_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::GatherOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_GATHER, Attribute_NONE, nullptr,
      std::vector<std::string>{input0_name, input1_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ScatterOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string input2_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_SCATTER, Attribute_NONE, nullptr,
      std::vector<std::string>{input0_name, input1_name, input2_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ResizeOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string scale_name = GetTensorName(op.getOperand(1));
  std::string offset_name = GetTensorName(op.getOperand(2));
  std::string border_name = GetTensorName(op.getOperand(3));
  std::string output_name = GetTensorName(op.getResult(0));

  auto mode_str =
      op.getAttr("mode").dyn_cast<mlir::StringAttr>().getValue().str();
  ResizeMode mode = ResizeModeStr2Enum(mode_str);

  TosaResizeAttribute attribute({}, {}, {}, mode);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_RESIZE, Attribute_ResizeAttribute, &attribute,
      std::vector<std::string>{input_name, scale_name, offset_name,
                               border_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ReverseOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  int32_t axis = op.getAttr("axis").dyn_cast<mlir::IntegerAttr>().getInt();

  TosaAxisAttribute attribute(axis);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_REVERSE, Attribute_AxisAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::MulOp>(
    mlir::Operation &op) const {

  mlir::tosa::MulOp mul_op = mlir::cast<mlir::tosa::MulOp>(op);
  std::string input0_name = GetTensorName(mul_op.getInput1());
  std::string input1_name = GetTensorName(mul_op.getInput2());
  std::string output_name = GetTensorName(mul_op.getOutput());
  std::string shift_name = GetTensorName(mul_op.getShift());

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_MUL, Attribute_NONE, nullptr, {input0_name, input1_name, shift_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::ArithmeticRightShiftOp>(
    mlir::Operation &op) const {
  std::string input0_name = GetTensorName(op.getOperand(0));
  std::string input1_name = GetTensorName(op.getOperand(1));
  std::string output_name = GetTensorName(op.getResult(0));

  bool round = op.getAttr("round").dyn_cast<mlir::BoolAttr>().getValue();

  TosaArithmeticRightShiftAttribute attribute(round);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_ARITHMETIC_RIGHT_SHIFT, Attribute_ArithmeticRightShiftAttribute,
      &attribute, std::vector<std::string>{input0_name, input1_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::TableOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  // Match table tensor as compile-time constant attribute
  mlir::ElementsAttr table_elems;
  if (!matchPattern(op.getOperand(1), m_Constant(&table_elems)))
    return nullptr;

  std::vector<int16_t> table;
  for (auto value : table_elems.getValues<mlir::IntegerAttr>()) {
    table.push_back(value.getInt());
  }

  TosaTableAttribute attribute(table);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_TABLE, Attribute_TableAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::RescaleOp>(
    mlir::Operation &op) const {
  int32_t input_zp =
      op.getAttr("input_zp").dyn_cast<mlir::IntegerAttr>().getInt();
  int32_t output_zp =
      op.getAttr("output_zp").dyn_cast<mlir::IntegerAttr>().getInt();
  bool scale32 = op.getAttr("scale32").dyn_cast<mlir::BoolAttr>().getValue();
  bool double_round =
      op.getAttr("double_round").dyn_cast<mlir::BoolAttr>().getValue();
  bool per_channel =
      op.getAttr("per_channel").dyn_cast<mlir::BoolAttr>().getValue();

  bool input_unsigned =
      op.getAttr("input_unsigned").dyn_cast<mlir::BoolAttr>().getValue();
  bool output_unsigned =
      op.getAttr("output_unsigned").dyn_cast<mlir::BoolAttr>().getValue();

  auto input = op.getOperand(0);
  auto input_ty = input.getType().cast<mlir::RankedTensorType>();
  auto output = op.getResult(0);
  auto output_ty = output.getType().cast<mlir::RankedTensorType>();

  std::string input_name = GetTensorName(input);
  std::string multiplier_name = GetTensorName(op.getOperand(1));
  std::string shift_name = GetTensorName(op.getOperand(2));
  std::string output_name = GetTensorName(output);

  TosaRescaleAttribute attribute(input_zp, output_zp, scale32, double_round,
                                 per_channel, input_unsigned, output_unsigned);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_RESCALE, Attribute_RescaleAttribute, &attribute,
      std::vector<std::string>{input_name, multiplier_name, shift_name},
      std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::CustomOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetTensorName(op.getResult(0));

  const std::string implementation_attrs = op.getAttr("implementation_attrs")
                                               .cast<mlir::StringAttr>()
                                               .getValue()
                                               .str();

  std::vector<uint8_t> attrs_data(implementation_attrs.size());
  memcpy(attrs_data.data(), implementation_attrs.data(), attrs_data.size());
  TosaCustomAttribute attribute(
      op.getAttr("operator_name").cast<mlir::StringAttr>().getValue().str(),
      op.getAttr("domain_name").cast<mlir::StringAttr>().getValue().str(),
      attrs_data);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_CUSTOM, Attribute_CustomAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_name});

  return tyop;
}

namespace {

// serialize a region and all its blocks, and return region's return values
TosaSerializationRegion *
BuildRegion(mlir::Region &region, const std::string region_name,
            const bool isolated_from_above,
            TosaSerializationRegionBuilder *curr_region_builder,
            TosaSerializationHandler *tsh,
            std::vector<mlir::Value> &return_values, bool is_top = false) {
  TosaSerializationRegion *ser_region =
      new TosaSerializationRegion(region_name, {});
  assert(ser_region);
  tsh->GetRegions().push_back(ser_region);

  TosaSerializationRegionBuilder *parent_value_scope =
      isolated_from_above ? nullptr : curr_region_builder;

  TosaSerializationRegionBuilder region_builder(ser_region, &region,
                                                parent_value_scope, tsh);
  if (region_builder.BuildAllBlocksInRegion(is_top, return_values).failed()) {
    return nullptr;
  }
  return ser_region;
}

static int input_tensor_index = 0;
static int intermediate_tensor_index = 0;
static int output_tensor_index = 0;

} // namespace

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::IfOp>(
    mlir::Operation &op) const {
  const std::string op_name = op.getName().getStringRef().str();
  const bool isolated_from_above =
      op.hasTrait<mlir::OpTrait::IsIsolatedFromAbove>();
  auto curr_region_builder = GetRegionBuilder();
  std::vector<std::string> input_names, output_names;
  std::vector<mlir::Value> then_yields, else_yields;
  auto tsh = GetTsh();

  mlir::Region &then_region = op.getRegion(0);
  mlir::Region &else_region = op.getRegion(1);

  const std::string then_region_name = op_name + "_then_region";
  TosaSerializationRegion *ser_then_region =
      BuildRegion(then_region, then_region_name, isolated_from_above,
                  curr_region_builder, tsh, then_yields);
  if (!ser_then_region) {
    return nullptr;
  }
  if (then_yields.size() != op.getNumResults()) {
    op.emitOpError("BuildOpCondIf: then_region yield.size() doesn't match "
                   "cond_if's output size");
    return nullptr;
  }

  const std::string else_region_name = op_name + "_else_region";
  TosaSerializationRegion *ser_else_region =
      BuildRegion(else_region, else_region_name, isolated_from_above,
                  curr_region_builder, tsh, else_yields);
  if (!ser_else_region) {
    return nullptr;
  }
  if (else_yields.size() != op.getNumResults()) {
    op.emitOpError("BuildOpCondIf: else_region yield.size() doesn't match "
                   "cond_if's output size");
    return nullptr;
  }

  TosaCondIfAttribute attribute(then_region_name, else_region_name);

  for (size_t i = 0; i < op.getNumOperands(); i++) {
    std::string input_name = GetTensorName(op.getOperand(i));
    input_names.push_back(input_name);
  }

  for (size_t i = 0; i < op.getNumResults(); i++) {
    std::string output_name = GetTensorName(op.getResult(i));
    output_names.push_back(output_name);
  }

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(Op_COND_IF, Attribute_CondIfAttribute,
                                    &attribute, input_names, output_names);

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::WhileOp>(
    mlir::Operation &op) const {
  const std::string op_name = op.getName().getStringRef().str();
  const bool isolated_from_above =
      op.hasTrait<mlir::OpTrait::IsIsolatedFromAbove>();
  auto curr_region_builder = GetRegionBuilder();
  std::vector<std::string> input_names, output_names;
  auto tsh = GetTsh();

  mlir::Region &cond_region = op.getRegion(0);
  mlir::Region &body_region = op.getRegion(1);
  std::vector<mlir::Value> cond_yields, body_yields;

  const std::string cond_region_name = op_name + "_cond_region";
  TosaSerializationRegion *ser_cond_region =
      BuildRegion(cond_region, cond_region_name, isolated_from_above,
                  curr_region_builder, tsh, cond_yields);
  if (!ser_cond_region) {
    return nullptr;
  }
  if (cond_yields.size() != 1) {
    op.emitOpError("BuildOpWhileLoop: cond_region yield.size() is not 1");
    return nullptr;
  }

  const std::string body_region_name = op_name + "_body_region";
  TosaSerializationRegion *ser_body_region =
      BuildRegion(body_region, body_region_name, isolated_from_above,
                  curr_region_builder, tsh, body_yields);
  if (!ser_body_region) {
    return nullptr;
  }
  if (body_yields.size() != op.getNumResults()) {
    op.emitOpError("BuildOpWhileLoop: body_region yield.size() doesn't "
                   "match while_loop's output size");
    return nullptr;
  }

  TosaWhileLoopAttribute attribute(cond_region_name, body_region_name);

  for (size_t i = 0; i < op.getNumOperands(); i++) {
    std::string input_name = GetTensorName(op.getOperand(i));
    input_names.push_back(input_name);
  }

  for (size_t i = 0; i < op.getNumResults(); i++) {
    std::string output_name = GetTensorName(op.getResult(i));
    output_names.push_back(output_name);
  }

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(Op_WHILE_LOOP, Attribute_WhileLoopAttribute,
                                    &attribute, input_names, output_names);

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::RFFT2dOp>(
    mlir::Operation &op) const {
  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_real_name = GetTensorName(op.getResult(0));
  std::string output_imag_name = GetTensorName(op.getResult(1));

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  TosaRFFTAttribute attribute(local_bound);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_RFFT2D, Attribute_RFFTAttribute, &attribute,
      std::vector<std::string>{input_name},
      std::vector<std::string>{output_real_name, output_imag_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::FFT2dOp>(
    mlir::Operation &op) const {

  bool inverse = op.getAttr("inverse").dyn_cast<mlir::BoolAttr>().getValue();

  bool local_bound =
      op.hasAttr("local_bound")
          ? op.getAttr("local_bound").dyn_cast<mlir::BoolAttr>().getValue()
          : false;

  std::string input_real_name = GetTensorName(op.getOperand(0));
  std::string input_imag_name = GetTensorName(op.getOperand(1));
  std::string output_real_name = GetTensorName(op.getResult(0));
  std::string output_imag_name = GetTensorName(op.getResult(1));

  TosaFFTAttribute attribute(inverse, local_bound);

  TosaSerializationOperator *tyop = new TosaSerializationOperator(
      Op_FFT2D, Attribute_FFTAttribute, &attribute,
      std::vector<std::string>{input_real_name, input_imag_name},
      std::vector<std::string>{output_real_name, output_imag_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::VariableReadOp>(
    mlir::Operation &op) const {

  std::string input_name = GetVariableTensorName(&op);
  std::string output_name = GetTensorName(op.getResult(0));

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(Op_IDENTITY, Attribute_NONE, nullptr,
                                    std::vector<std::string>{input_name},
                                    std::vector<std::string>{output_name});

  return tyop;
}

template <>
TosaSerializationOperator *
TosaSerializationOperatorBuilder::build<mlir::tosa::VariableWriteOp>(
    mlir::Operation &op) const {

  std::string input_name = GetTensorName(op.getOperand(0));
  std::string output_name = GetVariableTensorName(&op);

  TosaSerializationOperator *tyop =
      new TosaSerializationOperator(Op_IDENTITY, Attribute_NONE, nullptr,
                                    std::vector<std::string>{input_name},
                                    std::vector<std::string>{output_name});

  return tyop;
}

/* End translating TOSA operator */
mlir::LogicalResult TosaSerializationRegionBuilder::BuildAllBlocksInRegion(
    bool is_top, std::vector<mlir::Value> &return_values) {
  std::string region_name = ser_region->GetName();
  int block_index = 0;
  for (auto &block : this->region->getBlocks()) {
    // must name first block of top region "main"
    const std::string block_name =
        (is_top && block_index == 0)
            ? "main"
            : (region_name + "_bb" + std::to_string(block_index++));
    TosaSerializationBasicBlock *ser_block = new TosaSerializationBasicBlock(
        block_name, region_name, std::vector<TosaSerializationOperator *>(),
        std::vector<TosaSerializationTensor *>(), std::vector<std::string>(),
        std::vector<std::string>());

    // build the block
    TosaSerializationBlockBuilder block_builder(ser_block, this, &block);
    // Region Builders need access to block builders
    block_builders.push_back(&block_builder);

    if (block_builder.BuildAllOpsInBlock(return_values).failed()) {
      return mlir::failure();
    }

    if (return_values.empty()) {
      llvm::errs() << "BWarning: graph doesn't have return values\n";
    }

    // Add serialized block to serialized region
    ser_region->GetBlocks().push_back(ser_block);
  }

  return mlir::success();
}

mlir::LogicalResult TosaSerializationBlockBuilder::BuildAllOpsInBlock(
    std::vector<mlir::Value> &return_values) {
  TosaSerializationOperator *ser_operator = nullptr;
  TosaSerializationTensor *ser_tensor = nullptr;
  size_t num_blocks_in_region = 0;
  TosaSerializationOperatorBuilder op_builder(this);

  // Specify block input tensor name
  for (auto args : block->getArguments()) {
    std::string block_input_name =
        "TosaInput_" + std::to_string(input_tensor_index++);
    ser_block->GetInputs().push_back(block_input_name);
    tensor_map[args] = block_input_name;
    input_tensor_map[args] = block_input_name;
  }

  // Build tensor_map
  for (auto &op : block->getOperations()) {
    if (llvm::isa<mlir::tosa::VariableOp>(op)) {
      RegisterVariableOp(op);
    } else if (!(llvm::isa<mlir::tosa::YieldOp>(op) ||
                 llvm::isa<mlir::func::ReturnOp>(op) ||
                 llvm::isa<mlir::tensor::CastOp>(op))) {
      for (uint32_t i = 0; i < op.getNumResults(); i++) {
        std::string intermediate_tensor_name =
            "layer_" + std::to_string(intermediate_tensor_index++);
        tensor_map[op.getResult(i)] = intermediate_tensor_name;
      }
    } else {
      if (llvm::isa<mlir::tensor::CastOp>(op))
        continue;
      // Override return tensor name
      for (auto val : op.getOperands()) {
        // Workaround to skip mlir::tensor::CastOp before return
        mlir::Operation *val_defining_op = val.getDefiningOp();
        if (val_defining_op) {
          if (llvm::isa<mlir::tensor::CastOp>(*val_defining_op))
            val = val_defining_op->getOperand(0);
        }

        // Sanity check. This mlir::Value should be built in map since graph
        // is DAG
        if (tensor_map.find(val) == tensor_map.end()) {
          llvm::errs() << "ERROR: Can't find built mlir::Value key.\n";
          return mlir::failure();
        }

        // If returned value is block input, short-circuit the tensor name
        // Otherwise, build a new output name and override the origin tensor
        // name
        if (input_tensor_map.find(val) != input_tensor_map.end()) {
          ser_block->GetOutputs().push_back(input_tensor_map[val]);
          return_values.push_back(val);
        } else {
          std::string output_name =
              "TosaOutput_" + std::to_string(output_tensor_index++);
          tensor_map[val] = output_name;
          ser_block->GetOutputs().push_back(output_name);
          return_values.push_back(val);
        }
      }
    }
  }

  // Build variable tensor
  for (auto pair : variable_tensor_op_map) {
    mlir::Operation *op = pair.second;
    mlir::Value val = op->getResult(0);
    mlir::RankedTensorType tensor_type = op->getAttr("type")
                                             .cast<mlir::TypeAttr>()
                                             .getValue()
                                             .cast<mlir::RankedTensorType>();

    std::string variable_mlir_name =
        op->getAttr("name").cast<mlir::StringAttr>().getValue().str();

    ser_tensor = BuildTosaSerializationVariableTensor(
        tensor_type /* tensor_type */, pair.first /* flatbuffer name */,
        variable_mlir_name);
    if (!ser_tensor) {
      llvm::errs() << "ERROR: Failed to build TosaSerializationTensor\n";
      return mlir::failure();
    }
    // Initialize if "initial_value" attribute exists. If not, set data to all
    // zeros
    mlir::Attribute initial_value = op->getAttr("initial_value");
    std::vector<uint8_t> u8_data;
    if (initial_value) {
      if (initial_value.isa<mlir::DenseElementsAttr>()) {
        if (op_builder
                .GetDataFromAttribute(*op, initial_value,
                                      tensor_type.getElementType(), u8_data)
                .failed()) {
          llvm::errs() << "ERROR: GetDataFromAttribute() fails when building "
                          "initial_value of variable tensor\n";
          return mlir::failure();
        }
      } else {
        llvm::errs() << "ERROR: Unknown initial_value attribute type\n";
        return mlir::failure();
      }
    } else {
      TosaSerializationHandler::ForceAlignTensorData(u8_data);
    }
    ser_tensor->SetData(u8_data);
    ser_block->GetTensors().push_back(ser_tensor);
  }

  // Build tensor

  // The tensor_map is sorted by hashed mlir::Value types.
  // For serialization, sort tensors alphabetically by name for a
  // deterministic and human-friendly ordering.
  std::map<std::string, mlir::Value> tensor_name_sort;
  for (auto pair : tensor_map)
    tensor_name_sort[pair.second] = pair.first;

  for (auto pair : tensor_name_sort) {
    mlir::RankedTensorType tensor_type =
        pair.second.getType().cast<mlir::RankedTensorType>();
    ser_tensor = BuildTosaSerializationTensor(pair.second /* val */,
                                              pair.first /* name */);
    if (!ser_tensor) {
      llvm::errs() << "ERROR: Failed to build TosaSerializationTensor\n";
      return mlir::failure();
    }
    ser_block->GetTensors().push_back(ser_tensor);
  }

  // Build operator
  for (auto &op : block->getOperations()) {
    if (llvm::isa<mlir::tosa::YieldOp>(op) ||
        llvm::isa<mlir::func::ReturnOp>(op) ||
        llvm::isa<mlir::tensor::CastOp>(op) ||
        llvm::isa<mlir::tosa::VariableOp>(op))
      continue;
    ser_operator = BuildTosaSerializationOperator(op_builder, op);
    if (!ser_operator) {
      llvm::errs() << "ERROR: Failed to build TosaSerializationOperator\n";
      return mlir::failure();
    }
    ser_block->GetOperators().push_back(ser_operator);
  }
  return mlir::success();
}

TosaSerializationOperator *
TosaSerializationBlockBuilder::BuildTosaSerializationOperator(
    const TosaSerializationOperatorBuilder &op_builder, mlir::Operation &op) {
  TosaSerializationOperator *target_operator = nullptr;

  if (llvm::isa<mlir::tosa::VariableReadOp>(op)) {
    target_operator = op_builder.build<mlir::tosa::VariableReadOp>(op);
  } else if (llvm::isa<mlir::tosa::VariableWriteOp>(op)) {
    target_operator = op_builder.build<mlir::tosa::VariableWriteOp>(op);
  }
#define DEF_OPERATOR(MLIR_OP)                                                  \
  else if (llvm::isa<mlir::tosa::MLIR_OP##Op>(op)) {                           \
    target_operator = op_builder.build<mlir::tosa::MLIR_OP##Op>(op);           \
  }
#include "operator.def"
#undef DEF_OPERATOR
  else {
    llvm::errs() << "unsupported tosa operator " << op.getName().getStringRef()
                 << "\n";
  }

  if (!target_operator) {
    llvm::errs() << op.getName().getStringRef()
                 << " operator hasn't been translated to flatbuffer, skipped\n";
    return nullptr;
  }

  if (llvm::isa<mlir::tosa::VariableReadOp>(op) ||
      llvm::isa<mlir::tosa::VariableWriteOp>(op)) {
    return target_operator;
  }

  // Sanity check the number of inputs/outputs of TOSA dialect matches the
  // number of TOSA flatbuffer
  if (op.getNumOperands() != target_operator->GetInputTensorNames().size()) {
    llvm::errs() << "WARNING: MLIR operator has " << op.getNumOperands()
                 << " input tensors != Flatbuffer "
                    "operator has "
                 << target_operator->GetInputTensorNames().size()
                 << " input tensors\n";
  }
  if (op.getNumResults() != target_operator->GetOutputTensorNames().size()) {
    llvm::errs() << "WARNING: MLIR operator has " << op.getNumResults()
                 << " output tensors != Flatbuffer "
                    "operator has "
                 << target_operator->GetOutputTensorNames().size()
                 << " output tensors\n";
  }

  return target_operator;
}

TosaSerializationTensor *
TosaSerializationBlockBuilder::BuildTosaSerializationVariableTensor(
    mlir::RankedTensorType tensor_type, const std::string &name,
    const std::string &variable_mlir_name) {
  // If tensor already created before, use that tensor directly, create a new
  // one otherwise
  TosaSerializationTensor *ts = ser_block->GetTensorByName(name);
  if (ts) {
    return nullptr;
  }

  std::vector<int32_t> shape(tensor_type.getShape().begin(),
                             tensor_type.getShape().end());

  DType type = Type2DType(tensor_type.getElementType());

  ts = new TosaSerializationTensor(name, shape, type, std::vector<uint8_t>(),
                                   /* is_variable = */ true,
                                   /* is_unranked = */ false,
                                   variable_mlir_name);

  return ts;
}

TosaSerializationTensor *
TosaSerializationBlockBuilder::BuildTosaSerializationTensor(
    mlir::Value val, const std::string &name) {
  // If tensor already created before, use that tensor directly, create a new
  // one otherwise
  TosaSerializationTensor *ts = ser_block->GetTensorByName(name);
  if (ts) {
    return nullptr;
  }

  // handling of tosa.shape values
  if (auto shape_ty = val.getType().dyn_cast<mlir::tosa::shapeType>()) {
    auto rank = shape_ty.getRank();
    std::vector<int32_t> shape;
    if (rank > 0) {
      shape.push_back(rank);
    }
    ts = new TosaSerializationTensor(name,
                                     /* shape = */ shape,
                                     /* type = */ DType::DType_SHAPE,
                                     /* data = */ std::vector<uint8_t>());
    return ts;
  }

  auto ttype = val.getType().dyn_cast<mlir::TensorType>();
  if (!ttype) {
    llvm::errs() << "TOSA serialization, supplied value is not of TensorType\n";
    return nullptr;
  }

  const bool is_unranked = !ttype.hasRank();
  std::vector<int32_t> shape;
  if (!is_unranked) {
    auto shaped = val.getType().dyn_cast<mlir::ShapedType>();
    assert(shaped);
    for (int idx = 0; idx < ttype.getRank(); idx++) {
      if (shaped.isDynamicDim(idx)) {
        shape.push_back(0); // size of 0 represents dynamic dimension
      } else {
        auto dim = shaped.getDimSize(idx);
        shape.push_back(dim);
      }
    }
  }

  DType type = Type2DType(ttype.getElementType());
  ts = new TosaSerializationTensor(name, shape, type, std::vector<uint8_t>(),
                                   /* variable = */ false, is_unranked);

  return ts;
}

mlir::LogicalResult translate2FlatBuffer(mlir::func::FuncOp &func,
                                         TosaSerializationHandler &tsh) {
  mlir::Region *main_region = func.getCallableRegion();
  std::vector<mlir::Value> main_returns;

  if (!main_region) {
    llvm::errs() << "Invalid MLIR: doesn't have valid \"main\" region\n";
    return mlir::failure();
  }

  if (!tsh.GetRegions().empty()) {
    llvm::errs() << "Internal Error: TosaSerializationHandler's region list "
                    "must be empty\n";
    return mlir::failure();
  }

  // reset static counters
  input_tensor_index = 0;
  intermediate_tensor_index = 0;
  output_tensor_index = 0;

  TosaSerializationRegion *ser_main_region =
      BuildRegion(*main_region, "main", /* isolated_from_above = */ true,
                  /* parent_value_scope = */ nullptr, &tsh, main_returns,
                  /* is_top = */ true);
  if (!ser_main_region) {
    return mlir::failure();
  }

  if (main_returns.empty()) {
    llvm::errs() << "Warning: graph doesn't have return values\n";
  }

  return mlir::success();
}

mlir::LogicalResult dumpTosaFlatbuffer(mlir::func::FuncOp &func) {
  tosa::TosaSerializationHandler tsh;

  std::string tosa_flatbuffer_directory_fullpath;
  if (translate2FlatBuffer(func, tsh).failed()) {
    llvm::errs() << "Fail to translate TOSA MLIR to flatbuffer\n";
    return mlir::failure();
  }

  if (tsh.SaveFileTosaFlatbuffer(tosa_flatbuffer_filename.c_str())) {
    llvm::errs() << "Fail to save flatbuffer " << tosa_flatbuffer_filename
                 << "\n";
    return mlir::failure();
  }
  return mlir::success();
}

mlir::LogicalResult dumpTosaJSON(mlir::func::FuncOp &func) {
  tosa::TosaSerializationHandler tsh;

  const char *tosa_schema = tosa_flatbuffer_schema.c_str();

  if (!tosa_schema) {
    llvm::errs() << "Flatbuffer schema not defined\n";
    return mlir::failure();
  }

  if (tsh.LoadFileSchema(tosa_schema)) {
    llvm::errs() << "Error loading tosa schema file: " << tosa_schema << "\n";
    return mlir::failure();
  }

  std::string tosa_flatbuffer_directory_fullpath;
  if (translate2FlatBuffer(func, tsh).failed()) {
    llvm::errs() << "Fail to translate TOSA MLIR to flatbuffer\n";
    return mlir::failure();
  }

  if (tsh.SaveFileJson(tosa_flatbuffer_filename.c_str())) {
    llvm::errs() << "Fail to save flatbuffer " << tosa_flatbuffer_filename
                 << "\n";
    return mlir::failure();
  }

  return mlir::success();
}

#define GEN_PASS_DEF_TOSASERIALIZATIONPASS

namespace mlir {
namespace tosa {
namespace {

class TosaSerialize : public TosaSerializationPassBase<TosaSerialize> {
public:
  void runOnOperation() final {
    auto moduleOp = getOperation();

    // iterate through each op in the moduleOp, call dumpTosaFlatbuffer if
    // that's a func.funcOp

    auto regions = moduleOp->getRegions();
    auto region_size = regions.size();

    auto region_0 = regions.begin();
    auto block_size = region_0->getBlocks().size();

    auto block_0 = region_0->getBlocks().begin();

    auto op_size = block_0->getOperations().size();

    for (auto it = block_0->getOperations().begin();
         it != block_0->getOperations().end(); ++it) {
      // read variableOps that are declared outside of functionOps
      if (llvm::isa<mlir::tosa::VariableOp>(*it)) {
        RegisterVariableOp(*it);
      } else if (llvm::isa<mlir::func::FuncOp>(*it)) {
        auto funcOp = dyn_cast<mlir::func::FuncOp>((*it));
        if (dumpTosaFlatbuffer(funcOp).failed()) {
          llvm::errs() << "Failed to generate TOSA flatbuffer...\n";
          return signalPassFailure();
        }
      }
    }
  }
};

class TosaSerializeJSON
    : public TosaSerializationJSONPassBase<TosaSerializeJSON> {
public:
  void runOnOperation() final {
    auto function = getOperation();

    if (dumpTosaJSON(function).failed()) {
      llvm::errs() << "Failed to generate TOSA JSON...\n";
      return signalPassFailure();
    }
  }
};

} // anonymous namespace

// Creates an instance of the TOSA flatbuffer generation pass
std::unique_ptr<OperationPass<ModuleOp>> createTosaSerializePass() {
  return std::make_unique<TosaSerialize>();
}

std::unique_ptr<Pass> createTosaSerializeJSONPass() {
  return std::make_unique<TosaSerializeJSON>();
}

static PassRegistration<TosaSerialize> pass([] {
  return createTosaSerializePass();
});

static PassRegistration<TosaSerializeJSON> passJSON([] {
  return createTosaSerializeJSONPass();
});

} // namespace tosa
} // namespace mlir