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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2022 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "SpillCleanup.h"
#include "Assertions.h"
#include "FlowGraph.h"
#include "GraphColor.h"
#include <list>
uint32_t computeFillMsgDesc(unsigned int payloadSize, unsigned int offset);
uint32_t computeSpillMsgDesc(unsigned int payloadSize, unsigned int offset);
namespace vISA {
static bool isGRFAssigned(G4_Operand *opnd) {
vISA_ASSERT(opnd->isSrcRegRegion() || opnd->isDstRegRegion(),
"expecting src/dst reg region");
auto *topDcl = opnd->getTopDcl();
if (!topDcl)
return false;
auto *regVar = topDcl->getRegVar();
return regVar->getPhyReg() != nullptr;
}
G4_SrcRegRegion *CoalesceSpillFills::generateCoalescedSpill(
G4_SrcRegRegion *header, unsigned int scratchOffset,
unsigned int payloadSize, bool useNoMask, G4_InstOption mask,
G4_Declare *spillDcl, unsigned int row) {
// Generate split send instruction with specified payload size and offset
auto spillSrcPayload = kernel.fg.builder->createSrc(
spillDcl->getRegVar(), row, 0, kernel.fg.builder->getRegionStride1(),
Type_UD);
// Create send instruction with payloadSize starting at scratch offset min
G4_Declare *fp = nullptr;
if (kernel.fg.getHasStackCalls() || kernel.fg.getIsStackCallFunc())
fp = kernel.fg.getFramePtrDcl();
unsigned int option = useNoMask ? InstOpt_WriteEnable : 0;
auto spillInst = kernel.fg.builder->createSpill(
kernel.fg.builder->createNullDst(Type_UW), header, spillSrcPayload,
g4::SIMD16, payloadSize,
GlobalRA::GRFToHwordSize(scratchOffset, *kernel.fg.builder), fp,
static_cast<G4_InstOption>(option), false);
if (!useNoMask) {
// Care about mask only for non-NoMask sends
spillInst->setMaskOption(mask);
}
return spillSrcPayload;
}
G4_INST *CoalesceSpillFills::generateCoalescedFill(G4_SrcRegRegion *header,
unsigned int scratchOffset,
unsigned int payloadSize,
unsigned int dclSize,
bool evenAlignDst) {
// Generate split send instruction with specified payload size and offset
// Construct fillDst
const char *dclName = kernel.fg.builder->getNameString(
32, "COAL_FILL_%" PRIu64, kernel.Declares.size());
auto fillDcl = kernel.fg.builder->createDeclare(
dclName, G4_GRF, kernel.numEltPerGRF<Type_UD>(), dclSize, Type_UD,
DeclareType::CoalescedSpillFill);
if (evenAlignDst) {
fillDcl->setEvenAlign();
gra.setEvenAligned(fillDcl, true);
}
fillDcl->setDoNotSpill();
auto fillDst =
kernel.fg.builder->createDst(fillDcl->getRegVar(), 0, 0, 1, Type_UW);
G4_Declare *fp = nullptr;
if (kernel.fg.getHasStackCalls() || kernel.fg.getIsStackCallFunc())
fp = kernel.fg.getFramePtrDcl();
auto fillInst = kernel.fg.builder->createFill(
header, fillDst, g4::SIMD16, payloadSize,
GlobalRA::GRFToHwordSize(scratchOffset, *kernel.fg.builder), fp,
InstOpt_WriteEnable, false);
return fillInst;
}
G4_Declare *
CoalesceSpillFills::createCoalescedSpillDcl(unsigned int payloadSize) {
// Construct spill src
const char *dclName = nullptr;
G4_Declare *spillDcl = nullptr;
dclName = kernel.fg.builder->getNameString(32, "COAL_SPILL_%" PRIu64,
kernel.Declares.size());
spillDcl = kernel.fg.builder->createDeclare(
dclName, G4_GRF, kernel.numEltPerGRF<Type_UD>(), payloadSize, Type_UD,
DeclareType::CoalescedSpillFill);
spillDcl->setDoNotSpill();
return spillDcl;
}
void CoalesceSpillFills::coalesceSpills(
std::list<INST_LIST_ITER> &coalesceableSpills, unsigned int min,
unsigned int max, bool useNoMask, G4_InstOption mask) {
// Generate fill with minimum size = max-min. This should be compatible with
// payload sizes supported by hardware.
unsigned int payloadSize = (max - min) + 1;
auto leadInst = *coalesceableSpills.front();
vISA_ASSERT(payloadSize == 1 || payloadSize == 2 || payloadSize == 4 ||
payloadSize == 8,
"Unsupported payload size");
std::set<G4_Declare *> declares;
unsigned int minRow = UINT_MAX;
for (const auto &d : coalesceableSpills) {
auto src1Opnd = (*d)->getSrc(1)->asSrcRegRegion();
auto curRow = src1Opnd->getLeftBound() / kernel.numEltPerGRF<Type_UB>();
declares.insert(src1Opnd->getTopDcl());
minRow = minRow > curRow ? curRow : minRow;
vISA_ASSERT((*d)->isSpillIntrinsic() &&
!isGRFAssigned((*d)->asSpillIntrinsic()->getPayload()),
"shouldnt coalesce spill with assigned GRF");
}
G4_Declare *dcl = nullptr;
if (declares.size() == 1) {
dcl = (*declares.begin());
} else {
dcl = createCoalescedSpillDcl(payloadSize);
minRow = 0;
}
auto coalescedSpillSrc =
generateCoalescedSpill(kernel.fg.builder->duplicateOperand(
leadInst->asSpillIntrinsic()->getHeader()),
min, payloadSize, useNoMask, mask, dcl, minRow);
coalescedSpillSrc->getInst()->inheritDIFrom(leadInst);
if (declares.size() != 1) {
for (const auto &c : coalesceableSpills) {
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo((*c), scratchOffset, scratchSize);
unsigned int rowOff = scratchOffset - min;
replaceMap.insert(std::make_pair(
(*c)->getSrc(1)->getTopDcl(),
std::make_pair(coalescedSpillSrc->getTopDcl(), rowOff)));
}
}
auto f = coalesceableSpills.back();
f++;
for (const auto &spill : coalesceableSpills) {
gra.incRA.markForIntfUpdate(
(*spill)->asSpillIntrinsic()->getPayload()->getTopDcl());
curBB->erase(spill);
}
coalesceableSpills.clear();
curBB->insertBefore(f, coalescedSpillSrc->getInst());
}
void CoalesceSpillFills::coalesceFills(
std::list<INST_LIST_ITER> &coalesceableFills, unsigned int min,
unsigned int max) {
// Generate fill with minimum size = max-min. This should be compatible with
// payload sizes supported by hardware.
unsigned int payloadSize = (max - min) + 1;
if (payloadSize == 3)
payloadSize = 4;
else if (payloadSize > 4)
payloadSize = 8;
else if (payloadSize == 0)
payloadSize = 1;
vISA_ASSERT(payloadSize == 1 || payloadSize == 2 || payloadSize == 4 ||
payloadSize == 8,
"Unsupported payload size");
// dclSize could be larger than payload size when
// 2 variables across scratch writes are coalesced.
unsigned int dclSize = payloadSize;
for (const auto &c : coalesceableFills) {
unsigned int scratchOffset, scratchSize;
auto fill = (*c);
getScratchMsgInfo((*c), scratchOffset, scratchSize);
auto fillDst = fill->getDst();
auto fillDstRegOff = fillDst->getRegOff();
unsigned int dstDclRows = fillDst->getTopDcl()->getNumRows();
unsigned int maxRow = dstDclRows + scratchOffset - fillDstRegOff - min;
if (maxRow > dclSize)
dclSize = maxRow;
vISA_ASSERT((*c)->isFillIntrinsic() &&
!isGRFAssigned((*c)->asFillIntrinsic()->getDst()),
"cannot coalesce fill with pre-assigned GRF");
gra.incRA.markForIntfUpdate(fillDst->getTopDcl());
}
auto leadInst = *coalesceableFills.front();
auto newFill =
generateCoalescedFill(kernel.fg.builder->duplicateOperand(
leadInst->asFillIntrinsic()->getHeader()),
min, payloadSize, dclSize,
gra.isEvenAligned(leadInst->getDst()->getTopDcl()));
newFill->inheritDIFrom(leadInst);
for (const auto &c : coalesceableFills) {
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo((*c), scratchOffset, scratchSize);
unsigned int rowOff = scratchOffset - min;
replaceMap.insert(
std::make_pair((*c)->getDst()->getTopDcl(),
std::make_pair(newFill->getDst()->getTopDcl(), rowOff)));
}
auto f = coalesceableFills.front();
f++;
for (const auto &fill : coalesceableFills) {
if (fill == f) {
f++;
}
curBB->erase(fill);
}
coalesceableFills.clear();
curBB->insertBefore(f, newFill);
}
// Return true if heuristic agrees to coalescing.
bool CoalesceSpillFills::fillHeuristic(
std::list<INST_LIST_ITER> &coalesceableFills,
std::list<INST_LIST_ITER> &instList,
const std::list<INST_LIST_ITER> &origInstList, unsigned int &min,
unsigned int &max) {
#if 0
std::bitset<8> bits(0);
vISA_ASSERT(cMaxFillPayloadSize == 8, "Handle other max fill payload size");
#else
std::bitset<4> bits(0);
vISA_ASSERT(cMaxFillPayloadSize == 4, "Handle other max fill payload size");
#endif
if (coalesceableFills.size() <= 1) {
return false;
}
min = 0xffffffff, max = 0;
G4_Declare *header =
(*coalesceableFills.front())->asFillIntrinsic()->getHeader()->getTopDcl();
for (const auto &f : coalesceableFills) {
if ((*f)->asFillIntrinsic()->getHeader()->getTopDcl() != header &&
!(*f)->asFillIntrinsic()->getFP())
return false;
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo(*f, scratchOffset, scratchSize);
if (scratchSize == 8) {
return false;
}
if ((*f)->getDst()->getTopDcl()->isAddrSpillFill()) {
return false;
}
for (auto i = scratchOffset; i < (scratchOffset + scratchSize); i++)
bits.set(i - scratchOffset);
if (min > scratchOffset)
min = scratchOffset;
if (max < (scratchOffset + scratchSize - 1))
max = (scratchOffset + scratchSize - 1);
}
// Iterate over coalescable fills and ensure all rows of a variable
// are fill candidates. If not, then don't fill. This helps cases like,
// #1 FILL_V10(0,0) <-- load 0x10 ... (4 GRFs)
// #2 FILL_V10(4,0) <-- load 0x14 ... (1 GRF)
// #3 send ... FILL_V10(0,0) ... (use 3 GRFs of FILL_V10)
// #4 FILL_V11(0,0) <-- load 0x15 ... (1 GRF)
//
// Loads at #2 and #4 can be coalesced. But this requires a new coalesced
// variable of size = 6 GRFs. This size can quickly increase for Cm where
// payloads of 8 GRF are also present. So instead of allowing these cases
// at the risk of spilling more, we require that all rows of fill range
// are candidates in coalesceableFills list. So when only #2 and #4 are
// fill candidates, we will not coalesce them. This makes us miss some
// cases where subset rows of fills have been converted to movs.
const int maxDclSize = 128;
std::map<G4_Declare *, std::bitset<maxDclSize>> allRows;
for (const auto &c : coalesceableFills) {
auto topdcl = (*c)->getDst()->getTopDcl();
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo(*c, scratchOffset, scratchSize);
auto it = allRows.find(topdcl);
if (it == allRows.end()) {
allRows.insert(std::make_pair(topdcl, std::bitset<maxDclSize>()));
it = allRows.find(topdcl);
}
// Now mark bits corresponding to rows
unsigned int regOff = (*c)->getDst()->getRegOff();
for (unsigned int r = regOff; r < (regOff + scratchSize); r++) {
it->second.set(r);
}
}
// Check whether all dcls in map have all rows filled
for (auto &&r : allRows) {
unsigned int numRows = r.first->getNumRows();
for (unsigned int i = 0; i < numRows; i++) {
if (r.second.test(i) == false) {
// Found a row of variable that isnt captured in
// list of candidate fills.
return false;
}
}
}
#if 0
for (auto f : coalesceableFills)
{
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo(*f, scratchOffset, scratchSize);
for (auto i = scratchOffset; i < (scratchOffset + scratchSize); i++)
bits.set(i - min);
}
#endif
if (max - min <= 3) {
// Will emit at most 4GRF read
if (bits[0] != bits[1] && bits[2] != bits[3]) {
// Don't coalesce patterns like
// 1010, 0101
return false;
}
if ((bits[0] & bits[3]) && !(bits[1] | bits[2])) {
// 1001
return false;
}
}
return true;
}
bool CoalesceSpillFills::notOOB(unsigned int min, unsigned int max) {
// Disallow coalescing if it exceeds spill size computed by RA
// as this may cause segmentation fault.
auto nextSpillOffset = spill.getNextOffset();
auto diff = max - min;
auto HWordByteSize = GlobalRA::getHWordByteSize();
if (diff == 2 && (min + 3) * HWordByteSize >= nextSpillOffset) {
// Coalesce 1-1-
// Payload size 4 is needed as candidates are not back-to-back.
// Coalescing is illegal if last row exceeds spill size computed by RA.
return false;
} else if (diff > 3 && diff < 7 &&
(min + 7) * HWordByteSize >= nextSpillOffset) {
// payload size = 8 would be used
return false;
}
return true;
}
// instList contains all instructions (fills or spills) within window size.
// At exit, instList will contain instructions that will not be coalesced.
// coalescable list will contain instructions within min-max offset range.
// First instruction's offset in instList is set to be min. max is
// min + maxPayloadSize - 1.
void CoalesceSpillFills::sendsInRange(std::list<INST_LIST_ITER> &instList,
std::list<INST_LIST_ITER> &coalescable,
unsigned int maxPayloadSize,
unsigned int &min, unsigned int &max) {
min = 0xffffffff;
max = 0;
bool isFirstNoMask = false;
unsigned int mask = 0;
for (auto iter = instList.begin(); iter != instList.end();) {
unsigned scratchOffset, sizeInGrfUnit, lastScratchOffset;
auto inst = *(*iter);
getScratchMsgInfo(inst, scratchOffset, sizeInGrfUnit);
lastScratchOffset = scratchOffset + sizeInGrfUnit - 1;
if (min == 0xffffffff && max == 0) {
// First spill is definitely a candidate
min = scratchOffset;
max = lastScratchOffset;
coalescable.push_back(*iter);
iter = instList.erase(iter);
isFirstNoMask = inst->isWriteEnableInst();
mask = inst->getMaskOption();
if (inst->getDst()->getTopDcl()->isAddrSpillFill()) {
return;
}
continue;
}
if (min != 0xffffffff || max != 0) {
bool maskMatch = (isFirstNoMask && inst->isWriteEnableInst()) ||
(mask == inst->getMaskOption());
// don't coalesce if non-leading fill inst has alignment requirements,
// as we may not be able to satisfy it
bool fillDstisAligned = gra.isEvenAligned(inst->getDst()->getTopDcl());
if (!maskMatch || fillDstisAligned) {
iter++;
continue;
}
// Check whether min/max can be extended
if (scratchOffset <= min &&
(min - scratchOffset) <= (cMaxFillPayloadSize - 1) &&
(max - scratchOffset) <= (cMaxFillPayloadSize - 1) &&
notOOB(scratchOffset, max)) {
// This instruction can be coalesced
min = scratchOffset;
if (max < lastScratchOffset)
max = lastScratchOffset;
// vISA_ASSERT(max - min <= (cMaxFillPayloadSize - 1), "Unexpected
// fills coalesced. (max - min) is out of bounds - 1");
coalescable.push_back(*iter);
iter = instList.erase(iter);
} else if (scratchOffset >= max &&
(lastScratchOffset - min) <= (cMaxFillPayloadSize - 1) &&
(lastScratchOffset - max) <= (cMaxFillPayloadSize - 1) &&
notOOB(min, scratchOffset)) {
max = lastScratchOffset;
// vISA_ASSERT(max - min <= cMaxFillPayloadSize, "Unexpected spills
// coalesced. (max - min) is out of bounds - 2");
coalescable.push_back(*iter);
iter = instList.erase(iter);
} else if (scratchOffset >= min && lastScratchOffset <= max) {
coalescable.push_back(*iter);
iter = instList.erase(iter);
} else {
iter++;
}
}
}
}
// instList contains all spills seen in window.
// coalescable is empty and should contain consecutive spills.
// This funtion will prune spills so they write consecutive
// memory slots. First spill is first candidate to start window.
void CoalesceSpillFills::keepConsecutiveSpills(
std::list<INST_LIST_ITER> &instList, std::list<INST_LIST_ITER> &coalescable,
unsigned int maxPayloadSize, unsigned int &minOffset,
unsigned int &maxOffset, bool &useNoMask, G4_InstOption &mask) {
// allowed list contains instructions to be coalesced in
// ascending order of their spill slots.
std::list<INST_LIST_ITER> allowed;
auto origInstList = instList;
allowed.push_back(instList.front());
instList.pop_front();
unsigned int maskOffset = (*allowed.front())->getMaskOption();
mask = (G4_InstOption)(maskOffset & InstOpt_QuarterMasks);
useNoMask = (maskOffset & InstOpt_WriteEnable) ? true : false;
unsigned int size;
getScratchMsgInfo(*allowed.front(), minOffset, size);
maxOffset = minOffset + size - 1;
bool firstSpillFromSend = false;
G4_Declare *sendDstTopDcl = (*allowed.front())->getSrc(1)->getTopDcl();
if (sendDstDcl.find(sendDstTopDcl) != sendDstDcl.end())
firstSpillFromSend = true;
G4_Declare *header =
(*instList.front())->asSpillIntrinsic()->getHeader()->getTopDcl();
for (const auto &instIt : instList) {
auto inst = (*instIt);
if (inst->asSpillIntrinsic()->getHeader()->getTopDcl() != header &&
!inst->asSpillIntrinsic()->getFP()) {
return;
}
useNoMask &= inst->isWriteEnableInst();
if (!useNoMask)
break;
}
if (useNoMask) {
// Spill coalescing doesnt work as expected without NoMask
bool redo;
do {
redo = false;
for (auto spillIt = instList.begin(); spillIt != instList.end();
spillIt++) {
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo(*(*spillIt), scratchOffset, scratchSize);
auto src1 = (*(*spillIt))->getSrc(1);
if (src1 && src1->getTopDcl()->isAddrSpillFill()) {
// Address taken dcls should not be coalesed with others.
// This is dangerous because nothing ties indirect opnd
// with fill/spill instructions for it. Only after RA do
// we update offset of address register holding the
// indirect operand, based on RA assigment to spill/fill
// address taken variable.
continue;
}
if ( // Consecutive scratch offsets
scratchOffset == maxOffset + 1 &&
// Scratch offset + size is within max payload size
(scratchOffset + scratchSize - 1) <=
(minOffset + maxPayloadSize - 1) &&
// Either both masks are same or both are NoMask
(((*(*spillIt))->getMaskOption() ==
(maskOffset & InstOpt_QuarterMasks)) ||
(useNoMask && (*(*spillIt))->isWriteEnableInst()))) {
auto curInstDstTopDcl = (*(*spillIt))->getSrc(1)->getTopDcl();
// Check whether current inst's topdcl was spilled in a send.
// If it was and first instruction in instList wasnt then
// don't consider current instruction as coalescing candidate.
if (!firstSpillFromSend &&
sendDstDcl.find(curInstDstTopDcl) != sendDstDcl.end()) {
continue;
}
// This condition allows send coalescing iff
// a. Either none of the vars are defined in a send
// b. All vars defined in same send
if (!firstSpillFromSend || curInstDstTopDcl == sendDstTopDcl) {
if (curInstDstTopDcl == sendDstTopDcl) {
// Make sure src1 operands are consecutive
auto curSrc1Row =
(*(*spillIt))->getSrc(1)->asSrcRegRegion()->getRegOff();
bool success = true;
for (const auto &candidate : allowed) {
unsigned int candOffset, candSize;
getScratchMsgInfo(*candidate, candOffset, candSize);
auto prevSrc1Row =
(*candidate)->getSrc(1)->asSrcRegRegion()->getRegOff();
unsigned int scratchOffDelta = scratchOffset - candOffset;
if ((prevSrc1Row + scratchOffDelta) != curSrc1Row) {
// Following is disallowed
// send (8) V10(1,0) ... <-- resLen = 4
// sends (8) null r0 V10(1,0) 0x100 <-- extLen = 1
// mov (8) T2 V10(2,0)
// sends (8) null r0 r10(3,0) 0x101 <-- extLen = 1
// mov (8) T4 V10(4,0)
// Two scratch writes cannot be coalesced here
// because their src1 regions arent consecutive.
success = false;
break;
}
}
if (!success)
continue;
}
allowed.push_back(*spillIt);
instList.erase(spillIt);
redo = true;
maxOffset += scratchSize;
break;
}
}
}
} while (redo);
}
while (allowed.size() > 1) {
unsigned int slots = maxOffset - minOffset + 1;
if (slots == 2 || slots == 4) {
// Insert coalescable spills in order of appearance
for (const auto &origInst : origInstList) {
for (const auto &allowedSpills : allowed) {
if (*origInst == *allowedSpills) {
coalescable.push_back(origInst);
break;
}
}
}
vISA_ASSERT(coalescable.size() == allowed.size(),
"Coalesced spills list missing entries");
break;
} else {
allowed.pop_back();
unsigned int scratchOffset, scratchSize;
getScratchMsgInfo(*allowed.back(), scratchOffset, scratchSize);
maxOffset = scratchOffset + scratchSize - 1;
}
}
instList = std::move(origInstList);
for (auto coalIt = coalescable.begin(), instIt = instList.begin();
coalIt != coalescable.end(); coalIt++) {
if (*instIt == *coalIt)
instIt = instList.erase(instIt);
else {
while (*instIt != *coalIt) {
instIt++;
}
instIt = instList.erase(instIt);
}
}
}
INST_LIST_ITER
CoalesceSpillFills::analyzeSpillCoalescing(std::list<INST_LIST_ITER> &instList,
INST_LIST_ITER start,
INST_LIST_ITER end) {
// Check and perform coalescing, if possible, amongst spills in instList.
// Return inst iter points to either last inst+1 in instList if all spills
// were coalesced. Otherwise, it points to first spill that wasnt coalesced.
// Spill coalescing is possible only when all slots in coalesced range
// have a write.
INST_LIST_ITER last = end;
last++;
if (instList.size() < 2) {
return last;
}
std::list<INST_LIST_ITER> coalesceableSpills;
auto origInstList = instList;
unsigned int min, max;
G4_InstOption mask;
bool useNoMask;
keepConsecutiveSpills(instList, coalesceableSpills, cMaxSpillPayloadSize, min,
max, useNoMask, mask);
if (coalesceableSpills.size() > 1) {
coalesceSpills(coalesceableSpills, min, max, useNoMask, mask);
} else {
// When coalescing is not done, we want to
// move to second instruction in instList in
// next loop iteration.
instList.pop_front();
}
if (instList.size() == 0) {
return last;
} else {
return instList.front();
}
}
INST_LIST_ITER
CoalesceSpillFills::analyzeFillCoalescing(std::list<INST_LIST_ITER> &instList,
INST_LIST_ITER start,
INST_LIST_ITER end) {
// Check and perform coalescing, if possible, amongst fills in instList.
// Return inst iter points to either last inst+1 in instList if all fills
// were coalesced. Otherwise, it points to first fill that wasnt coalesced.
INST_LIST_ITER last = end;
last++;
#if 0
G4_INST* lastInst = nullptr;
if (last != bb->end())
lastInst = (*last);
#endif
if (instList.size() < 2) {
return last;
}
std::list<INST_LIST_ITER> coalesceableFills;
auto origInstList = instList;
unsigned int min, max;
sendsInRange(instList, coalesceableFills, cMaxFillPayloadSize, min, max);
bool heuristic =
fillHeuristic(coalesceableFills, instList, origInstList, min, max);
if (!heuristic) {
coalesceableFills.clear();
instList = std::move(origInstList);
instList.pop_front();
}
if (coalesceableFills.size() > 1) {
coalesceFills(coalesceableFills, min, max);
}
if (instList.size() == 0) {
return last;
} else {
return instList.front();
}
}
bool CoalesceSpillFills::overlap(G4_INST *inst1, G4_INST *inst2,
bool &isFullOverlap) {
unsigned int scratchOffset1, scratchSize1, scratchOffset2, scratchSize2;
unsigned int scratchEnd1, scratchEnd2;
getScratchMsgInfo(inst1, scratchOffset1, scratchSize1);
getScratchMsgInfo(inst2, scratchOffset2, scratchSize2);
// isFullOverlap is true only if inst1 full covers inst2
isFullOverlap = false;
scratchEnd1 = scratchOffset1 + scratchSize1 - 1;
scratchEnd2 = scratchOffset2 + scratchSize2 - 1;
if (scratchOffset1 <= scratchOffset2) {
// inst1 |---------| or |----------|
// inst2 |------| |---|
if (scratchEnd1 >= scratchOffset2) {
if (scratchOffset1 <= scratchOffset2 &&
(scratchOffset1 + scratchSize1) >= (scratchOffset2 + scratchSize2)) {
isFullOverlap = !isIncompatibleEMCm(inst1, inst2);
}
return true;
}
} else {
// inst1 |------| or |-----|
// inst2 |-----| |-----------|
if (scratchEnd2 >= scratchOffset1) {
if (scratchOffset1 <= scratchOffset2 &&
(scratchOffset1 + scratchSize1) >= (scratchOffset2 + scratchSize2)) {
isFullOverlap = !isIncompatibleEMCm(inst1, inst2);
}
return true;
}
}
return false;
}
bool CoalesceSpillFills::overlap(G4_INST *inst,
std::list<INST_LIST_ITER> &allInsts) {
for (const auto &sp : allInsts) {
bool t;
auto spillInst = (*sp);
if (overlap(inst, spillInst, t))
return true;
}
return false;
}
void CoalesceSpillFills::removeWARFills(std::list<INST_LIST_ITER> &fills,
std::list<INST_LIST_ITER> &spills) {
for (auto flIt = fills.begin(); flIt != fills.end();) {
if (overlap((*(*flIt)), spills)) {
flIt = fills.erase(flIt);
continue;
}
flIt++;
}
}
// Return true if an operand was replaced
bool CoalesceSpillFills::replaceCoalescedOperands(G4_INST *inst) {
bool IRChanged = false;
auto dst = inst->getDst();
if (dst && dst->getTopDcl()) {
auto dcl = dst->getTopDcl();
auto it = replaceMap.find(dcl);
if (it != replaceMap.end()) {
auto dstRgn = dst->asDstRegRegion();
auto newDstRgn = kernel.fg.builder->createDst(
it->second.first->getRegVar(),
it->second.second + dstRgn->getRegOff(), dstRgn->getSubRegOff(),
dstRgn->getHorzStride(), dstRgn->getType());
newDstRgn->setAccRegSel(dstRgn->getAccRegSel());
inst->setDest(newDstRgn);
IRChanged = true;
}
}
for (unsigned int i = 0, numSrc = inst->getNumSrc(); i < numSrc; i++) {
auto opnd = inst->getSrc(i);
if (opnd && opnd->getTopDcl()) {
auto dcl = opnd->getTopDcl();
auto it = replaceMap.find(dcl);
if (it == replaceMap.end())
continue;
if (opnd->isSrcRegRegion()) {
auto srcRgn = opnd->asSrcRegRegion();
auto oldRgnDesc = srcRgn->getRegion();
auto newSrcRgn = kernel.fg.builder->createSrcRegRegion(
srcRgn->getModifier(), Direct, it->second.first->getRegVar(),
it->second.second + srcRgn->getRegOff(), srcRgn->getSubRegOff(),
oldRgnDesc, opnd->getType());
newSrcRgn->setAccRegSel(srcRgn->getAccRegSel());
inst->setSrc(newSrcRgn, i);
IRChanged = true;
}
}
}
return IRChanged;
}
bool CoalesceSpillFills::allSpillsSameVar(std::list<INST_LIST_ITER> &spills) {
// Return true if all vars in spills list have same dcl
G4_Declare *dcl = nullptr;
for (const auto &s : spills) {
auto topdcl = (*s)->getSrc(1)->getTopDcl();
if (!dcl)
dcl = topdcl;
if (topdcl != dcl) {
return false;
}
}
// Allow only if all dcls are defined by same send
if (sendDstDcl.find(dcl) != sendDstDcl.end())
return true;
return false;
}
void CoalesceSpillFills::fills() {
// Iterate over all BBs, find fills that are closeby and coalesce
// a bunch of them. Insert movs as required.
for (auto bb : kernel.fg) {
if (!isSpillCleanupEnabled(bb))
continue;
if (!gra.hasSpillCodeInBB(bb))
continue;
curBB = bb;
auto endIter = bb->end();
std::list<INST_LIST_ITER> fillsToCoalesce;
std::list<INST_LIST_ITER> spills;
INST_LIST_ITER startIter = bb->begin();
unsigned int w = 0;
unsigned int maxW = 0;
unsigned int instRP = 0;
unsigned int numRowsFillsToCoalesce = 0;
const auto &splitInsts = LoopVarSplit::getSplitInsts(&gra, bb);
bool earlyCoalesce = false;
for (auto instIter = startIter; instIter != endIter;) {
auto inst = (*instIter);
if (inst->isPseudoKill() || inst->isLabel()) {
instIter++;
continue;
}
if (splitInsts.find(inst) != splitInsts.end()) {
// if inst was emitted by loop split transformation,
// then don't optimize it. such instructions are
// emitted in loop preheader/loop exit. if a split
// variable spills, we need to erase all fills and
// spills emitted for that split. if we coalesce
// 2 fills from split sequence, we may end up with
// fill loading data that wont be used. for eg,
//
// (W) mov (8|M0) SPLIT1 V10
// (W) mov (8|M0) SPLIT2 V11
// ==>
//
// (W) fill FILL_V10 @ 0x0
// (W) mov (8|M0) SPLIT1 FILL_V10
// (W) fill FILL_V11 @ 0x32
// (W) mov (8|M0) SPLIT2 FILL_V11
//
// we need to be able to erase 2nd fill and copy in
// case SPLIT2 spills. this cannot be done if the 2
// fills are coalesced.
instIter++;
continue;
}
// Determine window size based on register pressure
unsigned int RP = rpe.getRegisterPressure(inst);
if (RP > 0)
instRP = RP;
if (inst->isSpillIntrinsic()) {
if (inst->asSpillIntrinsic()->isScatterSpill() &&
overlap(inst, fillsToCoalesce)) {
// If current intrinsic is scatter spill and ovelaps fills being
// tracked, break the coalescing chain
earlyCoalesce = true;
}
spills.push_back(instIter);
} else if (inst->isFillIntrinsic()) {
// Check if coalescing is possible
if (fillsToCoalesce.size() == 0) {
w = 0;
maxW = 0;
startIter = instIter;
spills.clear();
numRowsFillsToCoalesce = 0;
}
if (!overlap(*instIter, spills) &&
// dont coalesce fills that have physical GRF assigned
!isGRFAssigned(inst->getDst())) {
fillsToCoalesce.push_back(instIter);
numRowsFillsToCoalesce += inst->asFillIntrinsic()->getNumRows();
}
}
if (fillsToCoalesce.size() > 0 && instRP > fillWindowSizeThreshold) {
// High register pressure region so reduce window size to 3
w = (cWindowSize - w > 3) ? cWindowSize - 3 : w;
}
if (w == cWindowSize &&
(instRP + numRowsFillsToCoalesce) < highRegPressureForWindow) {
// If register pressure is below the threshold even when considering
// potential increase, continue coalescing fills in current BB
--w;
}
if (w == cWindowSize || maxW == cMaxWindowSize || inst == bb->back() ||
earlyCoalesce) {
if (fillsToCoalesce.size() > 1) {
auto nextInst =
analyzeFillCoalescing(fillsToCoalesce, startIter, instIter);
if (earlyCoalesce)
instIter++;
else
instIter = nextInst;
} else if (w == cWindowSize) {
startIter = instIter;
} else if (inst == bb->back()) {
break;
}
w = 0;
maxW = 0;
numRowsFillsToCoalesce = 0;
fillsToCoalesce.clear();
spills.clear();
earlyCoalesce = false;
continue;
}
if (fillsToCoalesce.size() > 0) {
w++;
maxW++;
}
instIter++;
}
// One pass to replace old fills with coalesced dcl
for (auto instIt = bb->begin(); instIt != bb->end();) {
auto inst = (*instIt);
if (inst->isPseudoKill() &&
replaceMap.find(inst->getDst()->getTopDcl()) != replaceMap.end()) {
instIt = bb->erase(instIt);
continue;
}
while (replaceCoalescedOperands(inst)) {
// replaceCoalescedOperands() updates references to old
// dcls in IR that are coalesced. Having a single pass to
// replace operands is insufficient if a coalesced range
// gets re-coalesced. This can happen rarely when window
// heuristic hits boundary condition.
};
instIt++;
}
}
}
void CoalesceSpillFills::populateSendDstDcl() {
// Find and store all G4_Declares that are dest in sends
// and are spilled. This is required when coalescing
// scratch writes for such spills. We cannot mix coalescing
// for G4_Declares from one and and other instructions.
// Otherwise register pressure increases significantly.
for (auto bb : kernel.fg) {
for (auto inst : *bb) {
if (inst->isSend() && inst->getDst()) {
if (!inst->getDst()->isNullReg()) {
if (!inst->getMsgDesc()->isScratch()) {
auto topdcl = inst->getDst()->getTopDcl();
sendDstDcl.insert(topdcl);
}
}
} else if (inst->isSpillIntrinsic() &&
inst->getSrc(1)->getBase()->asRegVar()->isRegVarCoalesced()) {
auto topdcl = inst->getSrc(1)->getTopDcl();
sendDstDcl.insert(topdcl);
}
}
}
}
void CoalesceSpillFills::spills() {
populateSendDstDcl();
// Iterate over all BBs, find fills that are closeby and coalesce
// a bunch of them. Insert movs as required.
for (auto bb : kernel.fg) {
if (!isSpillCleanupEnabled(bb))
continue;
if (!gra.hasSpillCodeInBB(bb))
continue;
curBB = bb;
auto endIter = bb->end();
std::list<INST_LIST_ITER> spillsToCoalesce;
INST_LIST_ITER startIter = bb->begin();
unsigned int w = 0;
unsigned int maxW = 0;
unsigned int instRP = 0;
unsigned int numRowsSpillsToCoalesce = 0;
const auto &splitInsts = LoopVarSplit::getSplitInsts(&gra, bb);
for (auto instIter = startIter; instIter != endIter;) {
auto inst = (*instIter);
if (inst->isPseudoKill() || inst->isLabel()) {
instIter++;
continue;
}
if (splitInsts.find(inst) != splitInsts.end()) {
instIter++;
continue;
}
// Determine window size based on register pressure
unsigned int RP = rpe.getRegisterPressure(inst);
if (RP > 0)
instRP = RP;
bool earlyCoalesce = false;
if (inst->isSpillIntrinsic()) {
// Check if coalescing is possible
if (spillsToCoalesce.size() == 0) {
w = 0;
maxW = 0;
numRowsSpillsToCoalesce = 0;
startIter = instIter;
spillsToCoalesce.clear();
}
if (isGRFAssigned(inst->asSpillIntrinsic()->getPayload())) {
if (spillsToCoalesce.size() == 0) {
++instIter;
continue;
}
// if spill intrinsic payload has physical GRF assigned then treat it
// as fence
earlyCoalesce = true;
} else {
for (auto coalIt = spillsToCoalesce.begin();
coalIt != spillsToCoalesce.end();) {
bool fullOverlap = false;
if (overlap(*instIter, *(*coalIt), fullOverlap)) {
if (fullOverlap) {
gra.incRA.markForIntfUpdate((*(*coalIt))
->asSpillIntrinsic()
->getPayload()
->getTopDcl());
// Delete earlier spill since its made redundant
// by current spill.
bb->erase(*coalIt);
}
if (inst->asSpillIntrinsic()->isScatterSpill()) {
// If current spill is scatter and overlaps with other spills,
// break the current coalescing chain
earlyCoalesce = true;
break;
}
coalIt = spillsToCoalesce.erase(coalIt);
continue;
}
coalIt++;
}
if (!earlyCoalesce) {
spillsToCoalesce.push_back(instIter);
numRowsSpillsToCoalesce += inst->asSpillIntrinsic()->getNumRows();
}
}
} else if (inst->isFillIntrinsic()) {
for (auto coalIt = spillsToCoalesce.begin();
coalIt != spillsToCoalesce.end();) {
bool temp = false;
if (overlap(*instIter, *(*coalIt), temp)) {
#if 1
// Instead of deleting scratch writes try coalescing
// at this point. This way maybe the fill can also
// be cleaned up in later phase.
earlyCoalesce = true;
break;
#else
coalIt = spillsToCoalesce.erase(coalIt);
continue;
#endif
}
coalIt++;
}
}
if (spillsToCoalesce.size() > 0 && instRP > spillWindowSizeThreshold) {
if (!allSpillsSameVar(spillsToCoalesce)) {
// High register pressure region so reduce window size to 3
w = (cWindowSize - w > 3) ? cWindowSize - 3 : w;
}
}
if (w == cWindowSize &&
(instRP + numRowsSpillsToCoalesce) < highRegPressureForWindow) {
// If register pressure is below the threshold even when considering
// potential increase, continue coalescing spills in current BB
--w;
}
if (w == cWindowSize || maxW == cMaxWindowSize || inst == bb->back() ||
earlyCoalesce) {
if (spillsToCoalesce.size() > 1) {
auto nextInst =
analyzeSpillCoalescing(spillsToCoalesce, startIter, instIter);
if (earlyCoalesce)
instIter++;
else
instIter = nextInst;
} else if (w == cWindowSize) {
startIter = instIter;
} else if (inst == bb->back()) {
break;
}
w = 0;
maxW = 0;
numRowsSpillsToCoalesce = 0;
spillsToCoalesce.clear();
continue;
}
if (spillsToCoalesce.size() > 0) {
w++;
maxW++;
}
instIter++;
}
// One pass to replace old fills with coalesced dcl
for (auto instIt = bb->begin(); instIt != bb->end();) {
auto inst = (*instIt);
if (inst->isPseudoKill() &&
replaceMap.find(inst->getDst()->getTopDcl()) != replaceMap.end()) {
instIt = bb->erase(instIt);
continue;
}
while (replaceCoalescedOperands(inst)) {
// replaceCoalescedOperands() updates references to old
// dcls in IR that are coalesced. Having a single pass to
// replace operands is insufficient if a coalesced range
// gets re-coalesced. This can happen rarely when window
// heuristic hits boundary condition.
};
instIt++;
}
}
}
void CoalesceSpillFills::fixSendsSrcOverlap() {
// Overlap for sends src operands is not allowed.
//
// Fix for following code pattern after spill/fill coalescing:
// clang-format off
// send (16) COAL_FILL_373(0,0)<1>:ud r0 0xa 0x24c2001:ud{Align1, NoMask} // scratch read, resLen=4, msgLen=1
// sends(1) null:ud COAL_FILL_373(0,0) COAL_FILL_373(1,0) 0x4c:ud 0x40680ff:ud{ Align1,Q1, NoMask } // a64 scattered write, resLen=0, msgLen=2, extMsgLen=1
//
// svm_scatter.1.1 (M1_NM, 1) V441.0 V449.0
// clang-format on
//
// where V441 and V449 are both scalars of type :uq and :ud respectively
//
for (auto bb : kernel.fg) {
curBB = bb;
for (auto instIt = bb->begin(); instIt != bb->end(); instIt++) {
auto inst = (*instIt);
if (!inst->isSplitSend()) {
continue;
}
auto src0 = inst->getSrc(0);
auto src1 = inst->getSrc(1);
if (src0->getTopDcl() == src1->getTopDcl()) {
auto lb0 = src0->getLeftBound();
auto rb0 = src0->getRightBound();
auto lb1 = src1->getLeftBound();
auto rb1 = src1->getRightBound();
if ((lb0 < lb1 && rb0 > lb1) || (lb1 < lb0 && rb1 > lb0)) {
// Ideally we should create copy of
// operand with less number of GRFs,
// but this is a really corner case
// and probably shows up only for
// force spills. So we simply choose
// src1 of sends.
const char *dclName = kernel.fg.builder->getNameString(
32, "COPY_%zu", kernel.Declares.size());
G4_Declare *copyDcl = kernel.fg.builder->createDeclare(
dclName, G4_GRF, kernel.numEltPerGRF<Type_UD>(),
src1->getTopDcl()->getNumRows(), Type_UD);
gra.incRA.markForIntfUpdate(src1->getTopDcl());
unsigned int elems = copyDcl->getNumElems();
short row = 0;
while (elems > 0) {
G4_SrcRegRegion *srcRgn = kernel.fg.builder->createSrc(
src1->getTopDcl()->getRegVar(), row, 0,
kernel.fg.builder->getRegionStride1(), Type_UD);
G4_DstRegRegion *dstRgn = kernel.fg.builder->createDst(
copyDcl->getRegVar(), row, 0, 1, Type_UD);
G4_INST *copyInst = kernel.fg.builder->createMov(
g4::SIMD8, dstRgn, srcRgn, InstOpt_WriteEnable, false);
bb->insertBefore(instIt, copyInst);
if (gra.EUFusionNoMaskWANeeded()) {
gra.addEUFusionNoMaskWAInst(bb, copyInst);
}
elems -= 8;
row++;
}
G4_SrcRegRegion *sendSrc1 = kernel.fg.builder->createSrc(
copyDcl->getRegVar(), 0, 0, kernel.fg.builder->getRegionStride1(),
Type_UD);
inst->setSrc(sendSrc1, 1);
}
}
}
}
}
void CoalesceSpillFills::removeRedundantSplitMovs() {
// send (8) V_SAMPLER -- resLen = 3
// COAL_0(0,0) = V_SAMPLE(0,0)
// COAL_0(1,0) = V_SAMPLE(1,0)
// send (8) <null> COAL_0(0,0) <-- len = 2
// TV0(0,0) = V_SAMPLE(2,0)
// ===>
// send (8) V_SAMPLER -- resLen = 3
// send (8) <null> V_SAMPLE(0,0) <-- len = 2
// TV0(0,0) = V_SAMPLE(2,0)
// Look for scratch writes. Src1 is data to write to memory.
// Iterate in bottom-order to check whether raw movs exist
// that define src1 of scratch write and whether their
// source operands are consecutive.
// Store numUses for dcls replaced and location of defs.
// This structure is used to eliminate redundant movs
// later.
typedef std::pair<G4_BB *, INST_LIST_ITER> MovLoc;
typedef unsigned int NumRefs;
std::map<G4_Declare *, std::pair<NumRefs, std::list<MovLoc>>> movs;
for (auto bb : kernel.fg) {
// Store all dcls defined by non scratch sends
// as only they are candidates for this pass.
// Without this, we might end up identifying
// other raw movs coming from partial write like:
// clang-format off
// add (8) r8.0<1>:q r20.0<4;4,1>:q r4.0<0;1,0>:ud {Align1, Q1}
// send (16) r27.0<1>:uw r26 0xa 0x22c1000 : ud{ Align1, NoMask } // scratch read, fill, offset = 0, resLen=2, msgLen=1
// mov (8) r27.0<1> q r8.0<4;4,1>:q{ Align1, Q1 }
// sends (16) null:uw r26 r27 0x8a:ud 0x20f1000:ud{ Align1, NoMask } // scratch write, spill, offset = 0, resLen=0, msgLen=1, extMsgLen=2
// clang-format on
//
// Although there is a raw mov before scratch write,
// it has to be preserved for correctness.
std::set<G4_Declare *> sendDst;
for (auto inst : *bb) {
if (inst->isSend() && inst->getDst() && !inst->getDst()->isNullReg() &&
!inst->getMsgDesc()->isScratchRead() &&
!inst->getMsgDesc()->isScratchWrite()) {
sendDst.insert(inst->getDst()->getTopDcl());
}
}
for (auto instIt = bb->begin(), endIt = bb->end(); instIt != endIt;
instIt++) {
auto inst = (*instIt);
if (inst->isSpillIntrinsic()) {
// Spill sends
auto src1Dcl = inst->getSrc(1)->getTopDcl();
unsigned int lb = inst->getSrc(1)->getLeftBound();
unsigned int rb = inst->getSrc(1)->getRightBound();
std::set<unsigned int> rows;
for (unsigned int k = lb / kernel.numEltPerGRF<Type_UB>();
k != (rb + kernel.numEltPerGRF<Type_UB>() - 1) /
kernel.numEltPerGRF<Type_UB>();
k++) {
rows.insert(k);
}
auto tmpIt = instIt;
tmpIt--;
G4_Declare *srcDcl = nullptr;
std::map<unsigned int, unsigned int> dstSrcRowMapping;
std::list<MovLoc> copies;
while (tmpIt != bb->begin()) {
auto pInst = (*tmpIt);
// Each copy should be a raw mov
if (!pInst->isRawMov())
break;
// Ensure src0 topdcl comes from a send dst in this BB
if (sendDst.find(pInst->getSrc(0)->getTopDcl()) == sendDst.end())
break;
// Check whether dcls match
auto pDstDcl = pInst->getDst()->getTopDcl();
if (pDstDcl != src1Dcl)
break;
// Don't remove inst with pre-assigned GRF
if (pDstDcl->getRegVar()->getPhyReg())
break;
unsigned int plb = pInst->getDst()->getLeftBound();
unsigned int prb = pInst->getDst()->getRightBound();
// Check whether complete row(s) defined
if ((prb - plb + 1) % kernel.numEltPerGRF<Type_UB>() != 0)
break;
unsigned int rowStart = plb / kernel.numEltPerGRF<Type_UB>();
unsigned int numRows =
(prb - plb + 1) / kernel.numEltPerGRF<Type_UB>();
bool punt = false;
for (unsigned int k = rowStart; k != (rowStart + numRows); k++) {
if (rows.find(k) == rows.end()) {
punt = true;
break;
}
dstSrcRowMapping.insert(std::make_pair(k, INT_MAX));
}
if (punt)
break;
auto pSrc0 = pInst->getSrc(0);
if (!pSrc0->isSrcRegRegion())
break;
auto pSrcDcl = pSrc0->getTopDcl();
if (!srcDcl)
srcDcl = pSrcDcl;
else if (srcDcl != pSrcDcl)
break;
// mov src should be GRF aligned
if (pSrc0->getLeftBound() % kernel.numEltPerGRF<Type_UB>() != 0)
break;
unsigned int src0lb = pSrc0->getLeftBound();
unsigned int src0rb = pSrc0->getRightBound();
// (rb - lb) should match dst (rb - lb)
if ((src0rb - src0lb) != (prb - plb))
break;
unsigned int pStartRow =
pSrc0->getLeftBound() / kernel.numEltPerGRF<Type_UB>();
for (unsigned int k = rowStart; k != (rowStart + numRows); k++) {
auto it = dstSrcRowMapping.find(k);
if (it == dstSrcRowMapping.end()) {
punt = true;
break;
}
it->second = pStartRow + (k - rowStart);
}
if (punt)
break;
copies.push_back(std::make_pair(bb, tmpIt));
tmpIt--;
}
if (dstSrcRowMapping.size() > 0) {
// Now check whether each entry of src1 has a corresponding src offset
unsigned int dstRowStart = lb / kernel.numEltPerGRF<Type_UB>();
bool success = true;
auto baseIt = dstSrcRowMapping.find(dstRowStart);
if (baseIt != dstSrcRowMapping.end()) {
auto base = dstSrcRowMapping.find(dstRowStart)->second;
for (const auto &m : dstSrcRowMapping) {
unsigned int curRow = m.first - dstRowStart;
if (m.second == INT_MAX) {
success = false;
break;
}
if (m.second != (base + curRow)) {
success = false;
break;
}
}
if (success && srcDcl) {
gra.incRA.markForIntfUpdate(inst->getSrc(1)->getTopDcl());
gra.incRA.markForIntfUpdate(srcDcl);
// Replace src1 of send with srcDcl
G4_SrcRegRegion *sendSrc1 = kernel.fg.builder->createSrc(
srcDcl->getRegVar(), base, 0,
kernel.fg.builder->getRegionStride1(),
inst->getSrc(1)->getType());
inst->setSrc(sendSrc1, 1);
for (const auto &c : copies) {
auto defDcl = (*c.second)->getDst()->getTopDcl();
auto it = movs.find(defDcl);
if (it == movs.end()) {
std::list<MovLoc> t;
t.push_back(c);
movs.insert(std::make_pair(defDcl, std::make_pair(0, t)));
} else {
it->second.second.push_back(c);
}
}
}
}
}
}
}
}
// Update number of uses of each dcl
for (auto bb : kernel.fg) {
for (auto instIt = bb->begin(), endIt = bb->end(); instIt != endIt;
instIt++) {
auto inst = (*instIt);
if (inst->isPseudoKill()) {
auto dcl = inst->getDst()->getTopDcl();
auto it = movs.find(dcl);
if (it != movs.end()) {
it->second.second.push_back(std::make_pair(bb, instIt));
}
}
for (unsigned int i = 0, numSrc = inst->getNumSrc(); i < numSrc; i++) {
G4_Operand *opnd = inst->getSrc(i);
if (opnd && opnd->getTopDcl()) {
auto it = movs.find(opnd->getTopDcl());
if (it != movs.end())
it->second.first++;
}
}
}
}
for (const auto &mov : movs) {
auto dcl = mov.first;
auto numRefs = mov.second.first;
auto &allMovs = mov.second.second;
if (numRefs == 0 && !dcl->getAddressed()) {
for (const auto &m : allMovs) {
auto bb = m.first;
if (!isSpillCleanupEnabled(bb))
continue;
auto iter = m.second;
bb->erase(iter);
}
}
}
}
void CoalesceSpillFills::spillFillCleanup() {
// Eliminate redundant fills when a write
// is close by:
//
// spill TV1 at offset = 1
// ..
// ..
// ..
// fill FP1 from offset = 1
// = FP1
// ===>
// Remove fill and replace occurence of FP1 with TV1
//
std::map<unsigned int, G4_INST *> writesPerOffset;
std::set<G4_Declare *> defs;
for (auto bb : kernel.fg) {
if (!isSpillCleanupEnabled(bb))
continue;
if (!gra.hasSpillCodeInBB(bb))
continue;
curBB = bb;
auto startIt = bb->begin();
auto endIt = bb->end();
const auto &splitInsts = LoopVarSplit::getSplitInsts(&gra, bb);
unsigned int regPressure = 0;
bool scatterSpillCleanup = false;
for (auto instIt = startIt; instIt != endIt; instIt++) {
auto inst = (*instIt);
// register pressue estimate is computed per instruction before liveness
// analysis.
auto RP = rpe.getRegisterPressure(inst);
// spill code is inserted after coloring is complete. so newly generated
// spill instructions would not have valid register pressure estimate.
// in case current instruction doesnt have valid register pressure
// estimate, use a valid one from an earlier instruction.
regPressure = (RP > 0) ? RP : regPressure;
if (splitInsts.find(inst) != splitInsts.end())
continue;
if (inst->isFillIntrinsic()) {
writesPerOffset.clear();
defs.clear();
scatterSpillCleanup = false;
// Store offset, spill inst pair
unsigned int rowStart, numRows;
getScratchMsgInfo(inst, rowStart, numRows);
unsigned int lastRow = rowStart + numRows - 1;
// Scan window of instruction above current inst
// to check whether all rows read by current inst
// have been written.
auto pInstIt = instIt;
pInstIt--;
unsigned int w = spillFillCleanupWindowSize;
unsigned int maxW = 0;
auto dstSize =
inst->asFillIntrinsic()->getDst()->getTopDcl()->getNumRows();
if (dstSize >= 8 || (totalInputSize > inputSizeLimit && dstSize >= 4)) {
// avoid attempting cleanup for high reg pressure
if (regPressure > highRegPressureForCleanup) {
continue;
}
}
if (isGRFAssigned(inst->getDst())) {
// fill has GRF assigned so dont eliminate it
continue;
}
while (pInstIt != startIt && w > 0 && maxW < cMaxWindowSize) {
auto pInst = (*pInstIt);
// If pInst instruction is created by split on spill pass
// then skip cleanup. Because if this variable spills
// in later RA iteration then the spill will be erased from
// the BB rendering this cleanup optimization incorrect. For eg,
// consider that split on spill optimization promoted a spilled
// variable to a register when executing loop L1. This means
// we'll insert a load in pre-header and a spill in loop exit (in
// case the variable was updated in the loop). Here's an example
// where LOOPSPLIT1 and LOOPSPLIT2 are created to promote 2
// spilled variables in L1. Following shows loop exit BB:
//
// L1_Loop_Exit_BB:
// Store LOOPSPLIT1 @ Scratch Offset 0x100 <-- split on spill temp
// Store LOOPSPLIT2 @ Scratch Offset 0x200 <-- split on spill temp
// ...
// Load TMP @ Scratch Offset 0x100 <-- regular fill
// Op Dst, TMP
//
// Under normal circumstances, we can propagate LOOPSPLIT1
// to replace Load TMP with a register mov. But when LOOPSPLIT1
// is a spill due to split on spill, we cannot do so. Because a
// later RA iteration could decide to spill LOOPSPLIT1. When that
// happens, we erase LOOPSPLIT1 from program and replace its use in
// the loop with original spilled variable. Therefore, there's no
// need to load anything in pre-header or store anything in loop exit
// BB.
if (splitInsts.find(pInst) != splitInsts.end())
break;
if (pInst->isSpillIntrinsic()) {
unsigned int pRowStart, pNumRows;
getScratchMsgInfo(pInst, pRowStart, pNumRows);
// If any def of src1 dcl is found then don't
// consider this write for optimization. Its
// value in memory could be different than
// one held in variable.
auto pSrc1Dcl = pInst->getSrc(1)->getTopDcl();
if (defs.find(pSrc1Dcl) != defs.end()) {
// When a WAR is detected, punt out because
// any older spill is going to be stale.
// An optimization here is to detect variable
// row on which WAR occurs. If the row is
// different from current fill, then it
// should be safe to ignore pInst and continue
// till end of window.
pInstIt--;
break;
}
bool fullOverlap = false;
if (overlap(pInst, inst, fullOverlap)) {
if (pInst->asSpillIntrinsic()->isScatterSpill()) {
// When a scatter spill is found and it overlaps the fill
// being optimized, there are paths:
// 1. If there are no other block spills in between, it's safe
// to optimize the current scatter spill and potentially
// others
// 2. If there are block spills in between the cleanup search
// stops.
if (writesPerOffset.empty()) {
scatterSpillCleanup = true;
} else if (!scatterSpillCleanup) {
break;
}
} else {
// If block spill is found but there already scatter spills in
// between, the cleanup search stops
if (scatterSpillCleanup)
break;
}
for (unsigned int pRow = pRowStart;
pRow != (pRowStart + pNumRows); pRow++) {
auto writeIt = writesPerOffset.find(pRow);
// Check whether a more recent write was found for this row
if (writeIt != writesPerOffset.end())
continue;
writesPerOffset.insert(std::make_pair(pRow, pInst));
}
}
}
if (pInst->getDst() && pInst->getDst()->getTopDcl()) {
// Store any defs seen to handle WAR
defs.insert(pInst->getDst()->getTopDcl());
}
w--;
maxW++;
pInstIt--;
RP = rpe.getRegisterPressure(pInst);
if (RP > 0)
regPressure = RP;
if (w == 0 && (regPressure + numRows) < highRegPressureForWindow) {
// Continue cleanup conservatively considering potential increase in
// register pressure
++w;
}
}
// Check whether writes for all rows were found
bool found = true;
for (auto row = rowStart; row <= lastRow; row++) {
auto spillIt = writesPerOffset.find(row);
if (spillIt == writesPerOffset.end() ||
isIncompatibleEMCm((*spillIt).second, inst)) {
found = false;
break;
}
}
if (!found) {
continue;
}
// Writes for all rows found
G4_ExecSize execSize = kernel.getSimdSize() > g4::SIMD16
? g4::SIMD16
: kernel.getSimdSize();
for (auto row = rowStart; row <= lastRow;) {
if (execSize == g4::SIMD16 && row == lastRow) {
// In case of odd rows in SIMD16
execSize = g4::SIMD8;
} else if (execSize == g4::SIMD16) {
// In SIMD16 kernel 2 consecutive rows should come from same spill
if (writesPerOffset.find(row)->second !=
writesPerOffset.find(row + 1)->second) {
execSize = g4::SIMD8;
}
}
auto type = Type_UD;
// In spill cleanup, all units are in hword units independent of
// platform. For PVC, GRF size is 2x of Gen9. Correction to PVC is
// postponed to code generation time when translating spill/fill
// intrinsics to actual send. Since we're emitting a mov here, we need
// to do this correction here.
if (kernel.getGRFSize() == 64)
type = Type_UQ;
// Insert SIMD8 mov per row
G4_DstRegRegion *nDst = kernel.fg.builder->createDst(
inst->getDst()->getBase(),
row + inst->getDst()->asDstRegRegion()->getRegOff() - rowStart, 0,
1, type);
auto write = writesPerOffset.find(row)->second;
G4_SrcRegRegion *src1Write = write->getSrc(1)->asSrcRegRegion();
gra.incRA.markForIntfUpdate(src1Write->getTopDcl());
unsigned int writeRowStart = write->asSpillIntrinsic()->getOffset();
unsigned int diff = row - writeRowStart;
G4_SrcRegRegion *nSrc = kernel.fg.builder->createSrc(
src1Write->getBase(), diff + src1Write->getRegOff(), 0,
kernel.fg.builder->getRegionStride1(), type);
G4_INST *mov = kernel.fg.builder->createMov(
execSize, nDst, nSrc, InstOpt_WriteEnable, false);
gra.incRA.markForIntfUpdate(nDst->getTopDcl());
gra.incRA.markForIntfUpdate(nSrc->getTopDcl());
bb->insertBefore(instIt, mov);
if (gra.EUFusionNoMaskWANeeded()) {
gra.addEUFusionNoMaskWAInst(bb, mov);
}
row += execSize / 8;
}
auto tempIt = instIt;
tempIt--;
bb->erase(instIt);
instIt = tempIt;
}
}
}
}
bool CoalesceSpillFills::isIncompatibleEMCm(G4_INST *inst1,
G4_INST *inst2) const {
vISA_ASSERT(curBB, "expecting valid G4_BB* containing inst1, inst2");
vISA_ASSERT(std::find(curBB->begin(), curBB->end(), inst1) != curBB->end(),
"expecting inst1 in bb");
vISA_ASSERT(std::find(curBB->begin(), curBB->end(), inst2) != curBB->end(),
"expecting inst2 in bb");
if (curBB->isDivergent() && isCm) {
// Cm program may write a variable using NoMask once and then
// write it again with default EM. Later use of the variable could
// use NoMask. For eg,
// op1 (16) V10, ... {NoMask}
// op2 (16) V10, ... {H1}
// op3 (16) ..., V10 {NoMask}
//
// If V10 is spilled in above snippet, we'll emit 2 spill operations
// and 1 fill operation. Spill for op2 doesn't use NoMask as send msg
// can directly rely on EM behavior. Since fill uses NoMask, spill
// cleanup cannot coalesce the second spill and fill away. So for Cm,
// we disallow spill cleanup between a NoMask fill and default EM
// spill in divergent BBs.
return (inst1->isWriteEnableInst() ^ inst2->isWriteEnableInst());
}
return false;
}
void CoalesceSpillFills::removeRedundantWrites() {
typedef std::list<std::pair<G4_BB *, INST_LIST_ITER>> SPILLS;
typedef std::list<std::pair<G4_BB *, INST_LIST_ITER>> FILLS;
std::map<unsigned int, std::pair<SPILLS, FILLS>> scratchOffsetAccess;
// Traverse bottom-up to detect and remove redundant writes.
// Redundant writes include:
// 1. Successive writes to same offset without a fill in between,
// 2. Writes in program without any fill from that slot throughout
for (auto bb : kernel.fg) {
curBB = bb;
auto endIt = bb->end();
endIt--;
// Store spill slots that are written in to alongwith emask used
std::map<unsigned int, unsigned int> scratchOffToMask;
for (auto instIt = endIt; instIt != bb->begin(); instIt--) {
auto inst = (*instIt);
if (inst->isSpillIntrinsic() || inst->isFillIntrinsic()) {
unsigned int offset = 0, size = 0;
if (inst->isFillIntrinsic()) {
getScratchMsgInfo(inst, offset, size);
for (unsigned int k = offset; k != (offset + size); k++) {
auto it = scratchOffToMask.find(k);
if (it != scratchOffToMask.end()) {
scratchOffToMask.erase(it);
}
}
} else if (inst->isSpillIntrinsic()) {
getScratchMsgInfo(inst, offset, size);
bool allRowsFound = true;
unsigned int emask = inst->getMaskOption();
for (unsigned int k = offset; k != (offset + size); k++) {
auto it = scratchOffToMask.find(k);
if (it != scratchOffToMask.end()) {
if (emask != it->second &&
(it->second & InstOpt_WriteEnable) == 0) {
allRowsFound = false;
break;
}
} else {
allRowsFound = false;
break;
}
}
if (allRowsFound) {
if (!isGRFAssigned(inst->getDst()))
instIt = bb->erase(instIt);
} else {
unsigned int emask = inst->getOption();
for (unsigned int k = offset; k != (offset + size); k++) {
scratchOffToMask.insert(std::make_pair(k, emask));
}
}
}
}
}
}
for (auto bb : kernel.fg) {
auto endIt = bb->end();
for (auto instIt = bb->begin(); instIt != endIt; instIt++) {
auto inst = (*instIt);
if (inst->isFillIntrinsic() || inst->isSpillIntrinsic()) {
unsigned int offset, size;
getScratchMsgInfo(inst, offset, size);
bool isRead = inst->isFillIntrinsic();
for (unsigned int i = offset; i != (offset + size); i++) {
auto it = scratchOffsetAccess.find(i);
if (it != scratchOffsetAccess.end()) {
if (isRead) {
auto &fill = it->second.second;
fill.push_back(std::make_pair(bb, instIt));
} else {
auto &spill = it->second.first;
spill.push_back(std::make_pair(bb, instIt));
}
} else {
SPILLS s;
FILLS f;
if (isRead)
f.push_back(std::make_pair(bb, instIt));
else
s.push_back(std::make_pair(bb, instIt));
scratchOffsetAccess.insert(std::make_pair(i, std::make_pair(s, f)));
}
}
}
}
}
std::map<G4_INST *, std::pair<INST_LIST_ITER, G4_BB *>> spillToRemove;
for (const auto &scratchAccess : scratchOffsetAccess) {
if (scratchAccess.second.second.size() == 0 &&
scratchAccess.second.first.size() > 0) {
// 0 fills for scratch slot
// Check whether all spill slots have 0 fills
// in case spills are coalesced.
for (const auto &spill : scratchAccess.second.first) {
bool spillRequired = false;
unsigned int offset, size;
getScratchMsgInfo(*spill.second, offset, size);
// Verify that all slots from offset->(offset+size) have 0 fills
for (unsigned int slot = offset; slot != (offset + size); slot++) {
auto it = scratchOffsetAccess.find(slot);
if (it->second.second.size() != 0) {
spillRequired = true;
break;
}
}
if (!spillRequired) {
spillToRemove.insert(std::make_pair(
*spill.second, std::make_pair(spill.second, spill.first)));
}
}
} else if (scratchAccess.second.first.size() == 0 &&
scratchAccess.second.second.size() > 0) {
// 0 spills for scratch slot, non-zero fills
// Check whether all fill slots have 0 spills
// in case fills are coalesced.
for (const auto &fill : scratchAccess.second.second) {
if (*fill.second == gra.getRestoreBE_FPInst())
continue;
bool fillRequired = false;
unsigned int offset, size;
getScratchMsgInfo(*fill.second, offset, size);
// Verify that all slots from offset->(offset+size) have 0 spills
for (unsigned int slot = offset; slot != (offset + size); slot++) {
auto it = scratchOffsetAccess.find(slot);
if (it->second.first.size() != 0) {
fillRequired = true;
break;
}
}
if (!fillRequired) {
spillToRemove.insert(std::make_pair(
*fill.second, std::make_pair(fill.second, fill.first)));
}
}
}
}
for (const auto &removeSp : spillToRemove) {
G4_BB *bb = removeSp.second.second;
if (!isSpillCleanupEnabled(bb))
continue;
auto *inst = *removeSp.second.first;
if ((inst->isSpillIntrinsic() &&
!isGRFAssigned(inst->asSpillIntrinsic()->getPayload())) ||
(inst->isFillIntrinsic() &&
!isGRFAssigned(inst->asFillIntrinsic()->getDst()))) {
if (inst->isSpillIntrinsic())
gra.incRA.markForIntfUpdate(
inst->asSpillIntrinsic()->getPayload()->getTopDcl());
else if (inst->isFillIntrinsic())
gra.incRA.markForIntfUpdate(
inst->asFillIntrinsic()->getDst()->getTopDcl());
bb->erase(removeSp.second.first);
}
}
}
void CoalesceSpillFills::run() {
removeRedundantSplitMovs();
fills();
replaceMap.clear();
spills();
replaceMap.clear();
spillFillCleanup();
removeRedundantWrites();
fixSendsSrcOverlap();
kernel.dumpToFile("after.spillCleanup." + std::to_string(iterNo));
}
void CoalesceSpillFills::dumpKernel() {
for (auto bb : kernel.fg) {
for (auto inst : *bb) {
inst->emit(std::cerr);
std::cerr << "\t$" << inst->getVISAId() << ", #"
<< rpe.getRegisterPressure(inst) << "\n";
}
}
}
void CoalesceSpillFills::dumpKernel(unsigned int v1, unsigned int v2) {
bool start = false, end = false, canEnd = false;
for (auto bb : kernel.fg) {
if (end)
break;
for (auto inst : *bb) {
if (canEnd && inst->getVISAId() > (int)v2) {
end = true;
break;
}
if (inst->getVISAId() == v2) {
// This ensures invalid offsets
// are dumped till v2 is hit.
canEnd = true;
}
if (inst->getVISAId() == v1)
start = true;
if (start && !end) {
inst->dump();
printf(" // $%d, #%d\n", inst->getVISAId(),
rpe.getRegisterPressure(inst));
}
}
}
}
} // namespace vISA
|