File: IRGenDebugInfo.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (3792 lines) | stat: -rw-r--r-- 157,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
//===--- IRGenDebugInfo.cpp - Debug Info Support --------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements IR debug info generation for Swift.
//
//===----------------------------------------------------------------------===//

#include "IRGenDebugInfo.h"
#include "GenEnum.h"
#include "GenOpaque.h"
#include "GenStruct.h"
#include "GenTuple.h"
#include "GenType.h"
#include "IRBuilder.h"
#include "swift/AST/ASTDemangler.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Module.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/TypeDifferenceVisitor.h"
#include "swift/Basic/Compiler.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILModule.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/ExternalASTSource.h"
#include "clang/Basic/Module.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Config/config.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Local.h"

#define DEBUG_TYPE "debug-info"

using namespace swift;
using namespace irgen;

llvm::cl::opt<bool> VerifyLineTable(
    "verify-linetable", llvm::cl::init(false),
    llvm::cl::desc(
        "Verify that the debug locations within one scope are contiguous."));

namespace {
using TrackingDIRefMap =
    llvm::DenseMap<const llvm::MDString *, llvm::TrackingMDNodeRef>;

class EqualUpToClangTypes
    : public CanTypeDifferenceVisitor<EqualUpToClangTypes> {
public:
  bool visitDifferentTypeStructure(CanType t1, CanType t2) {
#define COMPARE_UPTO_CLANG_TYPE(CLASS)                                         \
  if (auto f1 = dyn_cast<CLASS>(t1)) {                                         \
    auto f2 = cast<CLASS>(t2);                                                 \
    return !f1->getExtInfo().isEqualTo(f2->getExtInfo(),                       \
                                       /*useClangTypes*/ false);               \
  }
    COMPARE_UPTO_CLANG_TYPE(FunctionType);
    COMPARE_UPTO_CLANG_TYPE(SILFunctionType);
#undef COMPARE_UPTO_CLANG_TYPE
    return true;
  }
  bool check(Type t1, Type t2) {
    return !visit(t1->getCanonicalType(), t2->getCanonicalType());
  };
};

static bool equalWithoutExistentialTypes(Type t1, Type t2) {
  static Type (*withoutExistentialTypes)(Type) = [](Type type) -> Type {
    return type.transform([](Type type) -> Type {
      if (auto existential = type->getAs<ExistentialType>()) {
        return withoutExistentialTypes(existential->getConstraintType());
      }
      return type;
    });
  };

  return withoutExistentialTypes(t1)
      ->isEqual(withoutExistentialTypes(t2));
}

class IRGenDebugInfoImpl : public IRGenDebugInfo {
  const IRGenOptions &Opts;
  ClangImporter &CI;
  SourceManager &SM;
  llvm::Module &M;
  llvm::DIBuilder DBuilder;
  IRGenModule &IGM;
  const PathRemapper &DebugPrefixMap;

  struct FileAndLocation {
    unsigned Line = 0;
    uint16_t Column = 0;
    llvm::DIFile *File = nullptr;
    StringRef getFilename() const { return File ? File->getFilename() : ""; }
    bool operator==(const FileAndLocation &other) const {
      return Line == other.Line && Column == other.Column && File == other.File;
    }
  };
  
  /// Various caches.
  /// \{
  llvm::StringSet<> VarNames;
  using VarID = std::tuple<llvm::MDNode *, llvm::StringRef, unsigned, uint16_t>;
  llvm::DenseMap<VarID, llvm::TrackingMDNodeRef> LocalVarCache;
  llvm::DenseMap<const SILDebugScope *, llvm::TrackingMDNodeRef> ScopeCache;
  llvm::DenseMap<const SILDebugScope *, llvm::TrackingMDNodeRef> InlinedAtCache;
  llvm::DenseMap<const void *, FileAndLocation> FileAndLocationCache;
  llvm::DenseMap<TypeBase *, llvm::TrackingMDNodeRef> DITypeCache;
  llvm::DenseMap<const void *, llvm::TrackingMDNodeRef> DIModuleCache;
  llvm::StringMap<llvm::TrackingMDNodeRef> DIFileCache;
  llvm::StringMap<llvm::TrackingMDNodeRef> RuntimeErrorFnCache;
  TrackingDIRefMap DIRefMap;
  TrackingDIRefMap InnerTypeCache;
  /// \}

  /// A list of replaceable fwddecls that need to be RAUWed at the end.
  std::vector<std::pair<StringRef, llvm::TrackingMDRef>> FwdDeclTypes;
  /// The set of imported modules.
  llvm::DenseSet<ModuleDecl *> ImportedModules;

  llvm::BumpPtrAllocator DebugInfoNames;
  /// The current working directory.
  StringRef CWDName;
  /// User-provided -D macro definitions.
  SmallString<0> ConfigMacros;
  /// The current compilation unit.
  llvm::DICompileUnit *TheCU = nullptr;
  /// The main file.
  llvm::DIFile *MainFile = nullptr;
  /// The default file for compiler-generated code.
  llvm::DIFile *CompilerGeneratedFile = nullptr;
  /// The current module.
  llvm::DIModule *MainModule = nullptr;
  /// Scope of entry point function (main by default).
  llvm::DIScope *EntryPointFn = nullptr;
  /// The artificial type decls for named archetypes.
  llvm::StringMap<TypeAliasDecl *> MetadataTypeDeclCache;
  /// Catch-all type for opaque internal types.
  llvm::DIType *InternalType = nullptr;

  /// The last location that was emitted.
  FileAndLocation LastFileAndLocation;
  /// The scope of that last location.
  const SILDebugScope *LastScope = nullptr;

  /// Used by pushLoc.
  SmallVector<std::pair<FileAndLocation, const SILDebugScope *>, 8>
      LocationStack;

#ifndef NDEBUG
  using UUFTuple = std::pair<std::pair<unsigned, unsigned>, llvm::DIFile *>;
  struct FileAndLocationKey : public UUFTuple {
    FileAndLocationKey(FileAndLocation DL)
        : UUFTuple({{DL.Line, DL.Column}, DL.File}) {}
    inline bool operator==(const FileAndLocation &DL) const {
      return first.first == DL.Line && first.second == DL.Column &&
             second == DL.File;
    }
  };
  llvm::DenseSet<UUFTuple> PreviousLineEntries;
  FileAndLocation PreviousFileAndLocation;
#endif

public:
  IRGenDebugInfoImpl(const IRGenOptions &Opts, ClangImporter &CI,
                     IRGenModule &IGM, llvm::Module &M,
                     StringRef MainOutputFilenameForDebugInfo,
                     StringRef PrivateDiscriminator);
  ~IRGenDebugInfoImpl() {
    // FIXME: SILPassManager sometimes creates an IGM and doesn't finalize it.
    if (!FwdDeclTypes.empty())
      finalize();
    assert(FwdDeclTypes.empty() && "finalize() was not called");
  }
  void finalize();

  void setCurrentLoc(IRBuilder &Builder, const SILDebugScope *DS,
                     SILLocation Loc);

  void addFailureMessageToCurrentLoc(IRBuilder &Builder, StringRef failureMsg);

  void clearLoc(IRBuilder &Builder);
  void pushLoc();
  void popLoc();
  void setInlinedTrapLocation(IRBuilder &Builder, const SILDebugScope *Scope);
  void setEntryPointLoc(IRBuilder &Builder);
  llvm::DIScope *getEntryPointFn();
  llvm::DIScope *getOrCreateScope(const SILDebugScope *DS);
  void emitImport(ImportDecl *D);
  llvm::DISubprogram *emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
                                   SILFunctionTypeRepresentation Rep,
                                   SILType Ty, DeclContext *DeclCtx = nullptr,
                                   StringRef outlinedFromName = StringRef());
  llvm::DISubprogram *emitFunction(SILFunction &SILFn, llvm::Function *Fn);
  void emitArtificialFunction(IRBuilder &Builder, llvm::Function *Fn,
                              SILType SILTy);
  void emitOutlinedFunction(IRBuilder &Builder,
                            llvm::Function *Fn,
                            StringRef outlinedFromName);

  /// Return false if we fail to create the right DW_OP_LLVM_fragment operand.
  bool handleFragmentDIExpr(const SILDIExprOperand &CurDIExprOp,
                            llvm::DIExpression::FragmentInfo &Fragment);
  /// Return false if we fail to create the right DW_OP_LLVM_fragment operand.
  bool handleTupleFragmentDIExpr(const SILDIExprOperand &CurDIExprOp,
                                 llvm::DIExpression::FragmentInfo &Fragment);
  /// Return false if we fail to create the desired !DIExpression.
  bool buildDebugInfoExpression(const SILDebugVariable &VarInfo,
                                SmallVectorImpl<uint64_t> &Operands,
                                llvm::DIExpression::FragmentInfo &Fragment);

  /// Emit a dbg.declare at the current insertion point in Builder.
  void emitVariableDeclaration(IRBuilder &Builder,
                               ArrayRef<llvm::Value *> Storage,
                               DebugTypeInfo Ty, const SILDebugScope *DS,
                               std::optional<SILLocation> VarLoc,
                               SILDebugVariable VarInfo,
                               IndirectionKind = DirectValue,
                               ArtificialKind = RealValue,
                               AddrDbgInstrKind = AddrDbgInstrKind::DbgDeclare);

  void emitDbgIntrinsic(IRBuilder &Builder, llvm::Value *Storage,
                        llvm::DILocalVariable *Var, llvm::DIExpression *Expr,
                        unsigned Line, unsigned Col, llvm::DILocalScope *Scope,
                        const SILDebugScope *DS, bool InCoroContext,
                        AddrDbgInstrKind = AddrDbgInstrKind::DbgDeclare);

  void emitGlobalVariableDeclaration(llvm::GlobalVariable *Storage,
                                     StringRef Name, StringRef LinkageName,
                                     DebugTypeInfo DebugType,
                                     bool IsLocalToUnit,
                                     std::optional<SILLocation> Loc);
  void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
                        unsigned Depth, unsigned Index, StringRef Name);
  void emitPackCountParameter(IRGenFunction &IGF, llvm::Value *Metadata,
                              SILDebugVariable VarInfo);

  /// Return the DIBuilder.
  llvm::DIBuilder &getBuilder() { return DBuilder; }

  /// Decode (and cache) a SourceLoc.
  FileAndLocation decodeSourceLoc(SourceLoc SL) {
    auto &Cached = FileAndLocationCache[SL.getOpaquePointerValue()];
    if (Cached.File)
      return Cached;

    if (!SL.isValid()) {
      Cached.File = CompilerGeneratedFile;
      return Cached;
    }

    // If the source buffer is a macro, extract its full text.
    std::optional<StringRef> Source;
    bool ForceGeneratedSourceToDisk = Opts.DWARFVersion < 5;
    if (!ForceGeneratedSourceToDisk) {
      auto BufferID = SM.findBufferContainingLoc(SL);
      if (auto generatedInfo = SM.getGeneratedSourceInfo(BufferID)) {
        // We only care about macros, so skip everything else.
        if (generatedInfo->kind != GeneratedSourceInfo::ReplacedFunctionBody &&
            generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted)
          if (auto *MemBuf = SM.getLLVMSourceMgr().getMemoryBuffer(BufferID)) {
            Source = MemBuf->getBuffer();
            // This is copying the buffer twice, but Xcode depends on this
            // comment in the file.
            auto origRange = generatedInfo->originalSourceRange;
            if (origRange.isValid()) {
              std::string s;
              {
                llvm::raw_string_ostream buffer(s);
                buffer << MemBuf->getBuffer() << "\n";
                auto originalFilename =
                    SM.getDisplayNameForLoc(origRange.getStart(), true);
                unsigned startLine, startColumn, endLine, endColumn;
                std::tie(startLine, startColumn) =
                    SM.getPresumedLineAndColumnForLoc(origRange.getStart());
                std::tie(endLine, endColumn) =
                    SM.getPresumedLineAndColumnForLoc(origRange.getEnd());
                buffer << "// original-source-range: "
                       << DebugPrefixMap.remapPath(originalFilename) << ":"
                       << startLine << ":" << startColumn << "-" << endLine
                       << ":" << endColumn << "\n";
              }
              Source = BumpAllocatedString(s);
            }
          }
      }
    }
    Cached.File = getOrCreateFile(
        SM.getDisplayNameForLoc(SL, ForceGeneratedSourceToDisk), Source);
    std::tie(Cached.Line, Cached.Column) =
        SM.getPresumedLineAndColumnForLoc(SL);
    // When WinDbg finds two locations with the same line but different
    // columns, the user must select an address when they break on that
    // line. Also, clang does not emit column locations in CodeView for C++.
    if (Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
      Cached.Column = 0;
    return Cached;
  }

  IRGenDebugInfoFormat getDebugInfoFormat() { return Opts.DebugInfoFormat; }

private:
  static StringRef getFilenameFromDC(const DeclContext *DC) {
    if (auto *LF = dyn_cast<LoadedFile>(DC))
      return LF->getFilename();
    if (auto *SF = dyn_cast<SourceFile>(DC))
      return SF->getFilename();
    if (auto *M = dyn_cast<ModuleDecl>(DC))
      return M->getModuleFilename();
    return {};
  }

  FileAndLocation getDeserializedLoc(Pattern *) { return {}; }
  FileAndLocation getDeserializedLoc(Expr *) { return {}; }
  FileAndLocation getDeserializedLoc(Decl *D) {
    FileAndLocation L;
    const DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
    StringRef Filename = getFilenameFromDC(DC);
    if (!Filename.empty())
      L.File = getOrCreateFile(Filename, {});
    return L;
  }

  FileAndLocation
  getFileAndLocation(const SILLocation::FilenameAndLocation &FL) {
    // When WinDbg finds two locations with the same line but different
    // columns, the user must select an address when they break on that
    // line. Also, clang does not emit column locations in CodeView for C++.
    bool CodeView = Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView;
    return {FL.line, CodeView ? (uint16_t)0 : FL.column,
            getOrCreateFile(FL.filename, {})};
  }

  /// Use the Swift SM to figure out the actual line/column of a SourceLoc.
  template <typename WithLoc>
  FileAndLocation getSwiftFileAndLocation(WithLoc *ASTNode, bool End) {
    if (!ASTNode)
      return {};

    SourceLoc Loc = End ? ASTNode->getEndLoc() : ASTNode->getStartLoc();
    if (Loc.isInvalid())
      // This may be a deserialized or clang-imported decl. And modules
      // don't come with SourceLocs right now. Get at least the name of
      // the module.
      return getDeserializedLoc(ASTNode);

    return decodeSourceLoc(Loc);
  }

  FileAndLocation getFileAndLocation(Pattern *P, bool End = false) {
    return getSwiftFileAndLocation(P, End);
  }
  FileAndLocation getFileAndLocation(Expr *E, bool End = false) {
    return getSwiftFileAndLocation(E, End);
  }
  FileAndLocation getFileAndLocation(Decl *D, bool End = false) {
    FileAndLocation L;
    if (!D)
      return L;

    if (auto *ClangDecl = D->getClangDecl()) {
      clang::SourceLocation ClangSrcLoc = ClangDecl->getBeginLoc();
      clang::SourceManager &ClangSM =
          CI.getClangASTContext().getSourceManager();
      clang::PresumedLoc PresumedLoc = ClangSM.getPresumedLoc(ClangSrcLoc);
      if (!PresumedLoc.isValid())
        return L;
      L.Line = PresumedLoc.getLine();
      L.Column = PresumedLoc.getColumn();
      L.File = getOrCreateFile(PresumedLoc.getFilename(), {});
      return L;
    }
    return getSwiftFileAndLocation(D, End);
  }

  FileAndLocation getStartLocation(std::optional<SILLocation> OptLoc) {
    if (!OptLoc)
      return {};
    if (OptLoc->isFilenameAndLocation())
      return getFileAndLocation(*OptLoc->getFilenameAndLocation());
    return decodeSourceLoc(OptLoc->getStartSourceLoc());
  }

  FileAndLocation decodeFileAndLocation(SILLocation Loc) {
    if (Loc.isFilenameAndLocation())
      return getFileAndLocation(*Loc.getFilenameAndLocation());
    return decodeSourceLoc(Loc.getSourceLocForDebugging());
  }

  /// Strdup a raw char array using the bump pointer.
  StringRef BumpAllocatedString(const char *Data, size_t Length) {
    char *Ptr = DebugInfoNames.Allocate<char>(Length + 1);
    memcpy(Ptr, Data, Length);
    *(Ptr + Length) = 0;
    return StringRef(Ptr, Length);
  }

  /// Strdup S using the bump pointer.
  StringRef BumpAllocatedString(std::string S) {
    return BumpAllocatedString(S.c_str(), S.length());
  }

  /// Strdup StringRef S using the bump pointer.
  StringRef BumpAllocatedString(StringRef S) {
    return BumpAllocatedString(S.data(), S.size());
  }

  /// Return the size reported by a type.
  static unsigned getSizeInBits(llvm::DIType *Ty) {
    // Follow derived types until we reach a type that
    // reports back a size.
    while (isa<llvm::DIDerivedType>(Ty) && !Ty->getSizeInBits()) {
      auto *DT = cast<llvm::DIDerivedType>(Ty);
      Ty = DT->getBaseType();
      if (!Ty)
        return 0;
    }
    return Ty->getSizeInBits();
  }

#ifndef NDEBUG

  /// Return the size reported by the variable's type.
  static unsigned getSizeInBits(const llvm::DILocalVariable *Var) {
    llvm::DIType *Ty = Var->getType();
    return getSizeInBits(Ty);
  }

#endif

  /// Determine whether this debug scope belongs to an explicit closure.
  static bool isExplicitClosure(const SILFunction *SILFn) {
    if (SILFn && SILFn->hasLocation())
      if (Expr *E = SILFn->getLocation().getAsASTNode<Expr>())
        if (isa<ClosureExpr>(E))
          return true;
    return false;
  }

public:
  llvm::MDNode *createInlinedAt(const SILDebugScope *DS) {
    auto *CS = DS->InlinedCallSite;
    if (!CS)
      return nullptr;

    auto CachedInlinedAt = InlinedAtCache.find(CS);
    if (CachedInlinedAt != InlinedAtCache.end())
      return cast<llvm::MDNode>(CachedInlinedAt->second);

    auto L = decodeFileAndLocation(CS->Loc);
    auto Scope = getOrCreateScope(CS->Parent.dyn_cast<const SILDebugScope *>());
    if (auto *Fn = CS->Parent.dyn_cast<SILFunction *>())
      Scope = getOrCreateScope(Fn->getDebugScope());
    // Pretend transparent functions don't exist.
    if (!Scope)
      return createInlinedAt(CS);
    auto InlinedAt = llvm::DILocation::getDistinct(
        IGM.getLLVMContext(), L.Line, L.Column, Scope, createInlinedAt(CS));
    InlinedAtCache.insert({CS, llvm::TrackingMDNodeRef(InlinedAt)});
    return InlinedAt;
  }

private:

#ifndef NDEBUG
  /// Perform a couple of soundness checks on scopes.
  static bool parentScopesAreSane(const SILDebugScope *DS) {
    auto *Parent = DS;
    while ((Parent = Parent->Parent.dyn_cast<const SILDebugScope *>())) {
      if (!DS->InlinedCallSite)
        assert(!Parent->InlinedCallSite &&
               "non-inlined scope has an inlined parent");
    }
    return true;
  }

  /// Assert that within one lexical block, each location is only visited once.
  bool lineEntryIsSane(FileAndLocation DL, const SILDebugScope *DS);

#endif

  llvm::DIFile *getOrCreateFile(StringRef Filename,
                                std::optional<StringRef> Source) {
    if (Filename.empty())
      Filename = SILLocation::getCompilerGeneratedLoc()->filename;

    // Look in the cache first.
    auto CachedFile = DIFileCache.find(Filename);
    if (CachedFile != DIFileCache.end()) {
      // Verify that the information still exists.
      if (llvm::Metadata *V = CachedFile->second)
        return cast<llvm::DIFile>(V);
    }

    // Detect the main file.
    StringRef MainFileName = MainFile->getFilename();
    if (MainFile && Filename.endswith(MainFileName)) {
      SmallString<256> AbsThisFile, AbsMainFile;
      AbsThisFile = Filename;
      llvm::sys::fs::make_absolute(AbsThisFile);
      if (llvm::sys::path::is_absolute(MainFileName))
        AbsMainFile = MainFileName;
      else
        llvm::sys::path::append(AbsMainFile, MainFile->getDirectory(),
                                MainFileName);
      if (AbsThisFile == DebugPrefixMap.remapPath(AbsMainFile)) {
        DIFileCache[Filename] = llvm::TrackingMDNodeRef(MainFile);
        return MainFile;
      }
    }

    return createFile(Filename, std::nullopt, Source);
  }

  /// This is effectively \p clang::CGDebugInfo::createFile().
  llvm::DIFile *
  createFile(StringRef FileName,
             std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,
             std::optional<StringRef> Source) {
    StringRef File, Dir;
    StringRef CurDir = Opts.DebugCompilationDir;
    SmallString<128> NormalizedFile(FileName);
    SmallString<128> FileBuf, DirBuf;
    llvm::sys::path::remove_dots(NormalizedFile);
    if (llvm::sys::path::is_absolute(NormalizedFile) &&
        llvm::sys::path::is_absolute(CurDir)) {
      // Strip the common prefix (if it is more than just "/") from current
      // directory and FileName for a more space-efficient encoding.
      auto FileIt = llvm::sys::path::begin(NormalizedFile);
      auto FileE = llvm::sys::path::end(NormalizedFile);
      auto CurDirIt = llvm::sys::path::begin(CurDir);
      auto CurDirE = llvm::sys::path::end(CurDir);
      for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)
        llvm::sys::path::append(DirBuf, *CurDirIt);
      if (std::distance(llvm::sys::path::begin(CurDir), CurDirIt) == 1) {
        // Don't strip the common prefix if it is only the root "/"
        // since that would make LLVM diagnostic locations confusing.
        Dir = {};
        File = NormalizedFile;
      } else {
        for (; FileIt != FileE; ++FileIt)
          llvm::sys::path::append(FileBuf, *FileIt);
        Dir = DirBuf;
        File = FileBuf;
      }
    } else {
      File = NormalizedFile;
      // Leave <compiler-generated> & friends as is, without directory.
      if (!(File.starts_with("<") && File.endswith(">")))
        Dir = CurDir;
      else
        Dir = llvm::sys::path::root_directory(CurDir);
    }
    llvm::DIFile *F =
        DBuilder.createFile(DebugPrefixMap.remapPath(File),
                            DebugPrefixMap.remapPath(Dir), CSInfo, Source);
    DIFileCache[FileName].reset(F);
    return F;
  }

  StringRef getName(const FuncDecl &FD) {
    // Getters and Setters are anonymous functions, so we forge a name
    // using its parent declaration.
    if (auto accessor = dyn_cast<AccessorDecl>(&FD))
      if (ValueDecl *VD = accessor->getStorage()) {
        const char *Kind;
        switch (accessor->getAccessorKind()) {
        case AccessorKind::Get:
          Kind = ".get";
          break;
        case AccessorKind::DistributedGet:
          Kind = "._distributed_get";
          break;
        case AccessorKind::Set:
          Kind = ".set";
          break;
        case AccessorKind::WillSet:
          Kind = ".willset";
          break;
        case AccessorKind::DidSet:
          Kind = ".didset";
          break;
        case AccessorKind::Address:
          Kind = ".addressor";
          break;
        case AccessorKind::MutableAddress:
          Kind = ".mutableAddressor";
          break;
        case AccessorKind::Read:
          Kind = ".read";
          break;
        case AccessorKind::Modify:
          Kind = ".modify";
          break;
        case AccessorKind::Init:
          Kind = ".init";
          break;
        }

        SmallVector<char, 64> Buf;
        StringRef Name =
            (VD->getBaseName().userFacingName() + Twine(Kind)).toStringRef(Buf);
        return BumpAllocatedString(Name);
      }

    if (FD.hasName())
      return FD.getBaseIdentifier().str();

    return StringRef();
  }

  StringRef getName(SILLocation L) {
    if (L.isNull())
      return StringRef();

    if (FuncDecl *FD = L.getAsASTNode<FuncDecl>())
      return getName(*FD);

    if (ValueDecl *D = L.getAsASTNode<ValueDecl>())
      return D->getBaseName().userFacingName();

    if (auto *D = L.getAsASTNode<MacroExpansionDecl>())
      return D->getMacroName().getBaseIdentifier().str();

    if (auto *E = L.getAsASTNode<MacroExpansionExpr>())
      return E->getMacroName().getBaseIdentifier().str();

    return StringRef();
  }

  static CanSILFunctionType getFunctionType(SILType SILTy) {
    if (!SILTy)
      return CanSILFunctionType();

    auto FnTy = SILTy.getAs<SILFunctionType>();
    if (!FnTy) {
      LLVM_DEBUG(llvm::dbgs() << "Unexpected function type: ";
                 SILTy.print(llvm::dbgs()); llvm::dbgs() << "\n");
      return CanSILFunctionType();
    }

    return FnTy;
  }

  llvm::DIScope *getOrCreateContext(DeclContext *DC) {
    if (!DC)
      return TheCU;

    if (isa<FuncDecl>(DC))
      if (auto *Decl = IGM.getSILModule().lookUpFunction(SILDeclRef(
              cast<AbstractFunctionDecl>(DC), SILDeclRef::Kind::Func)))
        return getOrCreateScope(Decl->getDebugScope());

    switch (DC->getContextKind()) {
    // The interesting cases are already handled above.
    case DeclContextKind::AbstractFunctionDecl:
    case DeclContextKind::AbstractClosureExpr:
    case DeclContextKind::SerializedAbstractClosure:

    // We don't model these in DWARF.
    case DeclContextKind::Initializer:
    case DeclContextKind::ExtensionDecl:
    case DeclContextKind::SubscriptDecl:
    case DeclContextKind::EnumElementDecl:
    case DeclContextKind::TopLevelCodeDecl:
    case DeclContextKind::SerializedTopLevelCodeDecl:
      return getOrCreateContext(DC->getParent());

    case DeclContextKind::Package: {
      auto *pkg = cast<PackageUnit>(DC);
      return getOrCreateContext(pkg);
    }
    case DeclContextKind::Module:
      return getOrCreateModule(
          {ImportPath::Access(), cast<ModuleDecl>(DC)});
    case DeclContextKind::FileUnit:
      // A module may contain multiple files.
      return getOrCreateContext(DC->getParent());
    case DeclContextKind::MacroDecl:
      return getOrCreateContext(DC->getParent());
    case DeclContextKind::GenericTypeDecl: {
      // The generic signature of this nominal type has no relation to the current
      // function's generic signature.
      auto *NTD = cast<NominalTypeDecl>(DC);
      GenericContextScope scope(IGM, NTD->getGenericSignature().getCanonicalSignature());

      auto Ty = NTD->getDeclaredInterfaceType();
      // Create a Forward-declared type.
      auto DbgTy = DebugTypeInfo::getForwardDecl(Ty);
      return getOrCreateType(DbgTy);
    }
    }
    return TheCU;
  }

  void createParameterType(llvm::SmallVectorImpl<llvm::Metadata *> &Parameters,
                           SILType type) {
    auto RealType = type.getASTType();
    auto DbgTy = DebugTypeInfo::getForwardDecl(RealType);
    Parameters.push_back(getOrCreateType(DbgTy));
  }

  // This is different from SILFunctionType::getAllResultsType() in some subtle
  // ways.
  static SILType getResultTypeForDebugInfo(IRGenModule &IGM,
                                           CanSILFunctionType fnTy) {
    if (fnTy->getNumResults() == 1) {
      return fnTy->getResults()[0].getSILStorageType(
          IGM.getSILModule(), fnTy, IGM.getMaximalTypeExpansionContext());
    } else if (!fnTy->getNumIndirectFormalResults()) {
      return fnTy->getDirectFormalResultsType(
          IGM.getSILModule(), IGM.getMaximalTypeExpansionContext());
    } else {
      SmallVector<TupleTypeElt, 4> eltTys;
      for (auto &result : fnTy->getResults()) {
        eltTys.push_back(result.getReturnValueType(
            IGM.getSILModule(), fnTy, IGM.getMaximalTypeExpansionContext()));
      }
      return SILType::getPrimitiveAddressType(
          CanType(TupleType::get(eltTys, fnTy->getASTContext())));
    }
  }

  llvm::DITypeRefArray createParameterTypes(SILType SILTy) {
    if (!SILTy)
      return nullptr;
    return createParameterTypes(SILTy.castTo<SILFunctionType>());
  }

  llvm::DITypeRefArray createParameterTypes(CanSILFunctionType FnTy) {
    SmallVector<llvm::Metadata *, 16> Parameters;

    GenericContextScope scope(IGM, FnTy->getInvocationGenericSignature());

    // The function return type is the first element in the list.
    createParameterType(Parameters, getResultTypeForDebugInfo(IGM, FnTy));

    for (auto &Param : FnTy->getParameters())
      createParameterType(
          Parameters, IGM.silConv.getSILType(
                          Param, FnTy, IGM.getMaximalTypeExpansionContext()));

    return DBuilder.getOrCreateTypeArray(Parameters);
  }

  /// FIXME: replace this condition with something more sound.
  static bool isAllocatingConstructor(SILFunctionTypeRepresentation Rep,
                                      DeclContext *DeclCtx) {
    return Rep != SILFunctionTypeRepresentation::Method && DeclCtx &&
           isa<ConstructorDecl>(DeclCtx);
  }

  void createImportedModule(llvm::DIScope *Context,
                            ImportedModule M, llvm::DIFile *File,
                            unsigned Line) {
    // For overlays of Clang modules also emit an import of the underlying Clang
    // module. The helps the debugger resolve types that are present only in the
    // underlying module.
    if (const clang::Module *UnderlyingClangModule =
            M.importedModule->findUnderlyingClangModule()) {
      DBuilder.createImportedModule(
          Context,
          getOrCreateModule(
              {*const_cast<clang::Module *>(UnderlyingClangModule)},
              UnderlyingClangModule),
          File, 0);
    }
    DBuilder.createImportedModule(Context, getOrCreateModule(M), File, Line);
  }

  llvm::DIModule *getOrCreateModule(const void *Key, llvm::DIScope *Parent,
                                    StringRef Name, StringRef IncludePath,
                                    uint64_t Signature = ~1ULL,
                                    StringRef ASTFile = StringRef()) {
    // Look in the cache first.
    auto Val = DIModuleCache.find(Key);
    if (Val != DIModuleCache.end())
      return cast<llvm::DIModule>(Val->second);

    std::string RemappedIncludePath = DebugPrefixMap.remapPath(IncludePath);
    std::string RemappedASTFile = DebugPrefixMap.remapPath(ASTFile);

    // For Clang modules / PCH, create a Skeleton CU pointing to the PCM/PCH.
    if (!Opts.DisableClangModuleSkeletonCUs) {
      bool CreateSkeletonCU = !ASTFile.empty();
      bool IsRootModule = !Parent;
      if (CreateSkeletonCU && IsRootModule) {
        llvm::DIBuilder DIB(M);
        DIB.createCompileUnit(IGM.ObjCInterop ? llvm::dwarf::DW_LANG_ObjC
                                              : llvm::dwarf::DW_LANG_C99,
                              DIB.createFile(Name, RemappedIncludePath),
                              TheCU->getProducer(), true, StringRef(), 0,
                              RemappedASTFile, llvm::DICompileUnit::FullDebug,
                              Signature);
        DIB.finalize();
      }
    }

    llvm::DIModule *M =
        DBuilder.createModule(Parent, Name, ConfigMacros, RemappedIncludePath);
    DIModuleCache.insert({Key, llvm::TrackingMDNodeRef(M)});
    return M;
  }

  using ASTSourceDescriptor = clang::ASTSourceDescriptor;
  /// Create a DIModule from a clang module or PCH.
  /// The clang::Module pointer is passed separately because the recursive case
  /// needs to fudge the AST descriptor.
  llvm::DIModule *getOrCreateModule(ASTSourceDescriptor Desc,
                                    const clang::Module *ClangModule) {
    // PCH files don't have a signature field in the control block,
    // but LLVM detects skeleton CUs by looking for a non-zero DWO id.
    // We use the lower 64 bits for debug info.
    uint64_t Signature =
      Desc.getSignature() ? Desc.getSignature().truncatedValue() : ~1ULL;

    // Clang modules using fmodule-file-home-is-cwd should have their
    // include path set to the working directory.
    auto &HSI =
        CI.getClangPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
    StringRef IncludePath =
        HSI.ModuleFileHomeIsCwd ? Opts.DebugCompilationDir : Desc.getPath();

    // Handle Clang modules.
    if (ClangModule) {
      llvm::DIModule *Parent = nullptr;
      if (ClangModule->Parent) {
        // The loading of additional modules by Sema may trigger an out-of-date
        // PCM rebuild in the Clang module dependencies of the additional
        // module. A PCM rebuild causes the ModuleManager to unload previously
        // loaded ASTFiles. For this reason we must use the cached ASTFile
        // information here instead of the potentially dangling pointer to the
        // ASTFile that is stored in the clang::Module object.
        //
        // Note: The implementation here assumes that all clang submodules
        //       belong to the same PCM file.
        ASTSourceDescriptor ParentDescriptor(*ClangModule->Parent);
        Parent = getOrCreateModule({ParentDescriptor.getModuleName(),
                                    ParentDescriptor.getPath(),
                                    Desc.getASTFile(), Desc.getSignature()},
                                   ClangModule->Parent);
      }
      return getOrCreateModule(ClangModule, Parent, Desc.getModuleName(),
                               IncludePath, Signature, Desc.getASTFile());
    }
    // Handle PCH.
    return getOrCreateModule(Desc.getASTFile().bytes_begin(), nullptr,
                             Desc.getModuleName(), IncludePath, Signature,
                             Desc.getASTFile());
  };

  static std::optional<ASTSourceDescriptor>
  getClangModule(const ModuleDecl &M) {
    for (auto *FU : M.getFiles())
      if (auto *CMU = dyn_cast_or_null<ClangModuleUnit>(FU))
        if (auto Desc = CMU->getASTSourceDescriptor())
          return Desc;
    return std::nullopt;
  }

  llvm::DIModule *getOrCreateModule(ImportedModule IM) {
    ModuleDecl *M = IM.importedModule;
    if (std::optional<ASTSourceDescriptor> ModuleDesc = getClangModule(*M))
      return getOrCreateModule(*ModuleDesc, ModuleDesc->getModuleOrNull());
    StringRef Path = getFilenameFromDC(M);
    // Use the module 'real' name, which can be different from the name if module
    // aliasing was used (swift modules only). For example, if a source file has
    // 'import Foo', and '-module-alias Foo=Bar' was passed in, the real name of
    // the module on disk is Bar (.swiftmodule or .swiftinterface), and is used
    // for loading and mangling.
    StringRef Name = M->getRealName().str();
    return getOrCreateModule(M, TheCU, Name, Path);
  }

  TypeAliasDecl *getMetadataType(StringRef ArchetypeName) {
    TypeAliasDecl *&Entry = MetadataTypeDeclCache[ArchetypeName];
    if (Entry)
      return Entry;

    SourceLoc NoLoc;
    Entry = new (IGM.Context) TypeAliasDecl(
        NoLoc, NoLoc, IGM.Context.getIdentifier(ArchetypeName), NoLoc,
        /*genericparams*/ nullptr, IGM.Context.TheBuiltinModule);
    Entry->setUnderlyingType(IGM.Context.TheRawPointerType);
    return Entry;
  }

  /// Return the DIFile that is the ancestor of Scope.
  llvm::DIFile *getFile(llvm::DIScope *Scope) {
    while (!isa<llvm::DIFile>(Scope)) {
      switch (Scope->getTag()) {
      case llvm::dwarf::DW_TAG_lexical_block:
        Scope = cast<llvm::DILexicalBlock>(Scope)->getScope();
        break;
      case llvm::dwarf::DW_TAG_subprogram:
        Scope = cast<llvm::DISubprogram>(Scope)->getFile();
        break;
      default:
        return MainFile;
      }
      if (Scope)
        return MainFile;
    }
    return cast<llvm::DIFile>(Scope);
  }

  static unsigned getStorageSizeInBits(const llvm::DataLayout &DL,
                                       ArrayRef<llvm::Value *> Storage) {
    unsigned SizeInBits = 0;
    for (llvm::Value *Piece : Storage)
      SizeInBits += DL.getTypeSizeInBits(Piece->getType());
    return SizeInBits;
  }

  StringRef getMangledName(DebugTypeInfo DbgTy) {
    if (DbgTy.isMetadataType())
      return MetadataTypeDeclCache.find(DbgTy.getDecl()->getName().str())
          ->getKey();

    // This is a bit of a hack. We need a generic signature to use for mangling.
    // If we started with an interface type, just use IGM.getCurGenericContext(),
    // since callers that use interface types typically push a signature that way.
    //
    // Otherwise, if we have a contextual type, find an archetype and ask it for
    // it's generic signature. The context generic signature from the IRGenModule
    // is unlikely to be useful here.
    GenericSignature Sig;
    Type Ty = DbgTy.getType();
    if (Ty->hasArchetype()) {
      Ty.findIf([&](Type t) -> bool {
        if (auto *archetypeTy = t->getAs<PrimaryArchetypeType>()) {
          Sig = archetypeTy->getGenericEnvironment()->getGenericSignature();
          return true;
        }

        if (auto *archetypeTy = t->getAs<PackArchetypeType>()) {
          Sig = archetypeTy->getGenericEnvironment()->getGenericSignature();
          return true;
        }

        return false;
      });

      Ty = Ty->mapTypeOutOfContext();
    } else {
      Sig = IGM.getCurGenericContext();
    }

    // Strip off top level of type sugar (except for type aliases).
    // We don't want Optional<T> and T? to get different debug types.
    while (true) {
      if (auto *ParenTy = dyn_cast<ParenType>(Ty.getPointer())) {
        Ty = ParenTy->getUnderlyingType();
        continue;
      }

      if (auto *SugarTy = dyn_cast<SyntaxSugarType>(Ty.getPointer())) {
        Ty = SugarTy->getSinglyDesugaredType();
        continue;
      }

      break;
    }

    // TODO: Eliminate substitutions in SILFunctionTypes for now.
    // On platforms where the substitutions affect representation, we will need
    // to preserve this info and teach type reconstruction about it.
    Ty = Ty->replaceSubstitutedSILFunctionTypesWithUnsubstituted(
        IGM.getSILModule());

    Mangle::ASTMangler Mangler;
    std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);

    // TODO(https://github.com/apple/swift/issues/57699): We currently cannot round trip some C++ types.
    if (!Opts.DisableRoundTripDebugTypes &&
        !Ty->getASTContext().LangOpts.EnableCXXInterop) {
      // Make sure we can reconstruct mangled types for the debugger.
      auto &Ctx = Ty->getASTContext();
      Type Reconstructed = Demangle::getTypeForMangling(Ctx, Result, Sig);
      if (!Reconstructed) {
        llvm::errs() << "Failed to reconstruct type for " << Result << "\n";
        llvm::errs() << "Original type:\n";
        Ty->dump(llvm::errs());
        if (Sig)
          llvm::errs() << "Generic signature: " << Sig << "\n";
        llvm::errs() << SWIFT_CRASH_BUG_REPORT_MESSAGE << "\n"
          << "Pass '-Xfrontend -disable-round-trip-debug-types' to disable "
             "this assertion.\n";
        abort();
      } else if (!Reconstructed->isEqual(Ty) &&
                 // FIXME: Some existential types are reconstructed without
                 // an explicit ExistentialType wrapping the constraint.
                 !equalWithoutExistentialTypes(Reconstructed, Ty) &&
                 !EqualUpToClangTypes().check(Reconstructed, Ty)) {
        // [FIXME: Include-Clang-type-in-mangling] Remove second check
        llvm::errs() << "Incorrect reconstructed type for " << Result << "\n";
        llvm::errs() << "Original type:\n";
        Ty->dump(llvm::errs());
        llvm::errs() << "Reconstructed type:\n";
        Reconstructed->dump(llvm::errs());
        if (Sig)
          llvm::errs() << "Generic signature: " << Sig << "\n";
        llvm::errs() << SWIFT_CRASH_BUG_REPORT_MESSAGE << "\n"
          << "Pass '-Xfrontend -disable-round-trip-debug-types' to disable "
             "this assertion.\n";
        abort();
      }
    }

    return BumpAllocatedString(Result);
  }

  llvm::DIDerivedType *createMemberType(DebugTypeInfo DbgTy, StringRef Name,
                                        unsigned &OffsetInBits,
                                        llvm::DIScope *Scope,
                                        llvm::DIFile *File,
                                        llvm::DINode::DIFlags Flags) {
    unsigned SizeOfByte = CI.getTargetInfo().getCharWidth();
    auto *Ty = getOrCreateType(DbgTy);
    auto SizeInBits = getSizeInBits(Ty);
    auto *DITy = DBuilder.createMemberType(
        Scope, Name, File, 0, SizeInBits, 0, OffsetInBits, Flags, Ty);
    OffsetInBits += SizeInBits;
    OffsetInBits = llvm::alignTo(OffsetInBits,
                                 SizeOfByte * DbgTy.getAlignment().getValue());
    return DITy;
  }

  llvm::TempDIType createStructForwardDecl(
      DebugTypeInfo DbgTy, NominalTypeDecl *Decl, llvm::DIScope *Scope,
      llvm::DIFile *File, unsigned Line, unsigned SizeInBits,
      llvm::DINode::DIFlags Flags, StringRef UniqueID, StringRef Name) {
    // Forward declare this first because types may be recursive.
    auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_structure_type, Name, Scope, File, Line,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, 0, Flags, UniqueID));

#ifndef NDEBUG
    if (UniqueID.empty())
      assert(!Name.empty() &&
             "no mangled name and no human readable name given");
    else
      assert((UniqueID.starts_with("_T") ||
              UniqueID.starts_with(MANGLING_PREFIX_STR)) &&
             "UID is not a mangled name");
#endif

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[DbgTy.getType()] = TH;
    return FwdDecl;
  }

  llvm::DICompositeType *
  createStructType(DebugTypeInfo DbgTy, NominalTypeDecl *Decl, Type BaseTy,
                   llvm::DIScope *Scope, llvm::DIFile *File, unsigned Line,
                   unsigned SizeInBits, unsigned AlignInBits,
                   llvm::DINode::DIFlags Flags, llvm::DIType *DerivedFrom,
                   unsigned RuntimeLang, StringRef UniqueID) {
    StringRef Name = Decl->getName().str();
    auto FwdDecl = createStructForwardDecl(DbgTy, Decl, Scope, File, Line,
                                           SizeInBits, Flags, UniqueID, Name);
    // Collect the members.
    SmallVector<llvm::Metadata *, 16> Elements;
    unsigned OffsetInBits = 0;
    for (VarDecl *VD : Decl->getStoredProperties()) {
      auto memberTy = BaseTy->getTypeOfMember(IGM.getSwiftModule(), VD);

      if (auto DbgTy = CompletedDebugTypeInfo::getFromTypeInfo(
              VD->getInterfaceType(),
              IGM.getTypeInfoForUnlowered(
                  IGM.getSILTypes().getAbstractionPattern(VD), memberTy),
              IGM))
        Elements.push_back(createMemberType(*DbgTy, VD->getName().str(),
                                            OffsetInBits, Scope, File, Flags));
      else
        // Without complete type info we can only create a forward decl.
        return DBuilder.createForwardDecl(
            llvm::dwarf::DW_TAG_structure_type, UniqueID, Scope, File, Line,
            llvm::dwarf::DW_LANG_Swift, SizeInBits, 0);
    }

    auto DITy = DBuilder.createStructType(
        Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, DerivedFrom,
        DBuilder.getOrCreateArray(Elements), RuntimeLang, nullptr, UniqueID);
    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  /// Creates debug info for a generic struct or class with archetypes (e.g.:
  /// Pair<τ_0_0, τ_0_1>). For types with unsubstituted generic type parameters,
  /// debug info generation doesn't attempt to emit the size and aligment of
  /// the type, as in the general case those are all dependent on substituting
  /// the type parameters in (some exceptions exist, like generic types that are
  /// class constrained). It also doesn't attempt to emit the offset of the
  /// members for the same reason.
  llvm::DICompositeType *createUnsubstitutedGenericStructOrClassType(
      DebugTypeInfo DbgTy, NominalTypeDecl *Decl, Type UnsubstitutedType,
      llvm::DIScope *Scope, llvm::DIFile *File, unsigned Line,
      llvm::DINode::DIFlags Flags, llvm::DIType *DerivedFrom,
      unsigned RuntimeLang, StringRef UniqueID) {
    // FIXME: ideally, we'd like to emit this type with no size and alignment at
    // all (instead of emitting them as 0). Fix this by changing DIBuilder to
    // allow for struct types that have optional size and alignment.
    unsigned SizeInBits = 0;
    unsigned AlignInBits = 0;
    StringRef Name = Decl->getName().str();
    auto FwdDecl = createStructForwardDecl(DbgTy, Decl, Scope, File, Line,
                                           SizeInBits, Flags, UniqueID, Name);

    // Collect the members.
    SmallVector<llvm::Metadata *, 16> Elements;
    for (VarDecl *VD : Decl->getStoredProperties()) {
      auto memberTy =
          UnsubstitutedType->getTypeOfMember(IGM.getSwiftModule(), VD);
      auto DbgTy = DebugTypeInfo::getFromTypeInfo(
          memberTy,
          IGM.getTypeInfoForUnlowered(
              IGM.getSILTypes().getAbstractionPattern(VD), memberTy),
          IGM);
      unsigned OffsetInBits = 0;
      llvm::DIType *DITy = createMemberType(DbgTy, VD->getName().str(),
                                            OffsetInBits, Scope, File, Flags);
      Elements.push_back(DITy);
    }

    auto DITy = DBuilder.createStructType(
        Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, DerivedFrom,
        DBuilder.getOrCreateArray(Elements), RuntimeLang, nullptr, UniqueID);
    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  llvm::DIType *
  createSpecializedEnumType(NominalOrBoundGenericNominalType *EnumTy,
                            EnumDecl *Decl, StringRef MangledName,
                            unsigned SizeInBits, unsigned AlignInBits,
                            llvm::DIScope *Scope, llvm::DIFile *File,
                            unsigned Line, llvm::DINode::DIFlags Flags) {
    auto UnsubstitutedTy = Decl->getDeclaredInterfaceType();
    UnsubstitutedTy = Decl->mapTypeIntoContext(UnsubstitutedTy);

    auto DbgTy = DebugTypeInfo::getFromTypeInfo(
        UnsubstitutedTy, IGM.getTypeInfoForUnlowered(UnsubstitutedTy), IGM);
    Mangle::ASTMangler Mangler;
    std::string DeclTypeMangledName = Mangler.mangleTypeForDebugger(
        UnsubstitutedTy->mapTypeOutOfContext(), {});
    if (DeclTypeMangledName == MangledName) {
      return createUnsubstitutedVariantType(DbgTy, Decl, MangledName, Scope,
                                            File, 0, Flags);
    }
    auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_structure_type, "", Scope, File, 0,
        llvm::dwarf::DW_LANG_Swift, 0, 0, llvm::DINode::FlagZero, MangledName));

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[EnumTy] = TH;
    // Force the creation of the unsubstituted type, don't create it
    // directly so it goes through all the caching/verification logic.
    auto unsubstitutedDbgTy = getOrCreateType(DbgTy);
    auto DIType = createOpaqueStruct(
        Scope, "", File, 0, SizeInBits, AlignInBits, Flags, MangledName,
        collectGenericParams(EnumTy), unsubstitutedDbgTy);
    DBuilder.replaceTemporary(std::move(FwdDecl), DIType);
    return DIType;
  }

/// Create a DICompositeType from a specialized struct. A specialized type
/// is a generic type, or a child type whose parent is generic.
llvm::DIType *
createSpecializedStructOrClassType(NominalOrBoundGenericNominalType *Type,
                                   NominalTypeDecl *Decl, llvm::DIScope *Scope,
                                   llvm::DIFile *File, unsigned Line,
                                   unsigned SizeInBits, unsigned AlignInBits,
                                   llvm::DINode::DIFlags Flags,
                                   StringRef MangledName,
                                   bool IsClass = false) {
  // To emit debug info of the DwarfTypes level for generic types, the strategy
  // is to emit a description of all the fields for the type with archetypes,
  // and still the same debug info as the ASTTypes level for the specialized
  // type. For example, given:
  // struct Pair<T, U> {
  //   let t: T
  //   let u: U
  // }
  // When emitting debug information for a type such as Pair<Int, Double>,
  // emit a description of all the fields for Pair<T, U>, and emit the regular
  // debug information for Pair<Int, Double>.

  auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
      llvm::dwarf::DW_TAG_structure_type, "", Scope, File, Line,
      llvm::dwarf::DW_LANG_Swift, SizeInBits, 0, Flags, MangledName));

  auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
  DITypeCache[Type] = TH;

  // Go from Pair<Int, Double> to Pair<T, U>.
  auto UnsubstitutedTy = Decl->getDeclaredInterfaceType();
  UnsubstitutedTy = Decl->mapTypeIntoContext(UnsubstitutedTy);

  auto DbgTy = DebugTypeInfo::getFromTypeInfo(
      UnsubstitutedTy, IGM.getTypeInfoForUnlowered(UnsubstitutedTy), IGM);
  Mangle::ASTMangler Mangler;
  std::string DeclTypeMangledName =
      Mangler.mangleTypeForDebugger(UnsubstitutedTy->mapTypeOutOfContext(), {});
  if (DeclTypeMangledName == MangledName) {
    return createUnsubstitutedGenericStructOrClassType(
        DbgTy, Decl, UnsubstitutedTy, Scope, File, Line, Flags, nullptr,
        llvm::dwarf::DW_LANG_Swift, DeclTypeMangledName);
  }
  // Force the creation of the unsubstituted type, don't create it
  // directly so it goes through all the caching/verification logic.
  auto UnsubstitutedType = getOrCreateType(DbgTy);

  if (auto *ClassTy = llvm::dyn_cast<BoundGenericClassType>(Type)) {
    auto SuperClassTy = ClassTy->getSuperclass();
    if (SuperClassTy) {
      auto SuperClassDbgTy = DebugTypeInfo::getFromTypeInfo(
          SuperClassTy, IGM.getTypeInfoForUnlowered(SuperClassTy), IGM);

      llvm::DIType *SuperClassDITy = getOrCreateType(SuperClassDbgTy);
      assert(SuperClassDITy && "getOrCreateType should never return null!");
      DBuilder.createInheritance(UnsubstitutedType, SuperClassDITy, 0, 0,
                                 llvm::DINode::FlagZero);
    }

    auto *OpaqueType = createPointerSizedStruct(
        Scope, Decl ? Decl->getNameStr() : MangledName, File, 0, Flags,
        MangledName, UnsubstitutedType);
    return OpaqueType;
  }

  auto *OpaqueType = createOpaqueStruct(
      Scope, "", File, Line, SizeInBits, AlignInBits, Flags, MangledName,
      collectGenericParams(Type), UnsubstitutedType);
  DBuilder.replaceTemporary(std::move(FwdDecl), OpaqueType);
  return OpaqueType;
}

  /// Create debug information for an enum with a raw type (enum E : Int {}).
  llvm::DICompositeType *createRawEnumType(CompletedDebugTypeInfo DbgTy,
                                           EnumDecl *Decl,
                                           StringRef MangledName,
                                           llvm::DIScope *Scope,
                                           llvm::DIFile *File, unsigned Line,
                                           llvm::DINode::DIFlags Flags) {
    assert(
        Decl->hasRawType() &&
        "Trying to create a raw enum debug info from enum with no raw type!");

    StringRef Name = Decl->getName().str();
    unsigned SizeInBits = DbgTy.getSizeInBits();
    // Default, since Swift doesn't allow specifying a custom alignment.
    unsigned AlignInBits = 0;

    auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_enumeration_type, MangledName, Scope, File, Line,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
        MangledName));

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[DbgTy.getType()] = TH;

    auto RawType = Decl->getRawType();
    auto &TI = IGM.getTypeInfoForUnlowered(RawType);
    std::optional<CompletedDebugTypeInfo> ElemDbgTy =
        CompletedDebugTypeInfo::getFromTypeInfo(RawType, TI, IGM);
    if (!ElemDbgTy)
      // Without complete type info we can only create a forward decl.
      return DBuilder.createForwardDecl(
          llvm::dwarf::DW_TAG_enumeration_type, Name, Scope, File, Line,
          llvm::dwarf::DW_LANG_Swift, SizeInBits, 0, MangledName);

    SmallVector<llvm::Metadata *, 16> Elements;
    for (auto *ElemDecl : Decl->getAllElements()) {
      // TODO: add the option to emit an enumerator with no value, and use that
      // instead of emitting a 0.
      auto MTy =
          DBuilder.createEnumerator(ElemDecl->getBaseIdentifier().str(), 0);
      Elements.push_back(MTy);
    }

    auto EnumType = getOrCreateType(*ElemDbgTy);
    auto DITy = DBuilder.createEnumerationType(
        Scope, Name, File, Line, SizeInBits, AlignInBits,
        DBuilder.getOrCreateArray(Elements), EnumType,
        llvm::dwarf::DW_LANG_Swift, MangledName, false);

    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  /// Create debug information for an enum with no raw type.
  llvm::DICompositeType *createVariantType(CompletedDebugTypeInfo DbgTy,
                                           EnumDecl *Decl,
                                           StringRef MangledName,
                                           unsigned AlignInBits,
                                           llvm::DIScope *Scope,
                                           llvm::DIFile *File, unsigned Line,
                                           llvm::DINode::DIFlags Flags) {
    assert(!Decl->getRawType() &&
           "Attempting to create variant debug info from raw enum!");

    StringRef Name = Decl->getName().str();
    unsigned SizeInBits = DbgTy.getSizeInBits();
    auto NumExtraInhabitants = DbgTy.getNumExtraInhabitants();

    // A variant part should actually be a child to a DW_TAG_structure_type
    // according to the DWARF spec.
    auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, File, Line,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
        MangledName));

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[DbgTy.getType()] = TH;

    SmallVector<llvm::Metadata *, 16> Elements;
    for (auto *ElemDecl : Decl->getAllElements()) {
      std::optional<CompletedDebugTypeInfo> ElemDbgTy;
      if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
        // A variant case which carries a payload.
        ArgTy = ElemDecl->getParentEnum()->mapTypeIntoContext(ArgTy);
        auto &TI = IGM.getTypeInfoForUnlowered(ArgTy);
        ElemDbgTy = CompletedDebugTypeInfo::getFromTypeInfo(ArgTy, TI, IGM);
        if (!ElemDbgTy) {
          // Without complete type info we can only create a forward decl.
          return DBuilder.createForwardDecl(
              llvm::dwarf::DW_TAG_structure_type, Name, Scope, File, Line,
              llvm::dwarf::DW_LANG_Swift, SizeInBits, 0, MangledName);
        }
        unsigned Offset = 0;
        auto MTy =
            createMemberType(*ElemDbgTy, ElemDecl->getBaseIdentifier().str(),
                             Offset, Scope, File, Flags);
        Elements.push_back(MTy);
      } else {
        // A variant with no payload.
        auto MTy = DBuilder.createMemberType(
            Scope, ElemDecl->getBaseIdentifier().str(), File, 0, 0, 0, 0, Flags,
            nullptr);
        Elements.push_back(MTy);
      }
    }

    APInt SpareBitsMask;
    auto &EnumStrategy =
        getEnumImplStrategy(IGM, DbgTy.getType()->getCanonicalType());

    auto VariantOffsetInBits = 0;
    if (auto SpareBitsMaskInfo = EnumStrategy.calculateSpareBitsMask()) {
      SpareBitsMask = SpareBitsMaskInfo->bits;
      // The offset of the variant mask in the overall enum.
      VariantOffsetInBits = SpareBitsMaskInfo->byteOffset * 8;
    }

    auto VPTy = DBuilder.createVariantPart(
        Scope, {}, File, Line, SizeInBits, AlignInBits, Flags, nullptr,
        DBuilder.getOrCreateArray(Elements), /*UniqueIdentifier=*/"",
        VariantOffsetInBits, SpareBitsMask);

    auto DITy = DBuilder.createStructType(
        Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, nullptr,
        DBuilder.getOrCreateArray(VPTy), llvm::dwarf::DW_LANG_Swift, nullptr,
        MangledName, nullptr, NumExtraInhabitants ? *NumExtraInhabitants : 0);
    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  // Create debug information for an enum with no raw type.
  llvm::DICompositeType *
  createUnsubstitutedVariantType(DebugTypeInfo DbgTy, EnumDecl *Decl,
                                 StringRef MangledName,
                                 llvm::DIScope *Scope, llvm::DIFile *File,
                                 unsigned Line, llvm::DINode::DIFlags Flags) {
    assert(!Decl->getRawType() &&
           "Attempting to create variant debug info from raw enum!");

    StringRef Name = Decl->getName().str();
    auto NumExtraInhabitants = DbgTy.getNumExtraInhabitants();

    unsigned SizeInBits = 0;
    unsigned AlignInBits = 0;
    // A variant part should actually be a child to a DW_TAG_structure_type
    // according to the DWARF spec.
    auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, File, Line,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
        MangledName));

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[DbgTy.getType()] = TH;

    SmallVector<llvm::Metadata *, 16> Elements;
    for (auto *ElemDecl : Decl->getAllElements()) {
      std::optional<DebugTypeInfo> ElemDbgTy;
      if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
        // A variant case which carries a payload.
        ArgTy = ElemDecl->getParentEnum()->mapTypeIntoContext(ArgTy);
        ElemDbgTy = DebugTypeInfo::getFromTypeInfo(
            ArgTy, IGM.getTypeInfoForUnlowered(ArgTy), IGM);
        unsigned Offset = 0;
        auto MTy =
            createMemberType(*ElemDbgTy, ElemDecl->getBaseIdentifier().str(),
                             Offset, Scope, File, Flags);
        Elements.push_back(MTy);
      } else {
        // A variant with no payload.
        auto MTy = DBuilder.createMemberType(
            Scope, ElemDecl->getBaseIdentifier().str(), File, 0, 0, 0, 0, Flags,
            nullptr);
        Elements.push_back(MTy);
      }
    }

    auto VPTy = DBuilder.createVariantPart(Scope, {}, File, Line, SizeInBits,
                                           AlignInBits, Flags, nullptr,
                                           DBuilder.getOrCreateArray(Elements));

    auto DITy = DBuilder.createStructType(
        Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, nullptr,
        DBuilder.getOrCreateArray(VPTy), llvm::dwarf::DW_LANG_Swift, nullptr,
        MangledName, nullptr, NumExtraInhabitants.value_or(0));
    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  llvm::DICompositeType *createEnumType(CompletedDebugTypeInfo DbgTy,
                                        EnumDecl *Decl, StringRef MangledName,
                                        unsigned AlignInBits,
                                        llvm::DIScope *Scope,
                                        llvm::DIFile *File, unsigned Line,
                                        llvm::DINode::DIFlags Flags) {
    if (Decl->hasRawType())
      return createRawEnumType(DbgTy, Decl, MangledName, Scope, File, Line,
                               Flags);
    return createVariantType(DbgTy, Decl, MangledName, AlignInBits, Scope, File,
                             Line, Flags);
  }

  llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DbgTy) {
    DebugTypeInfo BlandDbgTy(Ty, DbgTy.getFragmentStorageType(),
                             DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(),
                             DbgTy.isMetadataType(), DbgTy.isFixedBuffer());
    return getOrCreateType(BlandDbgTy);
  }

  uint64_t getSizeOfBasicType(CompletedDebugTypeInfo DbgTy) {
    uint64_t BitWidth = DbgTy.getSizeInBits();
    llvm::Type *StorageType = DbgTy.getFragmentStorageType()
                                  ? DbgTy.getFragmentStorageType()
                                  : IGM.DataLayout.getSmallestLegalIntType(
                                        IGM.getLLVMContext(), BitWidth);

    if (StorageType)
      return IGM.DataLayout.getTypeSizeInBits(StorageType);

    // This type is too large to fit in a register.
    assert(BitWidth > IGM.DataLayout.getLargestLegalIntTypeSizeInBits());
    return BitWidth;
  }

  /// Collect the type parameters of a bound generic type. This is needed to
  /// anchor any typedefs that may appear in parameters so they can be
  /// resolved in the debugger without needing to query the Swift module.
  llvm::DINodeArray
  collectGenericParams(NominalOrBoundGenericNominalType *BGT) {

    // Collect the generic args from the type and its parent.
    std::vector<Type> GenericArgs;
    Type CurrentType = BGT;
    while (CurrentType && CurrentType->getAnyNominal()) {
      if (auto *BGT = llvm::dyn_cast<BoundGenericType>(CurrentType))
        GenericArgs.insert(GenericArgs.end(), BGT->getGenericArgs().begin(),
                           BGT->getGenericArgs().end());
      CurrentType = CurrentType->getNominalParent();
    }

    SmallVector<llvm::Metadata *, 16> TemplateParams;
    for (auto Arg : GenericArgs) {
      DebugTypeInfo ParamDebugType;
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
        // For the DwarfTypes level don't generate just a forward declaration
        // for the generic type parameters.
        ParamDebugType = DebugTypeInfo::getFromTypeInfo(
            Arg, IGM.getTypeInfoForUnlowered(Arg), IGM);
      else
        ParamDebugType = DebugTypeInfo::getForwardDecl(Arg);

      TemplateParams.push_back(DBuilder.createTemplateTypeParameter(
          TheCU, "", getOrCreateType(ParamDebugType), false));
    }
    return DBuilder.getOrCreateArray(TemplateParams);
  }

  /// Create a sized container for a sizeless type. Used to represent
  /// BoundGenericEnums that may have different sizes depending on what they are
  /// bound to, but still share a mangled name.
  llvm::DIType *createOpaqueStructWithSizedContainer(
      llvm::DIScope *Scope, StringRef Name, llvm::DIFile *File, unsigned Line,
      unsigned SizeInBits, unsigned AlignInBits, llvm::DINode::DIFlags Flags,
      StringRef MangledName, llvm::DINodeArray BoundParams,
      llvm::DIType *SpecificationOf = nullptr) {
    // This uses a separate cache and not DIRefMap for the inner type to avoid
    // associating the anonymous container (which is specific to the
    // variable/storage and not the type) with the MangledName.
    llvm::DICompositeType *UniqueType = nullptr;
    auto *UID = llvm::MDString::get(IGM.getLLVMContext(), MangledName);
    if (llvm::Metadata *V = InnerTypeCache.lookup(UID))
      UniqueType = cast<llvm::DICompositeType>(V);
    else {
      UniqueType = DBuilder.createForwardDecl(
          llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, File, Line,
          llvm::dwarf::DW_LANG_Swift, 0, 0);
      if (BoundParams)
        DBuilder.replaceArrays(UniqueType, nullptr, BoundParams);
      InnerTypeCache[UID] = llvm::TrackingMDNodeRef(UniqueType);
    }

    llvm::Metadata *Elements[] = {DBuilder.createMemberType(
        Scope, "", File, 0, SizeInBits, AlignInBits, 0, Flags, UniqueType)};
    return DBuilder.createStructType(
        Scope, "", File, Line, SizeInBits, AlignInBits, Flags,
        /* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
        llvm::dwarf::DW_LANG_Swift, nullptr, "", SpecificationOf, 0);
  }

  llvm::DIType *
  createPointerSizedStruct(llvm::DIScope *Scope, StringRef Name,
                           llvm::DIFile *File, unsigned Line,
                           llvm::DINode::DIFlags Flags, StringRef MangledName,
                           llvm::DIType *SpecificationOf = nullptr) {
    if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
      auto FwdDecl = DBuilder.createForwardDecl(
          llvm::dwarf::DW_TAG_structure_type, Name, Scope, File, Line,
          llvm::dwarf::DW_LANG_Swift, 0, 0);
      return createPointerSizedStruct(Scope, Name, FwdDecl, File, Line, Flags,
                                      MangledName, SpecificationOf);
    } else {
      unsigned SizeInBits = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
      return createOpaqueStruct(Scope, Name, File, Line, SizeInBits, 0, Flags,
                                MangledName);
    }
  }

  llvm::DIType *createPointerSizedStruct(
      llvm::DIScope *Scope, StringRef Name, llvm::DIType *PointeeTy,
      llvm::DIFile *File, unsigned Line, llvm::DINode::DIFlags Flags,
      StringRef MangledName, llvm::DIType *SpecificationOf = nullptr) {
    unsigned PtrSize =
        CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
    auto PtrTy = DBuilder.createPointerType(PointeeTy, PtrSize, 0);
    llvm::Metadata *Elements[] = {DBuilder.createMemberType(
        Scope, "ptr", File, 0, PtrSize, 0, 0, Flags, PtrTy)};
    return DBuilder.createStructType(
        Scope, Name, File, Line, PtrSize, 0, Flags,
        /* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
        llvm::dwarf::DW_LANG_Swift, nullptr, MangledName, SpecificationOf);
  }

  llvm::DIType *
  createDoublePointerSizedStruct(llvm::DIScope *Scope, StringRef Name,
                                 llvm::DIType *PointeeTy, llvm::DIFile *File,
                                 unsigned Line, llvm::DINode::DIFlags Flags,
                                 StringRef MangledName) {
    unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
    llvm::Metadata *Elements[] = {
        DBuilder.createMemberType(
            Scope, "ptr", File, 0, PtrSize, 0, 0, Flags,
            DBuilder.createPointerType(PointeeTy, PtrSize, 0)),
        DBuilder.createMemberType(
            Scope, "_", File, 0, PtrSize, 0, 0, Flags,
            DBuilder.createPointerType(nullptr, PtrSize, 0))};
    return DBuilder.createStructType(
        Scope, Name, File, Line, 2 * PtrSize, 0, Flags,
        /* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
        llvm::dwarf::DW_LANG_Swift, nullptr, MangledName);
  }

  llvm::DIType *createFixedValueBufferStruct(llvm::DIType *PointeeTy) {
    unsigned Line = 0;
    unsigned PtrSize = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
    llvm::DINode::DIFlags Flags = llvm::DINode::FlagArtificial;
    llvm::DIFile *File = MainFile;
    llvm::DIScope *Scope = TheCU;
    llvm::Metadata *Elements[] = {DBuilder.createMemberType(
        Scope, "contents", File, 0, PtrSize, 0, 0, Flags, PointeeTy)};
    return DBuilder.createStructType(
        Scope, "$swift.fixedbuffer", File, Line, 3 * PtrSize, 0, Flags,
        /* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
        llvm::dwarf::DW_LANG_Swift, nullptr);
  }

  llvm::DIType *createFunctionPointer(DebugTypeInfo DbgTy, llvm::DIScope *Scope,
                                      unsigned SizeInBits, unsigned AlignInBits,
                                      llvm::DINode::DIFlags Flags,
                                      StringRef MangledName) {
    auto FwdDecl = llvm::TempDINode(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_subroutine_type, MangledName, Scope, MainFile, 0,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
        MangledName));

    auto TH = llvm::TrackingMDNodeRef(FwdDecl.get());
    DITypeCache[DbgTy.getType()] = TH;

    CanSILFunctionType FunTy;
    TypeBase *BaseTy = DbgTy.getType();
    if (auto *SILFnTy = dyn_cast<SILFunctionType>(BaseTy))
      FunTy = CanSILFunctionType(SILFnTy);
    // FIXME: Handling of generic parameters in SIL type lowering is in flux.
    // DebugInfo doesn't appear to care about the generic context, so just
    // throw it away before lowering.
    else if (isa<GenericFunctionType>(BaseTy)) {
      auto *fTy = cast<AnyFunctionType>(BaseTy);
      auto *nongenericTy = FunctionType::get(fTy->getParams(), fTy->getResult(),
                                             fTy->getExtInfo());

      FunTy = IGM.getLoweredType(nongenericTy).castTo<SILFunctionType>();
    } else
      FunTy = IGM.getLoweredType(BaseTy).castTo<SILFunctionType>();
    auto Params = createParameterTypes(FunTy);

    auto FnTy = DBuilder.createSubroutineType(Params, Flags);
    llvm::DIType *DITy;
    if (FunTy->getRepresentation() == SILFunctionType::Representation::Thick) {
      if (SizeInBits == 2 * CI.getTargetInfo().getPointerWidth(clang::LangAS::Default))
        // This is a FunctionPairTy: { i8*, %swift.refcounted* }.
        DITy = createDoublePointerSizedStruct(Scope, MangledName, FnTy,
                                              MainFile, 0, Flags, MangledName);
      else
        // This is a generic function as noted above.
        DITy = createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
                                  AlignInBits, Flags, MangledName);
    } else {
      assert(SizeInBits == CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
      DITy = createPointerSizedStruct(Scope, MangledName, FnTy, MainFile, 0,
                                      Flags, MangledName);
    }
    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  llvm::DIType *createTuple(DebugTypeInfo DbgTy, llvm::DIScope *Scope,
                            unsigned SizeInBits, unsigned AlignInBits,
                            llvm::DINode::DIFlags Flags,
                            StringRef MangledName) {
    TypeBase *BaseTy = DbgTy.getType();
    auto *TupleTy = BaseTy->castTo<TupleType>();

    SmallVector<llvm::Metadata *, 16> Elements;
    unsigned OffsetInBits = 0;
    auto genericSig = IGM.getCurGenericContext();
    for (auto ElemTy : TupleTy->getElementTypes()) {
      auto &elemTI = IGM.getTypeInfoForUnlowered(
          AbstractionPattern(genericSig, ElemTy->getCanonicalType()), ElemTy);
      auto DbgTy =
            DebugTypeInfo::getFromTypeInfo(ElemTy, elemTI, IGM);
        Elements.push_back(
            createMemberType(DbgTy, "", OffsetInBits, Scope, MainFile, Flags));
    }
    // FIXME: assert that SizeInBits == OffsetInBits.

    auto FwdDecl = llvm::TempDINode(DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, MainFile, 0,
        llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits, Flags,
        MangledName));

    DITypeCache[DbgTy.getType()] = llvm::TrackingMDNodeRef(FwdDecl.get());

    auto DITy = DBuilder.createStructType(
        Scope, MangledName, MainFile, 0, SizeInBits, AlignInBits, Flags,
        nullptr, // DerivedFrom
        DBuilder.getOrCreateArray(Elements), llvm::dwarf::DW_LANG_Swift,
        nullptr, MangledName);

    DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
    return DITy;
  }

  llvm::DICompositeType *
  createOpaqueStruct(llvm::DIScope *Scope, StringRef Name, llvm::DIFile *File,
                     unsigned Line, unsigned SizeInBits, unsigned AlignInBits,
                     llvm::DINode::DIFlags Flags, StringRef MangledName,
                     llvm::DINodeArray BoundParams = {},
                     llvm::DIType *SpecificationOf = nullptr) {

    auto StructType = DBuilder.createStructType(
        Scope, Name, File, Line, SizeInBits, AlignInBits, Flags,
        /* DerivedFrom */ nullptr,
        DBuilder.getOrCreateArray(ArrayRef<llvm::Metadata *>()),
        llvm::dwarf::DW_LANG_Swift, nullptr, MangledName, SpecificationOf);

    if (BoundParams)
      DBuilder.replaceArrays(StructType, nullptr, BoundParams);
    return StructType;
  }

  bool shouldCacheDIType(llvm::DIType *DITy, DebugTypeInfo &DbgTy) {
    // Don't cache a type alias to a forward declaration either.
    if (DbgTy.isForwardDecl() || DbgTy.isFixedBuffer() ||
        DITy->isForwardDecl())
      return false;

    if (auto Ty = DbgTy.getType())
      // FIXME: Primary archetypes carry all sorts of auxiliary information
      // that isn't contained in their mangled name.  See also
      // getMangledName().
      return Ty->getKind() != swift::TypeKind::PrimaryArchetype;
    return true;
  }

  llvm::DIType *createType(DebugTypeInfo DbgTy, StringRef MangledName,
                           llvm::DIScope *Scope, llvm::DIFile *File) {
    // FIXME: For SizeInBits, clang uses the actual size of the type on
    // the target machine instead of the storage size that is alloca'd
    // in the LLVM IR. For all types that are boxed in a struct, we are
    // emitting the storage size of the struct, but it may be necessary
    // to emit the (target!) size of the underlying basic type.
    uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
    auto CompletedDbgTy = CompletedDebugTypeInfo::getFromTypeInfo(
      DbgTy.getType(), IGM.getTypeInfoForUnlowered(DbgTy.getType()), IGM);
    std::optional<uint64_t> SizeInBitsOrNull;
    if (CompletedDbgTy)
      SizeInBitsOrNull = CompletedDbgTy->getSizeInBits();

    uint64_t SizeInBits = SizeInBitsOrNull.value_or(0);
    unsigned AlignInBits = DbgTy.hasDefaultAlignment()
                               ? 0
                               : DbgTy.getAlignment().getValue() * SizeOfByte;
    unsigned Encoding = 0;
    uint32_t NumExtraInhabitants = DbgTy.getNumExtraInhabitants().value_or(0);

    llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;

    TypeBase *BaseTy = DbgTy.getType();
    if (!BaseTy) {
      LLVM_DEBUG(llvm::dbgs() << "Type without TypeBase: ";
                 DbgTy.getType()->dump(llvm::dbgs()); llvm::dbgs() << "\n");
      if (!InternalType) {
        StringRef Name = "<internal>";
        InternalType = DBuilder.createForwardDecl(
            llvm::dwarf::DW_TAG_structure_type, Name, Scope, File,
            /*Line*/ 0, llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits,
            MangledName);
      }
      return InternalType;
    }

    // Here goes!
    switch (BaseTy->getKind()) {
    case TypeKind::BuiltinPackIndex:
    case TypeKind::BuiltinInteger: {
      Encoding = llvm::dwarf::DW_ATE_unsigned;
      if (CompletedDbgTy)
        SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
      break;
    }

    case TypeKind::BuiltinIntegerLiteral: {
      Encoding = llvm::dwarf::DW_ATE_unsigned; // ?
      if (CompletedDbgTy)
        SizeInBits = getSizeOfBasicType(*CompletedDbgTy);
      break;
    }

    case TypeKind::BuiltinFloat: {
      auto *FloatTy = BaseTy->castTo<BuiltinFloatType>();
      // Assuming that the bitwidth and FloatTy->getFPKind() are identical.
      SizeInBits = FloatTy->getBitWidth();
      Encoding = llvm::dwarf::DW_ATE_float;
      break;
    }

    case TypeKind::BuiltinNativeObject: 
    case TypeKind::BuiltinBridgeObject:
    case TypeKind::BuiltinRawPointer:
    case TypeKind::BuiltinRawUnsafeContinuation:
    case TypeKind::BuiltinJob: {
      unsigned PtrSize =
          CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
        Flags |= llvm::DINode::FlagArtificial;
        llvm::DICompositeType *PTy = DBuilder.createStructType(
            Scope, MangledName, File, 0, PtrSize, 0, Flags, nullptr, nullptr,
            llvm::dwarf::DW_LANG_Swift, nullptr, {}, nullptr,
            NumExtraInhabitants);
        return PTy;

      }
      llvm::DIDerivedType *PTy = DBuilder.createPointerType(
          nullptr, PtrSize, 0,
          /* DWARFAddressSpace */ std::nullopt, MangledName);

      return DBuilder.createObjectPointerType(PTy);
    }
    case TypeKind::BuiltinExecutor: {
      return createDoublePointerSizedStruct(
          Scope, "Builtin.Executor", nullptr, MainFile, 0,
          llvm::DINode::FlagArtificial, MangledName);
    }

    case TypeKind::DynamicSelf: {
      // Self. We don't have a way to represent instancetype in DWARF,
      // so we emit the static type instead. This is similar to what we
      // do with instancetype in Objective-C.
      auto *DynamicSelfTy = BaseTy->castTo<DynamicSelfType>();
      auto SelfTy =
          getOrCreateDesugaredType(DynamicSelfTy->getSelfType(), DbgTy);
      return DBuilder.createTypedef(SelfTy, MangledName, File, 0, File);
    }

    // Even builtin swift types usually come boxed in a struct.
    case TypeKind::Struct: {
      auto *StructTy = BaseTy->castTo<StructType>();
      auto *Decl = StructTy->getDecl();
      auto L = getFileAndLocation(Decl);
      // No line numbers are attached to type forward declarations.  This is
      // intentional: It interferes with the efficacy of incremental builds. We
      // don't want a whitespace change to an secondary file trigger a
      // recompilation of the debug info of a primary source file.
      unsigned FwdDeclLine = 0;
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
        if (StructTy->isSpecialized())
          return createSpecializedStructOrClassType(
              StructTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
              Flags, MangledName);
        return createStructType(DbgTy, Decl, StructTy, Scope, L.File, L.Line,
                                SizeInBits, AlignInBits, Flags, nullptr,
                                llvm::dwarf::DW_LANG_Swift, MangledName);
      }
      StringRef Name = Decl->getName().str();
      if (!SizeInBitsOrNull)
        return DBuilder.createForwardDecl(
            llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, L.File,
            FwdDeclLine, llvm::dwarf::DW_LANG_Swift, 0, AlignInBits);
      if (DbgTy.isFixedBuffer())
        return DBuilder.createForwardDecl(
            llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, L.File,
            FwdDeclLine, llvm::dwarf::DW_LANG_Swift, 0, AlignInBits);
      return createOpaqueStruct(Scope, Name, L.File, FwdDeclLine, SizeInBits,
                                AlignInBits, Flags, MangledName);
    }

    case TypeKind::Class: {
      // Classes are represented as DW_TAG_structure_type. This way the
      // DW_AT_APPLE_runtime_class(DW_LANG_Swift) attribute can be
      // used to differentiate them from C++ and ObjC classes.
      auto *ClassTy = BaseTy->castTo<ClassType>();
      auto *Decl = ClassTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      assert(SizeInBits ==
             CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
        if (ClassTy->isSpecialized()) 
          return createSpecializedStructOrClassType(
              ClassTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
              Flags, MangledName);

        auto *DIType = createStructType(
            DbgTy, Decl, ClassTy, Scope, File, L.Line, SizeInBits, AlignInBits,
            Flags, nullptr, llvm::dwarf::DW_LANG_Swift, MangledName);
        assert(DIType && "Unexpected null DIType!");
        assert(DIType && "createStructType should never return null!");
        auto SuperClassTy = ClassTy->getSuperclass();
        if (SuperClassTy) {
          auto SuperClassDbgTy = DebugTypeInfo::getFromTypeInfo(
              SuperClassTy, IGM.getTypeInfoForUnlowered(SuperClassTy), IGM);

          llvm::DIType *SuperClassDITy = getOrCreateType(SuperClassDbgTy);
          assert(SuperClassDITy && "getOrCreateType should never return null!");
          DBuilder.retainType(DBuilder.createInheritance(
              DIType, SuperClassDITy, 0, 0, llvm::DINode::FlagZero));
        }
        return DIType;
      }
      return createPointerSizedStruct(Scope, Decl->getNameStr(), L.File,
                                      FwdDeclLine, Flags, MangledName);
    }

    case TypeKind::Protocol: {
      auto *ProtocolTy = BaseTy->castTo<ProtocolType>();
      auto *Decl = ProtocolTy->getDecl();
      // FIXME: (LLVM branch) This should probably be a DW_TAG_interface_type.
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      return createOpaqueStruct(Scope, Decl ? Decl->getNameStr() : MangledName,
                                L.File, FwdDeclLine, SizeInBits, AlignInBits,
                                Flags, MangledName);
    }

    case TypeKind::Existential:
    case TypeKind::ProtocolComposition:
    case TypeKind::ParameterizedProtocol: {
      auto *Decl = DbgTy.getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      return createOpaqueStruct(Scope, Decl ? Decl->getNameStr() : MangledName,
                                L.File, FwdDeclLine, SizeInBits, AlignInBits,
                                Flags, MangledName);
    }

    case TypeKind::UnboundGeneric: {
      auto *UnboundTy = BaseTy->castTo<UnboundGenericType>();
      auto *Decl = UnboundTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      assert(SizeInBits ==
             CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
      return createPointerSizedStruct(Scope,
                                      Decl ? Decl->getNameStr() : MangledName,
                                      L.File, FwdDeclLine, Flags, MangledName);
    }

    case TypeKind::BoundGenericStruct: {
      auto *StructTy = BaseTy->castTo<BoundGenericStructType>();
      auto *Decl = StructTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
        return createSpecializedStructOrClassType(
            StructTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
            Flags, MangledName);
      
      return createOpaqueStructWithSizedContainer(
          Scope, Decl ? Decl->getNameStr() : "", L.File, FwdDeclLine,
          SizeInBits, AlignInBits, Flags, MangledName,
          collectGenericParams(StructTy));
    }

    case TypeKind::BoundGenericClass: {
      auto *ClassTy = BaseTy->castTo<BoundGenericClassType>();
      auto *Decl = ClassTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;

      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) 
        return createSpecializedStructOrClassType(
            ClassTy, Decl, Scope, L.File, L.Line, SizeInBits, AlignInBits,
            Flags, MangledName);

      // TODO: We may want to peek at Decl->isObjC() and set this
      // attribute accordingly.
      assert(SizeInBits ==
             CI.getTargetInfo().getPointerWidth(clang::LangAS::Default));
      return createPointerSizedStruct(Scope,
                                      Decl ? Decl->getNameStr() : MangledName,
                                      L.File, FwdDeclLine, Flags, MangledName);
    }

    case TypeKind::Pack:
    case TypeKind::PackElement:
      llvm_unreachable("Unimplemented!");

    case TypeKind::SILPack:
    case TypeKind::PackExpansion:
      //assert(SizeInBits == CI.getTargetInfo().getPointerWidth(0));
      return createPointerSizedStruct(Scope,
                                      MangledName,
                                      MainFile, 0, Flags, MangledName);

    case TypeKind::BuiltinTuple:
      llvm_unreachable("BuiltinTupleType should not show up here");

    case TypeKind::Tuple: {
      // Tuples are also represented as structs.  Since tuples are ephemeral
      // (not nominal) they don't have a source location.
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
        return createTuple(DbgTy, Scope, SizeInBits, AlignInBits, Flags,
                           MangledName);
      else
        return createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
                                  AlignInBits, Flags, MangledName);
    }

    case TypeKind::InOut:
      break;

    case TypeKind::OpaqueTypeArchetype:
    case TypeKind::PrimaryArchetype:
    case TypeKind::OpenedArchetype:
    case TypeKind::ElementArchetype:
    case TypeKind::PackArchetype: {
      auto *Archetype = BaseTy->castTo<ArchetypeType>();
      AssociatedTypeDecl *assocType = nullptr;
      if (auto depMemTy = Archetype->getInterfaceType()
              ->getAs<DependentMemberType>())
        assocType = depMemTy->getAssocType();
      auto L = getFileAndLocation(assocType);
      if (!L.File)
        L.File = CompilerGeneratedFile;
      unsigned FwdDeclLine = 0;
      auto Superclass = Archetype->getSuperclass();
      auto DerivedFrom = Superclass.isNull()
                             ? nullptr
                             : getOrCreateDesugaredType(Superclass, DbgTy);
      auto FwdDecl = llvm::TempDIType(DBuilder.createReplaceableCompositeType(
          llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, L.File,
          FwdDeclLine, llvm::dwarf::DW_LANG_Swift, SizeInBits, AlignInBits,
          Flags));

      // Emit the protocols the archetypes conform to.
      SmallVector<llvm::Metadata *, 4> Protocols;
      for (auto *ProtocolDecl : Archetype->getConformsTo()) {
        // Skip marker protocols, as they are not available at runtime.
        if (ProtocolDecl->isMarkerProtocol())
          continue;

        auto PTy =
            IGM.getLoweredType(ProtocolDecl->getInterfaceType()).getASTType();
        auto PDbgTy = DebugTypeInfo::getFromTypeInfo(
            ProtocolDecl->getInterfaceType(), IGM.getTypeInfoForLowered(PTy),
            IGM);
        auto PDITy = getOrCreateType(PDbgTy);
        Protocols.push_back(
            DBuilder.createInheritance(FwdDecl.get(), PDITy, 0, 0, Flags));
      }
      auto DITy = DBuilder.createStructType(
          Scope, MangledName, L.File, FwdDeclLine, SizeInBits, AlignInBits,
          Flags, DerivedFrom, DBuilder.getOrCreateArray(Protocols),
          llvm::dwarf::DW_LANG_Swift, nullptr);

      DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
      return DITy;
    }

    case TypeKind::ExistentialMetatype:
    case TypeKind::Metatype: {
      // Metatypes are (mostly) singleton type descriptors, often without
      // storage.
      Flags |= llvm::DINode::FlagArtificial;
      auto L = getFileAndLocation(DbgTy.getDecl());
      unsigned FwdDeclLine = 0;

      return DBuilder.createStructType(Scope, MangledName, L.File, FwdDeclLine,
                                       SizeInBits, AlignInBits, Flags, nullptr,
                                       nullptr, llvm::dwarf::DW_LANG_Swift,
                                       nullptr, MangledName);
    }

    case TypeKind::SILFunction:
    case TypeKind::Function:
    case TypeKind::GenericFunction: {
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
        return createFunctionPointer(DbgTy, Scope, SizeInBits, AlignInBits,
                                     Flags, MangledName);
      else
        return createOpaqueStruct(Scope, MangledName, MainFile, 0, SizeInBits,
                                  AlignInBits, Flags, MangledName);
    }

    case TypeKind::Enum: {
      auto *EnumTy = BaseTy->castTo<EnumType>();
      auto *Decl = EnumTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;
      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
        if (EnumTy->isSpecialized() && !Decl->hasRawType())
          return createSpecializedEnumType(EnumTy, Decl, MangledName,
                                           SizeInBits, AlignInBits, Scope, File,
                                           FwdDeclLine, Flags);

        if (CompletedDbgTy)
          return createEnumType(*CompletedDbgTy, Decl, MangledName, AlignInBits,
                                Scope, L.File, L.Line, Flags);
      }
      return createOpaqueStruct(Scope, Decl->getName().str(), L.File,
                                FwdDeclLine, SizeInBits, AlignInBits, Flags,
                                MangledName);
    }

    case TypeKind::BoundGenericEnum: {
      auto *EnumTy = BaseTy->castTo<BoundGenericEnumType>();
      auto *Decl = EnumTy->getDecl();
      auto L = getFileAndLocation(Decl);
      unsigned FwdDeclLine = 0;

      if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes) {
        if (EnumTy->isSpecialized())
          return createSpecializedEnumType(EnumTy, Decl, MangledName,
                                           SizeInBits, AlignInBits, Scope, File,
                                           FwdDeclLine, Flags);
        if (CompletedDbgTy)
          return createEnumType(*CompletedDbgTy, Decl, MangledName, AlignInBits,
                                Scope, L.File, L.Line, Flags);
      }
      return createOpaqueStructWithSizedContainer(
          Scope, Decl->getName().str(), L.File, FwdDeclLine, SizeInBits,
          AlignInBits, Flags, MangledName, collectGenericParams(EnumTy));
    }

    case TypeKind::BuiltinVector: {
      // FIXME: Emit the name somewhere.
      (void)MangledName;
      auto *BuiltinVectorTy = BaseTy->castTo<BuiltinVectorType>();
      auto ElemTy = BuiltinVectorTy->getElementType();
      auto ElemDbgTy = DebugTypeInfo::getFromTypeInfo(
          ElemTy, IGM.getTypeInfoForUnlowered(ElemTy), IGM);
      unsigned Count = BuiltinVectorTy->getNumElements();
      auto Subscript = DBuilder.getOrCreateSubrange(0, Count ? Count : -1);
      return DBuilder.createVectorType(SizeInBits, AlignInBits,
                                       getOrCreateType(ElemDbgTy),
                                       DBuilder.getOrCreateArray(Subscript));
    }

    // Reference storage types.
#define REF_STORAGE(Name, ...) case TypeKind::Name##Storage:
#include "swift/AST/ReferenceStorage.def"
      {
        auto *ReferenceTy = cast<ReferenceStorageType>(BaseTy);
        auto CanTy = ReferenceTy->getReferentType();
        auto L = getFileAndLocation(DbgTy.getDecl());
        unsigned CompilerGeneratedLine = 0;

        return DBuilder.createTypedef(getOrCreateDesugaredType(CanTy, DbgTy),
                                      MangledName, L.File,
                                      CompilerGeneratedLine, File);
      }

      // Sugared types.

    case TypeKind::TypeAlias: {
      auto *TypeAliasTy = cast<TypeAliasType>(BaseTy);
      auto *Decl = TypeAliasTy->getDecl();
      auto L = getFileAndLocation(Decl);
      auto AliasedTy = TypeAliasTy->getSinglyDesugaredType();

      // For TypeAlias types, the DeclContext for the aliased type is
      // in the decl of the alias type.
      DebugTypeInfo AliasedDbgTy(
          AliasedTy, DbgTy.getFragmentStorageType(),
          DbgTy.getAlignment(), DbgTy.hasDefaultAlignment(),
          /* IsMetadataType = */ false, DbgTy.isFixedBuffer(),
          DbgTy.getNumExtraInhabitants());
      return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
                                    L.File, 0, Scope);
    }

    case TypeKind::Paren: {
      auto Ty = cast<ParenType>(BaseTy)->getUnderlyingType();
      return getOrCreateDesugaredType(Ty, DbgTy);
    }

    // SyntaxSugarType derivations.
    case TypeKind::Dictionary:
    case TypeKind::ArraySlice:
    case TypeKind::Optional:
    case TypeKind::VariadicSequence: {
      auto *SyntaxSugarTy = cast<SyntaxSugarType>(BaseTy);
      auto *CanTy = SyntaxSugarTy->getSinglyDesugaredType();
      return getOrCreateDesugaredType(CanTy, DbgTy);
    }

    // SILBox should appear only inside of coroutine contexts.
    case TypeKind::SILBox:
    case TypeKind::DependentMember:
    case TypeKind::GenericTypeParam: {
      // FIXME: Provide a more meaningful debug type.
      return DBuilder.createStructType(
          Scope, MangledName, File, 0, SizeInBits, AlignInBits, Flags, nullptr,
          nullptr, llvm::dwarf::DW_LANG_Swift, nullptr, MangledName);
    }

    // The following types exist primarily for internal use by the type
    // checker.
    case TypeKind::Error:
    case TypeKind::Unresolved:
    case TypeKind::LValue:
    case TypeKind::TypeVariable:
    case TypeKind::ErrorUnion:
    case TypeKind::Placeholder:
    case TypeKind::Module:
    case TypeKind::SILBlockStorage:
    case TypeKind::SILToken:
    case TypeKind::BuiltinUnsafeValueBuffer:
    case TypeKind::BuiltinDefaultActorStorage:
    case TypeKind::BuiltinNonDefaultDistributedActorStorage:
    case TypeKind::SILMoveOnlyWrapped:
      LLVM_DEBUG(llvm::dbgs() << "Unhandled type: ";
                 DbgTy.getType()->dump(llvm::dbgs()); llvm::dbgs() << "\n");
      MangledName = "<unknown>";
    }
    return DBuilder.createBasicType(MangledName, SizeInBits, Encoding,
                                    llvm::DINode::FlagZero,
                                    NumExtraInhabitants);
  }

  /// Determine if there exists a name mangling for the given type.
  static bool canMangle(TypeBase *Ty) {
    switch (Ty->getKind()) {
    case TypeKind::GenericFunction: // Not yet supported.
    case TypeKind::SILBlockStorage: // Not supported at all.
      return false;
    default:
      return true;
    }
  }

  llvm::DIType *getTypeOrNull(TypeBase *Ty) {
    auto CachedType = DITypeCache.find(Ty);
    if (CachedType != DITypeCache.end()) {
      // Verify that the information still exists.
      if (llvm::Metadata *Val = CachedType->second) {
        auto DITy = cast<llvm::DIType>(Val);
        return DITy;
      }
    }
    return nullptr;
  }

  /// The private discriminator is represented as an inline namespace.
  llvm::DIScope *getFilePrivateScope(llvm::DIScope *Parent, TypeDecl *Decl) {
    // Retrieve the private discriminator.
    auto *MSC = Decl->getDeclContext()->getModuleScopeContext();
    auto *FU = cast<FileUnit>(MSC);
    Identifier PD = FU->getDiscriminatorForPrivateDecl(Decl);
    bool ExportSymbols = true;
    return DBuilder.createNameSpace(Parent, PD.str(), ExportSymbols);
  }

#ifndef NDEBUG
  /// Verify that the size of this type matches the one of the cached type.
  bool sanityCheckCachedType(DebugTypeInfo DbgTy, llvm::DIType *CachedType) {
    // If this is a temporary, we're in the middle of creating a recursive type,
    // so skip the sanity check.
    if (CachedType->isTemporary())
      return true;
    if (DbgTy.isForwardDecl())
      return true;
    auto CompletedDbgTy = CompletedDebugTypeInfo::getFromTypeInfo(
      DbgTy.getType(), IGM.getTypeInfoForUnlowered(DbgTy.getType()), IGM);
    std::optional<uint64_t> SizeInBits;
    if (CompletedDbgTy)
      SizeInBits = CompletedDbgTy->getSizeInBits();
    unsigned CachedSizeInBits = getSizeInBits(CachedType);
    if ((SizeInBits && CachedSizeInBits != *SizeInBits) ||
        (!SizeInBits && CachedSizeInBits)) {
      // In some situation a specialized type is emitted with size 0, even if the real 
      // type has a size.
      if (DbgTy.getType()->isSpecialized() && SizeInBits && *SizeInBits > 0 &&
          CachedSizeInBits == 0)
        return true;

      CachedType->dump();
      DbgTy.dump();
      llvm::errs() << "SizeInBits = " << SizeInBits << "\n";
      llvm::errs() << "CachedSizeInBits = " << CachedSizeInBits << "\n";
      return false;
    }
    return true;
  }
#endif

  /// Emits the special builtin types into the debug info. These types are the
  /// ones that are unconditionally emitted into the stdlib's metadata and are
  /// needed to correctly calculate the layout of more complex types built on
  /// top of them.
  void createSpecialStlibBuiltinTypes() {
    if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::ASTTypes) 
      return;
    for (auto BuiltinType: IGM.getOrCreateSpecialStlibBuiltinTypes()) {
      auto DbgTy = DebugTypeInfo::getFromTypeInfo(
          BuiltinType, IGM.getTypeInfoForUnlowered(BuiltinType), IGM);
      DBuilder.retainType(getOrCreateType(DbgTy));
    }
  }

  llvm::DIType *getOrCreateType(DebugTypeInfo DbgTy) {
    // Is this an empty type?
    if (DbgTy.isNull())
      // We can't use the empty type as an index into DenseMap.
      return createType(DbgTy, "", TheCU, MainFile);

    // Look in the cache first.
    if (auto *DITy = getTypeOrNull(DbgTy.getType())) {
      assert(sanityCheckCachedType(DbgTy, DITy));
      return DITy;
    }

    // Second line of defense: Look up the mangled name. TypeBase*'s are
    // not necessarily unique, but name mangling is too expensive to do
    // every time.
    StringRef MangledName;
    llvm::MDString *UID = nullptr;
    if (canMangle(DbgTy.getType())) {
      MangledName = getMangledName(DbgTy);
      UID = llvm::MDString::get(IGM.getLLVMContext(), MangledName);
      if (llvm::Metadata *CachedTy = DIRefMap.lookup(UID)) {
        auto DITy = cast<llvm::DIType>(CachedTy);
        assert(sanityCheckCachedType(DbgTy, DITy));
        return DITy;
      }
    }

    // Retrieve the context of the type, as opposed to the DeclContext
    // of the variable.
    //
    // FIXME: Builtin and qualified types in LLVM have no parent
    // scope. TODO: This can be fixed by extending DIBuilder.
    llvm::DIScope *Scope = nullptr;
    // Make sure to retrieve the context of the type alias, not the pointee.
    DeclContext *Context = nullptr;
    const Decl *TypeDecl = nullptr;
    const clang::Decl *ClangDecl = nullptr;
    if (auto Alias = dyn_cast<TypeAliasType>(DbgTy.getType())) {
      TypeAliasDecl *AliasDecl = Alias->getDecl();
      TypeDecl = AliasDecl;
      Context = AliasDecl->getParent();
      ClangDecl = AliasDecl->getClangDecl();
    } else if (auto *ND = DbgTy.getType()->getNominalOrBoundGenericNominal()) {
      TypeDecl = ND;
      Context = ND->getParent();
      ClangDecl = ND->getClangDecl();
    } else if (auto BNO = dyn_cast<BuiltinType>(DbgTy.getType())) {
      Context = BNO->getASTContext().TheBuiltinModule;
    }
    if (ClangDecl) {
      clang::ASTReader &Reader = *CI.getClangInstance().getASTReader();
      auto Idx = ClangDecl->getOwningModuleID();
      auto SubModuleDesc = Reader.getSourceDescriptor(Idx);
      auto TopLevelModuleDesc = getClangModule(*TypeDecl->getModuleContext());
      if (SubModuleDesc) {
        if (TopLevelModuleDesc)
          // Describe the submodule, but substitute the cached ASTFile from
          // the toplevel module. The ASTFile pointer in SubModule may be
          // dangling and cant be trusted.
          Scope = getOrCreateModule({SubModuleDesc->getModuleName(),
                                     SubModuleDesc->getPath(),
                                     TopLevelModuleDesc->getASTFile(),
                                     TopLevelModuleDesc->getSignature()},
                                    SubModuleDesc->getModuleOrNull());
        else if (SubModuleDesc->getModuleOrNull() == nullptr)
          // This is (bridging header) PCH.
          Scope = getOrCreateModule(*SubModuleDesc, nullptr);
      }
    }
    if (!Scope)
      Scope = getOrCreateContext(Context);

    // Scope outermost fileprivate decls in an inline private discriminator
    // namespace.
    StringRef Name = MangledName;
    if (auto *Decl = DbgTy.getDecl()) {
      Name = Decl->getName().str();
      if (Decl->isOutermostPrivateOrFilePrivateScope())
        Scope = getFilePrivateScope(Scope, Decl);
    }

    // If this is a forward decl, create one for this mangled name and don't
    // cache it.
    if (DbgTy.isForwardDecl() && !isa<TypeAliasType>(DbgTy.getType())) {
      // In LTO type uniquing is performed based on the UID. Forward
      // declarations may not have a unique ID to avoid a forward declaration
      // winning over a full definition.
      auto *FwdDecl = DBuilder.createReplaceableCompositeType(
          llvm::dwarf::DW_TAG_structure_type, MangledName, Scope, 0, 0,
          llvm::dwarf::DW_LANG_Swift, 0, 0, llvm::DINode::FlagFwdDecl);
      FwdDeclTypes.emplace_back(
          std::piecewise_construct, std::make_tuple(MangledName),
          std::make_tuple(static_cast<llvm::Metadata *>(FwdDecl)));
      return FwdDecl;
    }
    llvm::DIType *DITy = createType(DbgTy, MangledName, Scope, getFile(Scope));


    if (!shouldCacheDIType(DITy, DbgTy))
        return  DITy;

    // Incrementally build the DIRefMap.
    if (auto *CTy = dyn_cast<llvm::DICompositeType>(DITy)) {
#ifndef NDEBUG
      // Soundness check.
      if (llvm::Metadata *V = DIRefMap.lookup(UID)) {
        auto *CachedTy = cast<llvm::DIType>(V);
        assert(CachedTy == DITy && "conflicting types for one UID");
      }
#endif
      // If this type supports a UID, enter it to the cache.
      if (auto UID = CTy->getRawIdentifier()) {
        assert(UID->getString() == MangledName &&
               "Unique identifier is different from mangled name ");
        DIRefMap[UID] = llvm::TrackingMDNodeRef(DITy);
      }
    }

    // Store it in the cache.
    DITypeCache.insert({DbgTy.getType(), llvm::TrackingMDNodeRef(DITy)});

    return DITy;
  }
};

IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
                                       ClangImporter &CI, IRGenModule &IGM,
                                       llvm::Module &M,
                                       StringRef MainOutputFilenameForDebugInfo,
                                       StringRef PD)
    : Opts(Opts), CI(CI), SM(IGM.Context.SourceMgr), M(M), DBuilder(M),
      IGM(IGM), DebugPrefixMap(Opts.DebugPrefixMap) {
  assert(Opts.DebugInfoLevel > IRGenDebugInfoLevel::None &&
         "no debug info should be generated");
  llvm::SmallString<256> SourcePath;
  if (MainOutputFilenameForDebugInfo.empty())
    SourcePath = "<unknown>";
  else
    SourcePath = MainOutputFilenameForDebugInfo;

  unsigned Lang = llvm::dwarf::DW_LANG_Swift;
  std::string Producer = version::getSwiftFullVersion(
      IGM.Context.LangOpts.EffectiveLanguageVersion);
  unsigned Major, Minor;
  std::tie(Major, Minor) = version::getSwiftNumericVersion();
  unsigned MajorRuntimeVersion = Major;

  // No split DWARF on Darwin.
  StringRef SplitName = StringRef();
  // Note that File + Dir need not result in a valid path.
  // The directory part of the main file is the current working directory.
  std::string RemappedFile = DebugPrefixMap.remapPath(SourcePath);
  std::string RemappedDir = DebugPrefixMap.remapPath(Opts.DebugCompilationDir);
  bool RelFile = llvm::sys::path::is_relative(RemappedFile);
  bool RelDir = llvm::sys::path::is_relative(RemappedDir);
  MainFile = (RelFile && RelDir)
                 ? createFile(SourcePath, {}, {})
                 : DBuilder.createFile(RemappedFile, RemappedDir);
  CompilerGeneratedFile = getOrCreateFile("", {});

  StringRef Sysroot = IGM.Context.SearchPathOpts.getSDKPath();
  StringRef SDK;
  {
    auto B = llvm::sys::path::rbegin(Sysroot);
    auto E = llvm::sys::path::rend(Sysroot);
    auto It = std::find_if(B, E, [](auto SDK) { return SDK.endswith(".sdk"); });
    if (It != E)
      SDK = *It;
  }

  bool EnableCXXInterop =
      IGM.getSILModule().getASTContext().LangOpts.EnableCXXInterop;
  bool EnableEmbeddedSwift =
      IGM.getSILModule().getASTContext().LangOpts.hasFeature(Feature::Embedded);
  TheCU = DBuilder.createCompileUnit(
      Lang, MainFile, Producer, Opts.shouldOptimize(),
      Opts.getDebugFlags(PD, EnableCXXInterop, EnableEmbeddedSwift),
      MajorRuntimeVersion, SplitName,
      Opts.DebugInfoLevel > IRGenDebugInfoLevel::LineTables
          ? llvm::DICompileUnit::FullDebug
          : llvm::DICompileUnit::LineTablesOnly,
      /* DWOId */ 0, /* SplitDebugInlining */ true,
      /* DebugInfoForProfiling */ false,
      llvm::DICompileUnit::DebugNameTableKind::Default,
      /* RangesBaseAddress */ false, DebugPrefixMap.remapPath(Sysroot), SDK);

  // Because the swift compiler relies on Clang to setup the Module,
  // the clang CU is always created first.  Several dwarf-reading
  // tools (older versions of ld64, and lldb) can get confused if the
  // first CU in an object is empty, so ensure that the Swift CU comes
  // first by rearranging the list of CUs in the LLVM module.
  llvm::NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
  SmallVector<llvm::DICompileUnit *, 2> CUs;
  for (auto *N : CU_Nodes->operands())
    CUs.push_back(cast<llvm::DICompileUnit>(N));
  CU_Nodes->dropAllReferences();
  for (auto CU = CUs.rbegin(), CE = CUs.rend(); CU != CE; ++CU)
    CU_Nodes->addOperand(*CU);

  // Create a module for the current compile unit.
  auto *MDecl = IGM.getSwiftModule();
  llvm::sys::path::remove_filename(SourcePath);
  MainModule = getOrCreateModule(MDecl, TheCU, Opts.ModuleName, SourcePath);
  DBuilder.createImportedModule(MainFile, MainModule, MainFile, 0);

  // Macro definitions that were defined by the user with "-Xcc -D" on the
  // command line. This does not include any macros defined by ClangImporter.
  llvm::raw_svector_ostream OS(ConfigMacros);
  unsigned I = 0;
  // Translate the macro definitions back into a command line.
  for (auto &Macro : Opts.ClangDefines) {
    if (++I > 1)
      OS << ' ';
    OS << '"';
    for (char c : Macro)
      switch (c) {
      case '\\':
        OS << "\\\\";
        break;
      case '"':
        OS << "\\\"";
        break;
      default:
        OS << c;
      }
    OS << '"';
  }
  createSpecialStlibBuiltinTypes();
}

void IRGenDebugInfoImpl::finalize() {
  assert(LocationStack.empty() && "Mismatch of pushLoc() and popLoc().");

  // Get the list of imported modules (which may actually be different
  // from all ImportDecls).
  SmallVector<ImportedModule, 8> ModuleWideImports;
  IGM.getSwiftModule()->getImportedModules(ModuleWideImports,
                                           ModuleDecl::getImportFilterLocal());
  for (auto M : ModuleWideImports)
    if (!ImportedModules.count(M.importedModule))
      createImportedModule(MainFile, M, MainFile, 0);

  // Finalize all replaceable forward declarations.
  auto finalize = [&](llvm::MDNode *FwdDeclType, llvm::MDNode *FullType,
                      llvm::MDString *UID = nullptr) {
    llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(FwdDeclType));
    llvm::Metadata *Replacement = FullType ? FullType : FwdDeclType;
    llvm::Metadata *Replaced = DBuilder.replaceTemporary(
        std::move(FwdDecl), cast<llvm::MDNode>(Replacement));

    // Unique all identical forward declarations.
    if (UID && !FullType)
      DIRefMap[UID] = llvm::TrackingMDNodeRef(cast<llvm::MDNode>(Replaced));
  };

  for (auto &Ty : FwdDeclTypes) {
    auto *UID = llvm::MDString::get(IGM.getLLVMContext(), Ty.first);
    finalize(cast<llvm::MDNode>(Ty.second),
             llvm::cast_or_null<llvm::DIType>(DIRefMap.lookup(UID)), UID);
  }
  FwdDeclTypes.clear();

  // Finalize the DIBuilder.
  DBuilder.finalize();
}

#ifndef NDEBUG
bool IRGenDebugInfoImpl::lineEntryIsSane(FileAndLocation DL,
                                         const SILDebugScope *DS) {
  // All bets are off for optimized code.
  if (!VerifyLineTable || Opts.shouldOptimize())
    return true;
  // We entered a new lexical block.
  if (DS != LastScope)
    PreviousLineEntries.clear();
  if (DL.Line == 0 || DL == PreviousFileAndLocation)
    return true;
  // Save the last non-zero line entry.
  PreviousFileAndLocation = DL;
  auto ItNew = PreviousLineEntries.insert(FileAndLocationKey(DL));
  // Return true iff DL was not yet in PreviousLineEntries.
  return ItNew.second;
}
#endif

void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
                                       const SILDebugScope *DS,
                                       SILLocation Loc) {
  assert(DS && "empty scope");
  auto *Scope = getOrCreateScope(DS);
  if (!Scope)
    return;

  // NOTE: In CodeView, zero is not an artificial line location. We try to
  //       avoid those line locations near user code to reduce the number
  //       of breaks in the linetables.
  FileAndLocation L;
  SILFunction *Fn = DS->getInlinedFunction();
  if (Fn && (Fn->isThunk() || Fn->isTransparent())) {
    L = {0, 0, CompilerGeneratedFile};
  } else if (DS == LastScope && Loc.isHiddenFromDebugInfo()) {
    // Reuse the last source location if we are still in the same
    // scope to get a more contiguous line table.
    L = LastFileAndLocation;
  } else if (DS == LastScope &&
             (Loc.is<ArtificialUnreachableLocation>() || Loc.isLineZero(SM)) &&
             Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView) {
    // If the scope has not changed and the line number is either zero or
    // artificial, we want to keep the most recent debug location.
    L = LastFileAndLocation;
  } else {
    // Decode the location.
    if (!Loc.isInPrologue() ||
        Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
      L = decodeFileAndLocation(Loc);

    // Otherwise use a line 0 artificial location, but the file from the
    // location. If we are emitting CodeView, we do not want to use line zero
    // since it does not represent an artificial line location.
    if (Loc.isHiddenFromDebugInfo() &&
        Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView) {
      L.Line = 0;
      L.Column = 0;
    }
  }

  if (L.getFilename() != Scope->getFilename()) {
    // We changed files in the middle of a scope. This happens, for
    // example, when constructors are inlined. Create a new scope to
    // reflect this.
    Scope = DBuilder.createLexicalBlockFile(Scope, L.File);
  }

  assert(lineEntryIsSane(L, DS) &&
         "non-contiguous debug location in same scope at -Onone");
  LastFileAndLocation = L;
  LastScope = DS;

  auto *InlinedAt = createInlinedAt(DS);
  assert(((!InlinedAt) || (InlinedAt && Scope)) && "inlined w/o scope");
  assert(parentScopesAreSane(DS) && "parent scope sanity check failed");
  auto DL = llvm::DILocation::get(IGM.getLLVMContext(), L.Line, L.Column, Scope,
                                  InlinedAt);
#ifndef NDEBUG
  {
    llvm::DILocalScope *Scope = DL->getInlinedAtScope();
    llvm::DISubprogram *SP = Scope->getSubprogram();
    llvm::Function *F = Builder.GetInsertBlock()->getParent();
    assert((!F || SP->describes(F)) && "location points to different function");
  }
#endif
  Builder.SetCurrentDebugLocation(DL);
}

void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder,
                                                       StringRef failureMsg) {
  auto TrapLoc = Builder.getCurrentDebugLocation();

  // Create a function in the debug info which has failureMsg as name.
  // TrapSc is the SIL debug scope which corresponds to TrapSP in the LLVM debug
  // info.
  RegularLocation ALoc = RegularLocation::getAutoGeneratedLocation();
  const SILDebugScope *TrapSc = new (IGM.getSILModule()) SILDebugScope(ALoc);

  llvm::DISubroutineType *DIFnTy = DBuilder.createSubroutineType(nullptr);

  llvm::DISubprogram *TrapSP;
  auto It = RuntimeErrorFnCache.find(failureMsg);
  if (It != RuntimeErrorFnCache.end())
    TrapSP = llvm::cast<llvm::DISubprogram>(It->second);
  else {
    std::string FuncName = "Swift runtime failure: ";
    FuncName += failureMsg;
    llvm::DIFile *File = getOrCreateFile({}, {});
    TrapSP = DBuilder.createFunction(
        File, FuncName, StringRef(), File, 0,
        DIFnTy, 0, llvm::DINode::FlagArtificial,
        llvm::DISubprogram::SPFlagDefinition, nullptr, nullptr, nullptr);
    RuntimeErrorFnCache.insert({failureMsg, llvm::TrackingMDNodeRef(TrapSP)});
  }

  ScopeCache[TrapSc] = llvm::TrackingMDNodeRef(TrapSP);
  LastScope = TrapSc;

  assert(parentScopesAreSane(TrapSc) && "parent scope sanity check failed");

  // Wrap the existing TrapLoc into the failure function.
  auto DL = llvm::DILocation::get(IGM.getLLVMContext(), 0, 0, TrapSP, TrapLoc);
  Builder.SetCurrentDebugLocation(DL);
}

void IRGenDebugInfoImpl::clearLoc(IRBuilder &Builder) {
  LastFileAndLocation = {};
  LastScope = nullptr;
  Builder.SetCurrentDebugLocation(llvm::DebugLoc());
}

/// Push the current debug location onto a stack and initialize the
/// IRBuilder to an empty location.
void IRGenDebugInfoImpl::pushLoc() {
  LocationStack.push_back(std::make_pair(LastFileAndLocation, LastScope));
  LastFileAndLocation = {};
  LastScope = nullptr;
}

/// Restore the current debug location from the stack.
void IRGenDebugInfoImpl::popLoc() {
  std::tie(LastFileAndLocation, LastScope) = LocationStack.pop_back_val();
}

/// This is done for WinDbg to avoid having two non-contiguous sets of
/// instructions because the ``@llvm.trap`` instruction gets placed at the end
/// of the function.
void IRGenDebugInfoImpl::setInlinedTrapLocation(IRBuilder &Builder,
                                                const SILDebugScope *Scope) {
  if (Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView)
    return;

  // The @llvm.trap could be inlined into a chunk of code that was also inlined.
  // If this is the case then simply using the LastScope's location would
  // generate debug info that claimed Function A owned Block X and Block X
  // thought it was owned by Function B. Therefore, we need to find the last
  // inlined scope to point to.
  const SILDebugScope *TheLastScope = LastScope;
  while (TheLastScope->InlinedCallSite &&
         TheLastScope->InlinedCallSite != TheLastScope) {
    TheLastScope = TheLastScope->InlinedCallSite;
  }
  auto LastLocation = llvm::DILocation::get(
      IGM.getLLVMContext(), LastFileAndLocation.Line,
      LastFileAndLocation.Column, getOrCreateScope(TheLastScope));
  // FIXME: This location should point to stdlib instead of being artificial.
  auto DL = llvm::DILocation::get(IGM.getLLVMContext(), 0, 0,
                                  getOrCreateScope(Scope), LastLocation);
  Builder.SetCurrentDebugLocation(DL);
}

void IRGenDebugInfoImpl::setEntryPointLoc(IRBuilder &Builder) {
  auto DL = llvm::DILocation::get(IGM.getLLVMContext(), 0, 0, getEntryPointFn(),
                                  nullptr);
  Builder.SetCurrentDebugLocation(DL);
}

llvm::DIScope *IRGenDebugInfoImpl::getEntryPointFn() {
  // Lazily create EntryPointFn.
  if (!EntryPointFn) {
    EntryPointFn = DBuilder.createReplaceableCompositeType(
        llvm::dwarf::DW_TAG_subroutine_type,
        IGM.getSILModule().getASTContext().getEntryPointFunctionName(),
        MainFile, MainFile, 0);
  }
  return EntryPointFn;
}

llvm::DIScope *IRGenDebugInfoImpl::getOrCreateScope(const SILDebugScope *DS) {
  if (DS == nullptr)
    return MainFile;

  // Try to find it in the cache first.
  auto CachedScope = ScopeCache.find(DS);
  if (CachedScope != ScopeCache.end())
    return cast<llvm::DIScope>(CachedScope->second);

  // If this is an (inlined) function scope, the function may
  // not have been created yet.
  if (auto *SILFn = DS->Parent.dyn_cast<SILFunction *>()) {
    auto *FnScope = SILFn->getDebugScope();
    // FIXME: This is a bug in the SIL deserialization.
    if (!FnScope)
      SILFn->setDebugScope(DS);

    auto CachedScope = ScopeCache.find(FnScope);
    if (CachedScope != ScopeCache.end())
      return cast<llvm::DIScope>(CachedScope->second);

    // Force the debug info for the function to be emitted, even if it
    // is external or has been inlined.
    llvm::Function *Fn = nullptr;
    // Avoid materializing generic functions in embedded Swift mode.
    bool genericInEmbedded =
        IGM.Context.LangOpts.hasFeature(Feature::Embedded) &&
        SILFn->isGeneric();
    if (!SILFn->getName().empty() && !SILFn->isZombie() && !genericInEmbedded)
      Fn = IGM.getAddrOfSILFunction(SILFn, NotForDefinition);
    auto *SP = emitFunction(*SILFn, Fn);

    // Cache it.
    ScopeCache[DS] = llvm::TrackingMDNodeRef(SP);
    return SP;
  }

  auto *ParentScope = DS->Parent.get<const SILDebugScope *>();
  llvm::DIScope *Parent = getOrCreateScope(ParentScope);
  assert(isa<llvm::DILocalScope>(Parent) && "not a local scope");

  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return Parent;

  assert(DS->Parent && "lexical block must have a parent subprogram");
  auto L = getStartLocation(DS->Loc);
  auto *DScope = DBuilder.createLexicalBlock(Parent, L.File, L.Line, L.Column);

  // Cache it.
  ScopeCache[DS] = llvm::TrackingMDNodeRef(DScope);
  return DScope;
}

void IRGenDebugInfoImpl::emitImport(ImportDecl *D) {
  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return;

  assert(D->getModule() && "compiler-synthesized ImportDecl is incomplete");
  ImportedModule Imported = { D->getAccessPath(), D->getModule() };
  auto L = getFileAndLocation(D);
  createImportedModule(L.File, Imported, L.File, L.Line);
  ImportedModules.insert(Imported.importedModule);
}

llvm::DISubprogram *IRGenDebugInfoImpl::emitFunction(SILFunction &SILFn,
                                                     llvm::Function *Fn) {
  auto *DS = SILFn.getDebugScope();
  assert(DS && "SIL function has no debug scope");
  (void)DS;
  return emitFunction(SILFn.getDebugScope(), Fn, SILFn.getRepresentation(),
                      SILFn.getLoweredType(), SILFn.getDeclContext());
}

llvm::DISubprogram *
IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
                                 SILFunctionTypeRepresentation Rep,
                                 SILType SILTy, DeclContext *DeclCtx,
                                 StringRef outlinedFromName) {
  auto Cached = ScopeCache.find(DS);
  if (Cached != ScopeCache.end()) {
    auto SP = cast<llvm::DISubprogram>(Cached->second);
    // If we created the DISubprogram for a forward declaration,
    // attach it to the function now.
    if (!Fn->getSubprogram() && !Fn->isDeclaration())
      Fn->setSubprogram(SP);
    return SP;
  }

  // Some IRGen-generated helper functions don't have a corresponding
  // SIL function, hence the dyn_cast.
  auto *SILFn = DS ? DS->Parent.dyn_cast<SILFunction *>() : nullptr;

  StringRef LinkageName;
  if (!outlinedFromName.empty())
    LinkageName = outlinedFromName;
  else if (Fn)
    LinkageName = Fn->getName();
  else if (DS)
    LinkageName = SILFn->getName();
  else
    llvm_unreachable("function has no mangled name");

  StringRef Name;
  if (DS) {
    if (DS->Loc.isSILFile())
      Name = SILFn->getName();
    else
      Name = getName(DS->Loc);
  }

  /// The source line used for the function prologue.
  unsigned ScopeLine = 0;
  FileAndLocation L;
  if (!DS || (SILFn && (SILFn->isBare() || SILFn->isThunk() ||
                        SILFn->isTransparent()))) {
    // Bare functions and thunks should not have any line numbers. This
    // is especially important for shared functions like reabstraction
    // thunk helpers, where DS->Loc is an arbitrary location of whichever use
    // was emitted first.
    L = {0, 0, CompilerGeneratedFile};
  } else {
    L = decodeFileAndLocation(DS->Loc);
    ScopeLine = L.Line;
  }

  auto Line = L.Line;
  auto File = L.File;
  llvm::DIScope *Scope = MainModule;
  if (SILFn && SILFn->getDeclContext())
    Scope = getOrCreateContext(SILFn->getDeclContext()->getParent());

  // We know that main always comes from MainFile.
  if (LinkageName ==
      IGM.getSILModule().getASTContext().getEntryPointFunctionName()) {
    File = MainFile;
    Line = 1;
    Name = LinkageName;
  }

  CanSILFunctionType FnTy = getFunctionType(SILTy);
  auto Params = Opts.DebugInfoLevel > IRGenDebugInfoLevel::LineTables
                    ? createParameterTypes(SILTy)
                    : nullptr;
  llvm::DISubroutineType *DIFnTy = DBuilder.createSubroutineType(Params);
  llvm::DITemplateParameterArray TemplateParameters = nullptr;
  llvm::DISubprogram *Decl = nullptr;

  // Various flags.
  llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
  // Mark everything that is not visible from the source code (i.e.,
  // does not have a Swift name) as artificial, so the debugger can
  // ignore it. Explicit closures are exempt from this rule. We also
  // make an exception for toplevel code, which, although it does not
  // have a Swift name, does appear prominently in the source code.
  // ObjC thunks should also not show up in the linetable, because we
  // never want to set a breakpoint there.
  if ((Name.empty() &&
       LinkageName !=
           IGM.getSILModule().getASTContext().getEntryPointFunctionName() &&
       !isExplicitClosure(SILFn)) ||
      (Rep == SILFunctionTypeRepresentation::ObjCMethod) ||
      isAllocatingConstructor(Rep, DeclCtx)) {
    Flags |= llvm::DINode::FlagArtificial;
    ScopeLine = 0;
  }

  if (FnTy &&
      FnTy->getRepresentation() == SILFunctionType::Representation::Block)
    Flags |= llvm::DINode::FlagAppleBlock;

  // Get the throws information.
  llvm::DITypeArray Error = nullptr;
  if (FnTy && (Opts.DebugInfoLevel > IRGenDebugInfoLevel::LineTables))
    if (auto ErrorInfo = FnTy->getOptionalErrorResult()) {
      GenericContextScope scope(IGM, FnTy->getInvocationGenericSignature());
      CanType errorResultTy = ErrorInfo->getReturnValueType(
          IGM.getSILModule(), FnTy,
          IGM.getMaximalTypeExpansionContext());
      SILType SILTy = IGM.silConv.getSILType(
          *ErrorInfo, FnTy, IGM.getMaximalTypeExpansionContext());

      errorResultTy = SILFn->mapTypeIntoContext(errorResultTy)
          ->getCanonicalType();
      SILTy = SILFn->mapTypeIntoContext(SILTy);

      auto DTI = DebugTypeInfo::getFromTypeInfo(
          errorResultTy,
          IGM.getTypeInfo(SILTy), IGM);
      Error = DBuilder.getOrCreateArray({getOrCreateType(DTI)}).get();
    }

  llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::toSPFlags(
      /*IsLocalToUnit=*/Fn ? Fn->hasInternalLinkage() : true,
      /*IsDefinition=*/true, /*IsOptimized=*/Opts.shouldOptimize());

  // When the function is a method, we want a DW_AT_declaration there.
  // Because there's no good way to cross the CU boundary to insert a nested
  // DISubprogram definition in one CU into a type defined in another CU when
  // doing LTO builds.
  if (llvm::isa<llvm::DICompositeType>(Scope) &&
      (Rep == SILFunctionTypeRepresentation::Method ||
       Rep == SILFunctionTypeRepresentation::ObjCMethod ||
       Rep == SILFunctionTypeRepresentation::WitnessMethod ||
       Rep == SILFunctionTypeRepresentation::CXXMethod ||
       Rep == SILFunctionTypeRepresentation::Thin)) {
    llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::toSPFlags(
        /*IsLocalToUnit=*/Fn ? Fn->hasInternalLinkage() : true,
        /*IsDefinition=*/false, /*IsOptimized=*/Opts.shouldOptimize());
    Decl = DBuilder.createMethod(Scope, Name, LinkageName, File, Line, DIFnTy,
                                 0, 0, nullptr, Flags, SPFlags,
                                 TemplateParameters, Error);
  }

  // Construct the DISubprogram.
  llvm::DISubprogram *SP = DBuilder.createFunction(
      Scope, Name, LinkageName, File, Line, DIFnTy, ScopeLine, Flags, SPFlags,
      TemplateParameters, Decl, Error);

  if (Fn && !Fn->isDeclaration())
    Fn->setSubprogram(SP);

  // RAUW the entry point function forward declaration with the real thing.
  if (LinkageName ==
      IGM.getSILModule().getASTContext().getEntryPointFunctionName()) {
    if (EntryPointFn) {
      assert(EntryPointFn->isTemporary() &&
             "more than one entry point function");
      EntryPointFn->replaceAllUsesWith(SP);
      llvm::MDNode::deleteTemporary(EntryPointFn);
    }
    EntryPointFn = SP;
  }

  if (!DS)
    return nullptr;

  ScopeCache[DS] = llvm::TrackingMDNodeRef(SP);
  return SP;
}

void IRGenDebugInfoImpl::emitArtificialFunction(IRBuilder &Builder,
                                                llvm::Function *Fn,
                                                SILType SILTy) {
  RegularLocation ALoc = RegularLocation::getAutoGeneratedLocation();
  const SILDebugScope *Scope = new (IGM.getSILModule()) SILDebugScope(ALoc);
  emitFunction(Scope, Fn, SILFunctionTypeRepresentation::Thin, SILTy);
  /// Reusing the current file would be wrong: An objc thunk, for example, could
  /// be triggered from any random location. Use a placeholder name instead.
  setCurrentLoc(Builder, Scope, ALoc);
}

void IRGenDebugInfoImpl::emitOutlinedFunction(IRBuilder &Builder,
                                              llvm::Function *Fn,
                                              StringRef outlinedFromName) {
  RegularLocation ALoc = RegularLocation::getAutoGeneratedLocation();
  const SILDebugScope *Scope = new (IGM.getSILModule()) SILDebugScope(ALoc);
  emitFunction(Scope, Fn, SILFunctionTypeRepresentation::Thin, SILType(),
               nullptr, outlinedFromName);
  /// Reusing the current file would be wrong: An objc thunk, for example, could
  /// be triggered from any random location. Use a placeholder name instead.
  setCurrentLoc(Builder, Scope, ALoc);
}

bool IRGenDebugInfoImpl::handleFragmentDIExpr(
    const SILDIExprOperand &CurDIExprOp,
    llvm::DIExpression::FragmentInfo &Fragment) {
  if (CurDIExprOp.getOperator() == SILDIExprOperator::TupleFragment)
    return handleTupleFragmentDIExpr(CurDIExprOp, Fragment);
  assert(CurDIExprOp.getOperator() == SILDIExprOperator::Fragment);
  // Expecting a VarDecl that points to a field in an struct
  auto DIExprArgs = CurDIExprOp.args();
  auto *VD = dyn_cast_or_null<VarDecl>(DIExprArgs.size() ?
                                       DIExprArgs[0].getAsDecl() : nullptr);
  assert(VD && "Expecting a VarDecl as the operand for "
               "DIExprOperator::Fragment");
  // Translate the based type
  DeclContext *ParentDecl = VD->getDeclContext();
  assert(ParentDecl && "VarDecl has no parent context?");
  SILType ParentSILType =
      IGM.getLoweredType(ParentDecl->getDeclaredTypeInContext());
  // Retrieve the offset & size of the field
  llvm::Constant *Offset =
      emitPhysicalStructMemberFixedOffset(IGM, ParentSILType, VD);
  auto *FieldTypeInfo = getPhysicalStructFieldTypeInfo(IGM, ParentSILType, VD);
  // FIXME: This will only happen if IRGen hasn't processed ParentSILType
  // (into its own representation) but we probably should ask IRGen to process
  // it right now.
  if (!FieldTypeInfo)
    return false;
  llvm::Type *FieldTy = FieldTypeInfo->getStorageType();
  // Doesn't support non-fixed or empty types right now.
  if (!Offset || !FieldTy || !FieldTy->isSized())
    return false;

  uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
  uint64_t SizeInBits = IGM.DataLayout.getTypeSizeInBits(FieldTy);
  uint64_t OffsetInBits =
      Offset->getUniqueInteger().getLimitedValue() * SizeOfByte;

  // Translate to DW_OP_LLVM_fragment operands
  Fragment = {SizeInBits, OffsetInBits};

  return true;
}

bool IRGenDebugInfoImpl::handleTupleFragmentDIExpr(
    const SILDIExprOperand &CurDIExprOp,
    llvm::DIExpression::FragmentInfo &Fragment) {
  assert(CurDIExprOp.getOperator() == SILDIExprOperator::TupleFragment);
  // Expecting a TupleType followed by an index
  auto DIExprArgs = CurDIExprOp.args();
  assert(DIExprArgs.size() >= 2 && "Expecting two arguments for "
         "DIExprOperator::TupleFragment");
  auto *TT = dyn_cast<TupleType>(DIExprArgs[0].getAsType().getPointer());
  assert(TT && "Expecting a TupleType as the first operand for "
               "DIExprOperator::TupleFragment");
  auto Idx = DIExprArgs[1].getAsConstInt();
  assert(Idx && "Expecting an index as the second operand for "
               "DIExprOperator::TupleFragment");
  // Translate the based type
  SILType ParentSILType = IGM.getLoweredType(TT);
  // Retrieve the offset & size of the field
  auto Offset = getFixedTupleElementOffset(IGM, ParentSILType, *Idx);
  auto ElementType = TT->getElement(*Idx).getType()->getCanonicalType();
  llvm::Type *FieldTy = IGM.getStorageTypeForLowered(ElementType);
  // Doesn't support non-fixed or empty types right now.
  if (!Offset || !FieldTy || !FieldTy->isSized())
    return false;

  uint64_t SizeInBits = IGM.DataLayout.getTypeSizeInBits(FieldTy);
  uint64_t OffsetInBits = Offset->getValueInBits();

  // Translate to DW_OP_LLVM_fragment operands
  Fragment = {SizeInBits, OffsetInBits};

  return true;
}

bool IRGenDebugInfoImpl::buildDebugInfoExpression(
    const SILDebugVariable &VarInfo, SmallVectorImpl<uint64_t> &Operands,
    llvm::DIExpression::FragmentInfo &Fragment) {
  assert(VarInfo.DIExpr && "SIL debug info expression not found");

  const auto &DIExpr = VarInfo.DIExpr;
  for (const SILDIExprOperand &ExprOperand : DIExpr.operands()) {
    llvm::DIExpression::FragmentInfo SubFragment = {0, 0};
    switch (ExprOperand.getOperator()) {
    case SILDIExprOperator::Fragment:
    case SILDIExprOperator::TupleFragment:
      if (!handleFragmentDIExpr(ExprOperand, SubFragment))
        return false;
      assert(!Fragment.SizeInBits
             || (SubFragment.OffsetInBits + SubFragment.SizeInBits
              <= Fragment.SizeInBits)
             && "Invalid nested fragments");
      Fragment.OffsetInBits += SubFragment.OffsetInBits;
      Fragment.SizeInBits = SubFragment.SizeInBits;
      break;
    case SILDIExprOperator::Dereference:
      Operands.push_back(llvm::dwarf::DW_OP_deref);
      break;
    case SILDIExprOperator::Plus:
      Operands.push_back(llvm::dwarf::DW_OP_plus);
      break;
    case SILDIExprOperator::Minus:
      Operands.push_back(llvm::dwarf::DW_OP_minus);
      break;
    case SILDIExprOperator::ConstUInt:
      Operands.push_back(llvm::dwarf::DW_OP_constu);
      Operands.push_back(*ExprOperand[1].getAsConstInt());
      break;
    case SILDIExprOperator::ConstSInt:
      Operands.push_back(llvm::dwarf::DW_OP_consts);
      Operands.push_back(*ExprOperand[1].getAsConstInt());
      break;
    case SILDIExprOperator::INVALID:
      return false;
    }
  }
  if (Operands.size() && Operands.back() != llvm::dwarf::DW_OP_deref) {
    Operands.push_back(llvm::dwarf::DW_OP_stack_value);
  }
  return true;
}

void IRGenDebugInfoImpl::emitVariableDeclaration(
    IRBuilder &Builder, ArrayRef<llvm::Value *> Storage, DebugTypeInfo DbgTy,
    const SILDebugScope *DS, std::optional<SILLocation> DbgInstLoc,
    SILDebugVariable VarInfo, IndirectionKind Indirection,
    ArtificialKind Artificial, AddrDbgInstrKind AddrDInstrKind) {
  assert(DS && "variable has no scope");

  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return;

  // We cannot yet represent local archetypes.
  if (DbgTy.getType()->hasLocalArchetype())
    return;

  auto *Scope = dyn_cast_or_null<llvm::DILocalScope>(getOrCreateScope(DS));
  assert(Scope && "variable has no local scope");
  auto DInstLoc = getStartLocation(DbgInstLoc);

  // FIXME: this should be the scope of the type's declaration.
  // If this is an argument, attach it to the current function scope.
  uint16_t ArgNo = VarInfo.ArgNo;
  if (ArgNo > 0) {
    while (isa<llvm::DILexicalBlock>(Scope))
      Scope = cast<llvm::DILexicalBlock>(Scope)->getScope();
  }
  assert(isa_and_nonnull<llvm::DIScope>(Scope) && "variable has no scope");
  llvm::DIFile *Unit = getFile(Scope);
  llvm::DIType *DITy = getOrCreateType(DbgTy);
  assert(DITy && "could not determine debug type of variable");
  if (VarInfo.Constant)
    DITy = DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_const_type, DITy);

  unsigned DInstLine = DInstLoc.Line;

  // Self is always an artificial argument, so are variables without location.
  if (!DInstLine || (ArgNo > 0 && VarInfo.Name == IGM.Context.Id_self.str()))
    Artificial = ArtificialValue;

  llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
  if (Artificial || DITy->isArtificial() || DITy == InternalType)
    Flags |= llvm::DINode::FlagArtificial;

  // Create the descriptor for the variable.
  unsigned DVarLine = DInstLine;
  uint16_t DVarCol = DInstLoc.Column;
  auto VarInfoLoc = VarInfo.Loc ? VarInfo.Loc : DbgInstLoc;
  if (VarInfoLoc) {
    auto VarLoc = VarInfoLoc->strippedForDebugVariable();
    if (VarLoc != DbgInstLoc) {
      auto DVarLoc = getStartLocation(VarLoc);
      DVarLine = DVarLoc.Line;
      DVarCol = DVarLoc.Column;
    }
  }
  llvm::DIScope *VarScope = Scope;
  if (ArgNo == 0 && VarInfo.Scope) {
    if (auto *VS = dyn_cast_or_null<llvm::DILocalScope>(
            getOrCreateScope(VarInfo.Scope))) {
      VarScope = VS;
    }
  }

  // Get or create the DILocalVariable.
  llvm::DILocalVariable *Var;
  // VarInfo.Name points into tail-allocated storage in debug_value insns.
  llvm::StringRef UniqueName = VarNames.insert(VarInfo.Name).first->getKey();
  VarID Key(VarScope, UniqueName, DVarLine, DVarCol);
  auto CachedVar = LocalVarCache.find(Key);
  if (CachedVar != LocalVarCache.end()) {
    Var = cast<llvm::DILocalVariable>(CachedVar->second);
  } else {
    // The llvm.dbg.value(undef) emitted for zero-sized variables get filtered
    // out by DwarfDebug::collectEntityInfo(), so all variables need to be
    // preserved even at -Onone.
    bool Preserve = true;
    if (ArgNo > 0)
      Var = DBuilder.createParameterVariable(
          VarScope, VarInfo.Name, ArgNo, Unit, DVarLine, DITy, Preserve, Flags);
    else
      Var = DBuilder.createAutoVariable(VarScope, VarInfo.Name, Unit, DVarLine,
                                        DITy, Preserve, Flags);
    LocalVarCache.insert({Key, llvm::TrackingMDNodeRef(Var)});
  }

  // Running variables for the current/previous piece.
  bool IsPiece = Storage.size() > 1;
  uint64_t SizeOfByte = CI.getTargetInfo().getCharWidth();
  unsigned AlignInBits = SizeOfByte;
  unsigned OffsetInBits = 0;
  unsigned SizeInBits = 0;
  llvm::DIExpression::FragmentInfo Fragment = {0, 0};

  auto appendDIExpression =
      [&VarInfo, this](llvm::DIExpression *DIExpr,
                       llvm::DIExpression::FragmentInfo PieceFragment)
      -> llvm::DIExpression * {
    if (!VarInfo.DIExpr) {
      if (!PieceFragment.SizeInBits)
        return DIExpr;

      return llvm::DIExpression::createFragmentExpression(
                 DIExpr, PieceFragment.OffsetInBits, PieceFragment.SizeInBits)
          .value_or(nullptr);
    }

    llvm::SmallVector<uint64_t, 2> Operands;
    llvm::DIExpression::FragmentInfo VarFragment = {0, 0};
    if (!buildDebugInfoExpression(VarInfo, Operands, VarFragment))
      return nullptr;

    if (!Operands.empty())
      DIExpr = llvm::DIExpression::append(DIExpr, Operands);

    // Add the fragment of the SIL variable.
    if (VarFragment.SizeInBits)
      DIExpr = llvm::DIExpression::createFragmentExpression(
                   DIExpr, VarFragment.OffsetInBits, VarFragment.SizeInBits)
                   .value_or(nullptr);

    if (!DIExpr)
      return nullptr;

    // When the fragment of the SIL variable is further split into other
    // fragments (PieceFragment), merge them into one DW_OP_LLVM_Fragment
    // expression.
    if (PieceFragment.SizeInBits)
      return llvm::DIExpression::createFragmentExpression(
                 DIExpr, PieceFragment.OffsetInBits, PieceFragment.SizeInBits)
          .value_or(nullptr);

    return DIExpr;
  };

  for (llvm::Value *Piece : Storage) {
    SmallVector<uint64_t, 3> Operands;

    if (DbgTy.getType()->isForeignReferenceType())
      Operands.push_back(llvm::dwarf::DW_OP_deref);

    if (Indirection == IndirectValue || Indirection == CoroIndirectValue)
      Operands.push_back(llvm::dwarf::DW_OP_deref);

    if (IsPiece) {
      // Advance the offset for the next piece.
      OffsetInBits += SizeInBits;
      SizeInBits = IGM.DataLayout.getTypeSizeInBits(Piece->getType());
      AlignInBits = IGM.DataLayout.getABITypeAlign(Piece->getType()).value();
      if (!AlignInBits)
        AlignInBits = SizeOfByte;

      // Soundness checks.
#ifndef NDEBUG
      assert(SizeInBits && "zero-sized piece");
      if (getSizeInBits(Var)) {
        assert(SizeInBits < getSizeInBits(Var) && "piece covers entire var");
        assert(OffsetInBits + SizeInBits <= getSizeInBits(Var) &&
               "pars > totum");
      }
#endif

      // Add the piece DW_OP_LLVM_fragment operands
      Fragment.OffsetInBits = OffsetInBits;
      Fragment.SizeInBits = SizeInBits;
    }
    llvm::DIExpression *DIExpr = DBuilder.createExpression(Operands);
    DIExpr = appendDIExpression(DIExpr, Fragment);
    if (DIExpr)
      emitDbgIntrinsic(
          Builder, Piece, Var, DIExpr, DInstLine, DInstLoc.Column, Scope, DS,
          Indirection == CoroDirectValue || Indirection == CoroIndirectValue,
          AddrDInstrKind);
  }

  // Emit locationless intrinsic for variables that were optimized away.
  if (Storage.empty()) {
    llvm::DIExpression::FragmentInfo NoFragment = {0, 0};
    if (auto *DIExpr =
            appendDIExpression(DBuilder.createExpression(), NoFragment))
      emitDbgIntrinsic(Builder, llvm::ConstantInt::get(IGM.Int64Ty, 0), Var,
                       DIExpr, DInstLine, DInstLoc.Column, Scope, DS,
                       Indirection == CoroDirectValue ||
                           Indirection == CoroIndirectValue,
                       AddrDInstrKind);
  }
}

namespace {

/// A helper struct that is used by emitDbgIntrinsic to factor redundant code.
struct DbgIntrinsicEmitter {
  PointerUnion<llvm::BasicBlock *, llvm::Instruction *> InsertPt;
  irgen::IRBuilder &IRBuilder;
  llvm::DIBuilder &DIBuilder;
  AddrDbgInstrKind ForceDbgDeclare;

  /// Initialize the emitter and initialize the emitter to assume that it is
  /// going to insert an llvm.dbg.declare or an llvm.dbg.addr either at the
  /// current "generalized insertion point" of the IRBuilder. The "generalized
  /// insertion point" is
  DbgIntrinsicEmitter(irgen::IRBuilder &IRBuilder, llvm::DIBuilder &DIBuilder,
                      AddrDbgInstrKind ForceDebugDeclare)
      : InsertPt(), IRBuilder(IRBuilder), DIBuilder(DIBuilder),
        ForceDbgDeclare(ForceDebugDeclare) {
    auto *ParentBB = IRBuilder.GetInsertBlock();
    auto InsertBefore = IRBuilder.GetInsertPoint();

    if (InsertBefore != ParentBB->end())
      InsertPt = &*InsertBefore;
    else
      InsertPt = ParentBB;
  }

  ///

  llvm::Instruction *insert(llvm::Value *Addr, llvm::DILocalVariable *VarInfo,
                            llvm::DIExpression *Expr,
                            const llvm::DILocation *DL) {
    if (auto *Inst = InsertPt.dyn_cast<llvm::Instruction *>()) {
      return insert(Addr, VarInfo, Expr, DL, Inst);
    } else {
      return insert(Addr, VarInfo, Expr, DL,
                    InsertPt.get<llvm::BasicBlock *>());
    }
  }

  llvm::Instruction *insert(llvm::Value *Addr, llvm::DILocalVariable *VarInfo,
                            llvm::DIExpression *Expr,
                            const llvm::DILocation *DL,
                            llvm::Instruction *InsertBefore) {
    if (ForceDbgDeclare == AddrDbgInstrKind::DbgDeclare)
      return DIBuilder.insertDeclare(Addr, VarInfo, Expr, DL, InsertBefore);
    Expr = llvm::DIExpression::append(Expr, llvm::dwarf::DW_OP_deref);
    return DIBuilder.insertDbgValueIntrinsic(Addr, VarInfo, Expr, DL,
                                            InsertBefore);
  }

  llvm::Instruction *insert(llvm::Value *Addr, llvm::DILocalVariable *VarInfo,
                            llvm::DIExpression *Expr,
                            const llvm::DILocation *DL,
                            llvm::BasicBlock *Block) {
    if (ForceDbgDeclare == AddrDbgInstrKind::DbgDeclare)
      return DIBuilder.insertDeclare(Addr, VarInfo, Expr, DL, Block);
    Expr = llvm::DIExpression::append(Expr, llvm::dwarf::DW_OP_deref);
    return DIBuilder.insertDbgValueIntrinsic(Addr, VarInfo, Expr, DL, Block);
  }
};

} // namespace

void IRGenDebugInfoImpl::emitDbgIntrinsic(
    IRBuilder &Builder, llvm::Value *Storage, llvm::DILocalVariable *Var,
    llvm::DIExpression *Expr, unsigned Line, unsigned Col,
    llvm::DILocalScope *Scope, const SILDebugScope *DS, bool InCoroContext,
    AddrDbgInstrKind AddrDInstKind) {
  Storage = Storage->stripPointerCasts();
  // Set the location/scope of the intrinsic.
  auto *InlinedAt = createInlinedAt(DS);
  auto DL =
      llvm::DILocation::get(IGM.getLLVMContext(), Line, Col, Scope, InlinedAt);

  // Fragment DIExpression cannot cover the whole variable
  // or going out-of-bound.
  if (auto Fragment = Expr->getFragmentInfo()) {
    if (auto VarSize = Var->getSizeInBits()) {
      unsigned FragSize = Fragment->SizeInBits;
      unsigned FragOffset = Fragment->OffsetInBits;
      if (FragOffset + FragSize > *VarSize || FragSize == *VarSize) {
        // Drop the fragment part
        assert(Expr->isValid());
        // Since this expression is valid, DW_OP_LLVM_fragment
        // and its arguments must be the last 3 elements.
        auto OrigElements = Expr->getElements();
        Expr = DBuilder.createExpression(OrigElements.drop_back(3));
      }
    }
  }

  auto *ParentBlock = Builder.GetInsertBlock();

  // First before we do anything, check if we have an Undef. In this case, we
  // /always/ emit an llvm.dbg.value of undef.
  // If we have undef, always emit a llvm.dbg.value in the current position.
  if (isa<llvm::UndefValue>(Storage)) {
    if (Expr->getNumElements() &&
        (Expr->getElement(0) == llvm::dwarf::DW_OP_consts
         || Expr->getElement(0) == llvm::dwarf::DW_OP_constu)) {
      /// Convert `undef, expr op_consts:N:...` to `N, expr ...`
      Storage = llvm::ConstantInt::get(
          llvm::IntegerType::getInt64Ty(Builder.getContext()),
          Expr->getElement(1));
      Expr = llvm::DIExpression::get(Builder.getContext(),
                                     Expr->getElements().drop_front(2));
    }
    DBuilder.insertDbgValueIntrinsic(Storage, Var, Expr, DL, ParentBlock);
    return;
  }

  bool optimized = DS->getParentFunction()->shouldOptimize();
  if (optimized && (!InCoroContext || !Var->isParameter()))
    AddrDInstKind = AddrDbgInstrKind::DbgValueDeref;

  DbgIntrinsicEmitter inserter{Builder, DBuilder, AddrDInstKind};

  // If we have a single alloca...
  if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Storage)) {
    auto InsertBefore = Builder.GetInsertPoint();

    if (AddrDInstKind == AddrDbgInstrKind::DbgDeclare) {
      ParentBlock = Alloca->getParent();
      InsertBefore = std::next(Alloca->getIterator());
    }

    if (InsertBefore != ParentBlock->end()) {
      inserter.insert(Alloca, Var, Expr, DL, &*InsertBefore);
    } else {
      inserter.insert(Alloca, Var, Expr, DL, ParentBlock);
    }
    return;
  }

  if ((isa<llvm::IntrinsicInst>(Storage) &&
       cast<llvm::IntrinsicInst>(Storage)->getIntrinsicID() ==
           llvm::Intrinsic::coro_alloca_get)) {
    inserter.insert(Storage, Var, Expr, DL, ParentBlock);
    return;
  }

  if (InCoroContext && (Var->isParameter() || !optimized)) {
    PointerUnion<llvm::BasicBlock *, llvm::Instruction *> InsertPt;

    // If we have a dbg.declare, we are relying on a contract with the coroutine
    // splitter that in split coroutines we always create debug info for values
    // in the coroutine context by creating a llvm.dbg.declare for the variable
    // in the entry block of each funclet.
    if (AddrDInstKind == AddrDbgInstrKind::DbgDeclare) {
      // Function arguments in async functions are emitted without a shadow copy
      // (that would interfere with coroutine splitting) but with a
      // llvm.dbg.declare to give CoroSplit.cpp license to emit a shadow copy
      // for them pointing inside the Swift Context argument that is valid
      // throughout the function.
      auto &EntryBlock = ParentBlock->getParent()->getEntryBlock();
      if (auto *InsertBefore = &*EntryBlock.getFirstInsertionPt()) {
        InsertPt = InsertBefore;
      } else {
        InsertPt = &EntryBlock;
      }
    } else {
      // For llvm.dbg.value, we just want to insert the intrinsic at the current
      // insertion point. This is because our contract with the coroutine
      // splitter is that the coroutine splitter just needs to emit the
      // llvm.dbg.value where we placed them. It shouldn't move them or do
      // anything special with it. Instead, we have previously inserted extra
      // debug_value clones previously after each instruction at the SIL level
      // that corresponds with a funclet edge. This operation effectively sets
      // up the rest of the pipeline to be stupid and just emit the
      // llvm.dbg.value in the correct places. This is done by the SILOptimizer
      // pass DebugInfoCanonicalizer.
      auto InsertBefore = Builder.GetInsertPoint();
      if (InsertBefore != ParentBlock->end()) {
        InsertPt = &*InsertBefore;
      } else {
        InsertPt = ParentBlock;
      }
    }

    // Ok, we now have our insert pt. Call the appropriate operations.
    assert(InsertPt);
    if (auto *InsertBefore = InsertPt.dyn_cast<llvm::Instruction *>()) {
      inserter.insert(Storage, Var, Expr, DL, InsertBefore);
    } else {
      inserter.insert(Storage, Var, Expr, DL,
                      InsertPt.get<llvm::BasicBlock *>());
    }
    return;
  }

  // Insert a dbg.value at the current insertion point.
  if (isa<llvm::Argument>(Storage) && !Var->getArg() &&
      ParentBlock->getFirstNonPHIOrDbg())
    // SelectionDAGISel only generates debug info for a dbg.value
    // that is associated with a llvm::Argument if either its !DIVariable
    // is marked as argument or there is no non-debug intrinsic instruction
    // before it. So In the case of associating a llvm::Argument with a
    // non-argument debug variable -- usually via a !DIExpression -- we
    // need to make sure that dbg.value is before any non-phi / no-dbg
    // instruction.
    DBuilder.insertDbgValueIntrinsic(Storage, Var, Expr, DL,
                                     ParentBlock->getFirstNonPHIOrDbg());
  else
    DBuilder.insertDbgValueIntrinsic(Storage, Var, Expr, DL, ParentBlock);
}

void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
    llvm::GlobalVariable *Var, StringRef Name, StringRef LinkageName,
    DebugTypeInfo DbgTy, bool IsLocalToUnit, std::optional<SILLocation> Loc) {
  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return;

  llvm::DIType *DITy = getOrCreateType(DbgTy);
  VarDecl *VD = nullptr;
  if (Loc)
    VD = dyn_cast_or_null<VarDecl>(Loc->getAsASTNode<Decl>());
  if (!VD || VD->isLet())
    DITy = DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_const_type, DITy);

  if (DITy->isArtificial() || DITy == InternalType || !Loc)
    // FIXME: Really these should be marked as artificial, but LLVM
    // currently has no support for flags to be put on global
    // variables. In the mean time, elide these variables, they
    // would confuse both the user and LLDB.
    return;

  if (DbgTy.isFixedBuffer())
    DITy = createFixedValueBufferStruct(DITy);

  auto L = getStartLocation(Loc);

  // Emit it as global variable of the current module.
  llvm::DIExpression *Expr = nullptr;
  if (!Var)
    Expr = DBuilder.createConstantValueExpression(0);
  auto *GV = DBuilder.createGlobalVariableExpression(
      MainModule, Name, LinkageName, L.File, L.Line, DITy, IsLocalToUnit, true,
      Expr);
  if (Var)
    Var->addDebugInfo(GV);
}

void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
                                          llvm::Value *Metadata, unsigned Depth,
                                          unsigned Index, StringRef Name) {
  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return;

  // Don't emit debug info in transparent functions.
  auto *DS = IGF.getDebugScope();
  if (!DS || DS->getInlinedFunction()->isTransparent())
    return;

  llvm::SmallString<8> Buf;
  static const char *Tau = SWIFT_UTF8("\u03C4");
  llvm::raw_svector_ostream OS(Buf);
  OS << '$' << Tau << '_' << Depth << '_' << Index;
  uint64_t PtrWidthInBits = CI.getTargetInfo().getPointerWidth(clang::LangAS::Default);
  assert(PtrWidthInBits % 8 == 0);
  auto DbgTy = DebugTypeInfo::getTypeMetadata(
      getMetadataType(Name)->getDeclaredInterfaceType().getPointer(),
      Metadata->getType(), Size(PtrWidthInBits / 8),
      Alignment(CI.getTargetInfo().getPointerAlign(clang::LangAS::Default)));
  emitVariableDeclaration(IGF.Builder, Metadata, DbgTy, IGF.getDebugScope(),
                          {}, {OS.str().str(), 0, false},
                          // swift.type is already a pointer type,
                          // having a shadow copy doesn't add another
                          // layer of indirection.
                          IGF.isAsync() ? CoroDirectValue : DirectValue,
                          ArtificialValue);
}

void IRGenDebugInfoImpl::emitPackCountParameter(IRGenFunction &IGF,
                                                llvm::Value *Metadata,
                                                SILDebugVariable VarInfo) {
  if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
    return;

  // Don't emit debug info in transparent functions.
  auto *DS = IGF.getDebugScope();
  if (!DS || DS->getInlinedFunction()->isTransparent())
    return;

  Type IntTy = BuiltinIntegerType::get(CI.getTargetInfo().getPointerWidth(clang::LangAS::Default),
                                       IGM.getSwiftModule()->getASTContext());
  auto &TI = IGM.getTypeInfoForUnlowered(IntTy);
  auto DbgTy = *CompletedDebugTypeInfo::getFromTypeInfo(IntTy, TI, IGM);
  emitVariableDeclaration(
      IGF.Builder, Metadata, DbgTy, IGF.getDebugScope(), {}, VarInfo,
      IGF.isAsync() ? CoroDirectValue : DirectValue, ArtificialValue);
}

} // anonymous namespace

std::unique_ptr<IRGenDebugInfo> IRGenDebugInfo::createIRGenDebugInfo(
    const IRGenOptions &Opts, ClangImporter &CI, IRGenModule &IGM,
    llvm::Module &M, StringRef MainOutputFilenameForDebugInfo,
    StringRef PrivateDiscriminator) {
  return std::make_unique<IRGenDebugInfoImpl>(
      Opts, CI, IGM, M, MainOutputFilenameForDebugInfo, PrivateDiscriminator);
}

IRGenDebugInfo::~IRGenDebugInfo() {}

// Forwarding to the private implementation.
void IRGenDebugInfo::finalize() {
  static_cast<IRGenDebugInfoImpl *>(this)->finalize();
}

void IRGenDebugInfo::setCurrentLoc(IRBuilder &Builder, const SILDebugScope *DS,
                                   SILLocation Loc) {
  static_cast<IRGenDebugInfoImpl *>(this)->setCurrentLoc(Builder, DS, Loc);
}

void IRGenDebugInfo::addFailureMessageToCurrentLoc(IRBuilder &Builder,
                                                   StringRef failureMsg) {
  static_cast<IRGenDebugInfoImpl *>(this)->addFailureMessageToCurrentLoc(
      Builder, failureMsg);
}

void IRGenDebugInfo::clearLoc(IRBuilder &Builder) {
  static_cast<IRGenDebugInfoImpl *>(this)->clearLoc(Builder);
}

void IRGenDebugInfo::pushLoc() {
  static_cast<IRGenDebugInfoImpl *>(this)->pushLoc();
}

void IRGenDebugInfo::popLoc() {
  static_cast<IRGenDebugInfoImpl *>(this)->popLoc();
}

void IRGenDebugInfo::setInlinedTrapLocation(IRBuilder &Builder,
                                            const SILDebugScope *Scope) {
  static_cast<IRGenDebugInfoImpl *>(this)->setInlinedTrapLocation(Builder,
                                                                  Scope);
}

void IRGenDebugInfo::setEntryPointLoc(IRBuilder &Builder) {
  static_cast<IRGenDebugInfoImpl *>(this)->setEntryPointLoc(Builder);
}

llvm::DIScope *IRGenDebugInfo::getEntryPointFn() {
  return static_cast<IRGenDebugInfoImpl *>(this)->getEntryPointFn();
}

llvm::DIScope *IRGenDebugInfo::getOrCreateScope(const SILDebugScope *DS) {
  return static_cast<IRGenDebugInfoImpl *>(this)->getOrCreateScope(DS);
}

void IRGenDebugInfo::emitImport(ImportDecl *D) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitImport(D);
}

llvm::DISubprogram *
IRGenDebugInfo::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
                             SILFunctionTypeRepresentation Rep, SILType Ty,
                             DeclContext *DeclCtx, GenericEnvironment *GE) {
  return static_cast<IRGenDebugInfoImpl *>(this)->emitFunction(DS, Fn, Rep, Ty,
                                                               DeclCtx);
}

llvm::DISubprogram *IRGenDebugInfo::emitFunction(SILFunction &SILFn,
                                                 llvm::Function *Fn) {
  return static_cast<IRGenDebugInfoImpl *>(this)->emitFunction(SILFn, Fn);
}

void IRGenDebugInfo::emitArtificialFunction(IRBuilder &Builder,
                                            llvm::Function *Fn, SILType SILTy) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitArtificialFunction(Builder, Fn,
                                                                  SILTy);
}
void IRGenDebugInfo::emitOutlinedFunction(IRBuilder &Builder,
                                          llvm::Function *Fn, StringRef name) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitOutlinedFunction(Builder, Fn,
                                                                name);
}
void IRGenDebugInfo::emitVariableDeclaration(
    IRBuilder &Builder, ArrayRef<llvm::Value *> Storage, DebugTypeInfo Ty,
    const SILDebugScope *DS, std::optional<SILLocation> VarLoc,
    SILDebugVariable VarInfo, IndirectionKind Indirection,
    ArtificialKind Artificial, AddrDbgInstrKind AddrDInstKind) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitVariableDeclaration(
      Builder, Storage, Ty, DS, VarLoc, VarInfo, Indirection, Artificial,
      AddrDInstKind);
}

void IRGenDebugInfo::emitDbgIntrinsic(IRBuilder &Builder, llvm::Value *Storage,
                                      llvm::DILocalVariable *Var,
                                      llvm::DIExpression *Expr, unsigned Line,
                                      unsigned Col, llvm::DILocalScope *Scope,
                                      const SILDebugScope *DS,
                                      bool InCoroContext,
                                      AddrDbgInstrKind AddrDInstKind) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitDbgIntrinsic(
      Builder, Storage, Var, Expr, Line, Col, Scope, DS, InCoroContext,
      AddrDInstKind);
}

void IRGenDebugInfo::emitGlobalVariableDeclaration(
    llvm::GlobalVariable *Storage, StringRef Name, StringRef LinkageName,
    DebugTypeInfo DebugType, bool IsLocalToUnit,
    std::optional<SILLocation> Loc) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitGlobalVariableDeclaration(
      Storage, Name, LinkageName, DebugType, IsLocalToUnit, Loc);
}

void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
                                      unsigned Depth, unsigned Index,
                                      StringRef Name) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitTypeMetadata(IGF, Metadata,
                                                            Depth, Index, Name);
}

void IRGenDebugInfo::emitPackCountParameter(IRGenFunction &IGF,
                                            llvm::Value *Metadata,
                                            SILDebugVariable VarInfo) {
  static_cast<IRGenDebugInfoImpl *>(this)->emitPackCountParameter(IGF, Metadata,
                                                                  VarInfo);
}

llvm::DIBuilder &IRGenDebugInfo::getBuilder() {
  return static_cast<IRGenDebugInfoImpl *>(this)->getBuilder();
}

AutoRestoreLocation::AutoRestoreLocation(IRGenDebugInfo *DI, IRBuilder &Builder)
    : DI(DI), Builder(Builder) {
  if (DI)
    SavedLocation = Builder.getCurrentDebugLocation();
}

/// Autorestore everything back to normal.
AutoRestoreLocation::~AutoRestoreLocation() {
  if (DI)
    Builder.SetCurrentDebugLocation(SavedLocation);
}

ArtificialLocation::ArtificialLocation(const SILDebugScope *DS,
                                       IRGenDebugInfo *DI, IRBuilder &Builder)
    : AutoRestoreLocation(DI, Builder) {
  if (DI) {
    unsigned Line = 0;
    auto *Scope = DI->getOrCreateScope(DS);
    auto DII = static_cast<IRGenDebugInfoImpl *>(DI);
    if (DII->getDebugInfoFormat() == IRGenDebugInfoFormat::CodeView) {
      // In CodeView, line zero is not an artificial line location and so we
      // try to use the location of the scope.
      if (auto *LB = dyn_cast<llvm::DILexicalBlock>(Scope))
        Line = LB->getLine();
      else if (auto *SP = dyn_cast<llvm::DISubprogram>(Scope))
        Line = SP->getLine();
    }
    auto DL = llvm::DILocation::get(Scope->getContext(), Line, 0, Scope,
                                    DII->createInlinedAt(DS));
    Builder.SetCurrentDebugLocation(DL);
  }
}

PrologueLocation::PrologueLocation(IRGenDebugInfo *DI, IRBuilder &Builder)
    : AutoRestoreLocation(DI, Builder) {
  if (DI)
    DI->clearLoc(Builder);
}