weld 0.4.0

Weld is a language and runtime for improving the performance of data-intensive applications.
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
//! A dictionary in Weld.
//!
//! # Overview
//!
//! Weld's dictionary is an open addressed, linearly probed hash table. The hash code is 32-bits
//! wide and can be any value. The dictionary methods take a hash code as input, so the hash value
//! should be generated before calling the dictionary.
//!
//! Dictionaries provide a number of key methods, whose semantics are described below.
//!
//! ### Lookup
//!
//!

use llvm_sys;

use std::ffi::CString;

use crate::error::*;

use self::llvm_sys::core::*;
use self::llvm_sys::prelude::*;
use self::llvm_sys::LLVMIntPredicate::*;
use self::llvm_sys::LLVMTypeKind;

use crate::codegen::llvm2::intrinsic::Intrinsics;
use crate::codegen::llvm2::llvm_exts::LLVMExtAttribute::*;
use crate::codegen::llvm2::llvm_exts::*;

// Need vector type for ToVec and Serialize.
use crate::codegen::llvm2::vector;
use crate::codegen::llvm2::vector::Vector;

use super::CodeGenExt;

/// Slot index of dictionary key.
const KEY_INDEX: u32 = 0;
/// Slot index of dictionary value.
const VALUE_INDEX: u32 = 1;
/// Slot index storing the 32-bit hash.
const HASH_INDEX: u32 = 2;
/// Slot index of whether a slot is filled (a single byte value).
///
/// The filled byte also indicates the *capacity* of a grouping vector in a dictionary, in a power
/// of two. The capacity of the vector is (1 << FILLED) - 1. If the filled byte is 0 (i.e., the
/// slot is not filled, the vector is uninitialized.
const FILLED_INDEX: u32 = 3;

// Dictionary Layout: { slot_array*, capacity, size }

/// Data index.
const SLOT_ARR_INDEX: u32 = 0;
/// Capacity index.
const CAP_INDEX: u32 = 1;
/// Size index.
const SIZE_INDEX: u32 = 2;

/// Maximum load factor of the dictionary (out of 10).
const MAX_LOAD_FACTOR: i64 = 7;
/// Initial capacity of the dictionary. Must be a power-of-two.
///
/// After the initial capacity is resized, the load factor should be less than the maximum allowed
/// load factor.
pub const INITIAL_CAPACITY: i64 = 16;

/// The default capacity of a grouping vector.
const DEFAULT_GROUP_CAPACITY: i64 = 8;
/// The default value of the filled byte for grouping dictionaries, after initialization.
///
/// (0x1 << DEFAULT_GROUP_FILLED) == DEFAULT_GROUP_CAPACITY.
const DEFAULT_GROUP_FILLED: i8 = 3;

/// A dictionary data structure and its associated methods.
///
/// This struct defines methods generated for a particular dictionary type. A dictionary type is
/// defined by its key and value types.
pub struct Dict {
    pub name: String,
    pub dict_ty: LLVMTypeRef,
    pub slot_ty: SlotType,
    key_comparator: LLVMValueRef,
    dict_inner_ty: LLVMTypeRef,
    context: LLVMContextRef,
    module: LLVMModuleRef,
    slot_for_key: Option<LLVMValueRef>, // DONE
    new: Option<LLVMValueRef>,          // DONE
    lookup: Option<LLVMValueRef>,       // DONE
    opt_lookup: Option<LLVMValueRef>,   // DONE
    upsert: Option<LLVMValueRef>,       // DONE
    resize: Option<LLVMValueRef>,       // DONE
    key_exists: Option<LLVMValueRef>,   // DONE
    to_vec: Option<LLVMValueRef>,       // DONE

    // For grouping
    merge_grouped: Option<LLVMValueRef>, // DONE
}

/// Extensions for grouping dictionaries (i.e., the GroupMerger).
///
/// The grouping dictionary supports grouping values into vectors. The same underlying Dict type is
/// used for a grouping dictionary, except a special merge function is used to add values to the
/// dictionaries.
///
/// It is *incorrect behavior* to use the `upsert` method on a grouping dictionary -- values should
/// be inserted using the methods in this trait instead.
pub trait GroupingDict {
    /// Merge `value` into the group for `key` with the given `hash`.
    ///
    /// This method takes a `Vector`, which holds methods for the type `vec[V]`.
    unsafe fn gen_merge_grouped(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        group_vector: &mut Vector,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
        value: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef>;
}

impl CodeGenExt for Dict {
    fn module(&self) -> LLVMModuleRef {
        self.module
    }

    fn context(&self) -> LLVMContextRef {
        self.context
    }
}

/// A dictionary slot.
///
/// This struct provides accessor methods local to a single dictionary slot.
pub struct SlotType {
    /// The type of the slot.
    slot_ty: LLVMTypeRef,
    /// The key type.
    key_ty: LLVMTypeRef,
    /// The value type.
    val_ty: LLVMTypeRef,
    module: LLVMModuleRef,
    context: LLVMContextRef,
}

impl CodeGenExt for SlotType {
    fn module(&self) -> LLVMModuleRef {
        self.module
    }

    fn context(&self) -> LLVMContextRef {
        self.context
    }
}

impl SlotType {
    unsafe fn new<T: AsRef<str>>(
        name: T,
        key_ty: LLVMTypeRef,
        val_ty: LLVMTypeRef,
        context: LLVMContextRef,
        module: LLVMModuleRef,
    ) -> SlotType {
        // Create a name for the dictionary.
        let c_name = CString::new(name.as_ref()).unwrap();

        // A slot is a a struct with { key, value, hash, filled }
        let mut layout = [
            key_ty,
            val_ty,
            LLVMInt32TypeInContext(context),
            LLVMInt8TypeInContext(context),
        ];

        // For consistency.
        debug_assert!(LLVMGetTypeKind(layout[KEY_INDEX as usize]) == LLVMGetTypeKind(key_ty));
        debug_assert!(LLVMGetTypeKind(layout[VALUE_INDEX as usize]) == LLVMGetTypeKind(val_ty));
        debug_assert!(
            LLVMGetTypeKind(layout[HASH_INDEX as usize]) == LLVMTypeKind::LLVMIntegerTypeKind
        );
        debug_assert!(
            LLVMGetTypeKind(layout[FILLED_INDEX as usize]) == LLVMTypeKind::LLVMIntegerTypeKind
        );

        let slot_ty = LLVMStructCreateNamed(context, c_name.as_ptr());
        LLVMStructSetBody(slot_ty, layout.as_mut_ptr(), layout.len() as u32, 0);

        SlotType {
            slot_ty,
            key_ty,
            val_ty,
            context,
            module,
        }
    }

    /// Initialize a slot by marking it as filled and setting the key/value.
    ///
    /// The slot should be a pointer, and the key and value should be loaded (i.e., should have
    /// type `key_ty` and `val_ty`).
    unsafe fn init(
        &mut self,
        builder: LLVMBuilderRef,
        slot: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
        value: LLVMValueRef,
    ) {
        let key_pointer = self.key(builder, slot);
        LLVMBuildStore(builder, key, key_pointer);
        let value_pointer = self.value(builder, slot);
        LLVMBuildStore(builder, value, value_pointer);
        let hash_pointer = self.hash(builder, slot);
        LLVMBuildStore(builder, hash, hash_pointer);
        let filled_pointer = LLVMBuildStructGEP(builder, slot, FILLED_INDEX, c_str!(""));
        LLVMBuildStore(builder, self.i8(1), filled_pointer);
    }

    /// Return the key pointer for the provided slot.
    ///
    /// The slot should be a pointer.
    pub unsafe fn key(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        LLVMBuildStructGEP(builder, value, KEY_INDEX, c_str!("slot.key"))
    }

    /// Return the value pointer for the provide slot.
    ///
    /// The slot should be a pointer.
    pub unsafe fn value(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        LLVMBuildStructGEP(builder, value, VALUE_INDEX, c_str!("slot.value"))
    }

    /// Return the hash pointer for the provide slot.
    ///
    /// The slot should be a pointer.
    pub unsafe fn hash(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        LLVMBuildStructGEP(builder, value, HASH_INDEX, c_str!("slot.hash"))
    }

    /// Return whether the provided slot is filled as an `i1`.
    ///
    /// The slot is filled if the byte value of the filled field is non-zero.
    ///
    /// The slot should be a pointer.
    pub unsafe fn filled(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        let filled_pointer = LLVMBuildStructGEP(builder, value, FILLED_INDEX, c_str!(""));
        let filled = self.load(builder, filled_pointer).unwrap();
        LLVMBuildICmp(
            builder,
            LLVMIntNE,
            filled,
            self.i8(0),
            c_str!("slot.filled"),
        )
    }

    /// Return the filled value (used for grouping dictionaries).
    ///
    /// The slot should be a pointer.
    pub unsafe fn filled_value(
        &mut self,
        builder: LLVMBuilderRef,
        value: LLVMValueRef,
    ) -> LLVMValueRef {
        let filled_pointer = LLVMBuildStructGEP(builder, value, FILLED_INDEX, c_str!(""));
        LLVMBuildLoad(builder, filled_pointer, c_str!("slot.filledValue"))
    }

    /// Set the filled value to a value between 1 and 255.
    ///
    /// The filled value represents the capacity of a grouping vector in a grouping dictionary. A
    /// nonzero filled value indicates an initialized slot.
    ///
    /// NOTE: It is invalid to "unfill" a slot by changing it from a nonzero value to a zero value.
    /// We could add a debug assertion here to check that this never happens.
    pub unsafe fn set_filled(
        &mut self,
        builder: LLVMBuilderRef,
        slot: LLVMValueRef,
        value: LLVMValueRef,
    ) {
        let filled_pointer = LLVMBuildStructGEP(builder, slot, FILLED_INDEX, c_str!(""));
        LLVMBuildStore(builder, value, filled_pointer);
    }
}

impl Dict {
    /// Define a new dictionary type.
    ///
    /// The definition requires a key and a value type.
    pub unsafe fn define<T: AsRef<str>>(
        name: T,
        key_ty: LLVMTypeRef,
        key_comparator: LLVMValueRef,
        val_ty: LLVMTypeRef,
        context: LLVMContextRef,
        module: LLVMModuleRef,
    ) -> Dict {
        let c_name = CString::new(name.as_ref()).unwrap();
        let slot_ty = SlotType::new(
            format!("{}.slot", name.as_ref()),
            key_ty,
            val_ty,
            context,
            module,
        );

        // A dictionary holds an array of slots along with a capacity and size.
        let mut layout = [
            LLVMPointerType(slot_ty.slot_ty, 0),
            LLVMInt64TypeInContext(context),
            LLVMInt64TypeInContext(context),
        ];

        debug_assert!(
            LLVMGetTypeKind(layout[SLOT_ARR_INDEX as usize]) == LLVMTypeKind::LLVMPointerTypeKind
        );
        debug_assert!(
            LLVMGetTypeKind(layout[CAP_INDEX as usize]) == LLVMTypeKind::LLVMIntegerTypeKind
        );
        debug_assert!(
            LLVMGetTypeKind(layout[SIZE_INDEX as usize]) == LLVMTypeKind::LLVMIntegerTypeKind
        );

        let dict_inner_ty = LLVMStructCreateNamed(context, c_name.as_ptr());
        LLVMStructSetBody(dict_inner_ty, layout.as_mut_ptr(), layout.len() as u32, 0);

        let name = c_name.into_string().unwrap();

        Dict {
            name,
            dict_ty: LLVMPointerType(dict_inner_ty, 0),
            dict_inner_ty,
            key_comparator,
            slot_ty,
            context,
            module,
            slot_for_key: None,
            new: None,
            lookup: None,
            opt_lookup: None,
            upsert: None,
            resize: None,
            key_exists: None,
            to_vec: None,
            merge_grouped: None,
        }
    }

    unsafe fn slot_array(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        let slot_array_ptr = LLVMBuildStructGEP(builder, value, SLOT_ARR_INDEX, c_str!(""));
        LLVMBuildLoad(builder, slot_array_ptr, c_str!("dict.slotarray"))
    }

    unsafe fn capacity(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        let capacity_ptr = LLVMBuildStructGEP(builder, value, CAP_INDEX, c_str!(""));
        LLVMBuildLoad(builder, capacity_ptr, c_str!("dict.capacity"))
    }

    unsafe fn size(&mut self, builder: LLVMBuilderRef, value: LLVMValueRef) -> LLVMValueRef {
        let size_ptr = LLVMBuildStructGEP(builder, value, SIZE_INDEX, c_str!(""));
        LLVMBuildLoad(builder, size_ptr, c_str!("dict.size"))
    }

    /// Returns the probe index for an index value by computing the mod.
    ///
    /// For a power-of-2 hash table, this is `value & (capacity - 1)`.
    unsafe fn probe_index(
        &mut self,
        builder: LLVMBuilderRef,
        value: LLVMValueRef,
        capacity: LLVMValueRef,
    ) -> LLVMValueRef {
        let tmp = LLVMBuildSub(builder, capacity, self.i64(1), c_str!(""));
        LLVMBuildAnd(builder, value, tmp, c_str!(""))
    }

    /// Returns whether two keys are equal using their equality function.
    unsafe fn compare_keys(
        &mut self,
        builder: LLVMBuilderRef,
        slot_key: LLVMValueRef,
        key: LLVMValueRef,
    ) -> LLVMValueRef {
        let mut args = [slot_key, key];
        LLVMBuildCall(
            builder,
            self.key_comparator,
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }

    /// Returns the slot pointer at a given index into the slot array.
    unsafe fn slot_at_index(
        &mut self,
        builder: LLVMBuilderRef,
        slot_array: LLVMValueRef,
        index: LLVMValueRef,
    ) -> LLVMValueRef {
        LLVMBuildGEP(builder, slot_array, [index].as_mut_ptr(), 1, c_str!(""))
    }

    /// Returns an `i1` determining whether the dictionary should be resized.
    unsafe fn should_resize(
        &mut self,
        builder: LLVMBuilderRef,
        size: LLVMValueRef,
        capacity: LLVMValueRef,
    ) -> LLVMValueRef {
        let size = LLVMBuildNSWMul(builder, size, self.i64(10), c_str!(""));
        let capacity = LLVMBuildNSWMul(builder, capacity, self.i64(MAX_LOAD_FACTOR), c_str!(""));
        LLVMBuildICmp(builder, LLVMIntSGE, size, capacity, c_str!(""))
    }

    /// Returns a new dictionary with the given capacity.
    unsafe fn gen_new_dict_with_capacity(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        capacity: LLVMValueRef,
        run: LLVMValueRef,
    ) -> LLVMValueRef {
        let alloc_size = LLVMBuildNSWMul(
            builder,
            capacity,
            self.size_of(self.slot_ty.slot_ty),
            c_str!(""),
        );
        let bytes = intrinsics.call_weld_run_malloc(builder, run, alloc_size, None);
        let _ = intrinsics.call_memset_zero(builder, bytes, alloc_size);
        let slot_array = LLVMBuildBitCast(
            builder,
            bytes,
            LLVMPointerType(self.slot_ty.slot_ty, 0),
            c_str!(""),
        );

        let mut dict = LLVMGetUndef(self.dict_inner_ty);
        dict = LLVMBuildInsertValue(builder, dict, slot_array, SLOT_ARR_INDEX, c_str!(""));
        dict = LLVMBuildInsertValue(builder, dict, capacity, CAP_INDEX, c_str!(""));
        dict = LLVMBuildInsertValue(builder, dict, self.i64(0), SIZE_INDEX, c_str!(""));
        dict
    }

    /// Resize the dictionary if necessary.
    ///
    /// Returns an `i1` indicating whether a resize occurred. If the dictionary was resized, slots
    /// acquired from the dictionary should be re-acquired.
    unsafe fn gen_resize(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        dict: LLVMValueRef,
        run: LLVMValueRef,
    ) -> LLVMValueRef {
        if self.resize.is_none() {
            let mut arg_tys = [self.dict_ty, self.run_handle_type()];
            let ret_ty = self.i1_type();
            let name = format!("{}.resize", self.name);

            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, NonNull], 1);

            let dict = LLVMGetParam(function, 0);
            let run = LLVMGetParam(function, 1);

            // Type of the alloca is same as `self.dict_ty`
            let new_dictionary = LLVMBuildAlloca(builder, self.dict_inner_ty, c_str!(""));

            let old_size = self.size(builder, dict);
            let old_capacity = self.capacity(builder, dict);
            let old_slot_array = self.slot_array(builder, dict);

            // Resize
            let resize_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            // Copy loop.
            let copy_top_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let copy_cpy_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            // Probe for empty slot.
            let probe_top_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let probe_chk_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let probe_bot_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            // End of copy loop.
            let copy_bot_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let copy_fin_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            // Exit
            let exit_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            let should_resize = self.should_resize(builder, old_size, old_capacity);
            LLVMBuildCondBr(builder, should_resize, resize_block, exit_block);

            LLVMPositionBuilderAtEnd(builder, resize_block);

            // Resize the dictionary -- double it capacity.
            let new_capacity = LLVMBuildNSWMul(builder, old_capacity, self.i64(2), c_str!(""));
            let new_dictionary_inner =
                self.gen_new_dict_with_capacity(builder, intrinsics, new_capacity, run);
            LLVMBuildStore(builder, new_dictionary_inner, new_dictionary);

            let new_slot_array = self.slot_array(builder, new_dictionary);
            LLVMBuildBr(builder, copy_top_block);

            // Loop to copy slots:
            //
            // top:
            //  i = phi [ resizeblock, 0 ], [ bottom, i2 ]
            //  slot = slotatindex(i)
            //  br slot.filled ? copyblock : bottom
            // cpy:
            //  newslot = getempty(new_dictionary, slot.hash) XXX geherated with gen_probe_loop
            //  memcpy(slot, newslot, sizeof(slot))
            //  br bot
            // bot:
            //  i2 = i + 1
            //  br i2 == capacity : fin ? top
            // fin:
            //  free(dict.slot_array)
            //  memcpy(dict, new_dictionary, sizeof(dict))
            //  br exit
            // exit:
            //  ...

            // Top Block.
            LLVMPositionBuilderAtEnd(builder, copy_top_block);
            let index = LLVMBuildPhi(builder, self.i64_type(), c_str!(""));
            let old_slot = self.slot_at_index(builder, old_slot_array, index);
            let filled = self.slot_ty.filled(builder, old_slot);
            LLVMBuildCondBr(builder, filled, copy_cpy_block, copy_bot_block);

            // Copy Block.
            LLVMPositionBuilderAtEnd(builder, copy_cpy_block);
            let hash_pointer = self.slot_ty.hash(builder, old_slot);
            let old_slot_hash = self.load(builder, hash_pointer).unwrap();
            let new_slot = self.gen_probe_loop(
                builder,
                new_slot_array,
                new_capacity,
                old_slot_hash,
                (
                    copy_cpy_block,
                    probe_top_block,
                    probe_chk_block,
                    probe_bot_block,
                ),
                None,
            );

            let new_slot_bytes =
                LLVMBuildBitCast(builder, new_slot, self.void_pointer_type(), c_str!(""));
            let old_slot_bytes =
                LLVMBuildBitCast(builder, old_slot, self.void_pointer_type(), c_str!(""));
            let _ = intrinsics.call_memcpy(
                builder,
                new_slot_bytes,
                old_slot_bytes,
                self.size_of(self.slot_ty.slot_ty),
            );

            // Update the size.
            let size_pointer = LLVMBuildStructGEP(builder, new_dictionary, SIZE_INDEX, c_str!(""));
            let size = self.load(builder, size_pointer).unwrap();
            let new_size = LLVMBuildNSWAdd(builder, size, self.i64(1), c_str!(""));
            LLVMBuildStore(builder, new_size, size_pointer);
            LLVMBuildBr(builder, copy_bot_block);

            // Bottom Block.
            LLVMPositionBuilderAtEnd(builder, copy_bot_block);
            let update_index = LLVMBuildNSWAdd(builder, index, self.i64(1), c_str!(""));
            let finished =
                LLVMBuildICmp(builder, LLVMIntEQ, update_index, old_capacity, c_str!(""));
            LLVMBuildCondBr(builder, finished, copy_fin_block, copy_top_block);

            // Set the PHI value for the index.
            let mut blocks = [resize_block, copy_bot_block];
            let mut values = [self.i64(0), update_index];
            LLVMAddIncoming(
                index,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            // Finish Block.
            LLVMPositionBuilderAtEnd(builder, copy_fin_block);
            // 1. Free Old Dictionary buffer.
            let old_slotarray_bytes = LLVMBuildBitCast(
                builder,
                old_slot_array,
                self.void_pointer_type(),
                c_str!(""),
            );
            let _ = intrinsics.call_weld_run_free(builder, run, old_slotarray_bytes);

            // 2. Move the new dictionary state to the old dictionary pointer.
            let new_dict_bytes = LLVMBuildBitCast(
                builder,
                new_dictionary,
                self.void_pointer_type(),
                c_str!(""),
            );
            let old_dict_bytes =
                LLVMBuildBitCast(builder, dict, self.void_pointer_type(), c_str!(""));
            let _ = intrinsics.call_memcpy(
                builder,
                old_dict_bytes,
                new_dict_bytes,
                self.size_of(self.dict_inner_ty),
            );
            LLVMBuildBr(builder, exit_block);

            // Finished at last! Hallelujah. Who would've thought resizing a dictionary is so hard.
            LLVMPositionBuilderAtEnd(builder, exit_block);
            LLVMBuildRet(builder, should_resize);

            self.resize = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, run];
        LLVMBuildCall(
            builder,
            self.resize.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }

    /// Generate a probe loop. The probe loop uses the provided hash to search the hash table using
    /// linear probing. If a key is provided, either the matching slot or an empty slot is
    /// returned. If a key is not provided, the first empty slot starting at the hash's index is
    /// returned.
    ///
    /// This method takes four basic blocks for loop control flow: the basic block the builder is
    /// currently positioned in, a top block, a check block to check for the key, and an exit
    /// block.
    ///
    /// When the function returns, the builder is guaranteed to be positioned at the beginning of the
    /// exit block. The function returns the LLVMValue representing the slot found with the probe.
    unsafe fn gen_probe_loop(
        &mut self,
        builder: LLVMBuilderRef,
        slot_array: LLVMValueRef,
        capacity: LLVMValueRef,
        hash: LLVMValueRef,
        blocks: (
            LLVMBasicBlockRef,
            LLVMBasicBlockRef,
            LLVMBasicBlockRef,
            LLVMBasicBlockRef,
        ),
        key: Option<LLVMValueRef>,
    ) -> LLVMValueRef {
        // Generated Code:
        //
        //   start = probe_index(hash, cap);
        //   jump top
        // top:
        //   i = phi [ start, top ] [ i2, check_key ]
        //   s = getslot(i)
        //   br s.filled ? check_key : return
        // check_key:
        //   tmp = i + 1
        //   i2 = probe_index(tmp)
        //   br s.key == key ? return : top | br top, depending on `check_key`
        // return:
        //   return s
        // }

        let (start_block, top_block, check_block, end_block) = blocks;

        let hash = LLVMBuildZExt(builder, hash, self.i64_type(), c_str!(""));
        let start_index = self.probe_index(builder, hash, capacity);
        LLVMBuildBr(builder, top_block);

        LLVMPositionBuilderAtEnd(builder, top_block);
        let index = LLVMBuildPhi(builder, self.i64_type(), c_str!(""));
        let slot = self.slot_at_index(builder, slot_array, index);

        let filled = self.slot_ty.filled(builder, slot);
        LLVMBuildCondBr(builder, filled, check_block, end_block);

        LLVMPositionBuilderAtEnd(builder, check_block);
        let update_index = LLVMBuildNSWAdd(builder, index, self.i64(1), c_str!(""));
        let update_index = self.probe_index(builder, update_index, capacity);
        if let Some(key) = key {
            let slot_key = self.slot_ty.key(builder, slot);
            let keys_eq = self.compare_keys(builder, slot_key, key);
            LLVMBuildCondBr(builder, keys_eq, end_block, top_block);
        } else {
            LLVMBuildBr(builder, top_block);
        }

        let mut blocks = [start_block, check_block];
        let mut values = [start_index, update_index];
        LLVMAddIncoming(
            index,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        LLVMPositionBuilderAtEnd(builder, end_block);
        slot
    }

    /// Return the slot for a given key. The returned slot may be uninitialized if the key does not
    /// exist in the dictionary.
    ///
    /// The current algorithm assumes a power-of-two hash table and performs standard linear
    /// probing.
    unsafe fn gen_slot_for_key(
        &mut self,
        builder: LLVMBuilderRef,
        slot_array: LLVMValueRef,
        capacity: LLVMValueRef,
        hash: LLVMValueRef,
        key: LLVMValueRef,
    ) -> LLVMValueRef {
        if self.slot_for_key.is_none() {
            let mut arg_tys = [
                LLVMPointerType(self.slot_ty.slot_ty, 0),
                self.i64_type(),
                self.i32_type(),
                LLVMPointerType(self.slot_ty.key_ty, 0),
            ];
            let ret_ty = LLVMPointerType(self.slot_ty.slot_ty, 0);
            let name = format!("{}.slot_for_key", self.name);

            let (function, builder, entry_block) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, ReadOnly], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, ReadOnly], 3);

            let slot_array = LLVMGetParam(function, 0);
            let capacity = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);
            let key = LLVMGetParam(function, 3);

            let top_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let check_key_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let return_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            let slot = self.gen_probe_loop(
                builder,
                slot_array,
                capacity,
                hash,
                (entry_block, top_block, check_key_block, return_block),
                Some(key),
            );

            LLVMBuildRet(builder, slot);

            self.slot_for_key = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [slot_array, capacity, hash, key];
        LLVMBuildCall(
            builder,
            self.slot_for_key.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        )
    }
}

/// Public API.
impl Dict {
    /// Create a new dictionary.
    ///
    /// Dictionaries are hidden behind pointers so their ABI type is always a `void*`.
    pub unsafe fn gen_new(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        capacity: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.new.is_none() {
            let mut arg_tys = [self.i64_type(), self.run_handle_type()];
            let ret_ty = self.dict_ty;
            let name = format!("{}.new", self.name);

            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnReturn(self.context, function, &[NoAlias]);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, NonNull], 1);

            let capacity = LLVMGetParam(function, 0);
            let run = LLVMGetParam(function, 1);

            // Use a minimum initial capacity.
            let capacity_too_small = LLVMBuildICmp(
                builder,
                LLVMIntSGT,
                self.i64(INITIAL_CAPACITY),
                capacity,
                c_str!(""),
            );
            let capacity = LLVMBuildSelect(
                builder,
                capacity_too_small,
                self.i64(INITIAL_CAPACITY),
                capacity,
                c_str!(""),
            );
            let dict_inner = self.gen_new_dict_with_capacity(builder, intrinsics, capacity, run);

            // Wrap the dictionary in a pointer - the external view of a dictionary is always a
            // pointer so its easier to change the internal layout.
            let alloc_size = self.size_of(self.dict_inner_ty);
            let bytes = intrinsics.call_weld_run_malloc(builder, run, alloc_size, None);
            let dict_pointer = LLVMBuildBitCast(builder, bytes, self.dict_ty, c_str!(""));
            LLVMBuildStore(builder, dict_inner, dict_pointer);
            LLVMBuildRet(builder, dict_pointer);

            LLVMDisposeBuilder(builder);
            self.new = Some(function);
        }

        let mut args = [capacity, run];
        Ok(LLVMBuildCall(
            builder,
            self.new.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Returns the pointer to the slot for a key.
    ///
    /// If the key does not exist, a slot is initialized for the key, and a default value is
    /// inserted into the slot. The slot is then returned. The insertion may cause the dictionary
    /// to be resized.
    pub unsafe fn gen_upsert(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
        default: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.upsert.is_none() {
            let mut arg_tys = [
                self.dict_ty,
                LLVMPointerType(self.slot_ty.key_ty, 0),
                self.hash_type(),
                self.slot_ty.val_ty,
                self.run_handle_type(),
            ];

            // Generated Code:
            //
            // slot_array = slot.slot_array
            // capacity = slot.capacity
            // slot = slot_for_key(slot, capacity, hash, key)
            //
            // br slot.filled ? return : set_default
            //
            // set_default:
            // key = load keypointer
            // slot.key = key           |
            // slot.hash = hash         |-- slot.init
            // slot.filled = filled     |
            // dict.size = size + 1
            // resized = dict.resize(dict)
            // br resized ? reacquire : upsert
            //
            // reacquire:
            // resized_slot = slot_for_key(slot, capacity, hash, key)
            // br upsert
            //
            // upsert:
            // upsert_slot = phi [ slot, setdefault ] [ resized_slot, require ]
            // upsert_slot.value = default
            // br return
            //
            // return:
            // return_slot = phi [ slot, entry ] [ upsert_slot, upsert ]

            let ret_ty = LLVMPointerType(self.slot_ty.slot_ty, 0);

            let name = format!("{}.upsert", self.name);
            let (function, builder, entry_block) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, NonNull], 0);
            LLVMExtAddAttrsOnParameter(
                self.context,
                function,
                &[NoAlias, NoCapture, NonNull, ReadOnly],
                1,
            );
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, NoCapture, NonNull], 4);

            let set_default_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let reacquire_slot_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let upsert_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));
            let return_block = LLVMAppendBasicBlockInContext(self.context(), function, c_str!(""));

            let dict = LLVMGetParam(function, 0);
            let key = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);
            let default = LLVMGetParam(function, 3);
            let run = LLVMGetParam(function, 4);

            let slot_array = self.slot_array(builder, dict);
            let capacity = self.capacity(builder, dict);
            let slot = self.gen_slot_for_key(builder, slot_array, capacity, hash, key);

            let filled = self.slot_ty.filled(builder, slot);
            LLVMBuildCondBr(builder, filled, return_block, set_default_block);

            LLVMPositionBuilderAtEnd(builder, set_default_block);
            let key_loaded = self.load(builder, key).unwrap();

            self.slot_ty.init(builder, slot, key_loaded, hash, default);

            let size_pointer = LLVMBuildStructGEP(builder, dict, SIZE_INDEX, c_str!(""));
            let size = self.load(builder, size_pointer).unwrap();
            let new_size = LLVMBuildNSWAdd(builder, size, self.i64(1), c_str!(""));
            LLVMBuildStore(builder, new_size, size_pointer);

            // Check for resize.
            let resized = self.gen_resize(builder, intrinsics, dict, run);
            LLVMBuildCondBr(builder, resized, reacquire_slot_block, upsert_block);

            LLVMPositionBuilderAtEnd(builder, reacquire_slot_block);
            // Builder was resized - we need to reacquire the slot.
            let resized_slot_array = self.slot_array(builder, dict);
            let resized_capacity = self.capacity(builder, dict);
            let resized_slot =
                self.gen_slot_for_key(builder, resized_slot_array, resized_capacity, hash, key);

            LLVMBuildBr(builder, upsert_block);
            LLVMPositionBuilderAtEnd(builder, upsert_block);

            let slot_pointer_ty = LLVMPointerType(self.slot_ty.slot_ty, 0);

            let upsert_slot = LLVMBuildPhi(builder, slot_pointer_ty, c_str!(""));
            let value_pointer = self.slot_ty.value(builder, upsert_slot);
            LLVMBuildStore(builder, default, value_pointer);
            LLVMBuildBr(builder, return_block);

            LLVMPositionBuilderAtEnd(builder, return_block);
            let return_slot = LLVMBuildPhi(builder, ret_ty, c_str!(""));
            LLVMBuildRet(builder, return_slot);

            // Set the PHI values.
            let mut blocks = [set_default_block, reacquire_slot_block];
            let mut values = [slot, resized_slot];
            LLVMAddIncoming(
                upsert_slot,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            let mut blocks = [entry_block, upsert_block];
            let mut values = [slot, upsert_slot];
            LLVMAddIncoming(
                return_slot,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            self.upsert = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, key, hash, default, run];
        Ok(LLVMBuildCall(
            builder,
            self.upsert.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Returns the slot for a key.
    ///
    /// If the key is not in the hash table, returns an uninitialized slot. It is *invalid
    /// behavior* to modify an uninitialized slothe caller should observe the `filled` value of
    /// the slot to see whether it is initialized.
    pub unsafe fn gen_opt_lookup(
        &mut self,
        builder: LLVMBuilderRef,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.opt_lookup.is_none() {
            let mut arg_tys = [
                self.dict_ty,
                LLVMPointerType(self.slot_ty.key_ty, 0),
                self.hash_type(),
            ];
            let ret_ty = LLVMPointerType(self.slot_ty.slot_ty, 0);

            let name = format!("{}.optlookup", self.name);
            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 1);

            let dict = LLVMGetParam(function, 0);
            let key = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);

            let slot_array = self.slot_array(builder, dict);
            let capacity = self.capacity(builder, dict);
            let slot = self.gen_slot_for_key(builder, slot_array, capacity, hash, key);
            LLVMBuildRet(builder, slot);

            self.opt_lookup = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, key, hash];
        Ok(LLVMBuildCall(
            builder,
            self.opt_lookup.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Returns the pointer to the slot for a key.
    ///
    /// If the key does not exist, throws an KeyNotFoundError.
    ///
    /// It is *invalid* to change a filled slot to be unfilled (i.e., setting a non-zero filled
    /// value to 0).
    pub unsafe fn gen_lookup(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        use crate::runtime::WeldRuntimeErrno::KeyNotFoundError;

        if self.lookup.is_none() {
            let mut arg_tys = [
                self.dict_ty,
                LLVMPointerType(self.slot_ty.key_ty, 0),
                self.hash_type(),
                self.run_handle_type(),
            ];
            let ret_ty = LLVMPointerType(self.slot_ty.slot_ty, 0);

            let name = format!("{}.lookup", self.name);
            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 1);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 3);

            let crash_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("keynotfound"));
            let return_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("return"));

            let dict = LLVMGetParam(function, 0);
            let key = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);
            let run = LLVMGetParam(function, 3);

            let slot_array = self.slot_array(builder, dict);
            let capacity = self.capacity(builder, dict);
            let slot = self.gen_slot_for_key(builder, slot_array, capacity, hash, key);

            let filled = self.slot_ty.filled(builder, slot);
            LLVMBuildCondBr(builder, filled, return_block, crash_block);

            // Crash if the key is not found.
            LLVMPositionBuilderAtEnd(builder, crash_block);
            let error = self.i64(KeyNotFoundError as i64);
            intrinsics.call_weld_run_set_errno(builder, run, error, None);
            LLVMBuildUnreachable(builder);

            LLVMPositionBuilderAtEnd(builder, return_block);
            LLVMBuildRet(builder, slot);

            self.lookup = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, key, hash, run];
        Ok(LLVMBuildCall(
            builder,
            self.lookup.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Returns whether a key exists.
    ///
    /// TODO This expression may become deprecated if Lookup returns a boolean to indicate whether
    /// a value is contained within a dictionary.
    pub unsafe fn gen_key_exists(
        &mut self,
        builder: LLVMBuilderRef,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.key_exists.is_none() {
            let mut arg_tys = [
                self.dict_ty,
                LLVMPointerType(self.slot_ty.key_ty, 0),
                self.hash_type(),
            ];

            let ret_ty = self.bool_type(); // LLVMPointerType(self.slot_ty.slot_ty, 0);

            let name = format!("{}.keyexists", self.name);
            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 1);

            let dict = LLVMGetParam(function, 0);
            let key = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);

            let slot_array = self.slot_array(builder, dict);
            let capacity = self.capacity(builder, dict);
            let slot = self.gen_slot_for_key(builder, slot_array, capacity, hash, key);

            let filled = self.slot_ty.filled(builder, slot);
            let filled = self.i1_to_bool(builder, filled);
            LLVMBuildRet(builder, filled);

            self.key_exists = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, key, hash];
        Ok(LLVMBuildCall(
            builder,
            self.key_exists.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Returns the number of keys in the dictionary.
    pub unsafe fn gen_size(
        &mut self,
        builder: LLVMBuilderRef,
        dict: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        Ok(self.size(builder, dict))
    }

    /// Converts this dictionary to a vector of key/value pairs.
    pub unsafe fn gen_to_vec(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        kv_vector: &mut Vector,
        dict: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.to_vec.is_none() {
            let mut arg_tys = [self.dict_ty, self.run_handle_type()];
            let ret_ty = kv_vector.vector_ty;

            let name = format!("{}.tovec", self.name);
            let (function, builder, entry_block) = self.define_function(ret_ty, &mut arg_tys, name);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias, ReadOnly], 1);

            let dict = LLVMGetParam(function, 0);
            let run = LLVMGetParam(function, 1);

            // Generated Code:
            //
            // size = dict.size
            // size_nonzero = size > 0
            // br size_nonzero, makekvvec, return0
            //
            // makekvvec:
            // vec = vec.new(size)
            // capacity = dict.capacity
            // br top
            //
            // top:
            // i = [makkvvec, 0], [other, i2]
            // j = [makkvvec, 0], [other, j3]
            // slot = getslot(i)
            // br slot.filled ? tokv : bot
            //
            // tokv:
            // vec_ptr = gep vec, j
            // key_ptr = gep vecptr, 0
            // store slot.key into key_ptr
            // val_ptr = gep vecptr, 1
            // store slot.val into val_ptr
            // j2 = j+1
            // br bot
            //
            // bot:
            // j3 = phi [tokv, j2], [top, j]
            // i++
            // finished = i == capacity
            // br finished ? top : done
            //
            // done:
            // ret = phi [ bot, vec ], [ entry, zeroinitializer]
            //

            // A constant zero-vector.
            let mut zero_vector = LLVMGetUndef(kv_vector.vector_ty);
            zero_vector = LLVMConstInsertValue(
                zero_vector,
                self.null_ptr(kv_vector.elem_ty),
                [vector::POINTER_INDEX].as_mut_ptr(),
                1,
            );
            zero_vector = LLVMConstInsertValue(
                zero_vector,
                self.i64(0),
                [vector::SIZE_INDEX].as_mut_ptr(),
                1,
            );

            let after_nullcheck_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("after.nullcheck"));
            let start_convert_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("start.convert"));
            let top_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("loop.top"));
            let copy_kv_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("copy.kv"));
            let bot_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("loop.bot"));
            let return_block =
                LLVMAppendBasicBlockInContext(self.context(), function, c_str!("return"));

            // Hack: For uninitalized dictionaries (which *must* be null pointers), return a
            // zero-vector.
            let is_null = LLVMBuildICmp(
                builder,
                LLVMIntEQ,
                dict,
                self.null_ptr(self.dict_inner_ty),
                c_str!("isNull"),
            );
            LLVMBuildCondBr(builder, is_null, return_block, after_nullcheck_block);

            LLVMPositionBuilderAtEnd(builder, after_nullcheck_block);
            let size = self.size(builder, dict);

            let size_nonzero = LLVMBuildICmp(builder, LLVMIntSGT, size, self.i64(0), c_str!(""));
            LLVMBuildCondBr(builder, size_nonzero, start_convert_block, return_block);

            LLVMPositionBuilderAtEnd(builder, start_convert_block);
            let capacity = self.capacity(builder, dict);
            let slot_array = self.slot_array(builder, dict);
            let vec = kv_vector.gen_new(builder, intrinsics, run, size)?;
            LLVMBuildBr(builder, top_block);

            LLVMPositionBuilderAtEnd(builder, top_block);
            // Index into the dictionary slot array.
            let slot_arr_index = LLVMBuildPhi(builder, self.i64_type(), c_str!(""));
            // Index into the vector.
            let kv_vec_index = LLVMBuildPhi(builder, self.i64_type(), c_str!(""));

            let slot = self.slot_at_index(builder, slot_array, slot_arr_index);
            let filled = self.slot_ty.filled(builder, slot);
            LLVMBuildCondBr(builder, filled, copy_kv_block, bot_block);

            // Copy block - copy the key/value into the vector.
            LLVMPositionBuilderAtEnd(builder, copy_kv_block);
            // Pointer to KV struct at the correct index.
            let vector_ptr = kv_vector.gen_at(builder, vec, kv_vec_index)?;

            // Copy the key.
            let vec_key_ptr = LLVMBuildStructGEP(builder, vector_ptr, 0, c_str!(""));
            let slot_key_ptr = self.slot_ty.key(builder, slot);
            let slot_key = self.load(builder, slot_key_ptr).unwrap();
            LLVMBuildStore(builder, slot_key, vec_key_ptr);

            // Copy the value.
            let vec_val_ptr = LLVMBuildStructGEP(builder, vector_ptr, 1, c_str!(""));
            let slot_val_ptr = self.slot_ty.value(builder, slot);
            let slot_val = self.load(builder, slot_val_ptr).unwrap();
            LLVMBuildStore(builder, slot_val, vec_val_ptr);

            let inc_kv_vec_index = LLVMBuildNSWAdd(builder, kv_vec_index, self.i64(1), c_str!(""));
            LLVMBuildBr(builder, bot_block);

            // Bottom of loop -- increment induction variables and loop or exit.
            LLVMPositionBuilderAtEnd(builder, bot_block);
            let new_kv_vec_index = LLVMBuildPhi(builder, self.i64_type(), c_str!(""));
            let new_slot_arr_index =
                LLVMBuildNSWAdd(builder, slot_arr_index, self.i64(1), c_str!(""));
            let finished =
                LLVMBuildICmp(builder, LLVMIntEQ, new_slot_arr_index, capacity, c_str!(""));
            LLVMBuildCondBr(builder, finished, return_block, top_block);

            // Return Block.
            LLVMPositionBuilderAtEnd(builder, return_block);
            let ret = LLVMBuildPhi(builder, kv_vector.vector_ty, c_str!(""));
            LLVMBuildRet(builder, ret);

            // Set the PHI value for the return value.
            let mut blocks = [entry_block, after_nullcheck_block, bot_block];
            let mut values = [zero_vector, zero_vector, vec];
            LLVMAddIncoming(
                ret,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            // Set the PHI value for the slot array induction variable.
            let mut blocks = [start_convert_block, bot_block];
            let mut values = [self.i64(0), new_slot_arr_index];
            LLVMAddIncoming(
                slot_arr_index,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            // Set the PHI value for the offset into the KV vector.
            let mut blocks = [start_convert_block, bot_block];
            let mut values = [self.i64(0), new_kv_vec_index];
            LLVMAddIncoming(
                kv_vec_index,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            // Set the PHI value for the intermediate KV vector offset in bot_block.
            let mut blocks = [top_block, copy_kv_block];
            let mut values = [kv_vec_index, inc_kv_vec_index];
            LLVMAddIncoming(
                new_kv_vec_index,
                values.as_mut_ptr(),
                blocks.as_mut_ptr(),
                values.len() as u32,
            );

            self.to_vec = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, run];
        Ok(LLVMBuildCall(
            builder,
            self.to_vec.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }

    /// Generates the serialize function for dictionaries.
    ///
    /// The serialize function takes four arguments: the serializeation buffer, the  position in
    /// the buffer, the value to serialize (i.e., the dictionary), and the run handle.
    ///
    /// This function returns the updated serialization vector.
    ///
    /// `key_ser` and `val_ser` are the serialization functions for the key and value,
    /// respectively.
    ///
    /// # Return Value
    ///
    /// Returns the updated buffer and updated position.
    pub unsafe fn gen_serialize(
        &mut self,
        builder: LLVMBuilderRef,
        function: LLVMValueRef,
        entry_block: LLVMBasicBlockRef,
        intrinsics: &mut Intrinsics,
        buffer_vector: &mut Vector,
        arguments: (LLVMValueRef, LLVMValueRef, LLVMValueRef, LLVMValueRef),
        key_ser: LLVMValueRef,
        val_ser: LLVMValueRef,
    ) -> WeldResult<(LLVMValueRef, LLVMValueRef)> {
        // Function is already defined by caller - we just need to generate code for it.

        let (buffer, position, dict, run) = arguments;

        let start_convert_block =
            LLVMAppendBasicBlockInContext(self.context(), function, c_str!("start.ser"));
        let top_block =
            LLVMAppendBasicBlockInContext(self.context(), function, c_str!("ser.loop.top"));
        let copy_kv_block =
            LLVMAppendBasicBlockInContext(self.context(), function, c_str!("copy.kv"));
        let bot_block =
            LLVMAppendBasicBlockInContext(self.context(), function, c_str!("ser.loop.bot"));
        let return_block =
            LLVMAppendBasicBlockInContext(self.context(), function, c_str!("return"));

        // Write the 8-byte size into the buffer.
        let dict_size = self.size(builder, dict);

        let dict_size_ty = LLVMTypeOf(dict_size);
        let bytes_to_write = self.size_of(dict_size_ty);

        let required_size = LLVMBuildAdd(builder, position, bytes_to_write, c_str!("capWithSize"));
        let pre_loop_buffer =
            buffer_vector.gen_extend(builder, intrinsics, run, buffer, required_size)?;

        let pointer_ty = LLVMPointerType(dict_size_ty, 0);
        let pointer = buffer_vector.gen_at(builder, pre_loop_buffer, position)?;
        let pointer_typed = LLVMBuildBitCast(builder, pointer, pointer_ty, c_str!(""));
        LLVMBuildStore(builder, dict_size, pointer_typed);

        let pre_loop_position = required_size;

        let size_nonzero = LLVMBuildICmp(
            builder,
            LLVMIntSGT,
            dict_size,
            self.i64(0),
            c_str!("sizeNonZero"),
        );
        LLVMBuildCondBr(builder, size_nonzero, start_convert_block, return_block);

        LLVMPositionBuilderAtEnd(builder, start_convert_block);
        let capacity = self.capacity(builder, dict);
        let slot_array = self.slot_array(builder, dict);
        LLVMBuildBr(builder, top_block);

        LLVMPositionBuilderAtEnd(builder, top_block);
        // Index into the dictionary slot array.
        let slot_arr_index = LLVMBuildPhi(builder, self.i64_type(), c_str!("slotArrIndex"));

        // The buffer and serialization position.
        let ser_buffer = LLVMBuildPhi(builder, buffer_vector.vector_ty, c_str!("serBuf"));
        let ser_position = LLVMBuildPhi(builder, self.i64_type(), c_str!("serPos"));

        let slot = self.slot_at_index(builder, slot_array, slot_arr_index);
        let filled = self.slot_ty.filled(builder, slot);
        LLVMBuildCondBr(builder, filled, copy_kv_block, bot_block);

        // Copy block - copy the key/value into the vector.
        LLVMPositionBuilderAtEnd(builder, copy_kv_block);

        // Serialize the key and value.
        let slot_key_ptr = self.slot_ty.key(builder, slot);
        let mut args = [ser_buffer, ser_position, slot_key_ptr, run];

        // The serializeation function returns a { buffer, position } struct.
        let buffer_and_pos = LLVMBuildCall(
            builder,
            key_ser,
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        );

        let updated_buffer =
            LLVMBuildExtractValue(builder, buffer_and_pos, 0, c_str!("updatedBuf"));
        let updated_position =
            LLVMBuildExtractValue(builder, buffer_and_pos, 1, c_str!("updatedPos"));

        let slot_val_ptr = self.slot_ty.value(builder, slot);
        let mut args = [updated_buffer, updated_position, slot_val_ptr, run];
        let buffer_and_pos = LLVMBuildCall(
            builder,
            val_ser,
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        );

        let updated_buffer =
            LLVMBuildExtractValue(builder, buffer_and_pos, 0, c_str!("updatedBuf"));
        let updated_position =
            LLVMBuildExtractValue(builder, buffer_and_pos, 1, c_str!("updatedPos"));

        LLVMBuildBr(builder, bot_block);

        // Bottom of loop -- increment induction variables and loop or exit.
        LLVMPositionBuilderAtEnd(builder, bot_block);
        let new_ser_buffer = LLVMBuildPhi(builder, buffer_vector.vector_ty, c_str!("newBuf"));
        let new_ser_position = LLVMBuildPhi(builder, self.i64_type(), c_str!("newPos"));

        let new_slot_arr_index =
            LLVMBuildNSWAdd(builder, slot_arr_index, self.i64(1), c_str!("newArrIdx"));
        let finished = LLVMBuildICmp(
            builder,
            LLVMIntEQ,
            new_slot_arr_index,
            capacity,
            c_str!("finished"),
        );
        LLVMBuildCondBr(builder, finished, return_block, top_block);

        // Return Block.
        LLVMPositionBuilderAtEnd(builder, return_block);
        let ret_buffer = LLVMBuildPhi(builder, buffer_vector.vector_ty, c_str!("retBuf"));
        let ret_position = LLVMBuildPhi(builder, self.i64_type(), c_str!("retPos"));

        // Set the PHI value for the return value.
        let mut blocks = [entry_block, bot_block];
        let mut values = [pre_loop_buffer, new_ser_buffer];
        LLVMAddIncoming(
            ret_buffer,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );
        let mut values = [pre_loop_position, new_ser_position];
        LLVMAddIncoming(
            ret_position,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Set the PHI value for the slot array induction variable.
        let mut blocks = [start_convert_block, bot_block];
        let mut values = [self.i64(0), new_slot_arr_index];
        LLVMAddIncoming(
            slot_arr_index,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Set the PHI value for the offset into the KV vector.
        let mut blocks = [start_convert_block, bot_block];
        let mut values = [pre_loop_position, new_ser_position];
        LLVMAddIncoming(
            ser_position,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Set the PHI value for the intermediate KV vector offset in bot_block.
        let mut blocks = [top_block, copy_kv_block];
        let mut values = [ser_position, updated_position];
        LLVMAddIncoming(
            new_ser_position,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Set the PHI value for the buffer.
        let mut blocks = [start_convert_block, bot_block];
        let mut values = [pre_loop_buffer, new_ser_buffer];
        LLVMAddIncoming(
            ser_buffer,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Set the PHI value for the intermediate buffer in bot_block.
        let mut blocks = [top_block, copy_kv_block];
        let mut values = [ser_buffer, updated_buffer];
        LLVMAddIncoming(
            new_ser_buffer,
            values.as_mut_ptr(),
            blocks.as_mut_ptr(),
            values.len() as u32,
        );

        // Return the values: the calling serialization code will package them into a struct and
        // return them.
        Ok((ret_buffer, ret_position))
    }
}

impl GroupingDict for Dict {
    /// Merge `value` into the group for `key` with the given `hash`.
    ///
    /// This method takes a `Vector`, which holds methods for the type `vec[V]`.
    unsafe fn gen_merge_grouped(
        &mut self,
        builder: LLVMBuilderRef,
        intrinsics: &mut Intrinsics,
        group_vector: &mut Vector,
        dict: LLVMValueRef,
        key: LLVMValueRef,
        hash: LLVMValueRef,
        value: LLVMValueRef,
        run: LLVMValueRef,
    ) -> WeldResult<LLVMValueRef> {
        if self.merge_grouped.is_none() {
            let mut arg_tys = [
                self.dict_ty,
                LLVMPointerType(self.slot_ty.key_ty, 0),
                self.hash_type(),
                group_vector.elem_ty,
                self.run_handle_type(),
            ];
            let ret_ty = self.void_type();

            let name = format!("{}.merge_grouped", self.name);
            let (function, builder, _) = self.define_function(ret_ty, &mut arg_tys, name);

            let dict = LLVMGetParam(function, 0);
            let key = LLVMGetParam(function, 1);
            let hash = LLVMGetParam(function, 2);
            let value = LLVMGetParam(function, 3);
            let run = LLVMGetParam(function, 4);

            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias], 0);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias], 1);
            LLVMExtAddAttrsOnParameter(self.context, function, &[NoAlias], 4);

            let init_block = LLVMAppendBasicBlockInContext(self.context, function, c_str!("init"));
            let checkcap_block =
                LLVMAppendBasicBlockInContext(self.context, function, c_str!("checkcapacity"));
            let resize_block =
                LLVMAppendBasicBlockInContext(self.context, function, c_str!("resize"));
            let merge_block =
                LLVMAppendBasicBlockInContext(self.context, function, c_str!("merge"));

            let mut zero_vector = LLVMGetUndef(group_vector.vector_ty);
            zero_vector = LLVMConstInsertValue(
                zero_vector,
                self.null_ptr(group_vector.elem_ty),
                [vector::POINTER_INDEX].as_mut_ptr(),
                1,
            );
            zero_vector = LLVMConstInsertValue(
                zero_vector,
                self.i64(0),
                [vector::SIZE_INDEX].as_mut_ptr(),
                1,
            );

            // Upsert the zero vector. This will create a slot if one does not exist already. We
            // can then initialize the grouping vector if necessary, and append the new value to
            // the grouping vector.
            //
            // Generated Code:
            //
            //  slot = upsert(dict, key, hash, zeroinitializer, run)
            //  filled = slot.filled
            //  if filled == 1 ? init : checkcapacity
            // init:
            //  vec = vector.new()
            //  store vec into slot.value
            //  filled = log2(DEFAULT_CAPACITY)
            //  br merge
            //
            // checkcapacity:
            //  capacity = (1 << filled)
            //  vector = slot.value
            //  if capacity <= vector.size ? resize : merge
            //
            // resize:
            //  vector.extend
            //  filled ++
            //  br merge
            //
            // merge:
            //  size = vector.size
            //  ptr = vector.at(size)
            //  store value at ptr

            let slot = self.gen_upsert(builder, intrinsics, dict, key, hash, zero_vector, run)?;

            let value_pointer = self.slot_ty.value(builder, slot);

            // Array's size pointer. This points into the slot's value.
            let size_pointer = LLVMBuildStructGEP(
                builder,
                value_pointer,
                vector::SIZE_INDEX,
                c_str!("sizePtr"),
            );
            let filled_value = self.slot_ty.filled_value(builder, slot);

            // If capacity == 1, the slot was just initialized via the upsert. We need to
            // initialize the vector.
            let was_initialized = LLVMBuildICmp(
                builder,
                LLVMIntEQ,
                filled_value,
                self.i8(1),
                c_str!("initialized"),
            );
            LLVMBuildCondBr(builder, was_initialized, init_block, checkcap_block);

            // Initalize the vector with the default capacity. The vector capacity must be a
            // power-of-2 so we can represent it using the filled byte.
            LLVMPositionBuilderAtEnd(builder, init_block);
            let new_vector =
                group_vector.gen_new(builder, intrinsics, run, self.i64(DEFAULT_GROUP_CAPACITY))?;
            LLVMBuildStore(builder, new_vector, value_pointer);

            // We must set the size to 0, since we manually track the size within the vector. The
            // capacity is tracked via the filled byte.
            LLVMBuildStore(builder, self.i64(0), size_pointer);

            // Store the capacity as a power-of-2.
            let default_filled = self.i8(DEFAULT_GROUP_FILLED);
            self.slot_ty.set_filled(builder, slot, default_filled);
            LLVMBuildBr(builder, merge_block);

            // If the vector was already there, make sure we can fit the new element.
            LLVMPositionBuilderAtEnd(builder, checkcap_block);
            let ext_filled_value =
                LLVMBuildZExt(builder, filled_value, self.u64_type(), c_str!(""));
            let capacity = LLVMBuildShl(builder, self.i64(1), ext_filled_value, c_str!("capacity"));
            let cur_vector = self.load(builder, value_pointer).unwrap();
            let size = group_vector.gen_size(builder, cur_vector)?;
            let needs_resize =
                LLVMBuildICmp(builder, LLVMIntEQ, size, capacity, c_str!("shouldResize"));
            LLVMBuildCondBr(builder, needs_resize, resize_block, merge_block);

            // Extend the vector and update the slot.
            LLVMPositionBuilderAtEnd(builder, resize_block);
            let new_capacity =
                LLVMBuildNUWMul(builder, capacity, self.i64(2), c_str!("newCapacity"));
            let resized_vector =
                group_vector.gen_extend(builder, intrinsics, run, cur_vector, new_capacity)?;

            // Since extend sets the size, we need to "reset" it back to the previous size.
            LLVMBuildStore(builder, resized_vector, value_pointer);
            LLVMBuildStore(builder, size, size_pointer);

            // Set the new capacity: since we double the size of the vector, we just incremented
            // the filled value by 1.
            let new_filled_value =
                LLVMBuildAdd(builder, filled_value, self.i8(1), c_str!("newFilled"));
            self.slot_ty.set_filled(builder, slot, new_filled_value);
            LLVMBuildBr(builder, merge_block);

            // Merge the value. The capacity of the vector is guaranteed to accomdate the merge
            // value now.
            LLVMPositionBuilderAtEnd(builder, merge_block);
            let array_pointer = LLVMBuildStructGEP(
                builder,
                value_pointer,
                vector::POINTER_INDEX,
                c_str!("arrayPtr"),
            );
            let array_pointer = self.load(builder, array_pointer).unwrap();

            // Merge the value.
            let offset = self.load(builder, size_pointer).unwrap();
            let merge_pointer = LLVMBuildGEP(
                builder,
                array_pointer,
                [offset].as_mut_ptr(),
                1,
                c_str!("mergePtr"),
            );
            LLVMBuildStore(builder, value, merge_pointer);

            // Update the size.
            let inc_size = LLVMBuildNSWAdd(builder, offset, self.i64(1), c_str!("newSize"));
            LLVMBuildStore(builder, inc_size, size_pointer);
            LLVMBuildRetVoid(builder);

            self.merge_grouped = Some(function);
            LLVMDisposeBuilder(builder);
        }

        let mut args = [dict, key, hash, value, run];
        Ok(LLVMBuildCall(
            builder,
            self.merge_grouped.unwrap(),
            args.as_mut_ptr(),
            args.len() as u32,
            c_str!(""),
        ))
    }
}