pliron-llvm 0.16.0

LLVM dialect for pliron
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
//! Translate from pliron's LLVM dialect to LLVM-IR

use std::collections::hash_map;

use llvm_sys::{
    LLVMAtomicOrdering, LLVMAtomicRMWBinOp, LLVMInlineAsmDialect, LLVMIntPredicate, LLVMLinkage,
    LLVMRealPredicate,
};
use pliron::{
    attribute::{Attribute, attr_cast},
    basic_block::BasicBlock,
    builtin::{
        attr_interfaces::FloatAttr,
        attributes::{FPDoubleAttr, FPSingleAttr, IntegerAttr, StringAttr},
        op_interfaces::{
            AtMostOneRegionInterface, BranchOpInterface, CallOpCallable, CallOpInterface,
            OneOpdInterface, OneResultInterface, SingleBlockRegionInterface, SymbolOpInterface,
        },
        ops::{ConstantOp, ModuleOp},
        type_interfaces::FunctionTypeInterface,
        types::{FP16Type, FP32Type, FP64Type, IntegerType},
    },
    common_traits::Named,
    context::{Context, Ptr},
    derive::{attr_interface, attr_interface_impl},
    graph::traversals::region::topological_order,
    identifier::Identifier,
    input_err, input_err_noloc, input_error, input_error_noloc,
    linked_list::{ContainsLinkedList, LinkedList},
    location::Located,
    op::{Op, op_cast},
    operation::Operation,
    printable::Printable,
    result::Result,
    r#type::{Type, TypeHandle, Typed, type_cast},
    utils::apint::APInt,
    value::{DefiningEntity, Value},
};

use pliron::derive::{op_interface, op_interface_impl, type_interface, type_interface_impl};
use rustc_hash::FxHashMap;
use thiserror::Error;

use crate::{
    attributes::{
        AtomicOrderingAttr, AtomicRmwKindAttr, FCmpPredicateAttr, ICmpPredicateAttr, LinkageAttr,
    },
    llvm_sys::core::{
        LLVMBasicBlock, LLVMBuilder, LLVMContext, LLVMModule, LLVMType, LLVMValue,
        instruction_iter, llvm_add_case, llvm_add_function, llvm_add_global_in_address_space,
        llvm_add_incoming, llvm_append_basic_block_in_context, llvm_array_type2, llvm_build_add,
        llvm_build_addrspacecast, llvm_build_and, llvm_build_array_alloca, llvm_build_ashr,
        llvm_build_atomic_cmpxchg, llvm_build_atomic_rmw, llvm_build_bitcast, llvm_build_br,
        llvm_build_call2, llvm_build_cond_br, llvm_build_extract_element, llvm_build_extract_value,
        llvm_build_fadd, llvm_build_fcmp, llvm_build_fdiv, llvm_build_fence, llvm_build_fmul,
        llvm_build_fpext, llvm_build_fptosi, llvm_build_fptoui, llvm_build_fptrunc,
        llvm_build_freeze, llvm_build_frem, llvm_build_fsub, llvm_build_gep2, llvm_build_icmp,
        llvm_build_insert_element, llvm_build_insert_value, llvm_build_int_to_ptr,
        llvm_build_load2, llvm_build_lshr, llvm_build_mul, llvm_build_or, llvm_build_phi,
        llvm_build_ptr_to_int, llvm_build_ret, llvm_build_ret_void, llvm_build_sdiv,
        llvm_build_select, llvm_build_sext, llvm_build_shl, llvm_build_shuffle_vector,
        llvm_build_sitofp, llvm_build_srem, llvm_build_store, llvm_build_sub, llvm_build_switch,
        llvm_build_trunc, llvm_build_udiv, llvm_build_uitofp, llvm_build_unreachable,
        llvm_build_urem, llvm_build_va_arg, llvm_build_xor, llvm_build_zext,
        llvm_can_value_use_fast_math_flags, llvm_clear_insertion_position, llvm_const_int,
        llvm_const_null, llvm_const_real, llvm_const_vector, llvm_double_type_in_context,
        llvm_float_type_in_context, llvm_function_type, llvm_get_inline_asm,
        llvm_get_named_function, llvm_get_param, llvm_get_poison, llvm_get_sync_scope_id,
        llvm_get_undef, llvm_half_type_in_context, llvm_int_type_in_context, llvm_is_a,
        llvm_lookup_intrinsic_id, llvm_pointer_type_in_context, llvm_position_builder_at_end,
        llvm_scalable_vector_type, llvm_set_alignment, llvm_set_atomic_sync_scope_id,
        llvm_set_fast_math_flags, llvm_set_initializer, llvm_set_linkage, llvm_set_nneg,
        llvm_set_ordering, llvm_struct_create_named, llvm_struct_set_body,
        llvm_struct_type_in_context, llvm_type_of, llvm_vector_type, llvm_void_type_in_context,
    },
    op_interfaces::{
        AlignableOpInterface, FastMathFlags, IsDeclaration, LlvmSymbolName, NNegFlag,
        PointerTypeResult,
    },
    ops::{
        AShrOp, AddOp, AddrSpaceCastOp, AddressOfOp, AllocaOp, AndOp, AtomicCmpxchgOp,
        AtomicLoadOp, AtomicRmwOp, AtomicStoreOp, BitcastOp, BrOp, CallIntrinsicOp, CallOp,
        CondBrOp, ExtractElementOp, ExtractValueOp, FAddOp, FCmpOp, FDivOp, FMulOp, FPExtOp,
        FPToSIOp, FPToUIOp, FPTruncOp, FRemOp, FSubOp, FenceOp, FreezeOp, FuncOp, GetElementPtrOp,
        GlobalOp, ICmpOp, InlineAsmOp, InsertElementOp, InsertValueOp, IntToPtrOp, LShrOp, LoadOp,
        MulOp, OrOp, PoisonOp, PtrToIntOp, ReturnOp, SDivOp, SExtOp, SIToFPOp, SRemOp, SelectOp,
        ShlOp, ShuffleVectorOp, StoreOp, SubOp, SwitchOp, TruncOp, UDivOp, UIToFPOp, URemOp,
        UndefOp, UnreachableOp, VAArgOp, XorOp, ZExtOp, ZeroOp,
    },
    types::{ArrayType, FuncType, PointerType, StructType, VectorType, VoidType},
};

/// Mapping from pliron entities to LLVM entities.
pub struct ConversionContext<'a> {
    // The current LLVMModule being converted to.
    cur_llvm_module: &'a LLVMModule,
    // A map from pliron Values to LLVM Values.
    value_map: FxHashMap<Value, LLVMValue>,
    // A map from pliron basic blocks to LLVM.
    block_map: FxHashMap<Ptr<BasicBlock>, LLVMBasicBlock>,
    // A map from pliron functions to LLVM functions.
    function_map: FxHashMap<Identifier, LLVMValue>,
    // A map from pliron globals to LLVM globals.
    globals_map: FxHashMap<Identifier, LLVMValue>,
    // A map from pliron StructTypes to LLVM StructTypes.
    structs_map: FxHashMap<Identifier, LLVMType>,
    // Type cache to avoid redundant conversions.
    type_cache: FxHashMap<TypeHandle, LLVMType>,
    // The active LLVM builder.
    builder: LLVMBuilder,
    // Scratch builder in a scratch function for attempting to evaluate constants.
    scratch_builder: LLVMBuilder,
}

impl<'a> ConversionContext<'a> {
    pub fn new(llvm_ctx: &'a LLVMContext, cur_llvm_module: &'a LLVMModule) -> Self {
        Self {
            cur_llvm_module,
            value_map: FxHashMap::default(),
            block_map: FxHashMap::default(),
            function_map: FxHashMap::default(),
            globals_map: FxHashMap::default(),
            structs_map: FxHashMap::default(),
            type_cache: FxHashMap::default(),
            builder: LLVMBuilder::new(llvm_ctx),
            scratch_builder: LLVMBuilder::new(llvm_ctx),
        }
    }

    pub fn clear_per_function_data(&mut self) {
        self.value_map.clear();
        self.block_map.clear();
        llvm_clear_insertion_position(&self.builder);
    }
}

#[derive(Error, Debug)]
pub enum ToLLVMErr {
    #[error("Type {0} does not have a conversion to LLVM type implemented")]
    MissingTypeConversion(String),
    #[error("Operation {0} does not have a conversion to LLVM instruction implemented")]
    MissingOpConversion(String),
    #[error("Definition for value {0} not seen yet")]
    UndefinedValue(String),
    #[error("Block definition {0} not seen yet")]
    UndefinedBlock(String),
    #[error("Number of block args in the source dialect equal the number of PHIs in target IR")]
    NumBlockArgsNumPhisMismatch,
    #[error("ConstantOp must have integer or float value")]
    ConstOpNotIntOrFloat,
    #[error(
        "Insert/Extract value instructions must specify exactly one index, an LLVM-C API limitation"
    )]
    InsertExtractValueIndices,
    #[error("GlobalOp Initializer region does not terminate with a return with value")]
    GlobalOpInitializerRegionBadReturn,
    #[error("Cannot evaluate value to a constant")]
    CannotEvaluateToConst,
}

pub fn convert_ipredicate(pred: ICmpPredicateAttr) -> LLVMIntPredicate {
    match pred {
        ICmpPredicateAttr::EQ => LLVMIntPredicate::LLVMIntEQ,
        ICmpPredicateAttr::NE => LLVMIntPredicate::LLVMIntNE,
        ICmpPredicateAttr::UGT => LLVMIntPredicate::LLVMIntUGT,
        ICmpPredicateAttr::UGE => LLVMIntPredicate::LLVMIntUGE,
        ICmpPredicateAttr::ULT => LLVMIntPredicate::LLVMIntULT,
        ICmpPredicateAttr::ULE => LLVMIntPredicate::LLVMIntULE,
        ICmpPredicateAttr::SGT => LLVMIntPredicate::LLVMIntSGT,
        ICmpPredicateAttr::SGE => LLVMIntPredicate::LLVMIntSGE,
        ICmpPredicateAttr::SLT => LLVMIntPredicate::LLVMIntSLT,
        ICmpPredicateAttr::SLE => LLVMIntPredicate::LLVMIntSLE,
    }
}

pub fn convert_fpredicate(pred: FCmpPredicateAttr) -> LLVMRealPredicate {
    match pred {
        FCmpPredicateAttr::False => LLVMRealPredicate::LLVMRealPredicateFalse,
        FCmpPredicateAttr::OEQ => LLVMRealPredicate::LLVMRealOEQ,
        FCmpPredicateAttr::OGT => LLVMRealPredicate::LLVMRealOGT,
        FCmpPredicateAttr::OGE => LLVMRealPredicate::LLVMRealOGE,
        FCmpPredicateAttr::OLT => LLVMRealPredicate::LLVMRealOLT,
        FCmpPredicateAttr::OLE => LLVMRealPredicate::LLVMRealOLE,
        FCmpPredicateAttr::ONE => LLVMRealPredicate::LLVMRealONE,
        FCmpPredicateAttr::ORD => LLVMRealPredicate::LLVMRealORD,
        FCmpPredicateAttr::UNO => LLVMRealPredicate::LLVMRealUNO,
        FCmpPredicateAttr::UEQ => LLVMRealPredicate::LLVMRealUEQ,
        FCmpPredicateAttr::UGT => LLVMRealPredicate::LLVMRealUGT,
        FCmpPredicateAttr::UGE => LLVMRealPredicate::LLVMRealUGE,
        FCmpPredicateAttr::ULT => LLVMRealPredicate::LLVMRealULT,
        FCmpPredicateAttr::ULE => LLVMRealPredicate::LLVMRealULE,
        FCmpPredicateAttr::UNE => LLVMRealPredicate::LLVMRealUNE,
        FCmpPredicateAttr::True => LLVMRealPredicate::LLVMRealPredicateTrue,
    }
}

pub fn convert_linkage(linkage: LinkageAttr) -> LLVMLinkage {
    match linkage {
        LinkageAttr::ExternalLinkage => LLVMLinkage::LLVMExternalLinkage,
        LinkageAttr::AvailableExternallyLinkage => LLVMLinkage::LLVMAvailableExternallyLinkage,
        LinkageAttr::LinkOnceAnyLinkage => LLVMLinkage::LLVMLinkOnceAnyLinkage,
        LinkageAttr::LinkOnceODRLinkage => LLVMLinkage::LLVMLinkOnceODRLinkage,
        LinkageAttr::WeakAnyLinkage => LLVMLinkage::LLVMWeakAnyLinkage,
        LinkageAttr::WeakODRLinkage => LLVMLinkage::LLVMWeakODRLinkage,
        LinkageAttr::AppendingLinkage => LLVMLinkage::LLVMAppendingLinkage,
        LinkageAttr::InternalLinkage => LLVMLinkage::LLVMInternalLinkage,
        LinkageAttr::PrivateLinkage => LLVMLinkage::LLVMPrivateLinkage,
        LinkageAttr::DLLImportLinkage => LLVMLinkage::LLVMDLLImportLinkage,
        LinkageAttr::DLLExportLinkage => LLVMLinkage::LLVMDLLExportLinkage,
        LinkageAttr::ExternalWeakLinkage => LLVMLinkage::LLVMExternalWeakLinkage,
        LinkageAttr::GhostLinkage => LLVMLinkage::LLVMGhostLinkage,
        LinkageAttr::CommonLinkage => LLVMLinkage::LLVMCommonLinkage,
        LinkageAttr::LinkOnceODRAutoHideLinkage => LLVMLinkage::LLVMLinkOnceODRAutoHideLinkage,
        LinkageAttr::LinkerPrivateLinkage => LLVMLinkage::LLVMLinkerPrivateLinkage,
        LinkageAttr::LinkerPrivateWeakLinkage => LLVMLinkage::LLVMLinkerPrivateWeakLinkage,
    }
}

#[::pliron::linkme::distributed_slice]
#[linkme(crate = pliron::linkme)]
pub static TEST: [u64];

/// Convert a float attribute to fp64 (since LLVM's C-API pretty much restricts us to that).
#[attr_interface]
trait FloatAttrToFP64: FloatAttr {
    fn to_fp64(&self) -> f64;
    fn verify(_attr: &dyn Attribute, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

#[attr_interface_impl]
impl FloatAttrToFP64 for FPSingleAttr {
    fn to_fp64(&self) -> f64 {
        Into::<f32>::into(self.clone()) as f64
    }
}

#[attr_interface_impl]
impl FloatAttrToFP64 for FPDoubleAttr {
    fn to_fp64(&self) -> f64 {
        Into::<f64>::into(self.clone())
    }
}

/// A type that implements this is convertible to an [LLVMType].
#[type_interface]
trait ToLLVMType {
    /// Convert from pliron [Type] to [LLVMType].
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMType>;

    fn verify(_type: &dyn Type, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

/// An [Op] that implements this is convertible to an [LLVMValue].
#[op_interface]
trait ToLLVMValue {
    /// Convert from pliron [Op] to [LLVMValue].
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue>;

    fn verify(_op: &dyn Op, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

#[type_interface_impl]
impl ToLLVMType for IntegerType {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_int_type_in_context(llvm_ctx, self.width()))
    }
}

#[type_interface_impl]
impl ToLLVMType for ArrayType {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        let elem_ty = convert_type(ctx, llvm_ctx, cctx, self.elem_type())?;
        Ok(llvm_array_type2(elem_ty, self.size()))
    }
}

#[type_interface_impl]
impl ToLLVMType for FuncType {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        let args_tys: Vec<_> = self
            .arg_types()
            .iter()
            .map(|ty| convert_type(ctx, llvm_ctx, cctx, *ty))
            .collect::<Result<_>>()?;
        let ret_ty = convert_type(ctx, llvm_ctx, cctx, self.result_type())?;
        Ok(llvm_function_type(ret_ty, &args_tys, self.is_var_arg()))
    }
}

#[type_interface_impl]
impl ToLLVMType for VoidType {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_void_type_in_context(llvm_ctx))
    }
}

#[type_interface_impl]
impl ToLLVMType for PointerType {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_pointer_type_in_context(llvm_ctx, self.address_space()))
    }
}

#[type_interface_impl]
impl ToLLVMType for StructType {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        if self.is_opaque() {
            let name = self.name().expect("Opaqaue struct must have a name");
            Ok(llvm_struct_create_named(llvm_ctx, name.as_str()))
        } else {
            let field_types = self
                .fields()
                .map(|fty| convert_type(ctx, llvm_ctx, cctx, fty))
                .collect::<Result<Vec<_>>>()?;
            if let Some(name) = self.name() {
                match cctx.structs_map.entry(name) {
                    hash_map::Entry::Occupied(entry) => Ok(*entry.get()),
                    hash_map::Entry::Vacant(entry) => {
                        let str_ty = llvm_struct_create_named(llvm_ctx, entry.key());
                        llvm_struct_set_body(str_ty, &field_types, false);
                        entry.insert(str_ty);
                        Ok(str_ty)
                    }
                }
            } else {
                Ok(llvm_struct_type_in_context(llvm_ctx, &field_types, false))
            }
        }
    }
}

#[type_interface_impl]
impl ToLLVMType for VectorType {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        let elem_ty = convert_type(ctx, llvm_ctx, cctx, self.elem_type())?;
        let num_elems = self.num_elements();
        if self.is_scalable() {
            Ok(llvm_scalable_vector_type(elem_ty, num_elems))
        } else {
            Ok(llvm_vector_type(elem_ty, num_elems))
        }
    }
}

#[type_interface_impl]
impl ToLLVMType for FP32Type {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_float_type_in_context(llvm_ctx))
    }
}

#[type_interface_impl]
impl ToLLVMType for FP64Type {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_double_type_in_context(llvm_ctx))
    }
}

#[type_interface_impl]
impl ToLLVMType for FP16Type {
    fn convert(
        &self,
        _ctx: &Context,
        llvm_ctx: &LLVMContext,
        _cctx: &mut ConversionContext,
    ) -> Result<LLVMType> {
        Ok(llvm_half_type_in_context(llvm_ctx))
    }
}

/// Convert a pliron [Type] to [LLVMType].
pub fn convert_type(
    ctx: &Context,
    llvm_ctx: &LLVMContext,
    cctx: &mut ConversionContext,
    ty: TypeHandle,
) -> Result<LLVMType> {
    if let Some(cached) = cctx.type_cache.get(&ty) {
        return Ok(*cached);
    }
    if let Some(converter) = type_cast::<dyn ToLLVMType>(&*ty.deref(ctx)) {
        let llvm_ty = converter.convert(ctx, llvm_ctx, cctx)?;
        cctx.type_cache.insert(ty, llvm_ty);
        return Ok(llvm_ty);
    }

    input_err_noloc!(ToLLVMErr::MissingTypeConversion(
        ty.deref(ctx).get_type_id().to_string()
    ))
}

fn convert_value_operand(
    cctx: &mut ConversionContext,
    ctx: &Context,
    value: &Value,
) -> Result<LLVMValue> {
    match cctx.value_map.get(value) {
        Some(v) => Ok(*v),
        None => {
            input_err_noloc!(ToLLVMErr::UndefinedValue(value.unique_name(ctx).into()))
        }
    }
}

fn convert_block_operand(
    cctx: &mut ConversionContext,
    ctx: &Context,
    block: Ptr<BasicBlock>,
) -> Result<LLVMBasicBlock> {
    match cctx.block_map.get(&block) {
        Some(v) => Ok(*v),
        None => {
            input_err_noloc!(ToLLVMErr::UndefinedBlock(block.unique_name(ctx).into()))
        }
    }
}

macro_rules! to_llvm_value_int_bin_op {
    (
        $op_name:ident, $builder_function:ident
    ) => {
        #[pliron::derive::op_interface_impl]
        impl ToLLVMValue for $op_name {
            fn convert(
                &self,
                ctx: &Context,
                _llvm_ctx: &LLVMContext,
                cctx: &mut ConversionContext,
            ) -> Result<LLVMValue> {
                let op = self.get_operation().deref(ctx);
                let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
                let lhs = convert_value_operand(cctx, ctx, &lhs)?;
                let rhs = convert_value_operand(cctx, ctx, &rhs)?;
                Ok($builder_function(
                    &cctx.builder,
                    lhs,
                    rhs,
                    &self.get_result(ctx).unique_name(ctx),
                ))
            }
        }
    };
}

to_llvm_value_int_bin_op!(AddOp, llvm_build_add);
to_llvm_value_int_bin_op!(SubOp, llvm_build_sub);
to_llvm_value_int_bin_op!(MulOp, llvm_build_mul);
to_llvm_value_int_bin_op!(SDivOp, llvm_build_sdiv);
to_llvm_value_int_bin_op!(UDivOp, llvm_build_udiv);
to_llvm_value_int_bin_op!(URemOp, llvm_build_urem);
to_llvm_value_int_bin_op!(SRemOp, llvm_build_srem);
to_llvm_value_int_bin_op!(AndOp, llvm_build_and);
to_llvm_value_int_bin_op!(OrOp, llvm_build_or);
to_llvm_value_int_bin_op!(XorOp, llvm_build_xor);
to_llvm_value_int_bin_op!(ShlOp, llvm_build_shl);
to_llvm_value_int_bin_op!(LShrOp, llvm_build_lshr);
to_llvm_value_int_bin_op!(AShrOp, llvm_build_ashr);

#[op_interface_impl]
impl ToLLVMValue for AllocaOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_pointee_type(ctx))?;
        let size = convert_value_operand(cctx, ctx, &self.get_operand(ctx))?;
        let alloca_op = llvm_build_array_alloca(
            &cctx.builder,
            ty,
            size,
            &self.get_result(ctx).unique_name(ctx),
        );
        if let Some(alignment) = self.alignment(ctx) {
            llvm_set_alignment(alloca_op, alignment);
        }
        Ok(alloca_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for BitcastOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let arg = convert_value_operand(cctx, ctx, &self.get_operand(ctx))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let bitcast_op = llvm_build_bitcast(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(bitcast_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for AddrSpaceCastOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let arg = convert_value_operand(cctx, ctx, &self.get_operand(ctx))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let addrspacecast_op = llvm_build_addrspacecast(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(addrspacecast_op)
    }
}

fn link_succ_operands_with_phis(
    ctx: &Context,
    cctx: &mut ConversionContext,
    source_block: Ptr<BasicBlock>,
    target_block: LLVMBasicBlock,
    opds: Vec<Value>,
) -> Result<()> {
    let mut phis = vec![];
    for inst in instruction_iter(target_block) {
        if !llvm_is_a::phi_node(inst) {
            break;
        };
        phis.push(inst);
    }

    if phis.len() != opds.len() {
        return input_err!(
            source_block.deref(ctx).loc(),
            ToLLVMErr::NumBlockArgsNumPhisMismatch
        );
    }

    let source_block = convert_block_operand(cctx, ctx, source_block)?;

    for (idx, arg) in opds.iter().enumerate() {
        let arg = convert_value_operand(cctx, ctx, arg)?;
        llvm_add_incoming(phis[idx], &[arg], &[source_block]);
    }
    Ok(())
}

#[op_interface_impl]
impl ToLLVMValue for BrOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let succ = op.get_successor(0);
        let succ_llvm = convert_block_operand(cctx, ctx, succ)?;
        let branch_op = llvm_build_br(&cctx.builder, succ_llvm);

        // Link the arguments we pass to the block with the PHIs there.
        link_succ_operands_with_phis(
            ctx,
            cctx,
            op.get_container().expect("Unlinked operation"),
            succ_llvm,
            self.successor_operands(ctx, 0),
        )?;

        Ok(branch_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for CondBrOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (true_succ, false_succ) = (op.get_successor(0), op.get_successor(1));
        let true_succ_llvm = convert_block_operand(cctx, ctx, true_succ)?;
        let false_succ_llvm = convert_block_operand(cctx, ctx, false_succ)?;
        let cond = convert_value_operand(cctx, ctx, &self.get_operand_condition(ctx))?;

        let branch_op = llvm_build_cond_br(&cctx.builder, cond, true_succ_llvm, false_succ_llvm);

        // Link the arguments we pass to the block with the PHIs there.
        link_succ_operands_with_phis(
            ctx,
            cctx,
            op.get_container().expect("Unlinked operation"),
            true_succ_llvm,
            self.successor_operands(ctx, 0),
        )?;
        link_succ_operands_with_phis(
            ctx,
            cctx,
            op.get_container().expect("Unlinked operation"),
            false_succ_llvm,
            self.successor_operands(ctx, 1),
        )?;

        Ok(branch_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for SwitchOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let cond = convert_value_operand(cctx, ctx, &self.get_operand_condition(ctx))?;
        let default_succ = convert_block_operand(cctx, ctx, self.default_dest(ctx))?;
        let switch_op = llvm_build_switch(
            &cctx.builder,
            cond,
            default_succ,
            self.cases(ctx).len() as u32,
        );

        // Link the arguments we pass to the block with the PHIs there.
        link_succ_operands_with_phis(
            ctx,
            cctx,
            op.get_container().expect("Unlinked operation"),
            default_succ,
            self.default_dest_operands(ctx),
        )?;
        for case in self.cases(ctx) {
            let succ_llvm = convert_block_operand(cctx, ctx, case.dest)?;
            link_succ_operands_with_phis(
                ctx,
                cctx,
                op.get_container().expect("Unlinked operation"),
                succ_llvm,
                case.dest_opds,
            )?;

            let int_ty = case.value.get_type();
            let int_ty_llvm = convert_type(ctx, llvm_ctx, cctx, int_ty.into())?;
            let ap_int_val: APInt = case.value.clone().into();
            let case_const_val = llvm_const_int(int_ty_llvm, ap_int_val.to_u64(), false);

            llvm_add_case(switch_op, case_const_val, succ_llvm);
        }

        Ok(switch_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for LoadOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let pointee_ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let ptr = convert_value_operand(cctx, ctx, &self.get_operand(ctx))?;
        let load_op = llvm_build_load2(
            &cctx.builder,
            pointee_ty,
            ptr,
            &self.get_result(ctx).unique_name(ctx),
        );
        if let Some(alignment) = self.alignment(ctx) {
            llvm_set_alignment(load_op, alignment);
        }
        Ok(load_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for StoreOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let value = convert_value_operand(cctx, ctx, &self.get_operand_value(ctx))?;
        let ptr = convert_value_operand(cctx, ctx, &self.get_operand_address(ctx))?;
        let store_op = llvm_build_store(&cctx.builder, value, ptr);
        if let Some(alignment) = self.alignment(ctx) {
            llvm_set_alignment(store_op, alignment);
        }
        Ok(store_op)
    }
}

/// Map a pliron [AtomicOrderingAttr] to its LLVM-C counterpart.
fn convert_atomic_ordering(o: &AtomicOrderingAttr) -> LLVMAtomicOrdering {
    match o {
        AtomicOrderingAttr::Monotonic => LLVMAtomicOrdering::LLVMAtomicOrderingMonotonic,
        AtomicOrderingAttr::Acquire => LLVMAtomicOrdering::LLVMAtomicOrderingAcquire,
        AtomicOrderingAttr::Release => LLVMAtomicOrdering::LLVMAtomicOrderingRelease,
        AtomicOrderingAttr::AcqRel => LLVMAtomicOrdering::LLVMAtomicOrderingAcquireRelease,
        AtomicOrderingAttr::SeqCst => LLVMAtomicOrdering::LLVMAtomicOrderingSequentiallyConsistent,
    }
}

/// Map a pliron [AtomicRmwKindAttr] to its LLVM-C counterpart.
fn convert_rmw_kind(k: &AtomicRmwKindAttr) -> LLVMAtomicRMWBinOp {
    match k {
        AtomicRmwKindAttr::Xchg => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpXchg,
        AtomicRmwKindAttr::Add => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpAdd,
        AtomicRmwKindAttr::Sub => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpSub,
        AtomicRmwKindAttr::And => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpAnd,
        AtomicRmwKindAttr::Nand => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpNand,
        AtomicRmwKindAttr::Or => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpOr,
        AtomicRmwKindAttr::Xor => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpXor,
        AtomicRmwKindAttr::Max => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpMax,
        AtomicRmwKindAttr::Min => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpMin,
        AtomicRmwKindAttr::UMax => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpUMax,
        AtomicRmwKindAttr::UMin => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpUMin,
        AtomicRmwKindAttr::FAdd => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpFAdd,
        AtomicRmwKindAttr::FSub => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpFSub,
        AtomicRmwKindAttr::FMax => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpFMax,
        AtomicRmwKindAttr::FMin => LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpFMin,
    }
}

#[op_interface_impl]
impl ToLLVMValue for AtomicRmwOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let (ptr_opd, val_opd) = {
            let op = self.get_operation().deref(ctx);
            (op.get_operand(0), op.get_operand(1))
        };
        let ptr = convert_value_operand(cctx, ctx, &ptr_opd)?;
        let val = convert_value_operand(cctx, ctx, &val_opd)?;
        let kind = convert_rmw_kind(
            &self
                .get_attr_llvm_rmw_kind(ctx)
                .expect("atomicrmw missing rmw kind"),
        );
        let ordering = convert_atomic_ordering(
            &self
                .get_attr_llvm_rmw_ordering(ctx)
                .expect("atomicrmw missing ordering"),
        );
        let scope = self
            .get_attr_llvm_rmw_syncscope(ctx)
            .map(|s| String::from((*s).clone()))
            .unwrap_or_default();
        let ssid = llvm_get_sync_scope_id(llvm_ctx, &scope);
        Ok(llvm_build_atomic_rmw(
            &cctx.builder,
            kind,
            ptr,
            val,
            ordering,
            ssid,
        ))
    }
}

#[op_interface_impl]
impl ToLLVMValue for AtomicCmpxchgOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let (ptr_opd, cmp_opd, new_opd) = {
            let op = self.get_operation().deref(ctx);
            (op.get_operand(0), op.get_operand(1), op.get_operand(2))
        };
        let ptr = convert_value_operand(cctx, ctx, &ptr_opd)?;
        let cmp = convert_value_operand(cctx, ctx, &cmp_opd)?;
        let new = convert_value_operand(cctx, ctx, &new_opd)?;
        let success = convert_atomic_ordering(
            &self
                .get_attr_llvm_cas_success_ordering(ctx)
                .expect("cmpxchg missing success ordering"),
        );
        let failure = convert_atomic_ordering(
            &self
                .get_attr_llvm_cas_failure_ordering(ctx)
                .expect("cmpxchg missing failure ordering"),
        );
        let scope = self
            .get_attr_llvm_cas_syncscope(ctx)
            .map(|s| String::from((*s).clone()))
            .unwrap_or_default();
        let ssid = llvm_get_sync_scope_id(llvm_ctx, &scope);
        Ok(llvm_build_atomic_cmpxchg(
            &cctx.builder,
            ptr,
            cmp,
            new,
            success,
            failure,
            ssid,
        ))
    }
}

#[op_interface_impl]
impl ToLLVMValue for FenceOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ordering = convert_atomic_ordering(
            &self
                .get_attr_llvm_fence_ordering(ctx)
                .expect("fence missing ordering"),
        );
        let scope = self
            .get_attr_llvm_fence_syncscope(ctx)
            .map(|s| String::from((*s).clone()))
            .unwrap_or_default();
        let ssid = llvm_get_sync_scope_id(llvm_ctx, &scope);
        Ok(llvm_build_fence(&cctx.builder, ordering, ssid, ""))
    }
}

#[op_interface_impl]
impl ToLLVMValue for AtomicLoadOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let (ptr_opd, result_val) = {
            let op = self.get_operation().deref(ctx);
            (op.get_operand(0), op.get_result(0))
        };
        let pointee_ty = convert_type(ctx, llvm_ctx, cctx, result_val.get_type(ctx))?;
        let ptr = convert_value_operand(cctx, ctx, &ptr_opd)?;
        let load = llvm_build_load2(&cctx.builder, pointee_ty, ptr, &result_val.unique_name(ctx));
        let ordering = convert_atomic_ordering(
            &self
                .get_attr_llvm_ld_ordering(ctx)
                .expect("atomic load missing ordering"),
        );
        llvm_set_ordering(load, ordering);
        let scope = self
            .get_attr_llvm_ld_syncscope(ctx)
            .map(|s| String::from((*s).clone()))
            .unwrap_or_default();
        llvm_set_atomic_sync_scope_id(load, llvm_get_sync_scope_id(llvm_ctx, &scope));
        if let Some(alignment) = self.alignment(ctx) {
            llvm_set_alignment(load, alignment);
        }
        Ok(load)
    }
}

#[op_interface_impl]
impl ToLLVMValue for AtomicStoreOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let (val_opd, ptr_opd) = {
            let op = self.get_operation().deref(ctx);
            (op.get_operand(0), op.get_operand(1))
        };
        let value = convert_value_operand(cctx, ctx, &val_opd)?;
        let ptr = convert_value_operand(cctx, ctx, &ptr_opd)?;
        let store = llvm_build_store(&cctx.builder, value, ptr);
        let ordering = convert_atomic_ordering(
            &self
                .get_attr_llvm_st_ordering(ctx)
                .expect("atomic store missing ordering"),
        );
        llvm_set_ordering(store, ordering);
        let scope = self
            .get_attr_llvm_st_syncscope(ctx)
            .map(|s| String::from((*s).clone()))
            .unwrap_or_default();
        llvm_set_atomic_sync_scope_id(store, llvm_get_sync_scope_id(llvm_ctx, &scope));
        if let Some(alignment) = self.alignment(ctx) {
            llvm_set_alignment(store, alignment);
        }
        Ok(store)
    }
}

#[op_interface_impl]
impl ToLLVMValue for InlineAsmOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let (arg_opds, result_val) = {
            let op = self.get_operation().deref(ctx);
            let n = op.get_num_operands();
            let args: Vec<Value> = (0..n).map(|i| op.get_operand(i)).collect();
            (args, op.get_result(0))
        };
        let args: Vec<LLVMValue> = arg_opds
            .iter()
            .map(|v| convert_value_operand(cctx, ctx, v))
            .collect::<Result<_>>()?;
        let result_ty = result_val.get_type(ctx);
        let result_llvm_ty = convert_type(ctx, llvm_ctx, cctx, result_ty)?;
        let arg_types: Vec<LLVMType> = args.iter().map(|a| llvm_type_of(*a)).collect();
        let fn_ty = llvm_function_type(result_llvm_ty, &arg_types, false);
        let asm = String::from(
            (*self
                .get_attr_inline_asm_template(ctx)
                .expect("inline asm missing template"))
            .clone(),
        );
        let constraints = String::from(
            (*self
                .get_attr_inline_asm_constraints(ctx)
                .expect("inline asm missing constraints"))
            .clone(),
        );
        // `has_side_effects` is set unconditionally: this op does not model a
        // side-effects flag, and side-effecting asm is the safe default.
        // NOTE: the op's `inline_asm_convergent` attribute is not applied here.
        // `convergent` is an LLVM call-site attribute (not part of the inline-asm
        // value), so converting to LLVM IR drops the convergent flag.
        let asm_val = llvm_get_inline_asm(
            fn_ty,
            &asm,
            &constraints,
            true,
            false,
            LLVMInlineAsmDialect::LLVMInlineAsmDialectATT,
            false,
        );
        let name = if result_ty.deref(ctx).is::<VoidType>() {
            String::new()
        } else {
            result_val.unique_name(ctx).to_string()
        };
        Ok(llvm_build_call2(
            &cctx.builder,
            fn_ty,
            asm_val,
            &args,
            &name,
        ))
    }
}

#[op_interface_impl]
impl ToLLVMValue for ICmpOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let predicate = convert_ipredicate(self.predicate(ctx));
        let lhs = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let rhs = convert_value_operand(cctx, ctx, &op.get_operand(1))?;
        let icmp_op = llvm_build_icmp(
            &cctx.builder,
            predicate,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(icmp_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ReturnOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ret_op = if let Some(retval) = self.retval(ctx) {
            let retval = convert_value_operand(cctx, ctx, &retval)?;
            llvm_build_ret(&cctx.builder, retval)
        } else {
            llvm_build_ret_void(&cctx.builder)
        };
        Ok(ret_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for UnreachableOp {
    fn convert(
        &self,
        _ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        Ok(llvm_build_unreachable(&cctx.builder))
    }
}

#[op_interface_impl]
impl ToLLVMValue for ConstantOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        <Self as ToLLVMConstValue>::convert(self, ctx, llvm_ctx, cctx)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ZeroOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        <Self as ToLLVMConstValue>::convert(self, ctx, llvm_ctx, cctx)
    }
}

#[op_interface_impl]
impl ToLLVMValue for IntToPtrOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let inttoptr_op = llvm_build_int_to_ptr(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(inttoptr_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for PtrToIntOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let ptrtoint_op = llvm_build_ptr_to_int(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(ptrtoint_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for UndefOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        <Self as ToLLVMConstValue>::convert(self, ctx, llvm_ctx, cctx)
    }
}

#[op_interface_impl]
impl ToLLVMValue for PoisonOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        <Self as ToLLVMConstValue>::convert(self, ctx, llvm_ctx, cctx)
    }
}

#[op_interface_impl]
impl ToLLVMValue for AddressOfOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        <Self as ToLLVMConstValue>::convert(self, ctx, llvm_ctx, cctx)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FreezeOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let freeze_op =
            llvm_build_freeze(&cctx.builder, arg, &self.get_result(ctx).unique_name(ctx));
        Ok(freeze_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for CallOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let args: Vec<_> = self
            .args(ctx)
            .into_iter()
            .map(|v| convert_value_operand(cctx, ctx, &v))
            .collect::<Result<_>>()?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.callee_type(ctx))?;
        let res = self.get_result(ctx);
        let name = if res.get_type(ctx).deref(ctx).is::<VoidType>() {
            ""
        } else {
            &res.unique_name(ctx)
        };
        let callee = match self.callee(ctx) {
            CallOpCallable::Direct(callee_sym) => {
                *cctx.function_map.get(&callee_sym).ok_or_else(|| {
                    input_error_noloc!(ToLLVMErr::UndefinedValue(callee_sym.to_string()))
                })?
            }
            CallOpCallable::Indirect(callee) => convert_value_operand(cctx, ctx, &callee)?,
        };
        let call_val = llvm_build_call2(&cctx.builder, ty, callee, &args, name);
        if let Some(fmf) = self.get_attr_llvm_call_fastmath_flags(ctx)
            && llvm_can_value_use_fast_math_flags(call_val)
        {
            llvm_set_fast_math_flags(call_val, (*fmf).into());
        }
        Ok(call_val)
    }
}

#[op_interface_impl]
impl ToLLVMValue for CallIntrinsicOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let args: Vec<_> = (0..op.get_num_operands())
            .map(|i| convert_value_operand(cctx, ctx, &op.get_operand(i)))
            .collect::<Result<_>>()?;
        let fn_ty = convert_type(
            ctx,
            llvm_ctx,
            cctx,
            self.get_attr_llvm_intrinsic_type(ctx)
                .unwrap()
                .get_type(ctx),
        )?;

        let intrinsic_name = <StringAttr as Into<String>>::into(
            self.get_attr_llvm_intrinsic_name(ctx)
                .expect("Intrinsic call does not name the intrinsic to be called")
                .clone(),
        );

        let _intrinsic_id = llvm_lookup_intrinsic_id(&intrinsic_name).ok_or_else(|| {
            input_error_noloc!(ToLLVMErr::UndefinedValue(intrinsic_name.to_string()))
        })?;

        // We just use llvm_add_function instead of llvm_get_intrinsic_declaration here
        // because the latter requires that (and I quote from Intrinsics.h::getOrInsertDeclaration):
        //   "For a declaration of an overloaded intrinsic, Tys must provide exactly one
        //    type for each overloaded type in the intrinsic."
        // I don't know how to determine that from just the name and argument types.
        let intrinsic_fn = llvm_get_named_function(cctx.cur_llvm_module, &intrinsic_name)
            .unwrap_or_else(|| llvm_add_function(cctx.cur_llvm_module, &intrinsic_name, fn_ty));

        let res = self.get_result(ctx);
        let name = if res.get_type(ctx).deref(ctx).is::<VoidType>() {
            ""
        } else {
            &res.unique_name(ctx)
        };

        let intrinsic_op = llvm_build_call2(&cctx.builder, fn_ty, intrinsic_fn, &args, name);

        if let Some(fmf) = self.get_attr_llvm_intrinsic_fastmath_flags(ctx)
            && llvm_can_value_use_fast_math_flags(intrinsic_op)
        {
            llvm_set_fast_math_flags(intrinsic_op, (*fmf).into());
        }

        Ok(intrinsic_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for SExtOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let sext_op = llvm_build_sext(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(sext_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ZExtOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let zext_op = llvm_build_zext(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_is_a::instruction(zext_op) {
            let nneg = self.nneg(ctx);
            llvm_set_nneg(zext_op, nneg);
        }
        Ok(zext_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for TruncOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let trunc_op = llvm_build_trunc(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(trunc_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for GetElementPtrOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let indices = self
            .indices(ctx)
            .iter()
            .map(|v| match v {
                crate::ops::GepIndex::Constant(c) => Ok(llvm_const_int(
                    llvm_int_type_in_context(llvm_ctx, 32),
                    Into::<u64>::into(*c),
                    false,
                )),
                crate::ops::GepIndex::Value(value) => convert_value_operand(cctx, ctx, value),
            })
            .collect::<Result<Vec<_>>>()?;

        let base = convert_value_operand(cctx, ctx, &self.get_operand_src_ptr(ctx))?;

        let src_elem_type = convert_type(ctx, llvm_ctx, cctx, self.src_elem_type(ctx))?;
        let gep_op = llvm_build_gep2(
            &cctx.builder,
            src_elem_type,
            base,
            &indices,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(gep_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for InsertValueOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let base = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let value = convert_value_operand(cctx, ctx, &op.get_operand(1))?;
        let indices = self.indices(ctx);
        if indices.len() != 1 {
            return input_err!(op.loc(), ToLLVMErr::InsertExtractValueIndices);
        }
        let insert_op = llvm_build_insert_value(
            &cctx.builder,
            base,
            value,
            indices[0],
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(insert_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ExtractValueOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let base = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let indices = self.indices(ctx);
        if indices.len() != 1 {
            return input_err!(op.loc(), ToLLVMErr::InsertExtractValueIndices);
        }
        let extract_op = llvm_build_extract_value(
            &cctx.builder,
            base,
            indices[0],
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(extract_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for InsertElementOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let base = convert_value_operand(cctx, ctx, &self.get_operand_vector(ctx))?;
        let value = convert_value_operand(cctx, ctx, &self.get_operand_element(ctx))?;
        let index = convert_value_operand(cctx, ctx, &self.get_operand_index(ctx))?;
        let insert_op = llvm_build_insert_element(
            &cctx.builder,
            base,
            value,
            index,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(insert_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ExtractElementOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let base = convert_value_operand(cctx, ctx, &self.get_operand_vector(ctx))?;
        let index = convert_value_operand(cctx, ctx, &self.get_operand_index(ctx))?;
        let extract_op = llvm_build_extract_element(
            &cctx.builder,
            base,
            index,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(extract_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for ShuffleVectorOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let mask = &self
            .get_attr_llvm_shuffle_vector_mask(ctx)
            .expect("ShuffleVectorOp missing mask attribute")
            .0;
        let op = self.get_operation().deref(ctx);
        let vec1 = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let vec2 = convert_value_operand(cctx, ctx, &op.get_operand(1))?;
        let int_ty = llvm_int_type_in_context(llvm_ctx, 32);

        let mask = mask
            .iter()
            .map(|&i| llvm_const_int(int_ty, i as u64, true))
            .collect::<Vec<LLVMValue>>();
        let mask = llvm_const_vector(&mask);

        let shuffle_op = llvm_build_shuffle_vector(
            &cctx.builder,
            vec1,
            vec2,
            mask,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(shuffle_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for SelectOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let cond = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let true_val = convert_value_operand(cctx, ctx, &op.get_operand(1))?;
        let false_val = convert_value_operand(cctx, ctx, &op.get_operand(2))?;
        let select_op = llvm_build_select(
            &cctx.builder,
            cond,
            true_val,
            false_val,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(select_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FAddOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
        let lhs = convert_value_operand(cctx, ctx, &lhs)?;
        let rhs = convert_value_operand(cctx, ctx, &rhs)?;
        let inst = llvm_build_fadd(
            &cctx.builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(inst) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(inst, fastmath.into());
        }
        Ok(inst)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FSubOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
        let lhs = convert_value_operand(cctx, ctx, &lhs)?;
        let rhs = convert_value_operand(cctx, ctx, &rhs)?;
        let inst = llvm_build_fsub(
            &cctx.builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(inst) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(inst, fastmath.into());
        }
        Ok(inst)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FMulOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
        let lhs = convert_value_operand(cctx, ctx, &lhs)?;
        let rhs = convert_value_operand(cctx, ctx, &rhs)?;
        let inst = llvm_build_fmul(
            &cctx.builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(inst) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(inst, fastmath.into());
        }
        Ok(inst)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FDivOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
        let lhs = convert_value_operand(cctx, ctx, &lhs)?;
        let rhs = convert_value_operand(cctx, ctx, &rhs)?;
        let inst = llvm_build_fdiv(
            &cctx.builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(inst) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(inst, fastmath.into());
        }
        Ok(inst)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FRemOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let (lhs, rhs) = (op.get_operand(0), op.get_operand(1));
        let lhs = convert_value_operand(cctx, ctx, &lhs)?;
        let rhs = convert_value_operand(cctx, ctx, &rhs)?;
        let inst = llvm_build_frem(
            &cctx.builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(inst) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(inst, fastmath.into());
        }
        Ok(inst)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FCmpOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let predicate = convert_fpredicate(self.predicate(ctx));
        let lhs = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let rhs = convert_value_operand(cctx, ctx, &op.get_operand(1))?;
        let fcmp_op = llvm_build_fcmp(
            &cctx.builder,
            predicate,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(fcmp_op) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(fcmp_op, fastmath.into());
        }
        Ok(fcmp_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FPExtOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let fpext_op = llvm_build_fpext(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(fpext_op) {
            let fastmath = self.fast_math_flags(ctx);
            llvm_set_fast_math_flags(fpext_op, fastmath.into());
        }
        Ok(fpext_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FPTruncOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let fptrunc_op = llvm_build_fptrunc(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_can_value_use_fast_math_flags(fptrunc_op) {
            llvm_set_fast_math_flags(fptrunc_op, self.fast_math_flags(ctx).into());
        }
        Ok(fptrunc_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FPToSIOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let fptosi_op = llvm_build_fptosi(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(fptosi_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for SIToFPOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let sitofp_op = llvm_build_sitofp(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(sitofp_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for FPToUIOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let fptoui_op = llvm_build_fptoui(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(fptoui_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for UIToFPOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let uitofp_op = llvm_build_uitofp(
            &cctx.builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        // The built value may not even be an instruction, but a folded constant.
        if llvm_is_a::instruction(uitofp_op) {
            let nneg = self.nneg(ctx);
            llvm_set_nneg(uitofp_op, nneg);
        }
        Ok(uitofp_op)
    }
}

#[op_interface_impl]
impl ToLLVMValue for VAArgOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let opd = convert_value_operand(cctx, ctx, &op.get_operand(0))?;
        log::warn!("Generating va_arg instruction: It is poorly supported by LLVM");
        let vaarg_op = llvm_build_va_arg(
            &cctx.builder,
            opd,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        Ok(vaarg_op)
    }
}

/// Convert a pliron [BasicBlock] to [LLVMBasicBlock].
fn convert_block(
    ctx: &Context,
    llvm_ctx: &LLVMContext,
    cctx: &mut ConversionContext,
    block: Ptr<BasicBlock>,
) -> Result<()> {
    let block_llvm = cctx.block_map[&block];
    llvm_position_builder_at_end(&cctx.builder, block_llvm);

    for opr in block.deref(ctx).iter(ctx) {
        let op = Operation::get_op_dyn(opr, ctx);
        let op = op.as_ref();
        let Some(op_conv) = op_cast::<dyn ToLLVMValue>(op) else {
            let loc = op.loc(ctx);
            return input_err!(
                loc,
                ToLLVMErr::MissingOpConversion(op.get_opid().to_string())
            );
        };
        let op_llvm = op_conv.convert(ctx, llvm_ctx, cctx)?;
        let opr_ref = opr.deref(ctx);
        // LLVM instructions have at most one result.
        if opr_ref.get_num_results() == 1 {
            cctx.value_map.insert(opr_ref.get_result(0), op_llvm);
        }
    }

    Ok(())
}

/// Convert a pliron [FuncOp] to [LLVMValue]
fn convert_function(
    ctx: &Context,
    llvm_ctx: &LLVMContext,
    cctx: &mut ConversionContext,
    func_op: FuncOp,
) -> Result<LLVMValue> {
    cctx.clear_per_function_data();
    let func_llvm = cctx.function_map[&func_op.get_symbol_name(ctx)];

    if let Some(linkage) = func_op.get_attr_llvm_function_linkage(ctx) {
        let llvm_linkage: LLVMLinkage = convert_linkage(linkage.clone());
        llvm_set_linkage(func_llvm, llvm_linkage);
    }

    let f_region = func_op.get_region(ctx).expect("Function missing region");

    // Map all blocks, staring with entry.
    let mut block_iter = f_region.deref(ctx).iter(ctx);
    {
        let entry = block_iter.next().expect("Missing entry block");
        // Map entry block arguments to LLVM function arguments.
        for (arg_idx, arg) in entry.deref(ctx).arguments().enumerate() {
            cctx.value_map
                .insert(arg, llvm_get_param(func_llvm, arg_idx.try_into().unwrap()));
        }
        let llvm_entry_block = llvm_append_basic_block_in_context(
            llvm_ctx,
            func_llvm,
            &entry.deref(ctx).unique_name(ctx),
        );
        cctx.block_map.insert(entry, llvm_entry_block);
    }
    for block in block_iter {
        let llvm_block = llvm_append_basic_block_in_context(
            llvm_ctx,
            func_llvm,
            &block.deref(ctx).unique_name(ctx),
        );
        llvm_position_builder_at_end(&cctx.builder, llvm_block);
        for arg in block.deref(ctx).arguments() {
            let arg_type = convert_type(ctx, llvm_ctx, cctx, arg.get_type(ctx))?;
            let phi = llvm_build_phi(&cctx.builder, arg_type, &arg.unique_name(ctx));
            cctx.value_map.insert(arg, phi);
        }
        cctx.block_map.insert(block, llvm_block);
    }

    // Convert within every block.
    for block in topological_order(ctx, &f_region) {
        convert_block(ctx, llvm_ctx, cctx, block)?;
    }

    Ok(func_llvm)
}

#[op_interface]
trait ToLLVMConstValue {
    /// Convert from pliron [Op] to a constant [LLVMValue].
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue>;

    fn verify(_op: &dyn Op, _ctx: &Context) -> Result<()>
    where
        Self: Sized,
    {
        Ok(())
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for ConstantOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let value = self.get_value(ctx);
        if let Some(int_val) = value.downcast_ref::<IntegerAttr>() {
            let int_ty = int_val.get_type();
            let int_ty_llvm = convert_type(ctx, llvm_ctx, cctx, int_ty.into())?;
            let ap_int_val: APInt = int_val.clone().into();
            let const_val = llvm_const_int(int_ty_llvm, ap_int_val.to_u64(), false);
            Ok(const_val)
        } else if let Some(float_val) = attr_cast::<dyn FloatAttrToFP64>(&*value) {
            let float_ty = float_val.get_type(ctx);
            let float_ty_llvm = convert_type(ctx, llvm_ctx, cctx, float_ty)?;
            let const_val = llvm_const_real(float_ty_llvm, float_val.to_fp64());
            Ok(const_val)
        } else {
            input_err!(op.loc(), ToLLVMErr::ConstOpNotIntOrFloat)
        }
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for UndefOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        Ok(llvm_get_undef(ty))
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for PoisonOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        Ok(llvm_get_poison(ty))
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for ZeroOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;
        let zero_val = llvm_const_null(ty);
        Ok(zero_val)
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for AddressOfOp {
    fn convert(
        &self,
        ctx: &Context,
        _llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let sym = self.get_global_name(ctx);
        cctx.globals_map
            .get(&sym)
            .or_else(|| cctx.function_map.get(&sym))
            .cloned()
            .ok_or_else(|| input_error_noloc!(ToLLVMErr::CannotEvaluateToConst))
    }
}
#[op_interface_impl]
impl ToLLVMConstValue for InsertValueOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let base = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(0))?;
        let value = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(1))?;
        let indices = self.indices(ctx);
        if indices.len() != 1 {
            return input_err!(op.loc(), ToLLVMErr::InsertExtractValueIndices);
        }

        // LLVM's builder tries to fold this, so we rely on that.
        let insert_op = llvm_build_insert_value(
            &cctx.scratch_builder,
            base,
            value,
            indices[0],
            &self.get_result(ctx).unique_name(ctx),
        );
        if !llvm_is_a::constant(insert_op) {
            return input_err!(op.loc(), ToLLVMErr::CannotEvaluateToConst);
        }
        Ok(insert_op)
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for InsertElementOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let base = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(0))?;
        let value = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(1))?;
        let index = self.get_operand_index(ctx);
        let index = convert_to_llvm_const(ctx, cctx, llvm_ctx, index)?;

        // LLVM's builder tries to fold this, so we rely on that.
        let insert_op = llvm_build_insert_element(
            &cctx.scratch_builder,
            base,
            value,
            index,
            &self.get_result(ctx).unique_name(ctx),
        );
        if !llvm_is_a::constant(insert_op) {
            return input_err!(op.loc(), ToLLVMErr::CannotEvaluateToConst);
        }
        Ok(insert_op)
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for TruncOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;

        // LLVM's builder tries to fold this, so we rely on that.
        let trunc_op = llvm_build_trunc(
            &cctx.scratch_builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        if !llvm_is_a::constant(trunc_op) {
            return input_err!(op.loc(), ToLLVMErr::CannotEvaluateToConst);
        }
        Ok(trunc_op)
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for SubOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let lhs = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(0))?;
        let rhs = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(1))?;

        // LLVM's builder tries to fold this, so we rely on that.
        let sub_op = llvm_build_sub(
            &cctx.scratch_builder,
            lhs,
            rhs,
            &self.get_result(ctx).unique_name(ctx),
        );
        if !llvm_is_a::constant(sub_op) {
            return input_err!(op.loc(), ToLLVMErr::CannotEvaluateToConst);
        }
        Ok(sub_op)
    }
}

#[op_interface_impl]
impl ToLLVMConstValue for PtrToIntOp {
    fn convert(
        &self,
        ctx: &Context,
        llvm_ctx: &LLVMContext,
        cctx: &mut ConversionContext,
    ) -> Result<LLVMValue> {
        let op = self.get_operation().deref(ctx);
        let arg = convert_to_llvm_const(ctx, cctx, llvm_ctx, op.get_operand(0))?;
        let ty = convert_type(ctx, llvm_ctx, cctx, self.result_type(ctx))?;

        // LLVM's builder tries to fold this, so we rely on that.
        let ptoi_op = llvm_build_ptr_to_int(
            &cctx.scratch_builder,
            arg,
            ty,
            &self.get_result(ctx).unique_name(ctx),
        );
        if !llvm_is_a::constant(ptoi_op) {
            return input_err!(op.loc(), ToLLVMErr::CannotEvaluateToConst);
        }
        Ok(ptoi_op)
    }
}

fn convert_to_llvm_const(
    ctx: &Context,
    cctx: &mut ConversionContext,
    llvm_ctx: &LLVMContext,
    value: Value,
) -> Result<LLVMValue> {
    match value.defining_entity() {
        DefiningEntity::Op(op) => {
            let op = Operation::get_op_dyn(op, ctx);
            if let Some(const_trans) = op_cast::<dyn ToLLVMConstValue>(op.as_ref()) {
                const_trans.convert(ctx, llvm_ctx, cctx)
            } else {
                input_err!(value.loc(ctx), ToLLVMErr::CannotEvaluateToConst)
            }
        }
        DefiningEntity::Block(_) => {
            input_err!(value.loc(ctx), ToLLVMErr::CannotEvaluateToConst)
        }
    }
}

fn convert_global_initializer(
    ctx: &Context,
    llvm_ctx: &LLVMContext,
    cctx: &mut ConversionContext,
    global_op: GlobalOp,
) -> Result<Option<LLVMValue>> {
    if let Some(_initializer) = global_op.get_initializer_value(ctx) {
        todo!()
    }

    if let Some(init_block) = global_op.get_initializer_block(ctx) {
        let ret =
            Operation::get_op::<ReturnOp>(init_block.deref(ctx).get_terminator(ctx).unwrap(), ctx);
        let ret = ret.ok_or_else(|| {
            input_error!(
                global_op.loc(ctx),
                ToLLVMErr::GlobalOpInitializerRegionBadReturn
            )
        })?;
        let Some(ret_val) = ret.retval(ctx) else {
            return input_err!(
                global_op.loc(ctx),
                ToLLVMErr::GlobalOpInitializerRegionBadReturn
            );
        };
        let initializer_val = convert_to_llvm_const(ctx, cctx, llvm_ctx, ret_val)?;
        return Ok(Some(initializer_val));
    }

    Ok(None)
}

/// Convert pliron [ModuleOp] to [LLVMModule].
pub fn convert_module(
    ctx: &Context,
    llvm_ctx: &LLVMContext,
    module: ModuleOp,
) -> Result<LLVMModule> {
    let mod_name = module.get_symbol_name(ctx);
    let llvm_module = LLVMModule::new(&mod_name, llvm_ctx);
    let cctx = &mut ConversionContext::new(llvm_ctx, &llvm_module);

    // Setup the scratch builder for evaluating constants.
    // `scratch_module` is freed at the end of this function, when it exits the scope.
    let scratch_module = LLVMModule::new("__pliron_scratch_module", llvm_ctx);
    let scratch_function = llvm_add_function(
        &scratch_module,
        "scratch",
        llvm_function_type(llvm_void_type_in_context(llvm_ctx), &[], false),
    );
    let scratch_function_entry =
        llvm_append_basic_block_in_context(llvm_ctx, scratch_function, "entry");
    llvm_position_builder_at_end(&cctx.scratch_builder, scratch_function_entry);

    // Create new functions and map them.
    for op in module.get_body(ctx, 0).deref(ctx).iter(ctx) {
        if let Some(func_op) = Operation::get_op::<FuncOp>(op, ctx) {
            let func_ty = func_op.get_type(ctx).deref(ctx);
            let func_ty_to_llvm = type_cast::<dyn ToLLVMType>(&*func_ty).ok_or_else(|| {
                input_error_noloc!(ToLLVMErr::MissingTypeConversion(
                    func_ty.disp(ctx).to_string()
                ))
            })?;
            let fn_ty_llvm = func_ty_to_llvm.convert(ctx, llvm_ctx, cctx)?;
            let name = func_op.get_symbol_name(ctx);
            let llvm_name = func_op.llvm_symbol_name(ctx).unwrap_or(name.clone().into());
            let func_llvm = llvm_add_function(&llvm_module, &llvm_name, fn_ty_llvm);
            cctx.function_map.insert(name, func_llvm);
        }
        if let Some(global_op) = Operation::get_op::<GlobalOp>(op, ctx) {
            let global_ty = global_op.get_type(ctx);
            let global_ty_llvm = convert_type(ctx, llvm_ctx, cctx, global_ty)?;
            let global_name = global_op.get_symbol_name(ctx);
            let llvm_global_name = global_op
                .llvm_symbol_name(ctx)
                .unwrap_or(global_name.clone().into());
            let global_addr_space = global_op.address_space(ctx);
            let global_llvm = llvm_add_global_in_address_space(
                &llvm_module,
                global_ty_llvm,
                &llvm_global_name,
                global_addr_space,
            );
            cctx.globals_map.insert(global_name, global_llvm);
        }
    }

    for op in module.get_body(ctx, 0).deref(ctx).iter(ctx) {
        if let Some(func_op) = Operation::get_op::<FuncOp>(op, ctx)
            && !func_op.is_declaration(ctx)
        {
            convert_function(ctx, llvm_ctx, cctx, func_op)?;
        }
        if let Some(global_op) = Operation::get_op::<GlobalOp>(op, ctx) {
            let global_name = global_op.get_symbol_name(ctx);
            let global_llvm = cctx.globals_map[&global_name];
            if !global_op.is_declaration(ctx)
                && let Some(initializer) =
                    convert_global_initializer(ctx, llvm_ctx, cctx, global_op)?
            {
                llvm_set_initializer(global_llvm, initializer);
            }
            if let Some(linkage) = global_op.get_attr_llvm_global_linkage(ctx) {
                let llvm_linkage: LLVMLinkage = convert_linkage(linkage.clone());
                llvm_set_linkage(global_llvm, llvm_linkage);
            }
            if let Some(alignment) = global_op.alignment(ctx) {
                llvm_set_alignment(global_llvm, alignment);
            }
        }
    }

    Ok(llvm_module)
}