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 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932
|
//===- SPIRVWriter.cpp - Converts LLVM to SPIR-V ----------------*- C++ -*-===//
//
// The LLVM/SPIR-V Translator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
// Neither the names of Advanced Micro Devices, Inc., nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file implements conversion of LLVM intermediate language to SPIR-V
/// binary.
///
//===----------------------------------------------------------------------===//
#include "SPIRVWriter.h"
#include "LLVMToSPIRVDbgTran.h"
#include "OCLToSPIRV.h"
#include "PreprocessMetadata.h"
#include "SPIRVAsm.h"
#include "SPIRVBasicBlock.h"
#include "SPIRVEntry.h"
#include "SPIRVEnum.h"
#include "SPIRVExtInst.h"
#include "SPIRVFunction.h"
#include "SPIRVInstruction.h"
#include "SPIRVInternal.h"
#include "SPIRVLLVMUtil.h"
#include "SPIRVLowerBitCastToNonStandardType.h"
#include "SPIRVLowerBool.h"
#include "SPIRVLowerConstExpr.h"
#include "SPIRVLowerLLVMIntrinsic.h"
#include "SPIRVLowerMemmove.h"
#include "SPIRVLowerOCLBlocks.h"
#include "SPIRVMDWalker.h"
#include "SPIRVMemAliasingINTEL.h"
#include "SPIRVModule.h"
#include "SPIRVRegularizeLLVM.h"
#include "SPIRVType.h"
#include "SPIRVUtil.h"
#include "SPIRVValue.h"
#include "VectorComputeUtil.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/TypedPointerType.h"
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/LoopSimplify.h"
#include "llvm/Transforms/Utils/Mem2Reg.h"
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <queue>
#include <regex>
#include <set>
#include <vector>
#define DEBUG_TYPE "spirv"
using namespace llvm;
using namespace SPIRV;
using namespace OCLUtil;
namespace {
static SPIRVWord convertFloatToSPIRVWord(float F) {
union {
float F;
SPIRVWord Spir;
} FPMaxError;
FPMaxError.F = F;
return FPMaxError.Spir;
}
/// Return one of the SPIR-V 1.4 SignExtend or ZeroExtend image operands
/// for a function name, or 0 if the function does not return or
/// write an integer type.
int getImageSignZeroExt(Function *F) {
bool IsSigned = false;
bool IsUnsigned = false;
ParamSignedness RetSignedness;
SmallVector<ParamSignedness, 4> ArgSignedness;
if (!getRetParamSignedness(F, RetSignedness, ArgSignedness))
return 0;
StringRef Name = F->getName();
Name = Name.substr(Name.find(kSPIRVName::Prefix));
Name.consume_front(kSPIRVName::Prefix);
if (Name.consume_front("ImageRead") ||
Name.consume_front("ImageSampleExplicitLod")) {
if (RetSignedness == ParamSignedness::Signed)
IsSigned = true;
else if (RetSignedness == ParamSignedness::Unsigned)
IsUnsigned = true;
else if (F->getReturnType()->isIntOrIntVectorTy() &&
Name.consume_front("_R")) {
// Return type is mangled after _R, e.g. _Z23__spirv_ImageRead_Rint2li
IsSigned = isMangledTypeSigned(Name[0]);
IsUnsigned = Name.starts_with("u");
}
} else if (Name.starts_with("ImageWrite")) {
IsSigned = (ArgSignedness[2] == ParamSignedness::Signed);
IsUnsigned = (ArgSignedness[2] == ParamSignedness::Unsigned);
}
if (IsSigned)
return ImageOperandsMask::ImageOperandsSignExtendMask;
if (IsUnsigned)
return ImageOperandsMask::ImageOperandsZeroExtendMask;
return 0;
}
} // namespace
namespace SPIRV {
static void foreachKernelArgMD(
MDNode *MD, SPIRVFunction *BF,
std::function<void(const std::string &Str, SPIRVFunctionParameter *BA)>
Func) {
assert(BF->getNumArguments() == MD->getNumOperands() &&
"Invalid kernel metadata: Number of metadata operands and kernel "
"arguments do not match");
for (unsigned I = 0, E = MD->getNumOperands(); I != E; ++I) {
SPIRVFunctionParameter *BA = BF->getArgument(I);
Func(getMDOperandAsString(MD, I).str(), BA);
}
}
static void foreachKernelArgMD(
MDNode *MD, SPIRVFunction *BF,
std::function<void(Metadata *MDOp, SPIRVFunctionParameter *BA)> Func) {
assert(BF->getNumArguments() == MD->getNumOperands() &&
"Invalid kernel metadata: Number of metadata operands and kernel "
"arguments do not match");
for (unsigned I = 0, E = MD->getNumOperands(); I != E; ++I) {
SPIRVFunctionParameter *BA = BF->getArgument(I);
Func(MD->getOperand(I), BA);
}
}
static SPIRVMemoryModelKind getMemoryModel(Module &M) {
auto *MemoryModelMD = M.getNamedMetadata(kSPIRVMD::MemoryModel);
if (MemoryModelMD && (MemoryModelMD->getNumOperands() > 0)) {
auto *Ref0 = MemoryModelMD->getOperand(0);
if (Ref0 && Ref0->getNumOperands() > 1) {
auto &&ModelOp = Ref0->getOperand(1);
auto *ModelCI = mdconst::dyn_extract<ConstantInt>(ModelOp);
if (ModelCI && (ModelCI->getValue().getActiveBits() <= 64)) {
auto Model = static_cast<SPIRVMemoryModelKind>(ModelCI->getZExtValue());
return Model;
}
}
}
return SPIRVMemoryModelKind::MemoryModelMax;
}
static void translateSEVDecoration(Attribute Sev, SPIRVValue *Val) {
assert(Sev.isStringAttribute() &&
Sev.getKindAsString() == kVCMetadata::VCSingleElementVector);
auto *Ty = Val->getType();
assert((Ty->isTypeBool() || Ty->isTypeFloat() || Ty->isTypeInt() ||
Ty->isTypePointer()) &&
"This decoration is valid only for Scalar or Pointer types");
if (Ty->isTypePointer()) {
SPIRVWord IndirectLevelsOnElement = 0;
Sev.getValueAsString().getAsInteger(0, IndirectLevelsOnElement);
Val->addDecorate(DecorationSingleElementVectorINTEL,
IndirectLevelsOnElement);
} else
Val->addDecorate(DecorationSingleElementVectorINTEL);
}
LLVMToSPIRVBase::LLVMToSPIRVBase(SPIRVModule *SMod)
: BuiltinCallHelper(ManglingRules::None), M(nullptr), Ctx(nullptr),
BM(SMod), SrcLang(0), SrcLangVer(0) {
DbgTran = std::make_unique<LLVMToSPIRVDbgTran>(nullptr, SMod, this);
}
LLVMToSPIRVBase::~LLVMToSPIRVBase() {
for (auto *I : UnboundInst)
I->deleteValue();
}
bool LLVMToSPIRVBase::runLLVMToSPIRV(Module &Mod) {
M = &Mod;
initialize(Mod);
CG = std::make_unique<CallGraph>(Mod);
Ctx = &M->getContext();
DbgTran->setModule(M);
assert(BM && "SPIR-V module not initialized");
translate();
return true;
}
SPIRVValue *LLVMToSPIRVBase::getTranslatedValue(const Value *V) const {
auto Loc = ValueMap.find(V);
if (Loc != ValueMap.end())
return Loc->second;
return nullptr;
}
bool LLVMToSPIRVBase::isKernel(Function *F) {
if (F->getCallingConv() == CallingConv::SPIR_KERNEL)
return true;
return false;
}
bool LLVMToSPIRVBase::isBuiltinTransToInst(Function *F) {
StringRef DemangledName;
if (!oclIsBuiltin(F->getName(), DemangledName) &&
!isDecoratedSPIRVFunc(F, DemangledName))
return false;
SPIRVDBG(spvdbgs() << "CallInst: demangled name: " << DemangledName.str()
<< '\n');
return getSPIRVFuncOC(DemangledName) != OpNop;
}
bool LLVMToSPIRVBase::isBuiltinTransToExtInst(
Function *F, SPIRVExtInstSetKind *ExtSet, SPIRVWord *ExtOp,
SmallVectorImpl<std::string> *Dec) {
StringRef DemangledName;
if (!oclIsBuiltin(F->getName(), DemangledName))
return false;
LLVM_DEBUG(dbgs() << "[oclIsBuiltinTransToExtInst] CallInst: demangled name: "
<< DemangledName << '\n');
StringRef S = DemangledName;
if (!S.starts_with(kSPIRVName::Prefix))
return false;
S = S.drop_front(strlen(kSPIRVName::Prefix));
auto Loc = S.find(kSPIRVPostfix::Divider);
auto ExtSetName = S.substr(0, Loc);
SPIRVExtInstSetKind Set = SPIRVEIS_Count;
if (!SPIRVExtSetShortNameMap::rfind(ExtSetName.str(), &Set))
return false;
assert((Set == SPIRVEIS_OpenCL || Set == BM->getDebugInfoEIS()) &&
"Unsupported extended instruction set");
auto ExtOpName = S.substr(Loc + 1);
auto Splited = ExtOpName.split(kSPIRVPostfix::ExtDivider);
OCLExtOpKind EOC;
if (!OCLExtOpMap::rfind(Splited.first.str(), &EOC))
return false;
if (ExtSet)
*ExtSet = Set;
if (ExtOp)
*ExtOp = EOC;
if (Dec) {
SmallVector<StringRef, 2> P;
Splited.second.split(P, kSPIRVPostfix::Divider);
for (auto &I : P)
Dec->push_back(I.str());
}
return true;
}
bool isUniformGroupOperation(Function *F) {
auto Name = F->getName();
if (Name.contains("GroupIMulKHR") || Name.contains("GroupFMulKHR") ||
Name.contains("GroupBitwiseAndKHR") ||
Name.contains("GroupBitwiseOrKHR") ||
Name.contains("GroupBitwiseXorKHR") ||
Name.contains("GroupLogicalAndKHR") ||
Name.contains("GroupLogicalOrKHR") || Name.contains("GroupLogicalXorKHR"))
return true;
return false;
}
static bool recursiveType(const StructType *ST, const Type *Ty) {
SmallPtrSet<const StructType *, 4> Seen;
std::function<bool(const Type *Ty)> Run = [&](const Type *Ty) {
if (auto *StructTy = dyn_cast<StructType>(Ty)) {
if (StructTy == ST)
return true;
if (Seen.count(StructTy))
return false;
Seen.insert(StructTy);
return find_if(StructTy->element_begin(), StructTy->element_end(), Run) !=
StructTy->element_end();
}
if (auto *ArrayTy = dyn_cast<ArrayType>(Ty))
return Run(ArrayTy->getArrayElementType());
return false;
};
return Run(Ty);
}
// Add decoration if needed
void addFPBuiltinDecoration(SPIRVModule *BM, Instruction *Inst,
SPIRVInstruction *I) {
bool AllowFPMaxError =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fp_max_error);
auto *II = dyn_cast_or_null<IntrinsicInst>(Inst);
if (II && II->getCalledFunction()->getName().starts_with("llvm.fpbuiltin")) {
// Add a new decoration for llvm.builtin intrinsics, if needed
if (II->getAttributes().hasFnAttr("fpbuiltin-max-error")) {
BM->getErrorLog().checkError(AllowFPMaxError, SPIRVEC_RequiresExtension,
"SPV_INTEL_fp_max_error\n");
double F = 0.0;
II->getAttributes()
.getFnAttr("fpbuiltin-max-error")
.getValueAsString()
.getAsDouble(F);
I->addDecorate(DecorationFPMaxErrorDecorationINTEL,
convertFloatToSPIRVWord(F));
}
} else if (auto *MD = Inst->getMetadata("fpmath")) {
if (!AllowFPMaxError)
return;
auto *MDVal = mdconst::dyn_extract<ConstantFP>(MD->getOperand(0));
double ValAsDouble = MDVal->getValue().convertToFloat();
I->addDecorate(DecorationFPMaxErrorDecorationINTEL,
convertFloatToSPIRVWord(ValAsDouble));
}
}
SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
LLVMToSPIRVTypeMap::iterator Loc = TypeMap.find(T);
if (Loc != TypeMap.end())
return Loc->second;
SPIRVDBG(dbgs() << "[transType] " << *T << '\n');
if (T->isVoidTy())
return mapType(T, BM->addVoidType());
if (T->isIntegerTy(1))
return mapType(T, BM->addBoolType());
if (T->isIntegerTy()) {
unsigned BitWidth = T->getIntegerBitWidth();
// SPIR-V 2.16.1. Universal Validation Rules: Scalar integer types can be
// parameterized only as 32 bit, plus any additional sizes enabled by
// capabilities.
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_arbitrary_precision_integers) ||
BM->getErrorLog().checkError(
BitWidth == 8 || BitWidth == 16 || BitWidth == 32 || BitWidth == 64,
SPIRVEC_InvalidBitWidth, std::to_string(BitWidth))) {
return mapType(T, BM->addIntegerType(T->getIntegerBitWidth()));
}
}
if (T->isBFloatTy()) {
BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bfloat16),
SPIRVEC_RequiresExtension,
"SPV_KHR_bfloat16\n"
"NOTE: LLVM module contains bfloat type, translation of which "
"requires this extension");
return mapType(T, BM->addFloatType(16, FPEncodingBFloat16KHR));
}
if (T->isFloatingPointTy())
return mapType(T, BM->addFloatType(T->getPrimitiveSizeInBits()));
// A pointer to image or pipe type in LLVM is translated to a SPIRV
// (non-pointer) image or pipe type.
if (T->isPointerTy()) {
auto *ET = Type::getInt8Ty(T->getContext());
auto AddrSpc = T->getPointerAddressSpace();
return transPointerType(ET, AddrSpc);
}
if (auto *TPT = dyn_cast<TypedPointerType>(T)) {
return transPointerType(TPT->getElementType(), TPT->getAddressSpace());
}
if (auto *VecTy = dyn_cast<FixedVectorType>(T)) {
if (VecTy->getElementType()->isPointerTy() ||
isa<TypedPointerType>(VecTy->getElementType())) {
// SPV_INTEL_masked_gather_scatter extension changes 2.16.1. Universal
// Validation Rules:
// Vector types must be parameterized only with numerical types,
// [Physical Pointer Type] types or the [OpTypeBool] type.
// Without it vector of pointers is not allowed in SPIR-V.
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_masked_gather_scatter)) {
BM->getErrorLog().checkError(
false, SPIRVEC_RequiresExtension,
"SPV_INTEL_masked_gather_scatter\n"
"NOTE: LLVM module contains vector of pointers, translation "
"of which requires this extension");
return nullptr;
}
BM->addExtension(ExtensionID::SPV_INTEL_masked_gather_scatter);
BM->addCapability(internal::CapabilityMaskedGatherScatterINTEL);
}
return mapType(T, BM->addVectorType(transType(VecTy->getElementType()),
VecTy->getNumElements()));
}
if (T->isArrayTy()) {
// SPIR-V 1.3 s3.32.6: Length is the number of elements in the array.
// It must be at least 1.
if (T->getArrayNumElements() < 1) {
std::string Str;
llvm::raw_string_ostream OS(Str);
OS << *T;
SPIRVCK(T->getArrayNumElements() >= 1, InvalidArraySize, OS.str());
}
Type *ElTy = T->getArrayElementType();
SPIRVType *TransType = BM->addArrayType(
transType(ElTy),
static_cast<SPIRVConstant *>(transValue(
ConstantInt::get(getSizetType(), T->getArrayNumElements(), false),
nullptr)));
mapType(T, TransType);
if (ElTy->isPointerTy()) {
mapType(
ArrayType::get(TypedPointerType::get(Type::getInt8Ty(*Ctx),
ElTy->getPointerAddressSpace()),
T->getArrayNumElements()),
TransType);
}
return TransType;
}
if (T->isStructTy() && !T->isSized()) {
auto *ST = dyn_cast<StructType>(T);
(void)ST; // Silence warning
assert(!ST->getName().starts_with(kSPR2TypeName::PipeRO));
assert(!ST->getName().starts_with(kSPR2TypeName::PipeWO));
assert(!ST->getName().starts_with(kSPR2TypeName::ImagePrefix));
return mapType(T, BM->addOpaqueType(T->getStructName().str()));
}
if (auto *ST = dyn_cast<StructType>(T)) {
assert(ST->isSized());
StringRef Name;
if (ST->hasName())
Name = ST->getName();
if (Name == getSPIRVTypeName(kSPIRVTypeName::ConstantSampler))
return transSPIRVOpaqueType("spirv.Sampler", SPIRAS_Constant);
if (Name == getSPIRVTypeName(kSPIRVTypeName::ConstantPipeStorage))
return transSPIRVOpaqueType("spirv.PipeStorage", SPIRAS_Constant);
constexpr size_t MaxNumElements = MaxWordCount - SPIRVTypeStruct::FixedWC;
const size_t NumElements = ST->getNumElements();
size_t SPIRVStructNumElements = NumElements;
// In case number of elements is greater than maximum WordCount and
// SPV_INTEL_long_constant_composite is not enabled, the error will be
// emitted by validate functionality of SPIRVTypeStruct class.
if (NumElements > MaxNumElements &&
BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_long_constant_composite)) {
SPIRVStructNumElements = MaxNumElements;
}
auto *Struct = BM->openStructType(SPIRVStructNumElements, Name.str());
mapType(T, Struct);
if (NumElements > MaxNumElements &&
BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_long_constant_composite)) {
uint64_t NumOfContinuedInstructions = NumElements / MaxNumElements - 1;
for (uint64_t J = 0; J < NumOfContinuedInstructions; J++) {
auto *Continued = BM->addTypeStructContinuedINTEL(MaxNumElements);
Struct->addContinuedInstruction(
static_cast<SPIRVTypeStruct::ContinuedInstType>(Continued));
}
uint64_t Remains = NumElements % MaxNumElements;
if (Remains) {
auto *Continued = BM->addTypeStructContinuedINTEL(Remains);
Struct->addContinuedInstruction(
static_cast<SPIRVTypeStruct::ContinuedInstType>(Continued));
}
}
SmallVector<unsigned, 4> ForwardRefs;
for (unsigned I = 0, E = T->getStructNumElements(); I != E; ++I) {
auto *ElemTy = ST->getElementType(I);
if ((isa<StructType>(ElemTy) || isa<ArrayType>(ElemTy) ||
isa<VectorType>(ElemTy) || isa<PointerType>(ElemTy)) &&
recursiveType(ST, ElemTy))
ForwardRefs.push_back(I);
else
Struct->setMemberType(I, transType(ST->getElementType(I)));
}
BM->closeStructType(Struct, ST->isPacked());
for (auto I : ForwardRefs)
Struct->setMemberType(I, transType(ST->getElementType(I)));
return Struct;
}
if (FunctionType *FT = dyn_cast<FunctionType>(T)) {
SPIRVType *RT = transType(FT->getReturnType());
std::vector<SPIRVType *> PT;
for (FunctionType::param_iterator I = FT->param_begin(),
E = FT->param_end();
I != E; ++I)
PT.push_back(transType(*I));
return mapType(T, getSPIRVFunctionType(RT, PT));
}
if (auto *TargetTy = dyn_cast<TargetExtType>(T)) {
StringRef Name = TargetTy->getName();
if (Name.consume_front(kSPIRVTypeName::PrefixAndDelim)) {
auto Opcode = SPIRVOpaqueTypeOpCodeMap::map(Name.str());
auto CastAccess = [](unsigned Val) {
return static_cast<SPIRVAccessQualifierKind>(Val);
};
switch (static_cast<size_t>(Opcode)) {
case OpTypePipe: {
auto *PipeT = BM->addPipeType();
PipeT->setPipeAcessQualifier(CastAccess(TargetTy->getIntParameter(0)));
return mapType(T, PipeT);
}
case OpTypeImage: {
auto *SampledTy = transType(TargetTy->getTypeParameter(0));
ArrayRef<unsigned> Ops = TargetTy->int_params();
SPIRVTypeImageDescriptor Desc(static_cast<SPIRVImageDimKind>(Ops[0]),
Ops[1], Ops[2], Ops[3], Ops[4], Ops[5]);
return mapType(T,
BM->addImageType(SampledTy, Desc, CastAccess(Ops[6])));
}
case OpTypeSampledImage: {
auto *ImageTy = static_cast<SPIRVTypeImage *>(transType(adjustImageType(
T, kSPIRVTypeName::SampledImg, kSPIRVTypeName::Image)));
return mapType(T, BM->addSampledImageType(ImageTy));
}
case OpTypeVmeImageINTEL: {
auto *ImageTy = static_cast<SPIRVTypeImage *>(transType(adjustImageType(
T, kSPIRVTypeName::VmeImageINTEL, kSPIRVTypeName::Image)));
return mapType(T, BM->addVmeImageINTELType(ImageTy));
}
case OpTypeQueue:
return mapType(T, BM->addQueueType());
case OpTypeDeviceEvent:
return mapType(T, BM->addDeviceEventType());
case OpTypeBufferSurfaceINTEL: {
ArrayRef<unsigned> Ops = TargetTy->int_params();
return mapType(T, BM->addBufferSurfaceINTELType(CastAccess(Ops[0])));
}
case internal::OpTypeJointMatrixINTEL: {
// The expected representation is:
// target("spirv.JointMatrixINTEL", %element_type, %rows%, %cols%,
// %layout%, %scope%, %use%,
// (optional) %element_type_interpretation%)
auto *ElemTy = transType(TargetTy->getTypeParameter(0));
ArrayRef<unsigned> Ops = TargetTy->int_params();
std::vector<SPIRVValue *> Args;
for (const auto &Op : Ops)
Args.emplace_back(transConstant(getUInt32(M, Op)));
return mapType(T, BM->addJointMatrixINTELType(ElemTy, Args));
}
case OpTypeCooperativeMatrixKHR: {
// The expected representation is:
// target("spirv.CooperativeMatrixKHR", %element_type, %scope%, %rows%,
// %cols%, %use%)
auto *ElemTy = transType(TargetTy->getTypeParameter(0));
ArrayRef<unsigned> Ops = TargetTy->int_params();
std::vector<SPIRVValue *> Args;
for (const auto &Op : Ops)
Args.emplace_back(transConstant(getUInt32(M, Op)));
return mapType(T, BM->addCooperativeMatrixKHRType(ElemTy, Args));
}
case internal::OpTypeTaskSequenceINTEL:
return mapType(T, BM->addTaskSequenceINTELType());
default:
if (isSubgroupAvcINTELTypeOpCode(Opcode))
return mapType(T, BM->addSubgroupAvcINTELType(Opcode));
return mapType(T, BM->addOpaqueGenericType(Opcode));
}
}
}
if (T->isTokenTy()) {
BM->getErrorLog().checkError(
false, SPIRVEC_InvalidModule,
"LLVM module contains token type, which doesn't have a counterpart in "
"SPIR-V");
return nullptr;
}
llvm_unreachable("Not implemented!");
return 0;
}
SPIRVType *LLVMToSPIRVBase::transPointerType(Type *ET, unsigned AddrSpc) {
Type *T = PointerType::get(ET, AddrSpc);
if (ET->isFunctionTy() &&
!BM->checkExtension(ExtensionID::SPV_INTEL_function_pointers,
SPIRVEC_FunctionPointers, toString(T)))
return nullptr;
std::string TypeKey = (Twine((uintptr_t)ET) + Twine(AddrSpc)).str();
auto Loc = PointeeTypeMap.find(TypeKey);
if (Loc != PointeeTypeMap.end())
return Loc->second;
// A pointer to image or pipe type in LLVM is translated to a SPIRV
// (non-pointer) image or pipe type.
auto *ST = dyn_cast<StructType>(ET);
// Lower global_device and global_host address spaces that were added in
// SYCL as part of SYCL_INTEL_usm_address_spaces extension to just global
// address space if device doesn't support SPV_INTEL_usm_storage_classes
// extension
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes) &&
((AddrSpc == SPIRAS_GlobalDevice) || (AddrSpc == SPIRAS_GlobalHost))) {
return transPointerType(ET, SPIRAS_Global);
}
// Lower function pointer address space to private if
// spirv-emit-function-ptr-addr-space is not passed
if (AddrSpc == SPIRAS_CodeSectionINTEL &&
!BM->shouldEmitFunctionPtrAddrSpace())
return transPointerType(ET, SPIRAS_Private);
if (ST && !ST->isSized()) {
Op OpCode;
StringRef STName = ST->getName();
// Workaround for non-conformant SPIR binary
if (STName == "struct._event_t") {
STName = kSPR2TypeName::Event;
ST->setName(STName);
}
std::pair<StringRef, unsigned> Key = {STName, AddrSpc};
if (auto *MappedTy = OpaqueStructMap.lookup(Key))
return MappedTy;
auto SaveType = [&](SPIRVType *MappedTy) {
OpaqueStructMap[Key] = MappedTy;
PointeeTypeMap[TypeKey] = MappedTy;
return MappedTy;
};
if (STName.starts_with(kSPR2TypeName::PipeRO) ||
STName.starts_with(kSPR2TypeName::PipeWO)) {
auto *PipeT = BM->addPipeType();
PipeT->setPipeAcessQualifier(STName.starts_with(kSPR2TypeName::PipeRO)
? AccessQualifierReadOnly
: AccessQualifierWriteOnly);
return SaveType(PipeT);
}
if (STName.starts_with(kSPR2TypeName::ImagePrefix)) {
assert(AddrSpc == SPIRAS_Global);
Type *ImageTy =
adjustImageType(TypedPointerType::get(ST, AddrSpc),
kSPIRVTypeName::Image, kSPIRVTypeName::Image);
return SaveType(transType(ImageTy));
}
if (STName == kSPR2TypeName::Sampler)
return SaveType(transType(getSPIRVType(OpTypeSampler)));
if (STName.starts_with(kSPIRVTypeName::PrefixAndDelim))
return transSPIRVOpaqueType(STName, AddrSpc);
if (STName.starts_with(kOCLSubgroupsAVCIntel::TypePrefix))
return SaveType(BM->addSubgroupAvcINTELType(
OCLSubgroupINTELTypeOpCodeMap::map(ST->getName().str())));
if (OCLOpaqueTypeOpCodeMap::find(STName.str(), &OpCode)) {
Type *RealType = getSPIRVType(OpCode);
return SaveType(transType(RealType));
}
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute)) {
if (STName.starts_with(kVCType::VCBufferSurface)) {
// VCBufferSurface always have Access Qualifier
auto Access = getAccessQualifier(STName);
return SaveType(BM->addBufferSurfaceINTELType(Access));
}
}
if (ST->isOpaque()) {
return SaveType(BM->addPointerType(
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)),
transType(ET)));
}
} else {
SPIRVType *ElementType = transType(ET);
// ET, as a recursive type, may contain exactly the same pointer T, so it
// may happen that after translation of ET we already have translated T,
// added the translated pointer to the SPIR-V module and mapped T to this
// pointer. Now we have to check PointeeTypeMap again.
auto Loc = PointeeTypeMap.find(TypeKey);
if (Loc != PointeeTypeMap.end()) {
return Loc->second;
}
SPIRVType *TranslatedTy = transPointerType(ElementType, AddrSpc);
PointeeTypeMap[TypeKey] = TranslatedTy;
return TranslatedTy;
}
llvm_unreachable("Not implemented!");
return nullptr;
}
SPIRVType *LLVMToSPIRVBase::transPointerType(SPIRVType *ET, unsigned AddrSpc) {
std::string TypeKey = (Twine((uintptr_t)ET) + Twine(AddrSpc)).str();
auto Loc = PointeeTypeMap.find(TypeKey);
if (Loc != PointeeTypeMap.end())
return Loc->second;
if (AddrSpc == SPIRAS_CodeSectionINTEL &&
!BM->shouldEmitFunctionPtrAddrSpace())
return transPointerType(ET, SPIRAS_Private);
SPIRVType *TranslatedTy = BM->addPointerType(
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)), ET);
PointeeTypeMap[TypeKey] = TranslatedTy;
return TranslatedTy;
}
SPIRVType *LLVMToSPIRVBase::transSPIRVOpaqueType(StringRef STName,
unsigned AddrSpace) {
std::pair<StringRef, unsigned> Key = {STName, AddrSpace};
if (auto *MappedTy = OpaqueStructMap.lookup(Key))
return MappedTy;
auto SaveType = [&](SPIRVType *MappedTy) {
OpaqueStructMap[Key] = MappedTy;
return MappedTy;
};
StructType *ST = StructType::getTypeByName(M->getContext(), STName);
assert(STName.starts_with(kSPIRVTypeName::PrefixAndDelim) &&
"Invalid SPIR-V opaque type name");
SmallVector<std::string, 8> Postfixes;
auto TN = decodeSPIRVTypeName(STName, Postfixes);
if (TN == kSPIRVTypeName::Pipe) {
assert(AddrSpace == SPIRAS_Global);
assert(Postfixes.size() == 1 && "Invalid pipe type ops");
auto *PipeT = BM->addPipeType();
PipeT->setPipeAcessQualifier(
static_cast<spv::AccessQualifier>(atoi(Postfixes[0].c_str())));
return SaveType(PipeT);
} else if (TN == kSPIRVTypeName::Image) {
assert(AddrSpace == SPIRAS_Global);
// The sampled type needs to be translated through LLVM type to guarantee
// uniqueness.
auto *SampledT = transType(
getLLVMTypeForSPIRVImageSampledTypePostfix(Postfixes[0], *Ctx));
SmallVector<int, 7> Ops;
for (unsigned I = 1; I < 8; ++I)
Ops.push_back(atoi(Postfixes[I].c_str()));
SPIRVTypeImageDescriptor Desc(static_cast<SPIRVImageDimKind>(Ops[0]),
Ops[1], Ops[2], Ops[3], Ops[4], Ops[5]);
return SaveType(BM->addImageType(
SampledT, Desc, static_cast<spv::AccessQualifier>(Ops[6])));
} else if (TN == kSPIRVTypeName::SampledImg) {
return SaveType(BM->addSampledImageType(static_cast<SPIRVTypeImage *>(
transType(adjustImageType(TypedPointerType::get(ST, SPIRAS_Global),
kSPIRVTypeName::SampledImg,
kSPIRVTypeName::Image)))));
} else if (TN == kSPIRVTypeName::VmeImageINTEL) {
// This type is the same as SampledImageType, but consumed by Subgroup AVC
// Intel extension instructions.
return SaveType(BM->addVmeImageINTELType(static_cast<SPIRVTypeImage *>(
transType(adjustImageType(TypedPointerType::get(ST, SPIRAS_Global),
kSPIRVTypeName::VmeImageINTEL,
kSPIRVTypeName::Image)))));
} else if (TN == kSPIRVTypeName::Sampler)
return SaveType(BM->addSamplerType());
else if (TN == kSPIRVTypeName::DeviceEvent)
return SaveType(BM->addDeviceEventType());
else if (TN == kSPIRVTypeName::Queue)
return SaveType(BM->addQueueType());
else if (TN == kSPIRVTypeName::PipeStorage)
return SaveType(BM->addPipeStorageType());
else if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute) &&
TN == kSPIRVTypeName::BufferSurfaceINTEL) {
auto Access = getAccessQualifier(STName);
return SaveType(BM->addBufferSurfaceINTELType(Access));
} else
return SaveType(
BM->addOpaqueGenericType(SPIRVOpaqueTypeOpCodeMap::map(TN)));
}
SPIRVType *LLVMToSPIRVBase::transScavengedType(Value *V) {
if (auto *F = dyn_cast<Function>(V)) {
FunctionType *FnTy = Scavenger->getFunctionType(F);
SPIRVType *RT = transType(FnTy->getReturnType());
std::vector<SPIRVType *> PT;
for (Argument &Arg : F->args()) {
assert(OCLTypeToSPIRVPtr);
Type *Ty = OCLTypeToSPIRVPtr->getAdaptedArgumentType(F, Arg.getArgNo());
if (!Ty) {
Ty = FnTy->getParamType(Arg.getArgNo());
}
PT.push_back(transType(Ty));
}
return getSPIRVFunctionType(RT, PT);
}
return transType(Scavenger->getScavengedType(V));
}
SPIRVType *
LLVMToSPIRVBase::getSPIRVFunctionType(SPIRVType *RT,
const std::vector<SPIRVType *> &Args) {
// Come up with a unique string identifier for the arguments. This is a hacky
// way of doing so, but it works.
std::string TypeKey;
llvm::raw_string_ostream TKS(TypeKey);
TKS << (uintptr_t)RT << ",";
for (SPIRVType *ArgTy : Args) {
TKS << (uintptr_t)ArgTy << ",";
}
// Create a SPIRVType for the function type. Since SPIRVModule doesn't do
// any type uniquing for SPIRVType, we have to do it ourself.
TKS.flush();
auto It = PointeeTypeMap.find(TypeKey);
if (It == PointeeTypeMap.end())
It = PointeeTypeMap.insert({TypeKey, BM->addFunctionType(RT, Args)}).first;
return It->second;
}
SPIRVFunction *LLVMToSPIRVBase::transFunctionDecl(Function *F) {
if (auto *BF = getTranslatedValue(F))
return static_cast<SPIRVFunction *>(BF);
if (F->isIntrinsic() && (!BM->isSPIRVAllowUnknownIntrinsicsEnabled() ||
isKnownIntrinsic(F->getIntrinsicID()))) {
// We should not translate LLVM intrinsics as a function
assert(none_of(F->users(),
[this](User *U) { return getTranslatedValue(U); }) &&
"LLVM intrinsics shouldn't be called in SPIRV");
return nullptr;
}
SPIRVTypeFunction *BFT =
static_cast<SPIRVTypeFunction *>(transScavengedType(F));
SPIRVFunction *BF =
static_cast<SPIRVFunction *>(mapValue(F, BM->addFunction(BFT)));
BF->setFunctionControlMask(transFunctionControlMask(F));
if (F->hasName()) {
if (isKernel(F)) {
/* strip the prefix as the runtime will be looking for this name */
std::string Prefix = kSPIRVName::EntrypointPrefix;
std::string Name = F->getName().str();
BM->setName(BF, Name.substr(Prefix.size()));
} else {
if (isUniformGroupOperation(F))
BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(
ExtensionID::SPV_KHR_uniform_group_instructions),
SPIRVEC_RequiresExtension, "SPV_KHR_uniform_group_instructions\n");
BM->setName(BF, F->getName().str());
}
}
if (!isKernel(F) && F->getLinkage() != GlobalValue::InternalLinkage)
BF->setLinkageType(transLinkageType(F));
// Translate OpenCL/SYCL buffer_location metadata if it's attached to the
// translated function declaration
MDNode *BufferLocation = nullptr;
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_buffer_location))
BufferLocation = F->getMetadata("kernel_arg_buffer_location");
// Translate runtime_aligned metadata if it's attached to the translated
// function declaration
MDNode *RuntimeAligned = nullptr;
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_runtime_aligned))
RuntimeAligned = F->getMetadata("kernel_arg_runtime_aligned");
auto Attrs = F->getAttributes();
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
++I) {
auto ArgNo = I->getArgNo();
SPIRVFunctionParameter *BA = BF->getArgument(ArgNo);
if (I->hasName())
BM->setName(BA, I->getName().str());
if (I->hasByValAttr())
BA->addAttr(FunctionParameterAttributeByVal);
if (I->hasNoAliasAttr())
BA->addAttr(FunctionParameterAttributeNoAlias);
if (I->hasNoCaptureAttr())
BA->addAttr(FunctionParameterAttributeNoCapture);
if (I->hasStructRetAttr())
BA->addAttr(FunctionParameterAttributeSret);
if (Attrs.hasParamAttr(ArgNo, Attribute::ReadOnly))
BA->addAttr(FunctionParameterAttributeNoWrite);
if (Attrs.hasParamAttr(ArgNo, Attribute::ReadNone))
BA->addAttr(FunctionParameterAttributeNoReadWrite);
if (Attrs.hasParamAttr(ArgNo, Attribute::ZExt))
BA->addAttr(FunctionParameterAttributeZext);
if (Attrs.hasParamAttr(ArgNo, Attribute::SExt))
BA->addAttr(FunctionParameterAttributeSext);
if (Attrs.hasParamAttr(ArgNo, Attribute::Alignment)) {
SPIRVWord AlignmentBytes = Attrs.getParamAttr(ArgNo, Attribute::Alignment)
.getAlignment()
.valueOrOne()
.value();
BA->setAlignment(AlignmentBytes);
}
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_1) &&
Attrs.hasParamAttr(ArgNo, Attribute::Dereferenceable))
BA->addDecorate(DecorationMaxByteOffset,
Attrs.getParamAttr(ArgNo, Attribute::Dereferenceable)
.getDereferenceableBytes());
if (BufferLocation && I->getType()->isPointerTy()) {
// Order of integer numbers in MD node follows the order of function
// parameters on which we shall attach the appropriate decoration. Add
// decoration only if MD value is not negative.
int LocID = -1;
if (!isa<MDString>(BufferLocation->getOperand(ArgNo)) &&
!isa<MDNode>(BufferLocation->getOperand(ArgNo)))
LocID = getMDOperandAsInt(BufferLocation, ArgNo);
if (LocID >= 0)
BA->addDecorate(DecorationBufferLocationINTEL, LocID);
}
if (RuntimeAligned && I->getType()->isPointerTy()) {
// Order of integer numbers in MD node follows the order of function
// parameters on which we shall attach the appropriate decoration. Add
// decoration only if MD value is 1.
int IsRuntimeAligned = 0;
if (!isa<MDString>(RuntimeAligned->getOperand(ArgNo)) &&
!isa<MDNode>(RuntimeAligned->getOperand(ArgNo)))
IsRuntimeAligned = getMDOperandAsInt(RuntimeAligned, ArgNo);
if (IsRuntimeAligned == 1) {
// TODO: to replace non-conformant to the spec decoration generation
// with:
// BM->addExtension(ExtensionID::SPV_INTEL_runtime_aligned);
// BM->addCapability(CapabilityRuntimeAlignedAttributeINTEL);
// BA->addAttr(FunctionParameterAttributeRuntimeAlignedINTEL);
BA->addDecorate(internal::DecorationRuntimeAlignedINTEL,
IsRuntimeAligned);
}
}
}
if (Attrs.hasRetAttr(Attribute::ZExt))
BF->addDecorate(DecorationFuncParamAttr, FunctionParameterAttributeZext);
if (Attrs.hasRetAttr(Attribute::SExt))
BF->addDecorate(DecorationFuncParamAttr, FunctionParameterAttributeSext);
if (Attrs.hasFnAttr("referenced-indirectly")) {
assert(!isKernel(F) &&
"kernel function was marked as referenced-indirectly");
BF->addDecorate(DecorationReferencedIndirectlyINTEL);
}
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute))
transVectorComputeMetadata(F);
transFPGAFunctionMetadata(BF, F);
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_maximum_registers))
transFunctionMetadataAsExecutionMode(BF, F);
else
transFunctionMetadataAsUserSemanticDecoration(BF, F);
transAuxDataInst(BF, F);
SPIRVDBG(dbgs() << "[transFunction] " << *F << " => ";
spvdbgs() << *BF << '\n';)
return BF;
}
void LLVMToSPIRVBase::transVectorComputeMetadata(Function *F) {
using namespace VectorComputeUtil;
if (!BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute))
return;
auto *BF = static_cast<SPIRVFunction *>(getTranslatedValue(F));
assert(BF && "The SPIRVFunction pointer shouldn't be nullptr");
auto Attrs = F->getAttributes();
if (Attrs.hasFnAttr(kVCMetadata::VCStackCall))
BF->addDecorate(DecorationStackCallINTEL);
if (Attrs.hasFnAttr(kVCMetadata::VCFunction))
BF->addDecorate(DecorationVectorComputeFunctionINTEL);
if (Attrs.hasFnAttr(kVCMetadata::VCSIMTCall)) {
SPIRVWord SIMTMode = 0;
Attrs.getFnAttr(kVCMetadata::VCSIMTCall)
.getValueAsString()
.getAsInteger(0, SIMTMode);
BF->addDecorate(DecorationSIMTCallINTEL, SIMTMode);
}
if (Attrs.hasRetAttr(kVCMetadata::VCSingleElementVector))
translateSEVDecoration(
Attrs.getAttributeAtIndex(AttributeList::ReturnIndex,
kVCMetadata::VCSingleElementVector),
BF);
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
++I) {
auto ArgNo = I->getArgNo();
SPIRVFunctionParameter *BA = BF->getArgument(ArgNo);
if (Attrs.hasParamAttr(ArgNo, kVCMetadata::VCArgumentIOKind)) {
SPIRVWord Kind = {};
Attrs.getParamAttr(ArgNo, kVCMetadata::VCArgumentIOKind)
.getValueAsString()
.getAsInteger(0, Kind);
BA->addDecorate(DecorationFuncParamIOKindINTEL, Kind);
}
if (Attrs.hasParamAttr(ArgNo, kVCMetadata::VCSingleElementVector))
translateSEVDecoration(
Attrs.getParamAttr(ArgNo, kVCMetadata::VCSingleElementVector), BA);
if (Attrs.hasParamAttr(ArgNo, kVCMetadata::VCMediaBlockIO)) {
assert(BA->getType()->isTypeImage() &&
"VCMediaBlockIO attribute valid only on image parameters");
BA->addDecorate(DecorationMediaBlockIOINTEL);
}
}
if (!isKernel(F) &&
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_float_controls2) &&
Attrs.hasFnAttr(kVCMetadata::VCFloatControl)) {
SPIRVWord Mode = 0;
Attrs.getFnAttr(kVCMetadata::VCFloatControl)
.getValueAsString()
.getAsInteger(0, Mode);
VCFloatTypeSizeMap::foreach (
[&](VCFloatType FloatType, unsigned TargetWidth) {
BF->addDecorate(new SPIRVDecorateFunctionDenormModeINTEL(
BF, TargetWidth, getFPDenormMode(Mode, FloatType)));
BF->addDecorate(new SPIRVDecorateFunctionRoundingModeINTEL(
BF, TargetWidth, getFPRoundingMode(Mode)));
BF->addDecorate(new SPIRVDecorateFunctionFloatingPointModeINTEL(
BF, TargetWidth, getFPOperationMode(Mode)));
});
}
}
static void transMetadataDecorations(Metadata *MD, SPIRVValue *Target);
void LLVMToSPIRVBase::transFPGAFunctionMetadata(SPIRVFunction *BF,
Function *F) {
if (MDNode *StallEnable = F->getMetadata(kSPIR2MD::StallEnable)) {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_cluster_attributes)) {
if (getMDOperandAsInt(StallEnable, 0))
BF->addDecorate(new SPIRVDecorateStallEnableINTEL(BF));
}
}
if (MDNode *StallFree = F->getMetadata(kSPIR2MD::StallFree)) {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_cluster_attributes)) {
if (getMDOperandAsInt(StallFree, 0)) {
BF->addDecorate(new SPIRVDecorateStallFreeINTEL(BF));
}
}
}
if (MDNode *LoopFuse = F->getMetadata(kSPIR2MD::LoopFuse)) {
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_loop_fuse)) {
size_t Depth = getMDOperandAsInt(LoopFuse, 0);
size_t Independent = getMDOperandAsInt(LoopFuse, 1);
BF->addDecorate(
new SPIRVDecorateFuseLoopsInFunctionINTEL(BF, Depth, Independent));
}
}
if (MDNode *PreferDSP = F->getMetadata(kSPIR2MD::PreferDSP)) {
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_dsp_control)) {
size_t Mode = getMDOperandAsInt(PreferDSP, 0);
MDNode *PropDSPPref = F->getMetadata(kSPIR2MD::PropDSPPref);
size_t Propagate = PropDSPPref ? getMDOperandAsInt(PropDSPPref, 0) : 0;
BF->addDecorate(new SPIRVDecorateMathOpDSPModeINTEL(BF, Mode, Propagate));
}
}
if (MDNode *InitiationInterval =
F->getMetadata(kSPIR2MD::InitiationInterval)) {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_invocation_pipelining_attributes)) {
if (size_t Cycles = getMDOperandAsInt(InitiationInterval, 0))
BF->addDecorate(new SPIRVDecorateInitiationIntervalINTEL(BF, Cycles));
}
}
if (MDNode *MaxConcurrency = F->getMetadata(kSPIR2MD::MaxConcurrency)) {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_invocation_pipelining_attributes)) {
size_t Invocations = getMDOperandAsInt(MaxConcurrency, 0);
BF->addDecorate(new SPIRVDecorateMaxConcurrencyINTEL(BF, Invocations));
}
}
if (MDNode *PipelineKernel = F->getMetadata(kSPIR2MD::PipelineKernel)) {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_invocation_pipelining_attributes)) {
size_t Pipeline = getMDOperandAsInt(PipelineKernel, 0);
BF->addDecorate(new SPIRVDecoratePipelineEnableINTEL(BF, Pipeline));
}
}
// In addition, process the decorations on the function
if (auto *FDecoMD = F->getMetadata(SPIRV_MD_DECORATIONS))
transMetadataDecorations(FDecoMD, BF);
}
void LLVMToSPIRVBase::transFunctionMetadataAsExecutionMode(SPIRVFunction *BF,
Function *F) {
SmallVector<MDNode *, 1> RegisterAllocModeMDs;
F->getMetadata("RegisterAllocMode", RegisterAllocModeMDs);
for (unsigned I = 0; I < RegisterAllocModeMDs.size(); I++) {
auto *RegisterAllocMode = RegisterAllocModeMDs[I]->getOperand(0).get();
if (isa<MDString>(RegisterAllocMode)) {
StringRef Str = getMDOperandAsString(RegisterAllocModeMDs[I], 0);
NamedMaximumNumberOfRegisters NamedValue =
SPIRVNamedMaximumNumberOfRegistersNameMap::rmap(Str.str());
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, ExecutionModeNamedMaximumRegistersINTEL,
NamedValue)));
} else if (isa<MDNode>(RegisterAllocMode)) {
auto *RegisterAllocNodeMDOp =
getMDOperandAsMDNode(RegisterAllocModeMDs[I], 0);
int Num = getMDOperandAsInt(RegisterAllocNodeMDOp, 0);
auto *Const =
BM->addConstant(transType(Type::getInt32Ty(F->getContext())), Num);
BF->addExecutionMode(BM->add(new SPIRVExecutionModeId(
BF, ExecutionModeMaximumRegistersIdINTEL, Const->getId())));
} else {
int64_t RegisterAllocVal =
mdconst::dyn_extract<ConstantInt>(RegisterAllocMode)->getZExtValue();
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, ExecutionModeMaximumRegistersINTEL,
RegisterAllocVal)));
}
}
}
void LLVMToSPIRVBase::transFunctionMetadataAsUserSemanticDecoration(
SPIRVFunction *BF, Function *F) {
if (auto *RegisterAllocModeMD = F->getMetadata("RegisterAllocMode")) {
// TODO: Once the design for per-kernel register size allocation is
// finalized, we will need to move away from UserSemantic and introduce an
// extension
int RegisterAllocNodeMDOp = getMDOperandAsInt(RegisterAllocModeMD, 0);
// The current RegisterAllocMode metadata format is as follows
// AUTO - 0
// SMALL - 1
// LARGE - 2
// DEFAULT - 3
// Currently we only support AUTO, SMALL and LARGE
if (RegisterAllocNodeMDOp == 0 || RegisterAllocNodeMDOp == 1 ||
RegisterAllocNodeMDOp == 2) {
// 4 threads per eu means large grf mode, and 8 threads per eu
// means small grf mode, 0 means use internal heuristics to choose
std::string NumThreads;
switch (RegisterAllocNodeMDOp) {
case 0:
NumThreads = "0";
break;
case 1:
NumThreads = "8";
break;
case 2:
NumThreads = "4";
break;
default:
llvm_unreachable("Not implemented");
}
BF->addDecorate(new SPIRVDecorateUserSemanticAttr(
BF, "num-thread-per-eu " + NumThreads));
}
}
}
void LLVMToSPIRVBase::transAuxDataInst(SPIRVFunction *BF, Function *F) {
auto *BM = BF->getModule();
if (!BM->preserveAuxData())
return;
if (!BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_6))
BM->addExtension(SPIRV::ExtensionID::SPV_KHR_non_semantic_info);
else
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_6);
const auto &FnAttrs = F->getAttributes().getFnAttrs();
for (const auto &Attr : FnAttrs) {
std::vector<SPIRVWord> Ops;
Ops.push_back(BF->getId());
if (Attr.isStringAttribute()) {
// Format for String attributes is:
// NonSemanticAuxDataFunctionAttribute Fcn AttrName AttrValue
// or, if no value:
// NonSemanticAuxDataFunctionAttribute Fcn AttrName
//
// AttrName and AttrValue are always Strings
StringRef AttrKind = Attr.getKindAsString();
StringRef AttrValue = Attr.getValueAsString();
auto *KindSpvString = BM->getString(AttrKind.str());
Ops.push_back(KindSpvString->getId());
if (!AttrValue.empty()) {
auto *ValueSpvString = BM->getString(AttrValue.str());
Ops.push_back(ValueSpvString->getId());
}
} else {
// Format for other types is:
// NonSemanticAuxDataFunctionAttribute Fcn AttrStr
// AttrStr is always a String.
std::string AttrStr = Attr.getAsString();
auto *AttrSpvString = BM->getString(AttrStr);
Ops.push_back(AttrSpvString->getId());
}
BM->addAuxData(NonSemanticAuxData::FunctionAttribute,
transType(Type::getVoidTy(F->getContext())), Ops);
}
SmallVector<std::pair<unsigned, MDNode *>> AllMD;
SmallVector<StringRef> MDNames;
F->getContext().getMDKindNames(MDNames);
F->getAllMetadata(AllMD);
for (const auto &MD : AllMD) {
std::string MDName = MDNames[MD.first].str();
// spirv.Decorations, spirv.ParameterDecorations and debug information are
// handled elsewhere for both forward and reverse translation and are
// complicated to support here, so just skip them.
if (MDName == SPIRV_MD_DECORATIONS ||
MDName == SPIRV_MD_PARAMETER_DECORATIONS ||
MD.first == LLVMContext::MD_dbg)
continue;
// Format for metadata is:
// NonSemanticAuxDataFunctionMetadata Fcn MDName MDVals...
// MDName is always a String, MDVals have different types as explained
// below. Also note this instruction has a variable number of operands
std::vector<SPIRVWord> Ops;
Ops.push_back(BF->getId());
Ops.push_back(BM->getString(MDName)->getId());
for (unsigned int OpIdx = 0; OpIdx < MD.second->getNumOperands(); OpIdx++) {
const auto &CurOp = MD.second->getOperand(OpIdx);
if (auto *MDStr = dyn_cast<MDString>(CurOp)) {
// For MDString, MDVal is String
auto *SPIRVStr = BM->getString(MDStr->getString().str());
Ops.push_back(SPIRVStr->getId());
} else if (auto *ValueAsMeta = dyn_cast<ValueAsMetadata>(CurOp)) {
// For Value metadata, MDVal is a SPIRVValue
auto *SPIRVVal = transValue(ValueAsMeta->getValue(), nullptr);
Ops.push_back(SPIRVVal->getId());
} else {
assert(false && "Unsupported metadata type");
}
}
BM->addAuxData(NonSemanticAuxData::FunctionMetadata,
transType(Type::getVoidTy(F->getContext())), Ops);
}
}
SPIRVValue *LLVMToSPIRVBase::transConstantUse(Constant *C,
SPIRVType *ExpectedType) {
// Constant expressions expect their pointer types to be i8* in opaque pointer
// mode, but the value may have a different "natural" type. If that is the
// case, we need to adjust the type of the constant.
SPIRVValue *Trans = transValue(C, nullptr, true, FuncTransMode::Pointer);
if (Trans->getType() == ExpectedType || Trans->getType()->isTypePipeStorage())
return Trans;
assert(C->getType()->isPointerTy() &&
"Only pointer type mismatches should be possible");
// In the common case of strings ([N x i8] GVs), see if we can emit a GEP
// instruction.
if (auto *GV = dyn_cast<GlobalVariable>(C)) {
if (GV->getValueType()->isArrayTy() &&
GV->getValueType()->getArrayElementType()->isIntegerTy(8)) {
SPIRVValue *Offset = transValue(getUInt32(M, 0), nullptr);
return BM->addPtrAccessChainInst(ExpectedType, Trans, {Offset, Offset},
nullptr, true);
}
}
// Otherwise, just use a bitcast.
return BM->addUnaryInst(OpBitcast, ExpectedType, Trans, nullptr);
}
SPIRVValue *LLVMToSPIRVBase::transConstant(Value *V) {
SPIRVType *ExpectedType = transScavengedType(V);
if (isa<ConstantPointerNull>(V))
return BM->addNullConstant(bcast<SPIRVTypePointer>(ExpectedType));
if (isa<ConstantTargetNone>(V))
return BM->addNullConstant(ExpectedType);
if (auto *CAZero = dyn_cast<ConstantAggregateZero>(V)) {
Type *AggType = CAZero->getType();
if (const StructType *ST = dyn_cast<StructType>(AggType))
if (ST->hasName() &&
ST->getName() == getSPIRVTypeName(kSPIRVTypeName::ConstantSampler))
return BM->addSamplerConstant(transType(AggType), 0, 0, 0);
return BM->addNullConstant(transType(AggType));
}
if (auto *ConstI = dyn_cast<ConstantInt>(V)) {
unsigned BitWidth = ConstI->getIntegerType()->getBitWidth();
if (BitWidth > 64) {
BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_arbitrary_precision_integers),
SPIRVEC_InvalidBitWidth, std::to_string(BitWidth));
return BM->addConstant(ExpectedType, ConstI->getValue());
}
return BM->addConstant(ExpectedType, ConstI->getZExtValue());
}
if (auto *ConstFP = dyn_cast<ConstantFP>(V)) {
auto *BT = static_cast<SPIRVType *>(ExpectedType);
return BM->addConstant(
BT, ConstFP->getValueAPF().bitcastToAPInt().getZExtValue());
}
if (auto *ConstDA = dyn_cast<ConstantDataArray>(V)) {
SPIRVType *InnerTy = ExpectedType->getArrayElementType();
std::vector<SPIRVValue *> BV;
for (unsigned I = 0, E = ConstDA->getNumElements(); I != E; ++I)
BV.push_back(transConstantUse(ConstDA->getElementAsConstant(I), InnerTy));
return BM->addCompositeConstant(ExpectedType, BV);
}
if (auto *ConstA = dyn_cast<ConstantArray>(V)) {
SPIRVType *InnerTy = ExpectedType->getArrayElementType();
std::vector<SPIRVValue *> BV;
for (auto I = ConstA->op_begin(), E = ConstA->op_end(); I != E; ++I)
BV.push_back(transConstantUse(cast<Constant>(*I), InnerTy));
return BM->addCompositeConstant(ExpectedType, BV);
}
if (auto *ConstDV = dyn_cast<ConstantDataVector>(V)) {
SPIRVType *InnerTy = ExpectedType->getScalarType();
std::vector<SPIRVValue *> BV;
for (unsigned I = 0, E = ConstDV->getNumElements(); I != E; ++I)
BV.push_back(transConstantUse(ConstDV->getElementAsConstant(I), InnerTy));
return BM->addCompositeConstant(ExpectedType, BV);
}
if (auto *ConstV = dyn_cast<ConstantVector>(V)) {
SPIRVType *InnerTy = ExpectedType->getScalarType();
std::vector<SPIRVValue *> BV;
for (auto I = ConstV->op_begin(), E = ConstV->op_end(); I != E; ++I)
BV.push_back(transConstantUse(cast<Constant>(*I), InnerTy));
return BM->addCompositeConstant(ExpectedType, BV);
}
if (const auto *ConstV = dyn_cast<ConstantStruct>(V)) {
StringRef StructName;
if (ConstV->getType()->hasName())
StructName = ConstV->getType()->getName();
if (StructName == getSPIRVTypeName(kSPIRVTypeName::ConstantSampler)) {
assert(ConstV->getNumOperands() == 3);
SPIRVWord AddrMode =
ConstV->getOperand(0)->getUniqueInteger().getZExtValue(),
Normalized =
ConstV->getOperand(1)->getUniqueInteger().getZExtValue(),
FilterMode =
ConstV->getOperand(2)->getUniqueInteger().getZExtValue();
assert(AddrMode < 5 && "Invalid addressing mode");
assert(Normalized < 2 && "Invalid value of normalized coords");
assert(FilterMode < 2 && "Invalid filter mode");
SPIRVType *SamplerTy = transType(ConstV->getType());
return BM->addSamplerConstant(SamplerTy, AddrMode, Normalized,
FilterMode);
}
if (StructName == getSPIRVTypeName(kSPIRVTypeName::ConstantPipeStorage)) {
assert(ConstV->getNumOperands() == 3);
SPIRVWord PacketSize =
ConstV->getOperand(0)->getUniqueInteger().getZExtValue(),
PacketAlign =
ConstV->getOperand(1)->getUniqueInteger().getZExtValue(),
Capacity =
ConstV->getOperand(2)->getUniqueInteger().getZExtValue();
assert(PacketAlign >= 1 && "Invalid packet alignment");
assert(PacketSize >= PacketAlign && PacketSize % PacketAlign == 0 &&
"Invalid packet size and/or alignment.");
SPIRVType *PipeStorageTy = transType(ConstV->getType());
return BM->addPipeStorageConstant(PipeStorageTy, PacketSize, PacketAlign,
Capacity);
}
std::vector<SPIRVValue *> BV;
for (auto I = ConstV->op_begin(), E = ConstV->op_end(); I != E; ++I) {
SPIRVType *InnerTy = ExpectedType->getStructMemberType(BV.size());
BV.push_back(transConstantUse(cast<Constant>(*I), InnerTy));
}
return BM->addCompositeConstant(ExpectedType, BV);
}
if (auto *ConstUE = dyn_cast<ConstantExpr>(V)) {
if (auto *GEP = dyn_cast<GEPOperator>(ConstUE)) {
std::vector<SPIRVValue *> Indices;
for (unsigned I = 0, E = GEP->getNumIndices(); I != E; ++I)
Indices.push_back(transValue(GEP->getOperand(I + 1), nullptr));
auto *TransPointerOperand = transValue(GEP->getPointerOperand(), nullptr);
SPIRVType *TranslatedTy = transScavengedType(GEP);
return BM->addPtrAccessChainInst(TranslatedTy, TransPointerOperand,
Indices, nullptr, GEP->isInBounds());
}
auto *Inst = ConstUE->getAsInstruction();
SPIRVDBG(dbgs() << "ConstantExpr: " << *ConstUE << '\n';
dbgs() << "Instruction: " << *Inst << '\n';)
auto *BI = transValue(Inst, nullptr, false);
Inst->dropAllReferences();
UnboundInst.push_back(Inst);
return BI;
}
// Translate aliases to their aliasee because they can't be represented
// directly in SPIR-V.
if (auto *const ConstAlias = dyn_cast<GlobalAlias>(V)) {
return transValue(ConstAlias->getAliasee(), nullptr, false,
FuncTransMode::Pointer);
}
if (isa<UndefValue>(V)) {
return BM->addUndef(ExpectedType);
}
return nullptr;
}
SPIRVValue *LLVMToSPIRVBase::transValue(Value *V, SPIRVBasicBlock *BB,
bool CreateForward,
FuncTransMode FuncTrans) {
LLVMToSPIRVValueMap::iterator Loc = ValueMap.find(V);
if (Loc != ValueMap.end() && (!Loc->second->isForward() || CreateForward) &&
// do not return forward-decl of a function if we
// actually want to create a function pointer
!(FuncTrans == FuncTransMode::Pointer && isa<Function>(V)))
return Loc->second;
SPIRVDBG(dbgs() << "[transValue] " << *V << '\n');
assert((!isa<Instruction>(V) || isa<GetElementPtrInst>(V) ||
isa<CastInst>(V) || isa<ExtractElementInst>(V) || isa<ICmpInst>(V) ||
isa<BinaryOperator>(V) || BB) &&
"Invalid SPIRV BB");
auto *BV = transValueWithoutDecoration(V, BB, CreateForward, FuncTrans);
if (!BV)
return nullptr;
// Only translate decorations for non-forward instructions. Forward
// instructions will have their decorations translated when the actual
// instruction is seen and rewritten to a real SPIR-V instruction.
if (!BV->isForward() && !transDecoration(V, BV))
return nullptr;
StringRef Name = V->getName();
if (!Name.empty()) // Don't erase the name, which BM might already have
BM->setName(BV, Name.str());
return BV;
}
SPIRVInstruction *LLVMToSPIRVBase::transBinaryInst(BinaryOperator *B,
SPIRVBasicBlock *BB) {
unsigned LLVMOC = B->getOpcode();
auto *Op0 = transValue(B->getOperand(0), BB);
SPIRVInstruction *BI = BM->addBinaryInst(
transBoolOpCode(Op0, OpCodeMap::map(LLVMOC)), transType(B->getType()),
Op0, transValue(B->getOperand(1), BB), BB);
// BinaryOperator can have no parent if it is handled as an expression inside
// another instruction.
if (B->getParent() && isUnfusedMulAdd(B)) {
Function *F = B->getFunction();
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName()
<< ": possible fma candidate " << *B << '\n');
joinFPContract(F, FPContract::DISABLED);
}
return BI;
}
SPIRVInstruction *LLVMToSPIRVBase::transCmpInst(CmpInst *Cmp,
SPIRVBasicBlock *BB) {
auto *Op0 = Cmp->getOperand(0);
SPIRVValue *TOp0 = transValue(Op0, BB);
SPIRVValue *TOp1 = transValue(Cmp->getOperand(1), BB);
if (Op0->getType()->isPointerTy()) {
auto P = Cmp->getPredicate();
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4) &&
(P == ICmpInst::ICMP_EQ || P == ICmpInst::ICMP_NE) &&
Cmp->getOperand(1)->getType()->isPointerTy()) {
Op OC = P == ICmpInst::ICMP_EQ ? OpPtrEqual : OpPtrNotEqual;
return BM->addBinaryInst(OC, transType(Cmp->getType()), TOp0, TOp1, BB);
}
unsigned AS = cast<PointerType>(Op0->getType())->getAddressSpace();
SPIRVType *Ty = transType(getSizetType(AS));
TOp0 = BM->addUnaryInst(OpConvertPtrToU, Ty, TOp0, BB);
TOp1 = BM->addUnaryInst(OpConvertPtrToU, Ty, TOp1, BB);
}
SPIRVInstruction *BI =
BM->addCmpInst(transBoolOpCode(TOp0, CmpMap::map(Cmp->getPredicate())),
transType(Cmp->getType()), TOp0, TOp1, BB);
return BI;
}
SPIRVValue *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
SPIRVBasicBlock *BB) {
if (isa<BitCastInst>(U) && U->getType()->isPtrOrPtrVectorTy()) {
if (isa<ConstantPointerNull>(U->getOperand(0))) {
SPIRVType *ExpectedTy = transScavengedType(U);
return BM->addNullConstant(bcast<SPIRVTypePointer>(ExpectedTy));
}
if (isa<UndefValue>(U->getOperand(0))) {
SPIRVType *ExpectedTy = transScavengedType(U);
return BM->addUndef(ExpectedTy);
}
}
Op BOC = OpNop;
if (auto *Cast = dyn_cast<AddrSpaceCastInst>(U)) {
const auto SrcAddrSpace = Cast->getSrcTy()->getPointerAddressSpace();
const auto DestAddrSpace = Cast->getDestTy()->getPointerAddressSpace();
if (DestAddrSpace == SPIRAS_Generic) {
getErrorLog().checkError(
SrcAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule, U,
"Casts from constant address space to generic are illegal\n");
BOC = OpPtrCastToGeneric;
// In SPIR-V only casts to/from generic are allowed. But with
// SPV_INTEL_usm_storage_classes we can also have casts from global_device
// and global_host to global addr space and vice versa.
} else if (SrcAddrSpace == SPIRAS_GlobalDevice ||
SrcAddrSpace == SPIRAS_GlobalHost) {
getErrorLog().checkError(DestAddrSpace == SPIRAS_Global ||
DestAddrSpace == SPIRAS_Generic,
SPIRVEC_InvalidModule, U,
"Casts from global_device/global_host only "
"allowed to global/generic\n");
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes)) {
if (DestAddrSpace == SPIRAS_Global)
return nullptr;
BOC = OpPtrCastToGeneric;
} else {
BOC = OpPtrCastToCrossWorkgroupINTEL;
}
} else if (DestAddrSpace == SPIRAS_GlobalDevice ||
DestAddrSpace == SPIRAS_GlobalHost) {
getErrorLog().checkError(SrcAddrSpace == SPIRAS_Global ||
SrcAddrSpace == SPIRAS_Generic,
SPIRVEC_InvalidModule, U,
"Casts to global_device/global_host only "
"allowed from global/generic\n");
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes)) {
if (SrcAddrSpace == SPIRAS_Global)
return nullptr;
BOC = OpGenericCastToPtr;
} else {
BOC = OpCrossWorkgroupCastToPtrINTEL;
}
} else {
getErrorLog().checkError(
SrcAddrSpace == SPIRAS_Generic, SPIRVEC_InvalidModule, U,
"Casts from private/local/global address space are allowed only to "
"generic\n");
getErrorLog().checkError(
DestAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule, U,
"Casts from generic address space to constant are illegal\n");
BOC = OpGenericCastToPtr;
}
} else {
auto OpCode = U->getOpcode();
BOC = OpCodeMap::map(OpCode);
}
auto *Op = transValue(U->getOperand(0), BB, true, FuncTransMode::Pointer);
SPIRVType *TransTy = transScavengedType(U);
return BM->addUnaryInst(transBoolOpCode(Op, BOC), TransTy, Op, BB);
}
/// This helper class encapsulates information extraction from
/// "llvm.loop.parallel_access_indices" metadata hints. Initialize
/// with a pointer to an MDNode with the following structure:
/// !<Node> = !{!"llvm.loop.parallel_access_indices", !<Node>, !<Node>, ...}
/// OR:
/// !<Node> = !{!"llvm.loop.parallel_access_indices", !<Nodes...>, i32 <value>}
///
/// All of the MDNode-type operands mark the index groups for particular
/// array variables. An optional i32 value indicates the safelen (safe
/// number of iterations) for the optimization application to these
/// array variables. If the safelen value is absent, an infinite
/// number of iterations is implied.
class LLVMParallelAccessIndices {
public:
LLVMParallelAccessIndices(
MDNode *Node, LLVMToSPIRVBase::LLVMToSPIRVMetadataMap &IndexGroupArrayMap)
: Node(Node), IndexGroupArrayMap(IndexGroupArrayMap) {
assert(isValid() &&
"LLVMParallelAccessIndices initialized from an invalid MDNode");
unsigned NumOperands = Node->getNumOperands();
auto *SafeLenExpression = mdconst::dyn_extract_or_null<ConstantInt>(
Node->getOperand(NumOperands - 1));
// If no safelen value is specified and the last operand
// casts to an MDNode* rather than an int, 0 will be stored
SafeLen = SafeLenExpression ? SafeLenExpression->getZExtValue() : 0;
// Count MDNode operands that refer to index groups:
// - operand [0] is a string literal and should be ignored;
// - depending on whether a particular safelen is specified as the
// last operand, we may or may not want to extract the latter
// as an index group
unsigned NumIdxGroups = SafeLen ? NumOperands - 2 : NumOperands - 1;
for (unsigned I = 1; I <= NumIdxGroups; ++I) {
MDNode *IdxGroupNode = getMDOperandAsMDNode(Node, I);
assert(IdxGroupNode &&
"Invalid operand in the MDNode for LLVMParallelAccessIndices");
auto IdxGroupArrayPairIt = IndexGroupArrayMap.find(IdxGroupNode);
// TODO: Some LLVM IR optimizations (e.g. loop inlining as part of
// the function inlining) can result in invalid parallel_access_indices
// metadata. Only valid cases will pass the subsequent check and
// survive the translation. This check should be replaced with an
// assertion once all known cases are handled.
if (IdxGroupArrayPairIt != IndexGroupArrayMap.end())
for (SPIRVId ArrayAccessId : IdxGroupArrayPairIt->second)
ArrayVariablesVec.push_back(ArrayAccessId);
}
}
bool isValid() {
bool IsNamedCorrectly = getMDOperandAsString(Node, 0) == ExpectedName;
return Node && IsNamedCorrectly;
}
unsigned getSafeLen() { return SafeLen; }
const std::vector<SPIRVId> &getArrayVariables() { return ArrayVariablesVec; }
private:
MDNode *Node;
LLVMToSPIRVBase::LLVMToSPIRVMetadataMap &IndexGroupArrayMap;
const std::string ExpectedName = "llvm.loop.parallel_access_indices";
std::vector<SPIRVId> ArrayVariablesVec;
unsigned SafeLen;
};
/// Go through the operands !llvm.loop metadata attached to the branch
/// instruction, fill the Loop Control mask and possible parameters for its
/// fields.
spv::LoopControlMask
LLVMToSPIRVBase::getLoopControl(const BranchInst *Branch,
std::vector<SPIRVWord> &Parameters) {
if (!Branch)
return spv::LoopControlMaskNone;
MDNode *LoopMD = Branch->getMetadata("llvm.loop");
if (!LoopMD)
return spv::LoopControlMaskNone;
size_t LoopControl = spv::LoopControlMaskNone;
std::vector<std::pair<SPIRVWord, SPIRVWord>> ParametersToSort;
// If only a subset of loop count parameters is defined in metadata
// then undefined ones should have a default value -1 in SPIR-V.
// Preset all loop count parameters with the default value.
struct LoopCountInfo {
int64_t Min = -1, Max = -1, Avg = -1;
} LoopCount;
// Unlike with most of the cases, some loop metadata specifications
// can occur multiple times - for these, all correspondent tokens
// need to be collected first, and only then added to SPIR-V loop
// parameters in a separate routine
std::vector<std::pair<SPIRVWord, SPIRVWord>> DependencyArrayParameters;
for (const MDOperand &MDOp : LoopMD->operands()) {
if (MDNode *Node = dyn_cast<MDNode>(MDOp)) {
StringRef S = getMDOperandAsString(Node, 0);
// Set the loop control bits. Parameters are set in the order described
// in 3.23 SPIR-V Spec. rev. 1.4:
// Bits that are set can indicate whether an additional operand follows,
// as described by the table. If there are multiple following operands
// indicated, they are ordered: Those indicated by smaller-numbered bits
// appear first.
if (S == "llvm.loop.unroll.disable")
LoopControl |= spv::LoopControlDontUnrollMask;
else if (S == "llvm.loop.unroll.enable")
LoopControl |= spv::LoopControlUnrollMask;
// Attempt to do full unroll of the loop and disable unrolling if the trip
// count is not known at compile time by setting PartialCount to 1
else if (S == "llvm.loop.unroll.full") {
LoopControl |= spv::LoopControlUnrollMask;
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
ParametersToSort.emplace_back(spv::LoopControlPartialCountMask, 1);
LoopControl |= spv::LoopControlPartialCountMask;
}
}
// PartialCount must not be used with the DontUnroll bit
else if (S == "llvm.loop.unroll.count" &&
!(LoopControl & LoopControlDontUnrollMask)) {
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(spv::LoopControlPartialCountMask, I);
LoopControl |= spv::LoopControlPartialCountMask;
}
} else if (S == "llvm.loop.ivdep.enable")
LoopControl |= spv::LoopControlDependencyInfiniteMask;
else if (S == "llvm.loop.ivdep.safelen") {
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(spv::LoopControlDependencyLengthMask, I);
LoopControl |= spv::LoopControlDependencyLengthMask;
} else if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_loop_controls)) {
// Add Intel specific Loop Control masks
if (S == "llvm.loop.ii.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(
spv::LoopControlInitiationIntervalINTELMask, I);
LoopControl |= spv::LoopControlInitiationIntervalINTELMask;
} else if (S == "llvm.loop.max_concurrency.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(spv::LoopControlMaxConcurrencyINTELMask,
I);
LoopControl |= spv::LoopControlMaxConcurrencyINTELMask;
} else if (S == "llvm.loop.parallel_access_indices") {
// Intel FPGA IVDep loop attribute
LLVMParallelAccessIndices IVDep(Node, IndexGroupArrayMap);
// Store IVDep-specific parameters into an intermediate
// container to address the case when there're multiple
// IVDep metadata nodes and this condition gets entered multiple
// times. The update of the main parameters vector & the loop control
// mask will be done later, in the main scope of the function
unsigned SafeLen = IVDep.getSafeLen();
for (auto &ArrayId : IVDep.getArrayVariables())
DependencyArrayParameters.emplace_back(ArrayId, SafeLen);
} else if (S == "llvm.loop.intel.pipelining.enable") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(spv::LoopControlPipelineEnableINTELMask,
I);
LoopControl |= spv::LoopControlPipelineEnableINTELMask;
} else if (S == "llvm.loop.coalesce.enable") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
ParametersToSort.emplace_back(spv::LoopControlLoopCoalesceINTELMask,
0);
LoopControl |= spv::LoopControlLoopCoalesceINTELMask;
} else if (S == "llvm.loop.coalesce.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(spv::LoopControlLoopCoalesceINTELMask,
I);
LoopControl |= spv::LoopControlLoopCoalesceINTELMask;
} else if (S == "llvm.loop.max_interleaving.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(
spv::LoopControlMaxInterleavingINTELMask, I);
LoopControl |= spv::LoopControlMaxInterleavingINTELMask;
} else if (S == "llvm.loop.intel.speculated.iterations.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(
spv::LoopControlSpeculatedIterationsINTELMask, I);
LoopControl |= spv::LoopControlSpeculatedIterationsINTELMask;
} else if (S == "llvm.loop.fusion.disable") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
LoopControl |= spv::LoopControlNoFusionINTELMask;
} else if (S == "llvm.loop.intel.loopcount_min") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
LoopCount.Min = getMDOperandAsInt(Node, 1);
LoopControl |= spv::LoopControlLoopCountINTELMask;
} else if (S == "llvm.loop.intel.loopcount_max") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
LoopCount.Max = getMDOperandAsInt(Node, 1);
LoopControl |= spv::LoopControlLoopCountINTELMask;
} else if (S == "llvm.loop.intel.loopcount_avg") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
LoopCount.Avg = getMDOperandAsInt(Node, 1);
LoopControl |= spv::LoopControlLoopCountINTELMask;
} else if (S == "llvm.loop.intel.max_reinvocation_delay.count") {
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
size_t I = getMDOperandAsInt(Node, 1);
ParametersToSort.emplace_back(
spv::LoopControlMaxReinvocationDelayINTELMask, I);
LoopControl |= spv::LoopControlMaxReinvocationDelayINTELMask;
}
}
}
}
if (LoopControl & spv::LoopControlLoopCountINTELMask) {
// LoopCountINTELMask have int64 literal parameters and we need to store
// int64 into 2 SPIRVWords
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Min));
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Min >> 32));
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Max));
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Max >> 32));
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Avg));
ParametersToSort.emplace_back(spv::LoopControlLoopCountINTELMask,
static_cast<SPIRVWord>(LoopCount.Avg >> 32));
}
// If any loop control parameters were held back until fully collected,
// now is the time to move the information to the main parameters collection
if (!DependencyArrayParameters.empty()) {
// The first parameter states the number of <array, safelen> pairs to be
// listed
ParametersToSort.emplace_back(spv::LoopControlDependencyArrayINTELMask,
DependencyArrayParameters.size());
for (auto &ArraySflnPair : DependencyArrayParameters) {
ParametersToSort.emplace_back(spv::LoopControlDependencyArrayINTELMask,
ArraySflnPair.first);
ParametersToSort.emplace_back(spv::LoopControlDependencyArrayINTELMask,
ArraySflnPair.second);
}
BM->addExtension(ExtensionID::SPV_INTEL_fpga_loop_controls);
BM->addCapability(CapabilityFPGALoopControlsINTEL);
LoopControl |= spv::LoopControlDependencyArrayINTELMask;
}
std::stable_sort(ParametersToSort.begin(), ParametersToSort.end(),
[](const std::pair<SPIRVWord, SPIRVWord> &CompareLeft,
const std::pair<SPIRVWord, SPIRVWord> &CompareRight) {
return CompareLeft.first < CompareRight.first;
});
for (const auto &Param : ParametersToSort)
Parameters.push_back(Param.second);
return static_cast<spv::LoopControlMask>(LoopControl);
}
static int transAtomicOrdering(llvm::AtomicOrdering Ordering) {
return OCLMemOrderMap::map(
static_cast<OCLMemOrderKind>(llvm::toCABI(Ordering)));
}
SPIRVValue *LLVMToSPIRVBase::transAtomicStore(StoreInst *ST,
SPIRVBasicBlock *BB) {
SmallVector<StringRef> SSIDs;
ST->getContext().getSyncScopeNames(SSIDs);
spv::Scope S;
// Fill unknown syncscope value to default Device scope.
if (!OCLStrMemScopeMap::find(SSIDs[ST->getSyncScopeID()].str(), &S)) {
S = ScopeDevice;
}
std::vector<Value *> Ops{ST->getPointerOperand(), getUInt32(M, S),
getUInt32(M, transAtomicOrdering(ST->getOrdering())),
ST->getValueOperand()};
std::vector<SPIRVValue *> SPIRVOps = transValue(Ops, BB);
return mapValue(ST, BM->addInstTemplate(OpAtomicStore, BM->getIds(SPIRVOps),
BB, nullptr));
}
SPIRVValue *LLVMToSPIRVBase::transAtomicLoad(LoadInst *LD,
SPIRVBasicBlock *BB) {
SmallVector<StringRef> SSIDs;
LD->getContext().getSyncScopeNames(SSIDs);
spv::Scope S;
// Fill unknown syncscope value to default Device scope.
if (!OCLStrMemScopeMap::find(SSIDs[LD->getSyncScopeID()].str(), &S)) {
S = ScopeDevice;
}
std::vector<Value *> Ops{
LD->getPointerOperand(), getUInt32(M, S),
getUInt32(M, transAtomicOrdering(LD->getOrdering()))};
std::vector<SPIRVValue *> SPIRVOps = transValue(Ops, BB);
return mapValue(LD, BM->addInstTemplate(OpAtomicLoad, BM->getIds(SPIRVOps),
BB, transScavengedType(LD)));
}
// Aliasing list MD contains several scope MD nodes whithin it. Each scope MD
// has a selfreference and an extra MD node for aliasing domain and also it
// can contain an optional string operand. Domain MD contains a self-reference
// with an optional string operand. Here we unfold the list, creating SPIR-V
// aliasing instructions.
// TODO: add support for an optional string operand.
SPIRVEntry *addMemAliasingINTELInstructions(SPIRVModule *M,
MDNode *AliasingListMD) {
if (AliasingListMD->getNumOperands() == 0)
return nullptr;
std::vector<SPIRVId> ListId;
for (const MDOperand &MDListOp : AliasingListMD->operands()) {
if (MDNode *ScopeMD = dyn_cast<MDNode>(MDListOp)) {
if (ScopeMD->getNumOperands() < 2)
return nullptr;
MDNode *DomainMD = dyn_cast<MDNode>(ScopeMD->getOperand(1));
if (!DomainMD)
return nullptr;
auto *Domain =
M->getOrAddAliasDomainDeclINTELInst(std::vector<SPIRVId>(), DomainMD);
auto *Scope =
M->getOrAddAliasScopeDeclINTELInst({Domain->getId()}, ScopeMD);
ListId.push_back(Scope->getId());
}
}
return M->getOrAddAliasScopeListDeclINTELInst(ListId, AliasingListMD);
}
// Translate alias.scope/noalias metadata attached to store and load
// instructions.
void transAliasingMemAccess(SPIRVModule *BM, MDNode *AliasingListMD,
std::vector<uint32_t> &MemoryAccess,
SPIRVWord MemAccessMask) {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_memory_access_aliasing))
return;
auto *MemAliasList = addMemAliasingINTELInstructions(BM, AliasingListMD);
if (!MemAliasList)
return;
MemoryAccess[0] |= MemAccessMask;
MemoryAccess.push_back(MemAliasList->getId());
}
/// An instruction may use an instruction from another BB which has not been
/// translated. SPIRVForward should be created as place holder for these
/// instructions and replaced later by the real instructions.
/// Use CreateForward = true to indicate such situation.
SPIRVValue *
LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
bool CreateForward,
FuncTransMode FuncTrans) {
if (auto *LBB = dyn_cast<BasicBlock>(V)) {
auto *BF =
static_cast<SPIRVFunction *>(getTranslatedValue(LBB->getParent()));
assert(BF && "Function not translated");
BB = static_cast<SPIRVBasicBlock *>(mapValue(V, BM->addBasicBlock(BF)));
BM->setName(BB, LBB->getName().str());
return BB;
}
if (auto *F = dyn_cast<Function>(V)) {
if (FuncTrans == FuncTransMode::Decl)
return transFunctionDecl(F);
if (!BM->checkExtension(ExtensionID::SPV_INTEL_function_pointers,
SPIRVEC_FunctionPointers, toString(V)))
return nullptr;
return BM->addConstantFunctionPointerINTEL(
transPointerType(transScavengedType(F), F->getAddressSpace()),
static_cast<SPIRVFunction *>(transValue(F, nullptr)));
}
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
llvm::Type *Ty = Scavenger->getScavengedType(GV);
// Though variables with common linkage type are initialized by 0,
// they can be represented in SPIR-V as uninitialized variables with
// 'Export' linkage type, just as tentative definitions look in C
llvm::Constant *Init = GV->hasInitializer() && !GV->hasCommonLinkage()
? GV->getInitializer()
: nullptr;
SPIRVValue *BVarInit = nullptr;
StructType *ST = Init ? dyn_cast<StructType>(Init->getType()) : nullptr;
if (ST && ST->hasName() && isSPIRVConstantName(ST->getName())) {
auto *BV = transConstant(Init);
assert(BV);
return mapValue(V, BV);
}
if (isa_and_nonnull<ConstantExpr>(Init)) {
BVarInit = transValue(Init, nullptr);
} else if (ST && isa<UndefValue>(Init)) {
// Undef initializer for LLVM structure be can translated to
// OpConstantComposite with OpUndef constituents.
auto I = ValueMap.find(Init);
if (I == ValueMap.end()) {
std::vector<SPIRVValue *> Elements;
for (Type *E : ST->elements())
Elements.push_back(transValue(UndefValue::get(E), nullptr));
BVarInit = BM->addCompositeConstant(transType(ST), Elements);
ValueMap[Init] = BVarInit;
} else
BVarInit = I->second;
} else if (Init && !isa<UndefValue>(Init)) {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_long_constant_composite)) {
if (auto *ArrTy = dyn_cast_or_null<ArrayType>(Init->getType())) {
// First 3 words of OpConstantComposite encode: 1) word count &
// opcode, 2) Result Type and 3) Result Id. Max length of SPIRV
// instruction = 65535 words.
constexpr int MaxNumElements =
MaxWordCount - SPIRVSpecConstantComposite::FixedWC;
if (ArrTy->getNumElements() > MaxNumElements &&
!isa<ConstantAggregateZero>(Init)) {
std::stringstream SS;
SS << "Global variable has a constant array initializer with a "
<< "number of elements greater than OpConstantComposite can "
<< "have (" << MaxNumElements << "). Should the array be "
<< "split?\n Original LLVM value:\n"
<< toString(GV);
getErrorLog().checkError(false, SPIRVEC_InvalidWordCount, SS.str());
}
}
}
SPIRVType *TransTy = transType(Ty);
BVarInit = transConstantUse(Init, TransTy->getPointerElementType());
}
SPIRVStorageClassKind StorageClass;
auto AddressSpace = static_cast<SPIRAddressSpace>(GV->getAddressSpace());
bool IsVectorCompute =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute) &&
GV->hasAttribute(kVCMetadata::VCGlobalVariable);
if (IsVectorCompute)
StorageClass =
VectorComputeUtil::getVCGlobalVarStorageClass(AddressSpace);
else {
// Lower global_device and global_host address spaces that were added in
// SYCL as part of SYCL_INTEL_usm_address_spaces extension to just global
// address space if device doesn't support SPV_INTEL_usm_storage_classes
// extension
if ((AddressSpace == SPIRAS_GlobalDevice ||
AddressSpace == SPIRAS_GlobalHost) &&
!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes))
AddressSpace = SPIRAS_Global;
StorageClass = SPIRSPIRVAddrSpaceMap::map(AddressSpace);
if (StorageClass == StorageClassFunction) {
std::stringstream SS;
SS << "Global variable cannot have Function storage class. "
<< "Consider setting a proper address space.\n "
<< "Original LLVM value:\n"
<< toString(GV);
getErrorLog().checkError(false, SPIRVEC_InvalidInstruction, SS.str());
}
}
SPIRVType *TranslatedTy = transType(Ty);
auto *BVar = static_cast<SPIRVVariable *>(
BM->addVariable(TranslatedTy, GV->isConstant(), transLinkageType(GV),
BVarInit, GV->getName().str(), StorageClass, nullptr));
if (IsVectorCompute) {
BVar->addDecorate(DecorationVectorComputeVariableINTEL);
if (GV->hasAttribute(kVCMetadata::VCByteOffset)) {
SPIRVWord Offset = {};
GV->getAttribute(kVCMetadata::VCByteOffset)
.getValueAsString()
.getAsInteger(0, Offset);
BVar->addDecorate(DecorationGlobalVariableOffsetINTEL, Offset);
}
if (GV->hasAttribute(kVCMetadata::VCVolatile))
BVar->addDecorate(DecorationVolatile);
if (GV->hasAttribute(kVCMetadata::VCSingleElementVector))
translateSEVDecoration(
GV->getAttribute(kVCMetadata::VCSingleElementVector), BVar);
}
mapValue(V, BVar);
spv::BuiltIn Builtin = spv::BuiltInPosition;
if (!GV->hasName() || !getSPIRVBuiltin(GV->getName().str(), Builtin))
return BVar;
if (static_cast<uint32_t>(Builtin) >= internal::BuiltInSubDeviceIDINTEL &&
static_cast<uint32_t>(Builtin) <=
internal::BuiltInGlobalHWThreadIDINTEL) {
std::string ErrorStr = "SPV_INTEL_hw_thread_queries\n"
"Please report to "
"https://github.com/intel/llvm in case if you see "
"this error.\nRef LLVM Value:";
getErrorLog().checkError(false, SPIRVEC_DeprecatedExtension, V, ErrorStr);
}
BVar->setBuiltin(Builtin);
return BVar;
}
if (isa<Constant>(V)) {
auto *BV = transConstant(V);
assert(BV);
// Don't store pointer constants in the map -- they are opaque and thus we
// might reuse the wrong type (Example: a null value) if we do so.
if (V->getType()->isPointerTy())
return BV;
return mapValue(V, BV);
}
if (auto *Arg = dyn_cast<Argument>(V)) {
unsigned ArgNo = Arg->getArgNo();
SPIRVFunction *BF = BB->getParent();
// assert(BF->existArgument(ArgNo));
return mapValue(V, BF->getArgument(ArgNo));
}
if (CreateForward)
return mapValue(V, BM->addForward(transScavengedType(V)));
if (StoreInst *ST = dyn_cast<StoreInst>(V)) {
if (ST->isAtomic())
return transAtomicStore(ST, BB);
// Keep this vector to store MemoryAccess operands for both Alignment and
// Aliasing information.
std::vector<SPIRVWord> MemoryAccess(1, 0);
if (ST->isVolatile())
MemoryAccess[0] |= MemoryAccessVolatileMask;
MemoryAccess[0] |= MemoryAccessAlignedMask;
MemoryAccess.push_back(ST->getAlign().value());
if (ST->getMetadata(LLVMContext::MD_nontemporal))
MemoryAccess[0] |= MemoryAccessNontemporalMask;
if (MDNode *AliasingListMD = ST->getMetadata(LLVMContext::MD_alias_scope))
transAliasingMemAccess(BM, AliasingListMD, MemoryAccess,
MemoryAccessAliasScopeINTELMaskMask);
if (MDNode *AliasingListMD = ST->getMetadata(LLVMContext::MD_noalias))
transAliasingMemAccess(BM, AliasingListMD, MemoryAccess,
MemoryAccessNoAliasINTELMaskMask);
if (MemoryAccess.front() == 0)
MemoryAccess.clear();
return mapValue(V,
BM->addStoreInst(transValue(ST->getPointerOperand(), BB),
transValue(ST->getValueOperand(), BB, true,
FuncTransMode::Pointer),
MemoryAccess, BB));
}
if (LoadInst *LD = dyn_cast<LoadInst>(V)) {
if (LD->isAtomic())
return transAtomicLoad(LD, BB);
// Keep this vector to store MemoryAccess operands for both Alignment and
// Aliasing information.
std::vector<uint32_t> MemoryAccess(1, 0);
if (LD->isVolatile())
MemoryAccess[0] |= MemoryAccessVolatileMask;
MemoryAccess[0] |= MemoryAccessAlignedMask;
MemoryAccess.push_back(LD->getAlign().value());
if (LD->getMetadata(LLVMContext::MD_nontemporal))
MemoryAccess[0] |= MemoryAccessNontemporalMask;
if (MDNode *AliasingListMD = LD->getMetadata(LLVMContext::MD_alias_scope))
transAliasingMemAccess(BM, AliasingListMD, MemoryAccess,
MemoryAccessAliasScopeINTELMaskMask);
if (MDNode *AliasingListMD = LD->getMetadata(LLVMContext::MD_noalias))
transAliasingMemAccess(BM, AliasingListMD, MemoryAccess,
MemoryAccessNoAliasINTELMaskMask);
if (MemoryAccess.front() == 0)
MemoryAccess.clear();
return mapValue(V, BM->addLoadInst(transValue(LD->getPointerOperand(), BB),
MemoryAccess, BB));
}
if (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
SPIRVInstruction *BI = transBinaryInst(B, BB);
return mapValue(V, BI);
}
if (dyn_cast<UnreachableInst>(V))
return mapValue(V, BM->addUnreachableInst(BB));
if (auto *RI = dyn_cast<ReturnInst>(V)) {
if (auto *RV = RI->getReturnValue()) {
if (auto *II = dyn_cast<IntrinsicInst>(RV)) {
if (II->getIntrinsicID() == Intrinsic::frexp) {
// create composite type from the return value and second operand
auto *FrexpResult = transValue(RV, BB);
SPIRVValue *IntFromFrexpResult =
static_cast<SPIRVExtInst *>(FrexpResult)->getArgValues()[1];
IntFromFrexpResult = BM->addLoadInst(IntFromFrexpResult, {}, BB);
std::vector<SPIRVId> Operands = {FrexpResult->getId(),
IntFromFrexpResult->getId()};
auto *Compos = BM->addCompositeConstructInst(transType(RV->getType()),
Operands, BB);
return mapValue(V, BM->addReturnValueInst(Compos, BB));
}
}
return mapValue(V, BM->addReturnValueInst(transValue(RV, BB), BB));
}
return mapValue(V, BM->addReturnInst(BB));
}
if (CmpInst *Cmp = dyn_cast<CmpInst>(V)) {
if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_FALSE) {
auto *CmpTy = Cmp->getType();
SPIRVValue *FalseValue = CmpTy->isVectorTy()
? BM->addNullConstant(transType(CmpTy))
: BM->addConstant(BM->addBoolType(), 0);
return mapValue(V, FalseValue);
}
SPIRVInstruction *BI = transCmpInst(Cmp, BB);
return mapValue(V, BI);
}
if (SelectInst *Sel = dyn_cast<SelectInst>(V))
return mapValue(
V,
BM->addSelectInst(
transValue(Sel->getCondition(), BB),
transValue(Sel->getTrueValue(), BB, true, FuncTransMode::Pointer),
transValue(Sel->getFalseValue(), BB, true, FuncTransMode::Pointer),
BB));
if (AllocaInst *Alc = dyn_cast<AllocaInst>(V)) {
SPIRVType *TranslatedTy = transScavengedType(V);
if (Alc->isArrayAllocation()) {
SPIRVValue *Length = transValue(Alc->getArraySize(), BB);
assert(Length && "Couldn't translate array size!");
if (isSpecConstantOpCode(Length->getOpCode())) {
// SPIR-V arrays length can be expressed using a specialization
// constant.
//
// Spec Constant Length Arrays need special treatment, as the allocation
// type will be 'OpTypePointer(Function, OpTypeArray(ElementType,
// Length))', we need to bitcast the obtained pointer to the expected
// type: 'OpTypePointer(Function, ElementType).
SPIRVType *AllocationType = BM->addPointerType(
StorageClassFunction,
BM->addArrayType(transType(Alc->getAllocatedType()), Length));
SPIRVValue *Arr = BM->addVariable(
AllocationType, false, spv::internal::LinkageTypeInternal, nullptr,
Alc->getName().str() + "_alloca", StorageClassFunction, BB);
// Manually set alignment. OpBitcast created below will be decorated as
// that's the SPIR-V value mapped to the original LLVM one.
transAlign(Alc, Arr);
return mapValue(V, BM->addUnaryInst(OpBitcast, TranslatedTy, Arr, BB));
}
if (!BM->checkExtension(ExtensionID::SPV_INTEL_variable_length_array,
SPIRVEC_InvalidInstruction,
toString(Alc) +
"\nTranslation of dynamic alloca requires "
"SPV_INTEL_variable_length_array extension."))
return nullptr;
return mapValue(V,
BM->addInstTemplate(OpVariableLengthArrayINTEL,
{Length->getId()}, BB, TranslatedTy));
}
SPIRVType *VarTy =
V->getType()->getPointerAddressSpace() == SPIRAS_Generic
? BM->addPointerType(StorageClassFunction,
TranslatedTy->getPointerElementType())
: TranslatedTy;
SPIRVValue *Var = BM->addVariable(
VarTy, false, spv::internal::LinkageTypeInternal, nullptr,
Alc->getName().str(), StorageClassFunction, BB);
if (V->getType()->getPointerAddressSpace() == SPIRAS_Generic) {
SPIRVValue *Cast =
BM->addUnaryInst(OpPtrCastToGeneric, TranslatedTy, Var, BB);
return mapValue(V, Cast);
}
return mapValue(V, Var);
}
if (auto *Switch = dyn_cast<SwitchInst>(V)) {
std::vector<SPIRVSwitch::PairTy> Pairs;
auto *Select = transValue(Switch->getCondition(), BB);
for (auto I = Switch->case_begin(), E = Switch->case_end(); I != E; ++I) {
SPIRVSwitch::LiteralTy Lit;
uint64_t CaseValue = I->getCaseValue()->getZExtValue();
Lit.push_back(CaseValue);
assert(Select->getType()->getBitWidth() <= 64 &&
"unexpected selector bitwidth");
if (Select->getType()->getBitWidth() == 64)
Lit.push_back(CaseValue >> 32);
Pairs.push_back(
std::make_pair(Lit, static_cast<SPIRVBasicBlock *>(
transValue(I->getCaseSuccessor(), nullptr))));
}
return mapValue(
V, BM->addSwitchInst(Select,
static_cast<SPIRVBasicBlock *>(
transValue(Switch->getDefaultDest(), nullptr)),
Pairs, BB));
}
if (BranchInst *Branch = dyn_cast<BranchInst>(V)) {
SPIRVLabel *SuccessorTrue =
static_cast<SPIRVLabel *>(transValue(Branch->getSuccessor(0), BB));
/// Clang attaches !llvm.loop metadata to "latch" BB. This kind of blocks
/// has an edge directed to the loop header. Thus latch BB matching to
/// "Continue Target" per the SPIR-V spec. This statement is true only after
/// applying the loop-simplify pass to the LLVM module.
/// For "for" and "while" loops latch BB is terminated by an
/// unconditional branch. Also for this kind of loops "Merge Block" can
/// be found as block targeted by false edge of the "Header" BB.
/// For "do while" loop the latch is terminated by a conditional branch
/// with true edge going to the header and the false edge going out of
/// the loop, which corresponds to a "Merge Block" per the SPIR-V spec.
std::vector<SPIRVWord> Parameters;
spv::LoopControlMask LoopControl = getLoopControl(Branch, Parameters);
if (Branch->isUnconditional()) {
// Usually, "for" and "while" loops llvm.loop metadata is attached to an
// unconditional branch instruction.
if (LoopControl != spv::LoopControlMaskNone) {
// SuccessorTrue is the loop header BB.
const SPIRVInstruction *Term = SuccessorTrue->getTerminateInstr();
if (Term && Term->getOpCode() == OpBranchConditional) {
const auto *Br = static_cast<const SPIRVBranchConditional *>(Term);
BM->addLoopMergeInst(Br->getFalseLabel()->getId(), // Merge Block
BB->getId(), // Continue Target
LoopControl, Parameters, SuccessorTrue);
} else {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_unstructured_loop_controls)) {
// For unstructured loop we add a special loop control instruction.
// Simple example of unstructured loop is an infinite loop, that has
// no terminate instruction.
BM->addLoopControlINTELInst(LoopControl, Parameters, SuccessorTrue);
}
}
}
return mapValue(V, BM->addBranchInst(SuccessorTrue, BB));
}
// For "do-while" (and in some cases, for "for" and "while") loops,
// llvm.loop metadata is attached to a conditional branch instructions
SPIRVLabel *SuccessorFalse =
static_cast<SPIRVLabel *>(transValue(Branch->getSuccessor(1), BB));
if (LoopControl != spv::LoopControlMaskNone) {
Function *Fun = Branch->getFunction();
DominatorTree DomTree(*Fun);
LoopInfo LI(DomTree);
for (const auto *LoopObj : LI.getLoopsInPreorder()) {
// Check whether SuccessorFalse or SuccessorTrue is the loop header BB.
// For example consider following LLVM IR:
// br i1 %compare, label %for.body, label %for.end
// <- SuccessorTrue is 'for.body' aka successor(0)
// br i1 %compare.not, label %for.end, label %for.body
// <- SuccessorTrue is 'for.end' aka successor(1)
// meanwhile the true successor (by definition) should be a loop header
// aka 'for.body'
if (LoopObj->getHeader() == Branch->getSuccessor(1))
// SuccessorFalse is the loop header BB.
BM->addLoopMergeInst(SuccessorTrue->getId(), // Merge Block
BB->getId(), // Continue Target
LoopControl, Parameters, SuccessorFalse);
else
// SuccessorTrue is the loop header BB.
BM->addLoopMergeInst(SuccessorFalse->getId(), // Merge Block
BB->getId(), // Continue Target
LoopControl, Parameters, SuccessorTrue);
}
}
return mapValue(
V, BM->addBranchConditionalInst(transValue(Branch->getCondition(), BB),
SuccessorTrue, SuccessorFalse, BB));
}
if (auto *Phi = dyn_cast<PHINode>(V)) {
std::vector<SPIRVValue *> IncomingPairs;
for (size_t I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
IncomingPairs.push_back(transValue(Phi->getIncomingValue(I), BB, true,
FuncTransMode::Pointer));
IncomingPairs.push_back(transValue(Phi->getIncomingBlock(I), nullptr));
}
return mapValue(V,
BM->addPhiInst(transScavengedType(Phi), IncomingPairs, BB));
}
if (auto *Ext = dyn_cast<ExtractValueInst>(V)) {
if (auto *II = dyn_cast<IntrinsicInst>(Ext->getAggregateOperand())) {
if (II->getIntrinsicID() == Intrinsic::frexp) {
unsigned Idx = Ext->getIndices()[0];
auto *Val = transValue(II, BB);
if (Idx == 0)
return mapValue(V, Val);
// Idx = 1
SPIRVValue *IntFromFrexpResult =
static_cast<SPIRVExtInst *>(Val)->getArgValues()[1];
IntFromFrexpResult = BM->addLoadInst(IntFromFrexpResult, {}, BB);
return mapValue(V, IntFromFrexpResult);
}
}
return mapValue(V, BM->addCompositeExtractInst(
transScavengedType(Ext),
transValue(Ext->getAggregateOperand(), BB),
Ext->getIndices(), BB));
}
if (auto *Ins = dyn_cast<InsertValueInst>(V)) {
return mapValue(V, BM->addCompositeInsertInst(
transValue(Ins->getInsertedValueOperand(), BB),
transValue(Ins->getAggregateOperand(), BB),
Ins->getIndices(), BB));
}
if (UnaryInstruction *U = dyn_cast<UnaryInstruction>(V)) {
auto *UI = transUnaryInst(U, BB);
return mapValue(V, UI ? UI : transValue(U->getOperand(0), BB));
}
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
std::vector<SPIRVValue *> Indices;
for (unsigned I = 0, E = GEP->getNumIndices(); I != E; ++I)
Indices.push_back(transValue(GEP->getOperand(I + 1), BB));
auto *PointerOperand = GEP->getPointerOperand();
auto *TransPointerOperand = transValue(PointerOperand, BB);
// Certain array-related optimization hints can be expressed via
// LLVM metadata. For the purpose of linking this metadata with
// the accessed array variables, our GEP may have been marked into
// a so-called index group, an MDNode by itself.
if (MDNode *IndexGroup = GEP->getMetadata("llvm.index.group")) {
SPIRVValue *ActualMemoryPtr = TransPointerOperand;
// If the source is a no-op bitcast (generated to fix up types), look
// through it to the underlying gep if possible.
if (auto *BC = dyn_cast<CastInst>(PointerOperand))
if (BC->getSrcTy() == BC->getDestTy()) {
PointerOperand = BC->getOperand(0);
}
if (auto *Load = dyn_cast<LoadInst>(PointerOperand)) {
ActualMemoryPtr = transValue(Load->getPointerOperand(), BB);
}
SPIRVId AccessedArrayId = ActualMemoryPtr->getId();
unsigned NumOperands = IndexGroup->getNumOperands();
// When we're working with embedded loops, it's natural that
// the outer loop's hints apply to all code contained within.
// The inner loop's specific hints, however, should stay private
// to the inner loop's scope.
// Consequently, the following division of the index group metadata
// nodes emerges:
// 1) The metadata node has no operands. It will be directly referenced
// from within the optimization hint metadata.
if (NumOperands == 0)
IndexGroupArrayMap[IndexGroup].insert(AccessedArrayId);
// 2) The metadata node has several operands. It serves to link an index
// group specific to some embedded loop with other index groups that
// mark the same array variable for the outer loop(s).
for (unsigned I = 0; I < NumOperands; ++I) {
auto *ContainedIndexGroup = getMDOperandAsMDNode(IndexGroup, I);
IndexGroupArrayMap[ContainedIndexGroup].insert(AccessedArrayId);
}
}
// GEP can return a vector of pointers, in this case GEP will calculate
// addresses for each pointer in the vector
SPIRVType *TranslatedTy = transScavengedType(GEP);
return mapValue(V,
BM->addPtrAccessChainInst(TranslatedTy, TransPointerOperand,
Indices, BB, GEP->isInBounds()));
}
if (auto *Ext = dyn_cast<ExtractElementInst>(V)) {
auto *Index = Ext->getIndexOperand();
if (auto *Const = dyn_cast<ConstantInt>(Index))
return mapValue(V, BM->addCompositeExtractInst(
transScavengedType(Ext),
transValue(Ext->getVectorOperand(), BB),
std::vector<SPIRVWord>(1, Const->getZExtValue()),
BB));
else
return mapValue(V, BM->addVectorExtractDynamicInst(
transValue(Ext->getVectorOperand(), BB),
transValue(Index, BB), BB));
}
if (auto *Ins = dyn_cast<InsertElementInst>(V)) {
auto *Index = Ins->getOperand(2);
if (auto *Const = dyn_cast<ConstantInt>(Index)) {
return mapValue(
V,
BM->addCompositeInsertInst(
transValue(Ins->getOperand(1), BB, true, FuncTransMode::Pointer),
transValue(Ins->getOperand(0), BB),
std::vector<SPIRVWord>(1, Const->getZExtValue()), BB));
} else
return mapValue(
V, BM->addVectorInsertDynamicInst(transValue(Ins->getOperand(0), BB),
transValue(Ins->getOperand(1), BB),
transValue(Index, BB), BB));
}
if (auto *SF = dyn_cast<ShuffleVectorInst>(V)) {
std::vector<SPIRVWord> Comp;
for (auto &I : SF->getShuffleMask())
Comp.push_back(I);
return mapValue(V, BM->addVectorShuffleInst(
transScavengedType(SF),
transValue(SF->getOperand(0), BB),
transValue(SF->getOperand(1), BB), Comp, BB));
}
if (AtomicRMWInst *ARMW = dyn_cast<AtomicRMWInst>(V)) {
AtomicRMWInst::BinOp Op = ARMW->getOperation();
bool SupportedAtomicInst =
AtomicRMWInst::isFPOperation(Op)
? (Op == AtomicRMWInst::FAdd || Op == AtomicRMWInst::FSub ||
Op == AtomicRMWInst::FMin || Op == AtomicRMWInst::FMax)
: Op != AtomicRMWInst::Nand;
if (!BM->getErrorLog().checkError(
SupportedAtomicInst, SPIRVEC_InvalidInstruction, V,
"Atomic " + AtomicRMWInst::getOperationName(Op).str() +
" is not supported in SPIR-V!\n"))
return nullptr;
AtomicOrderingCABI Ordering = llvm::toCABI(ARMW->getOrdering());
auto MemSem = OCLMemOrderMap::map(static_cast<OCLMemOrderKind>(Ordering));
std::vector<Value *> Operands(4);
Operands[0] = ARMW->getPointerOperand();
// To get the memory scope argument we use ARMW->getSyncScopeID(), but
// atomicrmw LLVM instruction is not aware of OpenCL(or SPIR-V) memory scope
// enumeration. If the scope is not set and assuming the produced SPIR-V
// module will be consumed in an OpenCL environment, we can use the same
// memory scope as OpenCL atomic functions that don't have memory_scope
// argument i.e. memory_scope_device. See the OpenCL C specification
// p6.13.11. "Atomic Functions"
SmallVector<StringRef> SSIDs;
ARMW->getContext().getSyncScopeNames(SSIDs);
spv::Scope S;
// Fill unknown syncscope value to default Device scope.
if (!OCLStrMemScopeMap::find(SSIDs[ARMW->getSyncScopeID()].str(), &S)) {
S = ScopeDevice;
}
Operands[1] = getUInt32(M, S);
Operands[2] = getUInt32(M, MemSem);
Operands[3] = ARMW->getValOperand();
std::vector<SPIRVValue *> OpVals = transValue(Operands, BB);
std::vector<SPIRVId> Ops = BM->getIds(OpVals);
SPIRVType *Ty = transScavengedType(ARMW);
spv::Op OC;
if (Op == AtomicRMWInst::FSub) {
// Implement FSub through FNegate and AtomicFAddExt
Ops[3] = BM->addUnaryInst(OpFNegate, Ty, OpVals[3], BB)->getId();
OC = OpAtomicFAddEXT;
} else
OC = LLVMSPIRVAtomicRmwOpCodeMap::map(Op);
return mapValue(V, BM->addInstTemplate(OC, Ops, BB, Ty));
}
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(V)) {
SPIRVValue *BV = transIntrinsicInst(II, BB);
return BV ? mapValue(V, BV) : nullptr;
}
if (FenceInst *FI = dyn_cast<FenceInst>(V)) {
SPIRVValue *BV = transFenceInst(FI, BB);
return BV ? mapValue(V, BV) : nullptr;
}
if (InlineAsm *IA = dyn_cast<InlineAsm>(V))
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_inline_assembly))
return mapValue(V, transAsmINTEL(IA));
if (CallInst *CI = dyn_cast<CallInst>(V)) {
if (auto *Alias =
dyn_cast_or_null<llvm::GlobalAlias>(CI->getCalledOperand())) {
CI->setCalledFunction(cast<Function>(Alias->getAliasee()));
}
return mapValue(V, transCallInst(CI, BB));
}
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
BM->SPIRVCK(false, InvalidInstruction, toString(Inst));
}
llvm_unreachable("Not implemented");
return nullptr;
}
SPIRVType *LLVMToSPIRVBase::mapType(Type *T, SPIRVType *BT) {
assert(!T->isPointerTy() && "Pointer types cannot be stored in the type map");
auto EmplaceStatus = TypeMap.try_emplace(T, BT);
// TODO: Uncomment the assertion, once the type mapping issue is resolved
// assert(EmplaceStatus.second && "The type was already added to the map");
SPIRVDBG(dbgs() << "[mapType] " << *T << " => "; spvdbgs() << *BT << '\n');
if (!EmplaceStatus.second)
return TypeMap[T];
return BT;
}
SPIRVValue *LLVMToSPIRVBase::mapValue(Value *V, SPIRVValue *BV) {
auto Loc = ValueMap.find(V);
if (Loc != ValueMap.end()) {
if (Loc->second == BV)
return BV;
assert(Loc->second->isForward() &&
"LLVM Value is mapped to different SPIRV Values");
auto *Forward = static_cast<SPIRVForward *>(Loc->second);
BM->replaceForward(Forward, BV);
}
ValueMap[V] = BV;
SPIRVDBG(dbgs() << "[mapValue] " << *V << " => "; spvdbgs() << BV << "\n");
return BV;
}
bool LLVMToSPIRVBase::shouldTryToAddMemAliasingDecoration(Instruction *Inst) {
// Limit translation of aliasing metadata with only this set of instructions
// gracefully considering others as compilation mistakes and ignoring them
if (!Inst->mayReadOrWriteMemory())
return false;
// Loads and Stores are handled during memory access mask addition
if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
return false;
CallInst *CI = dyn_cast<CallInst>(Inst);
if (!CI)
return true;
if (Function *Fun = CI->getCalledFunction()) {
// Calls to intrinsics are skipped. At some point lifetime start/end will be
// handled separately, but specification isn't ready.
if (Fun->isIntrinsic())
return false;
// Also skip SPIR-V instructions that don't have result id to attach the
// decorations
if (isBuiltinTransToInst(Fun))
if (Fun->getReturnType()->isVoidTy())
return false;
}
return true;
}
void addFuncPointerCallArgumentAttributes(CallInst *CI,
SPIRVValue *FuncPtrCall) {
for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) {
for (const auto &I : CI->getAttributes().getParamAttrs(ArgNo)) {
spv::FunctionParameterAttribute Attr = spv::FunctionParameterAttributeMax;
SPIRSPIRVFuncParamAttrMap::find(I.getKindAsEnum(), &Attr);
if (Attr != spv::FunctionParameterAttributeMax)
FuncPtrCall->addDecorate(
new SPIRVDecorate(spv::internal::DecorationArgumentAttributeINTEL,
FuncPtrCall, ArgNo, Attr));
}
}
}
#define ONE_STRING_DECORATION_CASE(NAME, NAMESPACE) \
case NAMESPACE::Decoration##NAME: { \
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule, \
#NAME " requires exactly 1 extra operand"); \
auto *StrDecoEO = dyn_cast<MDString>(DecoMD->getOperand(1)); \
ErrLog.checkError(StrDecoEO, SPIRVEC_InvalidLlvmModule, \
#NAME " requires extra operand to be a string"); \
Target->addDecorate( \
new SPIRVDecorate##NAME##Attr(Target, StrDecoEO->getString().str())); \
break; \
}
#define ONE_INT_DECORATION_CASE(NAME, NAMESPACE, TYPE) \
case NAMESPACE::Decoration##NAME: { \
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule, \
#NAME " requires exactly 1 extra operand"); \
auto *IntDecoEO = \
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1)); \
ErrLog.checkError(IntDecoEO, SPIRVEC_InvalidLlvmModule, \
#NAME " requires extra operand to be an integer"); \
Target->addDecorate(new SPIRVDecorate##NAME( \
Target, static_cast<TYPE>(IntDecoEO->getZExtValue()))); \
break; \
}
#define TWO_INT_DECORATION_CASE(NAME, NAMESPACE, TYPE1, TYPE2) \
case NAMESPACE::Decoration##NAME: { \
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule, \
#NAME " requires exactly 2 extra operands"); \
auto *IntDecoEO1 = \
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1)); \
ErrLog.checkError(IntDecoEO1, SPIRVEC_InvalidLlvmModule, \
#NAME " requires first extra operand to be an integer"); \
auto *IntDecoEO2 = \
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2)); \
ErrLog.checkError(IntDecoEO2, SPIRVEC_InvalidLlvmModule, \
#NAME \
" requires second extra operand to be an integer"); \
Target->addDecorate(new SPIRVDecorate##NAME( \
Target, static_cast<TYPE1>(IntDecoEO1->getZExtValue()), \
static_cast<TYPE2>(IntDecoEO2->getZExtValue()))); \
break; \
}
void checkIsGlobalVar(SPIRVEntry *E, Decoration Dec) {
std::string ErrStr =
SPIRVDecorationNameMap::map(Dec) + " can only be applied to a variable";
E->getErrorLog().checkError(E->isVariable(), SPIRVEC_InvalidModule, ErrStr);
auto AddrSpace = SPIRSPIRVAddrSpaceMap::rmap(
static_cast<SPIRVVariable *>(E)->getStorageClass());
ErrStr += " in a global (module) scope";
E->getErrorLog().checkError(AddrSpace == SPIRAS_Global, SPIRVEC_InvalidModule,
ErrStr);
}
static void transMetadataDecorations(Metadata *MD, SPIRVValue *Target) {
SPIRVErrorLog &ErrLog = Target->getErrorLog();
auto *ArgDecoMD = dyn_cast<MDNode>(MD);
assert(ArgDecoMD && "Decoration list must be a metadata node");
for (unsigned I = 0, E = ArgDecoMD->getNumOperands(); I != E; ++I) {
auto *DecoMD = dyn_cast<MDNode>(ArgDecoMD->getOperand(I));
ErrLog.checkError(DecoMD, SPIRVEC_InvalidLlvmModule,
"Decoration does not name metadata");
ErrLog.checkError(DecoMD->getNumOperands() > 0, SPIRVEC_InvalidLlvmModule,
"Decoration metadata must have at least one operand");
auto *DecoKindConst =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(0));
ErrLog.checkError(DecoKindConst, SPIRVEC_InvalidLlvmModule,
"First operand of decoration must be the kind");
auto DecoKind = static_cast<Decoration>(DecoKindConst->getZExtValue());
const size_t NumOperands = DecoMD->getNumOperands();
switch (static_cast<size_t>(DecoKind)) {
case DecorationAlignment: {
// Handle Alignment via SPIRVValue::setAlignment() to avoid duplicate
// Alignment decorations.
auto *Alignment =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(Alignment, SPIRVEC_InvalidLlvmModule,
"Alignment operand must be an integer.");
Target->setAlignment(Alignment->getZExtValue());
break;
}
ONE_STRING_DECORATION_CASE(MemoryINTEL, spv)
ONE_STRING_DECORATION_CASE(UserSemantic, spv)
ONE_INT_DECORATION_CASE(AliasScopeINTEL, spv, SPIRVId)
ONE_INT_DECORATION_CASE(NoAliasINTEL, spv, SPIRVId)
ONE_INT_DECORATION_CASE(InitiationIntervalINTEL, spv, SPIRVWord)
ONE_INT_DECORATION_CASE(MaxConcurrencyINTEL, spv, SPIRVWord)
ONE_INT_DECORATION_CASE(PipelineEnableINTEL, spv, SPIRVWord)
TWO_INT_DECORATION_CASE(FunctionRoundingModeINTEL, spv, SPIRVWord,
FPRoundingMode);
TWO_INT_DECORATION_CASE(FunctionDenormModeINTEL, spv, SPIRVWord,
FPDenormMode);
TWO_INT_DECORATION_CASE(FunctionFloatingPointModeINTEL, spv, SPIRVWord,
FPOperationMode);
TWO_INT_DECORATION_CASE(FuseLoopsInFunctionINTEL, spv, SPIRVWord,
SPIRVWord);
TWO_INT_DECORATION_CASE(MathOpDSPModeINTEL, spv, SPIRVWord, SPIRVWord);
case DecorationConduitKernelArgumentINTEL:
case DecorationRegisterMapKernelArgumentINTEL:
case DecorationStableKernelArgumentINTEL:
case DecorationRestrict: {
Target->addDecorate(new SPIRVDecorate(DecoKind, Target));
break;
}
case DecorationBufferLocationINTEL:
case DecorationMMHostInterfaceReadWriteModeINTEL:
case DecorationMMHostInterfaceAddressWidthINTEL:
case DecorationMMHostInterfaceDataWidthINTEL:
case DecorationMMHostInterfaceLatencyINTEL:
case DecorationMMHostInterfaceMaxBurstINTEL:
case DecorationMMHostInterfaceWaitRequestINTEL: {
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule,
"MMHost Kernel Argument Annotation requires exactly 2 "
"extra operands");
auto *DecoValEO1 =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
Target->addDecorate(
new SPIRVDecorate(DecoKind, Target, DecoValEO1->getZExtValue()));
break;
}
case DecorationStallEnableINTEL: {
Target->addDecorate(new SPIRVDecorateStallEnableINTEL(Target));
break;
}
case DecorationStallFreeINTEL: {
Target->addDecorate(new SPIRVDecorateStallFreeINTEL(Target));
break;
}
case DecorationMergeINTEL: {
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"MergeINTEL requires exactly 3 extra operands");
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(1));
ErrLog.checkError(
Name, SPIRVEC_InvalidLlvmModule,
"MergeINTEL requires first extra operand to be a string");
auto *Direction = dyn_cast<MDString>(DecoMD->getOperand(2));
ErrLog.checkError(
Direction, SPIRVEC_InvalidLlvmModule,
"MergeINTEL requires second extra operand to be a string");
Target->addDecorate(new SPIRVDecorateMergeINTELAttr(
Target, Name->getString().str(), Direction->getString().str()));
break;
}
case DecorationLinkageAttributes: {
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"LinkageAttributes requires exactly 3 extra operands");
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(1));
ErrLog.checkError(
Name, SPIRVEC_InvalidLlvmModule,
"LinkageAttributes requires first extra operand to be a string");
auto *Type = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
ErrLog.checkError(
Type, SPIRVEC_InvalidLlvmModule,
"LinkageAttributes requires second extra operand to be an int");
auto TypeKind = static_cast<SPIRVLinkageTypeKind>(Type->getZExtValue());
Target->addDecorate(new SPIRVDecorateLinkageAttr(
Target, Name->getString().str(), TypeKind));
break;
}
case spv::internal::DecorationHostAccessINTEL:
case DecorationHostAccessINTEL: {
checkIsGlobalVar(Target, DecoKind);
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"HostAccessINTEL requires exactly 2 extra operands "
"after the decoration kind number");
auto *AccessMode =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(
AccessMode, SPIRVEC_InvalidLlvmModule,
"HostAccessINTEL requires first extra operand to be an int");
HostAccessQualifier Q =
static_cast<HostAccessQualifier>(AccessMode->getZExtValue());
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(2));
ErrLog.checkError(
Name, SPIRVEC_InvalidLlvmModule,
"HostAccessINTEL requires second extra operand to be a string");
if (DecoKind == DecorationHostAccessINTEL)
Target->addDecorate(new SPIRVDecorateHostAccessINTEL(
Target, Q, Name->getString().str()));
else
Target->addDecorate(new SPIRVDecorateHostAccessINTELLegacy(
Target, Q, Name->getString().str()));
break;
}
case spv::internal::DecorationInitModeINTEL:
case DecorationInitModeINTEL: {
checkIsGlobalVar(Target, DecoKind);
ErrLog.checkError(static_cast<SPIRVVariable *>(Target)->getInitializer(),
SPIRVEC_InvalidLlvmModule,
"InitModeINTEL only be applied to a global (module "
"scope) variable which has an Initializer operand");
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule,
"InitModeINTEL requires exactly 1 extra operand");
auto *Trigger = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(Trigger, SPIRVEC_InvalidLlvmModule,
"InitModeINTEL requires extra operand to be an int");
InitializationModeQualifier Q =
static_cast<InitializationModeQualifier>(Trigger->getZExtValue());
if (DecoKind == DecorationInitModeINTEL)
Target->addDecorate(new SPIRVDecorateInitModeINTEL(Target, Q));
else
Target->addDecorate(new SPIRVDecorateInitModeINTELLegacy(Target, Q));
break;
}
case spv::internal::DecorationImplementInCSRINTEL: {
checkIsGlobalVar(Target, DecoKind);
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule,
"ImplementInCSRINTEL requires exactly 1 extra operand");
auto *Value = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(
Value, SPIRVEC_InvalidLlvmModule,
"ImplementInCSRINTEL requires extra operand to be an integer");
Target->addDecorate(
new SPIRVDecorateImplementInCSRINTEL(Target, Value->getZExtValue()));
break;
}
case DecorationImplementInRegisterMapINTEL: {
checkIsGlobalVar(Target, DecoKind);
ErrLog.checkError(
NumOperands == 2, SPIRVEC_InvalidLlvmModule,
"ImplementInRegisterMapINTEL requires exactly 1 extra operand");
auto *Value = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(Value, SPIRVEC_InvalidLlvmModule,
"ImplementInRegisterMapINTEL requires extra operand to "
"be an integer");
Target->addDecorate(new SPIRVDecorateImplementInRegisterMapINTEL(
Target, Value->getZExtValue()));
break;
}
case DecorationCacheControlLoadINTEL: {
ErrLog.checkError(
NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"CacheControlLoadINTEL requires exactly 2 extra operands");
auto *CacheLevel =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
auto *CacheControl =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
ErrLog.checkError(CacheLevel, SPIRVEC_InvalidLlvmModule,
"CacheControlLoadINTEL cache level operand is required "
"to be an integer");
ErrLog.checkError(CacheControl, SPIRVEC_InvalidLlvmModule,
"CacheControlLoadINTEL cache control operand is "
"required to be an integer");
Target->addDecorate(new SPIRVDecorateCacheControlLoadINTEL(
Target, CacheLevel->getZExtValue(),
static_cast<LoadCacheControl>(CacheControl->getZExtValue())));
break;
}
case DecorationCacheControlStoreINTEL: {
ErrLog.checkError(
NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"CacheControlStoreINTEL requires exactly 2 extra operands");
auto *CacheLevel =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
auto *CacheControl =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
ErrLog.checkError(CacheLevel, SPIRVEC_InvalidLlvmModule,
"CacheControlStoreINTEL cache level operand is "
"required to be an integer");
ErrLog.checkError(CacheControl, SPIRVEC_InvalidLlvmModule,
"CacheControlStoreINTEL cache control operand is "
"required to be an integer");
Target->addDecorate(new SPIRVDecorateCacheControlStoreINTEL(
Target, CacheLevel->getZExtValue(),
static_cast<StoreCacheControl>(CacheControl->getZExtValue())));
break;
}
default: {
if (NumOperands == 1) {
Target->addDecorate(new SPIRVDecorate(DecoKind, Target));
break;
}
auto *DecoValEO1 =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
ErrLog.checkError(
DecoValEO1, SPIRVEC_InvalidLlvmModule,
"First extra operand in default decoration case must be integer.");
if (NumOperands == 2) {
Target->addDecorate(
new SPIRVDecorate(DecoKind, Target, DecoValEO1->getZExtValue()));
break;
}
auto *DecoValEO2 =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
ErrLog.checkError(
DecoValEO2, SPIRVEC_InvalidLlvmModule,
"Second extra operand in default decoration case must be integer.");
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
"At most 2 extra operands expected.");
Target->addDecorate(new SPIRVDecorate(DecoKind, Target,
DecoValEO1->getZExtValue(),
DecoValEO2->getZExtValue()));
}
}
}
}
#undef ONE_STRING_DECORATION_CASE
#undef ONE_INT_DECORATION_CASE
#undef TWO_INT_DECORATION_CASE
bool LLVMToSPIRVBase::transDecoration(Value *V, SPIRVValue *BV) {
if (!transAlign(V, BV))
return false;
if ((isa<AtomicCmpXchgInst>(V) && cast<AtomicCmpXchgInst>(V)->isVolatile()) ||
(isa<AtomicRMWInst>(V) && cast<AtomicRMWInst>(V)->isVolatile()))
BV->setVolatile(true);
if (auto *BVO = dyn_cast_or_null<OverflowingBinaryOperator>(V)) {
if (BVO->hasNoSignedWrap()) {
BV->setNoIntegerDecorationWrap<DecorationNoSignedWrap>(true);
}
if (BVO->hasNoUnsignedWrap()) {
BV->setNoIntegerDecorationWrap<DecorationNoUnsignedWrap>(true);
}
}
if (auto *BVF = dyn_cast_or_null<FPMathOperator>(V)) {
auto Opcode = BVF->getOpcode();
if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub ||
Opcode == Instruction::FMul || Opcode == Instruction::FDiv ||
Opcode == Instruction::FRem ||
((Opcode == Instruction::FNeg || Opcode == Instruction::FCmp ||
BV->isExtInst()) &&
BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_6))) {
FastMathFlags FMF = BVF->getFastMathFlags();
SPIRVWord M{0};
if (FMF.isFast())
M |= FPFastMathModeFastMask;
else {
if (FMF.noNaNs())
M |= FPFastMathModeNotNaNMask;
if (FMF.noInfs())
M |= FPFastMathModeNotInfMask;
if (FMF.noSignedZeros())
M |= FPFastMathModeNSZMask;
if (FMF.allowReciprocal())
M |= FPFastMathModeAllowRecipMask;
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fp_fast_math_mode)) {
if (FMF.allowContract()) {
M |= FPFastMathModeAllowContractFastINTELMask;
BM->addCapability(CapabilityFPFastMathModeINTEL);
BM->addExtension(ExtensionID::SPV_INTEL_fp_fast_math_mode);
}
if (FMF.allowReassoc()) {
M |= FPFastMathModeAllowReassocINTELMask;
BM->addCapability(CapabilityFPFastMathModeINTEL);
BM->addExtension(ExtensionID::SPV_INTEL_fp_fast_math_mode);
}
}
}
// Handle nofpclass attribute. Nothing to do if fast math flag is already
// set.
if ((BV->isExtInst() &&
static_cast<SPIRVExtInst *>(BV)->getExtSetKind() ==
SPIRVEIS_OpenCL) &&
BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_6) &&
!(M & FPFastMathModeFastMask)) {
auto *F = cast<CallInst>(V)->getCalledFunction();
auto FAttrs = F->getAttributes();
AttributeSet RetAttrs = FAttrs.getRetAttrs();
if (RetAttrs.hasAttribute(Attribute::NoFPClass)) {
FPClassTest RetTest =
RetAttrs.getAttribute(Attribute::NoFPClass).getNoFPClass();
AttributeSet RetAttrs = FAttrs.getRetAttrs();
// Only Nan and Inf tests are representable in SPIR-V now.
bool ToAddNoNan = RetTest & fcNan;
bool ToAddNoInf = RetTest & fcInf;
if (ToAddNoNan || ToAddNoInf) {
const auto *FT = F->getFunctionType();
const size_t NumParams = FT->getNumParams();
for (size_t I = 0; I != NumParams; ++I) {
if (!FT->getParamType(I)->isFloatTy())
continue;
if (!F->hasParamAttribute(I, Attribute::NoFPClass)) {
ToAddNoNan = false;
ToAddNoInf = false;
break;
}
FPClassTest ArgTest =
FAttrs.getParamAttr(I, Attribute::NoFPClass).getNoFPClass();
ToAddNoNan = ToAddNoNan && static_cast<bool>(ArgTest & fcNan);
ToAddNoInf = ToAddNoInf && static_cast<bool>(ArgTest & fcInf);
}
}
if (ToAddNoNan)
M |= FPFastMathModeNotNaNMask;
if (ToAddNoInf)
M |= FPFastMathModeNotInfMask;
}
}
if (M != 0) {
BV->setFPFastMathMode(M);
if (Opcode == Instruction::FNeg || Opcode == Instruction::FCmp ||
BV->isExtInst())
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_6);
}
}
}
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
if (shouldTryToAddMemAliasingDecoration(Inst))
transMemAliasingINTELDecorations(Inst, BV);
if (auto *IDecoMD = Inst->getMetadata(SPIRV_MD_DECORATIONS))
transMetadataDecorations(IDecoMD, BV);
if (BV->isInst())
addFPBuiltinDecoration(BM, Inst, static_cast<SPIRVInstruction *>(BV));
}
if (auto *CI = dyn_cast<CallInst>(V)) {
auto OC = BV->getOpCode();
if (OC == OpSpecConstantTrue || OC == OpSpecConstantFalse ||
OC == OpSpecConstant) {
auto SpecId = cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue();
BV->addDecorate(DecorationSpecId, SpecId);
}
if (OC == OpFunctionPointerCallINTEL)
addFuncPointerCallArgumentAttributes(CI, BV);
}
if (auto *GV = dyn_cast<GlobalVariable>(V))
if (auto *GVDecoMD = GV->getMetadata(SPIRV_MD_DECORATIONS))
transMetadataDecorations(GVDecoMD, BV);
return true;
}
bool LLVMToSPIRVBase::transAlign(Value *V, SPIRVValue *BV) {
if (auto *AL = dyn_cast<AllocaInst>(V)) {
BM->setAlignment(BV, AL->getAlign().value());
return true;
}
if (auto *GV = dyn_cast<GlobalVariable>(V)) {
BM->setAlignment(BV, GV->getAlignment());
return true;
}
return true;
}
// Apply aliasing decorations to instructions annotated with aliasing metadata.
// Do it for any instruction but loads and stores.
void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Instruction *Inst,
SPIRVValue *BV) {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_memory_access_aliasing))
return;
if (MDNode *AliasingListMD =
Inst->getMetadata(LLVMContext::MD_alias_scope)) {
auto *MemAliasList =
addMemAliasingINTELInstructions(BM, AliasingListMD);
if (!MemAliasList)
return;
BV->addDecorate(new SPIRVDecorateId(DecorationAliasScopeINTEL, BV,
MemAliasList->getId()));
}
if (MDNode *AliasingListMD = Inst->getMetadata(LLVMContext::MD_noalias)) {
auto *MemAliasList =
addMemAliasingINTELInstructions(BM, AliasingListMD);
if (!MemAliasList)
return;
BV->addDecorate(
new SPIRVDecorateId(DecorationNoAliasINTEL, BV, MemAliasList->getId()));
}
}
/// Do this after source language is set.
bool LLVMToSPIRVBase::transBuiltinSet() {
SPIRVId EISId;
if (!BM->importBuiltinSet("OpenCL.std", &EISId))
return false;
if (SPIRVMDWalker(*M).getNamedMD("llvm.dbg.cu")) {
if (!BM->importBuiltinSet(
SPIRVBuiltinSetNameMap::map(BM->getDebugInfoEIS()), &EISId))
return false;
}
if (BM->preserveAuxData()) {
if (!BM->importBuiltinSet(
SPIRVBuiltinSetNameMap::map(SPIRVEIS_NonSemantic_AuxData), &EISId))
return false;
}
return true;
}
/// Translate sampler* spcv.cast(i32 arg) or
/// sampler* __translate_sampler_initializer(i32 arg)
/// Three cases are possible:
/// arg = ConstantInt x -> SPIRVConstantSampler
/// arg = i32 argument -> transValue(arg)
/// arg = load from sampler -> look through load
SPIRVValue *LLVMToSPIRVBase::oclTransSpvcCastSampler(CallInst *CI,
SPIRVBasicBlock *BB) {
assert(CI->getCalledFunction() && "Unexpected indirect call");
auto *Arg = CI->getArgOperand(0);
auto *TransRT = transType(getSPIRVType(OpTypeSampler));
auto GetSamplerConstant = [&](uint64_t SamplerValue) {
auto AddrMode = (SamplerValue & 0xE) >> 1;
auto Param = SamplerValue & 0x1;
auto Filter = SamplerValue ? ((SamplerValue & 0x30) >> 4) - 1 : 0;
auto *BV = BM->addSamplerConstant(TransRT, AddrMode, Param, Filter);
return BV;
};
if (auto *Const = dyn_cast<ConstantInt>(Arg)) {
// Sampler is declared as a kernel scope constant
return GetSamplerConstant(Const->getZExtValue());
} else if (auto *Load = dyn_cast<LoadInst>(Arg)) {
// If value of the sampler is loaded from a global constant, use its
// initializer for initialization of the sampler.
auto *Op = Load->getPointerOperand();
assert(isa<GlobalVariable>(Op) && "Unknown sampler pattern!");
auto *GV = cast<GlobalVariable>(Op);
assert(GV->isConstant() ||
GV->getType()->getPointerAddressSpace() == SPIRAS_Constant);
auto *Initializer = GV->getInitializer();
assert(isa<ConstantInt>(Initializer) && "sampler not constant int?");
return GetSamplerConstant(cast<ConstantInt>(Initializer)->getZExtValue());
}
// Sampler is a function argument
auto *BV = transValue(Arg, BB);
assert(BV && BV->getType() == TransRT);
return BV;
}
using DecorationsInfoVec =
std::vector<std::pair<Decoration, std::vector<std::string>>>;
struct AnnotationDecorations {
DecorationsInfoVec MemoryAttributesVec;
DecorationsInfoVec MemoryAccessesVec;
DecorationsInfoVec BufferLocationVec;
DecorationsInfoVec LatencyControlVec;
DecorationsInfoVec CacheControlVec;
bool empty() {
return (MemoryAttributesVec.empty() && MemoryAccessesVec.empty() &&
BufferLocationVec.empty() && LatencyControlVec.empty() &&
CacheControlVec.empty());
}
};
struct IntelLSUControlsInfo {
void setWithBitMask(unsigned ParamsBitMask) {
if (ParamsBitMask & IntelFPGAMemoryAccessesVal::BurstCoalesce)
BurstCoalesce = true;
if (ParamsBitMask & IntelFPGAMemoryAccessesVal::CacheSizeFlag)
CacheSizeInfo = 0;
if (ParamsBitMask & IntelFPGAMemoryAccessesVal::DontStaticallyCoalesce)
DontStaticallyCoalesce = true;
if (ParamsBitMask & IntelFPGAMemoryAccessesVal::PrefetchFlag)
PrefetchInfo = 0;
}
DecorationsInfoVec getDecorationsFromCurrentState() {
DecorationsInfoVec ResultVec;
// Simple flags
if (BurstCoalesce)
ResultVec.emplace_back(DecorationBurstCoalesceINTEL,
std::vector<std::string>());
if (DontStaticallyCoalesce)
ResultVec.emplace_back(DecorationDontStaticallyCoalesceINTEL,
std::vector<std::string>());
// Conditional values
if (CacheSizeInfo.has_value()) {
ResultVec.emplace_back(
DecorationCacheSizeINTEL,
std::vector<std::string>{std::to_string(CacheSizeInfo.value())});
}
if (PrefetchInfo.has_value()) {
ResultVec.emplace_back(
DecorationPrefetchINTEL,
std::vector<std::string>{std::to_string(PrefetchInfo.value())});
}
return ResultVec;
}
bool BurstCoalesce = false;
std::optional<unsigned> CacheSizeInfo;
bool DontStaticallyCoalesce = false;
std::optional<unsigned> PrefetchInfo;
};
// Handle optional var/ptr/global annotation parameter. It can be for example
// { %struct.S, i8*, void ()* } { %struct.S undef, i8* null,
// void ()* @_Z4blahv }
// Now we will just handle integer constants (wrapped in a constant
// struct, that is being bitcasted to i8*), converting them to string.
// TODO: remove this workaround when/if an extension spec that allows or adds
// variadic-arguments UserSemantic decoration
void processOptionalAnnotationInfo(Constant *Const,
std::string &AnnotationString) {
if (!Const->getNumOperands())
return;
if (auto *CStruct = dyn_cast<ConstantStruct>(Const->getOperand(0))) {
uint32_t NumOperands = CStruct->getNumOperands();
if (!NumOperands)
return;
if (auto *CInt = dyn_cast<ConstantInt>(CStruct->getOperand(0))) {
AnnotationString += ": ";
// For boolean, emit 0/1 for ease of readability.
if (CInt->getType()->getIntegerBitWidth() == 1)
AnnotationString += std::to_string(CInt->getZExtValue());
else
AnnotationString += std::to_string(CInt->getSExtValue());
}
for (uint32_t I = 1; I != NumOperands; ++I) {
if (auto *CInt = dyn_cast<ConstantInt>(CStruct->getOperand(I))) {
AnnotationString += ", ";
AnnotationString += std::to_string(CInt->getSExtValue());
}
}
} else if (auto *ZeroStruct =
dyn_cast<ConstantAggregateZero>(Const->getOperand(0))) {
// It covers case when all elements of struct are 0 and they become
// zeroinitializer. It represents like: { i32 i32 ... } zeroinitializer
uint32_t NumOperands = ZeroStruct->getType()->getStructNumElements();
AnnotationString += ": ";
AnnotationString += "0";
for (uint32_t I = 1; I != NumOperands; ++I) {
AnnotationString += ", ";
AnnotationString += "0";
}
}
}
// Process main var/ptr/global annotation string with the attached optional
// integer parameters
void processAnnotationString(IntrinsicInst *II, std::string &AnnotationString) {
auto *StrVal = II->getArgOperand(1);
auto *StrValTy = StrVal->getType();
if (StrValTy->isPointerTy()) {
StringRef StrRef;
if (getConstantStringInfo(dyn_cast<Constant>(StrVal), StrRef))
AnnotationString += StrRef.str();
if (auto *C = dyn_cast_or_null<Constant>(II->getArgOperand(4)))
processOptionalAnnotationInfo(C, AnnotationString);
return;
}
if (auto *GEP = dyn_cast<GetElementPtrInst>(StrVal)) {
if (auto *C = dyn_cast<Constant>(GEP->getOperand(0))) {
StringRef StrRef;
if (getConstantStringInfo(C, StrRef))
AnnotationString += StrRef.str();
}
}
if (auto *Cast = dyn_cast<BitCastInst>(II->getArgOperand(4)))
if (auto *C = dyn_cast_or_null<Constant>(Cast->getOperand(0)))
processOptionalAnnotationInfo(C, AnnotationString);
}
// Try to parse the annotation decoration values in a string. These values must
// be separated by a "," and must be either a word (including numbers) or a
// quotation mark enclosed string.
static bool tryParseAnnotationDecoValues(StringRef ValueStr,
std::vector<std::string> &ParsedArgs) {
unsigned ValueStart = 0;
bool IsParsingStringLiteral = false;
for (unsigned I = 0; I < ValueStr.size(); ++I) {
const char CurrentC = ValueStr[I];
if (IsParsingStringLiteral) {
if (CurrentC == '"') {
// We have reached the end of a string literal and have the arg string
// between this character and the start of the string literal.
IsParsingStringLiteral = false;
ParsedArgs.push_back(ValueStr.substr(ValueStart, I - ValueStart).str());
// End of a string literal must either be at the end of the values or
// right before a comma.
if (I + 1 != ValueStr.size() && ValueStr[I + 1] != ',')
return false;
// Skip the , delimiter and go directly to the start of next value.
ValueStart = (++I) + 1;
continue;
}
}
if (CurrentC == ',') {
// Since we are not currently in a string literal, comma denotes a
// separation of decoration arguments and we can copy the substring we are
// currently parsing.
ParsedArgs.push_back(ValueStr.substr(ValueStart, I - ValueStart).str());
ValueStart = I + 1;
continue;
}
if (CurrentC == '"') {
// We are entering a string literal. This must be either at the beginning
// of the values or right after a comma.
if (I != 0 && ValueStr[I - 1] != ',')
return false;
IsParsingStringLiteral = true;
ValueStart = I + 1;
continue;
}
// Any other character will be consumed as part of the argument.
}
// If we were still parsing a decoration argument when reaching the end of the
// parsed string, we must be at the end of the argument.
if (ValueStart < ValueStr.size())
ParsedArgs.push_back(
ValueStr.substr(ValueStart, ValueStr.size() - ValueStart).str());
// At the end, the arguments parsed are valid if we were not parsing a string
// literal with no end.
return !IsParsingStringLiteral;
}
AnnotationDecorations tryParseAnnotationString(SPIRVModule *BM,
StringRef AnnotatedCode) {
AnnotationDecorations Decorates;
// Annotation string decorations are separated into {word} OR
// {word:value,value,...} blocks, where value is either a word (including
// numbers) or a quotation mark enclosed string.
std::regex DecorationRegex("\\{\\w([\\w:,-]|\"[^\"]*\")*\\}");
using RegexIterT = std::regex_iterator<StringRef::const_iterator>;
RegexIterT DecorationsIt(AnnotatedCode.begin(), AnnotatedCode.end(),
DecorationRegex);
RegexIterT DecorationsEnd;
// If we didn't find any annotations that are separated as described above,
// then add a UserSemantic decoration
if (DecorationsIt == DecorationsEnd) {
Decorates.MemoryAttributesVec.emplace_back(
DecorationUserSemantic, std::vector<std::string>{AnnotatedCode.str()});
return Decorates;
}
const bool AllowFPGAMemAccesses =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_memory_accesses);
const bool AllowFPGAMemAttr = BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes);
const bool AllowFPGABufLoc =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_buffer_location);
const bool AllowFPGALatencyControl =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_latency_control);
const bool AllowCacheControls =
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_cache_controls);
bool ValidDecorationFound = false;
DecorationsInfoVec DecorationsVec;
IntelLSUControlsInfo LSUControls;
for (; DecorationsIt != DecorationsEnd; ++DecorationsIt) {
// Drop the braces surrounding the actual decoration
const StringRef AnnotatedDecoration = AnnotatedCode.substr(
DecorationsIt->position() + 1, DecorationsIt->length() - 2);
std::pair<StringRef, StringRef> Split = AnnotatedDecoration.split(':');
StringRef Name = Split.first, ValueStr = Split.second;
SPIRVDBG(spvdbgs() << "[tryParseAnnotationString]: AnnotationString: "
<< Name.str() << "\n");
unsigned DecorationKind = 0;
if (!Name.getAsInteger(10, DecorationKind)) {
// If the name is a number it represents the decoration by its kind.
std::vector<std::string> DecValues;
if (tryParseAnnotationDecoValues(ValueStr, DecValues)) {
ValidDecorationFound = true;
if (AllowFPGABufLoc &&
DecorationKind == DecorationBufferLocationINTEL) {
Decorates.BufferLocationVec.emplace_back(
static_cast<Decoration>(DecorationKind), std::move(DecValues));
} else if (AllowFPGALatencyControl &&
(DecorationKind == DecorationLatencyControlLabelINTEL ||
DecorationKind ==
DecorationLatencyControlConstraintINTEL)) {
Decorates.LatencyControlVec.emplace_back(
static_cast<Decoration>(DecorationKind), std::move(DecValues));
} else if (AllowCacheControls &&
DecorationKind == DecorationCacheControlLoadINTEL) {
Decorates.CacheControlVec.emplace_back(
static_cast<Decoration>(DecorationKind), std::move(DecValues));
} else if (DecorationKind == DecorationMemoryINTEL) {
// SPIRV doesn't allow the same Decoration to be applied multiple
// times on a single SPIRVEntry, unless explicitly allowed by the
// language spec. Filter out the less specific MemoryINTEL
// decorations, if applied multiple times
auto CanFilterOut = [](auto &Val) {
if (!Val.second.empty())
return (Val.second[0] == "DEFAULT");
return false;
};
auto It = std::find_if(DecorationsVec.begin(), DecorationsVec.end(),
CanFilterOut);
if (It != DecorationsVec.end()) {
// replace the less specific decoration
*It = {static_cast<Decoration>(DecorationKind),
std::move(DecValues)};
} else {
// add new decoration
DecorationsVec.emplace_back(static_cast<Decoration>(DecorationKind),
std::move(DecValues));
}
} else {
DecorationsVec.emplace_back(static_cast<Decoration>(DecorationKind),
std::move(DecValues));
}
}
continue;
}
if (AllowFPGAMemAccesses) {
if (Name == "params") {
ValidDecorationFound = true;
unsigned ParamsBitMask = 0;
bool Failure = ValueStr.getAsInteger(10, ParamsBitMask);
assert(!Failure && "Non-integer LSU controls value");
(void)Failure;
LSUControls.setWithBitMask(ParamsBitMask);
} else if (Name == "cache-size") {
ValidDecorationFound = true;
if (!LSUControls.CacheSizeInfo.has_value())
continue;
unsigned CacheSizeValue = 0;
bool Failure = ValueStr.getAsInteger(10, CacheSizeValue);
assert(!Failure && "Non-integer cache size value");
(void)Failure;
LSUControls.CacheSizeInfo = CacheSizeValue;
} // TODO: Support LSU prefetch size, which currently defaults to 0
}
if (AllowFPGAMemAttr) {
std::vector<std::string> DecValues;
Decoration Dec;
if (Name == "pump") {
ValidDecorationFound = true;
Dec = llvm::StringSwitch<Decoration>(ValueStr)
.Case("1", DecorationSinglepumpINTEL)
.Case("2", DecorationDoublepumpINTEL);
} else if (Name == "register") {
ValidDecorationFound = true;
Dec = DecorationRegisterINTEL;
} else if (Name == "simple_dual_port") {
ValidDecorationFound = true;
Dec = DecorationSimpleDualPortINTEL;
} else {
Dec = llvm::StringSwitch<Decoration>(Name)
.Case("memory", DecorationMemoryINTEL)
.Case("numbanks", DecorationNumbanksINTEL)
.Case("bankwidth", DecorationBankwidthINTEL)
.Case("private_copies", DecorationMaxPrivateCopiesINTEL)
.Case("max_replicates", DecorationMaxReplicatesINTEL)
.Case("bank_bits", DecorationBankBitsINTEL)
.Case("merge", DecorationMergeINTEL)
.Case("force_pow2_depth", DecorationForcePow2DepthINTEL)
.Case("stride_size", DecorationStridesizeINTEL)
.Case("word_size", DecorationWordsizeINTEL)
.Case("true_dual_port", DecorationTrueDualPortINTEL)
.Default(DecorationUserSemantic);
if (Dec == DecorationUserSemantic)
// Restore the braces to translate the whole input string
DecValues =
std::vector<std::string>({'{' + AnnotatedDecoration.str() + '}'});
else if (Dec == DecorationMergeINTEL) {
ValidDecorationFound = true;
std::pair<StringRef, StringRef> MergeValues = ValueStr.split(':');
DecValues = std::vector<std::string>(
{MergeValues.first.str(), MergeValues.second.str()});
} else if (Dec == DecorationBankBitsINTEL) {
ValidDecorationFound = true;
SmallVector<StringRef, 4> BitsStrs;
ValueStr.split(BitsStrs, ',');
DecValues.reserve(BitsStrs.size());
for (const StringRef &BitsStr : BitsStrs)
DecValues.push_back(BitsStr.str());
} else {
ValidDecorationFound = true;
DecValues = std::vector<std::string>({ValueStr.str()});
}
}
DecorationsVec.emplace_back(Dec, std::move(DecValues));
}
}
// Even if there is an annotation string that is split in blocks like Intel
// FPGA annotation, it's not necessarily an FPGA annotation. Translate the
// whole string as UserSemantic decoration in this case.
if (ValidDecorationFound)
Decorates.MemoryAttributesVec = DecorationsVec;
else
Decorates.MemoryAttributesVec.emplace_back(
DecorationUserSemantic,
std::vector<std::string>({AnnotatedCode.str()}));
Decorates.MemoryAccessesVec = LSUControls.getDecorationsFromCurrentState();
return Decorates;
}
std::vector<SPIRVWord>
getLiteralsFromStrings(const std::vector<std::string> &Strings) {
std::vector<SPIRVWord> Literals(Strings.size());
for (size_t J = 0; J < Strings.size(); ++J)
if (StringRef(Strings[J]).getAsInteger(10, Literals[J]))
return {};
return Literals;
}
void addAnnotationDecorations(SPIRVEntry *E, DecorationsInfoVec &Decorations) {
SPIRVModule *M = E->getModule();
for (const auto &I : Decorations) {
// Such decoration already exists on a type, try to skip it
if (E->hasDecorate(I.first, /*Index=*/0, /*Result=*/nullptr))
// Allow multiple UserSemantic Decorations
if (I.first != DecorationUserSemantic)
continue;
switch (static_cast<size_t>(I.first)) {
case DecorationUserSemantic:
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"UserSemantic requires a single argument.");
E->addDecorate(new SPIRVDecorateUserSemanticAttr(E, I.second[0]));
break;
case DecorationMemoryINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes)) {
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"MemoryINTEL requires a single argument.");
E->addDecorate(new SPIRVDecorateMemoryINTELAttr(E, I.second[0]));
}
} break;
case DecorationMergeINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes)) {
M->getErrorLog().checkError(I.second.size() == 2,
SPIRVEC_InvalidLlvmModule,
"MergeINTEL requires two arguments.");
// First argument is the name and the second argument is the direction.
E->addDecorate(
new SPIRVDecorateMergeINTELAttr(E, I.second[0], I.second[1]));
}
} break;
case DecorationBankBitsINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes)) {
M->getErrorLog().checkError(
I.second.size() > 0, SPIRVEC_InvalidLlvmModule,
"BankBitsINTEL requires at least one argument.");
E->addDecorate(new SPIRVDecorateBankBitsINTELAttr(
E, getLiteralsFromStrings(I.second)));
}
} break;
case DecorationRegisterINTEL:
case DecorationSinglepumpINTEL:
case DecorationDoublepumpINTEL:
case DecorationSimpleDualPortINTEL:
case DecorationTrueDualPortINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes)) {
M->getErrorLog().checkError(I.second.empty(), SPIRVEC_InvalidLlvmModule,
"Decoration takes no arguments.");
E->addDecorate(I.first);
}
} break;
case DecorationBurstCoalesceINTEL:
case DecorationDontStaticallyCoalesceINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_accesses)) {
M->getErrorLog().checkError(I.second.empty(), SPIRVEC_InvalidLlvmModule,
"Decoration takes no arguments.");
E->addDecorate(I.first);
}
} break;
case DecorationNumbanksINTEL:
case DecorationBankwidthINTEL:
case DecorationMaxPrivateCopiesINTEL:
case DecorationMaxReplicatesINTEL:
case DecorationForcePow2DepthINTEL:
case DecorationStridesizeINTEL:
case DecorationWordsizeINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_attributes)) {
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"Decoration requires a single argument.");
SPIRVWord Result = 0;
StringRef(I.second[0]).getAsInteger(10, Result);
E->addDecorate(I.first, Result);
}
} break;
case DecorationCacheSizeINTEL:
case DecorationPrefetchINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_memory_accesses)) {
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"Decoration requires a single argument.");
SPIRVWord Result = 0;
StringRef(I.second[0]).getAsInteger(10, Result);
E->addDecorate(I.first, Result);
}
} break;
case DecorationBufferLocationINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_buffer_location)) {
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"Decoration requires a single argument.");
SPIRVWord Result = 0;
StringRef(I.second[0]).getAsInteger(10, Result);
E->addDecorate(I.first, Result);
}
} break;
case DecorationLatencyControlLabelINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_latency_control)) {
M->getErrorLog().checkError(
I.second.size() == 1, SPIRVEC_InvalidLlvmModule,
"LatencyControlLabelINTEL requires exactly 1 extra operand");
SPIRVWord Label = 0;
StringRef(I.second[0]).getAsInteger(10, Label);
E->addDecorate(
new SPIRVDecorate(DecorationLatencyControlLabelINTEL, E, Label));
}
break;
}
case DecorationLatencyControlConstraintINTEL: {
if (M->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_fpga_latency_control)) {
M->getErrorLog().checkError(
I.second.size() == 3, SPIRVEC_InvalidLlvmModule,
"LatencyControlConstraintINTEL requires exactly 3 extra operands");
auto Literals = getLiteralsFromStrings(I.second);
E->addDecorate(
new SPIRVDecorate(DecorationLatencyControlConstraintINTEL, E,
Literals[0], Literals[1], Literals[2]));
}
break;
}
case DecorationCacheControlLoadINTEL: {
if (M->isAllowedToUseExtension(ExtensionID::SPV_INTEL_cache_controls)) {
M->getErrorLog().checkError(
I.second.size() == 2, SPIRVEC_InvalidLlvmModule,
"CacheControlLoadINTEL requires exactly 2 extra operands");
SPIRVWord CacheLevel = 0;
SPIRVWord CacheControl = 0;
StringRef(I.second[0]).getAsInteger(10, CacheLevel);
StringRef(I.second[1]).getAsInteger(10, CacheControl);
E->addDecorate(new SPIRVDecorateCacheControlLoadINTEL(
E, CacheLevel, static_cast<LoadCacheControl>(CacheControl)));
}
}
default:
// Other decorations are either not supported by the translator or
// handled in other places.
break;
}
}
}
void addAnnotationDecorationsForStructMember(SPIRVEntry *E,
SPIRVWord MemberNumber,
DecorationsInfoVec &Decorations) {
SPIRVModule *M = E->getModule();
for (const auto &I : Decorations) {
// Such decoration already exists on a type, skip it
if (E->hasMemberDecorate(I.first, /*Index=*/0, MemberNumber,
/*Result=*/nullptr))
// Allow multiple UserSemantic Decorations
if (I.first != DecorationUserSemantic)
continue;
switch (I.first) {
case DecorationUserSemantic:
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"UserSemantic requires a single argument.");
E->addMemberDecorate(new SPIRVMemberDecorateUserSemanticAttr(
E, MemberNumber, I.second[0]));
break;
case DecorationMemoryINTEL:
M->getErrorLog().checkError(I.second.size() == 1,
SPIRVEC_InvalidLlvmModule,
"MemoryINTEL requires a single argument.");
E->addMemberDecorate(
new SPIRVMemberDecorateMemoryINTELAttr(E, MemberNumber, I.second[0]));
break;
case DecorationMergeINTEL: {
M->getErrorLog().checkError(I.second.size() == 2,
SPIRVEC_InvalidLlvmModule,
"MergeINTEL requires two arguments.");
// First argument is the name, the other is the direction.
E->addMemberDecorate(new SPIRVMemberDecorateMergeINTELAttr(
E, MemberNumber, I.second[0], I.second[1]));
} break;
case DecorationBankBitsINTEL:
M->getErrorLog().checkError(
I.second.size() > 0, SPIRVEC_InvalidLlvmModule,
"BankBitsINTEL requires at least one argument.");
E->addMemberDecorate(new SPIRVMemberDecorateBankBitsINTELAttr(
E, MemberNumber, getLiteralsFromStrings(I.second)));
break;
case DecorationRegisterINTEL:
case DecorationSinglepumpINTEL:
case DecorationDoublepumpINTEL:
case DecorationSimpleDualPortINTEL:
case DecorationTrueDualPortINTEL:
M->getErrorLog().checkError(I.second.empty(), SPIRVEC_InvalidLlvmModule,
"Member decoration takes no arguments.");
E->addMemberDecorate(MemberNumber, I.first);
break;
// The rest of IntelFPGA decorations:
// DecorationNumbanksINTEL
// DecorationBankwidthINTEL
// DecorationMaxPrivateCopiesINTEL
// DecorationMaxReplicatesINTEL
// DecorationForcePow2DepthINTEL
// DecorarionStridesizeINTEL
// DecorationWordsizeINTEL
default:
M->getErrorLog().checkError(
I.second.size() == 1, SPIRVEC_InvalidLlvmModule,
"Member decoration requires a single argument.");
SPIRVWord Result = 0;
StringRef(I.second[0]).getAsInteger(10, Result);
E->addMemberDecorate(MemberNumber, I.first, Result);
break;
}
}
}
bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
// Known intrinsics usually do not need translation of their declaration
switch (Id) {
case Intrinsic::abs:
case Intrinsic::assume:
case Intrinsic::bitreverse:
case Intrinsic::ceil:
case Intrinsic::copysign:
case Intrinsic::cos:
case Intrinsic::exp:
case Intrinsic::exp2:
case Intrinsic::fabs:
case Intrinsic::floor:
case Intrinsic::fma:
case Intrinsic::frexp:
case Intrinsic::log:
case Intrinsic::log10:
case Intrinsic::log2:
case Intrinsic::maximum:
case Intrinsic::maxnum:
case Intrinsic::smax:
case Intrinsic::umax:
case Intrinsic::minimum:
case Intrinsic::minnum:
case Intrinsic::smin:
case Intrinsic::umin:
case Intrinsic::nearbyint:
case Intrinsic::pow:
case Intrinsic::powi:
case Intrinsic::rint:
case Intrinsic::round:
case Intrinsic::roundeven:
case Intrinsic::sin:
case Intrinsic::sqrt:
case Intrinsic::tan:
case Intrinsic::trunc:
case Intrinsic::ctpop:
case Intrinsic::ctlz:
case Intrinsic::cttz:
case Intrinsic::expect:
case Intrinsic::experimental_noalias_scope_decl:
case Intrinsic::experimental_constrained_fadd:
case Intrinsic::experimental_constrained_fsub:
case Intrinsic::experimental_constrained_fmul:
case Intrinsic::experimental_constrained_fdiv:
case Intrinsic::experimental_constrained_frem:
case Intrinsic::experimental_constrained_fma:
case Intrinsic::experimental_constrained_fptoui:
case Intrinsic::experimental_constrained_fptosi:
case Intrinsic::experimental_constrained_uitofp:
case Intrinsic::experimental_constrained_sitofp:
case Intrinsic::experimental_constrained_fptrunc:
case Intrinsic::experimental_constrained_fpext:
case Intrinsic::experimental_constrained_fcmp:
case Intrinsic::experimental_constrained_fcmps:
case Intrinsic::experimental_constrained_fmuladd:
case Intrinsic::fmuladd:
case Intrinsic::memset:
case Intrinsic::memcpy:
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
case Intrinsic::dbg_declare:
case Intrinsic::dbg_value:
case Intrinsic::annotation:
case Intrinsic::var_annotation:
case Intrinsic::ptr_annotation:
case Intrinsic::invariant_start:
case Intrinsic::invariant_end:
case Intrinsic::dbg_label:
case Intrinsic::trap:
case Intrinsic::ubsantrap:
case Intrinsic::uadd_with_overflow:
case Intrinsic::usub_with_overflow:
case Intrinsic::arithmetic_fence:
case Intrinsic::masked_gather:
case Intrinsic::masked_scatter:
return true;
default:
// Unknown intrinsics' declarations should always be translated
return false;
}
}
// Performs mapping of LLVM IR rounding mode to SPIR-V rounding mode
// Value *V is metadata <rounding mode> argument of
// llvm.experimental.constrained.* intrinsics
SPIRVInstruction *
LLVMToSPIRVBase::applyRoundingModeConstraint(Value *V, SPIRVInstruction *I) {
StringRef RMode =
cast<MDString>(cast<MetadataAsValue>(V)->getMetadata())->getString();
if (RMode.ends_with("tonearest"))
I->addFPRoundingMode(FPRoundingModeRTE);
else if (RMode.ends_with("towardzero"))
I->addFPRoundingMode(FPRoundingModeRTZ);
else if (RMode.ends_with("upward"))
I->addFPRoundingMode(FPRoundingModeRTP);
else if (RMode.ends_with("downward"))
I->addFPRoundingMode(FPRoundingModeRTN);
return I;
}
static SPIRVWord getBuiltinIdForIntrinsic(Intrinsic::ID IID) {
switch (IID) {
// Note: In some cases the semantics of the OpenCL builtin are not identical
// to the semantics of the corresponding LLVM IR intrinsic. The LLVM
// intrinsics handled here assume the default floating point environment
// (no unmasked exceptions, round-to-nearest-ties-even rounding mode)
// and assume that the operations have no side effects (FP status flags
// aren't maintained), so the OpenCL builtin behavior should be
// acceptable.
case Intrinsic::acos:
return OpenCLLIB::Acos;
case Intrinsic::asin:
return OpenCLLIB::Asin;
case Intrinsic::atan:
return OpenCLLIB::Atan;
case Intrinsic::ceil:
return OpenCLLIB::Ceil;
case Intrinsic::copysign:
return OpenCLLIB::Copysign;
case Intrinsic::cos:
return OpenCLLIB::Cos;
case Intrinsic::cosh:
return OpenCLLIB::Cosh;
case Intrinsic::exp:
return OpenCLLIB::Exp;
case Intrinsic::exp2:
return OpenCLLIB::Exp2;
case Intrinsic::fabs:
return OpenCLLIB::Fabs;
case Intrinsic::floor:
return OpenCLLIB::Floor;
case Intrinsic::fma:
return OpenCLLIB::Fma;
case Intrinsic::frexp:
return OpenCLLIB::Frexp;
case Intrinsic::log:
return OpenCLLIB::Log;
case Intrinsic::log10:
return OpenCLLIB::Log10;
case Intrinsic::log2:
return OpenCLLIB::Log2;
case Intrinsic::maximum:
return OpenCLLIB::Fmax;
case Intrinsic::maxnum:
return OpenCLLIB::Fmax;
case Intrinsic::minimum:
return OpenCLLIB::Fmin;
case Intrinsic::minnum:
return OpenCLLIB::Fmin;
case Intrinsic::nearbyint:
return OpenCLLIB::Rint;
case Intrinsic::pow:
return OpenCLLIB::Pow;
case Intrinsic::powi:
return OpenCLLIB::Pown;
case Intrinsic::rint:
return OpenCLLIB::Rint;
case Intrinsic::round:
return OpenCLLIB::Round;
case Intrinsic::roundeven:
return OpenCLLIB::Rint;
case Intrinsic::sin:
return OpenCLLIB::Sin;
case Intrinsic::sinh:
return OpenCLLIB::Sinh;
case Intrinsic::sqrt:
return OpenCLLIB::Sqrt;
case Intrinsic::tan:
return OpenCLLIB::Tan;
case Intrinsic::tanh:
return OpenCLLIB::Tanh;
case Intrinsic::trunc:
return OpenCLLIB::Trunc;
default:
assert(false && "Builtin ID requested for Unhandled intrinsic!");
return 0;
}
}
static SPIRVWord getNativeBuiltinIdForIntrinsic(Intrinsic::ID IID) {
switch (IID) {
case Intrinsic::cos:
return OpenCLLIB::Native_cos;
case Intrinsic::exp:
return OpenCLLIB::Native_exp;
case Intrinsic::exp2:
return OpenCLLIB::Native_exp2;
case Intrinsic::log:
return OpenCLLIB::Native_log;
case Intrinsic::log10:
return OpenCLLIB::Native_log10;
case Intrinsic::log2:
return OpenCLLIB::Native_log2;
case Intrinsic::sin:
return OpenCLLIB::Native_sin;
case Intrinsic::sqrt:
return OpenCLLIB::Native_sqrt;
case Intrinsic::tan:
return OpenCLLIB::Native_tan;
default:
return getBuiltinIdForIntrinsic(IID);
}
}
static bool allowsApproxFunction(IntrinsicInst *II) {
auto *Ty = II->getType();
// OpenCL native_* built-ins only support single precision data type
return II->hasApproxFunc() &&
(Ty->isFloatTy() ||
(Ty->isVectorTy() &&
cast<VectorType>(Ty)->getElementType()->isFloatTy()));
}
namespace {
bool checkMemUser(User *User) {
if (isa<LoadInst>(User) || isa<StoreInst>(User))
return true;
if (auto *III = dyn_cast<IntrinsicInst>(User)) {
if (III->getIntrinsicID() == Intrinsic::memcpy)
return true;
}
return false;
}
} // namespace
bool allowDecorateWithLatencyControlINTEL(IntrinsicInst *II) {
for (auto *Inst : II->users()) {
// if castInst, check Successors
if (auto *Cast = dyn_cast<CastInst>(Inst)) {
for (auto *Successor : Cast->users())
if (checkMemUser(Successor))
return true;
} else {
if (checkMemUser(Inst))
return true;
}
}
return false;
}
SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
SPIRVBasicBlock *BB) {
auto GetMemoryAccess =
[](MemIntrinsic *MI,
bool AllowTwoMemAccessMasks) -> std::vector<SPIRVWord> {
std::vector<SPIRVWord> MemoryAccess(1, MemoryAccessMaskNone);
MaybeAlign DestAlignVal = MI->getDestAlign();
if (DestAlignVal) {
Align AlignVal = *DestAlignVal;
MemoryAccess[0] |= MemoryAccessAlignedMask;
if (auto *MTI = dyn_cast<MemCpyInst>(MI)) {
MaybeAlign SourceAlignVal = MTI->getSourceAlign();
assert(SourceAlignVal && "Missed Source alignment!");
// In a case when alignment of source differs from dest one
// we either preserve both (allowed since SPIR-V 1.4), or the least
// value is guaranteed anyway.
if (AllowTwoMemAccessMasks) {
if (*DestAlignVal != *SourceAlignVal) {
MemoryAccess.push_back(DestAlignVal.valueOrOne().value());
MemoryAccess.push_back(MemoryAccessAlignedMask);
AlignVal = *SourceAlignVal;
}
} else {
AlignVal = std::min(*DestAlignVal, *SourceAlignVal);
}
}
MemoryAccess.push_back(AlignVal.value());
}
if (MI->isVolatile())
MemoryAccess[0] |= MemoryAccessVolatileMask;
return MemoryAccess;
};
// LLVM intrinsics with known translation to SPIR-V are handled here. They
// also must be registered at isKnownIntrinsic function in order to make
// -spirv-allow-unknown-intrinsics work correctly.
auto IID = II->getIntrinsicID();
switch (IID) {
case Intrinsic::assume: {
// llvm.assume translation is currently supported only within
// SPV_KHR_expect_assume extension, ignore it otherwise, since it's
// an optimization hint
if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_expect_assume)) {
SPIRVValue *Condition = transValue(II->getArgOperand(0), BB);
return BM->addAssumeTrueKHRInst(Condition, BB);
}
return nullptr;
}
case Intrinsic::bitreverse: {
if (!BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_bit_instructions),
SPIRVEC_InvalidFunctionCall, II,
"Translation of llvm.bitreverse intrinsic requires "
"SPV_KHR_bit_instructions extension.")) {
return nullptr;
}
SPIRVType *Ty = transType(II->getType());
SPIRVValue *Op = transValue(II->getArgOperand(0), BB);
return BM->addUnaryInst(OpBitReverse, Ty, Op, BB);
}
// Unary FP intrinsic
case Intrinsic::acos:
case Intrinsic::asin:
case Intrinsic::atan:
case Intrinsic::ceil:
case Intrinsic::cos:
case Intrinsic::cosh:
case Intrinsic::exp:
case Intrinsic::exp2:
case Intrinsic::fabs:
case Intrinsic::floor:
case Intrinsic::log:
case Intrinsic::log10:
case Intrinsic::log2:
case Intrinsic::nearbyint:
case Intrinsic::rint:
case Intrinsic::round:
case Intrinsic::roundeven:
case Intrinsic::sin:
case Intrinsic::sinh:
case Intrinsic::sqrt:
case Intrinsic::tan:
case Intrinsic::tanh:
case Intrinsic::trunc: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVWord ExtOp = allowsApproxFunction(II)
? getNativeBuiltinIdForIntrinsic(IID)
: getBuiltinIdForIntrinsic(IID);
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops(1, transValue(II->getArgOperand(0), BB));
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case Intrinsic::frexp: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVWord ExtOp = getBuiltinIdForIntrinsic(IID);
SPIRVType *FTy = transType(II->getType()->getStructElementType(0));
SPIRVTypePointer *ITy = static_cast<SPIRVTypePointer *>(transPointerType(
II->getType()->getStructElementType(1), SPIRAS_Private));
unsigned BitWidth = ITy->getElementType()->getBitWidth();
BM->getErrorLog().checkError(BitWidth == 32, SPIRVEC_InvalidBitWidth,
std::to_string(BitWidth));
SPIRVValue *IntVal =
BM->addVariable(ITy, false, spv::internal::LinkageTypeInternal, nullptr,
"", ITy->getStorageClass(), BB);
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB), IntVal};
return BM->addExtInst(FTy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
// Binary FP intrinsics
case Intrinsic::copysign:
case Intrinsic::pow:
case Intrinsic::powi:
case Intrinsic::maximum:
case Intrinsic::maxnum:
case Intrinsic::minimum:
case Intrinsic::minnum: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVWord ExtOp = allowsApproxFunction(II)
? getNativeBuiltinIdForIntrinsic(IID)
: getBuiltinIdForIntrinsic(IID);
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB)};
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case Intrinsic::umin:
case Intrinsic::umax:
case Intrinsic::smin:
case Intrinsic::smax: {
Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
SPIRVValue *FirstArgVal = transValue(II->getArgOperand(0), BB);
SPIRVValue *SecondArgVal = transValue(II->getArgOperand(1), BB);
Op OC =
(IID == Intrinsic::smin)
? OpSLessThan
: ((IID == Intrinsic::smax)
? OpSGreaterThan
: ((IID == Intrinsic::umin) ? OpULessThan : OpUGreaterThan));
if (auto *VecTy = dyn_cast<VectorType>(II->getArgOperand(0)->getType()))
BoolTy = VectorType::get(BoolTy, VecTy->getElementCount());
SPIRVValue *Cmp =
BM->addCmpInst(OC, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
return BM->addSelectInst(Cmp, FirstArgVal, SecondArgVal, BB);
}
case Intrinsic::fma: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVWord ExtOp = OpenCLLIB::Fma;
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB),
transValue(II->getArgOperand(2), BB)};
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case Intrinsic::abs: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
// LLVM has only one version of abs and it is only for signed integers. We
// unconditionally choose SAbs here
SPIRVWord ExtOp = OpenCLLIB::SAbs;
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops(1, transValue(II->getArgOperand(0), BB));
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case Intrinsic::ctpop: {
return BM->addUnaryInst(OpBitCount, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
}
case Intrinsic::ctlz:
case Intrinsic::cttz: {
SPIRVWord ExtOp = IID == Intrinsic::ctlz ? OpenCLLIB::Clz : OpenCLLIB::Ctz;
SPIRVType *Ty = transType(II->getType());
std::vector<SPIRVValue *> Ops(1, transValue(II->getArgOperand(0), BB));
return BM->addExtInst(Ty, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case Intrinsic::expect: {
// llvm.expect translation is currently supported only within
// SPV_KHR_expect_assume extension, replace it with a translated value of #0
// operand otherwise, since it's an optimization hint
SPIRVValue *Value = transValue(II->getArgOperand(0), BB);
if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_expect_assume)) {
SPIRVType *Ty = transType(II->getType());
SPIRVValue *ExpectedValue = transValue(II->getArgOperand(1), BB);
return BM->addExpectKHRInst(Ty, Value, ExpectedValue, BB);
}
return Value;
}
case Intrinsic::experimental_constrained_fadd: {
auto *BI = BM->addBinaryInst(OpFAdd, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return applyRoundingModeConstraint(II->getOperand(2), BI);
}
case Intrinsic::experimental_constrained_fsub: {
auto *BI = BM->addBinaryInst(OpFSub, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return applyRoundingModeConstraint(II->getOperand(2), BI);
}
case Intrinsic::experimental_constrained_fmul: {
auto *BI = BM->addBinaryInst(OpFMul, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return applyRoundingModeConstraint(II->getOperand(2), BI);
}
case Intrinsic::experimental_constrained_fdiv: {
auto *BI = BM->addBinaryInst(OpFDiv, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return applyRoundingModeConstraint(II->getOperand(2), BI);
}
case Intrinsic::experimental_constrained_frem: {
auto *BI = BM->addBinaryInst(OpFRem, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return applyRoundingModeConstraint(II->getOperand(2), BI);
}
case Intrinsic::experimental_constrained_fma: {
std::vector<SPIRVValue *> Args{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB),
transValue(II->getArgOperand(2), BB)};
auto *BI = BM->addExtInst(transType(II->getType()),
BM->getExtInstSetId(SPIRVEIS_OpenCL),
OpenCLLIB::Fma, Args, BB);
return applyRoundingModeConstraint(II->getOperand(3), BI);
}
case Intrinsic::experimental_constrained_fptoui: {
return BM->addUnaryInst(OpConvertFToU, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
}
case Intrinsic::experimental_constrained_fptosi: {
return BM->addUnaryInst(OpConvertFToS, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
}
case Intrinsic::experimental_constrained_uitofp: {
auto *BI = BM->addUnaryInst(OpConvertUToF, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
return applyRoundingModeConstraint(II->getOperand(1), BI);
}
case Intrinsic::experimental_constrained_sitofp: {
auto *BI = BM->addUnaryInst(OpConvertSToF, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
return applyRoundingModeConstraint(II->getOperand(1), BI);
}
case Intrinsic::experimental_constrained_fpext: {
return BM->addUnaryInst(OpFConvert, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
}
case Intrinsic::experimental_constrained_fptrunc: {
auto *BI = BM->addUnaryInst(OpFConvert, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
return applyRoundingModeConstraint(II->getOperand(1), BI);
}
case Intrinsic::experimental_constrained_fcmp:
case Intrinsic::experimental_constrained_fcmps: {
auto *MetaMod = cast<MetadataAsValue>(II->getOperand(2))->getMetadata();
Op CmpTypeOp = StringSwitch<Op>(cast<MDString>(MetaMod)->getString())
.Case("oeq", OpFOrdEqual)
.Case("ogt", OpFOrdGreaterThan)
.Case("oge", OpFOrdGreaterThanEqual)
.Case("olt", OpFOrdLessThan)
.Case("ole", OpFOrdLessThanEqual)
.Case("one", OpFOrdNotEqual)
.Case("ord", OpOrdered)
.Case("ueq", OpFUnordEqual)
.Case("ugt", OpFUnordGreaterThan)
.Case("uge", OpFUnordGreaterThanEqual)
.Case("ult", OpFUnordLessThan)
.Case("ule", OpFUnordLessThanEqual)
.Case("une", OpFUnordNotEqual)
.Case("uno", OpUnordered)
.Default(OpNop);
assert(CmpTypeOp != OpNop && "Invalid condition code!");
return BM->addCmpInst(CmpTypeOp, transType(II->getType()),
transValue(II->getOperand(0), BB),
transValue(II->getOperand(1), BB), BB);
}
case Intrinsic::experimental_constrained_fmuladd: {
SPIRVType *Ty = transType(II->getType());
SPIRVValue *Mul =
BM->addBinaryInst(OpFMul, Ty, transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
auto *BI = BM->addBinaryInst(OpFAdd, Ty, Mul,
transValue(II->getArgOperand(2), BB), BB);
return applyRoundingModeConstraint(II->getOperand(3), BI);
}
case Intrinsic::fmuladd: {
// For llvm.fmuladd.* fusion is not guaranteed. If a fused multiply-add
// is required the corresponding llvm.fma.* intrinsic function should be
// used instead.
// If allowed, let's replace llvm.fmuladd.* with mad from OpenCL extended
// instruction set, as it has the same semantic for FULL_PROFILE OpenCL
// devices (implementation-defined for EMBEDDED_PROFILE).
if (BM->shouldReplaceLLVMFmulAddWithOpenCLMad() ||
BM->getExtInst() == SPIRV::ExtInst::OpenCL) {
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB),
transValue(II->getArgOperand(2), BB)};
return BM->addExtInst(transType(II->getType()),
BM->getExtInstSetId(SPIRVEIS_OpenCL),
OpenCLLIB::Mad, Ops, BB);
}
// Otherwise, just break llvm.fmuladd.* into a pair of fmul + fadd
SPIRVType *Ty = transType(II->getType());
SPIRVValue *Mul =
BM->addBinaryInst(OpFMul, Ty, transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
return BM->addBinaryInst(OpFAdd, Ty, Mul,
transValue(II->getArgOperand(2), BB), BB);
}
case Intrinsic::fptoui_sat: {
auto *UI = BM->addUnaryInst(OpConvertFToU, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
UI->setSaturatedConversion(true);
return UI;
}
case Intrinsic::fptosi_sat: {
auto *UI = BM->addUnaryInst(OpConvertFToS, transType(II->getType()),
transValue(II->getArgOperand(0), BB), BB);
UI->setSaturatedConversion(true);
return UI;
}
case Intrinsic::uadd_sat:
case Intrinsic::usub_sat:
case Intrinsic::sadd_sat:
case Intrinsic::ssub_sat: {
SPIRVWord ExtOp;
if (IID == Intrinsic::uadd_sat)
ExtOp = OpenCLLIB::UAdd_sat;
else if (IID == Intrinsic::usub_sat)
ExtOp = OpenCLLIB::USub_sat;
else if (IID == Intrinsic::sadd_sat)
ExtOp = OpenCLLIB::SAdd_sat;
else
ExtOp = OpenCLLIB::SSub_sat;
SPIRVType *Ty = transType(II->getType());
std::vector<SPIRVValue *> Operands = {transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB)};
return BM->addExtInst(Ty, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp,
std::move(Operands), BB);
}
case Intrinsic::uadd_with_overflow: {
return BM->addBinaryInst(OpIAddCarry, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
}
case Intrinsic::usub_with_overflow: {
return BM->addBinaryInst(OpISubBorrow, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
}
case Intrinsic::vector_reduce_add:
case Intrinsic::vector_reduce_mul:
case Intrinsic::vector_reduce_and:
case Intrinsic::vector_reduce_or:
case Intrinsic::vector_reduce_xor: {
Op Op;
if (IID == Intrinsic::vector_reduce_add) {
Op = OpIAdd;
} else if (IID == Intrinsic::vector_reduce_mul) {
Op = OpIMul;
} else if (IID == Intrinsic::vector_reduce_and) {
Op = OpBitwiseAnd;
} else if (IID == Intrinsic::vector_reduce_or) {
Op = OpBitwiseOr;
} else {
Op = OpBitwiseXor;
}
VectorType *VecTy = cast<VectorType>(II->getArgOperand(0)->getType());
SPIRVValue *VecSVal = transValue(II->getArgOperand(0), BB);
SPIRVTypeInt *ResultSType =
BM->addIntegerType(VecTy->getElementType()->getIntegerBitWidth());
SPIRVTypeInt *I32STy = BM->addIntegerType(32);
unsigned VecSize = VecTy->getElementCount().getFixedValue();
if (VecSize > 0) {
SmallVector<SPIRVValue *, 16> Extracts(VecSize);
for (unsigned Idx = 0; Idx < VecSize; ++Idx) {
Extracts[Idx] = BM->addVectorExtractDynamicInst(
VecSVal, BM->addIntegerConstant(I32STy, Idx), BB);
}
unsigned Counter = VecSize >> 1;
while (Counter != 0) {
for (unsigned Idx = 0; Idx < Counter; ++Idx) {
Extracts[Idx] = BM->addBinaryInst(Op, ResultSType, Extracts[Idx << 1],
Extracts[(Idx << 1) + 1], BB);
}
Counter >>= 1;
}
if ((VecSize & 1) != 0) {
Extracts[0] = BM->addBinaryInst(Op, ResultSType, Extracts[0],
Extracts[VecSize - 1], BB);
}
return Extracts[0];
}
assert(VecSize && "Zero Extracts size for vector reduce lowering");
return nullptr;
}
case Intrinsic::vector_reduce_fadd:
case Intrinsic::vector_reduce_fmul: {
Op Op = IID == Intrinsic::vector_reduce_fadd ? OpFAdd : OpFMul;
VectorType *VecTy = cast<VectorType>(II->getArgOperand(1)->getType());
SPIRVValue *VecSVal = transValue(II->getArgOperand(1), BB);
SPIRVValue *StartingSVal = transValue(II->getArgOperand(0), BB);
SPIRVTypeInt *I32STy = BM->addIntegerType(32);
unsigned VecSize = VecTy->getElementCount().getFixedValue();
if (VecSize > 0) {
SmallVector<SPIRVValue *, 16> Extracts(VecSize);
for (unsigned Idx = 0; Idx < VecSize; ++Idx) {
Extracts[Idx] = BM->addVectorExtractDynamicInst(
VecSVal, BM->addIntegerConstant(I32STy, Idx), BB);
}
SPIRVValue *V = BM->addBinaryInst(Op, StartingSVal->getType(),
StartingSVal, Extracts[0], BB);
for (unsigned Idx = 1; Idx < VecSize; ++Idx) {
V = BM->addBinaryInst(Op, StartingSVal->getType(), V, Extracts[Idx],
BB);
}
return V;
}
assert(VecSize && "Zero Extracts size for vector reduce lowering");
return nullptr;
}
case Intrinsic::vector_reduce_smax:
case Intrinsic::vector_reduce_smin:
case Intrinsic::vector_reduce_umax:
case Intrinsic::vector_reduce_umin:
case Intrinsic::vector_reduce_fmax:
case Intrinsic::vector_reduce_fmin:
case Intrinsic::vector_reduce_fmaximum:
case Intrinsic::vector_reduce_fminimum: {
Op Op;
if (IID == Intrinsic::vector_reduce_smax) {
Op = OpSGreaterThan;
} else if (IID == Intrinsic::vector_reduce_smin) {
Op = OpSLessThan;
} else if (IID == Intrinsic::vector_reduce_umax) {
Op = OpUGreaterThan;
} else if (IID == Intrinsic::vector_reduce_umin) {
Op = OpULessThan;
} else if (IID == Intrinsic::vector_reduce_fmax) {
Op = OpFOrdGreaterThan;
} else if (IID == Intrinsic::vector_reduce_fmin) {
Op = OpFOrdLessThan;
} else if (IID == Intrinsic::vector_reduce_fmaximum) {
Op = OpFUnordGreaterThan;
} else {
Op = OpFUnordLessThan;
}
VectorType *VecTy = cast<VectorType>(II->getArgOperand(0)->getType());
SPIRVValue *VecSVal = transValue(II->getArgOperand(0), BB);
SPIRVType *BoolSTy = transType(Type::getInt1Ty(II->getContext()));
SPIRVTypeInt *I32STy = BM->addIntegerType(32);
unsigned VecSize = VecTy->getElementCount().getFixedValue();
SmallVector<SPIRVValue *, 16> Extracts(VecSize);
if (VecSize > 0) {
for (unsigned Idx = 0; Idx < VecSize; ++Idx) {
Extracts[Idx] = BM->addVectorExtractDynamicInst(
VecSVal, BM->addIntegerConstant(I32STy, Idx), BB);
}
unsigned Counter = VecSize >> 1;
while (Counter != 0) {
for (unsigned Idx = 0; Idx < Counter; ++Idx) {
SPIRVValue *Cond = BM->addBinaryInst(Op, BoolSTy, Extracts[Idx << 1],
Extracts[(Idx << 1) + 1], BB);
Extracts[Idx] = BM->addSelectInst(Cond, Extracts[Idx << 1],
Extracts[(Idx << 1) + 1], BB);
}
Counter >>= 1;
}
if ((VecSize & 1) != 0) {
SPIRVValue *Cond = BM->addBinaryInst(Op, BoolSTy, Extracts[0],
Extracts[VecSize - 1], BB);
Extracts[0] =
BM->addSelectInst(Cond, Extracts[0], Extracts[VecSize - 1], BB);
}
return Extracts[0];
}
assert(VecSize && "Zero Extracts size for vector reduce lowering");
return nullptr;
}
case Intrinsic::memset: {
// Generally there is no direct mapping of memset to SPIR-V. But it turns
// out that memset is emitted by Clang for initialization in default
// constructors so we need some basic support. The code below only handles
// cases with constant value and constant length.
MemSetInst *MSI = cast<MemSetInst>(II);
Value *Val = MSI->getValue();
if (!isa<Constant>(Val)) {
assert(false &&
"Can't translate llvm.memset with non-const `value` argument");
return nullptr;
}
Value *Len = MSI->getLength();
if (!isa<ConstantInt>(Len)) {
assert(false &&
"Can't translate llvm.memset with non-const `length` argument");
return nullptr;
}
uint64_t NumElements = static_cast<ConstantInt *>(Len)->getZExtValue();
auto *AT = ArrayType::get(Val->getType(), NumElements);
SPIRVTypeArray *CompositeTy = static_cast<SPIRVTypeArray *>(transType(AT));
SPIRVValue *Init;
if (cast<Constant>(Val)->isZeroValue()) {
Init = BM->addNullConstant(CompositeTy);
} else {
// On 32-bit systems, size_type of std::vector is not a 64-bit type. Let's
// assume that we won't encounter memset for more than 2^32 elements and
// insert explicit cast to avoid possible warning/error about narrowing
// conversion
auto TNumElts =
static_cast<std::vector<SPIRVValue *>::size_type>(NumElements);
std::vector<SPIRVValue *> Elts(TNumElts, transValue(Val, BB));
Init = BM->addCompositeConstant(CompositeTy, Elts);
}
SPIRVType *VarTy = transPointerType(AT, SPIRV::SPIRAS_Constant);
SPIRVValue *Var = BM->addVariable(VarTy, /*isConstant*/ true,
spv::internal::LinkageTypeInternal, Init,
"", StorageClassUniformConstant, nullptr);
SPIRVType *SourceTy =
transPointerType(Val->getType(), SPIRV::SPIRAS_Constant);
SPIRVValue *Source = BM->addUnaryInst(OpBitcast, SourceTy, Var, BB);
SPIRVValue *Target = transValue(MSI->getRawDest(), BB);
return BM->addCopyMemorySizedInst(
Target, Source, CompositeTy->getLength(),
GetMemoryAccess(MSI,
BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)),
BB);
} break;
case Intrinsic::memcpy:
return BM->addCopyMemorySizedInst(
transValue(II->getOperand(0), BB), transValue(II->getOperand(1), BB),
transValue(II->getOperand(2), BB),
GetMemoryAccess(cast<MemIntrinsic>(II),
BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)),
BB);
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end: {
Op OC = (II->getIntrinsicID() == Intrinsic::lifetime_start)
? OpLifetimeStart
: OpLifetimeStop;
int64_t Size = dyn_cast<ConstantInt>(II->getOperand(0))->getSExtValue();
if (Size == -1)
Size = 0;
Value *LLVMPtrOp = II->getOperand(1);
unsigned PtrAS = cast<PointerType>(LLVMPtrOp->getType())->getAddressSpace();
auto *PtrOp = transValue(LLVMPtrOp, BB);
if (PtrAS == SPIRAS_Private)
return BM->addLifetimeInst(OC, PtrOp, Size, BB);
// If pointer address space is Generic - cast to private first
BM->getErrorLog().checkError(
PtrAS == SPIRAS_Generic, SPIRVEC_InvalidInstruction, II,
"lifetime intrinsic pointer operand must be in private or generic AS");
auto *SrcTy = PtrOp->getType();
auto *DstTy = BM->addPointerType(StorageClassFunction,
SrcTy->getPointerElementType());
PtrOp = BM->addUnaryInst(OpGenericCastToPtr, DstTy, PtrOp, BB);
ValueMap[LLVMPtrOp] = PtrOp;
return BM->addLifetimeInst(OC, PtrOp, Size, BB);
}
// We don't want to mix translation of regular code and debug info, because
// it creates a mess, therefore translation of debug intrinsics is
// postponed until LLVMToSPIRVDbgTran::finalizeDebug...() methods.
case Intrinsic::dbg_declare:
return DbgTran->createDebugDeclarePlaceholder(cast<DbgDeclareInst>(II), BB);
case Intrinsic::dbg_value:
return DbgTran->createDebugValuePlaceholder(cast<DbgValueInst>(II), BB);
case Intrinsic::annotation: {
SPIRVType *Ty = transScavengedType(II);
Constant *C = cast<Constant>(II->getArgOperand(1)->stripPointerCasts());
StringRef AnnotationString;
if (!getConstantStringInfo(C, AnnotationString))
return nullptr;
if (AnnotationString == kOCLBuiltinName::FPGARegIntel) {
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_reg))
return BM->addFPGARegINTELInst(Ty, transValue(II->getOperand(0), BB),
BB);
else
return transValue(II->getOperand(0), BB);
}
return nullptr;
}
case Intrinsic::var_annotation: {
SPIRVValue *SV;
if (auto *BI = dyn_cast<BitCastInst>(II->getArgOperand(0))) {
SV = transValue(BI->getOperand(0), BB);
} else {
SV = transValue(II->getOperand(0), BB);
}
std::string AnnotationString;
processAnnotationString(II, AnnotationString);
DecorationsInfoVec Decorations =
tryParseAnnotationString(BM, AnnotationString).MemoryAttributesVec;
// If we didn't find any IntelFPGA-specific decorations, let's add the whole
// annotation string as UserSemantic Decoration
if (Decorations.empty()) {
SV->addDecorate(
new SPIRVDecorateUserSemanticAttr(SV, AnnotationString.c_str()));
} else {
addAnnotationDecorations(SV, Decorations);
}
return SV;
}
// The layout of llvm.ptr.annotation is:
// declare iN* @llvm.ptr.annotation.p<address space>iN(
// iN* <val>, i8* <str>, i8* <str>, i32 <int>, i8* <ptr>)
// where N is a power of two number,
// first i8* <str> stands for the annotation itself,
// second i8* <str> is for the location (file name),
// i8* <ptr> is a pointer on a GV, which can carry optinal variadic
// clang::annotation attribute expression arguments.
case Intrinsic::ptr_annotation: {
Value *AnnotSubj = nullptr;
if (auto *BI = dyn_cast<BitCastInst>(II->getArgOperand(0))) {
AnnotSubj = BI->getOperand(0);
} else {
AnnotSubj = II->getOperand(0);
}
std::string AnnotationString;
processAnnotationString(II, AnnotationString);
AnnotationDecorations Decorations =
tryParseAnnotationString(BM, AnnotationString);
// Translate FPGARegIntel annotations to OpFPGARegINTEL.
if (AnnotationString == kOCLBuiltinName::FPGARegIntel) {
auto *Ty = transScavengedType(II);
auto *BI = dyn_cast<BitCastInst>(II->getOperand(0));
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_reg))
return BM->addFPGARegINTELInst(Ty, transValue(BI, BB), BB);
return transValue(BI, BB);
}
SPIRVValue *DecSubj = transValue(AnnotSubj, BB);
if (Decorations.empty()) {
DecSubj->addDecorate(
new SPIRVDecorateUserSemanticAttr(DecSubj, AnnotationString.c_str()));
} else {
addAnnotationDecorations(DecSubj, Decorations.MemoryAttributesVec);
// Apply the LSU parameter decoration to the pointer result of a GEP
// to the given struct member (InBoundsPtrAccessChain in SPIR-V).
// Decorating the member itself with a MemberDecoration is not feasible,
// because multiple accesses to the struct-held memory can require
// different LSU parameters.
addAnnotationDecorations(DecSubj, Decorations.MemoryAccessesVec);
addAnnotationDecorations(DecSubj, Decorations.CacheControlVec);
addAnnotationDecorations(DecSubj, Decorations.BufferLocationVec);
if (allowDecorateWithLatencyControlINTEL(II)) {
addAnnotationDecorations(DecSubj, Decorations.LatencyControlVec);
}
}
II->replaceAllUsesWith(II->getOperand(0));
return DecSubj;
}
case Intrinsic::stacksave: {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_variable_length_array)) {
auto *Ty = transPointerType(Type::getInt8Ty(M->getContext()), 0);
return BM->addInstTemplate(OpSaveMemoryINTEL, BB, Ty);
}
BM->getErrorLog().checkError(
BM->isUnknownIntrinsicAllowed(II), SPIRVEC_InvalidFunctionCall, II,
"Translation of llvm.stacksave intrinsic requires "
"SPV_INTEL_variable_length_array extension or "
"-spirv-allow-unknown-intrinsics option.");
break;
}
case Intrinsic::stackrestore: {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_variable_length_array)) {
auto *Ptr = transValue(II->getArgOperand(0), BB);
return BM->addInstTemplate(OpRestoreMemoryINTEL, {Ptr->getId()}, BB,
nullptr);
}
BM->getErrorLog().checkError(
BM->isUnknownIntrinsicAllowed(II), SPIRVEC_InvalidFunctionCall, II,
"Translation of llvm.restore intrinsic requires "
"SPV_INTEL_variable_length_array extension or "
"-spirv-allow-unknown-intrinsics option.");
break;
}
// We can just ignore/drop some intrinsics, like optimizations hint.
case Intrinsic::experimental_noalias_scope_decl:
case Intrinsic::invariant_start:
case Intrinsic::invariant_end:
case Intrinsic::dbg_label:
// llvm.trap intrinsic is not implemented. But for now don't crash. This
// change is pending the trap/abort intrinsic implementation.
case Intrinsic::trap:
case Intrinsic::ubsantrap:
// llvm.instrprof.* intrinsics are not supported
case Intrinsic::instrprof_increment:
case Intrinsic::instrprof_increment_step:
case Intrinsic::instrprof_value_profile:
return nullptr;
case Intrinsic::is_constant: {
auto *CO = dyn_cast<Constant>(II->getOperand(0));
if (CO && isManifestConstant(CO))
return transValue(ConstantInt::getTrue(II->getType()), BB, false);
else
return transValue(ConstantInt::getFalse(II->getType()), BB, false);
}
case Intrinsic::arithmetic_fence: {
SPIRVType *Ty = transType(II->getType());
SPIRVValue *Op = transValue(II->getArgOperand(0), BB);
if (BM->isAllowedToUseExtension(ExtensionID::SPV_EXT_arithmetic_fence)) {
BM->addCapability(CapabilityArithmeticFenceEXT);
BM->addExtension(ExtensionID::SPV_EXT_arithmetic_fence);
return BM->addUnaryInst(OpArithmeticFenceEXT, Ty, Op, BB);
}
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_arithmetic_fence)) {
// Note: SPV_INTEL_arithmetic_fence was unpublished and superseded by
// SPV_EXT_arithmetic_fence.
BM->addCapability(CapabilityArithmeticFenceEXT);
BM->addExtension(ExtensionID::SPV_INTEL_arithmetic_fence);
return BM->addUnaryInst(OpArithmeticFenceEXT, Ty, Op, BB);
}
return Op;
}
case Intrinsic::masked_gather: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_masked_gather_scatter)) {
BM->getErrorLog().checkError(
BM->isUnknownIntrinsicAllowed(II), SPIRVEC_InvalidFunctionCall, II,
"Translation of llvm.masked.gather intrinsic requires "
"SPV_INTEL_masked_gather_scatter extension or "
"-spirv-allow-unknown-intrinsics option.");
return nullptr;
}
SPIRVType *Ty = transScavengedType(II);
auto *PtrVector = transValue(II->getArgOperand(0), BB);
uint32_t Alignment =
cast<ConstantInt>(II->getArgOperand(1))->getZExtValue();
auto *Mask = transValue(II->getArgOperand(2), BB);
auto *FillEmpty = transValue(II->getArgOperand(3), BB);
std::vector<SPIRVWord> Ops = {PtrVector->getId(), Alignment, Mask->getId(),
FillEmpty->getId()};
return BM->addInstTemplate(internal::OpMaskedGatherINTEL, Ops, BB, Ty);
}
case Intrinsic::masked_scatter: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_masked_gather_scatter)) {
BM->getErrorLog().checkError(
BM->isUnknownIntrinsicAllowed(II), SPIRVEC_InvalidFunctionCall, II,
"Translation of llvm.masked.scatter intrinsic requires "
"SPV_INTEL_masked_gather_scatter extension or "
"-spirv-allow-unknown-intrinsics option.");
return nullptr;
}
auto *InputVector = transValue(II->getArgOperand(0), BB);
auto *PtrVector = transValue(II->getArgOperand(1), BB);
uint32_t Alignment =
cast<ConstantInt>(II->getArgOperand(2))->getZExtValue();
auto *Mask = transValue(II->getArgOperand(3), BB);
std::vector<SPIRVWord> Ops = {InputVector->getId(), PtrVector->getId(),
Alignment, Mask->getId()};
return BM->addInstTemplate(internal::OpMaskedScatterINTEL, Ops, BB,
nullptr);
}
case Intrinsic::is_fpclass: {
// There is no direct counterpart for the intrinsic in SPIR-V, hence
// we need to emulate its work by sequence of other instructions
SPIRVType *ResTy = transType(II->getType());
llvm::FPClassTest FPClass = static_cast<llvm::FPClassTest>(
cast<ConstantInt>(II->getArgOperand(1))->getZExtValue());
// if no tests are provided - return false
if (FPClass == 0)
return BM->addConstant(ResTy, false);
// if all tests are provided - return true
if (FPClass == fcAllFlags)
return BM->addConstant(ResTy, true);
Type *OpLLVMTy = II->getArgOperand(0)->getType();
SPIRVValue *InputFloat = transValue(II->getArgOperand(0), BB);
std::vector<SPIRVValue *> ResultVec;
// Adds test for Negative/Positive values
SPIRVValue *SignBitTest = nullptr;
SPIRVValue *NoSignTest = nullptr;
auto GetNegPosInstTest = [&](SPIRVValue *TestInst,
bool IsNegative) -> SPIRVValue * {
SignBitTest = (SignBitTest)
? SignBitTest
: BM->addInstTemplate(OpSignBitSet,
{InputFloat->getId()}, BB, ResTy);
if (IsNegative) {
return BM->addInstTemplate(
OpLogicalAnd, {SignBitTest->getId(), TestInst->getId()}, BB, ResTy);
}
NoSignTest = (NoSignTest)
? NoSignTest
: BM->addInstTemplate(OpLogicalNot,
{SignBitTest->getId()}, BB, ResTy);
return BM->addInstTemplate(
OpLogicalAnd, {NoSignTest->getId(), TestInst->getId()}, BB, ResTy);
};
// Get LLVM Op type converted to integer. It can be either scalar or vector.
const uint32_t BitSize = OpLLVMTy->getScalarSizeInBits();
Type *IntOpLLVMTy = IntegerType::getIntNTy(M->getContext(), BitSize);
if (OpLLVMTy->isVectorTy())
IntOpLLVMTy = FixedVectorType::get(
IntOpLLVMTy, cast<FixedVectorType>(OpLLVMTy)->getNumElements());
SPIRVType *OpSPIRVTy = transType(IntOpLLVMTy);
const llvm::fltSemantics &Semantics =
OpLLVMTy->getScalarType()->getFltSemantics();
const APInt Inf = APFloat::getInf(Semantics).bitcastToAPInt();
const APInt AllOneMantissa =
APFloat::getLargest(Semantics).bitcastToAPInt() & ~Inf;
// Some checks can be inverted tests for simple cases, for example
// simultaneous check for inf, normal, subnormal and zero is a check for
// non nan.
auto GetInvertedFPClassTest =
[](const llvm::FPClassTest Test) -> llvm::FPClassTest {
llvm::FPClassTest InvertedTest = ~Test & fcAllFlags;
switch (InvertedTest) {
default:
break;
case fcNan:
case fcSNan:
case fcQNan:
case fcInf:
case fcPosInf:
case fcNegInf:
case fcNormal:
case fcPosNormal:
case fcNegNormal:
case fcSubnormal:
case fcPosSubnormal:
case fcNegSubnormal:
case fcZero:
case fcPosZero:
case fcNegZero:
case fcFinite:
case fcPosFinite:
case fcNegFinite:
return InvertedTest;
}
return fcNone;
};
bool IsInverted = false;
if (llvm::FPClassTest InvertedCheck = GetInvertedFPClassTest(FPClass)) {
IsInverted = true;
FPClass = InvertedCheck;
}
auto GetInvertedTestIfNeeded = [&](SPIRVValue *TestInst) -> SPIRVValue * {
if (!IsInverted)
return TestInst;
return BM->addInstTemplate(OpLogicalNot, {TestInst->getId()}, BB, ResTy);
};
// TODO: we can add some optimization for fcFinite check by replacing it
// with fabs + cmp to 0x7FF0000000000000
// Integer parameter of the intrinsic is combined from several bit masks
// referenced in FPClassTest enum from FloatingPointMode.h in LLVM.
// Since a single intrinsic can provide multiple tests - here we might end
// up adding several sequences of SPIR-V instructions
if (FPClass & fcNan) {
// Map on OpIsNan if we have both QNan and SNan test bits set
if (FPClass & fcSNan && FPClass & fcQNan) {
auto *TestIsNan =
BM->addInstTemplate(OpIsNan, {InputFloat->getId()}, BB, ResTy);
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsNan));
} else {
// isquiet(V) ==> abs(V) >= (unsigned(Inf) | quiet_bit)
APInt QNaNBitMask =
APInt::getOneBitSet(BitSize, AllOneMantissa.getActiveBits() - 1);
APInt InfWithQnanBit = Inf | QNaNBitMask;
auto *QNanBitConst = transValue(
Constant::getIntegerValue(IntOpLLVMTy, InfWithQnanBit), BB);
auto *BitCastToInt =
BM->addUnaryInst(OpBitcast, OpSPIRVTy, InputFloat, BB);
auto *TestIsQNan = BM->addCmpInst(OpUGreaterThanEqual, ResTy,
BitCastToInt, QNanBitConst, BB);
if (FPClass & fcQNan) {
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsQNan));
} else {
// issignaling(V) ==> isnan(V) && !isquiet(V)
auto *TestIsNan =
BM->addInstTemplate(OpIsNan, {InputFloat->getId()}, BB, ResTy);
auto *NotQNan = BM->addInstTemplate(OpLogicalNot,
{TestIsQNan->getId()}, BB, ResTy);
auto *TestIsSNan = BM->addInstTemplate(
OpLogicalAnd, {TestIsNan->getId(), NotQNan->getId()}, BB, ResTy);
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsSNan));
}
}
}
if (FPClass & fcInf) {
auto *TestIsInf =
BM->addInstTemplate(OpIsInf, {InputFloat->getId()}, BB, ResTy);
if (FPClass & fcNegInf && FPClass & fcPosInf)
// Map on OpIsInf if we have both Inf test bits set
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsInf));
else
// Map on OpIsInf with following check for sign bit
ResultVec.emplace_back(GetInvertedTestIfNeeded(
GetNegPosInstTest(TestIsInf, FPClass & fcNegInf)));
}
if (FPClass & fcNormal) {
auto *TestIsNormal =
BM->addInstTemplate(OpIsNormal, {InputFloat->getId()}, BB, ResTy);
if (FPClass & fcNegNormal && FPClass & fcPosNormal)
// Map on OpIsNormal if we have both Normal test bits set
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsNormal));
else
// Map on OpIsNormal with following check for sign bit
ResultVec.emplace_back(GetInvertedTestIfNeeded(
GetNegPosInstTest(TestIsNormal, FPClass & fcNegNormal)));
}
if (FPClass & fcSubnormal) {
// issubnormal(V) ==> unsigned(abs(V) - 1) < (all mantissa bits set)
auto *BitCastToInt =
BM->addUnaryInst(OpBitcast, OpSPIRVTy, InputFloat, BB);
auto *MantissaConst = transValue(
Constant::getIntegerValue(IntOpLLVMTy, AllOneMantissa), BB);
auto *MinusOne =
BM->addBinaryInst(OpISub, OpSPIRVTy, BitCastToInt, MantissaConst, BB);
auto *TestIsSubnormal =
BM->addCmpInst(OpULessThan, ResTy, MinusOne, MantissaConst, BB);
if (FPClass & fcPosSubnormal && FPClass & fcNegSubnormal)
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsSubnormal));
else
ResultVec.emplace_back(GetInvertedTestIfNeeded(
GetNegPosInstTest(TestIsSubnormal, FPClass & fcNegSubnormal)));
}
if (FPClass & fcZero) {
// Create zero integer constant and check for equality with bitcasted to
// int float value
auto SetUpCMPToZero = [&](SPIRVValue *BitCastToInt,
bool IsPositive) -> SPIRVValue * {
APInt ZeroInt = APInt::getZero(BitSize);
if (IsPositive) {
auto *ZeroConst =
transValue(Constant::getIntegerValue(IntOpLLVMTy, ZeroInt), BB);
return BM->addCmpInst(OpIEqual, ResTy, BitCastToInt, ZeroConst, BB);
}
// Created 'negated' zero
ZeroInt.setSignBit();
auto *NegZeroConst =
transValue(Constant::getIntegerValue(IntOpLLVMTy, ZeroInt), BB);
return BM->addCmpInst(OpIEqual, ResTy, BitCastToInt, NegZeroConst, BB);
};
auto *BitCastToInt =
BM->addUnaryInst(OpBitcast, OpSPIRVTy, InputFloat, BB);
if (FPClass & fcPosZero && FPClass & fcNegZero) {
APInt ZeroInt = APInt::getZero(BitSize);
auto *ZeroConst =
transValue(Constant::getIntegerValue(IntOpLLVMTy, ZeroInt), BB);
APInt MaskToClearSignBit = APInt::getSignedMaxValue(BitSize);
auto *MaskToClearSignBitConst = transValue(
Constant::getIntegerValue(IntOpLLVMTy, MaskToClearSignBit), BB);
auto *BitwiseAndRes = BM->addBinaryInst(
OpBitwiseAnd, OpSPIRVTy, BitCastToInt, MaskToClearSignBitConst, BB);
auto *TestIsZero =
BM->addCmpInst(OpIEqual, ResTy, BitwiseAndRes, ZeroConst, BB);
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsZero));
} else if (FPClass & fcPosZero) {
auto *TestIsPosZero =
SetUpCMPToZero(BitCastToInt, true /*'positive' zero*/);
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsPosZero));
} else {
auto *TestIsNegZero =
SetUpCMPToZero(BitCastToInt, false /*'negated' zero*/);
ResultVec.emplace_back(GetInvertedTestIfNeeded(TestIsNegZero));
}
}
if (ResultVec.size() == 1)
return ResultVec.back();
SPIRVValue *Result = ResultVec.front();
for (size_t I = 1; I != ResultVec.size(); ++I) {
// Create a sequence of LogicalOr instructions from ResultVec to get
// the overall test result
std::vector<SPIRVId> LogicOps = {Result->getId(), ResultVec[I]->getId()};
Result = BM->addInstTemplate(OpLogicalOr, LogicOps, BB, ResTy);
}
return Result;
}
default:
if (auto *BVar = transFPBuiltinIntrinsicInst(II, BB))
return BVar;
if (BM->isUnknownIntrinsicAllowed(II))
return BM->addCallInst(
transFunctionDecl(II->getCalledFunction()),
transArguments(II, BB,
SPIRVEntry::createUnique(OpFunctionCall).get()),
BB);
else
// Other LLVM intrinsics shouldn't get to SPIRV, because they
// can't be represented in SPIRV or aren't implemented yet.
BM->SPIRVCK(
false, InvalidFunctionCall, II->getCalledOperand()->getName().str());
}
return nullptr;
}
LLVMToSPIRVBase::FPBuiltinType
LLVMToSPIRVBase::getFPBuiltinType(IntrinsicInst *II, StringRef &OpName) {
StringRef Name = II->getCalledFunction()->getName();
if (!Name.consume_front("llvm.fpbuiltin."))
return FPBuiltinType::UNKNOWN;
OpName = Name.split('.').first;
FPBuiltinType Type =
StringSwitch<FPBuiltinType>(OpName)
.Cases("fadd", "fsub", "fmul", "fdiv", "frem",
FPBuiltinType::REGULAR_MATH)
.Cases("sin", "cos", "tan", FPBuiltinType::EXT_1OPS)
.Cases("sinh", "cosh", "tanh", FPBuiltinType::EXT_1OPS)
.Cases("asin", "acos", "atan", FPBuiltinType::EXT_1OPS)
.Cases("asinh", "acosh", "atanh", FPBuiltinType::EXT_1OPS)
.Cases("exp", "exp2", "exp10", "expm1", FPBuiltinType::EXT_1OPS)
.Cases("log", "log2", "log10", "log1p", FPBuiltinType::EXT_1OPS)
.Cases("sqrt", "rsqrt", "erf", "erfc", FPBuiltinType::EXT_1OPS)
.Cases("atan2", "pow", "hypot", "ldexp", FPBuiltinType::EXT_2OPS)
.Case("sincos", FPBuiltinType::EXT_3OPS)
.Default(FPBuiltinType::UNKNOWN);
return Type;
}
SPIRVValue *LLVMToSPIRVBase::transFPBuiltinIntrinsicInst(IntrinsicInst *II,
SPIRVBasicBlock *BB) {
StringRef OpName;
auto FPBuiltinTypeVal = getFPBuiltinType(II, OpName);
if (FPBuiltinTypeVal == FPBuiltinType::UNKNOWN)
return nullptr;
switch (FPBuiltinTypeVal) {
case FPBuiltinType::REGULAR_MATH: {
auto BinOp = StringSwitch<Op>(OpName)
.Case("fadd", OpFAdd)
.Case("fsub", OpFSub)
.Case("fmul", OpFMul)
.Case("fdiv", OpFDiv)
.Case("frem", OpFRem)
.Default(OpUndef);
return BM->addBinaryInst(BinOp, transType(II->getType()),
transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB), BB);
}
case FPBuiltinType::EXT_1OPS: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops(1, transValue(II->getArgOperand(0), BB));
auto ExtOp = StringSwitch<SPIRVWord>(OpName)
.Case("sin", OpenCLLIB::Sin)
.Case("cos", OpenCLLIB::Cos)
.Case("tan", OpenCLLIB::Tan)
.Case("sinh", OpenCLLIB::Sinh)
.Case("cosh", OpenCLLIB::Cosh)
.Case("tanh", OpenCLLIB::Tanh)
.Case("asin", OpenCLLIB::Asin)
.Case("acos", OpenCLLIB::Acos)
.Case("atan", OpenCLLIB::Atan)
.Case("asinh", OpenCLLIB::Asinh)
.Case("acosh", OpenCLLIB::Acosh)
.Case("atanh", OpenCLLIB::Atanh)
.Case("exp", OpenCLLIB::Exp)
.Case("exp2", OpenCLLIB::Exp2)
.Case("exp10", OpenCLLIB::Exp10)
.Case("expm1", OpenCLLIB::Expm1)
.Case("log", OpenCLLIB::Log)
.Case("log2", OpenCLLIB::Log2)
.Case("log10", OpenCLLIB::Log10)
.Case("log1p", OpenCLLIB::Log1p)
.Case("sqrt", OpenCLLIB::Sqrt)
.Case("rsqrt", OpenCLLIB::Rsqrt)
.Case("erf", OpenCLLIB::Erf)
.Case("erfc", OpenCLLIB::Erfc)
.Default(SPIRVWORD_MAX);
assert(ExtOp != SPIRVWORD_MAX);
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case FPBuiltinType::EXT_2OPS: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB)};
auto ExtOp = StringSwitch<SPIRVWord>(OpName)
.Case("atan2", OpenCLLIB::Atan2)
.Case("hypot", OpenCLLIB::Hypot)
.Case("pow", OpenCLLIB::Pow)
.Case("ldexp", OpenCLLIB::Ldexp)
.Default(SPIRVWORD_MAX);
assert(ExtOp != SPIRVWORD_MAX);
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
case FPBuiltinType::EXT_3OPS: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
SPIRVType *STy = transType(II->getType());
std::vector<SPIRVValue *> Ops{transValue(II->getArgOperand(0), BB),
transValue(II->getArgOperand(1), BB),
transValue(II->getArgOperand(2), BB)};
auto ExtOp = StringSwitch<SPIRVWord>(OpName)
.Case("sincos", OpenCLLIB::Sincos)
.Default(SPIRVWORD_MAX);
assert(ExtOp != SPIRVWORD_MAX);
return BM->addExtInst(STy, BM->getExtInstSetId(SPIRVEIS_OpenCL), ExtOp, Ops,
BB);
}
default:
return nullptr;
}
return nullptr;
}
SPIRVValue *LLVMToSPIRVBase::transFenceInst(FenceInst *FI,
SPIRVBasicBlock *BB) {
SPIRVWord MemorySemantics;
// Fence ordering may only be Acquire, Release, AcquireRelease, or
// SequentiallyConsistent
switch (FI->getOrdering()) {
case llvm::AtomicOrdering::Acquire:
MemorySemantics = MemorySemanticsAcquireMask;
break;
case llvm::AtomicOrdering::Release:
MemorySemantics = MemorySemanticsReleaseMask;
break;
case llvm::AtomicOrdering::AcquireRelease:
MemorySemantics = MemorySemanticsAcquireReleaseMask;
break;
case llvm::AtomicOrdering::SequentiallyConsistent:
MemorySemantics = MemorySemanticsSequentiallyConsistentMask;
break;
default:
assert(false && "Unexpected fence ordering");
MemorySemantics = SPIRVWORD_MAX;
break;
}
Module *M = FI->getParent()->getModule();
SmallVector<StringRef> SSIDs;
FI->getContext().getSyncScopeNames(SSIDs);
spv::Scope S;
// Treat all llvm.fence instructions as having CrossDevice scope by default
if (!OCLStrMemScopeMap::find(SSIDs[FI->getSyncScopeID()].str(), &S)) {
S = ScopeCrossDevice;
}
SPIRVValue *RetScope = transConstant(getUInt32(M, S));
SPIRVValue *Val = transConstant(getUInt32(M, MemorySemantics));
assert(RetScope && Val && "RetScope and Value are not constants");
return BM->addMemoryBarrierInst(static_cast<Scope>(RetScope->getId()),
Val->getId(), BB);
}
SPIRVValue *LLVMToSPIRVBase::transCallInst(CallInst *CI, SPIRVBasicBlock *BB) {
assert(CI);
Function *F = CI->getFunction();
if (isa<InlineAsm>(CI->getCalledOperand()) &&
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_inline_assembly)) {
// Inline asm is opaque, so we cannot reason about its FP contraction
// requirements.
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName()
<< ": inline asm " << *CI << '\n');
joinFPContract(F, FPContract::DISABLED);
return transAsmCallINTEL(CI, BB);
}
if (CI->isIndirectCall()) {
// The function is not known in advance
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName()
<< ": indirect call " << *CI << '\n');
joinFPContract(F, FPContract::DISABLED);
return transIndirectCallInst(CI, BB);
}
return transDirectCallInst(CI, BB);
}
SPIRVValue *LLVMToSPIRVBase::transDirectCallInst(CallInst *CI,
SPIRVBasicBlock *BB) {
SPIRVExtInstSetKind ExtSetKind = SPIRVEIS_Count;
SPIRVWord ExtOp = SPIRVWORD_MAX;
llvm::Function *F = CI->getCalledFunction();
auto MangledName = F->getName();
StringRef DemangledName;
if (MangledName.starts_with(SPCV_CAST) || MangledName == SAMPLER_INIT)
return oclTransSpvcCastSampler(CI, BB);
if (oclIsBuiltin(MangledName, DemangledName) ||
isDecoratedSPIRVFunc(F, DemangledName)) {
if (auto *BV = transBuiltinToConstant(DemangledName, CI))
return BV;
if (auto *BV = transBuiltinToInst(DemangledName, CI, BB))
return BV;
}
SmallVector<std::string, 2> Dec;
if (isBuiltinTransToExtInst(CI->getCalledFunction(), &ExtSetKind, &ExtOp,
&Dec)) {
if (DemangledName.find("__spirv_ocl_printf") != StringRef::npos) {
auto *FormatStrPtr = cast<PointerType>(CI->getArgOperand(0)->getType());
if (FormatStrPtr->getAddressSpace() !=
SPIR::TypeAttributeEnum::ATTR_CONST) {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_EXT_relaxed_printf_string_address_space)) {
std::string ErrorStr =
"Either SPV_EXT_relaxed_printf_string_address_space extension "
"should be allowed to translate this module, because this LLVM "
"module contains the printf function with format string, whose "
"address space is not equal to 2 (constant).";
getErrorLog().checkError(false, SPIRVEC_RequiresExtension, CI,
ErrorStr);
}
BM->addExtension(
ExtensionID::SPV_EXT_relaxed_printf_string_address_space);
}
}
return addDecorations(
BM->addExtInst(
transScavengedType(CI), BM->getExtInstSetId(ExtSetKind), ExtOp,
transArguments(CI, BB,
SPIRVEntry::createUnique(ExtSetKind, ExtOp).get()),
BB),
Dec);
}
Function *Callee = CI->getCalledFunction();
if (Callee->isDeclaration()) {
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName().str()
<< ": call to an undefined function " << *CI << '\n');
joinFPContract(CI->getFunction(), FPContract::DISABLED);
} else {
FPContract CalleeFPC = getFPContract(Callee);
joinFPContract(CI->getFunction(), CalleeFPC);
if (CalleeFPC == FPContract::DISABLED) {
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName().str()
<< ": call to a function with disabled contraction: "
<< *CI << '\n');
}
}
return BM->addCallInst(
transFunctionDecl(Callee),
transArguments(CI, BB, SPIRVEntry::createUnique(OpFunctionCall).get()),
BB);
}
SPIRVValue *LLVMToSPIRVBase::transIndirectCallInst(CallInst *CI,
SPIRVBasicBlock *BB) {
if (BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_function_pointers),
SPIRVEC_FunctionPointers, CI)) {
return BM->addIndirectCallInst(
transValue(CI->getCalledOperand(), BB), transScavengedType(CI),
transArguments(CI, BB, SPIRVEntry::createUnique(OpFunctionCall).get()),
BB);
}
return nullptr;
}
SPIRVValue *LLVMToSPIRVBase::transAsmINTEL(InlineAsm *IA) {
assert(IA);
// TODO: intention here is to provide information about actual target
// but in fact spir-64 is substituted as triple when translator works
// eventually we need to fix it (not urgent)
StringRef TripleStr(M->getTargetTriple());
auto *AsmTarget = static_cast<SPIRVAsmTargetINTEL *>(
BM->getOrAddAsmTargetINTEL(TripleStr.str()));
auto *SIA = BM->addAsmINTEL(
static_cast<SPIRVTypeFunction *>(transType(IA->getFunctionType())),
AsmTarget, IA->getAsmString(), IA->getConstraintString());
if (IA->hasSideEffects())
SIA->addDecorate(DecorationSideEffectsINTEL);
return SIA;
}
SPIRVValue *LLVMToSPIRVBase::transAsmCallINTEL(CallInst *CI,
SPIRVBasicBlock *BB) {
assert(CI);
auto *IA = cast<InlineAsm>(CI->getCalledOperand());
return BM->addAsmCallINTELInst(
static_cast<SPIRVAsmINTEL *>(transValue(IA, BB, false)),
transArguments(CI, BB, SPIRVEntry::createUnique(OpAsmCallINTEL).get()),
BB);
}
bool LLVMToSPIRVBase::transAddressingMode() {
Triple TargetTriple(M->getTargetTriple());
if (TargetTriple.isArch32Bit())
BM->setAddressingModel(AddressingModelPhysical32);
else
BM->setAddressingModel(AddressingModelPhysical64);
// Physical addressing model requires Addresses capability
BM->addCapability(CapabilityAddresses);
return true;
}
std::vector<SPIRVValue *>
LLVMToSPIRVBase::transValue(const std::vector<Value *> &Args,
SPIRVBasicBlock *BB) {
std::vector<SPIRVValue *> BArgs;
for (auto &I : Args)
BArgs.push_back(transValue(I, BB));
return BArgs;
}
std::vector<SPIRVWord>
LLVMToSPIRVBase::transValue(const std::vector<Value *> &Args,
SPIRVBasicBlock *BB, SPIRVEntry *Entry) {
std::vector<SPIRVWord> Operands;
for (size_t I = 0, E = Args.size(); I != E; ++I) {
Operands.push_back(Entry->isOperandLiteral(I)
? cast<ConstantInt>(Args[I])->getZExtValue()
: transValue(Args[I], BB)->getId());
}
return Operands;
}
std::vector<SPIRVWord> LLVMToSPIRVBase::transArguments(CallInst *CI,
SPIRVBasicBlock *BB,
SPIRVEntry *Entry) {
return transValue(getArguments(CI), BB, Entry);
}
SPIRVWord LLVMToSPIRVBase::transFunctionControlMask(Function *F) {
SPIRVWord FCM = 0;
SPIRSPIRVFuncCtlMaskMap::foreach (
[&](Attribute::AttrKind Attr, SPIRVFunctionControlMaskKind Mask) {
if (F->hasFnAttribute(Attr)) {
if (Attr == Attribute::OptimizeNone) {
if (BM->isAllowedToUseExtension(ExtensionID::SPV_EXT_optnone)) {
BM->addExtension(ExtensionID::SPV_EXT_optnone);
BM->addCapability(CapabilityOptNoneEXT);
} else if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_optnone)) {
BM->addExtension(ExtensionID::SPV_INTEL_optnone);
BM->addCapability(CapabilityOptNoneINTEL);
} else
return;
}
FCM |= Mask;
}
});
return FCM;
}
void LLVMToSPIRVBase::transGlobalAnnotation(GlobalVariable *V) {
SPIRVDBG(dbgs() << "[transGlobalAnnotation] " << *V << '\n');
// @llvm.global.annotations is an array that contains structs with 4 fields.
// Get the array of structs with metadata
// TODO: actually, now it contains 5 fields, the fifth by default is nullptr
// or undef, but it can be defined to include variadic arguments of
// clang::annotation attribute. Need to refactor this function to turn on this
// translation
ConstantArray *CA = cast<ConstantArray>(V->getOperand(0));
for (Value *Op : CA->operands()) {
ConstantStruct *CS = cast<ConstantStruct>(Op);
// The first field of the struct contains a pointer to annotated variable
Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts();
SPIRVValue *SV = transValue(AnnotatedVar, nullptr);
// The second field contains a pointer to a global annotation string
GlobalVariable *GV =
cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
StringRef AnnotationString;
if (!getConstantStringInfo(GV, AnnotationString)) {
assert(!"Annotation string missing");
return;
}
DecorationsInfoVec Decorations =
tryParseAnnotationString(BM, AnnotationString).MemoryAttributesVec;
// If we didn't find any annotation decorations, let's add the whole
// annotation string as UserSemantic Decoration
if (Decorations.empty()) {
SV->addDecorate(
new SPIRVDecorateUserSemanticAttr(SV, AnnotationString.str()));
} else {
addAnnotationDecorations(SV, Decorations);
}
}
}
void LLVMToSPIRVBase::transGlobalIOPipeStorage(GlobalVariable *V, MDNode *IO) {
SPIRVDBG(dbgs() << "[transGlobalIOPipeStorage] " << *V << '\n');
SPIRVValue *SV = transValue(V, nullptr);
assert(SV && "Failed to process OCL PipeStorage object");
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_io_pipes)) {
unsigned ID = getMDOperandAsInt(IO, 0);
SV->addDecorate(DecorationIOPipeStorageINTEL, ID);
}
}
bool LLVMToSPIRVBase::transGlobalVariables() {
for (auto I = M->global_begin(), E = M->global_end(); I != E; ++I) {
if ((*I).getName() == "llvm.global.annotations")
transGlobalAnnotation(&(*I));
else if ([I]() -> bool {
// Check if the GV is used only in var/ptr instructions. If yes -
// skip processing of this since it's only an annotation GV.
if (I->user_empty())
return false;
for (auto *U : I->users()) {
Value *V = U;
while (isa<BitCastInst>(V) || isa<AddrSpaceCastInst>(V))
V = cast<CastInst>(V)->getOperand(0);
auto *GEP = dyn_cast_or_null<GetElementPtrInst>(V);
if (!GEP)
return false;
for (auto *GEPU : GEP->users()) {
auto *II = dyn_cast<IntrinsicInst>(GEPU);
if (!II)
return false;
switch (II->getIntrinsicID()) {
case Intrinsic::var_annotation:
case Intrinsic::ptr_annotation:
continue;
default:
return false;
}
}
}
return true;
}())
continue;
else if ((I->getName() == "llvm.global_ctors" ||
I->getName() == "llvm.global_dtors") &&
!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_function_pointers)) {
// Function pointers are required to represent structor lists; do not
// translate the variable if function pointers are not available.
continue;
} else if (MDNode *IO = ((*I).getMetadata("io_pipe_id")))
transGlobalIOPipeStorage(&(*I), IO);
else if (!transValue(&(*I), nullptr))
return false;
}
return true;
}
bool LLVMToSPIRVBase::isAnyFunctionReachableFromFunction(
const Function *FS,
const std::unordered_set<const Function *> Funcs) const {
std::unordered_set<const Function *> Done;
std::unordered_set<const Function *> ToDo;
ToDo.insert(FS);
while (!ToDo.empty()) {
auto It = ToDo.begin();
const Function *F = *It;
if (Funcs.find(F) != Funcs.end())
return true;
ToDo.erase(It);
Done.insert(F);
const CallGraphNode *FN = (*CG)[F];
for (unsigned I = 0; I < FN->size(); ++I) {
const CallGraphNode *NN = (*FN)[I];
const Function *NNF = NN->getFunction();
if (!NNF)
continue;
if (Done.find(NNF) == Done.end()) {
ToDo.insert(NNF);
}
}
}
return false;
}
std::vector<SPIRVId>
LLVMToSPIRVBase::collectEntryPointInterfaces(SPIRVFunction *SF, Function *F) {
std::vector<SPIRVId> Interface;
for (auto &GV : M->globals()) {
const auto AS = GV.getAddressSpace();
SPIRVModule *BM = SF->getModule();
if (!BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4))
if (AS != SPIRAS_Input && AS != SPIRAS_Output)
continue;
std::unordered_set<const Function *> Funcs;
for (const auto &U : GV.uses()) {
const Instruction *Inst = dyn_cast<Instruction>(U.getUser());
if (!Inst)
continue;
Funcs.insert(Inst->getFunction());
}
if (isAnyFunctionReachableFromFunction(F, Funcs)) {
SPIRVWord ModuleVersion = static_cast<SPIRVWord>(BM->getSPIRVVersion());
if (AS != SPIRAS_Input && AS != SPIRAS_Output &&
ModuleVersion < static_cast<SPIRVWord>(VersionNumber::SPIRV_1_4))
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
Interface.push_back(ValueMap[&GV]->getId());
}
}
return Interface;
}
void LLVMToSPIRVBase::mutateFuncArgType(
const std::unordered_map<unsigned, Type *> &ChangedType, Function *F) {
for (auto &I : ChangedType) {
for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ++UI) {
auto *Call = dyn_cast<CallInst>(*UI);
if (!Call)
continue;
auto *Arg = Call->getArgOperand(I.first);
auto *OrigTy = Arg->getType();
if (OrigTy == I.second)
continue;
SPIRVDBG(dbgs() << "[mutate arg type] " << *Call << ", " << *Arg << '\n');
auto CastF = M->getOrInsertFunction(SPCV_CAST, I.second, OrigTy);
std::vector<Value *> Args;
Args.push_back(Arg);
auto *Cast = CallInst::Create(CastF, Args, "", Call->getIterator());
Call->replaceUsesOfWith(Arg, Cast);
SPIRVDBG(dbgs() << "[mutate arg type] -> " << *Cast << '\n');
}
}
}
// Propagate contraction requirement of F up the call graph.
void LLVMToSPIRVBase::fpContractUpdateRecursive(Function *F, FPContract FPC) {
std::queue<User *> Users;
for (User *FU : F->users()) {
Users.push(FU);
}
bool EnableLogger = FPC == FPContract::DISABLED && !Users.empty();
if (EnableLogger) {
SPIRVDBG(dbgs() << "[fp-contract] disabled for users of " << F->getName()
<< '\n');
}
while (!Users.empty()) {
User *U = Users.front();
Users.pop();
if (EnableLogger) {
SPIRVDBG(dbgs() << "[fp-contract] user: " << *U << '\n');
}
// Move from an Instruction to its Function
if (Instruction *I = dyn_cast<Instruction>(U)) {
Users.push(I->getFunction());
continue;
}
if (Function *F = dyn_cast<Function>(U)) {
if (!joinFPContract(F, FPC)) {
// FP contract was not updated - no need to propagate
// This also terminates a recursion (if any).
if (EnableLogger) {
SPIRVDBG(dbgs() << "[fp-contract] already disabled " << F->getName()
<< '\n');
}
continue;
}
if (EnableLogger) {
SPIRVDBG(dbgs() << "[fp-contract] disabled for " << F->getName()
<< '\n');
}
for (User *FU : F->users()) {
Users.push(FU);
}
continue;
}
// Unwrap a constant until we reach an Instruction.
// This is checked after the Function, because a Function is also a
// Constant.
if (Constant *C = dyn_cast<Constant>(U)) {
for (User *CU : C->users()) {
Users.push(CU);
}
continue;
}
llvm_unreachable("Unexpected use.");
}
}
/// Returns a range that traverses \p F ensuring that dominator blocks are
/// visited before the blocks they dominate.
///
/// Compared to llvm::ReversePostOrderTraversal which also visits dominators
/// before dominated blocks, this traversal aims to be more stable and will keep
/// basic blocks in their original order as much as possible, only reordering
/// them to visit dominators ahead of their dominated blocks when needed. \p DT
/// is not copied by this function and needs to outlive any iterators created
/// from this range.
static auto stablePreDominatorTraversal(Function &F, const DominatorTree &DT) {
// A local iterator type for traversing a function in the desired order.
class StablePreDominatorIterator
: public iterator_facade_base<StablePreDominatorIterator,
std::forward_iterator_tag, BasicBlock> {
// The passed DominatorTree; may be unset for end iterators.
const DominatorTree *DT = nullptr;
// The set of basic blocks already visited in this traversal.
SmallPtrSet<const BasicBlock *, 4> VisitedBBs;
// The next basic block in original function order, or nullptr if the
// traversal is over.
BasicBlock *NextBB = nullptr;
// The current basic block in the traversal, or nullptr for end iterators.
BasicBlock *CurBB = nullptr;
// Returns the most immediate dominator of \p BB which does not have an
// unvisited dominator and so can be visited in this traversal.
BasicBlock *visitableDominator(BasicBlock *BB) const {
// Find BB's dominator; if there is none, BB can be visited immediately.
const auto *const BBNode = DT->getNode(BB);
if (!BBNode)
return BB;
const auto *const DomNode = BBNode->getIDom();
if (!DomNode)
return BB;
BasicBlock *const Dominator = DomNode->getBlock();
// If the dominator's been visited, BB can now be visited.
if (VisitedBBs.contains(Dominator))
return BB;
// Otherwise, find the dominator's visitable dominator instead.
return visitableDominator(Dominator);
}
// Advances the iterator and returns the next basic block to be visited in
// the traversal.
BasicBlock *next() {
// If NextBB is nullptr, the end of the traversal has been reached.
if (!NextBB)
return nullptr;
// Check if NextBB has already been visited; if so, advance past it.
if (VisitedBBs.contains(NextBB)) {
NextBB = NextBB->getNextNode();
return next();
}
// If NextBB is unvisited, visit its next visitable dominator.
BasicBlock *const ToVisit = visitableDominator(NextBB);
VisitedBBs.insert(ToVisit);
return ToVisit;
}
public:
// Constructs an end iterator.
StablePreDominatorIterator() {}
// Constructs a begin iterator at the start of \p F.
StablePreDominatorIterator(Function &F, const DominatorTree &DT)
: DT(&DT), NextBB(&F.getEntryBlock()) {
++*this;
}
// Methods required by iterator_facade_base.
bool operator==(const StablePreDominatorIterator &Other) const {
return CurBB == Other.CurBB;
}
BasicBlock &operator*() const { return *CurBB; }
StablePreDominatorIterator &operator++() {
CurBB = next();
return *this;
}
};
return make_range(StablePreDominatorIterator(F, DT),
StablePreDominatorIterator());
}
void LLVMToSPIRVBase::transFunction(Function *I) {
SPIRVFunction *BF = transFunctionDecl(I);
// Creating all basic blocks before creating any instruction. SPIR-V requires
// that blocks appear after their dominators, so stablePreDominatorTraversal
// is used to ensure blocks are written in the right order.
const DominatorTree DT(*I);
for (BasicBlock &FI : stablePreDominatorTraversal(*I, DT)) {
FI.convertFromNewDbgValues();
transValue(&FI, nullptr);
}
for (auto &FI : *I) {
SPIRVBasicBlock *BB =
static_cast<SPIRVBasicBlock *>(transValue(&FI, nullptr));
for (auto &BI : FI) {
transValue(&BI, BB, false);
}
}
// Enable FP contraction unless proven otherwise
joinFPContract(I, FPContract::ENABLED);
fpContractUpdateRecursive(I, getFPContract(I));
if (isKernel(I)) {
auto Interface = collectEntryPointInterfaces(BF, I);
BM->addEntryPoint(ExecutionModelKernel, BF->getId(), BF->getName(),
Interface);
}
}
bool isEmptyLLVMModule(Module *M) {
return M->empty() && // No functions
M->global_empty(); // No global variables
}
bool LLVMToSPIRVBase::translate() {
BM->setGeneratorVer(KTranslatorVer);
if (isEmptyLLVMModule(M))
BM->addCapability(CapabilityLinkage);
if (!lowerBuiltinCallsToVariables(M))
return false;
// Use the type scavenger to recover pointer element types.
Scavenger = std::make_unique<SPIRVTypeScavenger>(*M);
if (!transSourceLanguage())
return false;
if (!transExtension())
return false;
if (!transBuiltinSet())
return false;
if (!transAddressingMode())
return false;
if (!transGlobalVariables())
return false;
for (auto &F : *M) {
auto *FT = F.getFunctionType();
std::unordered_map<unsigned, Type *> ChangedType;
oclGetMutatedArgumentTypesByBuiltin(FT, ChangedType, &F);
mutateFuncArgType(ChangedType, &F);
}
// SPIR-V logical layout requires all function declarations go before
// function definitions.
std::vector<Function *> Decls, Defs;
for (auto &F : *M) {
if (isBuiltinTransToInst(&F) || isBuiltinTransToExtInst(&F) ||
F.getName().starts_with(SPCV_CAST) ||
F.getName().starts_with(LLVM_MEMCPY) ||
F.getName().starts_with(SAMPLER_INIT))
continue;
if (F.isDeclaration())
Decls.push_back(&F);
else
Defs.push_back(&F);
}
for (auto *I : Decls)
transFunctionDecl(I);
for (auto *I : Defs)
transFunction(I);
if (!transMetadata())
return false;
if (!transExecutionMode())
return false;
BM->resolveUnknownStructFields();
DbgTran->transDebugMetadata();
return true;
}
llvm::IntegerType *LLVMToSPIRVBase::getSizetType(unsigned AS) {
return IntegerType::getIntNTy(M->getContext(),
M->getDataLayout().getPointerSizeInBits(AS));
}
void LLVMToSPIRVBase::oclGetMutatedArgumentTypesByBuiltin(
llvm::FunctionType *FT, std::unordered_map<unsigned, Type *> &ChangedType,
Function *F) {
StringRef Demangled;
if (!oclIsBuiltin(F->getName(), Demangled))
return;
// Note: kSPIRVName::ConvertHandleToSampledImageINTEL contains
// kSPIRVName::SampledImage as a substring, but we still want to return in
// this case.
if (Demangled.find(kSPIRVName::SampledImage) == std::string::npos ||
Demangled.find(kSPIRVName::ConvertHandleToSampledImageINTEL) !=
std::string::npos)
return;
if (FT->getParamType(1)->isIntegerTy())
ChangedType[1] = getSPIRVType(OpTypeSampler, true);
}
SPIRVValue *LLVMToSPIRVBase::transBuiltinToConstant(StringRef DemangledName,
CallInst *CI) {
Op OC = getSPIRVFuncOC(DemangledName);
if (!isSpecConstantOpCode(OC))
return nullptr;
if (OC == spv::OpSpecConstantComposite) {
return BM->addSpecConstantComposite(transType(CI->getType()),
transValue(getArguments(CI), nullptr));
}
Value *V = CI->getArgOperand(1);
Type *Ty = CI->getType();
assert(((Ty == V->getType()) ||
// If bool is stored into memory, then clang will emit it as i8,
// however for other usages of bool (like return type of a function),
// it is emitted as i1.
// Therefore, situation when we encounter
// i1 _Z20__spirv_SpecConstant(i32, i8) is valid
(Ty->isIntegerTy(1) && V->getType()->isIntegerTy(8))) &&
"Type mismatch!");
uint64_t Val = 0;
if (Ty->isIntegerTy())
Val = cast<ConstantInt>(V)->getZExtValue();
else if (Ty->isFloatingPointTy())
Val = cast<ConstantFP>(V)->getValueAPF().bitcastToAPInt().getZExtValue();
else
return nullptr;
SPIRVValue *SC = BM->addSpecConstant(transType(Ty), Val);
return SC;
}
SPIRVInstruction *LLVMToSPIRVBase::transBuiltinToInst(StringRef DemangledName,
CallInst *CI,
SPIRVBasicBlock *BB) {
SmallVector<std::string, 2> Dec;
auto OC = getSPIRVFuncOC(DemangledName, &Dec);
if (OC == OpNop)
return nullptr;
if (OpReadPipeBlockingINTEL <= OC && OC <= OpWritePipeBlockingINTEL &&
!BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_blocking_pipes))
return nullptr;
if (OpFixedSqrtINTEL <= OC && OC <= OpFixedExpINTEL)
BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_arbitrary_precision_fixed_point),
SPIRVEC_InvalidInstruction,
CI->getCalledOperand()->getName().str() +
"\nFixed point instructions can't be translated correctly without "
"enabled SPV_INTEL_arbitrary_precision_fixed_point extension!\n");
if ((OpArbitraryFloatSinCosPiINTEL <= OC &&
OC <= OpArbitraryFloatCastToIntINTEL) ||
(OpArbitraryFloatAddINTEL <= OC && OC <= OpArbitraryFloatPowNINTEL))
BM->getErrorLog().checkError(
BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_arbitrary_precision_floating_point),
SPIRVEC_InvalidInstruction,
CI->getCalledOperand()->getName().str() +
"\nFloating point instructions can't be translated correctly "
"without enabled SPV_INTEL_arbitrary_precision_floating_point "
"extension!\n");
auto *Inst = transBuiltinToInstWithoutDecoration(OC, CI, BB);
addDecorations(Inst, Dec);
return Inst;
}
bool LLVMToSPIRVBase::transExecutionMode() {
if (auto NMD = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::ExecutionMode)) {
while (!NMD.atEnd()) {
unsigned EMode = ~0U;
Function *F = nullptr;
auto N = NMD.nextOp(); /* execution mode MDNode */
N.get(F).get(EMode);
SPIRVFunction *BF = static_cast<SPIRVFunction *>(getTranslatedValue(F));
assert(BF && "Invalid kernel function");
if (!BF)
return false;
auto AddSingleArgExecutionMode = [&](ExecutionMode EMode) {
uint32_t Arg = ~0u;
N.get(Arg);
BF->addExecutionMode(
BM->add(new SPIRVExecutionMode(OpExecutionMode, BF, EMode, Arg)));
};
switch (EMode) {
case spv::ExecutionModeContractionOff:
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode))));
break;
case spv::ExecutionModeInitializer:
case spv::ExecutionModeFinalizer:
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_1)) {
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode))));
} else {
getErrorLog().checkError(false, SPIRVEC_Requires1_1,
"Initializer/Finalizer Execution Mode");
return false;
}
break;
case spv::ExecutionModeLocalSize:
case spv::ExecutionModeLocalSizeHint: {
unsigned X = 0, Y = 0, Z = 0;
N.get(X).get(Y).get(Z);
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode), X, Y, Z)));
} break;
case spv::ExecutionModeMaxWorkgroupSizeINTEL: {
if (BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_kernel_attributes)) {
unsigned X = 0, Y = 0, Z = 0;
N.get(X).get(Y).get(Z);
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode), X, Y,
Z)));
BM->addExtension(ExtensionID::SPV_INTEL_kernel_attributes);
BM->addCapability(CapabilityKernelAttributesINTEL);
}
} break;
case spv::ExecutionModeNoGlobalOffsetINTEL: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_kernel_attributes))
break;
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode))));
BM->addExtension(ExtensionID::SPV_INTEL_kernel_attributes);
BM->addCapability(CapabilityKernelAttributesINTEL);
} break;
case spv::ExecutionModeVecTypeHint:
case spv::ExecutionModeSubgroupSize:
case spv::ExecutionModeSubgroupsPerWorkgroup:
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
break;
case spv::ExecutionModeNumSIMDWorkitemsINTEL:
case spv::ExecutionModeSchedulerTargetFmaxMhzINTEL:
case spv::ExecutionModeMaxWorkDimINTEL:
case spv::ExecutionModeRegisterMapInterfaceINTEL: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_kernel_attributes))
break;
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
BM->addExtension(ExtensionID::SPV_INTEL_kernel_attributes);
BM->addCapability(CapabilityFPGAKernelAttributesINTEL);
// RegisterMapInterfaceINTEL mode is defined by the
// CapabilityFPGAKernelAttributesv2INTEL capability and that
// capability implicitly defines CapabilityFPGAKernelAttributesINTEL
if (EMode == spv::ExecutionModeRegisterMapInterfaceINTEL)
BM->addCapability(CapabilityFPGAKernelAttributesv2INTEL);
} break;
case spv::ExecutionModeStreamingInterfaceINTEL: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_kernel_attributes))
break;
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
BM->addExtension(ExtensionID::SPV_INTEL_kernel_attributes);
BM->addCapability(CapabilityFPGAKernelAttributesINTEL);
} break;
case spv::ExecutionModeSharedLocalMemorySizeINTEL: {
if (!BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute))
break;
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
} break;
case spv::ExecutionModeNamedBarrierCountINTEL: {
if (!BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_vector_compute))
break;
unsigned NBarrierCnt = 0;
N.get(NBarrierCnt);
BF->addExecutionMode(BM->add(new SPIRVExecutionMode(
OpExecutionMode, BF, static_cast<ExecutionMode>(EMode),
NBarrierCnt)));
BM->addExtension(ExtensionID::SPV_INTEL_vector_compute);
BM->addCapability(CapabilityVectorComputeINTEL);
} break;
case spv::ExecutionModeDenormPreserve:
case spv::ExecutionModeDenormFlushToZero:
case spv::ExecutionModeSignedZeroInfNanPreserve:
case spv::ExecutionModeRoundingModeRTE:
case spv::ExecutionModeRoundingModeRTZ: {
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
BM->setMinSPIRVVersion(VersionNumber::SPIRV_1_4);
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
} else if (BM->isAllowedToUseExtension(
ExtensionID::SPV_KHR_float_controls)) {
BM->addExtension(ExtensionID::SPV_KHR_float_controls);
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
}
} break;
case spv::ExecutionModeRoundingModeRTPINTEL:
case spv::ExecutionModeRoundingModeRTNINTEL:
case spv::ExecutionModeFloatingPointModeALTINTEL:
case spv::ExecutionModeFloatingPointModeIEEEINTEL: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_float_controls2))
break;
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
} break;
case spv::internal::ExecutionModeNamedSubgroupSizeINTEL: {
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_subgroup_requirements))
break;
AddSingleArgExecutionMode(static_cast<ExecutionMode>(EMode));
} break;
default:
llvm_unreachable("invalid execution mode");
}
}
}
transFPContract();
return true;
}
void LLVMToSPIRVBase::transFPContract() {
FPContractMode Mode = BM->getFPContractMode();
for (Function &F : *M) {
SPIRVValue *TranslatedF = getTranslatedValue(&F);
if (!TranslatedF) {
continue;
}
SPIRVFunction *BF = static_cast<SPIRVFunction *>(TranslatedF);
bool IsKernelEntryPoint =
BF->getModule()->isEntryPoint(spv::ExecutionModelKernel, BF->getId());
if (!IsKernelEntryPoint)
continue;
FPContract FPC = getFPContract(&F);
assert(FPC != FPContract::UNDEF);
bool DisableContraction = false;
switch (Mode) {
case FPContractMode::Fast:
DisableContraction = false;
break;
case FPContractMode::On:
DisableContraction = FPC == FPContract::DISABLED;
break;
case FPContractMode::Off:
DisableContraction = true;
break;
}
if (DisableContraction) {
BF->addExecutionMode(BF->getModule()->add(new SPIRVExecutionMode(
OpExecutionMode, BF, spv::ExecutionModeContractionOff)));
}
}
}
bool LLVMToSPIRVBase::transMetadata() {
if (!transOCLMetadata())
return false;
auto Model = getMemoryModel(*M);
if (Model != SPIRVMemoryModelKind::MemoryModelMax)
BM->setMemoryModel(static_cast<SPIRVMemoryModelKind>(Model));
return true;
}
// Work around to translate kernel_arg_type and kernel_arg_type_qual metadata
static void transKernelArgTypeMD(SPIRVModule *BM, Function *F, MDNode *MD,
std::string MDName) {
std::string Prefix = kSPIRVName::EntrypointPrefix;
std::string Name = F->getName().str().substr(Prefix.size());
std::string KernelArgTypesMDStr = std::string(MDName) + "." + Name + ".";
for (const auto &TyOp : MD->operands())
KernelArgTypesMDStr += cast<MDString>(TyOp)->getString().str() + ",";
BM->getString(KernelArgTypesMDStr);
}
bool LLVMToSPIRVBase::transOCLMetadata() {
for (auto &F : *M) {
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
continue;
SPIRVFunction *BF = static_cast<SPIRVFunction *>(getTranslatedValue(&F));
assert(BF && "Kernel function should be translated first");
// Create 'OpString' as a workaround to store information about
// *orignal* (typedef'ed, unsigned integers) type names of kernel arguments.
// OpString "kernel_arg_type.%kernel_name%.typename0,typename1,..."
if (auto *KernelArgType = F.getMetadata(SPIR_MD_KERNEL_ARG_TYPE))
if (BM->shouldPreserveOCLKernelArgTypeMetadataThroughString())
transKernelArgTypeMD(BM, &F, KernelArgType, SPIR_MD_KERNEL_ARG_TYPE);
if (auto *KernelArgTypeQual = F.getMetadata(SPIR_MD_KERNEL_ARG_TYPE_QUAL)) {
foreachKernelArgMD(
KernelArgTypeQual, BF,
[](const std::string &Str, SPIRVFunctionParameter *BA) {
if (Str.find("volatile") != std::string::npos)
BA->addDecorate(new SPIRVDecorate(DecorationVolatile, BA));
if (Str.find("restrict") != std::string::npos)
BA->addDecorate(
new SPIRVDecorate(DecorationFuncParamAttr, BA,
FunctionParameterAttributeNoAlias));
});
// Create 'OpString' as a workaround to store information about
// constant qualifiers of pointer kernel arguments. Store empty string
// for a non constant parameter.
// OpString "kernel_arg_type_qual.%kernel_name%.qual0,qual1,..."
if (BM->shouldPreserveOCLKernelArgTypeMetadataThroughString())
transKernelArgTypeMD(BM, &F, KernelArgTypeQual,
SPIR_MD_KERNEL_ARG_TYPE_QUAL);
}
if (auto *KernelArgName = F.getMetadata(SPIR_MD_KERNEL_ARG_NAME)) {
foreachKernelArgMD(
KernelArgName, BF,
[=](const std::string &Str, SPIRVFunctionParameter *BA) {
BM->setName(BA, Str);
});
}
if (auto *KernArgDecoMD = F.getMetadata(SPIRV_MD_PARAMETER_DECORATIONS))
foreachKernelArgMD(KernArgDecoMD, BF, transMetadataDecorations);
}
return true;
}
bool LLVMToSPIRVBase::transSourceLanguage() {
auto Src = getSPIRVSource(M);
SrcLang = std::get<0>(Src);
SrcLangVer = std::get<1>(Src);
BM->setSourceLanguage(static_cast<SourceLanguage>(SrcLang), SrcLangVer);
return true;
}
bool LLVMToSPIRVBase::transExtension() {
if (auto N = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::Extension)) {
while (!N.atEnd()) {
std::string S;
N.nextOp().get(S);
assert(!S.empty() && "Invalid extension");
ExtensionID ExtID = SPIRVMap<ExtensionID, std::string>::rmap(S);
if (!BM->getErrorLog().checkError(BM->isAllowedToUseExtension(ExtID),
SPIRVEC_RequiresExtension, S))
return false;
BM->getExtension().insert(S);
}
}
if (auto N = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::SourceExtension)) {
while (!N.atEnd()) {
std::string S;
N.nextOp().get(S);
assert(!S.empty() && "Invalid extension");
BM->getSourceExtension().insert(S);
}
}
for (auto &I :
map<SPIRVCapabilityKind>(rmap<OclExt::Kind>(BM->getExtension())))
BM->addCapability(I);
return true;
}
void LLVMToSPIRVBase::dumpUsers(Value *V) {
SPIRVDBG(dbgs() << "Users of " << *V << " :\n");
for (auto UI = V->user_begin(), UE = V->user_end(); UI != UE; ++UI)
SPIRVDBG(dbgs() << " " << **UI << '\n');
}
Op LLVMToSPIRVBase::transBoolOpCode(SPIRVValue *Opn, Op OC) {
if (!Opn->getType()->isTypeVectorOrScalarBool())
return OC;
IntBoolOpMap::find(OC, &OC);
return OC;
}
SPIRVInstruction *
LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
SPIRVBasicBlock *BB) {
// OpAtomicCompareExchangeWeak is not "weak" at all,
// but instead has the same semantics as OpAtomicCompareExchange.
// Moreover, OpAtomicCompareExchangeWeak has been deprecated.
if (OC == OpAtomicCompareExchangeWeak)
OC = OpAtomicCompareExchange;
// We should do this replacement only for SPIR-V 1.5, as OpLessOrGreater is
// deprecated there. However we do such replacement for the usual pipeline
// (not via SPIR-V friendly calls) without minding the version, so we can do
// such thing here as well.
if (OC == OpLessOrGreater &&
BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_5))
OC = OpFOrdNotEqual;
if (isGroupOpCode(OC))
BM->addCapability(CapabilityGroups);
switch (static_cast<size_t>(OC)) {
case OpControlBarrier: {
auto BArgs = transValue(getArguments(CI), BB);
return BM->addControlBarrierInst(BArgs[0], BArgs[1], BArgs[2], BB);
} break;
case OpGroupAsyncCopy: {
auto BArgs = transValue(getArguments(CI), BB);
return BM->addAsyncGroupCopy(BArgs[0], BArgs[1], BArgs[2], BArgs[3],
BArgs[4], BArgs[5], BB);
} break;
case OpVectorExtractDynamic: {
auto BArgs = transValue(getArguments(CI), BB);
return BM->addVectorExtractDynamicInst(BArgs[0], BArgs[1], BB);
} break;
case OpVectorInsertDynamic: {
auto BArgs = transValue(getArguments(CI), BB);
return BM->addVectorInsertDynamicInst(BArgs[0], BArgs[1], BArgs[2], BB);
} break;
case OpSampledImage: {
// Clang can generate SPIRV-friendly call for OpSampledImage instruction,
// i.e. __spirv_SampledImage... But it can't generate correct return type
// for this call, because there is no support for type corresponding to
// OpTypeSampledImage. So, in this case, we create the required type here.
Value *Image = CI->getArgOperand(0);
Type *SampledImgTy =
adjustImageType(getCallValueType(CI, 0), kSPIRVTypeName::Image,
kSPIRVTypeName::SampledImg);
Value *Sampler = CI->getArgOperand(1);
return BM->addSampledImageInst(transType(SampledImgTy),
transValue(Image, BB),
transValue(Sampler, BB), BB);
}
case OpImageRead:
case OpImageSampleExplicitLod:
case OpImageWrite: {
// Image Op needs handling of SignExtend or ZeroExtend image operands.
auto Args = getArguments(CI);
SPIRVType *SPRetTy =
CI->getType()->isVoidTy() ? nullptr : transScavengedType(CI);
auto *SPI = SPIRVInstTemplateBase::create(OC);
std::vector<SPIRVWord> SPArgs;
for (size_t I = 0, E = Args.size(); I != E; ++I) {
if (Args[I]->getType()->isPointerTy()) {
[[maybe_unused]] Value *Pointee = Args[I]->stripPointerCasts();
assert((Pointee == Args[I] || !isa<Function>(Pointee)) &&
"Illegal use of a function pointer type");
}
SPArgs.push_back(SPI->isOperandLiteral(I)
? cast<ConstantInt>(Args[I])->getZExtValue()
: transValue(Args[I], BB)->getId());
}
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4)) {
size_t ImOpIdx = getImageOperandsIndex(OC);
if (Args.size() > ImOpIdx) {
// Update existing ImageOperands with SignExtendMask/ZeroExtendMask.
if (auto *ImOp = dyn_cast<ConstantInt>(Args[ImOpIdx])) {
uint64_t ImOpVal = ImOp->getZExtValue();
unsigned SignZeroExtMasks =
ImageOperandsMask::ImageOperandsSignExtendMask |
ImageOperandsMask::ImageOperandsZeroExtendMask;
if (!(ImOpVal & SignZeroExtMasks))
if (int SignZeroExt = getImageSignZeroExt(CI->getCalledFunction()))
SPArgs[ImOpIdx] = ImOpVal | SignZeroExt;
}
} else {
// Add new ImageOperands argument.
if (int SignZeroExt = getImageSignZeroExt(CI->getCalledFunction()))
SPArgs.push_back(SignZeroExt);
}
}
BM->addInstTemplate(SPI, SPArgs, BB, SPRetTy);
return SPI;
}
case OpFixedSqrtINTEL:
case OpFixedRecipINTEL:
case OpFixedRsqrtINTEL:
case OpFixedSinINTEL:
case OpFixedCosINTEL:
case OpFixedSinCosINTEL:
case OpFixedSinPiINTEL:
case OpFixedCosPiINTEL:
case OpFixedSinCosPiINTEL:
case OpFixedLogINTEL:
case OpFixedExpINTEL: {
// LLVM fixed point functions return value:
// iN (arbitrary precision integer of N bits length)
// Arguments:
// A(iN), S(i1), I(i32), rI(i32), Quantization(i32), Overflow(i32)
// where A - integer input of any width.
// SPIR-V fixed point instruction contains:
// <id>ResTy Res<id> In<id> \
// Literal S Literal I Literal rI Literal Q Literal O
Type *ResTy = CI->getType();
auto OpItr = CI->value_op_begin();
auto OpEnd = OpItr + CI->arg_size();
// If the return type of an instruction is wider than 64-bit, then this
// instruction will return via 'sret' argument added into the arguments
// list. Here we reverse this, removing 'sret' argument and restoring
// the original return type.
if (CI->hasStructRetAttr()) {
assert(ResTy->isVoidTy() && "Return type is not void");
ResTy = CI->getParamStructRetType(0);
OpItr++;
}
// Input - integer input of any width or 'byval' pointer to this integer
SPIRVValue *Input = transValue(*OpItr, BB);
if (OpItr->getType()->isPointerTy())
Input = BM->addLoadInst(Input, {}, BB);
OpItr++;
std::vector<SPIRVWord> Literals;
std::transform(OpItr, OpEnd, std::back_inserter(Literals), [](auto *O) {
return cast<llvm::ConstantInt>(O)->getZExtValue();
});
auto *APIntInst =
BM->addFixedPointIntelInst(OC, transType(ResTy), Input, Literals, BB);
if (!CI->hasStructRetAttr())
return APIntInst;
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), APIntInst, {},
BB);
}
case OpArbitraryFloatCastINTEL:
case OpArbitraryFloatCastFromIntINTEL:
case OpArbitraryFloatCastToIntINTEL:
case OpArbitraryFloatRecipINTEL:
case OpArbitraryFloatRSqrtINTEL:
case OpArbitraryFloatCbrtINTEL:
case OpArbitraryFloatSqrtINTEL:
case OpArbitraryFloatLogINTEL:
case OpArbitraryFloatLog2INTEL:
case OpArbitraryFloatLog10INTEL:
case OpArbitraryFloatLog1pINTEL:
case OpArbitraryFloatExpINTEL:
case OpArbitraryFloatExp2INTEL:
case OpArbitraryFloatExp10INTEL:
case OpArbitraryFloatExpm1INTEL:
case OpArbitraryFloatSinINTEL:
case OpArbitraryFloatCosINTEL:
case OpArbitraryFloatSinCosINTEL:
case OpArbitraryFloatSinPiINTEL:
case OpArbitraryFloatCosPiINTEL:
case OpArbitraryFloatSinCosPiINTEL:
case OpArbitraryFloatASinINTEL:
case OpArbitraryFloatASinPiINTEL:
case OpArbitraryFloatACosINTEL:
case OpArbitraryFloatACosPiINTEL:
case OpArbitraryFloatATanINTEL:
case OpArbitraryFloatATanPiINTEL: {
// Format of instruction CastFromInt:
// LLVM arbitrary floating point functions return value type:
// iN (arbitrary precision integer of N bits length)
// Arguments: A(iN), Mout(i32), FromSign(bool), EnableSubnormals(i32),
// RoundingMode(i32), RoundingAccuracy(i32)
// where A and return values are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal Mout Literal FromSign
// Literal EnableSubnormals Literal RoundingMode
// Literal RoundingAccuracy
// Format of instruction CastToInt:
// LLVM arbitrary floating point functions return value: iN
// Arguments: A(iN), MA(i32), ToSign(bool), EnableSubnormals(i32),
// RoundingMode(i32), RoundingAccuracy(i32)
// where A and return values are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal MA Literal ToSign
// Literal EnableSubnormals Literal RoundingMode
// Literal RoundingAccuracy
// Format of other instructions:
// LLVM arbitrary floating point functions return value: iN
// Arguments: A(iN), MA(i32), Mout(i32), EnableSubnormals(i32),
// RoundingMode(i32), RoundingAccuracy(i32)
// where A and return values are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal MA Literal Mout
// Literal EnableSubnormals Literal RoundingMode
// Literal RoundingAccuracy
Type *ResTy = CI->getType();
auto OpItr = CI->value_op_begin();
auto OpEnd = OpItr + CI->arg_size();
// If the return type of an instruction is wider than 64-bit, then this
// instruction will return via 'sret' argument added into the arguments
// list. Here we reverse this, removing 'sret' argument and restoring
// the original return type.
if (CI->hasStructRetAttr()) {
assert(ResTy->isVoidTy() && "Return type is not void");
ResTy = CI->getParamStructRetType(0);
OpItr++;
}
// Input - integer input of any width or 'byval' pointer to this integer
SPIRVValue *Input = transValue(*OpItr, BB);
if (OpItr->getType()->isPointerTy())
Input = BM->addLoadInst(Input, {}, BB);
OpItr++;
std::vector<SPIRVWord> Literals;
std::transform(OpItr, OpEnd, std::back_inserter(Literals), [](auto *O) {
return cast<llvm::ConstantInt>(O)->getZExtValue();
});
auto *APIntInst = BM->addArbFloatPointIntelInst(OC, transType(ResTy), Input,
nullptr, Literals, BB);
if (!CI->hasStructRetAttr())
return APIntInst;
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), APIntInst, {},
BB);
}
case OpArbitraryFloatAddINTEL:
case OpArbitraryFloatSubINTEL:
case OpArbitraryFloatMulINTEL:
case OpArbitraryFloatDivINTEL:
case OpArbitraryFloatGTINTEL:
case OpArbitraryFloatGEINTEL:
case OpArbitraryFloatLTINTEL:
case OpArbitraryFloatLEINTEL:
case OpArbitraryFloatEQINTEL:
case OpArbitraryFloatHypotINTEL:
case OpArbitraryFloatATan2INTEL:
case OpArbitraryFloatPowINTEL:
case OpArbitraryFloatPowRINTEL:
case OpArbitraryFloatPowNINTEL: {
// Format of instructions Add, Sub, Mul, Div, Hypot, ATan2, Pow, PowR:
// LLVM arbitrary floating point functions return value:
// iN (arbitrary precision integer of N bits length)
// Arguments: A(iN), MA(i32), B(iN), MB(i32), Mout(i32),
// EnableSubnormals(i32), RoundingMode(i32),
// RoundingAccuracy(i32)
// where A, B and return values are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal MB Literal Mout
// Literal EnableSubnormals Literal RoundingMode
// Literal RoundingAccuracy
// Format of instruction PowN:
// LLVM arbitrary floating point functions return value: iN
// Arguments: A(iN), MA(i32), B(iN), SignOfB(i1), Mout(i32),
// EnableSubnormals(i32), RoundingMode(i32),
// RoundingAccuracy(i32)
// where A, B and return values are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal SignOfB Literal Mout
// Literal EnableSubnormals Literal RoundingMode
// Literal RoundingAccuracy
// Format of instructions GT, GE, LT, LE, EQ:
// LLVM arbitrary floating point functions return value: Bool
// Arguments: A(iN), MA(i32), B(iN), MB(i32)
// where A and B are of arbitrary precision integer type.
// SPIR-V arbitrary floating point instruction layout:
// <id>ResTy Res<id> A<id> Literal MA B<id> Literal MB
Type *ResTy = CI->getType();
auto OpItr = CI->value_op_begin();
auto OpEnd = OpItr + CI->arg_size();
// If the return type of an instruction is wider than 64-bit, then this
// instruction will return via 'sret' argument added into the arguments
// list. Here we reverse this, removing 'sret' argument and restoring
// the original return type.
if (CI->hasStructRetAttr()) {
assert(ResTy->isVoidTy() && "Return type is not void");
ResTy = CI->getParamStructRetType(0);
OpItr++;
}
// InA - integer input of any width or 'byval' pointer to this integer
SPIRVValue *InA = transValue(*OpItr, BB);
if (OpItr->getType()->isPointerTy())
InA = BM->addLoadInst(InA, {}, BB);
OpItr++;
std::vector<SPIRVWord> Literals;
Literals.push_back(cast<llvm::ConstantInt>(*OpItr++)->getZExtValue());
// InB - integer input of any width or 'byval' pointer to this integer
SPIRVValue *InB = transValue(*OpItr, BB);
if (OpItr->getType()->isPointerTy()) {
std::vector<SPIRVWord> Mem;
InB = BM->addLoadInst(InB, Mem, BB);
}
OpItr++;
std::transform(OpItr, OpEnd, std::back_inserter(Literals), [](auto *O) {
return cast<llvm::ConstantInt>(O)->getZExtValue();
});
auto *APIntInst = BM->addArbFloatPointIntelInst(OC, transType(ResTy), InA,
InB, Literals, BB);
if (!CI->hasStructRetAttr())
return APIntInst;
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), APIntInst, {},
BB);
}
case internal::OpTaskSequenceGetINTEL: {
Type *ResTy = nullptr;
auto OpItr = CI->value_op_begin();
if (CI->hasStructRetAttr()) {
assert(CI->getType()->isVoidTy() && "Return type is not void");
ResTy = CI->getParamStructRetType(0);
OpItr++;
}
SPIRVType *RetTy = ResTy ? transType(ResTy) : transScavengedType(CI);
auto *TaskSeqGet =
BM->addTaskSequenceGetINTELInst(RetTy, transValue(*OpItr++, BB), BB);
if (!CI->hasStructRetAttr())
return TaskSeqGet;
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), TaskSeqGet,
{}, BB);
}
case OpLoad: {
std::vector<SPIRVWord> MemoryAccess;
assert(CI->arg_size() > 0 && "Expected at least 1 operand for OpLoad call");
for (size_t I = 1; I < CI->arg_size(); ++I)
MemoryAccess.push_back(
cast<ConstantInt>(CI->getArgOperand(I))->getZExtValue());
return BM->addLoadInst(transValue(CI->getArgOperand(0), BB), MemoryAccess,
BB);
}
case OpStore: {
std::vector<SPIRVWord> MemoryAccess;
assert(CI->arg_size() > 1 &&
"Expected at least 2 operands for OpStore call");
for (size_t I = 2; I < CI->arg_size(); ++I)
MemoryAccess.push_back(
cast<ConstantInt>(CI->getArgOperand(I))->getZExtValue());
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB),
transValue(CI->getArgOperand(1), BB), MemoryAccess,
BB);
}
case OpCompositeConstruct: {
std::vector<SPIRVId> Operands = {
transValue(CI->getArgOperand(0), BB)->getId()};
return BM->addCompositeConstructInst(transType(CI->getType()), Operands,
BB);
}
case OpIAddCarry:
case OpISubBorrow:
case OpUMulExtended:
case OpSMulExtended: {
Function *F = CI->getCalledFunction();
StructType *St = cast<StructType>(F->getParamStructRetType(0));
SPIRVValue *V = BM->addBinaryInst(OC, transType(St),
transValue(CI->getArgOperand(1), BB),
transValue(CI->getArgOperand(2), BB), BB);
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), V, {}, BB);
}
case OpCooperativeMatrixLengthKHR: {
return BM->addCooperativeMatrixLengthKHRInst(
transScavengedType(CI), transType(CI->getArgOperand(0)->getType()), BB);
}
case OpGroupNonUniformShuffleDown: {
Function *F = CI->getCalledFunction();
if (F->arg_size() && F->getArg(0)->hasStructRetAttr()) {
StructType *St = cast<StructType>(F->getParamStructRetType(0));
assert(isSYCLHalfType(St) || isSYCLBfloat16Type(St));
SPIRVValue *InValue =
transValue(CI->getArgOperand(0)->stripPointerCasts(), BB);
SPIRVId ScopeId = transValue(CI->getArgOperand(1), BB)->getId();
SPIRVValue *Delta = transValue(CI->getArgOperand(3), BB);
SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB);
Type *MemberTy = St->getElementType(0);
SPIRVType *ElementTy = transType(MemberTy);
SPIRVValue *Element0 =
BM->addCompositeExtractInst(ElementTy, Composite0, {0}, BB);
SPIRVValue *Src =
BM->addGroupInst(OpGroupNonUniformShuffleDown, ElementTy,
static_cast<Scope>(ScopeId), {Element0, Delta}, BB);
SPIRVValue *Composite1 =
BM->addCompositeInsertInst(Src, Composite0, {0}, BB);
return BM->addStoreInst(InValue, Composite1, {}, BB);
}
[[fallthrough]];
}
default: {
if (isCvtOpCode(OC) && OC != OpGenericCastToPtrExplicit) {
return BM->addUnaryInst(OC, transScavengedType(CI),
transValue(CI->getArgOperand(0), BB), BB);
} else if (isCmpOpCode(OC) || isUnaryPredicateOpCode(OC)) {
auto *ResultTy = CI->getType();
Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
auto IsVector = ResultTy->isVectorTy();
if (IsVector)
BoolTy = FixedVectorType::get(
BoolTy, cast<FixedVectorType>(ResultTy)->getNumElements());
auto *BBT = transType(BoolTy);
SPIRVInstruction *Res;
if (isCmpOpCode(OC)) {
assert(CI && CI->arg_size() == 2 && "Invalid call inst");
Res = BM->addCmpInst(OC, BBT, transValue(CI->getArgOperand(0), BB),
transValue(CI->getArgOperand(1), BB), BB);
} else {
assert(CI && CI->arg_size() == 1 && "Invalid call inst");
Res =
BM->addUnaryInst(OC, BBT, transValue(CI->getArgOperand(0), BB), BB);
}
// OpenCL C and OpenCL C++ built-ins may have different return type
if (ResultTy == BoolTy)
return Res;
assert(IsVector || (!IsVector && ResultTy->isIntegerTy(32)));
auto *Zero = transValue(Constant::getNullValue(ResultTy), BB);
auto *One = transValue(
IsVector ? Constant::getAllOnesValue(ResultTy) : getInt32(M, 1), BB);
return BM->addSelectInst(Res, One, Zero, BB);
} else if (isBinaryOpCode(OC)) {
assert(CI && CI->arg_size() == 2 && "Invalid call inst");
return BM->addBinaryInst(OC, transScavengedType(CI),
transValue(CI->getArgOperand(0), BB),
transValue(CI->getArgOperand(1), BB), BB);
} else if (CI->arg_size() == 1 && !CI->getType()->isVoidTy() &&
!hasExecScope(OC) && !isAtomicOpCode(OC)) {
return BM->addUnaryInst(OC, transScavengedType(CI),
transValue(CI->getArgOperand(0), BB), BB);
} else {
auto Args = getArguments(CI);
SPIRVType *SPRetTy = nullptr;
Type *RetTy = CI->getType();
auto *F = CI->getCalledFunction();
if (!RetTy->isVoidTy()) {
SPRetTy = transScavengedType(CI);
} else if (Args.size() > 0 && F->arg_begin()->hasStructRetAttr()) {
SPRetTy = transType(F->getParamStructRetType(0));
Args.erase(Args.begin());
}
auto *SPI = SPIRVInstTemplateBase::create(OC);
std::vector<SPIRVWord> SPArgs;
for (size_t I = 0, E = Args.size(); I != E; ++I) {
if (Args[I]->getType()->isPointerTy()) {
Value *Pointee = Args[I]->stripPointerCasts();
(void)Pointee;
assert((Pointee == Args[I] || !isa<Function>(Pointee)) &&
"Illegal use of a function pointer type");
}
SPArgs.push_back(SPI->isOperandLiteral(I)
? cast<ConstantInt>(Args[I])->getZExtValue()
: transValue(Args[I], BB)->getId());
}
BM->addInstTemplate(SPI, SPArgs, BB, SPRetTy);
if (!SPRetTy || !SPRetTy->isTypeStruct())
return SPI;
std::vector<SPIRVWord> Mem;
SPIRVDBG(spvdbgs() << *SPI << '\n');
return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), SPI, Mem,
BB);
}
}
}
return nullptr;
}
SPIRV::SPIRVLinkageTypeKind
LLVMToSPIRVBase::transLinkageType(const GlobalValue *GV) {
if (GV->isDeclarationForLinker())
return SPIRVLinkageTypeKind::LinkageTypeImport;
if (GV->hasInternalLinkage() || GV->hasPrivateLinkage())
return spv::internal::LinkageTypeInternal;
if (GV->hasLinkOnceODRLinkage())
if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_linkonce_odr))
return SPIRVLinkageTypeKind::LinkageTypeLinkOnceODR;
return SPIRVLinkageTypeKind::LinkageTypeExport;
}
LLVMToSPIRVBase::FPContract LLVMToSPIRVBase::getFPContract(Function *F) {
auto It = FPContractMap.find(F);
if (It == FPContractMap.end()) {
return FPContract::UNDEF;
}
return It->second;
}
bool LLVMToSPIRVBase::joinFPContract(Function *F, FPContract C) {
FPContract &Existing = FPContractMap[F];
switch (Existing) {
case FPContract::UNDEF:
if (C != FPContract::UNDEF) {
Existing = C;
return true;
}
return false;
case FPContract::ENABLED:
if (C == FPContract::DISABLED) {
Existing = C;
return true;
}
return false;
case FPContract::DISABLED:
return false;
}
llvm_unreachable("Unhandled FPContract value.");
}
} // namespace SPIRV
char LLVMToSPIRVLegacy::ID = 0;
INITIALIZE_PASS_BEGIN(LLVMToSPIRVLegacy, "llvmtospv",
"Translate LLVM to SPIR-V", false, false)
INITIALIZE_PASS_DEPENDENCY(OCLTypeToSPIRVLegacy)
INITIALIZE_PASS_END(LLVMToSPIRVLegacy, "llvmtospv", "Translate LLVM to SPIR-V",
false, false)
ModulePass *llvm::createLLVMToSPIRVLegacy(SPIRVModule *SMod) {
return new LLVMToSPIRVLegacy(SMod);
}
void addPassesForSPIRV(ModulePassManager &PassMgr,
const SPIRV::TranslatorOpts &Opts) {
if (Opts.isSPIRVMemToRegEnabled())
PassMgr.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
PassMgr.addPass(PreprocessMetadataPass());
PassMgr.addPass(SPIRVLowerOCLBlocksPass());
PassMgr.addPass(OCLToSPIRVPass());
PassMgr.addPass(SPIRVRegularizeLLVMPass());
PassMgr.addPass(SPIRVLowerConstExprPass());
PassMgr.addPass(SPIRVLowerBoolPass());
PassMgr.addPass(SPIRVLowerMemmovePass());
PassMgr.addPass(SPIRVLowerLLVMIntrinsicPass(Opts));
PassMgr.addPass(createModuleToFunctionPassAdaptor(
SPIRVLowerBitCastToNonStandardTypePass(Opts)));
}
bool isValidLLVMModule(Module *M, SPIRVErrorLog &ErrorLog) {
if (!M)
return false;
if (isEmptyLLVMModule(M))
return true;
Triple TT(M->getTargetTriple());
if (!ErrorLog.checkError(isSupportedTriple(TT), SPIRVEC_InvalidTargetTriple,
"Actual target triple is " + M->getTargetTriple()))
return false;
return true;
}
namespace {
VersionNumber getVersionFromTriple(const Triple &TT, SPIRVErrorLog &ErrorLog) {
switch (TT.getSubArch()) {
case Triple::SPIRVSubArch_v10:
return VersionNumber::SPIRV_1_0;
case Triple::SPIRVSubArch_v11:
return VersionNumber::SPIRV_1_1;
case Triple::SPIRVSubArch_v12:
return VersionNumber::SPIRV_1_2;
case Triple::SPIRVSubArch_v13:
return VersionNumber::SPIRV_1_3;
case Triple::SPIRVSubArch_v14:
return VersionNumber::SPIRV_1_4;
case Triple::SPIRVSubArch_v15:
return VersionNumber::SPIRV_1_5;
case Triple::SPIRVSubArch_v16:
return VersionNumber::SPIRV_1_6;
default:
ErrorLog.checkError(false, SPIRVEC_InvalidSubArch, TT.getArchName().str());
return VersionNumber::MaximumVersion;
}
}
bool runSpirvWriterPasses(Module *M, std::ostream *OS, std::string &ErrMsg,
const SPIRV::TranslatorOpts &Opts) {
// Perform the conversion and write the resulting SPIR-V if an ostream has
// been given; otherwise only perform regularization.
bool WriteSpirv = OS != nullptr;
std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule(Opts));
if (!isValidLLVMModule(M, BM->getErrorLog()))
return false;
// If the module carries a SPIR-V triple with a version subarch, target
// that SPIR-V version.
Triple TargetTriple(M->getTargetTriple());
if ((TargetTriple.getArch() == Triple::spirv32 ||
TargetTriple.getArch() == Triple::spirv64) &&
TargetTriple.getSubArch() != Triple::NoSubArch) {
VersionNumber ModuleVer =
getVersionFromTriple(TargetTriple, BM->getErrorLog());
if (!BM->getErrorLog().checkError(ModuleVer <= Opts.getMaxVersion(),
SPIRVEC_TripleMaxVersionIncompatible))
return false;
BM->setMinSPIRVVersion(ModuleVer);
BM->setMaxSPIRVVersion(ModuleVer);
}
ModulePassManager PassMgr;
addPassesForSPIRV(PassMgr, Opts);
if (WriteSpirv) {
// Run loop simplify pass in order to avoid duplicate OpLoopMerge
// instruction. It can happen in case of continue operand in the loop.
if (hasLoopMetadata(M))
PassMgr.addPass(createModuleToFunctionPassAdaptor(LoopSimplifyPass()));
PassMgr.addPass(LLVMToSPIRVPass(BM.get()));
}
LoopAnalysisManager LAM;
CGSCCAnalysisManager CGAM;
FunctionAnalysisManager FAM;
ModuleAnalysisManager MAM;
PassBuilder PB;
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
MAM.registerPass([&] { return OCLTypeToSPIRVPass(); });
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
PassMgr.run(*M, MAM);
if (BM->getError(ErrMsg) != SPIRVEC_Success)
return false;
if (WriteSpirv)
*OS << *BM;
return true;
}
} // namespace
bool llvm::writeSpirv(Module *M, std::ostream &OS, std::string &ErrMsg) {
SPIRV::TranslatorOpts DefaultOpts;
// To preserve old behavior of the translator, let's enable all extensions
// by default in this API
DefaultOpts.enableAllExtensions();
return llvm::writeSpirv(M, DefaultOpts, OS, ErrMsg);
}
bool llvm::writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts,
std::ostream &OS, std::string &ErrMsg) {
return runSpirvWriterPasses(M, &OS, ErrMsg, Opts);
}
bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg) {
SPIRV::TranslatorOpts DefaultOpts;
// To preserve old behavior of the translator, let's enable all extensions
// by default in this API
DefaultOpts.enableAllExtensions();
return llvm::regularizeLlvmForSpirv(M, ErrMsg, DefaultOpts);
}
bool llvm::regularizeLlvmForSpirv(Module *M, std::string &ErrMsg,
const SPIRV::TranslatorOpts &Opts) {
return runSpirvWriterPasses(M, nullptr, ErrMsg, Opts);
}
|