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
|
/* Producing binary form of HSA BRIG from our internal representation.
Copyright (C) 2013-2018 Free Software Foundation, Inc.
Contributed by Martin Jambor <mjambor@suse.cz> and
Martin Liska <mliska@suse.cz>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "target.h"
#include "memmodel.h"
#include "tm_p.h"
#include "is-a.h"
#include "vec.h"
#include "hash-table.h"
#include "hash-map.h"
#include "tree.h"
#include "tree-iterator.h"
#include "stor-layout.h"
#include "output.h"
#include "basic-block.h"
#include "cfg.h"
#include "function.h"
#include "fold-const.h"
#include "stringpool.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "cgraph.h"
#include "dumpfile.h"
#include "print-tree.h"
#include "symbol-summary.h"
#include "hsa-common.h"
#include "gomp-constants.h"
/* Convert VAL to little endian form, if necessary. */
static uint16_t
lendian16 (uint16_t val)
{
#if GCC_VERSION >= 4008
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap16 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return val;
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
return (val >> 8) | (val << 8);
#endif
#endif
}
/* Convert VAL to little endian form, if necessary. */
static uint32_t
lendian32 (uint32_t val)
{
#if GCC_VERSION >= 4006
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap32 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return (val >> 16) | (val << 16);
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
val = ((val & 0xff00ff00) >> 8) | ((val & 0xff00ff) << 8);
return (val >> 16) | (val << 16);
#endif
#endif
}
/* Convert VAL to little endian form, if necessary. */
static uint64_t
lendian64 (uint64_t val)
{
#if GCC_VERSION >= 4006
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return val;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
return __builtin_bswap64 (val);
#else /* __ORDER_PDP_ENDIAN__ */
return (((val & 0xffffll) << 48)
| ((val & 0xffff0000ll) << 16)
| ((val & 0xffff00000000ll) >> 16)
| ((val & 0xffff000000000000ll) >> 48));
#endif
#else
// provide a safe slower default, with shifts and masking
#ifndef WORDS_BIGENDIAN
return val;
#else
val = (((val & 0xff00ff00ff00ff00ll) >> 8)
| ((val & 0x00ff00ff00ff00ffll) << 8));
val = ((( val & 0xffff0000ffff0000ll) >> 16)
| (( val & 0x0000ffff0000ffffll) << 16));
return (val >> 32) | (val << 32);
#endif
#endif
}
#define BRIG_ELF_SECTION_NAME ".brig"
#define BRIG_LABEL_STRING "hsa_brig"
#define BRIG_SECTION_DATA_NAME "hsa_data"
#define BRIG_SECTION_CODE_NAME "hsa_code"
#define BRIG_SECTION_OPERAND_NAME "hsa_operand"
#define BRIG_CHUNK_MAX_SIZE (64 * 1024)
/* Required HSA section alignment. */
#define HSA_SECTION_ALIGNMENT 16
/* Chunks of BRIG binary data. */
struct hsa_brig_data_chunk
{
/* Size of the data already stored into a chunk. */
unsigned size;
/* Pointer to the data. */
char *data;
};
/* Structure representing a BRIG section, holding and writing its data. */
class hsa_brig_section
{
public:
/* Section name that will be output to the BRIG. */
const char *section_name;
/* Size in bytes of all data stored in the section. */
unsigned total_size;
/* The size of the header of the section including padding. */
unsigned header_byte_count;
/* The size of the header of the section without any padding. */
unsigned header_byte_delta;
void init (const char *name);
void release ();
void output ();
unsigned add (const void *data, unsigned len, void **output = NULL);
void round_size_up (int factor);
void *get_ptr_by_offset (unsigned int offset);
private:
void allocate_new_chunk ();
/* Buffers of binary data, each containing BRIG_CHUNK_MAX_SIZE bytes. */
vec <struct hsa_brig_data_chunk> chunks;
/* More convenient access to the last chunk from the vector above. */
struct hsa_brig_data_chunk *cur_chunk;
};
static struct hsa_brig_section brig_data, brig_code, brig_operand;
static uint32_t brig_insn_count;
static bool brig_initialized = false;
/* Mapping between emitted HSA functions and their offset in code segment. */
static hash_map<tree, BrigCodeOffset32_t> *function_offsets;
/* Hash map of emitted function declarations. */
static hash_map <tree, BrigDirectiveExecutable *> *emitted_declarations;
/* Hash table of emitted internal function declaration offsets. */
hash_table <hsa_internal_fn_hasher> *hsa_emitted_internal_decls;
/* List of sbr instructions. */
static vec <hsa_insn_sbr *> *switch_instructions;
struct function_linkage_pair
{
function_linkage_pair (tree decl, unsigned int off)
: function_decl (decl), offset (off) {}
/* Declaration of called function. */
tree function_decl;
/* Offset in operand section. */
unsigned int offset;
};
/* Vector of function calls where we need to resolve function offsets. */
static auto_vec <function_linkage_pair> function_call_linkage;
/* Add a new chunk, allocate data for it and initialize it. */
void
hsa_brig_section::allocate_new_chunk ()
{
struct hsa_brig_data_chunk new_chunk;
new_chunk.data = XCNEWVEC (char, BRIG_CHUNK_MAX_SIZE);
new_chunk.size = 0;
cur_chunk = chunks.safe_push (new_chunk);
}
/* Initialize the brig section. */
void
hsa_brig_section::init (const char *name)
{
section_name = name;
/* While the following computation is basically wrong, because the intent
certainly wasn't to have the first character of name and padding, which
are a part of sizeof (BrigSectionHeader), included in the first addend,
this is what the disassembler expects. */
total_size = sizeof (BrigSectionHeader) + strlen (section_name);
chunks.create (1);
allocate_new_chunk ();
header_byte_delta = total_size;
round_size_up (4);
header_byte_count = total_size;
}
/* Free all data in the section. */
void
hsa_brig_section::release ()
{
for (unsigned i = 0; i < chunks.length (); i++)
free (chunks[i].data);
chunks.release ();
cur_chunk = NULL;
}
/* Write the section to the output file to a section with the name given at
initialization. Switches the output section and does not restore it. */
void
hsa_brig_section::output ()
{
struct BrigSectionHeader section_header;
char padding[8];
section_header.byteCount = lendian64 (total_size);
section_header.headerByteCount = lendian32 (header_byte_count);
section_header.nameLength = lendian32 (strlen (section_name));
assemble_string ((const char *) §ion_header, 16);
assemble_string (section_name, (section_header.nameLength));
memset (&padding, 0, sizeof (padding));
/* This is also a consequence of the wrong header size computation described
in a comment in hsa_brig_section::init. */
assemble_string (padding, 8);
for (unsigned i = 0; i < chunks.length (); i++)
assemble_string (chunks[i].data, chunks[i].size);
}
/* Add to the stream LEN bytes of opaque binary DATA. Return the offset at
which it was stored. If OUTPUT is not NULL, store into it the pointer to
the place where DATA was actually stored. */
unsigned
hsa_brig_section::add (const void *data, unsigned len, void **output)
{
unsigned offset = total_size;
gcc_assert (len <= BRIG_CHUNK_MAX_SIZE);
if (cur_chunk->size > (BRIG_CHUNK_MAX_SIZE - len))
allocate_new_chunk ();
char *dst = cur_chunk->data + cur_chunk->size;
memcpy (dst, data, len);
if (output)
*output = dst;
cur_chunk->size += len;
total_size += len;
return offset;
}
/* Add padding to section so that its size is divisible by FACTOR. */
void
hsa_brig_section::round_size_up (int factor)
{
unsigned padding, res = total_size % factor;
if (res == 0)
return;
padding = factor - res;
total_size += padding;
if (cur_chunk->size > (BRIG_CHUNK_MAX_SIZE - padding))
{
padding -= BRIG_CHUNK_MAX_SIZE - cur_chunk->size;
cur_chunk->size = BRIG_CHUNK_MAX_SIZE;
allocate_new_chunk ();
}
cur_chunk->size += padding;
}
/* Return pointer to data by global OFFSET in the section. */
void *
hsa_brig_section::get_ptr_by_offset (unsigned int offset)
{
gcc_assert (offset < total_size);
offset -= header_byte_delta;
unsigned i;
for (i = 0; offset >= chunks[i].size; i++)
offset -= chunks[i].size;
return chunks[i].data + offset;
}
/* BRIG string data hashing. */
struct brig_string_slot
{
const char *s;
char prefix;
int len;
uint32_t offset;
};
/* Hash table helpers. */
struct brig_string_slot_hasher : pointer_hash <brig_string_slot>
{
static inline hashval_t hash (const value_type);
static inline bool equal (const value_type, const compare_type);
static inline void remove (value_type);
};
/* Returns a hash code for DS. Adapted from libiberty's htab_hash_string
to support strings that may not end in '\0'. */
inline hashval_t
brig_string_slot_hasher::hash (const value_type ds)
{
hashval_t r = ds->len;
int i;
for (i = 0; i < ds->len; i++)
r = r * 67 + (unsigned) ds->s[i] - 113;
r = r * 67 + (unsigned) ds->prefix - 113;
return r;
}
/* Returns nonzero if DS1 and DS2 are equal. */
inline bool
brig_string_slot_hasher::equal (const value_type ds1, const compare_type ds2)
{
if (ds1->len == ds2->len)
return ds1->prefix == ds2->prefix
&& memcmp (ds1->s, ds2->s, ds1->len) == 0;
return 0;
}
/* Deallocate memory for DS upon its removal. */
inline void
brig_string_slot_hasher::remove (value_type ds)
{
free (const_cast<char *> (ds->s));
free (ds);
}
/* Hash for strings we output in order not to duplicate them needlessly. */
static hash_table<brig_string_slot_hasher> *brig_string_htab;
/* Emit a null terminated string STR to the data section and return its
offset in it. If PREFIX is non-zero, output it just before STR too.
Sanitize the string if SANITIZE option is set to true. */
static unsigned
brig_emit_string (const char *str, char prefix = 0, bool sanitize = true)
{
unsigned slen = strlen (str);
unsigned offset, len = slen + (prefix ? 1 : 0);
uint32_t hdr_len = lendian32 (len);
brig_string_slot s_slot;
brig_string_slot **slot;
char *str2;
str2 = xstrdup (str);
if (sanitize)
hsa_sanitize_name (str2);
s_slot.s = str2;
s_slot.len = slen;
s_slot.prefix = prefix;
s_slot.offset = 0;
slot = brig_string_htab->find_slot (&s_slot, INSERT);
if (*slot == NULL)
{
brig_string_slot *new_slot = XCNEW (brig_string_slot);
/* In theory we should fill in BrigData but that would mean copying
the string to a buffer for no reason, so we just emulate it. */
offset = brig_data.add (&hdr_len, sizeof (hdr_len));
if (prefix)
brig_data.add (&prefix, 1);
brig_data.add (str2, slen);
brig_data.round_size_up (4);
/* TODO: could use the string we just copied into
brig_string->cur_chunk */
new_slot->s = str2;
new_slot->len = slen;
new_slot->prefix = prefix;
new_slot->offset = offset;
*slot = new_slot;
}
else
{
offset = (*slot)->offset;
free (str2);
}
return offset;
}
/* Linked list of queued operands. */
static struct operand_queue
{
/* First from the chain of queued operands. */
hsa_op_base *first_op, *last_op;
/* The offset at which the next operand will be enqueued. */
unsigned projected_size;
} op_queue;
/* Unless already initialized, initialize infrastructure to produce BRIG. */
static void
brig_init (void)
{
brig_insn_count = 0;
if (brig_initialized)
return;
brig_string_htab = new hash_table<brig_string_slot_hasher> (37);
brig_data.init (BRIG_SECTION_DATA_NAME);
brig_code.init (BRIG_SECTION_CODE_NAME);
brig_operand.init (BRIG_SECTION_OPERAND_NAME);
brig_initialized = true;
struct BrigDirectiveModule moddir;
memset (&moddir, 0, sizeof (moddir));
moddir.base.byteCount = lendian16 (sizeof (moddir));
char *modname;
if (main_input_filename && *main_input_filename != '\0')
{
const char *part = strrchr (main_input_filename, '/');
if (!part)
part = main_input_filename;
else
part++;
modname = concat ("&__hsa_module_", part, NULL);
char *extension = strchr (modname, '.');
if (extension)
*extension = '\0';
/* As in LTO mode, we have to emit a different module names. */
if (flag_ltrans)
{
part = strrchr (asm_file_name, '/');
if (!part)
part = asm_file_name;
else
part++;
char *modname2;
modname2 = xasprintf ("%s_%s", modname, part);
free (modname);
modname = modname2;
}
hsa_sanitize_name (modname);
moddir.name = brig_emit_string (modname);
free (modname);
}
else
moddir.name = brig_emit_string ("__hsa_module_unnamed", '&');
moddir.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_MODULE);
moddir.hsailMajor = lendian32 (BRIG_VERSION_HSAIL_MAJOR);
moddir.hsailMinor = lendian32 (BRIG_VERSION_HSAIL_MINOR);
moddir.profile = hsa_full_profile_p () ? BRIG_PROFILE_FULL: BRIG_PROFILE_BASE;
if (hsa_machine_large_p ())
moddir.machineModel = BRIG_MACHINE_LARGE;
else
moddir.machineModel = BRIG_MACHINE_SMALL;
moddir.defaultFloatRound = BRIG_ROUND_FLOAT_DEFAULT;
brig_code.add (&moddir, sizeof (moddir));
}
/* Free all BRIG data. */
static void
brig_release_data (void)
{
delete brig_string_htab;
brig_data.release ();
brig_code.release ();
brig_operand.release ();
brig_initialized = 0;
}
/* Enqueue operation OP. Return the offset at which it will be stored. */
static unsigned int
enqueue_op (hsa_op_base *op)
{
unsigned ret;
if (op->m_brig_op_offset)
return op->m_brig_op_offset;
ret = op_queue.projected_size;
op->m_brig_op_offset = op_queue.projected_size;
if (!op_queue.first_op)
op_queue.first_op = op;
else
op_queue.last_op->m_next = op;
op_queue.last_op = op;
if (is_a <hsa_op_immed *> (op))
op_queue.projected_size += sizeof (struct BrigOperandConstantBytes);
else if (is_a <hsa_op_reg *> (op))
op_queue.projected_size += sizeof (struct BrigOperandRegister);
else if (is_a <hsa_op_address *> (op))
op_queue.projected_size += sizeof (struct BrigOperandAddress);
else if (is_a <hsa_op_code_ref *> (op))
op_queue.projected_size += sizeof (struct BrigOperandCodeRef);
else if (is_a <hsa_op_code_list *> (op))
op_queue.projected_size += sizeof (struct BrigOperandCodeList);
else if (is_a <hsa_op_operand_list *> (op))
op_queue.projected_size += sizeof (struct BrigOperandOperandList);
else
gcc_unreachable ();
return ret;
}
static void emit_immediate_operand (hsa_op_immed *imm);
/* Emit directive describing a symbol if it has not been emitted already.
Return the offset of the directive. */
static unsigned
emit_directive_variable (struct hsa_symbol *symbol)
{
struct BrigDirectiveVariable dirvar;
unsigned name_offset;
static unsigned res_name_offset;
if (symbol->m_directive_offset)
return symbol->m_directive_offset;
memset (&dirvar, 0, sizeof (dirvar));
dirvar.base.byteCount = lendian16 (sizeof (dirvar));
dirvar.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_VARIABLE);
dirvar.allocation = symbol->m_allocation;
char prefix = symbol->m_global_scope_p ? '&' : '%';
if (symbol->m_decl && TREE_CODE (symbol->m_decl) == RESULT_DECL)
{
if (res_name_offset == 0)
res_name_offset = brig_emit_string (symbol->m_name, '%');
name_offset = res_name_offset;
}
else if (symbol->m_name)
name_offset = brig_emit_string (symbol->m_name, prefix);
else
{
char buf[64];
snprintf (buf, 64, "__%s_%i", hsa_seg_name (symbol->m_segment),
symbol->m_name_number);
name_offset = brig_emit_string (buf, prefix);
}
dirvar.name = lendian32 (name_offset);
if (symbol->m_decl && TREE_CODE (symbol->m_decl) == CONST_DECL)
{
hsa_op_immed *tmp = new hsa_op_immed (DECL_INITIAL (symbol->m_decl));
dirvar.init = lendian32 (enqueue_op (tmp));
}
else
dirvar.init = 0;
dirvar.type = lendian16 (symbol->m_type);
dirvar.segment = symbol->m_segment;
dirvar.align = symbol->m_align;
dirvar.linkage = symbol->m_linkage;
dirvar.dim.lo = symbol->m_dim;
dirvar.dim.hi = symbol->m_dim >> 32;
/* Global variables are just declared and linked via HSA runtime. */
if (symbol->m_linkage != BRIG_ALLOCATION_PROGRAM)
dirvar.modifier |= BRIG_VARIABLE_DEFINITION;
dirvar.reserved = 0;
if (symbol->m_cst_value)
{
dirvar.modifier |= BRIG_VARIABLE_CONST;
dirvar.init = lendian32 (enqueue_op (symbol->m_cst_value));
}
symbol->m_directive_offset = brig_code.add (&dirvar, sizeof (dirvar));
return symbol->m_directive_offset;
}
/* Emit directives describing either a function declaration or definition F and
return the produced BrigDirectiveExecutable structure. The function does
not take into account any instructions when calculating nextModuleEntry
field of the produced BrigDirectiveExecutable structure so when emitting
actual definitions, this field needs to be updated after all of the function
is actually added to the code section. */
static BrigDirectiveExecutable *
emit_function_directives (hsa_function_representation *f, bool is_declaration)
{
struct BrigDirectiveExecutable fndir;
unsigned name_offset, inarg_off, scoped_off, next_toplev_off;
int count = 0;
void *ptr_to_fndir;
hsa_symbol *sym;
if (!f->m_declaration_p)
for (int i = 0; f->m_global_symbols.iterate (i, &sym); i++)
{
gcc_assert (!sym->m_emitted_to_brig);
sym->m_emitted_to_brig = true;
emit_directive_variable (sym);
brig_insn_count++;
}
name_offset = brig_emit_string (f->m_name, '&');
inarg_off = brig_code.total_size + sizeof (fndir)
+ (f->m_output_arg ? sizeof (struct BrigDirectiveVariable) : 0);
scoped_off = inarg_off
+ f->m_input_args.length () * sizeof (struct BrigDirectiveVariable);
if (!f->m_declaration_p)
{
count += f->m_spill_symbols.length ();
count += f->m_private_variables.length ();
}
next_toplev_off = scoped_off + count * sizeof (struct BrigDirectiveVariable);
memset (&fndir, 0, sizeof (fndir));
fndir.base.byteCount = lendian16 (sizeof (fndir));
fndir.base.kind = lendian16 (f->m_kern_p ? BRIG_KIND_DIRECTIVE_KERNEL
: BRIG_KIND_DIRECTIVE_FUNCTION);
fndir.name = lendian32 (name_offset);
fndir.inArgCount = lendian16 (f->m_input_args.length ());
fndir.outArgCount = lendian16 (f->m_output_arg ? 1 : 0);
fndir.firstInArg = lendian32 (inarg_off);
fndir.firstCodeBlockEntry = lendian32 (scoped_off);
fndir.nextModuleEntry = lendian32 (next_toplev_off);
fndir.linkage = f->get_linkage ();
if (!f->m_declaration_p)
fndir.modifier |= BRIG_EXECUTABLE_DEFINITION;
memset (&fndir.reserved, 0, sizeof (fndir.reserved));
/* Once we put a definition of function_offsets, we should not overwrite
it with a declaration of the function. */
if (f->m_internal_fn == NULL)
{
if (!function_offsets->get (f->m_decl) || !is_declaration)
function_offsets->put (f->m_decl, brig_code.total_size);
}
else
{
/* Internal function. */
hsa_internal_fn **slot
= hsa_emitted_internal_decls->find_slot (f->m_internal_fn, INSERT);
hsa_internal_fn *int_fn = new hsa_internal_fn (f->m_internal_fn);
int_fn->m_offset = brig_code.total_size;
*slot = int_fn;
}
brig_code.add (&fndir, sizeof (fndir), &ptr_to_fndir);
if (f->m_output_arg)
emit_directive_variable (f->m_output_arg);
for (unsigned i = 0; i < f->m_input_args.length (); i++)
emit_directive_variable (f->m_input_args[i]);
if (!f->m_declaration_p)
{
for (int i = 0; f->m_spill_symbols.iterate (i, &sym); i++)
{
emit_directive_variable (sym);
brig_insn_count++;
}
for (unsigned i = 0; i < f->m_private_variables.length (); i++)
{
emit_directive_variable (f->m_private_variables[i]);
brig_insn_count++;
}
}
return (BrigDirectiveExecutable *) ptr_to_fndir;
}
/* Emit a label directive for the given HBB. We assume it is about to start on
the current offset in the code section. */
static void
emit_bb_label_directive (hsa_bb *hbb)
{
struct BrigDirectiveLabel lbldir;
lbldir.base.byteCount = lendian16 (sizeof (lbldir));
lbldir.base.kind = lendian16 (BRIG_KIND_DIRECTIVE_LABEL);
char buf[32];
snprintf (buf, 32, "BB_%u_%i", DECL_UID (current_function_decl),
hbb->m_index);
lbldir.name = lendian32 (brig_emit_string (buf, '@'));
hbb->m_label_ref.m_directive_offset = brig_code.add (&lbldir,
sizeof (lbldir));
brig_insn_count++;
}
/* Map a normal HSAIL type to the type of the equivalent BRIG operand
holding such, for constants and registers. */
static BrigType16_t
regtype_for_type (BrigType16_t t)
{
switch (t)
{
case BRIG_TYPE_B1:
return BRIG_TYPE_B1;
case BRIG_TYPE_U8:
case BRIG_TYPE_U16:
case BRIG_TYPE_U32:
case BRIG_TYPE_S8:
case BRIG_TYPE_S16:
case BRIG_TYPE_S32:
case BRIG_TYPE_B8:
case BRIG_TYPE_B16:
case BRIG_TYPE_B32:
case BRIG_TYPE_F16:
case BRIG_TYPE_F32:
case BRIG_TYPE_U8X4:
case BRIG_TYPE_U16X2:
case BRIG_TYPE_S8X4:
case BRIG_TYPE_S16X2:
case BRIG_TYPE_F16X2:
return BRIG_TYPE_B32;
case BRIG_TYPE_U64:
case BRIG_TYPE_S64:
case BRIG_TYPE_F64:
case BRIG_TYPE_B64:
case BRIG_TYPE_U8X8:
case BRIG_TYPE_U16X4:
case BRIG_TYPE_U32X2:
case BRIG_TYPE_S8X8:
case BRIG_TYPE_S16X4:
case BRIG_TYPE_S32X2:
case BRIG_TYPE_F16X4:
case BRIG_TYPE_F32X2:
return BRIG_TYPE_B64;
case BRIG_TYPE_B128:
case BRIG_TYPE_U8X16:
case BRIG_TYPE_U16X8:
case BRIG_TYPE_U32X4:
case BRIG_TYPE_U64X2:
case BRIG_TYPE_S8X16:
case BRIG_TYPE_S16X8:
case BRIG_TYPE_S32X4:
case BRIG_TYPE_S64X2:
case BRIG_TYPE_F16X8:
case BRIG_TYPE_F32X4:
case BRIG_TYPE_F64X2:
return BRIG_TYPE_B128;
default:
gcc_unreachable ();
}
}
/* Return the length of the BRIG type TYPE that is going to be streamed out as
an immediate constant (so it must not be B1). */
unsigned
hsa_get_imm_brig_type_len (BrigType16_t type)
{
BrigType16_t base_type = type & BRIG_TYPE_BASE_MASK;
BrigType16_t pack_type = type & BRIG_TYPE_PACK_MASK;
switch (pack_type)
{
case BRIG_TYPE_PACK_NONE:
break;
case BRIG_TYPE_PACK_32:
return 4;
case BRIG_TYPE_PACK_64:
return 8;
case BRIG_TYPE_PACK_128:
return 16;
default:
gcc_unreachable ();
}
switch (base_type)
{
case BRIG_TYPE_U8:
case BRIG_TYPE_S8:
case BRIG_TYPE_B8:
return 1;
case BRIG_TYPE_U16:
case BRIG_TYPE_S16:
case BRIG_TYPE_F16:
case BRIG_TYPE_B16:
return 2;
case BRIG_TYPE_U32:
case BRIG_TYPE_S32:
case BRIG_TYPE_F32:
case BRIG_TYPE_B32:
return 4;
case BRIG_TYPE_U64:
case BRIG_TYPE_S64:
case BRIG_TYPE_F64:
case BRIG_TYPE_B64:
return 8;
case BRIG_TYPE_B128:
return 16;
default:
gcc_unreachable ();
}
}
/* Emit one scalar VALUE to the buffer DATA intended for BRIG emission.
If NEED_LEN is not equal to zero, shrink or extend the value
to NEED_LEN bytes. Return how many bytes were written. */
static int
emit_immediate_scalar_to_buffer (tree value, char *data, unsigned need_len)
{
union hsa_bytes bytes;
memset (&bytes, 0, sizeof (bytes));
tree type = TREE_TYPE (value);
gcc_checking_assert (TREE_CODE (type) != VECTOR_TYPE);
unsigned data_len = tree_to_uhwi (TYPE_SIZE (type)) / BITS_PER_UNIT;
if (INTEGRAL_TYPE_P (type)
|| (POINTER_TYPE_P (type) && TREE_CODE (value) == INTEGER_CST))
switch (data_len)
{
case 1:
bytes.b8 = (uint8_t) TREE_INT_CST_LOW (value);
break;
case 2:
bytes.b16 = (uint16_t) TREE_INT_CST_LOW (value);
break;
case 4:
bytes.b32 = (uint32_t) TREE_INT_CST_LOW (value);
break;
case 8:
bytes.b64 = (uint64_t) TREE_INT_CST_LOW (value);
break;
default:
gcc_unreachable ();
}
else if (SCALAR_FLOAT_TYPE_P (type))
{
if (data_len == 2)
{
sorry ("Support for HSA does not implement immediate 16 bit FPU "
"operands");
return 2;
}
unsigned int_len = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
/* There are always 32 bits in each long, no matter the size of
the hosts long. */
long tmp[6];
real_to_target (tmp, TREE_REAL_CST_PTR (value), TYPE_MODE (type));
if (int_len == 4)
bytes.b32 = (uint32_t) tmp[0];
else
{
bytes.b64 = (uint64_t)(uint32_t) tmp[1];
bytes.b64 <<= 32;
bytes.b64 |= (uint32_t) tmp[0];
}
}
else
gcc_unreachable ();
int len;
if (need_len == 0)
len = data_len;
else
len = need_len;
memcpy (data, &bytes, len);
return len;
}
char *
hsa_op_immed::emit_to_buffer (unsigned *brig_repr_size)
{
char *brig_repr;
*brig_repr_size = hsa_get_imm_brig_type_len (m_type);
if (m_tree_value != NULL_TREE)
{
/* Update brig_repr_size for special tree values. */
if (TREE_CODE (m_tree_value) == STRING_CST)
*brig_repr_size = TREE_STRING_LENGTH (m_tree_value);
else if (TREE_CODE (m_tree_value) == CONSTRUCTOR)
*brig_repr_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (m_tree_value)));
unsigned total_len = *brig_repr_size;
/* As we can have a constructor with fewer elements, fill the memory
with zeros. */
brig_repr = XCNEWVEC (char, total_len);
char *p = brig_repr;
if (TREE_CODE (m_tree_value) == VECTOR_CST)
{
/* Variable-length vectors aren't supported. */
int i, num = VECTOR_CST_NELTS (m_tree_value).to_constant ();
for (i = 0; i < num; i++)
{
tree v = VECTOR_CST_ELT (m_tree_value, i);
unsigned actual = emit_immediate_scalar_to_buffer (v, p, 0);
total_len -= actual;
p += actual;
}
/* Vectors should have the exact size. */
gcc_assert (total_len == 0);
}
else if (TREE_CODE (m_tree_value) == STRING_CST)
memcpy (brig_repr, TREE_STRING_POINTER (m_tree_value),
TREE_STRING_LENGTH (m_tree_value));
else if (TREE_CODE (m_tree_value) == COMPLEX_CST)
{
gcc_assert (total_len % 2 == 0);
unsigned actual;
actual
= emit_immediate_scalar_to_buffer (TREE_REALPART (m_tree_value), p,
total_len / 2);
gcc_assert (actual == total_len / 2);
p += actual;
actual
= emit_immediate_scalar_to_buffer (TREE_IMAGPART (m_tree_value), p,
total_len / 2);
gcc_assert (actual == total_len / 2);
}
else if (TREE_CODE (m_tree_value) == CONSTRUCTOR)
{
unsigned len = CONSTRUCTOR_NELTS (m_tree_value);
for (unsigned i = 0; i < len; i++)
{
tree v = CONSTRUCTOR_ELT (m_tree_value, i)->value;
unsigned actual = emit_immediate_scalar_to_buffer (v, p, 0);
total_len -= actual;
p += actual;
}
}
else
emit_immediate_scalar_to_buffer (m_tree_value, p, total_len);
}
else
{
hsa_bytes bytes;
switch (*brig_repr_size)
{
case 1:
bytes.b8 = (uint8_t) m_int_value;
break;
case 2:
bytes.b16 = (uint16_t) m_int_value;
break;
case 4:
bytes.b32 = (uint32_t) m_int_value;
break;
case 8:
bytes.b64 = (uint64_t) m_int_value;
break;
default:
gcc_unreachable ();
}
brig_repr = XNEWVEC (char, *brig_repr_size);
memcpy (brig_repr, &bytes, *brig_repr_size);
}
return brig_repr;
}
/* Emit an immediate BRIG operand IMM. The BRIG type of the immediate might
have been massaged to comply with various HSA/BRIG type requirements, so the
only important aspect of that is the length (because HSAIL might expect
smaller constants or become bit-data). The data should be represented
according to what is in the tree representation. */
static void
emit_immediate_operand (hsa_op_immed *imm)
{
unsigned brig_repr_size;
char *brig_repr = imm->emit_to_buffer (&brig_repr_size);
struct BrigOperandConstantBytes out;
memset (&out, 0, sizeof (out));
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_CONSTANT_BYTES);
uint32_t byteCount = lendian32 (brig_repr_size);
out.type = lendian16 (imm->m_type);
out.bytes = lendian32 (brig_data.add (&byteCount, sizeof (byteCount)));
brig_operand.add (&out, sizeof (out));
brig_data.add (brig_repr, brig_repr_size);
brig_data.round_size_up (4);
free (brig_repr);
}
/* Emit a register BRIG operand REG. */
static void
emit_register_operand (hsa_op_reg *reg)
{
struct BrigOperandRegister out;
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_REGISTER);
out.regNum = lendian32 (reg->m_hard_num);
switch (regtype_for_type (reg->m_type))
{
case BRIG_TYPE_B32:
out.regKind = BRIG_REGISTER_KIND_SINGLE;
break;
case BRIG_TYPE_B64:
out.regKind = BRIG_REGISTER_KIND_DOUBLE;
break;
case BRIG_TYPE_B128:
out.regKind = BRIG_REGISTER_KIND_QUAD;
break;
case BRIG_TYPE_B1:
out.regKind = BRIG_REGISTER_KIND_CONTROL;
break;
default:
gcc_unreachable ();
}
brig_operand.add (&out, sizeof (out));
}
/* Emit an address BRIG operand ADDR. */
static void
emit_address_operand (hsa_op_address *addr)
{
struct BrigOperandAddress out;
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_ADDRESS);
out.symbol = addr->m_symbol
? lendian32 (emit_directive_variable (addr->m_symbol)) : 0;
out.reg = addr->m_reg ? lendian32 (enqueue_op (addr->m_reg)) : 0;
if (sizeof (addr->m_imm_offset) == 8)
{
out.offset.lo = lendian32 (addr->m_imm_offset);
out.offset.hi = lendian32 (addr->m_imm_offset >> 32);
}
else
{
gcc_assert (sizeof (addr->m_imm_offset) == 4);
out.offset.lo = lendian32 (addr->m_imm_offset);
out.offset.hi = 0;
}
brig_operand.add (&out, sizeof (out));
}
/* Emit a code reference operand REF. */
static void
emit_code_ref_operand (hsa_op_code_ref *ref)
{
struct BrigOperandCodeRef out;
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_CODE_REF);
out.ref = lendian32 (ref->m_directive_offset);
brig_operand.add (&out, sizeof (out));
}
/* Emit a code list operand CODE_LIST. */
static void
emit_code_list_operand (hsa_op_code_list *code_list)
{
struct BrigOperandCodeList out;
unsigned args = code_list->m_offsets.length ();
for (unsigned i = 0; i < args; i++)
gcc_assert (code_list->m_offsets[i]);
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_CODE_LIST);
uint32_t byteCount = lendian32 (4 * args);
out.elements = lendian32 (brig_data.add (&byteCount, sizeof (byteCount)));
brig_data.add (code_list->m_offsets.address (), args * sizeof (uint32_t));
brig_data.round_size_up (4);
brig_operand.add (&out, sizeof (out));
}
/* Emit an operand list operand OPERAND_LIST. */
static void
emit_operand_list_operand (hsa_op_operand_list *operand_list)
{
struct BrigOperandOperandList out;
unsigned args = operand_list->m_offsets.length ();
for (unsigned i = 0; i < args; i++)
gcc_assert (operand_list->m_offsets[i]);
out.base.byteCount = lendian16 (sizeof (out));
out.base.kind = lendian16 (BRIG_KIND_OPERAND_OPERAND_LIST);
uint32_t byteCount = lendian32 (4 * args);
out.elements = lendian32 (brig_data.add (&byteCount, sizeof (byteCount)));
brig_data.add (operand_list->m_offsets.address (), args * sizeof (uint32_t));
brig_data.round_size_up (4);
brig_operand.add (&out, sizeof (out));
}
/* Emit all operands queued for writing. */
static void
emit_queued_operands (void)
{
for (hsa_op_base *op = op_queue.first_op; op; op = op->m_next)
{
gcc_assert (op->m_brig_op_offset == brig_operand.total_size);
if (hsa_op_immed *imm = dyn_cast <hsa_op_immed *> (op))
emit_immediate_operand (imm);
else if (hsa_op_reg *reg = dyn_cast <hsa_op_reg *> (op))
emit_register_operand (reg);
else if (hsa_op_address *addr = dyn_cast <hsa_op_address *> (op))
emit_address_operand (addr);
else if (hsa_op_code_ref *ref = dyn_cast <hsa_op_code_ref *> (op))
emit_code_ref_operand (ref);
else if (hsa_op_code_list *code_list = dyn_cast <hsa_op_code_list *> (op))
emit_code_list_operand (code_list);
else if (hsa_op_operand_list *l = dyn_cast <hsa_op_operand_list *> (op))
emit_operand_list_operand (l);
else
gcc_unreachable ();
}
}
/* Emit directives describing the function that is used for
a function declaration. */
static BrigDirectiveExecutable *
emit_function_declaration (tree decl)
{
hsa_function_representation *f = hsa_generate_function_declaration (decl);
BrigDirectiveExecutable *e = emit_function_directives (f, true);
emit_queued_operands ();
delete f;
return e;
}
/* Emit directives describing the function that is used for
an internal function declaration. */
static BrigDirectiveExecutable *
emit_internal_fn_decl (hsa_internal_fn *fn)
{
hsa_function_representation *f = hsa_generate_internal_fn_decl (fn);
BrigDirectiveExecutable *e = emit_function_directives (f, true);
emit_queued_operands ();
delete f;
return e;
}
/* Enqueue all operands of INSN and return offset to BRIG data section
to list of operand offsets. */
static unsigned
emit_insn_operands (hsa_insn_basic *insn)
{
auto_vec<BrigOperandOffset32_t, HSA_BRIG_INT_STORAGE_OPERANDS>
operand_offsets;
unsigned l = insn->operand_count ();
/* We have N operands so use 4 * N for the byte_count. */
uint32_t byte_count = lendian32 (4 * l);
unsigned offset = brig_data.add (&byte_count, sizeof (byte_count));
if (l > 0)
{
operand_offsets.safe_grow (l);
for (unsigned i = 0; i < l; i++)
operand_offsets[i] = lendian32 (enqueue_op (insn->get_op (i)));
brig_data.add (operand_offsets.address (),
l * sizeof (BrigOperandOffset32_t));
}
brig_data.round_size_up (4);
return offset;
}
/* Enqueue operand OP0, OP1, OP2 (if different from NULL) and return offset
to BRIG data section to list of operand offsets. */
static unsigned
emit_operands (hsa_op_base *op0, hsa_op_base *op1 = NULL,
hsa_op_base *op2 = NULL)
{
auto_vec<BrigOperandOffset32_t, HSA_BRIG_INT_STORAGE_OPERANDS>
operand_offsets;
gcc_checking_assert (op0 != NULL);
operand_offsets.safe_push (enqueue_op (op0));
if (op1 != NULL)
{
operand_offsets.safe_push (enqueue_op (op1));
if (op2 != NULL)
operand_offsets.safe_push (enqueue_op (op2));
}
unsigned l = operand_offsets.length ();
/* We have N operands so use 4 * N for the byte_count. */
uint32_t byte_count = lendian32 (4 * l);
unsigned offset = brig_data.add (&byte_count, sizeof (byte_count));
brig_data.add (operand_offsets.address (),
l * sizeof (BrigOperandOffset32_t));
brig_data.round_size_up (4);
return offset;
}
/* Emit an HSA memory instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_memory_insn (hsa_insn_mem *mem)
{
struct BrigInstMem repr;
gcc_checking_assert (mem->operand_count () == 2);
hsa_op_address *addr = as_a <hsa_op_address *> (mem->get_op (1));
/* This is necessary because of the erroneous typedef of
BrigMemoryModifier8_t which introduces padding which may then contain
random stuff (which we do not want so that we can test things don't
change). */
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_MEM);
repr.base.opcode = lendian16 (mem->m_opcode);
repr.base.type = lendian16 (mem->m_type);
repr.base.operands = lendian32 (emit_insn_operands (mem));
if (addr->m_symbol)
repr.segment = addr->m_symbol->m_segment;
else
repr.segment = BRIG_SEGMENT_FLAT;
repr.modifier = 0;
repr.equivClass = mem->m_equiv_class;
repr.align = mem->m_align;
if (mem->m_opcode == BRIG_OPCODE_LD)
repr.width = BRIG_WIDTH_1;
else
repr.width = BRIG_WIDTH_NONE;
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA signal memory instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_signal_insn (hsa_insn_signal *mem)
{
struct BrigInstSignal repr;
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_SIGNAL);
repr.base.opcode = lendian16 (mem->m_opcode);
repr.base.type = lendian16 (mem->m_type);
repr.base.operands = lendian32 (emit_insn_operands (mem));
repr.memoryOrder = mem->m_memory_order;
repr.signalOperation = mem->m_signalop;
repr.signalType = hsa_machine_large_p () ? BRIG_TYPE_SIG64 : BRIG_TYPE_SIG32;
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA atomic memory instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_atomic_insn (hsa_insn_atomic *mem)
{
struct BrigInstAtomic repr;
/* Either operand[0] or operand[1] must be an address operand. */
hsa_op_address *addr = NULL;
if (is_a <hsa_op_address *> (mem->get_op (0)))
addr = as_a <hsa_op_address *> (mem->get_op (0));
else
addr = as_a <hsa_op_address *> (mem->get_op (1));
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_ATOMIC);
repr.base.opcode = lendian16 (mem->m_opcode);
repr.base.type = lendian16 (mem->m_type);
repr.base.operands = lendian32 (emit_insn_operands (mem));
if (addr->m_symbol)
repr.segment = addr->m_symbol->m_segment;
else
repr.segment = BRIG_SEGMENT_FLAT;
repr.memoryOrder = mem->m_memoryorder;
repr.memoryScope = mem->m_memoryscope;
repr.atomicOperation = mem->m_atomicop;
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA LDA instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_addr_insn (hsa_insn_basic *insn)
{
struct BrigInstAddr repr;
hsa_op_address *addr = as_a <hsa_op_address *> (insn->get_op (1));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_ADDR);
repr.base.opcode = lendian16 (insn->m_opcode);
repr.base.type = lendian16 (insn->m_type);
repr.base.operands = lendian32 (emit_insn_operands (insn));
if (addr->m_symbol)
repr.segment = addr->m_symbol->m_segment;
else
repr.segment = BRIG_SEGMENT_FLAT;
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA segment conversion instruction and all necessary directives,
schedule necessary operands for writing. */
static void
emit_segment_insn (hsa_insn_seg *seg)
{
struct BrigInstSegCvt repr;
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_SEG_CVT);
repr.base.opcode = lendian16 (seg->m_opcode);
repr.base.type = lendian16 (seg->m_type);
repr.base.operands = lendian32 (emit_insn_operands (seg));
repr.sourceType = lendian16 (as_a <hsa_op_reg *> (seg->get_op (1))->m_type);
repr.segment = seg->m_segment;
repr.modifier = 0;
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA alloca instruction and all necessary directives,
schedule necessary operands for writing. */
static void
emit_alloca_insn (hsa_insn_alloca *alloca)
{
struct BrigInstMem repr;
gcc_checking_assert (alloca->operand_count () == 2);
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_MEM);
repr.base.opcode = lendian16 (alloca->m_opcode);
repr.base.type = lendian16 (alloca->m_type);
repr.base.operands = lendian32 (emit_insn_operands (alloca));
repr.segment = BRIG_SEGMENT_PRIVATE;
repr.modifier = 0;
repr.equivClass = 0;
repr.align = alloca->m_align;
repr.width = BRIG_WIDTH_NONE;
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA comparison instruction and all necessary directives,
schedule necessary operands for writing. */
static void
emit_cmp_insn (hsa_insn_cmp *cmp)
{
struct BrigInstCmp repr;
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_CMP);
repr.base.opcode = lendian16 (cmp->m_opcode);
repr.base.type = lendian16 (cmp->m_type);
repr.base.operands = lendian32 (emit_insn_operands (cmp));
if (is_a <hsa_op_reg *> (cmp->get_op (1)))
repr.sourceType
= lendian16 (as_a <hsa_op_reg *> (cmp->get_op (1))->m_type);
else
repr.sourceType
= lendian16 (as_a <hsa_op_immed *> (cmp->get_op (1))->m_type);
repr.modifier = 0;
repr.compare = cmp->m_compare;
repr.pack = 0;
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA generic branching/sycnronization instruction. */
static void
emit_generic_branch_insn (hsa_insn_br *br)
{
struct BrigInstBr repr;
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BR);
repr.base.opcode = lendian16 (br->m_opcode);
repr.width = br->m_width;
repr.base.type = lendian16 (br->m_type);
repr.base.operands = lendian32 (emit_insn_operands (br));
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA conditional branching instruction and all necessary directives,
schedule necessary operands for writing. */
static void
emit_cond_branch_insn (hsa_insn_cbr *br)
{
struct BrigInstBr repr;
basic_block target = NULL;
edge_iterator ei;
edge e;
/* At the moment we only handle direct conditional jumps. */
gcc_assert (br->m_opcode == BRIG_OPCODE_CBR);
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BR);
repr.base.opcode = lendian16 (br->m_opcode);
repr.width = br->m_width;
/* For Conditional jumps the type is always B1. */
repr.base.type = lendian16 (BRIG_TYPE_B1);
FOR_EACH_EDGE (e, ei, br->m_bb->succs)
if (e->flags & EDGE_TRUE_VALUE)
{
target = e->dest;
break;
}
gcc_assert (target);
repr.base.operands
= lendian32 (emit_operands (br->get_op (0),
&hsa_bb_for_bb (target)->m_label_ref));
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA unconditional jump branching instruction that points to
a label REFERENCE. */
static void
emit_unconditional_jump (hsa_op_code_ref *reference)
{
struct BrigInstBr repr;
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BR);
repr.base.opcode = lendian16 (BRIG_OPCODE_BR);
repr.base.type = lendian16 (BRIG_TYPE_NONE);
/* Direct branches to labels must be width(all). */
repr.width = BRIG_WIDTH_ALL;
repr.base.operands = lendian32 (emit_operands (reference));
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit an HSA switch jump instruction that uses a jump table to
jump to a destination label. */
static void
emit_switch_insn (hsa_insn_sbr *sbr)
{
struct BrigInstBr repr;
gcc_assert (sbr->m_opcode == BRIG_OPCODE_SBR);
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BR);
repr.base.opcode = lendian16 (sbr->m_opcode);
repr.width = BRIG_WIDTH_1;
/* For Conditional jumps the type is always B1. */
hsa_op_reg *index = as_a <hsa_op_reg *> (sbr->get_op (0));
repr.base.type = lendian16 (index->m_type);
repr.base.operands
= lendian32 (emit_operands (sbr->get_op (0), sbr->m_label_code_list));
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit a HSA convert instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_cvt_insn (hsa_insn_cvt *insn)
{
struct BrigInstCvt repr;
BrigType16_t srctype;
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_CVT);
repr.base.opcode = lendian16 (insn->m_opcode);
repr.base.type = lendian16 (insn->m_type);
repr.base.operands = lendian32 (emit_insn_operands (insn));
if (is_a <hsa_op_reg *> (insn->get_op (1)))
srctype = as_a <hsa_op_reg *> (insn->get_op (1))->m_type;
else
srctype = as_a <hsa_op_immed *> (insn->get_op (1))->m_type;
repr.sourceType = lendian16 (srctype);
repr.modifier = 0;
/* float to smaller float requires a rounding setting (we default
to 'near'. */
if (hsa_type_float_p (insn->m_type)
&& (!hsa_type_float_p (srctype)
|| ((insn->m_type & BRIG_TYPE_BASE_MASK)
< (srctype & BRIG_TYPE_BASE_MASK))))
repr.round = BRIG_ROUND_FLOAT_NEAR_EVEN;
else if (hsa_type_integer_p (insn->m_type) &&
hsa_type_float_p (srctype))
repr.round = BRIG_ROUND_INTEGER_ZERO;
else
repr.round = BRIG_ROUND_NONE;
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit call instruction INSN, where this instruction must be closed
within a call block instruction. */
static void
emit_call_insn (hsa_insn_call *call)
{
struct BrigInstBr repr;
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BR);
repr.base.opcode = lendian16 (BRIG_OPCODE_CALL);
repr.base.type = lendian16 (BRIG_TYPE_NONE);
repr.base.operands
= lendian32 (emit_operands (call->m_result_code_list, &call->m_func,
call->m_args_code_list));
/* Internal functions have not set m_called_function. */
if (call->m_called_function)
{
function_linkage_pair pair (call->m_called_function,
call->m_func.m_brig_op_offset);
function_call_linkage.safe_push (pair);
}
else
{
hsa_internal_fn *slot
= hsa_emitted_internal_decls->find (call->m_called_internal_fn);
gcc_assert (slot);
gcc_assert (slot->m_offset > 0);
call->m_func.m_directive_offset = slot->m_offset;
}
repr.width = BRIG_WIDTH_ALL;
memset (&repr.reserved, 0, sizeof (repr.reserved));
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit argument block directive. */
static void
emit_arg_block_insn (hsa_insn_arg_block *insn)
{
switch (insn->m_kind)
{
case BRIG_KIND_DIRECTIVE_ARG_BLOCK_START:
{
struct BrigDirectiveArgBlock repr;
repr.base.byteCount = lendian16 (sizeof (repr));
repr.base.kind = lendian16 (insn->m_kind);
brig_code.add (&repr, sizeof (repr));
for (unsigned i = 0; i < insn->m_call_insn->m_input_args.length (); i++)
{
insn->m_call_insn->m_args_code_list->m_offsets[i]
= lendian32 (emit_directive_variable
(insn->m_call_insn->m_input_args[i]));
brig_insn_count++;
}
if (insn->m_call_insn->m_output_arg)
{
insn->m_call_insn->m_result_code_list->m_offsets[0]
= lendian32 (emit_directive_variable
(insn->m_call_insn->m_output_arg));
brig_insn_count++;
}
break;
}
case BRIG_KIND_DIRECTIVE_ARG_BLOCK_END:
{
struct BrigDirectiveArgBlock repr;
repr.base.byteCount = lendian16 (sizeof (repr));
repr.base.kind = lendian16 (insn->m_kind);
brig_code.add (&repr, sizeof (repr));
break;
}
default:
gcc_unreachable ();
}
brig_insn_count++;
}
/* Emit comment directive. */
static void
emit_comment_insn (hsa_insn_comment *insn)
{
struct BrigDirectiveComment repr;
memset (&repr, 0, sizeof (repr));
repr.base.byteCount = lendian16 (sizeof (repr));
repr.base.kind = lendian16 (insn->m_opcode);
repr.name = brig_emit_string (insn->m_comment, '\0', false);
brig_code.add (&repr, sizeof (repr));
}
/* Emit queue instruction INSN. */
static void
emit_queue_insn (hsa_insn_queue *insn)
{
BrigInstQueue repr;
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_QUEUE);
repr.base.opcode = lendian16 (insn->m_opcode);
repr.base.type = lendian16 (insn->m_type);
repr.segment = insn->m_segment;
repr.memoryOrder = insn->m_memory_order;
repr.base.operands = lendian32 (emit_insn_operands (insn));
brig_data.round_size_up (4);
brig_code.add (&repr, sizeof (repr));
brig_insn_count++;
}
/* Emit source type instruction INSN. */
static void
emit_srctype_insn (hsa_insn_srctype *insn)
{
/* We assume that BrigInstMod has a BrigInstBasic prefix. */
struct BrigInstSourceType repr;
unsigned operand_count = insn->operand_count ();
gcc_checking_assert (operand_count >= 2);
memset (&repr, 0, sizeof (repr));
repr.sourceType = lendian16 (insn->m_source_type);
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_SOURCE_TYPE);
repr.base.opcode = lendian16 (insn->m_opcode);
repr.base.type = lendian16 (insn->m_type);
repr.base.operands = lendian32 (emit_insn_operands (insn));
brig_code.add (&repr, sizeof (struct BrigInstSourceType));
brig_insn_count++;
}
/* Emit packed instruction INSN. */
static void
emit_packed_insn (hsa_insn_packed *insn)
{
/* We assume that BrigInstMod has a BrigInstBasic prefix. */
struct BrigInstSourceType repr;
unsigned operand_count = insn->operand_count ();
gcc_checking_assert (operand_count >= 2);
memset (&repr, 0, sizeof (repr));
repr.sourceType = lendian16 (insn->m_source_type);
repr.base.base.byteCount = lendian16 (sizeof (repr));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_SOURCE_TYPE);
repr.base.opcode = lendian16 (insn->m_opcode);
repr.base.type = lendian16 (insn->m_type);
if (insn->m_opcode == BRIG_OPCODE_COMBINE)
{
/* Create operand list for packed type. */
for (unsigned i = 1; i < operand_count; i++)
{
gcc_checking_assert (insn->get_op (i));
insn->m_operand_list->m_offsets[i - 1]
= lendian32 (enqueue_op (insn->get_op (i)));
}
repr.base.operands = lendian32 (emit_operands (insn->get_op (0),
insn->m_operand_list));
}
else if (insn->m_opcode == BRIG_OPCODE_EXPAND)
{
/* Create operand list for packed type. */
for (unsigned i = 0; i < operand_count - 1; i++)
{
gcc_checking_assert (insn->get_op (i));
insn->m_operand_list->m_offsets[i]
= lendian32 (enqueue_op (insn->get_op (i)));
}
unsigned ops = emit_operands (insn->m_operand_list,
insn->get_op (insn->operand_count () - 1));
repr.base.operands = lendian32 (ops);
}
brig_code.add (&repr, sizeof (struct BrigInstSourceType));
brig_insn_count++;
}
/* Emit a basic HSA instruction and all necessary directives, schedule
necessary operands for writing. */
static void
emit_basic_insn (hsa_insn_basic *insn)
{
/* We assume that BrigInstMod has a BrigInstBasic prefix. */
struct BrigInstMod repr;
BrigType16_t type;
memset (&repr, 0, sizeof (repr));
repr.base.base.byteCount = lendian16 (sizeof (BrigInstBasic));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_BASIC);
repr.base.opcode = lendian16 (insn->m_opcode);
switch (insn->m_opcode)
{
/* And the bit-logical operations need bit types and whine about
arithmetic types :-/ */
case BRIG_OPCODE_AND:
case BRIG_OPCODE_OR:
case BRIG_OPCODE_XOR:
case BRIG_OPCODE_NOT:
type = regtype_for_type (insn->m_type);
break;
default:
type = insn->m_type;
break;
}
repr.base.type = lendian16 (type);
repr.base.operands = lendian32 (emit_insn_operands (insn));
if (hsa_type_packed_p (type))
{
if (hsa_type_float_p (type)
&& !hsa_opcode_floating_bit_insn_p (insn->m_opcode))
repr.round = BRIG_ROUND_FLOAT_NEAR_EVEN;
else
repr.round = 0;
/* We assume that destination and sources agree in packing layout. */
if (insn->num_used_ops () >= 2)
repr.pack = BRIG_PACK_PP;
else
repr.pack = BRIG_PACK_P;
repr.reserved = 0;
repr.base.base.byteCount = lendian16 (sizeof (BrigInstMod));
repr.base.base.kind = lendian16 (BRIG_KIND_INST_MOD);
brig_code.add (&repr, sizeof (struct BrigInstMod));
}
else
brig_code.add (&repr, sizeof (struct BrigInstBasic));
brig_insn_count++;
}
/* Emit an HSA instruction and all necessary directives, schedule necessary
operands for writing. */
static void
emit_insn (hsa_insn_basic *insn)
{
gcc_assert (!is_a <hsa_insn_phi *> (insn));
insn->m_brig_offset = brig_code.total_size;
if (hsa_insn_signal *signal = dyn_cast <hsa_insn_signal *> (insn))
emit_signal_insn (signal);
else if (hsa_insn_atomic *atom = dyn_cast <hsa_insn_atomic *> (insn))
emit_atomic_insn (atom);
else if (hsa_insn_mem *mem = dyn_cast <hsa_insn_mem *> (insn))
emit_memory_insn (mem);
else if (insn->m_opcode == BRIG_OPCODE_LDA)
emit_addr_insn (insn);
else if (hsa_insn_seg *seg = dyn_cast <hsa_insn_seg *> (insn))
emit_segment_insn (seg);
else if (hsa_insn_cmp *cmp = dyn_cast <hsa_insn_cmp *> (insn))
emit_cmp_insn (cmp);
else if (hsa_insn_cbr *br = dyn_cast <hsa_insn_cbr *> (insn))
emit_cond_branch_insn (br);
else if (hsa_insn_sbr *sbr = dyn_cast <hsa_insn_sbr *> (insn))
{
if (switch_instructions == NULL)
switch_instructions = new vec <hsa_insn_sbr *> ();
switch_instructions->safe_push (sbr);
emit_switch_insn (sbr);
}
else if (hsa_insn_br *br = dyn_cast <hsa_insn_br *> (insn))
emit_generic_branch_insn (br);
else if (hsa_insn_arg_block *block = dyn_cast <hsa_insn_arg_block *> (insn))
emit_arg_block_insn (block);
else if (hsa_insn_call *call = dyn_cast <hsa_insn_call *> (insn))
emit_call_insn (call);
else if (hsa_insn_comment *comment = dyn_cast <hsa_insn_comment *> (insn))
emit_comment_insn (comment);
else if (hsa_insn_queue *queue = dyn_cast <hsa_insn_queue *> (insn))
emit_queue_insn (queue);
else if (hsa_insn_srctype *srctype = dyn_cast <hsa_insn_srctype *> (insn))
emit_srctype_insn (srctype);
else if (hsa_insn_packed *packed = dyn_cast <hsa_insn_packed *> (insn))
emit_packed_insn (packed);
else if (hsa_insn_cvt *cvt = dyn_cast <hsa_insn_cvt *> (insn))
emit_cvt_insn (cvt);
else if (hsa_insn_alloca *alloca = dyn_cast <hsa_insn_alloca *> (insn))
emit_alloca_insn (alloca);
else
emit_basic_insn (insn);
}
/* We have just finished emitting BB and are about to emit NEXT_BB if non-NULL,
or we are about to finish emitting code, if it is NULL. If the fall through
edge from BB does not lead to NEXT_BB, emit an unconditional jump. */
static void
perhaps_emit_branch (basic_block bb, basic_block next_bb)
{
basic_block t_bb = NULL, ff = NULL;
edge_iterator ei;
edge e;
/* If the last instruction of BB is a switch, ignore emission of all
edges. */
if (hsa_bb_for_bb (bb)->m_last_insn
&& is_a <hsa_insn_sbr *> (hsa_bb_for_bb (bb)->m_last_insn))
return;
FOR_EACH_EDGE (e, ei, bb->succs)
if (e->flags & EDGE_TRUE_VALUE)
{
gcc_assert (!t_bb);
t_bb = e->dest;
}
else
{
gcc_assert (!ff);
ff = e->dest;
}
if (!ff || ff == next_bb || ff == EXIT_BLOCK_PTR_FOR_FN (cfun))
return;
emit_unconditional_jump (&hsa_bb_for_bb (ff)->m_label_ref);
}
/* Emit the a function with name NAME to the various brig sections. */
void
hsa_brig_emit_function (void)
{
basic_block bb, prev_bb;
hsa_insn_basic *insn;
BrigDirectiveExecutable *ptr_to_fndir;
brig_init ();
brig_insn_count = 0;
memset (&op_queue, 0, sizeof (op_queue));
op_queue.projected_size = brig_operand.total_size;
if (!function_offsets)
function_offsets = new hash_map<tree, BrigCodeOffset32_t> ();
if (!emitted_declarations)
emitted_declarations = new hash_map <tree, BrigDirectiveExecutable *> ();
for (unsigned i = 0; i < hsa_cfun->m_called_functions.length (); i++)
{
tree called = hsa_cfun->m_called_functions[i];
/* If the function has no definition, emit a declaration. */
if (!emitted_declarations->get (called))
{
BrigDirectiveExecutable *e = emit_function_declaration (called);
emitted_declarations->put (called, e);
}
}
for (unsigned i = 0; i < hsa_cfun->m_called_internal_fns.length (); i++)
{
hsa_internal_fn *called = hsa_cfun->m_called_internal_fns[i];
emit_internal_fn_decl (called);
}
ptr_to_fndir = emit_function_directives (hsa_cfun, false);
for (insn = hsa_bb_for_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun))->m_first_insn;
insn;
insn = insn->m_next)
emit_insn (insn);
prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
FOR_EACH_BB_FN (bb, cfun)
{
perhaps_emit_branch (prev_bb, bb);
emit_bb_label_directive (hsa_bb_for_bb (bb));
for (insn = hsa_bb_for_bb (bb)->m_first_insn; insn; insn = insn->m_next)
emit_insn (insn);
prev_bb = bb;
}
perhaps_emit_branch (prev_bb, NULL);
ptr_to_fndir->nextModuleEntry = lendian32 (brig_code.total_size);
/* Fill up label references for all sbr instructions. */
if (switch_instructions)
{
for (unsigned i = 0; i < switch_instructions->length (); i++)
{
hsa_insn_sbr *sbr = (*switch_instructions)[i];
for (unsigned j = 0; j < sbr->m_jump_table.length (); j++)
{
hsa_bb *hbb = hsa_bb_for_bb (sbr->m_jump_table[j]);
sbr->m_label_code_list->m_offsets[j]
= hbb->m_label_ref.m_directive_offset;
}
}
switch_instructions->release ();
delete switch_instructions;
switch_instructions = NULL;
}
if (dump_file)
{
fprintf (dump_file, "------- After BRIG emission: -------\n");
dump_hsa_cfun (dump_file);
}
emit_queued_operands ();
}
/* Emit all OMP symbols related to OMP. */
void
hsa_brig_emit_omp_symbols (void)
{
brig_init ();
emit_directive_variable (hsa_num_threads);
}
/* Create and return __hsa_global_variables symbol that contains
all informations consumed by libgomp to link global variables
with their string names used by an HSA kernel. */
static tree
hsa_output_global_variables ()
{
unsigned l = hsa_global_variable_symbols->elements ();
tree variable_info_type = make_node (RECORD_TYPE);
tree id_f1 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("name"), ptr_type_node);
DECL_CHAIN (id_f1) = NULL_TREE;
tree id_f2 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("omp_data_size"),
ptr_type_node);
DECL_CHAIN (id_f2) = id_f1;
finish_builtin_struct (variable_info_type, "__hsa_variable_info", id_f2,
NULL_TREE);
tree int_num_of_global_vars;
int_num_of_global_vars = build_int_cst (uint32_type_node, l);
tree global_vars_num_index_type = build_index_type (int_num_of_global_vars);
tree global_vars_array_type = build_array_type (variable_info_type,
global_vars_num_index_type);
TYPE_ARTIFICIAL (global_vars_array_type) = 1;
vec<constructor_elt, va_gc> *global_vars_vec = NULL;
for (hash_table <hsa_noop_symbol_hasher>::iterator it
= hsa_global_variable_symbols->begin ();
it != hsa_global_variable_symbols->end (); ++it)
{
unsigned len = strlen ((*it)->m_name);
char *copy = XNEWVEC (char, len + 2);
copy[0] = '&';
memcpy (copy + 1, (*it)->m_name, len);
copy[len + 1] = '\0';
len++;
hsa_sanitize_name (copy);
tree var_name = build_string (len, copy);
TREE_TYPE (var_name)
= build_array_type (char_type_node, build_index_type (size_int (len)));
free (copy);
vec<constructor_elt, va_gc> *variable_info_vec = NULL;
CONSTRUCTOR_APPEND_ELT (variable_info_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type (TREE_TYPE (var_name)),
var_name));
CONSTRUCTOR_APPEND_ELT (variable_info_vec, NULL_TREE,
build_fold_addr_expr ((*it)->m_decl));
tree variable_info_ctor = build_constructor (variable_info_type,
variable_info_vec);
CONSTRUCTOR_APPEND_ELT (global_vars_vec, NULL_TREE,
variable_info_ctor);
}
tree global_vars_ctor = build_constructor (global_vars_array_type,
global_vars_vec);
char tmp_name[64];
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_global_variables", 1);
tree global_vars_table = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
global_vars_array_type);
TREE_STATIC (global_vars_table) = 1;
TREE_READONLY (global_vars_table) = 1;
TREE_PUBLIC (global_vars_table) = 0;
DECL_ARTIFICIAL (global_vars_table) = 1;
DECL_IGNORED_P (global_vars_table) = 1;
DECL_EXTERNAL (global_vars_table) = 0;
TREE_CONSTANT (global_vars_table) = 1;
DECL_INITIAL (global_vars_table) = global_vars_ctor;
varpool_node::finalize_decl (global_vars_table);
return global_vars_table;
}
/* Create __hsa_host_functions and __hsa_kernels that contain
all informations consumed by libgomp to register all kernels
in the BRIG binary. */
static void
hsa_output_kernels (tree *host_func_table, tree *kernels)
{
unsigned map_count = hsa_get_number_decl_kernel_mappings ();
tree int_num_of_kernels;
int_num_of_kernels = build_int_cst (uint32_type_node, map_count);
tree kernel_num_index_type = build_index_type (int_num_of_kernels);
tree host_functions_array_type = build_array_type (ptr_type_node,
kernel_num_index_type);
TYPE_ARTIFICIAL (host_functions_array_type) = 1;
vec<constructor_elt, va_gc> *host_functions_vec = NULL;
for (unsigned i = 0; i < map_count; ++i)
{
tree decl = hsa_get_decl_kernel_mapping_decl (i);
tree host_fn = build_fold_addr_expr (hsa_get_host_function (decl));
CONSTRUCTOR_APPEND_ELT (host_functions_vec, NULL_TREE, host_fn);
}
tree host_functions_ctor = build_constructor (host_functions_array_type,
host_functions_vec);
char tmp_name[64];
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_host_functions", 1);
tree hsa_host_func_table = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
host_functions_array_type);
TREE_STATIC (hsa_host_func_table) = 1;
TREE_READONLY (hsa_host_func_table) = 1;
TREE_PUBLIC (hsa_host_func_table) = 0;
DECL_ARTIFICIAL (hsa_host_func_table) = 1;
DECL_IGNORED_P (hsa_host_func_table) = 1;
DECL_EXTERNAL (hsa_host_func_table) = 0;
TREE_CONSTANT (hsa_host_func_table) = 1;
DECL_INITIAL (hsa_host_func_table) = host_functions_ctor;
varpool_node::finalize_decl (hsa_host_func_table);
*host_func_table = hsa_host_func_table;
/* Following code emits list of kernel_info structures. */
tree kernel_info_type = make_node (RECORD_TYPE);
tree id_f1 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("name"), ptr_type_node);
DECL_CHAIN (id_f1) = NULL_TREE;
tree id_f2 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("omp_data_size"),
unsigned_type_node);
DECL_CHAIN (id_f2) = id_f1;
tree id_f3 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("gridified_kernel_p"),
boolean_type_node);
DECL_CHAIN (id_f3) = id_f2;
tree id_f4 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("kernel_dependencies_count"),
unsigned_type_node);
DECL_CHAIN (id_f4) = id_f3;
tree id_f5 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("kernel_dependencies"),
build_pointer_type (build_pointer_type
(char_type_node)));
DECL_CHAIN (id_f5) = id_f4;
finish_builtin_struct (kernel_info_type, "__hsa_kernel_info", id_f5,
NULL_TREE);
int_num_of_kernels = build_int_cstu (uint32_type_node, map_count);
tree kernel_info_vector_type
= build_array_type (kernel_info_type,
build_index_type (int_num_of_kernels));
TYPE_ARTIFICIAL (kernel_info_vector_type) = 1;
vec<constructor_elt, va_gc> *kernel_info_vector_vec = NULL;
tree kernel_dependencies_vector_type = NULL;
for (unsigned i = 0; i < map_count; ++i)
{
tree kernel = hsa_get_decl_kernel_mapping_decl (i);
char *name = hsa_get_decl_kernel_mapping_name (i);
unsigned len = strlen (name);
char *copy = XNEWVEC (char, len + 2);
copy[0] = '&';
memcpy (copy + 1, name, len);
copy[len + 1] = '\0';
len++;
tree kern_name = build_string (len, copy);
TREE_TYPE (kern_name)
= build_array_type (char_type_node, build_index_type (size_int (len)));
free (copy);
unsigned omp_size = hsa_get_decl_kernel_mapping_omp_size (i);
tree omp_data_size = build_int_cstu (unsigned_type_node, omp_size);
bool gridified_kernel_p = hsa_get_decl_kernel_mapping_gridified (i);
tree gridified_kernel_p_tree = build_int_cstu (boolean_type_node,
gridified_kernel_p);
unsigned count = 0;
vec<constructor_elt, va_gc> *kernel_dependencies_vec = NULL;
if (hsa_decl_kernel_dependencies)
{
vec<const char *> **slot;
slot = hsa_decl_kernel_dependencies->get (kernel);
if (slot)
{
vec <const char *> *dependencies = *slot;
count = dependencies->length ();
kernel_dependencies_vector_type
= build_array_type (build_pointer_type (char_type_node),
build_index_type (size_int (count)));
TYPE_ARTIFICIAL (kernel_dependencies_vector_type) = 1;
for (unsigned j = 0; j < count; j++)
{
const char *d = (*dependencies)[j];
len = strlen (d);
tree dependency_name = build_string (len, d);
TREE_TYPE (dependency_name)
= build_array_type (char_type_node,
build_index_type (size_int (len)));
CONSTRUCTOR_APPEND_ELT
(kernel_dependencies_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type (TREE_TYPE (dependency_name)),
dependency_name));
}
}
}
tree dependencies_count = build_int_cstu (unsigned_type_node, count);
vec<constructor_elt, va_gc> *kernel_info_vec = NULL;
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type (TREE_TYPE
(kern_name)),
kern_name));
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE, omp_data_size);
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE,
gridified_kernel_p_tree);
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE, dependencies_count);
if (count > 0)
{
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_dependencies_list", i);
gcc_checking_assert (kernel_dependencies_vector_type);
tree dependencies_list = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
kernel_dependencies_vector_type);
TREE_STATIC (dependencies_list) = 1;
TREE_READONLY (dependencies_list) = 1;
TREE_PUBLIC (dependencies_list) = 0;
DECL_ARTIFICIAL (dependencies_list) = 1;
DECL_IGNORED_P (dependencies_list) = 1;
DECL_EXTERNAL (dependencies_list) = 0;
TREE_CONSTANT (dependencies_list) = 1;
DECL_INITIAL (dependencies_list)
= build_constructor (kernel_dependencies_vector_type,
kernel_dependencies_vec);
varpool_node::finalize_decl (dependencies_list);
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type
(TREE_TYPE (dependencies_list)),
dependencies_list));
}
else
CONSTRUCTOR_APPEND_ELT (kernel_info_vec, NULL_TREE, null_pointer_node);
tree kernel_info_ctor = build_constructor (kernel_info_type,
kernel_info_vec);
CONSTRUCTOR_APPEND_ELT (kernel_info_vector_vec, NULL_TREE,
kernel_info_ctor);
}
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_kernels", 1);
tree hsa_kernels = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
kernel_info_vector_type);
TREE_STATIC (hsa_kernels) = 1;
TREE_READONLY (hsa_kernels) = 1;
TREE_PUBLIC (hsa_kernels) = 0;
DECL_ARTIFICIAL (hsa_kernels) = 1;
DECL_IGNORED_P (hsa_kernels) = 1;
DECL_EXTERNAL (hsa_kernels) = 0;
TREE_CONSTANT (hsa_kernels) = 1;
DECL_INITIAL (hsa_kernels) = build_constructor (kernel_info_vector_type,
kernel_info_vector_vec);
varpool_node::finalize_decl (hsa_kernels);
*kernels = hsa_kernels;
}
/* Create a static constructor that will register out brig stuff with
libgomp. */
static void
hsa_output_libgomp_mapping (tree brig_decl)
{
unsigned kernel_count = hsa_get_number_decl_kernel_mappings ();
unsigned global_variable_count = hsa_global_variable_symbols->elements ();
tree kernels;
tree host_func_table;
hsa_output_kernels (&host_func_table, &kernels);
tree global_vars = hsa_output_global_variables ();
tree hsa_image_desc_type = make_node (RECORD_TYPE);
tree id_f1 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("brig_module"), ptr_type_node);
DECL_CHAIN (id_f1) = NULL_TREE;
tree id_f2 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("kernel_count"),
unsigned_type_node);
DECL_CHAIN (id_f2) = id_f1;
tree id_f3 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("hsa_kernel_infos"),
ptr_type_node);
DECL_CHAIN (id_f3) = id_f2;
tree id_f4 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("global_variable_count"),
unsigned_type_node);
DECL_CHAIN (id_f4) = id_f3;
tree id_f5 = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("hsa_global_variable_infos"),
ptr_type_node);
DECL_CHAIN (id_f5) = id_f4;
finish_builtin_struct (hsa_image_desc_type, "__hsa_image_desc", id_f5,
NULL_TREE);
TYPE_ARTIFICIAL (hsa_image_desc_type) = 1;
vec<constructor_elt, va_gc> *img_desc_vec = NULL;
CONSTRUCTOR_APPEND_ELT (img_desc_vec, NULL_TREE,
build_fold_addr_expr (brig_decl));
CONSTRUCTOR_APPEND_ELT (img_desc_vec, NULL_TREE,
build_int_cstu (unsigned_type_node, kernel_count));
CONSTRUCTOR_APPEND_ELT (img_desc_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type (TREE_TYPE (kernels)),
kernels));
CONSTRUCTOR_APPEND_ELT (img_desc_vec, NULL_TREE,
build_int_cstu (unsigned_type_node,
global_variable_count));
CONSTRUCTOR_APPEND_ELT (img_desc_vec, NULL_TREE,
build1 (ADDR_EXPR,
build_pointer_type (TREE_TYPE (global_vars)),
global_vars));
tree img_desc_ctor = build_constructor (hsa_image_desc_type, img_desc_vec);
char tmp_name[64];
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_img_descriptor", 1);
tree hsa_img_descriptor = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
hsa_image_desc_type);
TREE_STATIC (hsa_img_descriptor) = 1;
TREE_READONLY (hsa_img_descriptor) = 1;
TREE_PUBLIC (hsa_img_descriptor) = 0;
DECL_ARTIFICIAL (hsa_img_descriptor) = 1;
DECL_IGNORED_P (hsa_img_descriptor) = 1;
DECL_EXTERNAL (hsa_img_descriptor) = 0;
TREE_CONSTANT (hsa_img_descriptor) = 1;
DECL_INITIAL (hsa_img_descriptor) = img_desc_ctor;
varpool_node::finalize_decl (hsa_img_descriptor);
/* Construct the "host_table" libgomp expects. */
tree index_type = build_index_type (build_int_cst (integer_type_node, 4));
tree libgomp_host_table_type = build_array_type (ptr_type_node, index_type);
TYPE_ARTIFICIAL (libgomp_host_table_type) = 1;
vec<constructor_elt, va_gc> *libgomp_host_table_vec = NULL;
tree host_func_table_addr = build_fold_addr_expr (host_func_table);
CONSTRUCTOR_APPEND_ELT (libgomp_host_table_vec, NULL_TREE,
host_func_table_addr);
offset_int func_table_size
= wi::to_offset (TYPE_SIZE_UNIT (ptr_type_node)) * kernel_count;
CONSTRUCTOR_APPEND_ELT (libgomp_host_table_vec, NULL_TREE,
fold_build2 (POINTER_PLUS_EXPR,
TREE_TYPE (host_func_table_addr),
host_func_table_addr,
build_int_cst (size_type_node,
func_table_size.to_uhwi
())));
CONSTRUCTOR_APPEND_ELT (libgomp_host_table_vec, NULL_TREE, null_pointer_node);
CONSTRUCTOR_APPEND_ELT (libgomp_host_table_vec, NULL_TREE, null_pointer_node);
tree libgomp_host_table_ctor = build_constructor (libgomp_host_table_type,
libgomp_host_table_vec);
ASM_GENERATE_INTERNAL_LABEL (tmp_name, "__hsa_libgomp_host_table", 1);
tree hsa_libgomp_host_table = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (tmp_name),
libgomp_host_table_type);
TREE_STATIC (hsa_libgomp_host_table) = 1;
TREE_READONLY (hsa_libgomp_host_table) = 1;
TREE_PUBLIC (hsa_libgomp_host_table) = 0;
DECL_ARTIFICIAL (hsa_libgomp_host_table) = 1;
DECL_IGNORED_P (hsa_libgomp_host_table) = 1;
DECL_EXTERNAL (hsa_libgomp_host_table) = 0;
TREE_CONSTANT (hsa_libgomp_host_table) = 1;
DECL_INITIAL (hsa_libgomp_host_table) = libgomp_host_table_ctor;
varpool_node::finalize_decl (hsa_libgomp_host_table);
/* Generate an initializer with a call to the registration routine. */
tree offload_register
= builtin_decl_explicit (BUILT_IN_GOMP_OFFLOAD_REGISTER);
gcc_checking_assert (offload_register);
tree *hsa_ctor_stmts = hsa_get_ctor_statements ();
append_to_statement_list
(build_call_expr (offload_register, 4,
build_int_cstu (unsigned_type_node,
GOMP_VERSION_PACK (GOMP_VERSION,
GOMP_VERSION_HSA)),
build_fold_addr_expr (hsa_libgomp_host_table),
build_int_cst (integer_type_node, GOMP_DEVICE_HSA),
build_fold_addr_expr (hsa_img_descriptor)),
hsa_ctor_stmts);
cgraph_build_static_cdtor ('I', *hsa_ctor_stmts, DEFAULT_INIT_PRIORITY);
tree offload_unregister
= builtin_decl_explicit (BUILT_IN_GOMP_OFFLOAD_UNREGISTER);
gcc_checking_assert (offload_unregister);
tree *hsa_dtor_stmts = hsa_get_dtor_statements ();
append_to_statement_list
(build_call_expr (offload_unregister, 4,
build_int_cstu (unsigned_type_node,
GOMP_VERSION_PACK (GOMP_VERSION,
GOMP_VERSION_HSA)),
build_fold_addr_expr (hsa_libgomp_host_table),
build_int_cst (integer_type_node, GOMP_DEVICE_HSA),
build_fold_addr_expr (hsa_img_descriptor)),
hsa_dtor_stmts);
cgraph_build_static_cdtor ('D', *hsa_dtor_stmts, DEFAULT_INIT_PRIORITY);
}
/* Emit the brig module we have compiled to a section in the final assembly and
also create a compile unit static constructor that will register the brig
module with libgomp. */
void
hsa_output_brig (void)
{
section *saved_section;
if (!brig_initialized)
return;
for (unsigned i = 0; i < function_call_linkage.length (); i++)
{
function_linkage_pair p = function_call_linkage[i];
BrigCodeOffset32_t *func_offset = function_offsets->get (p.function_decl);
gcc_assert (*func_offset);
BrigOperandCodeRef *code_ref
= (BrigOperandCodeRef *) (brig_operand.get_ptr_by_offset (p.offset));
gcc_assert (code_ref->base.kind == BRIG_KIND_OPERAND_CODE_REF);
code_ref->ref = lendian32 (*func_offset);
}
/* Iterate all function declarations and if we meet a function that should
have module linkage and we are unable to emit HSAIL for the function,
then change the linkage to program linkage. Doing so, we will emit
a valid BRIG image. */
if (hsa_failed_functions != NULL && emitted_declarations != NULL)
for (hash_map <tree, BrigDirectiveExecutable *>::iterator it
= emitted_declarations->begin ();
it != emitted_declarations->end ();
++it)
{
if (hsa_failed_functions->contains ((*it).first))
(*it).second->linkage = BRIG_LINKAGE_PROGRAM;
}
saved_section = in_section;
switch_to_section (get_section (BRIG_ELF_SECTION_NAME, SECTION_NOTYPE, NULL));
char tmp_name[64];
ASM_GENERATE_INTERNAL_LABEL (tmp_name, BRIG_LABEL_STRING, 1);
ASM_OUTPUT_LABEL (asm_out_file, tmp_name);
tree brig_id = get_identifier (tmp_name);
tree brig_decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, brig_id,
char_type_node);
SET_DECL_ASSEMBLER_NAME (brig_decl, brig_id);
TREE_ADDRESSABLE (brig_decl) = 1;
TREE_READONLY (brig_decl) = 1;
DECL_ARTIFICIAL (brig_decl) = 1;
DECL_IGNORED_P (brig_decl) = 1;
TREE_STATIC (brig_decl) = 1;
TREE_PUBLIC (brig_decl) = 0;
TREE_USED (brig_decl) = 1;
DECL_INITIAL (brig_decl) = brig_decl;
TREE_ASM_WRITTEN (brig_decl) = 1;
BrigModuleHeader module_header;
memcpy (&module_header.identification, "HSA BRIG",
sizeof (module_header.identification));
module_header.brigMajor = lendian32 (BRIG_VERSION_BRIG_MAJOR);
module_header.brigMinor = lendian32 (BRIG_VERSION_BRIG_MINOR);
uint64_t section_index[3];
int data_padding, code_padding, operand_padding;
data_padding = HSA_SECTION_ALIGNMENT
- brig_data.total_size % HSA_SECTION_ALIGNMENT;
code_padding = HSA_SECTION_ALIGNMENT
- brig_code.total_size % HSA_SECTION_ALIGNMENT;
operand_padding = HSA_SECTION_ALIGNMENT
- brig_operand.total_size % HSA_SECTION_ALIGNMENT;
uint64_t module_size = sizeof (module_header)
+ sizeof (section_index)
+ brig_data.total_size
+ data_padding
+ brig_code.total_size
+ code_padding
+ brig_operand.total_size
+ operand_padding;
gcc_assert ((module_size % 16) == 0);
module_header.byteCount = lendian64 (module_size);
memset (&module_header.hash, 0, sizeof (module_header.hash));
module_header.reserved = 0;
module_header.sectionCount = lendian32 (3);
module_header.sectionIndex = lendian64 (sizeof (module_header));
assemble_string ((const char *) &module_header, sizeof (module_header));
uint64_t off = sizeof (module_header) + sizeof (section_index);
section_index[0] = lendian64 (off);
off += brig_data.total_size + data_padding;
section_index[1] = lendian64 (off);
off += brig_code.total_size + code_padding;
section_index[2] = lendian64 (off);
assemble_string ((const char *) §ion_index, sizeof (section_index));
char padding[HSA_SECTION_ALIGNMENT];
memset (padding, 0, sizeof (padding));
brig_data.output ();
assemble_string (padding, data_padding);
brig_code.output ();
assemble_string (padding, code_padding);
brig_operand.output ();
assemble_string (padding, operand_padding);
if (saved_section)
switch_to_section (saved_section);
hsa_output_libgomp_mapping (brig_decl);
hsa_free_decl_kernel_mapping ();
brig_release_data ();
hsa_deinit_compilation_unit_data ();
delete emitted_declarations;
emitted_declarations = NULL;
delete function_offsets;
function_offsets = NULL;
}
|