1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
|
#include "mkql_computation_node_codegen.h" // Y_IGNORE
#include "mkql_computation_node_holders.h"
#include <yql/essentials/minikql/codegen/codegen.h>
#include <yql/essentials/public/decimal/yql_decimal.h>
#include <util/string/cast.h>
#include <util/folder/path.h>
#ifndef MKQL_DISABLE_CODEGEN
extern "C" void DeleteBoxed(NKikimr::NUdf::IBoxedValue *const boxed) {
delete boxed;
}
extern "C" void DeleteString(void* strData) {
auto& str = *(NKikimr::NUdf::TStringValue*)(&strData);
UdfFreeWithSize(strData, 16 + str.Capacity());
}
namespace NKikimr {
namespace NMiniKQL {
constexpr bool EnableStaticRefcount = true;
using namespace llvm;
Type* GetCompContextType(LLVMContext &context) {
const auto ptrValueType = PointerType::getUnqual(Type::getInt128Ty(context));
const auto structPtrType = PointerType::getUnqual(StructType::get(context));
const auto stringRefType = StructType::get(context, {
Type::getInt8PtrTy(context),
Type::getInt32Ty(context),
Type::getInt32Ty(context)
});
const auto sourcePosType = StructType::get(context, {
Type::getInt32Ty(context),
Type::getInt32Ty(context),
stringRefType
});
return StructType::get(context, {
structPtrType, // factory
structPtrType, // stats
ptrValueType, // mutables
structPtrType, // builder
Type::getFloatTy(context), // adjustor
Type::getInt32Ty(context), // rsscounter
PointerType::getUnqual(sourcePosType)
});
}
Value* TCodegenContext::GetFactory() const {
if (!Factory) {
auto& context = Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto ptrType = PointerType::getUnqual(StructType::get(context));
if (Func->getEntryBlock().empty()) {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 0)}, "factory_ptr", &Func->getEntryBlock());
const_cast<Value*&>(Factory) = new LoadInst(ptrType, ptr, "factory", &Func->getEntryBlock());
} else {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 0)}, "factory_ptr", &Func->getEntryBlock().front());
const_cast<Value*&>(Factory) = new LoadInst(ptrType, ptr, "factory", &Func->getEntryBlock().back());
}
}
return Factory;
}
Value* TCodegenContext::GetStat() const {
if (!Stat) {
auto& context = Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto ptrType = PointerType::getUnqual(StructType::get(context));
if (Func->getEntryBlock().empty()) {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 1)}, "stat_ptr", &Func->getEntryBlock());
const_cast<Value*&>(Stat) = new LoadInst(ptrType, ptr, "stat", &Func->getEntryBlock());
} else {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 1)}, "stat_ptr", &Func->getEntryBlock().front());
const_cast<Value*&>(Stat) = new LoadInst(ptrType, ptr, "stat", &Func->getEntryBlock().back());
}
}
return Stat;
}
Value* TCodegenContext::GetMutables() const {
if (!Mutables) {
auto& context = Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto ptrType = PointerType::getUnqual(Type::getInt128Ty(context));
if (Func->getEntryBlock().empty()) {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 2)}, "mutables_ptr", &Func->getEntryBlock());
const_cast<Value*&>(Mutables) = new LoadInst(ptrType, ptr, "mutables", &Func->getEntryBlock());
} else {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 2)}, "mutables_ptr", &Func->getEntryBlock().front());
const_cast<Value*&>(Mutables) = new LoadInst(ptrType, ptr, "mutables", &Func->getEntryBlock().back());
}
}
return Mutables;
}
Value* TCodegenContext::GetBuilder() const {
if (!Builder) {
auto& context = Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto ptrType = PointerType::getUnqual(StructType::get(context));
if (Func->getEntryBlock().empty()) {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 3)}, "builder_ptr", &Func->getEntryBlock());
const_cast<Value*&>(Builder) = new LoadInst(ptrType, ptr, "builder", &Func->getEntryBlock());
} else {
const auto ptr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 3)}, "builder_ptr", &Func->getEntryBlock().front());
const_cast<Value*&>(Builder) = new LoadInst(ptrType, ptr, "builder", &Func->getEntryBlock().back());
}
}
return Builder;
}
Function* GenerateCompareFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, IComputationExternalNode* left,
IComputationExternalNode* right, IComputationNode* compare) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto codegenLeft = dynamic_cast<ICodegeneratorExternalNode*>(left);
const auto codegenRight = dynamic_cast<ICodegeneratorExternalNode*>(right);
MKQL_ENSURE(codegenLeft, "Left must be codegenerator node.");
MKQL_ENSURE(codegenRight, "Right must be codegenerator node.");
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto ptrType = PointerType::getUnqual(valueType);
const auto returnType = Type::getInt1Ty(context);
const auto contextType = GetCompContextType(context);
const auto funcType = codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(returnType, {PointerType::getUnqual(contextType), valueType, valueType}, false):
FunctionType::get(returnType, {PointerType::getUnqual(contextType), ptrType, ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
ctx.Ctx = &*args;
ctx.Ctx->addAttr(Attribute::NonNull);
const auto lv = &*++args;
const auto rv = &*++args;
codegenLeft->SetValueBuilder([lv](const TCodegenContext&) { return lv; });
codegenRight->SetValueBuilder([rv](const TCodegenContext&) { return rv; });
codegenLeft->CreateInvalidate(ctx, block);
codegenRight->CreateInvalidate(ctx, block);
const auto res = GetNodeValue(compare, ctx, block);
const auto cast = CastInst::Create(Instruction::Trunc, res, returnType, "bool", block);
ReturnInst::Create(context, cast, block);
codegenLeft->SetValueBuilder({});
codegenRight->SetValueBuilder({});
return ctx.Func;
}
Value* GetterFor(NUdf::EDataSlot slot, Value* value, LLVMContext &context, BasicBlock* block) {
switch (slot) {
case NUdf::EDataSlot::Bool: return GetterFor<bool>(value, context, block);
case NUdf::EDataSlot::Decimal: return GetterForInt128(value, block);
case NUdf::EDataSlot::Float: return GetterFor<float>(value, context, block);
case NUdf::EDataSlot::Double: return GetterFor<double>(value, context, block);
default: break;
}
const auto trunc = CastInst::Create(Instruction::Trunc, value, IntegerType::get(context, NUdf::GetDataTypeInfo(slot).FixedSize << 3U), "trunc", block);
return trunc;
}
namespace {
Value* GetMarkFromUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock* block) {
auto& context = ctx.Codegen.GetContext();
const auto type8 = Type::getInt8Ty(context);
if (value->getType()->isPointerTy()) {
const auto type = StructType::get(context, {PointerType::getUnqual(StructType::get(context)), ArrayType::get(type8, 8U)});
const auto cast = CastInst::Create(Instruction::BitCast, value, PointerType::getUnqual(type), "cast", block);
const auto type32 = Type::getInt32Ty(context);
const auto metaptr = GetElementPtrInst::CreateInBounds(type, cast, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 1), ConstantInt::get(type32, 7)}, "metaptr", block);
const auto meta = new LoadInst(type8, metaptr, "meta", block);
const auto mark = BinaryOperator::CreateAnd(meta, ConstantInt::get(meta->getType(), 3), "mark", block);
return mark;
} else {
const auto lshr = BinaryOperator::CreateLShr(value, ConstantInt::get(value->getType(), 120), "lshr", block);
const auto meta = CastInst::Create(Instruction::Trunc, lshr, type8, "meta", block);
const auto mark = BinaryOperator::CreateAnd(ConstantInt::get(meta->getType(), 3), meta, "mark", block);
return mark;
}
}
template<bool BoxedOrString>
Value* GetPointerFromUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock* block) {
auto& context = ctx.Codegen.GetContext();
const auto type32 = Type::getInt32Ty(context);
const auto type64 = Type::getInt64Ty(context);
const auto type = PointerType::getUnqual(BoxedOrString ?
StructType::get(context, {PointerType::getUnqual(StructType::get(context)), type32, Type::getInt16Ty(context)}):
StructType::get(context, {type32, type32, type32, type32})
);
if (value->getType()->isPointerTy()) {
const auto strType = StructType::get(context, {type, type64});
const auto cast = CastInst::Create(Instruction::BitCast, value, PointerType::getUnqual(strType), "cast", block);
const auto ptr = GetElementPtrInst::CreateInBounds(strType, cast, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 0)}, "ptr", block);
const auto pointer = new LoadInst(type, ptr, "pointer", block);
return pointer;
} else {
const auto half = CastInst::Create(Instruction::Trunc, value, type64, "half", block);
const auto pointer = CastInst::Create(Instruction::IntToPtr, half, type, "pointer", block);
return pointer;
}
}
ui32 MyCompareStrings(NUdf::TUnboxedValuePod lhs, NUdf::TUnboxedValuePod rhs) {
return NUdf::CompareStrings(lhs, rhs);
}
bool MyEquteStrings(NUdf::TUnboxedValuePod lhs, NUdf::TUnboxedValuePod rhs) {
return NUdf::EquateStrings(lhs, rhs);
}
NUdf::THashType MyHashString(NUdf::TUnboxedValuePod val) {
return NUdf::GetStringHash(val);
}
template <bool IsOptional>
Value* GenEqualsFunction(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block);
template <>
Value* GenEqualsFunction<false>(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto& info = NUdf::GetDataTypeInfo(slot);
if ((info.Features & NUdf::EDataTypeFeatures::CommonType) && (info.Features & NUdf::EDataTypeFeatures::StringType || NUdf::EDataSlot::Uuid == slot || NUdf::EDataSlot::DyNumber == slot)) {
return CallBinaryUnboxedValueFunction(&MyEquteStrings, Type::getInt1Ty(context), lv, rv, ctx.Codegen, block);
}
const auto lhs = GetterFor(slot, lv, context, block);
const auto rhs = GetterFor(slot, rv, context, block);
if (info.Features & (NUdf::EDataTypeFeatures::IntegralType | NUdf::EDataTypeFeatures::DateType | NUdf::EDataTypeFeatures::TimeIntervalType | NUdf::EDataTypeFeatures::DecimalType) || NUdf::EDataSlot::Bool == slot) {
const auto equal = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, lhs, rhs, "equal", block);
return equal;
}
if (info.Features & NUdf::EDataTypeFeatures::FloatType) {
const auto ueq = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UEQ, lhs, rhs, "equals", block);
const auto lord = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ORD, ConstantFP::get(lhs->getType(), 0.0), lhs, "lord", block);
const auto runo = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UNO, ConstantFP::get(rhs->getType(), 0.0), rhs, "runo", block);
const auto once = BinaryOperator::CreateXor(lord, runo, "xor", block);
return BinaryOperator::CreateAnd(ueq, once, "and", block);
}
if (info.Features & NUdf::EDataTypeFeatures::TzDateType) {
const auto ltz = GetterForTimezone(context, lv, block);
const auto rtz = GetterForTimezone(context, rv, block);
const auto one = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, lhs, rhs, "one", block);
const auto two = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, ltz, rtz, "two", block);
return BinaryOperator::CreateAnd(one, two, "and", block);
}
return nullptr;
}
template <>
Value* GenEqualsFunction<true>(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto tiny = BasicBlock::Create(context, "tiny", ctx.Func);
const auto test = BasicBlock::Create(context, "test", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto res = PHINode::Create(Type::getInt1Ty(context), 2U, "result", done);
const auto le = IsEmpty(lv, block);
const auto re = IsEmpty(rv, block);
const auto any = BinaryOperator::CreateOr(le, re, "or", block);
BranchInst::Create(tiny, test, any, block);
block = tiny;
const auto both = BinaryOperator::CreateAnd(le, re, "and", block);
res->addIncoming(both, block);
BranchInst::Create(done, block);
block = test;
const auto comp = GenEqualsFunction<false>(slot, lv, rv, ctx, block);
res->addIncoming(comp, block);
BranchInst::Create(done, block);
block = done;
return res;
}
Value* GenEqualsFunction(NUdf::EDataSlot slot, bool isOptional, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
return isOptional ? GenEqualsFunction<true>(slot, lv, rv, ctx, block) : GenEqualsFunction<false>(slot, lv, rv, ctx, block);
}
template <bool IsOptional>
Value* GenCompareFunction(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block);
template <>
Value* GenCompareFunction<false>(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto& info = NUdf::GetDataTypeInfo(slot);
if ((info.Features & NUdf::EDataTypeFeatures::CommonType) && (info.Features & NUdf::EDataTypeFeatures::StringType || NUdf::EDataSlot::Uuid == slot || NUdf::EDataSlot::DyNumber == slot)) {
return CallBinaryUnboxedValueFunction(&MyCompareStrings, Type::getInt32Ty(context), lv, rv, ctx.Codegen, block);
}
const bool extra = info.Features & (NUdf::EDataTypeFeatures::FloatType | NUdf::EDataTypeFeatures::TzDateType);
const auto resultType = Type::getInt32Ty(context);
const auto exit = BasicBlock::Create(context, "exit", ctx.Func);
const auto test = BasicBlock::Create(context, "test", ctx.Func);
const auto res = PHINode::Create(resultType, extra ? 3U : 2U, "result", exit);
const auto lhs = GetterFor(slot, lv, context, block);
const auto rhs = GetterFor(slot, rv, context, block);
if (info.Features & NUdf::EDataTypeFeatures::FloatType) {
const auto more = BasicBlock::Create(context, "more", ctx.Func);
const auto next = BasicBlock::Create(context, "next", ctx.Func);
const auto uno = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UNO, lhs, rhs, "unorded", block);
BranchInst::Create(more, next, uno, block);
block = more;
const auto luno = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UNO, ConstantFP::get(lhs->getType(), 0.0), lhs, "luno", block);
const auto runo = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UNO, ConstantFP::get(rhs->getType(), 0.0), rhs, "runo", block);
const auto once = BinaryOperator::CreateXor(luno, runo, "xor", block);
const auto left = SelectInst::Create(luno, ConstantInt::get(resultType, 1), ConstantInt::get(resultType, -1), "left", block);
const auto both = SelectInst::Create(once, left, ConstantInt::get(resultType, 0), "both", block);
res->addIncoming(both, block);
BranchInst::Create(exit, block);
block = next;
}
const auto equals = info.Features & NUdf::EDataTypeFeatures::FloatType ?
CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OEQ, lhs, rhs, "equals", block):
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, lhs, rhs, "equals", block);
if (info.Features & NUdf::EDataTypeFeatures::TzDateType) {
const auto more = BasicBlock::Create(context, "more", ctx.Func);
const auto next = BasicBlock::Create(context, "next", ctx.Func);
BranchInst::Create(more, test, equals, block);
block = more;
const auto ltz = GetterForTimezone(context, lv, block);
const auto rtz = GetterForTimezone(context, rv, block);
const auto tzeq = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, ltz, rtz, "tzeq", block);
res->addIncoming(ConstantInt::get(resultType, 0), block);
BranchInst::Create(exit, next, tzeq, block);
block = next;
const auto tzlt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, ltz, rtz, "tzlt", block);
const auto tzout = SelectInst::Create(tzlt, ConstantInt::get(resultType, -1), ConstantInt::get(resultType, 1), "tzout", block);
res->addIncoming(tzout, block);
BranchInst::Create(exit, block);
} else {
res->addIncoming(ConstantInt::get(resultType, 0), block);
BranchInst::Create(exit, test, equals, block);
}
block = test;
const auto less = info.Features & NUdf::EDataTypeFeatures::FloatType ?
CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OLT, lhs, rhs, "less", block): // float
info.Features & (NUdf::EDataTypeFeatures::SignedIntegralType | NUdf::EDataTypeFeatures::TimeIntervalType | NUdf::EDataTypeFeatures::DecimalType) ?
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, lhs, rhs, "less", block): // signed
info.Features & (NUdf::EDataTypeFeatures::UnsignedIntegralType | NUdf::EDataTypeFeatures::DateType | NUdf::EDataTypeFeatures::TzDateType) ?
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, lhs, rhs, "less", block): // unsigned
rhs; // bool
const auto out = SelectInst::Create(less, ConstantInt::get(resultType, -1), ConstantInt::get(resultType, 1), "out", block);
res->addIncoming(out, block);
BranchInst::Create(exit, block);
block = exit;
return res;
}
template <>
Value* GenCompareFunction<true>(NUdf::EDataSlot slot, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto tiny = BasicBlock::Create(context, "tiny", ctx.Func);
const auto side = BasicBlock::Create(context, "side", ctx.Func);
const auto test = BasicBlock::Create(context, "test", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto resultType = Type::getInt32Ty(context);
const auto res = PHINode::Create(resultType, 3U, "result", done);
const auto le = IsEmpty(lv, block);
const auto re = IsEmpty(rv, block);
const auto any = BinaryOperator::CreateOr(le, re, "or", block);
BranchInst::Create(tiny, test, any, block);
block = tiny;
const auto both = BinaryOperator::CreateAnd(le, re, "and", block);
res->addIncoming(ConstantInt::get(resultType, 0), block);
BranchInst::Create(done, side, both, block);
block = side;
const auto out = SelectInst::Create(le, ConstantInt::get(resultType, -1), ConstantInt::get(resultType, 1), "out", block);
res->addIncoming(out, block);
BranchInst::Create(done, block);
block = test;
const auto comp = GenCompareFunction<false>(slot, lv, rv, ctx, block);
res->addIncoming(comp, block);
BranchInst::Create(done, block);
block = done;
return res;
}
Value* GenCompareFunction(NUdf::EDataSlot slot, bool isOptional, Value* lv, Value* rv, TCodegenContext& ctx, BasicBlock*& block) {
return isOptional ? GenCompareFunction<true>(slot, lv, rv, ctx, block) : GenCompareFunction<false>(slot, lv, rv, ctx, block);
}
Value* GenCombineHashes(Value* first, Value* second, BasicBlock* block) {
// key += ~(key << 32);
const auto x01 = BinaryOperator::CreateShl(first, ConstantInt::get(first->getType(), 32), "x01", block);
const auto x02 = BinaryOperator::CreateXor(x01, ConstantInt::get(x01->getType(), ~0), "x02", block);
const auto x03 = BinaryOperator::CreateAdd(x02, first, "x03", block);
// key ^= (key >> 22);
const auto x04 = BinaryOperator::CreateLShr(x03, ConstantInt::get(x03->getType(), 22), "x04", block);
const auto x05 = BinaryOperator::CreateXor(x04, x03, "x05", block);
// key += ~(key << 13);
const auto x06 = BinaryOperator::CreateShl(x05, ConstantInt::get(x05->getType(), 13), "x06", block);
const auto x07 = BinaryOperator::CreateXor(x06, ConstantInt::get(x06->getType(), ~0), "x07", block);
const auto x08 = BinaryOperator::CreateAdd(x05, x07, "x08", block);
// key ^= (key >> 8);
const auto x09 = BinaryOperator::CreateLShr(x08, ConstantInt::get(x08->getType(), 8), "x09", block);
const auto x10 = BinaryOperator::CreateXor(x08, x09, "x10", block);
// key += (key << 3);
const auto x11 = BinaryOperator::CreateShl(x10, ConstantInt::get(x10->getType(), 3), "x11", block);
const auto x12 = BinaryOperator::CreateAdd(x10, x11, "x12", block);
// key ^= (key >> 15);
const auto x13 = BinaryOperator::CreateLShr(x12, ConstantInt::get(x12->getType(), 15), "x13", block);
const auto x14 = BinaryOperator::CreateXor(x13, x12, "x14", block);
// key += ~(key << 27);
const auto x15 = BinaryOperator::CreateShl(x14, ConstantInt::get(x14->getType(), 27), "x15", block);
const auto x16 = BinaryOperator::CreateXor(x15, ConstantInt::get(x15->getType(), ~0), "x16", block);
const auto x17 = BinaryOperator::CreateAdd(x14, x16, "x17", block);
// key ^= (key >> 31);
const auto x18 = BinaryOperator::CreateLShr(x17, ConstantInt::get(x17->getType(), 31), "x18", block);
const auto x19 = BinaryOperator::CreateXor(x17, x18, "x19", block);
return BinaryOperator::CreateXor(x19, second, "both", block);
}
template <bool IsOptional>
Value* GenHashFunction(NUdf::EDataSlot slot, Value* value, TCodegenContext& ctx, BasicBlock*& block);
template <>
Value* GenHashFunction<false>(NUdf::EDataSlot slot, Value* value, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto& info = NUdf::GetDataTypeInfo(slot);
if ((info.Features & NUdf::EDataTypeFeatures::CommonType) && (info.Features & NUdf::EDataTypeFeatures::StringType || NUdf::EDataSlot::Uuid == slot || NUdf::EDataSlot::DyNumber == slot)) {
return CallUnaryUnboxedValueFunction(&MyHashString, Type::getInt64Ty(context), value, ctx.Codegen, block);
}
const auto val = GetterFor(slot, value, context, block);
const auto hashType = Type::getInt64Ty(context);
if (info.Features & (NUdf::EDataTypeFeatures::IntegralType | NUdf::EDataTypeFeatures::DateType | NUdf::EDataTypeFeatures::TimeIntervalType) || NUdf::EDataSlot::Bool == slot) {
if (val->getType() == hashType) {
return val;
}
const auto ext = CastInst::Create(Instruction::ZExt, val, hashType, "ext", block);
return ext;
}
if (info.Features & NUdf::EDataTypeFeatures::FloatType) {
const auto nan = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_UNO, val, val, "nan", block);
const auto zero = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OEQ, val, ConstantFP::get(val->getType(), 0), "zero", block);
if (NUdf::EDataSlot::Float == slot) {
const auto cast = CastInst::Create(Instruction::BitCast, val, Type::getInt32Ty(context), "cast", block);
const auto ext = CastInst::Create(Instruction::ZExt, cast, hashType, "ext", block);
const auto first = SelectInst::Create(nan, ConstantInt::get(hashType, ~0), ext, "first", block);
const auto second = SelectInst::Create(zero, ConstantInt::get(hashType, 0), first, "second", block);
return second;
} else {
const auto cast = CastInst::Create(Instruction::BitCast, val, hashType, "cast", block);
const auto first = SelectInst::Create(nan, ConstantInt::get(hashType, ~0), cast, "first", block);
const auto second = SelectInst::Create(zero, ConstantInt::get(hashType, 0), first, "second", block);
return second;
}
}
if (info.Features & NUdf::EDataTypeFeatures::TzDateType) {
const auto tz = GetterForTimezone(context, value, block);
const auto ext = val->getType() == hashType ? val : CastInst::Create(Instruction::ZExt, val, hashType, "ext", block);
const auto etz = CastInst::Create(Instruction::ZExt, tz, hashType, "etz", block);
return GenCombineHashes(ext, etz, block);
}
if (info.Features & NUdf::EDataTypeFeatures::DecimalType) {
const auto low = CastInst::Create(Instruction::Trunc, val, hashType, "low", block);
const auto lshr = BinaryOperator::CreateLShr(val, ConstantInt::get(val->getType(), 64), "lshr", block);
const auto high = CastInst::Create(Instruction::Trunc, lshr, hashType, "high", block);
return GenCombineHashes(low, high, block);
}
return nullptr;
}
template <>
Value* GenHashFunction<true>(NUdf::EDataSlot slot, Value* value, TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto tiny = BasicBlock::Create(context, "tiny", ctx.Func);
const auto test = BasicBlock::Create(context, "test", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto res = PHINode::Create(Type::getInt64Ty(context), 2U, "result", done);
BranchInst::Create(tiny, test, IsEmpty(value, block), block);
block = tiny;
res->addIncoming(ConstantInt::get(Type::getInt64Ty(context), ~0ULL), block);
BranchInst::Create(done, block);
block = test;
const auto comp = GenHashFunction<false>(slot, value, ctx, block);
res->addIncoming(comp, block);
BranchInst::Create(done, block);
block = done;
return res;
}
Value* GenHashFunction(NUdf::EDataSlot slot, bool isOptional, Value* value, TCodegenContext& ctx, BasicBlock*& block) {
return isOptional ? GenHashFunction<true>(slot, value, ctx, block) : GenHashFunction<false>(slot, value, ctx, block);
}
Value* LoadIfPointer(Value* value, BasicBlock* block) {
return value->getType()->isPointerTy() ? new LoadInst(value->getType()->getPointerElementType(), value, "load_value", block) : value;
}
}
Function* GenerateEqualsFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, bool isTuple, const TKeyTypes& types) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto ptrType = PointerType::getUnqual(valueType);
const auto returnType = Type::getInt1Ty(context);
const auto funcType = codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(returnType, {valueType, valueType}, false):
FunctionType::get(returnType, {ptrType, ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
const auto lv = LoadIfPointer(&*args, block);
const auto rv = LoadIfPointer(&*++args, block);
if (isTuple) {
if (types.empty()) {
ReturnInst::Create(context, ConstantInt::getTrue(context), block);
return ctx.Func;
}
const auto fast = BasicBlock::Create(context, "fast", ctx.Func);
const auto slow = BasicBlock::Create(context, "slow", ctx.Func);
const auto stop = BasicBlock::Create(context, "stop", ctx.Func);
const auto elementsType = ArrayType::get(valueType, types.size());
const auto elementsPtrType = PointerType::getUnqual(elementsType);
const auto elementsPtrOne = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElements>(elementsPtrType, lv, ctx.Codegen, block);
const auto elementsPtrTwo = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElements>(elementsPtrType, rv, ctx.Codegen, block);
const auto goodOne = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, elementsPtrOne, ConstantPointerNull::get(elementsPtrType), "good_one", block);
const auto goodTwo = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, elementsPtrTwo, ConstantPointerNull::get(elementsPtrType), "good_two", block);
const auto good = BinaryOperator::CreateAnd(goodOne, goodTwo, "good", block);
BranchInst::Create(fast, slow, good, block);
const auto last = types.size() - 1U;
{
block = fast;
const auto elementsOne = new LoadInst(elementsType, elementsPtrOne, "elements_one", block);
const auto elementsTwo = new LoadInst(elementsType, elementsPtrTwo, "elements_two", block);
for (ui32 i = 0U; i < last; ++i) {
const auto nextOne = ExtractValueInst::Create(elementsOne, i, (TString("next_one_") += ToString(i)).c_str(), block);
const auto nextTwo = ExtractValueInst::Create(elementsTwo, i, (TString("next_two_") += ToString(i)).c_str(), block);
const auto step = BasicBlock::Create(context, (TString("step") += ToString(i)).c_str(), ctx.Func);
const auto test = GenEqualsFunction(types[i].first, types[i].second, nextOne, nextTwo, ctx, block);
BranchInst::Create(step, stop, test, block);
block = step;
}
const auto backOne = ExtractValueInst::Create(elementsOne, last, "back_one", block);
const auto backTwo = ExtractValueInst::Create(elementsTwo, last, "back_two", block);
const auto result = GenEqualsFunction(types.back().first, types.back().second, backOne, backTwo, ctx, block);
ReturnInst::Create(context, result, block);
}
{
block = slow;
const auto elementOne = new AllocaInst(valueType, 0U, "element_one", block);
const auto elementTwo = new AllocaInst(valueType, 0U, "element_two", block);
const auto indexType = Type::getInt32Ty(context);
for (ui32 i = 0U; i < last; ++i) {
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(elementOne, lv, ctx.Codegen, block, ConstantInt::get(indexType, i));
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(elementTwo, rv, ctx.Codegen, block, ConstantInt::get(indexType, i));
const auto nextOne = new LoadInst(valueType, elementOne, (TString("next_one_") += ToString(i)).c_str(), block);
const auto nextTwo = new LoadInst(valueType, elementTwo, (TString("next_two_") += ToString(i)).c_str(), block);
if (NUdf::GetDataTypeInfo(types[i].first).Features & NUdf::EDataTypeFeatures::StringType) {
ValueRelease(EValueRepresentation::String, nextOne, ctx, block);
ValueRelease(EValueRepresentation::String, nextTwo, ctx, block);
}
const auto step = BasicBlock::Create(context, (TString("step") += ToString(i)).c_str(), ctx.Func);
const auto test = GenEqualsFunction(types[i].first, types[i].second, nextOne, nextTwo, ctx, block);
BranchInst::Create(step, stop, test, block);
block = step;
}
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(elementOne, lv, ctx.Codegen, block, ConstantInt::get(indexType, last));
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(elementTwo, rv, ctx.Codegen, block, ConstantInt::get(indexType, last));
const auto backOne = new LoadInst(valueType, elementOne, "back_one", block);
const auto backTwo = new LoadInst(valueType, elementTwo, "back_two", block);
if (NUdf::GetDataTypeInfo(types.back().first).Features & NUdf::EDataTypeFeatures::StringType) {
ValueRelease(EValueRepresentation::String, backOne, ctx, block);
ValueRelease(EValueRepresentation::String, backTwo, ctx, block);
}
const auto result = GenEqualsFunction(types.back().first, types.back().second, backOne, backTwo, ctx, block);
ReturnInst::Create(context, result, block);
}
block = stop;
ReturnInst::Create(context, ConstantInt::getFalse(context), block);
} else {
const auto result = GenEqualsFunction(types.front().first, types.front().second, lv, rv, ctx, block);
ReturnInst::Create(context, result, block);
}
return ctx.Func;
}
Function* GenerateHashFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, bool isTuple, const TKeyTypes& types) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto ptrType = PointerType::getUnqual(valueType);
const auto returnType = Type::getInt64Ty(context);
const auto funcType = codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(returnType, {valueType}, false):
FunctionType::get(returnType, {ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
const auto arg = LoadIfPointer(&*ctx.Func->arg_begin(), block);
if (isTuple) {
if (types.empty()) {
ReturnInst::Create(context, ConstantInt::get(returnType, 0), block);
return ctx.Func;
}
const auto fast = BasicBlock::Create(context, "fast", ctx.Func);
const auto slow = BasicBlock::Create(context, "slow", ctx.Func);
const auto elementsType = ArrayType::get(valueType, types.size());
const auto elementsPtrType = PointerType::getUnqual(elementsType);
const auto elementsPtr = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElements>(elementsPtrType, arg, ctx.Codegen, block);
const auto null = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, elementsPtr, ConstantPointerNull::get(elementsPtrType), "null", block);
BranchInst::Create(slow, fast, null, block);
{
block = fast;
const auto elements = new LoadInst(elementsType, elementsPtr, "elements", block);
auto result = static_cast<Value*>(ConstantInt::get(returnType, 0));
for (auto i = 0U; i < types.size(); ++i) {
const auto next = ExtractValueInst::Create(elements, i, (TString("next_") += ToString(i)).c_str(), block);
const auto plus = GenHashFunction(types[i].first, types[i].second, next, ctx, block);
result = GenCombineHashes(result, plus, block);
}
ReturnInst::Create(context, result, block);
}
{
block = slow;
const auto element = new AllocaInst(valueType, 0U, "element", block);
auto result = static_cast<Value*>(ConstantInt::get(returnType, 0));
for (auto i = 0U; i < types.size(); ++i) {
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetElement>(element, arg, ctx.Codegen, block, ConstantInt::get(Type::getInt32Ty(context), i));
const auto next = new LoadInst(valueType, element, (TString("next_") += ToString(i)).c_str(), block);
if (NUdf::GetDataTypeInfo(types[i].first).Features & NUdf::EDataTypeFeatures::StringType) {
ValueRelease(EValueRepresentation::String, next, ctx, block);
}
const auto plus = GenHashFunction(types[i].first, types[i].second, next, ctx, block);
result = GenCombineHashes(result, plus, block);
}
ReturnInst::Create(context, result, block);
}
} else {
const auto result = GenHashFunction(types.front().first, types.front().second, arg, ctx, block);
ReturnInst::Create(context, result, block);
}
return ctx.Func;
}
Function* GenerateEqualsFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, const TKeyTypes& types) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto elementsType = ArrayType::get(valueType, types.size());
const auto ptrType = PointerType::getUnqual(elementsType);
const auto returnType = Type::getInt1Ty(context);
const auto funcType = FunctionType::get(returnType, {ptrType, ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
const auto lv = &*args;
const auto rv = &*++args;
if (types.empty()) {
ReturnInst::Create(context, ConstantInt::getTrue(context), block);
return ctx.Func;
}
const auto elementsOne = new LoadInst(elementsType, lv, "elements_one", block);
const auto elementsTwo = new LoadInst(elementsType, rv, "elements_two", block);
const auto stop = BasicBlock::Create(context, "stop", ctx.Func);
ReturnInst::Create(context, ConstantInt::getFalse(context), stop);
const auto last = types.size() - 1U;
for (ui32 i = 0U; i < last; ++i) {
const auto nextOne = ExtractValueInst::Create(elementsOne, i, (TString("next_one_") += ToString(i)).c_str(), block);
const auto nextTwo = ExtractValueInst::Create(elementsTwo, i, (TString("next_two_") += ToString(i)).c_str(), block);
const auto step = BasicBlock::Create(context, (TString("step_") += ToString(i)).c_str(), ctx.Func);
const auto test = GenEqualsFunction(types[i].first, types[i].second, nextOne, nextTwo, ctx, block);
BranchInst::Create(step, stop, test, block);
block = step;
}
const auto backOne = ExtractValueInst::Create(elementsOne, last, "back_one", block);
const auto backTwo = ExtractValueInst::Create(elementsTwo, last, "back_two", block);
const auto result = GenEqualsFunction(types.back().first, types.back().second, backOne, backTwo, ctx, block);
ReturnInst::Create(context, result, block);
return ctx.Func;
}
Function* GenerateHashFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, const TKeyTypes& types) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto elementsType = ArrayType::get(valueType, types.size());
const auto ptrType = PointerType::getUnqual(elementsType);
const auto returnType = Type::getInt64Ty(context);
const auto funcType = FunctionType::get(returnType, {ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
if (types.empty()) {
ReturnInst::Create(context, ConstantInt::get(returnType, 0), block);
return ctx.Func;
}
const auto arg = &*ctx.Func->arg_begin();
const auto elements = new LoadInst(elementsType, arg, "elements", block);
if (types.size() > 1U) {
auto result = static_cast<Value*>(ConstantInt::get(returnType, 0));
for (auto i = 0U; i < types.size(); ++i) {
const auto item = ExtractValueInst::Create(elements, i, (TString("item_") += ToString(i)).c_str(), block);
const auto plus = GenHashFunction(types[i].first, types[i].second, item, ctx, block);
result = GenCombineHashes(result, plus, block);
}
ReturnInst::Create(context, result, block);
} else {
const auto value = ExtractValueInst::Create(elements, 0, "value", block);
const auto result = GenHashFunction(types.front().first, types.front().second, value, ctx, block);
ReturnInst::Create(context, result, block);
}
return ctx.Func;
}
Function* GenerateCompareFunction(NYql::NCodegen::ICodegen& codegen, const TString& name, const TKeyTypes& types) {
auto& module = codegen.GetModule();
if (const auto f = module.getFunction(name.c_str()))
return f;
auto& context = codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto elementsType = ArrayType::get(valueType, types.size());
const auto ptrType = PointerType::getUnqual(elementsType);
const auto dirsType = ArrayType::get(Type::getInt1Ty(context), types.size());
const auto ptrDirsType = PointerType::getUnqual(dirsType);
const auto returnType = Type::getInt32Ty(context);
const auto funcType = FunctionType::get(returnType, {ptrDirsType, ptrType, ptrType}, false);
TCodegenContext ctx(codegen);
ctx.AlwaysInline = true;
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
const auto main = BasicBlock::Create(context, "main", ctx.Func);
auto block = main;
const auto dp = &*args;
const auto lv = &*++args;
const auto rv = &*++args;
if (types.empty()) {
ReturnInst::Create(context, ConstantInt::get(returnType, 0), block);
return ctx.Func;
}
const auto directions = new LoadInst(dirsType, dp, "directions", block);
const auto elementsOne = new LoadInst(elementsType, lv, "elements_one", block);
const auto elementsTwo = new LoadInst(elementsType, rv, "elements_two", block);
const auto zero = ConstantInt::get(returnType, 0);
for (auto i = 0U; i < types.size(); ++i) {
const auto nextOne = ExtractValueInst::Create(elementsOne, i, (TString("next_one_") += ToString(i)).c_str(), block);
const auto nextTwo = ExtractValueInst::Create(elementsTwo, i, (TString("next_two_") += ToString(i)).c_str(), block);
const auto exit = BasicBlock::Create(context, (TString("exit_") += ToString(i)).c_str(), ctx.Func);
const auto step = BasicBlock::Create(context, (TString("step_") += ToString(i)).c_str(), ctx.Func);
const auto test = GenCompareFunction(types[i].first, types[i].second, nextOne, nextTwo, ctx, block);
const auto skip = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, zero, test, (TString("skip_") += ToString(i)).c_str(), block);
BranchInst::Create(step, exit, skip, block);
block = exit;
const auto dir = ExtractValueInst::Create(directions, i, (TString("dir_") += ToString(i)).c_str(), block);
const auto neg = BinaryOperator::CreateNeg(test, (TString("neg_") += ToString(i)).c_str(), block);
const auto out = SelectInst::Create(dir, test, neg, (TString("neg_") += ToString(i)).c_str(), block);
ReturnInst::Create(context, out, block);
block = step;
}
ReturnInst::Create(context, zero, block);
return ctx.Func;
}
void GenInvalidate(const TCodegenContext& ctx, const std::vector<std::pair<ui32, EValueRepresentation>>& invalidationSet, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto values = ctx.GetMutables();
for (const auto& index : invalidationSet) {
const auto invPtr = GetElementPtrInst::CreateInBounds(valueType, values, {ConstantInt::get(indexType, index.first)}, "inv_ptr", block);
ValueUnRef(index.second, invPtr, ctx, block);
new StoreInst(GetInvalid(context), invPtr, block);
}
}
TUnboxedImmutableCodegeneratorNode::TUnboxedImmutableCodegeneratorNode(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& value)
: TUnboxedImmutableComputationNode(memInfo, std::move(value))
{}
Value* TUnboxedImmutableCodegeneratorNode::CreateGetValue(const TCodegenContext& ctx, BasicBlock*&) const {
return ConstantInt::get(Type::getInt128Ty(ctx.Codegen.GetContext()), APInt(128, 2, reinterpret_cast<const uint64_t*>(&UnboxedValue)));
}
TExternalCodegeneratorNode::TExternalCodegeneratorNode(TComputationMutables& mutables, EValueRepresentation kind)
: TExternalComputationNode(mutables, kind)
{}
TExternalCodegeneratorRootNode::TExternalCodegeneratorRootNode(TComputationMutables& mutables, EValueRepresentation kind)
: TExternalCodegeneratorNode(mutables, kind)
{}
NUdf::TUnboxedValue TExternalCodegeneratorRootNode::GetValue(TComputationContext& compCtx) const {
if (compCtx.ExecuteLLVM && GetFunction)
return GetFunction(&compCtx);
return TExternalComputationNode::GetValue(compCtx);
}
void TExternalCodegeneratorRootNode::SetValue(TComputationContext& compCtx, NUdf::TUnboxedValue&& newValue) const {
if (compCtx.ExecuteLLVM && SetFunction)
return SetFunction(&compCtx, newValue.Release());
TExternalComputationNode::SetValue(compCtx, std::move(newValue));
}
TString TExternalCodegeneratorRootNode::MakeName(const TString& method) const {
TStringStream out;
out << DebugString() << "::" << method << "_(" << static_cast<const void*>(this) << ").";
return out.Str();
}
void TExternalCodegeneratorRootNode::FinalizeFunctions(NYql::NCodegen::ICodegen& codegen) {
if (GetValueFunc)
GetFunction = reinterpret_cast<TGetPtr>(codegen.GetPointerToFunction(GetValueFunc));
if (SetValueFunc)
SetFunction = reinterpret_cast<TSetPtr>(codegen.GetPointerToFunction(SetValueFunc));
}
void TExternalCodegeneratorRootNode::GenerateFunctions(NYql::NCodegen::ICodegen& codegen) {
GetValueFunc = GenerateGetValue(codegen);
SetValueFunc = GenerateSetValue(codegen);
codegen.ExportSymbol(GetValueFunc);
codegen.ExportSymbol(SetValueFunc);
}
Function* TExternalCodegeneratorRootNode::GenerateGetValue(NYql::NCodegen::ICodegen& codegen) {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto& name = MakeName("Get");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto valueType = Type::getInt128Ty(context);
const auto contextType = GetCompContextType(context);
const auto funcType = codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(valueType, {PointerType::getUnqual(contextType)}, false):
FunctionType::get(Type::getVoidTy(context), {PointerType::getUnqual(valueType), PointerType::getUnqual(contextType)}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
if (codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows) {
auto& firstArg = *args++;
firstArg.addAttr(Attribute::StructRet);
firstArg.addAttr(Attribute::NoAlias);
}
auto main = BasicBlock::Create(context, "main", ctx.Func);
ctx.Ctx = &*args;
ctx.Ctx->addAttr(Attribute::NonNull);
const auto get = CreateGetValue(ctx, main);
if (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
ReturnInst::Create(context, get, main);
} else {
new StoreInst(get, &*--args, main);
ReturnInst::Create(context, main);
}
return ctx.Func;
}
Function* TExternalCodegeneratorRootNode::GenerateSetValue(NYql::NCodegen::ICodegen& codegen) {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto& name = MakeName("Set");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto intType = Type::getInt128Ty(context);
const auto contextType = GetCompContextType(context);
const auto valueType = codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows ?
(Type*)PointerType::getUnqual(intType) : (Type*)intType;
const auto funcType = FunctionType::get(Type::getVoidTy(context), {PointerType::getUnqual(contextType), valueType}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
auto main = BasicBlock::Create(context, "main", ctx.Func);
ctx.Ctx = &*args;
ctx.Ctx->addAttr(Attribute::NonNull);
const auto valueArg = &*++args;
if (codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows) {
const auto value = new LoadInst(valueArg->getType()->getPointerElementType(), valueArg, "load_value", main);
CreateSetValue(ctx, main, value);
} else {
CreateSetValue(ctx, main, valueArg);
}
ReturnInst::Create(context, main);
return ctx.Func;
}
Value* TExternalCodegeneratorNode::CreateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
if (ValueGetterBuilder) {
llvm::Function * ValueGetter = ValueGetterBuilder(ctx);
return CallInst::Create(ValueGetter, {ctx.Ctx}, "getter", block);
}
if (ValueBuilder) {
llvm::Value * TemporaryValue = ValueBuilder(ctx);
return LoadIfPointer(TemporaryValue, block);
}
MKQL_ENSURE(!Getter, "Wrong LLVM function generation order.");
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(indexType, ValueIndex)}, "value_ptr", block);
const auto value = new LoadInst(valueType, valuePtr, "value", block);
return value;
}
Value* TExternalCodegeneratorNode::CreateRefValue(const TCodegenContext& ctx, BasicBlock*& block) const {
CreateInvalidate(ctx, block);
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto values = ctx.GetMutables();
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, values, {ConstantInt::get(indexType, ValueIndex)}, "value_ptr", block);
return valuePtr;
}
void TExternalCodegeneratorNode::CreateSetValue(const TCodegenContext& ctx, BasicBlock*& block, Value* value) const {
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto values = ctx.GetMutables();
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, values, {ConstantInt::get(indexType, ValueIndex)}, "value_ptr", block);
if (value->getType()->isPointerTy()) {
ValueUnRef(RepresentationKind, valuePtr, ctx, block);
const auto load = new LoadInst(valueType, value, "value", block);
new StoreInst(load, valuePtr, block);
new StoreInst(ConstantInt::get(load->getType(), 0), value, block);
} else {
if (EValueRepresentation::Embedded == RepresentationKind) {
new StoreInst(value, valuePtr, block);
} else {
const auto load = new LoadInst(valueType, valuePtr, "value", block);
const auto equal = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, value, load, "equal", block);
const auto skip = BasicBlock::Create(context, "skip", ctx.Func);
const auto refs = BasicBlock::Create(context, "refs", ctx.Func);
BranchInst::Create(skip, refs, equal, block);
block = refs;
ValueUnRef(RepresentationKind, valuePtr, ctx, block);
new StoreInst(value, valuePtr, block);
ValueAddRef(RepresentationKind, valuePtr, ctx, block);
BranchInst::Create(skip, block);
block = skip;
}
}
CreateInvalidate(ctx, block);
}
Value* TExternalCodegeneratorNode::CreateSwapValue(const TCodegenContext& ctx, BasicBlock*& block, Value* value) const {
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto values = ctx.GetMutables();
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, values, {ConstantInt::get(indexType, ValueIndex)}, "value_ptr", block);
const auto output = new LoadInst(valueType, valuePtr, "output", block);
ValueRelease(RepresentationKind, output, ctx, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(valueType, value, "load", block);
new StoreInst(load, valuePtr, block);
new StoreInst(ConstantInt::get(load->getType(), 0), value, block);
} else {
ValueAddRef(RepresentationKind, value, ctx, block);
new StoreInst(value, valuePtr, block);
}
CreateInvalidate(ctx, block);
return output;
}
void TExternalCodegeneratorNode::CreateInvalidate(const TCodegenContext& ctx, BasicBlock*& block) const {
GenInvalidate(ctx, InvalidationSet, block);
}
void TExternalCodegeneratorNode::SetValueBuilder(TValueBuilder valueBuilder)
{
ValueBuilder = std::move(valueBuilder);
}
void TExternalCodegeneratorNode::SetValueGetterBuilder(TValueGetterBuilder valueGetterBuilder)
{
ValueGetterBuilder = std::move(valueGetterBuilder);
}
void TWideFlowProxyCodegeneratorNode::CreateInvalidate(const TCodegenContext& ctx, BasicBlock*& block) const {
GenInvalidate(ctx, InvalidationSet, block);
}
void TWideFlowProxyCodegeneratorNode::SetGenerator(TGenerator&& generator) {
Generator = std::move(generator);
}
ICodegeneratorInlineWideNode::TGenerateResult
TWideFlowProxyCodegeneratorNode::GenGetValues(const TCodegenContext& ctx, BasicBlock*& block) const {
return Generator(ctx, block);
}
Value* GetOptionalValue(LLVMContext& context, Value* value, BasicBlock* block) {
const auto type = Type::getInt128Ty(context);
const auto data = ConstantInt::get(type, 0xFFFFFFFFFFFFFFFFULL);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, value, data, "check", block);
const auto decr = BinaryOperator::CreateSub(value, ConstantInt::get(type, 1), "decr", block);
const auto result = SelectInst::Create(check, value, decr, "result", block);
return result;
}
Value* MakeOptional(LLVMContext& context, Value* value, BasicBlock* block) {
const auto type = Type::getInt128Ty(context);
const auto data = ConstantInt::get(type, 0xFFFFFFFFFFFFFFFFULL);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, value, data, "check", block);
const auto incr = BinaryOperator::CreateAdd(value, ConstantInt::get(type, 1), "incr", block);
const auto result = SelectInst::Create(check, value, incr, "result", block);
return result;
}
ConstantInt* GetTrue(LLVMContext &context) {
const uint64_t init[] = {1ULL, 0x100000000000000ULL};
return ConstantInt::get(context, APInt(128, 2, init));
}
ConstantInt* GetFalse(LLVMContext &context) {
const uint64_t init[] = {0ULL, 0x100000000000000ULL};
return ConstantInt::get(context, APInt(128, 2, init));
}
ConstantInt* GetDecimalPlusInf(LLVMContext &context) {
const auto& pair = NYql::NDecimal::MakePair(+NYql::NDecimal::Inf());
const uint64_t init[] = {pair.first, pair.second};
return ConstantInt::get(context, APInt(128, 2, init));
}
ConstantInt* GetDecimalMinusInf(LLVMContext &context) {
const auto& pair = NYql::NDecimal::MakePair(-NYql::NDecimal::Inf());
const uint64_t init[] = {pair.first, pair.second};
return ConstantInt::get(context, APInt(128, 2, init));
}
ConstantInt* GetDecimalNan(LLVMContext &context) {
const auto& pair = NYql::NDecimal::MakePair(NYql::NDecimal::Nan());
const uint64_t init[] = {pair.first, pair.second};
return ConstantInt::get(context, APInt(128, 2, init));
}
ConstantInt* GetDecimalMinusNan(LLVMContext &context) {
const auto& pair = NYql::NDecimal::MakePair(-NYql::NDecimal::Nan());
const uint64_t init[] = {pair.first, pair.second};
return ConstantInt::get(context, APInt(128, 2, init));
}
static constexpr ui64 InvalidData = std::numeric_limits<ui64>::max();
static constexpr ui64 FinishData = InvalidData - 1ULL;
static constexpr ui64 YieldData = InvalidData;
ConstantInt* GetEmpty(LLVMContext &context) {
return ConstantInt::get(Type::getInt128Ty(context), 0ULL);
}
ConstantInt* GetInvalid(LLVMContext &context) {
return ConstantInt::get(Type::getInt128Ty(context), InvalidData);
}
ConstantInt* GetFinish(LLVMContext &context) {
return ConstantInt::get(Type::getInt128Ty(context), FinishData);
}
ConstantInt* GetYield(LLVMContext &context) {
return ConstantInt::get(Type::getInt128Ty(context), YieldData);
}
ConstantInt* GetConstant(ui64 value, LLVMContext &context) {
const uint64_t init[] = {value, 0x100000000000000ULL};
return ConstantInt::get(context, APInt(128, 2, init));
}
Value* IsExists(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, v, ConstantInt::get(v->getType(), 0ULL), "exists", block);
}
Value* IsEmpty(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, v, ConstantInt::get(v->getType(), 0ULL), "empty", block);
}
Value* IsInvalid(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, v, ConstantInt::get(v->getType(), InvalidData), "invalid", block);
}
Value* IsValid(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, v, ConstantInt::get(v->getType(), InvalidData), "valid", block);
}
Value* IsFinish(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, v, ConstantInt::get(v->getType(), FinishData), "finish", block);
}
Value* IsYield(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, v, ConstantInt::get(v->getType(), YieldData), "yield", block);
}
Value* IsSpecial(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return BinaryOperator::CreateOr(IsFinish(v, block), IsYield(v, block), "special", block);
}
Value* HasValue(Value* value, BasicBlock* block) {
const auto v = LoadIfPointer(value, block);
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, v, ConstantInt::get(v->getType(), InvalidData), "has", block);
}
Value* MakeBoolean(Value* boolean , LLVMContext &context, BasicBlock* block) {
return SelectInst::Create(boolean, GetTrue(context), GetFalse(context), "result", block);
}
Value* SetterForInt128(Value* value, BasicBlock* block) {
const uint64_t mask[] = {0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFULL};
const auto drop = ConstantInt::get(value->getType(), APInt(128, 2, mask));
const auto data = BinaryOperator::CreateAnd(value, drop, "and", block);
const uint64_t init[] = {0ULL, 0x100000000000000ULL}; // Embedded
const auto meta = ConstantInt::get(value->getType(), APInt(128, 2, init));
const auto full = BinaryOperator::CreateOr(data, meta, "or", block);
return full;
}
Value* GetterForInt128(Value* value, BasicBlock* block) {
const uint64_t init[] = {0ULL, 0x80000000000000ULL};
const auto test = ConstantInt::get(value->getType(), APInt(128, 2, init));
const auto sign = BinaryOperator::CreateAnd(value, test, "and", block);
const uint64_t fill[] = {0ULL, 0xFF00000000000000ULL};
const auto sext = ConstantInt::get(value->getType(), APInt(128, 2, fill));
const auto minus = BinaryOperator::CreateOr(value, sext, "or", block);
const uint64_t mask[] = {0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFULL};
const auto trun = ConstantInt::get(value->getType(), APInt(128, 2, mask));
const auto plus = BinaryOperator::CreateAnd(value, trun, "and", block);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, sign, ConstantInt::get(sign->getType(), 0), "check", block);
const auto result = SelectInst::Create(check, plus, minus, "result", block);
return result;
}
Value* GetterForTimezone(LLVMContext& context, Value* value, BasicBlock* block) {
const auto lshr = BinaryOperator::CreateLShr(value, ConstantInt::get(value->getType(), 64ULL), "lshr", block);
const auto trunc = CastInst::Create(Instruction::Trunc, lshr, Type::getInt16Ty(context), "trunc", block);
return trunc;
}
template<> Type* GetTypeFor<bool>(LLVMContext &context) { return Type::getInt1Ty(context); }
template<> Type* GetTypeFor<ui8>(LLVMContext &context) { return Type::getInt8Ty(context); }
template<> Type* GetTypeFor<i8>(LLVMContext &context) { return Type::getInt8Ty(context); }
template<> Type* GetTypeFor<i16>(LLVMContext &context) { return Type::getInt16Ty(context); }
template<> Type* GetTypeFor<ui16>(LLVMContext &context) { return Type::getInt16Ty(context); }
template<> Type* GetTypeFor<i32>(LLVMContext &context) { return Type::getInt32Ty(context); }
template<> Type* GetTypeFor<ui32>(LLVMContext &context) { return Type::getInt32Ty(context); }
template<> Type* GetTypeFor<i64>(LLVMContext &context) { return Type::getInt64Ty(context); }
template<> Type* GetTypeFor<ui64>(LLVMContext &context) { return Type::getInt64Ty(context); }
template<> Type* GetTypeFor<float>(LLVMContext &context) { return Type::getFloatTy(context); }
template<> Type* GetTypeFor<double>(LLVMContext &context) { return Type::getDoubleTy(context); }
void AddRefBoxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto load = value->getType()->isPointerTy() ? new LoadInst(value->getType()->getPointerElementType(), value, "load", block) : value;
const auto half = CastInst::Create(Instruction::Trunc, load, Type::getInt64Ty(context), "half", block);
const auto counterType = Type::getInt32Ty(context);
const auto type = StructType::get(context, {PointerType::getUnqual(StructType::get(context)), counterType, Type::getInt16Ty(context)});
const auto boxptr = CastInst::Create(Instruction::IntToPtr, half, PointerType::getUnqual(type), "boxptr", block);
const auto cntptr = GetElementPtrInst::CreateInBounds(type, boxptr, {ConstantInt::get(Type::getInt32Ty(context), 0), ConstantInt::get(Type::getInt32Ty(context), 1)}, "cntptr", block);
const auto refs = new LoadInst(counterType, cntptr, "refs", block);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 4)
if constexpr (EnableStaticRefcount) {
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto skip = BasicBlock::Create(context, "skip", ctx.Func);
const auto magic = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, refs, ConstantInt::get(refs->getType(), 0), "magic", block);
BranchInst::Create(skip, work, magic, block);
block = work;
const auto incr = BinaryOperator::CreateAdd(refs, ConstantInt::get(refs->getType(), 1), "incr", block);
new StoreInst(incr, cntptr, block);
BranchInst::Create(skip, block);
block = skip;
} else {
const auto incr = BinaryOperator::CreateAdd(refs, ConstantInt::get(refs->getType(), 1), "incr", block);
new StoreInst(incr, cntptr, block);
}
#else
const auto incr = BinaryOperator::CreateAdd(refs, ConstantInt::get(refs->getType(), 1), "incr", block);
new StoreInst(incr, cntptr, block);
#endif
}
void UnRefBoxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto load = value->getType()->isPointerTy() ? new LoadInst(value->getType()->getPointerElementType(), value, "load", block) : value;
const auto half = CastInst::Create(Instruction::Trunc, load, Type::getInt64Ty(context), "half", block);
const auto counterType = Type::getInt32Ty(context);
const auto type = StructType::get(context, {PointerType::getUnqual(StructType::get(context)), counterType, Type::getInt16Ty(context)});
const auto boxptr = CastInst::Create(Instruction::IntToPtr, half, PointerType::getUnqual(type), "boxptr", block);
const auto cntptr = GetElementPtrInst::CreateInBounds(type, boxptr, {ConstantInt::get(Type::getInt32Ty(context), 0), ConstantInt::get(Type::getInt32Ty(context), 1)}, "cntptr", block);
const auto refs = new LoadInst(counterType, cntptr, "refs", block);
const auto live = BasicBlock::Create(context, "live", ctx.Func);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 4)
if constexpr (EnableStaticRefcount) {
const auto magic = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, refs, ConstantInt::get(refs->getType(), 0), "magic", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
BranchInst::Create(live, work, magic, block);
block = work;
}
#endif
const auto decr = BinaryOperator::CreateSub(refs, ConstantInt::get(refs->getType(), 1), "decr", block);
new StoreInst(decr, cntptr, block);
const auto test = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, decr, ConstantInt::get(decr->getType(), 0), "many", block);
const auto kill = BasicBlock::Create(context, "kill", ctx.Func);
BranchInst::Create(live, kill, test, block);
block = kill;
const auto fnType = FunctionType::get(Type::getVoidTy(context), {boxptr->getType()}, false);
const auto name = "DeleteBoxed";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DeleteBoxed));
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType).getCallee();
CallInst::Create(fnType, func, {boxptr}, "", block);
BranchInst::Create(live, block);
block = live;
}
void CleanupBoxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto load = value->getType()->isPointerTy() ? new LoadInst(value->getType()->getPointerElementType(), value, "load", block) : value;
const auto half = CastInst::Create(Instruction::Trunc, load, Type::getInt64Ty(context), "half", block);
const auto counterType = Type::getInt32Ty(context);
const auto type = StructType::get(context, {PointerType::getUnqual(StructType::get(context)), counterType, Type::getInt16Ty(context)});
const auto boxptr = CastInst::Create(Instruction::IntToPtr, half, PointerType::getUnqual(type), "boxptr", block);
const auto cntptr = GetElementPtrInst::CreateInBounds(type, boxptr, {ConstantInt::get(Type::getInt32Ty(context), 0), ConstantInt::get(Type::getInt32Ty(context), 1)}, "cntptr", block);
const auto refs = new LoadInst(counterType, cntptr, "refs", block);
const auto test = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, refs, ConstantInt::get(refs->getType(), 0), "many", block);
const auto live = BasicBlock::Create(context, "live", ctx.Func);
const auto kill = BasicBlock::Create(context, "kill", ctx.Func);
BranchInst::Create(live, kill, test, block);
block = kill;
const auto fnType = FunctionType::get(Type::getVoidTy(context), {boxptr->getType()}, false);
const auto name = "DeleteBoxed";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DeleteBoxed));
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType).getCallee();
CallInst::Create(fnType, func, {boxptr}, "", block);
BranchInst::Create(live, block);
block = live;
}
template<bool IncOrDec>
void ChangeRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto type8 = Type::getInt8Ty(context);
const auto type32 = Type::getInt32Ty(context);
const auto mark = GetMarkFromUnboxed(value, ctx, block);
const auto boxb = BasicBlock::Create(context, "boxb", ctx.Func);
const auto strb = BasicBlock::Create(context, "strb", ctx.Func);
const auto doit = BasicBlock::Create(context, "doit", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto refsPtrType = PointerType::getUnqual(type32);
const auto refsptr = PHINode::Create(refsPtrType, 2U, "refsptr", doit);
const auto choise = SwitchInst::Create(mark, done, 2U, block);
choise->addCase(ConstantInt::get(type8, 2), strb);
choise->addCase(ConstantInt::get(type8, 3), boxb);
{
block = strb;
const auto strptr = GetPointerFromUnboxed<false>(value, ctx, block);
const auto elemptr = GetElementPtrInst::CreateInBounds(strptr->getType()->getPointerElementType(), strptr, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 1)}, "elemptr", block);
refsptr->addIncoming(elemptr, block);
BranchInst::Create(doit, block);
}
{
block = boxb;
const auto boxptr = GetPointerFromUnboxed<true>(value, ctx, block);;
const auto elemptr = GetElementPtrInst::CreateInBounds(boxptr->getType()->getPointerElementType(), boxptr, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 1)}, "elemptr", block);
refsptr->addIncoming(elemptr, block);
BranchInst::Create(doit, block);
}
block = doit;
const auto refs = new LoadInst(type32, refsptr, "refs", block);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 4)
if constexpr (EnableStaticRefcount) {
const auto magic = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, refs, ConstantInt::get(refs->getType(), 0), "magic", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
BranchInst::Create(done, work, magic, block);
block = work;
}
#endif
const auto next = IncOrDec ?
BinaryOperator::CreateAdd(refs, ConstantInt::get(refs->getType(), 1), "incr", block):
BinaryOperator::CreateSub(refs, ConstantInt::get(refs->getType(), 1), "decr", block);
new StoreInst(next, refsptr, block);
BranchInst::Create(done, block);
block = done;
}
template<bool Decrement>
void CheckRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto type8 = Type::getInt8Ty(context);
const auto type32 = Type::getInt32Ty(context);
const auto mark = GetMarkFromUnboxed(value, ctx, block);
const auto boxb = BasicBlock::Create(context, "boxb", ctx.Func);
const auto strb = BasicBlock::Create(context, "strb", ctx.Func);
const auto nope = BasicBlock::Create(context, "nope", ctx.Func);
const auto choise = SwitchInst::Create(mark, nope, 2U, block);
choise->addCase(ConstantInt::get(type8, 2), strb);
choise->addCase(ConstantInt::get(type8, 3), boxb);
{
block = strb;
const auto strptr = GetPointerFromUnboxed<false>(value, ctx, block);
const auto refptr = GetElementPtrInst::CreateInBounds(strptr->getType()->getPointerElementType(), strptr, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 1)}, "refptr", block);
const auto refs = new LoadInst(type32, refptr, "refs", block);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 4)
if constexpr (EnableStaticRefcount) {
const auto magic = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, refs, ConstantInt::get(refs->getType(), 0), "magic", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
BranchInst::Create(nope, work, magic, block);
block = work;
}
#endif
Value* test = refs;
if constexpr (Decrement) {
const auto decr = BinaryOperator::CreateSub(refs, ConstantInt::get(refs->getType(), 1), "decr", block);
new StoreInst(decr, refptr, block);
test = decr;
}
const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, test, ConstantInt::get(test->getType(), 0), "test", block);
const auto free = BasicBlock::Create(context, "free", ctx.Func);
BranchInst::Create(nope, free, good, block);
block = free;
const auto fnType = FunctionType::get(Type::getVoidTy(context), {strptr->getType()}, false);
const auto name = "DeleteString";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DeleteString));
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType).getCallee();
CallInst::Create(fnType, func, {strptr}, "", block);
BranchInst::Create(nope, block);
}
{
block = boxb;
const auto boxptr = GetPointerFromUnboxed<true>(value, ctx, block);;
const auto refptr = GetElementPtrInst::CreateInBounds(boxptr->getType()->getPointerElementType(), boxptr, {ConstantInt::get(type32, 0), ConstantInt::get(type32, 1)}, "cntptr", block);
const auto refs = new LoadInst(type32, refptr, "refs", block);
#if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 4)
if constexpr (EnableStaticRefcount) {
const auto magic = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, refs, ConstantInt::get(refs->getType(), 0), "magic", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
BranchInst::Create(nope, work, magic, block);
block = work;
}
#endif
Value* test = refs;
if constexpr (Decrement) {
const auto decr = BinaryOperator::CreateSub(refs, ConstantInt::get(refs->getType(), 1), "decr", block);
new StoreInst(decr, refptr, block);
test = decr;
}
const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, test, ConstantInt::get(test->getType(), 0), "test", block);
const auto kill = BasicBlock::Create(context, "kill", ctx.Func);
BranchInst::Create(nope, kill, good, block);
block = kill;
const auto fnType = FunctionType::get(Type::getVoidTy(context), {boxptr->getType()}, false);
const auto name = "DeleteBoxed";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DeleteBoxed));
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType).getCallee();
CallInst::Create(fnType, func, {boxptr}, "", block);
BranchInst::Create(nope, block);
}
block = nope;
}
#ifdef MAKE_UNBOXED_VALUE_LLVM_REFCOUNTION_FUNCTIONS
Function* GenRefCountFunction(const char* label, void (*func)(Value*, const TCodegenContext&, BasicBlock*&), Type* type, NYql::NCodegen::ICodegen& codegen) {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
const auto name = TString(label) += (type->isPointerTy() ? "Ptr" : "Val");
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto funcType = FunctionType::get(Type::getVoidTy(context), {type}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee()).getCallee();
auto main = BasicBlock::Create(context, "main", ctx.Func);
auto value = &*ctx.Func->arg_begin();
func(value, ctx, main);
ReturnInst::Create(context, main);
return ctx.Func;
}
void AddRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
CallInst::Create(GenRefCountFunction(__func__, &ChangeRefUnboxed<true>, value->getType(), ctx.Codegen), {value}, "", block);
}
void ReleaseUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
CallInst::Create(GenRefCountFunction(__func__, &ChangeRefUnboxed<false>, value->getType(), ctx.Codegen), {value}, "", block);
}
void UnRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
CallInst::Create(GenRefCountFunction(__func__, &CheckRefUnboxed<true>, value->getType(), ctx.Codegen), {value}, "", block);
}
void CleanupUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
CallInst::Create(GenRefCountFunction(__func__, &CheckRefUnboxed<false>, value->getType(), ctx.Codegen), {value}, "", block);
}
#else
void AddRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
return ChangeRefUnboxed<true>(value, ctx, block);
}
void ReleaseUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
return ChangeRefUnboxed<false>(value, ctx, block);
}
void UnRefUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
return CheckRefUnboxed<true>(value, ctx, block);
}
void CleanupUnboxed(Value* value, const TCodegenContext& ctx, BasicBlock*& block) {
return CheckRefUnboxed<false>(value, ctx, block);
}
#endif
void SafeUnRefUnboxed(Value* pointer, const TCodegenContext& ctx, BasicBlock*& block) {
if (const auto itemType = pointer->getType()->getPointerElementType(); itemType->isArrayTy()) {
const auto indexType = Type::getInt64Ty(ctx.Codegen.GetContext());
Value* zeros = UndefValue::get(itemType);
for (ui32 idx = 0U; idx < itemType->getArrayNumElements(); ++idx) {
const auto item = GetElementPtrInst::CreateInBounds(itemType, pointer, { ConstantInt::get(indexType, 0), ConstantInt::get(indexType, idx) }, (TString("item_") += ToString(idx)).c_str(), block);
UnRefUnboxed(item, ctx, block);
zeros = InsertValueInst::Create(zeros, ConstantInt::get(itemType->getArrayElementType(), 0), {idx}, (TString("zero_") += ToString(idx)).c_str(), block);
}
new StoreInst(zeros, pointer, block);
} else {
UnRefUnboxed(pointer, ctx, block);
new StoreInst(ConstantInt::get(itemType, 0), pointer, block);
}
}
void ValueAddRef(EValueRepresentation kind, Value* pointer, const TCodegenContext& ctx, BasicBlock*& block) {
switch (kind) {
case EValueRepresentation::Embedded: return;
case EValueRepresentation::Boxed: // TODO
case EValueRepresentation::String: // TODO
case EValueRepresentation::Any: return AddRefUnboxed(pointer, ctx, block);
}
}
void ValueUnRef(EValueRepresentation kind, Value* pointer, const TCodegenContext& ctx, BasicBlock*& block) {
switch (kind) {
case EValueRepresentation::Embedded: return;
case EValueRepresentation::Boxed: // TODO
case EValueRepresentation::String: // TODO
case EValueRepresentation::Any: return UnRefUnboxed(pointer, ctx, block);
}
}
void ValueCleanup(EValueRepresentation kind, Value* pointer, const TCodegenContext& ctx, BasicBlock*& block) {
switch (kind) {
case EValueRepresentation::Embedded: return;
case EValueRepresentation::Boxed: // TODO
case EValueRepresentation::String: // TODO
case EValueRepresentation::Any: return CleanupUnboxed(pointer, ctx, block);
}
}
void ValueRelease(EValueRepresentation kind, Value* pointer, const TCodegenContext& ctx, BasicBlock*& block) {
switch (kind) {
case EValueRepresentation::Embedded: return;
case EValueRepresentation::Boxed: // TODO
case EValueRepresentation::String: // TODO
case EValueRepresentation::Any: return ReleaseUnboxed(pointer, ctx, block);
}
}
std::pair<Value*, Value*> GetVariantParts(Value* variant, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto type = Type::getInt32Ty(context);
const auto lshr = BinaryOperator::CreateLShr(variant, ConstantInt::get(variant->getType(), 122), "lshr", block);
const auto trunc = CastInst::Create(Instruction::Trunc, lshr, type, "trunc", block);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_NE, trunc, ConstantInt::get(type , 0), "check", block);
const auto boxed = BasicBlock::Create(context, "boxed", ctx.Func);
const auto embed = BasicBlock::Create(context, "embed", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto index = PHINode::Create(type, 2U, "index", done);
const auto item = PHINode::Create(variant->getType(), 2U, "index", done);
BranchInst::Create(embed, boxed, check, block);
{
block = embed;
const uint64_t init[] = {0xFFFFFFFFFFFFFFFFULL, 0x3FFFFFFFFFFFFFFULL};
const auto mask = ConstantInt::get(variant->getType(), APInt(128, 2, init));
const auto clean = BinaryOperator::CreateAnd(variant, mask, "clean", block);
const auto dec = BinaryOperator::CreateSub(trunc, ConstantInt::get(type, 1), "dec", block);
index->addIncoming(dec, block);
item->addIncoming(clean, block);
BranchInst::Create(done, block);
}
{
block = boxed;
const auto place = new AllocaInst(item->getType(), 0U, "place", &ctx.Func->getEntryBlock().back());
const auto idx = CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetVariantIndex>(type, variant, ctx.Codegen, block);
CallBoxedValueVirtualMethod<NUdf::TBoxedValueAccessor::EMethod::GetVariantItem>(place, variant, ctx.Codegen, block);
const auto clean = new LoadInst(item->getType(), place, "clean", block);
ValueRelease(EValueRepresentation::Any, clean, ctx, block);
index->addIncoming(idx, block);
item->addIncoming(clean, block);
BranchInst::Create(done, block);
}
block = done;
return std::make_pair(index, item);
}
Value* MakeVariant(Value* item, Value* variant, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
const auto boxed = BasicBlock::Create(context, "boxed", ctx.Func);
const auto embed = BasicBlock::Create(context, "embed", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
const auto result = PHINode::Create(item->getType(), 1U, "index", done);
const auto offset = ConstantInt::get(item->getType(), 122);
const auto lshr = BinaryOperator::CreateLShr(item, offset, "lshr", block);
const auto checkItem = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, lshr, ConstantInt::get(lshr->getType(), 0), "check_item", block);
const auto checkIndex = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, variant, ConstantInt::get(variant->getType(), (1U << 6U) - 1U), "check_index", block);
const auto check = BinaryOperator::CreateAnd(checkItem, checkIndex, "and", block);
BranchInst::Create(embed, boxed, check, block);
{
block = embed;
const auto index = BinaryOperator::CreateAdd(variant, ConstantInt::get(variant->getType(), 1), "index", block);
const auto extend = CastInst::Create(Instruction::ZExt, index, item->getType(), "extend", block);
const auto shift = BinaryOperator::CreateShl(extend, offset, "shift", block);
const auto output = BinaryOperator::CreateOr(item, shift, "output", block);
result->addIncoming(output, block);
BranchInst::Create(done, block);
}
{
block = boxed;
const auto factory = ctx.GetFactory();
const auto func = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&THolderFactory::CreateBoxedVariantHolder));
if (NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget()) {
const auto signature = FunctionType::get(item->getType(), {factory->getType(), item->getType(), variant->getType()}, false);
const auto creator = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(signature), "creator", block);
const auto output = CallInst::Create(signature, creator, {factory, item, variant}, "output", block);
result->addIncoming(output, block);
} else {
const auto place = new AllocaInst(item->getType(), 0U, "place", block);
new StoreInst(item, place, block);
const auto signature = FunctionType::get(Type::getVoidTy(context), {factory->getType(), place->getType(), place->getType(), variant->getType()}, false);
const auto creator = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(signature), "creator", block);
CallInst::Create(signature, creator, {factory, place, place, variant}, "", block);
const auto output = new LoadInst(item->getType(), place, "output", block);
result->addIncoming(output, block);
}
BranchInst::Create(done, block);
}
block = done;
return result;
}
Value* GetNodeValue(IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) {
if (const auto codegen = dynamic_cast<ICodegeneratorInlineNode*>(node))
return codegen->CreateGetValue(ctx, block);
auto& context = ctx.Codegen.GetContext();
const auto ptr = ConstantInt::get(Type::getInt64Ty(context), intptr_t(node));
const auto ptrType = PointerType::getUnqual(StructType::get(context));
const auto nodeThis = CastInst::Create(Instruction::IntToPtr, ptr, ptrType, "node_this", block);
const auto valueType = Type::getInt128Ty(context);
const auto retPtr = new AllocaInst(valueType, 0U, "return_ptr", &ctx.Func->getEntryBlock().back());
const auto funType = ctx.Codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(Type::getVoidTy(context), {retPtr->getType(), nodeThis->getType(), ctx.Ctx->getType()}, false):
FunctionType::get(Type::getVoidTy(context), {nodeThis->getType(), retPtr->getType(), ctx.Ctx->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto nodeVTable = CastInst::Create(Instruction::IntToPtr, ptr, PointerType::getUnqual(tableType), "node_vtable", block);
const auto table = new LoadInst(tableType, nodeVTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodIndex(&IComputationNode::GetValue))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
if (ctx.Codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
CallInst::Create(funType, func, {retPtr, nodeThis, ctx.Ctx}, "", block);
} else {
CallInst::Create(funType, func, {nodeThis, retPtr, ctx.Ctx}, "", block);
}
ValueRelease(node->GetRepresentation(), retPtr, ctx, block);
const auto result = new LoadInst(valueType, retPtr, "return", false, block);
return result;
}
void GetNodeValue(Value* value, IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) {
if (const auto codegen = dynamic_cast<ICodegeneratorInlineNode*>(node)) {
const auto v = codegen->CreateGetValue(ctx, block);
new StoreInst(v, value, block);
ValueAddRef(node->GetRepresentation(), value, ctx, block);
return;
}
auto& context = ctx.Codegen.GetContext();
const auto ptr = ConstantInt::get(Type::getInt64Ty(context), intptr_t(node));
const auto ptrType = PointerType::getUnqual(StructType::get(context));
const auto nodeThis = CastInst::Create(Instruction::IntToPtr, ptr, ptrType, "node_this", block);
const auto funType = ctx.Codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(Type::getVoidTy(context), {value->getType(), nodeThis->getType(), ctx.Ctx->getType()}, false):
FunctionType::get(Type::getVoidTy(context), {nodeThis->getType(), value->getType(), ctx.Ctx->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto nodeVTable = CastInst::Create(Instruction::IntToPtr, ptr, PointerType::getUnqual(tableType), "node_vtable", block);
const auto table = new LoadInst(tableType, nodeVTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodIndex(&IComputationNode::GetValue))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
if (ctx.Codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
CallInst::Create(funType, func, {value, nodeThis, ctx.Ctx}, "", block);
} else {
CallInst::Create(funType, func, {nodeThis, value, ctx.Ctx}, "", block);
}
}
ICodegeneratorInlineWideNode::TGenerateResult GetNodeValues(IComputationWideFlowNode* node, const TCodegenContext& ctx, BasicBlock*& block) {
if (const auto codegen = dynamic_cast<ICodegeneratorInlineWideNode*>(node))
return codegen->GenGetValues(ctx, block);
throw TNoCodegen();
}
Value* GenNewArray(const TCodegenContext& ctx, Value* size, Value* items, BasicBlock* block) {
auto& context = ctx.Codegen.GetContext();
const auto fact = ctx.GetFactory();
const auto func = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&THolderFactory::CreateDirectArrayHolder));
const auto valueType = Type::getInt128Ty(context);
if (NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget()) {
const auto funType = FunctionType::get(valueType, {fact->getType(), size->getType(), items->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(funType), "function", block);
return CallInst::Create(funType, funcPtr, {fact, size, items}, "array", block);
} else {
const auto resultPtr = new AllocaInst(valueType, 0U, "return", block);
const auto funType = FunctionType::get(Type::getVoidTy(context), {fact->getType(), resultPtr->getType(), size->getType(), items->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(funType), "function", block);
CallInst::Create(funType, funcPtr, {fact, resultPtr, size, items}, "", block);
return new LoadInst(valueType, resultPtr, "array", block);
}
}
Value* GetMemoryUsed(ui64 limit, const TCodegenContext& ctx, BasicBlock* block) {
if (!limit) {
return nullptr;
}
auto& context = ctx.Codegen.GetContext();
const auto fact = ctx.GetFactory();
const auto func = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&THolderFactory::GetMemoryUsed));
const auto funType = FunctionType::get(Type::getInt64Ty(context), {fact->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(funType), "get_used", block);
return CallInst::Create(funType, funcPtr, {fact}, "mem_used", block);
}
template <bool TrackRss>
Value* CheckAdjustedMemLimit(ui64 limit, Value* init, const TCodegenContext& ctx, BasicBlock*& block) {
auto& context = ctx.Codegen.GetContext();
if (!limit || !init) {
return ConstantInt::getFalse(context);
}
const auto indexType = Type::getInt32Ty(context);
if constexpr (TrackRss) {
const auto rssPtr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), ctx.Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 5)}, "rss_ptr", block);
const auto rss = new LoadInst(Type::getInt32Ty(context), rssPtr, "rsscounter", block);
const auto inc = BinaryOperator::CreateAdd(rss, ConstantInt::get(rss->getType(), 1), "inc", block);
new StoreInst(inc, rssPtr, block);
const auto mod = BinaryOperator::CreateURem(rss, ConstantInt::get(rss->getType(), STEP_FOR_RSS_CHECK), "mod", block);
const auto now = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, mod, ConstantInt::get(mod->getType() , 0), "now", block);
const auto call = BasicBlock::Create(context, "call", ctx.Func);
const auto skip = BasicBlock::Create(context, "skip", ctx.Func);
BranchInst::Create(call, skip, now, block);
block = call;
const auto func = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TComputationContext::UpdateUsageAdjustor));
const auto funType = FunctionType::get(Type::getVoidTy(context), {ctx.Ctx->getType(), Type::getInt64Ty(context)}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, func, PointerType::getUnqual(funType), "update", block);
CallInst::Create(funType, funcPtr, {ctx.Ctx, ConstantInt::get(init->getType(), limit)}, "", block);
BranchInst::Create(skip, block);
block = skip;
}
const auto adjPtr = GetElementPtrInst::CreateInBounds(GetCompContextType(context), ctx.Ctx, {ConstantInt::get(indexType, 0), ConstantInt::get(indexType, 4)}, "adj_ptr", block);
const auto adjustor = new LoadInst(Type::getFloatTy(context), adjPtr, "adjustor", block);
const auto curr = GetMemoryUsed(limit, ctx, block);
const auto cast = CastInst::Create(Instruction::UIToFP, curr, adjustor->getType(), "cast", block);
const auto used = BinaryOperator::CreateFMul(cast, adjustor, "used", block);
const auto add = BinaryOperator::CreateAdd(init, ConstantInt::get(init->getType(), limit), "add", block);
const auto upper = CastInst::Create(Instruction::UIToFP, add, adjustor->getType(), "upper", block);
return CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OGE, used, upper, "enough", block);
}
template Value* CheckAdjustedMemLimit<false>(ui64 limit, Value* init, const TCodegenContext& ctx, BasicBlock*& block);
template Value* CheckAdjustedMemLimit<true>(ui64 limit, Value* init, const TCodegenContext& ctx, BasicBlock*& block);
Value* WrapArgumentForWindows(Value* arg, const TCodegenContext& ctx, BasicBlock* block) {
if (ctx.Codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
return arg;
}
const auto newArg = new AllocaInst(arg->getType(), 0, "argument", block);
new StoreInst(arg, newArg, block);
return newArg;
}
Value* CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Type* returnType, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = FunctionType::get(returnType, {boxed->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
const auto call = CallInst::Create(funType, func, {boxed}, returnType->isVoidTy() ? "" : "return", block);
return call;
}
void CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Value* output, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) ?
FunctionType::get(Type::getVoidTy(context), {output->getType(), boxed->getType()}, false):
FunctionType::get(Type::getVoidTy(context), {boxed->getType(), output->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
if (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
CallInst::Create(funType, func, {output, boxed}, "", block);
} else {
CallInst::Create(funType, func, {boxed, output}, "", block);
}
}
void CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Value* output, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block, Value* argument) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) ?
FunctionType::get(Type::getVoidTy(context), {output->getType(), boxed->getType(), argument->getType()}, false):
FunctionType::get(Type::getVoidTy(context), {boxed->getType(), output->getType(), argument->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
if (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
CallInst::Create(funType, func, {output, boxed, argument}, "", block);
} else {
CallInst::Create(funType, func, {boxed, output, argument}, "", block);
}
}
Value* CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Type* returnType, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block, Value* argument) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = FunctionType::get(returnType, {boxed->getType(), argument->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
const auto call = CallInst::Create(funType, func, {boxed, argument}, returnType->isVoidTy() ? "" : "return", block);
return call;
}
void CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Value* output, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block, Value* arg1, Value* arg2) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) ?
FunctionType::get(Type::getVoidTy(context), {output->getType(), boxed->getType(), arg1->getType(), arg2->getType()}, false):
FunctionType::get(Type::getVoidTy(context), {boxed->getType(), output->getType(), arg1->getType(), arg2->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
if (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
CallInst::Create(funType, func, {output, boxed, arg1, arg2}, "", block);
} else {
CallInst::Create(funType, func, {boxed, output, arg1, arg2}, "", block);
}
}
Value* CallBoxedValueVirtualMethodImpl(uintptr_t methodPtr, Type* returnType, Value* value, NYql::NCodegen::ICodegen& codegen, BasicBlock* block, Value* arg1, Value* arg2) {
auto& context = codegen.GetContext();
const auto data = CastInst::Create(Instruction::Trunc, value, Type::getInt64Ty(context), "data", block);
const auto ptrStructType = PointerType::getUnqual(StructType::get(context));
const auto boxed = CastInst::Create(Instruction::IntToPtr, data, ptrStructType, "boxed", block);
const auto funType = FunctionType::get(returnType, {boxed->getType(), arg1->getType(), arg2->getType()}, false);
const auto ptrFunType = PointerType::getUnqual(funType);
const auto tableType = PointerType::getUnqual(ptrFunType);
const auto vTable = CastInst::Create(Instruction::IntToPtr, data, PointerType::getUnqual(tableType), "vtable", block);
const auto table = new LoadInst(tableType, vTable, "table", false, block);
const auto elem = GetElementPtrInst::CreateInBounds(ptrFunType, table, {ConstantInt::get(Type::getInt64Ty(context), GetMethodPtrIndex(methodPtr))}, "element", block);
const auto func = new LoadInst(ptrFunType, elem, "func", false, block);
const auto call = CallInst::Create(funType, func, {boxed, arg1, arg2}, returnType->isVoidTy() ? "" : "return", block);
return call;
}
Value* CallUnaryUnboxedValueFunctionImpl(uintptr_t methodPtr, Type* result, Value* arg, NYql::NCodegen::ICodegen& codegen, BasicBlock* block) {
auto& context = codegen.GetContext();
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), methodPtr);
if (NYql::NCodegen::ETarget::Windows != codegen.GetEffectiveTarget()) {
const auto funType = FunctionType::get(result, {arg->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
const auto call = CallInst::Create(funType, funcPtr, {arg}, "call", block);
return call;
} else {
const auto ptrArg = new AllocaInst(arg->getType(), 0U, "arg", block);
new StoreInst(arg, ptrArg, block);
if (Type::getInt128Ty(context) == result) {
const auto ptrResult = new AllocaInst(result, 0U, "result", block);
const auto funType = FunctionType::get(Type::getVoidTy(context), {ptrResult->getType(), ptrArg->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
CallInst::Create(funType, funcPtr, {ptrResult, ptrArg}, "", block);
const auto res = new LoadInst(result, ptrResult, "res", block);
return res;
} else {
const auto funType = FunctionType::get(result, {ptrArg->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
const auto call = CallInst::Create(funType, funcPtr, {ptrArg}, "call", block);
return call;
}
}
}
Value* CallBinaryUnboxedValueFunctionImpl(uintptr_t methodPtr, Type* result, Value* left, Value* right, NYql::NCodegen::ICodegen& codegen, BasicBlock* block) {
auto& context = codegen.GetContext();
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), methodPtr);
if (NYql::NCodegen::ETarget::Windows != codegen.GetEffectiveTarget()) {
const auto funType = FunctionType::get(result, {left->getType(), right->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
const auto call = CallInst::Create(funType, funcPtr, {left, right}, "call", block);
return call;
} else {
const auto ptrLeft = new AllocaInst(left->getType(), 0U, "left", block);
const auto ptrRight = new AllocaInst(right->getType(), 0U, "right", block);
new StoreInst(left, ptrLeft, block);
new StoreInst(right, ptrRight, block);
if (Type::getInt128Ty(context) == result) {
const auto ptrResult = new AllocaInst(result, 0U, "result", block);
const auto funType = FunctionType::get(Type::getVoidTy(context), {ptrResult->getType(), ptrLeft->getType(), ptrRight->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
CallInst::Create(funType, funcPtr, {ptrResult, ptrLeft, ptrRight}, "", block);
const auto res = new LoadInst(result, ptrResult, "res", block);
return res;
} else {
const auto funType = FunctionType::get(result, {ptrLeft->getType(), ptrRight->getType()}, false);
const auto funcPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "ptr", block);
const auto call = CallInst::Create(funType, funcPtr, {ptrLeft, ptrRight}, "call", block);
return call;
}
}
}
Y_NO_INLINE Value* TDecoratorCodegeneratorNodeBase::CreateGetValueImpl(IComputationNode* node,
const TCodegenContext& ctx, BasicBlock*& block) const {
const auto arg = GetNodeValue(node, ctx, block);
const auto value = DoGenerateGetValue(ctx, arg, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(Type::getInt128Ty(ctx.Codegen.GetContext()), value, "load", block);
ValueRelease(node->GetRepresentation(), load, ctx, block);
return load;
} else {
return value;
}
}
Y_NO_INLINE Value* TStatelessFlowCodegeneratorNodeBase::CreateGetValueImpl(const IComputationNode* node,
const TCodegenContext& ctx, BasicBlock*& block) const {
const auto value = DoGenerateGetValue(ctx, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(Type::getInt128Ty(ctx.Codegen.GetContext()), value, "load", block);
ValueRelease(node->GetRepresentation(), load, ctx, block);
return load;
} else {
return value;
}
}
Y_NO_INLINE ICodegeneratorInlineWideNode::TGenerateResult TStatelessWideFlowCodegeneratorNodeBase::GenGetValuesImpl(
const TCodegenContext& ctx, BasicBlock*& block) const {
return DoGenGetValues(ctx, block);
}
Y_NO_INLINE Value* TFlowSourceCodegeneratorNodeBase::CreateGetValueImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto statePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), node->GetIndex())}, "state_ptr", block);
const auto value = DoGenerateGetValue(ctx, statePtr, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(valueType, value, "load", block);
ValueRelease(node->GetRepresentation(), load, ctx, block);
return load;
} else {
return value;
}
}
Y_NO_INLINE ICodegeneratorInlineWideNode::TGenerateResult TWideFlowSourceCodegeneratorNodeBase::GenGetValuesImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto statePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), node->GetIndex())}, "state_ptr", block);
return DoGenGetValues(ctx, statePtr, block);
}
Y_NO_INLINE Value* TStatefulFlowCodegeneratorNodeBase::CreateGetValueImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto statePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), node->GetIndex())}, "state_ptr", block);
const auto value = DoGenerateGetValue(ctx, statePtr, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(valueType, value, "load", block);
ValueRelease(node->GetRepresentation(), load, ctx, block);
return load;
} else {
return value;
}
}
Y_NO_INLINE ICodegeneratorInlineWideNode::TGenerateResult TStatefulWideFlowCodegeneratorNodeBase::GenGetValuesImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valueType = Type::getInt128Ty(context);
const auto statePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), node->GetIndex())}, "state_ptr", block);
return DoGenGetValues(ctx, statePtr, block);
}
Y_NO_INLINE ICodegeneratorInlineWideNode::TGenerateResult TPairStateWideFlowCodegeneratorNodeBase::GenGetValuesImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
auto idx = node->GetIndex();
const auto valueType = Type::getInt128Ty(context);
const auto firstPtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), idx)}, "first_ptr", block);
const auto secondPtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), ++idx)}, "second_ptr", block);
return DoGenGetValues(ctx, firstPtr, secondPtr, block);
}
Y_NO_INLINE Value* TPairStateFlowCodegeneratorNodeBase::CreateGetValueImpl(
const IComputationNode* node, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
auto idx = node->GetIndex();
const auto valueType = Type::getInt128Ty(context);
const auto firstPtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), idx)}, "first_ptr", block);
const auto secondPtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(Type::getInt32Ty(context), ++idx)}, "second_ptr", block);
const auto value = DoGenerateGetValue(ctx, firstPtr, secondPtr, block);
if (value->getType()->isPointerTy()) {
const auto load = new LoadInst(valueType, value, "load", block);
ValueRelease(node->GetRepresentation(), load, ctx, block);
return load;
} else {
return value;
}
}
Y_NO_INLINE Value* TBinaryCodegeneratorNodeBase::CreateGetValueImpl(const IComputationNode* node,
const TCodegenContext& ctx, BasicBlock*& block) const {
const auto value = DoGenerateGetValue(ctx, block);
if (value->getType()->isPointerTy()) {
ValueRelease(node->GetRepresentation(), value, ctx, block);
const auto load = new LoadInst(Type::getInt128Ty(ctx.Codegen.GetContext()), value, "load", block);
return load;
} else {
return value;
}
}
Y_NO_INLINE Value* TMutableCodegeneratorNodeBase::CreateGetValueImpl(
bool stateless, EValueRepresentation representation, ui32 valueIndex,
const TString& name, const TCodegenContext& ctx, BasicBlock*& block) const {
if (stateless) {
const auto newValue = DoGenerateGetValue(ctx, block);
if (newValue->getType()->isPointerTy()) {
ValueRelease(representation, newValue, ctx, block);
const auto load = new LoadInst(Type::getInt128Ty(ctx.Codegen.GetContext()), newValue, "load", block);
return load;
} else {
return newValue;
}
}
return ctx.AlwaysInline ? MakeGetValueBody(representation, valueIndex, ctx, block) :
CallInst::Create(GenerateInternalGetValue(name, representation, valueIndex, ctx.Codegen), {ctx.Ctx}, "getter", block);
}
Function* TMutableCodegeneratorNodeBase::GenerateInternalGetValue(const TString& name,
EValueRepresentation representation, ui32 valueIndex, NYql::NCodegen::ICodegen& codegen) const {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto funcType = FunctionType::get(Type::getInt128Ty(context), {PointerType::getUnqual(GetCompContextType(context))}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto main = BasicBlock::Create(context, "main", ctx.Func);
ctx.Ctx = &*ctx.Func->arg_begin();
ctx.Ctx->addAttr(Attribute::NonNull);
const auto get = MakeGetValueBody(representation, valueIndex, ctx, main);
ReturnInst::Create(context, get, main);
return ctx.Func;
}
Value* TMutableCodegeneratorNodeBase::MakeGetValueBody(EValueRepresentation representation, ui32 valueIndex, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(indexType, valueIndex)}, "value_ptr", block);
const auto value = new LoadInst(valueType, valuePtr, "value", block);
const auto invv = ConstantInt::get(value->getType(), 0xFFFFFFFFFFFFFFFFULL);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, value, invv, "check", block);
const auto comp = BasicBlock::Create(context, "comp", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
BranchInst::Create(comp, done, check, block);
block = comp;
const auto newValue = DoGenerateGetValue(ctx, block);
if (newValue->getType()->isPointerTy()) {
const auto load = new LoadInst(valueType, newValue, "value", block);
new StoreInst(load, valuePtr, block);
new StoreInst(ConstantInt::get(load->getType(), 0), newValue, block);
} else {
new StoreInst(newValue, valuePtr, block);
ValueAddRef(representation, valuePtr, ctx, block);
}
BranchInst::Create(done, block);
block = done;
const auto result = new LoadInst(valueType, valuePtr, "result", false, block);
return result;
}
Y_NO_INLINE Value* TMutableCodegeneratorPtrNodeBase::CreateGetValueImpl(
bool stateless, EValueRepresentation representation, ui32 valueIndex,
const TString& name, const TCodegenContext& ctx, BasicBlock*& block) const {
if (stateless) {
const auto type = Type::getInt128Ty(ctx.Codegen.GetContext());
const auto pointer = ctx.Func->getEntryBlock().empty() ?
new AllocaInst(type, 0U, "output", &ctx.Func->getEntryBlock()):
new AllocaInst(type, 0U, "output", &ctx.Func->getEntryBlock().back());
DoGenerateGetValue(ctx, pointer, block);
ValueRelease(representation, pointer, ctx, block);
const auto load = new LoadInst(type, pointer, "load", block);
return load;
}
return ctx.AlwaysInline ? MakeGetValueBody(valueIndex, ctx, block) :
CallInst::Create(GenerateInternalGetValue(name, valueIndex, ctx.Codegen), {ctx.Ctx}, "getter", block);
}
Value* TMutableCodegeneratorPtrNodeBase::MakeGetValueBody(ui32 valueIndex, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto indexType = Type::getInt32Ty(context);
const auto valueType = Type::getInt128Ty(context);
const auto valuePtr = GetElementPtrInst::CreateInBounds(valueType, ctx.GetMutables(), {ConstantInt::get(indexType, valueIndex)}, "value_ptr", block);
const auto value = new LoadInst(valueType, valuePtr, "value", block);
const auto invv = ConstantInt::get(value->getType(), 0xFFFFFFFFFFFFFFFFULL);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, value, invv, "check", block);
const auto comp = BasicBlock::Create(context, "comp", ctx.Func);
const auto done = BasicBlock::Create(context, "done", ctx.Func);
BranchInst::Create(comp, done, check, block);
block = comp;
DoGenerateGetValue(ctx, valuePtr, block);
BranchInst::Create(done, block);
block = done;
const auto result = new LoadInst(valueType, valuePtr, "result", false, block);
return result;
}
Function* TMutableCodegeneratorPtrNodeBase::GenerateInternalGetValue(const TString& name, ui32 valueIndex, NYql::NCodegen::ICodegen& codegen) const {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto contextType = GetCompContextType(context);
const auto funcType = FunctionType::get(Type::getInt128Ty(context), {PointerType::getUnqual(contextType)}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto main = BasicBlock::Create(context, "main", ctx.Func);
ctx.Ctx = &*ctx.Func->arg_begin();
ctx.Ctx->addAttr(Attribute::NonNull);
const auto get = MakeGetValueBody(valueIndex, ctx, main);
ReturnInst::Create(context, get, main);
return ctx.Func;
}
Y_NO_INLINE Value* TMutableCodegeneratorFallbackNodeBase::DoGenerateGetValueImpl(
uintptr_t methodPtr, uintptr_t thisPtr, const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto type = Type::getInt128Ty(context);
const auto ptrType = PointerType::getUnqual(StructType::get(context));
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), methodPtr);
const auto self = CastInst::Create(Instruction::IntToPtr, ConstantInt::get(Type::getInt64Ty(context), thisPtr), ptrType, "self", block);
if (NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget()) {
const auto funType = FunctionType::get(type, {self->getType(), ctx.Ctx->getType()}, false);
const auto doFuncPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "function", block);
const auto value = CallInst::Create(funType, doFuncPtr, {self, ctx.Ctx}, "value", block);
return value;
} else {
const auto resultPtr = new AllocaInst(type, 0U, "return", block);
const auto funType = FunctionType::get(Type::getVoidTy(context), {self->getType(), resultPtr->getType(), ctx.Ctx->getType()}, false);
const auto doFuncPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(funType), "function", block);
CallInst::Create(funType, doFuncPtr, {self, resultPtr, ctx.Ctx}, "", block);
const auto value = new LoadInst(type, resultPtr, "value", block);
return value;
}
}
Y_NO_INLINE Function* TCodegeneratorRootNodeBase::GenerateGetValueImpl(
const TString& name, const ICodegeneratorInlineNode* gen, NYql::NCodegen::ICodegen& codegen) {
auto& module = codegen.GetModule();
auto& context = codegen.GetContext();
if (const auto f = module.getFunction(name.c_str()))
return f;
const auto valueType = Type::getInt128Ty(context);
const auto contextType = GetCompContextType(context);
const auto funcType = codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows ?
FunctionType::get(valueType, {PointerType::getUnqual(contextType)}, false):
FunctionType::get(Type::getVoidTy(context) , {PointerType::getUnqual(valueType), PointerType::getUnqual(contextType)}, false);
TCodegenContext ctx(codegen);
ctx.Func = cast<Function>(module.getOrInsertFunction(name.c_str(), funcType).getCallee());
DISubprogramAnnotator annotator(ctx, ctx.Func);
auto args = ctx.Func->arg_begin();
if (codegen.GetEffectiveTarget() == NYql::NCodegen::ETarget::Windows) {
auto& firstArg = *args++;
firstArg.addAttr(Attribute::StructRet);
firstArg.addAttr(Attribute::NoAlias);
}
auto main = BasicBlock::Create(context, "main", ctx.Func);
ctx.Ctx = &*args;
ctx.Ctx->addAttr(Attribute::NonNull);
const auto get = gen->CreateGetValue(ctx, main);
if (codegen.GetEffectiveTarget() != NYql::NCodegen::ETarget::Windows) {
ReturnInst::Create(context, get, main);
} else {
new StoreInst(get, &*--args, main);
ReturnInst::Create(context, main);
}
return ctx.Func;
}
#if __clang__ && (__clang_major__ < 16)
TSrcLocation TSrcLocation::current() {
return {};
}
const char* TSrcLocation::file_name() const {
return __FILE__;
}
size_t TSrcLocation::line() const {
return __LINE__;
}
size_t TSrcLocation::column() const {
return 0;
}
#endif
DISubprogramAnnotator::DISubprogramAnnotator(TCodegenContext& ctx, Function* subprogramFunc, const TSrcLocation& location)
: Ctx(ctx)
, DebugBuilder(std::make_unique<DIBuilder>(ctx.Codegen.GetModule()))
, Subprogram(MakeDISubprogram(subprogramFunc->getName(), location))
, Func(subprogramFunc)
{
subprogramFunc->setSubprogram(Subprogram);
Ctx.Annotator = this;
}
DISubprogramAnnotator::~DISubprogramAnnotator() {
Ctx.Annotator = nullptr;
{ // necessary stub annotation of "CallInst"s
DIScopeAnnotator stubAnnotate(this);
for (BasicBlock& block : *Func) {
for (Instruction& inst : block) {
if (CallInst* callInst = dyn_cast_or_null<CallInst>(&inst)) {
const auto& debugLoc = callInst->getDebugLoc();
if (!debugLoc) {
stubAnnotate(callInst);
}
}
}
}
}
DebugBuilder->finalizeSubprogram(Subprogram);
}
DIFile* DISubprogramAnnotator::MakeDIFile(const TSrcLocation& location) {
TFsPath path = TString(location.file_name());
return DebugBuilder->createFile(path.GetName().c_str(), path.Parent().GetPath().c_str());
}
DISubprogram* DISubprogramAnnotator::MakeDISubprogram(const StringRef& name, const TSrcLocation& location) {
const auto file = MakeDIFile(location);
const auto unit = DebugBuilder->createCompileUnit(llvm::dwarf::DW_LANG_C_plus_plus, file, "MKQL", false, "", 0);
const auto subroutineType = DebugBuilder->createSubroutineType(DebugBuilder->getOrCreateTypeArray({}));
return DebugBuilder->createFunction(
unit,
name,
llvm::StringRef(),
file, 0,
subroutineType, 0, llvm::DINode::FlagPrototyped, llvm::DISubprogram::SPFlagDefinition
);
}
DIScopeAnnotator::DIScopeAnnotator(DISubprogramAnnotator* subprogramAnnotator, const TSrcLocation& location)
: SubprogramAnnotator(nullptr)
, Scope(nullptr)
{
Y_ENSURE(subprogramAnnotator != nullptr);
SubprogramAnnotator = subprogramAnnotator;
Scope = SubprogramAnnotator->DebugBuilder->createLexicalBlock(SubprogramAnnotator->Subprogram, SubprogramAnnotator->MakeDIFile(location), location.line(), location.column());
}
Instruction* DIScopeAnnotator::operator()(Instruction* inst, const TSrcLocation& location) const {
inst->setDebugLoc(DILocation::get(SubprogramAnnotator->Ctx.Codegen.GetContext(), location.line(), location.column(), Scope));
return inst;
}
}
}
#endif
|