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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "BinaryEncodingIGA.h"
#include "BuildIR.h"
#include "Common_ISA_framework.h"
#include "Timer.h"
#include "iga/IGALibrary/Frontend/FormatterJSON.hpp"
#include "iga/IGALibrary/api/igaEncoderWrapper.hpp"
#include <fstream>
#include <map>
#include <utility>
using namespace iga;
using namespace vISA;
// all the gunk related to extended send descriptors
struct SendExDescOpts {
SendDesc exDesc;
uint32_t exDescImmOff = 0;
int xlen = -1;
InstOptSet extraOpts;
};
class BinaryEncodingIGA {
G4_Kernel &kernel;
std::string fileName; // .dat filename
Kernel *IGAKernel = nullptr;
const Model *platformModel;
const TARGET_PLATFORM platform;
public:
BinaryEncodingIGA(vISA::G4_Kernel &k, const std::string& fname);
~BinaryEncodingIGA() { delete IGAKernel; }
void SetSWSB(G4_INST *inst, SWSB &sw);
// translates and encodes (formerly "DoAll")
void Encode();
void EmitJSON(int dumpJSON);
///////////////////////////////////////////////////////////////////////////
// these function translate G4 IR to IGA IR
Instruction *translateInstruction(G4_INST *g4inst, Block *&bbNew);
void translateInstructionDst(G4_INST *g4inst, Instruction *igaInst);
void translateInstructionBranchSrcs(G4_INST *g4inst, Instruction *igaInst,
Block *&bbNew);
void translateInstructionSrcs(G4_INST *g4inst, Instruction *igaInst);
void FixInst();
void *EmitBinary(size_t &binarySize);
private:
BinaryEncodingIGA(const BinaryEncodingIGA &other);
BinaryEncodingIGA &operator=(const BinaryEncodingIGA &other);
std::map<G4_Label *, Block *> labelToBlockMap;
public:
static ExecSize getIGAExecSize(int execSize);
static ChannelOffset getIGAChannelOffset(int offset);
static MaskCtrl getIGAMaskCtrl(bool noMask);
static RegName getIGAARFName(G4_ArchRegKind areg);
static Type getIGAType(G4_Type type, TARGET_PLATFORM genxPlatform);
/// getIGAInternalPlatform - a helper function to transform visa platform to
/// iga platform
static Platform getIGAInternalPlatform(TARGET_PLATFORM genxPlatform);
static std::pair<const OpSpec *, Subfunction>
getIgaOpInfo(const G4_INST *inst, const Model *m, bool allowUnknownOp,
const IR_Builder &builder);
private:
static PredCtrl getIGAPredCtrl(G4_Predicate_Control g4PredCntrl);
static Predication getIGAPredication(const G4_Predicate *predG4,
G4_ExecSize execSize,
const IR_Builder &builder);
static BranchCntrl getIGABranchCntrl(bool isOn) {
return isOn ? BranchCntrl::ON : BranchCntrl::OFF;
}
static DstModifier getIGADstModifier(bool sat) {
return sat ? DstModifier::SAT : DstModifier::NONE;
}
static SrcModifier getIGASrcModifier(G4_SrcModifier srcMod);
static Region::Vert getIGAVert(int vstride);
static Region::Width getIGAWidth(int width);
static Region::Horz getIGAHorz(int hstride);
static Region getIGARegion(G4_SrcRegRegion *srcRegion, int srcPos);
SWSB_ENCODE_MODE getIGASWSBEncodeMode() const;
MathMacroExt getIGAImplAcc(G4_AccRegSel accSel) const {
switch (accSel) {
case ACC2:
return MathMacroExt::MME0;
case ACC3:
return MathMacroExt::MME1;
case ACC4:
return MathMacroExt::MME2;
case ACC5:
return MathMacroExt::MME3;
case ACC6:
return MathMacroExt::MME4;
case ACC7:
return MathMacroExt::MME5;
case ACC8:
return MathMacroExt::MME6;
case ACC9:
return MathMacroExt::MME7;
case NOACC:
return MathMacroExt::NOMME;
default:
vISA_ASSERT_UNREACHABLE("illegal acc (mme) channel select");
return MathMacroExt::INVALID;
}
}
ImmVal::Kind getIGAImmType(G4_Type type) {
switch (type) {
case Type_UW:
return ImmVal::Kind::U16;
case Type_W:
return ImmVal::Kind::S16;
case Type_UD:
return ImmVal::Kind::U32;
case Type_D:
return ImmVal::Kind::S32;
case Type_UQ:
return ImmVal::Kind::U64;
case Type_Q:
return ImmVal::Kind::S64;
case Type_HF:
return ImmVal::Kind::F16;
case Type_F:
return ImmVal::Kind::F32;
case Type_DF:
return ImmVal::Kind::F64;
case Type_UV:
case Type_V:
case Type_VF:
return ImmVal::Kind::U32;
default:
vISA_ASSERT_UNREACHABLE("invalid immediate type");
return ImmVal::Kind::UNDEF;
}
}
InstOptSet getIGAInstOptSet(G4_INST *inst) const;
SendDesc getIGASendDesc(G4_InstSend *sendInst) const;
SendExDescOpts getIGASendExDesc(G4_InstSend *sendInst) const;
void printSendDataToFile(uint32_t src0Len,
uint32_t src1Len,
uint32_t dstLen,
const char *filePath) const;
SendDesc encodeExDescImm(G4_INST *sendInst, SendExDescOpts &sdos) const;
SendDesc encodeExDescRegA0(G4_INST *sendInst, SendExDescOpts &sdos) const;
RegName getIGARegName(G4_Operand *opnd) const {
G4_VarBase *base = opnd->getBase();
vISA_ASSERT(base != nullptr, "base should not be null");
if (base->isRegVar()) {
G4_VarBase *phyReg = base->asRegVar()->getPhyReg();
return phyReg->isAreg()
? getIGAARFName(phyReg->asAreg()->getArchRegType())
: RegName::GRF_R;
} else {
return base->isAreg() ? getIGAARFName(base->asAreg()->getArchRegType())
: RegName::GRF_R;
}
}
RegRef getIGARegRef(G4_Operand *opnd) const {
RegRef regRef;
G4_VarBase *base = opnd->getBase();
vISA_ASSERT(base != nullptr, "base should not be null");
if (base->isGreg()) {
uint32_t byteAddress = opnd->getLinearizedStart();
regRef.regNum = byteAddress / kernel.numEltPerGRF<Type_UB>();
regRef.subRegNum =
(byteAddress % kernel.numEltPerGRF<Type_UB>()) / opnd->getTypeSize();
} else if (opnd->isSrcRegRegion()) {
bool valid, subvalid;
regRef.regNum = (uint8_t)opnd->asSrcRegRegion()->ExRegNum(valid);
regRef.subRegNum = (uint8_t)opnd->asSrcRegRegion()->ExSubRegNum(subvalid);
} else {
vISA_ASSERT(opnd->isDstRegRegion(), "expect DstRegRegion");
bool valid, subvalid;
regRef.regNum = (uint8_t)opnd->asDstRegRegion()->ExRegNum(valid);
regRef.subRegNum = (uint8_t)opnd->asDstRegRegion()->ExSubRegNum(subvalid);
}
return regRef;
}
Block *lookupIGABlock(G4_Label *label, Kernel &IGAKernel) {
Block *b = nullptr;
auto itr = labelToBlockMap.find(label);
if (itr == labelToBlockMap.end()) {
b = IGAKernel.createBlock();
labelToBlockMap[label] = b;
} else {
b = itr->second;
}
return b;
}
void getIGAFlagInfo(G4_INST *inst, const OpSpec *opSpec, Subfunction sf,
Predication &pred, FlagModifier &condMod,
RegRef &flagReg);
RegRef getIGAFlagReg(G4_VarBase *g4Base) const {
RegRef reg = REGREF_INVALID;
bool flagRegNumValid = true;
reg.regNum = (uint8_t)g4Base->ExRegNum(flagRegNumValid);
vISA_ASSERT(flagRegNumValid, "Unable to retrieve flag Reg Num for "
"predicate or conditional modifier.");
reg.subRegNum = (uint8_t)g4Base->asRegVar()->getPhyRegOff();
return reg;
}
FlagModifier getIGAFlagModifier(G4_CondMod *cMod) const {
if (cMod == nullptr) {
return FlagModifier::NONE;
}
G4_CondModifier mod = cMod->getMod();
switch (mod) {
case Mod_z: // fallthrough
case Mod_e:
return FlagModifier::EQ;
case Mod_nz: // fallthrough
case Mod_ne:
return FlagModifier::NE;
case Mod_g:
return FlagModifier::GT;
case Mod_ge:
return FlagModifier::GE;
case Mod_l:
return FlagModifier::LT;
case Mod_le:
return FlagModifier::LE;
case Mod_o: // fallthrough
case Mod_r:
return FlagModifier::OV;
case Mod_u:
return FlagModifier::UN;
default:
vISA_ASSERT_UNREACHABLE("Invalid FlagModifier.");
return FlagModifier::NONE;
}
}
// get IGA type from GenPrecision
Type getIGAPrecisionType(GenPrecision p) const {
switch (p) {
case GenPrecision::U1:
return Type::U1;
case GenPrecision::U2:
return Type::U2;
case GenPrecision::U4:
return Type::U4;
case GenPrecision::U8:
return Type::UB;
case GenPrecision::S2:
return Type::S2;
case GenPrecision::S4:
return Type::S4;
case GenPrecision::S8:
return Type::B;
case GenPrecision::FP16:
return Type::HF;
case GenPrecision::BF16:
return Type::BF;
case GenPrecision::TF32:
return Type::TF32;
default:
vISA_ASSERT_UNREACHABLE("illegal Operand Precision");
return Type::INVALID;
}
}
Type getIGADpasType(G4_InstDpas *DpasInst, int SrcOprdIx) const {
Type ty;
switch (SrcOprdIx) {
default:
vISA_ASSERT_UNREACHABLE("Invalid SrcOprdIx!");
ty = Type::INVALID;
break;
case 0:
{
G4_Operand *src0 = DpasInst->getSrc(0);
if (src0->isNullReg()) {
ty = getIGAType(DpasInst->getDst()->getType(), platform);
} else {
ty = getIGAType(src0->getType(), platform);
}
break;
}
case 1:
ty = getIGAPrecisionType(DpasInst->getSrc1Precision());
break;
case 2:
ty = getIGAPrecisionType(DpasInst->getSrc2Precision());
break;
}
return ty;
}
RegRef getIGADpasRegRef(G4_InstDpas *DpasInst, int SrcOprdIx) const {
G4_Operand *src = DpasInst->getSrc(SrcOprdIx);
RegRef regref = getIGARegRef(src);
if (SrcOprdIx == 2) {
// By default, subRegNum is in terms of operand's type (D/UD for
// dpas's src1/2). IGA needs it to be in terms of precision type.
// Note that no need to do it for src1 as it must be grf-aligned!
vISA_ASSERT((regref.subRegNum % 2) == 0,
"Minimum alignemnt of dpas's src2 must be QW");
uint32_t bitOffsets = regref.subRegNum * src->getTypeSize() * 8;
uint32_t PBits =
G4_InstDpas::GetPrecisionSizeInBits(DpasInst->getSrc2Precision());
regref.subRegNum = bitOffsets / PBits;
}
return regref;
}
static BfnFC getBfnFC(const G4_INST *inst) {
uint8_t funcCtrl = inst->asBfnInst()->getBooleanFuncCtrl();
return BfnFC(funcCtrl);
}
static iga::SFID getSFID(const G4_INST *inst);
static MathFC getMathFC(const G4_INST *inst);
Type getIGAType(const G4_INST *I, Gen4_Operand_Number O, TARGET_PLATFORM P);
void *m_kernelBuffer = nullptr;
uint32_t m_kernelBufferSize = 0;
}; // class BinaryEncodingIGA
Platform
BinaryEncodingIGA::getIGAInternalPlatform(TARGET_PLATFORM genxPlatform) {
Platform platform = Platform::INVALID;
switch (genxPlatform) {
case GENX_BDW:
platform = Platform::GEN8;
break;
case GENX_CHV:
platform = Platform::GEN8;
break;
case GENX_SKL:
case GENX_BXT:
platform = Platform::GEN9;
break;
case GENX_ICLLP:
platform = Platform::GEN11;
break;
case GENX_TGLLP:
platform = Platform::XE;
break;
case Xe_XeHPSDV:
platform = Platform::XE_HP;
break;
case Xe_DG2:
case Xe_MTL:
case Xe_ARL:
platform = Platform::XE_HPG;
break;
case Xe_PVC:
case Xe_PVCXT:
platform = Platform::XE_HPC;
break;
case Xe2:
platform = Platform::XE2;
break;
case Xe3:
platform = Platform::XE3;
break;
default:
break;
}
return platform;
}
BinaryEncodingIGA::BinaryEncodingIGA(vISA::G4_Kernel &k, const std::string& fname)
: kernel(k), fileName(fname), platform(k.fg.builder->getPlatform()) {
platformModel = Model::LookupModel(getIGAInternalPlatform(platform));
IGAKernel = new Kernel(*platformModel);
}
SWSB_ENCODE_MODE BinaryEncodingIGA::getIGASWSBEncodeMode() const {
if (platform == TARGET_PLATFORM::Xe_MTL ||
platform == TARGET_PLATFORM::Xe_ARL)
return SWSB_ENCODE_MODE::ThreeDistPipeDPMath;
return platformModel->getSWSBEncodeMode();
}
InstOptSet BinaryEncodingIGA::getIGAInstOptSet(G4_INST *inst) const {
InstOptSet options;
if (inst->isAccWrCtrlInst() && kernel.fg.builder->encodeAccWrEn()) {
options.add(InstOpt::ACCWREN);
}
if (inst->isAtomicInst()) {
options.add(InstOpt::ATOMIC);
}
if (inst->isBreakPointInst()) {
options.add(InstOpt::BREAKPOINT);
}
if (inst->isCachelineAligned()) {
options.add(InstOpt::CACHELINEALIGN);
}
if (inst->isNoDDChkInst()) {
options.add(InstOpt::NODDCHK);
}
if (inst->isNoDDClrInst()) {
options.add(InstOpt::NODDCLR);
}
if (inst->isNoPreemptInst()) {
options.add(InstOpt::NOPREEMPT);
}
if (inst->isYieldInst()) {
options.add(InstOpt::SWITCH);
}
if (inst->isSend()) {
if (inst->isEOT()) {
options.add(InstOpt::EOT);
}
if (inst->isNoSrcDepSet()) {
options.add(InstOpt::NOSRCDEPSET);
}
if (inst->asSendInst()->isSerializedInst()) {
options.add(InstOpt::SERIALIZE);
}
}
// Force instruction to be compacted if InstOpt::COMPACTED is given
// even if autoCompact is not given to IGA
if (inst->isCompactedInst()) {
options.add(InstOpt::COMPACTED);
}
// Force instruction to be nocompacted
if (inst->isNoCompactedInst()) {
options.add(InstOpt::NOCOMPACT);
}
return options;
}
void BinaryEncodingIGA::FixInst() {
for (auto bb : kernel.fg) {
for (auto iter = bb->begin(); iter != bb->end();) {
G4_INST *inst = *iter;
if (inst->isIntrinsic()) {
// WA for simulation: remove any intrinsics that should be lowered
// before binary encoding
vISA_ASSERT(inst->asIntrinsicInst()->getLoweredByPhase() ==
Phase::BinaryEncoding,
"Unexpected intrinsics in binary encoding");
iter = bb->erase(iter);
} else {
++iter;
}
}
}
}
iga::SFID BinaryEncodingIGA::getSFID(const G4_INST *inst) {
vISA_ASSERT(inst->isSend(), "Only send has SFID");
G4_SendDesc *msgDesc = inst->getMsgDesc();
auto funcID = msgDesc->getSFID();
auto sfid = iga::SFID::INVALID;
switch (funcID) {
case vISA::SFID::NULL_SFID:
sfid = iga::SFID::NULL_;
break;
case vISA::SFID::SAMPLER:
sfid = iga::SFID::SMPL;
break;
case vISA::SFID::GATEWAY:
sfid = iga::SFID::GTWY;
break;
case vISA::SFID::DP_DC2:
sfid = iga::SFID::DC2;
break;
case vISA::SFID::DP_RC:
sfid = iga::SFID::RC;
break;
case vISA::SFID::URB:
sfid = iga::SFID::URB;
break;
case vISA::SFID::SPAWNER:
sfid = iga::SFID::TS;
break;
case vISA::SFID::VME:
sfid = iga::SFID::VME;
break;
case vISA::SFID::DP_CC:
sfid = iga::SFID::DCRO;
break;
case vISA::SFID::DP_DC0:
sfid = iga::SFID::DC0;
break;
case vISA::SFID::DP_PI:
sfid = iga::SFID::PIXI;
break;
case vISA::SFID::DP_DC1:
sfid = iga::SFID::DC1;
break;
case vISA::SFID::CRE:
sfid = iga::SFID::CRE;
break;
case vISA::SFID::SLM:
sfid = iga::SFID::SLM;
break;
case vISA::SFID::UGM:
sfid = iga::SFID::UGM;
break;
case vISA::SFID::BTD:
sfid = iga::SFID::BTD;
break;
case vISA::SFID::RTHW:
sfid = iga::SFID::RTA;
break;
case vISA::SFID::TGM:
sfid = iga::SFID::TGM;
break;
case vISA::SFID::UGML:
sfid = iga::SFID::UGML;
break;
default:
vISA_ASSERT_UNREACHABLE("Unknown SFID generated from vISA");
break;
}
return sfid;
}
MathFC BinaryEncodingIGA::getMathFC(const G4_INST *inst) {
G4_MathOp mathControlValue = inst->asMathInst()->getMathCtrl();
switch (mathControlValue) {
case MATH_INV:
return MathFC::INV;
case MATH_LOG:
return MathFC::LOG;
case MATH_EXP:
return MathFC::EXP;
case MATH_SQRT:
return MathFC::SQT;
case MATH_RSQ:
return MathFC::RSQT;
case MATH_SIN:
return MathFC::SIN;
case MATH_COS:
return MathFC::COS;
case MATH_FDIV:
return MathFC::FDIV;
case MATH_POW:
return MathFC::POW;
case MATH_INT_DIV:
return MathFC::IDIV;
case MATH_INT_DIV_QUOT:
return MathFC::IQOT;
case MATH_INT_DIV_REM:
return MathFC::IREM;
case MATH_INVM:
return MathFC::INVM;
case MATH_RSQRTM:
return MathFC::RSQTM;
default:
vISA_ASSERT_UNREACHABLE("invalid math subfunction");
return MathFC::INVALID;
}
}
//
// Return the IGA op for the given vISA instruction <op> for platform p.
// <inst> is sometimes necessary to compute the subopcode (e.g., send)
// As vISA may call this function to access instruction properties such as
// saturation and conditional modifier and this may happen before all pseudo
// opperations are lowered, <allowUnknownOp> may be used to suppress assert;
// an invalid will be returned in this case.
//
std::pair<const OpSpec *, Subfunction>
BinaryEncodingIGA::getIgaOpInfo(const G4_INST *inst, const Model *m,
bool allowUnknownOp,
const IR_Builder &builder) {
Platform p = m->platform;
Op igaOp = Op::INVALID;
Subfunction sf = InvalidFC::INVALID;
switch (inst->opcode()) {
case G4_illegal:
igaOp = Op::ILLEGAL;
break;
case G4_mov:
igaOp = Op::MOV;
break;
case G4_sel:
igaOp = Op::SEL;
break;
case G4_movi:
igaOp = Op::MOVI;
break;
case G4_not:
igaOp = Op::NOT;
break;
case G4_and:
igaOp = Op::AND;
break;
case G4_or:
igaOp = Op::OR;
break;
case G4_xor:
igaOp = Op::XOR;
break;
case G4_shr:
igaOp = Op::SHR;
break;
case G4_shl:
igaOp = Op::SHL;
break;
case G4_smov:
igaOp = Op::SMOV;
break;
case G4_asr:
igaOp = Op::ASR;
break;
case G4_ror:
igaOp = Op::ROR;
break;
case G4_rol:
igaOp = Op::ROL;
break;
case G4_cmp:
igaOp = Op::CMP;
break;
case G4_cmpn:
igaOp = Op::CMPN;
break;
case G4_csel:
igaOp = Op::CSEL;
break;
case G4_bfrev:
igaOp = Op::BFREV;
break;
case G4_bfe:
igaOp = Op::BFE;
break;
case G4_bfi1:
igaOp = Op::BFI1;
break;
case G4_bfi2:
igaOp = Op::BFI2;
break;
case G4_jmpi:
igaOp = Op::JMPI;
break;
case G4_brd:
igaOp = Op::BRD;
break;
case G4_if:
igaOp = Op::IF;
break;
case G4_brc:
igaOp = Op::BRC;
break;
case G4_else:
igaOp = Op::ELSE;
break;
case G4_endif:
igaOp = Op::ENDIF;
break;
case G4_while:
igaOp = Op::WHILE;
break;
case G4_break:
igaOp = Op::BREAK;
break;
case G4_cont:
igaOp = Op::CONT;
break;
case G4_halt:
igaOp = Op::HALT;
break;
case G4_call: {
igaOp = Op::CALL;
// check if we're using calla for indirect call
if (builder.supportCallaRegSrc()) {
// check if we're doing indiret call
auto callTarget = inst->getSrc(0);
if (callTarget->isGreg() || (callTarget->isSrcRegRegion() &&
callTarget->asSrcRegRegion()->isIndirect()))
igaOp = Op::CALLA;
}
break;
}
case G4_return:
igaOp = Op::RET;
break;
case G4_goto:
igaOp = Op::GOTO;
break;
case G4_join:
igaOp = Op::JOIN;
break;
case G4_wait:
if (p >= Platform::XE) {
igaOp = Op::SYNC;
sf = SyncFC::BAR;
} else {
igaOp = Op::WAIT;
}
break;
case G4_send:
igaOp = Op::SEND;
sf = getSFID(inst);
break;
case G4_sendc:
igaOp = Op::SENDC;
sf = getSFID(inst);
break;
case G4_sends:
sf = getSFID(inst);
if (p >= Platform::XE) {
// G4 IR still calls send sends after Xe
igaOp = Op::SEND;
} else {
igaOp = Op::SENDS;
}
break;
case G4_sendsc:
sf = getSFID(inst);
if (p >= Platform::XE) {
// G4 IR still calls send sends after Xe
igaOp = Op::SENDC;
} else {
igaOp = Op::SENDSC;
}
break;
case G4_math:
sf = getMathFC(inst);
igaOp = Op::MATH;
break;
case G4_add:
igaOp = Op::ADD;
break;
case G4_mul:
igaOp = Op::MUL;
break;
case G4_avg:
igaOp = Op::AVG;
break;
case G4_frc:
igaOp = Op::FRC;
break;
case G4_rndu:
igaOp = Op::RNDU;
break;
case G4_rndd:
igaOp = Op::RNDD;
break;
case G4_rnde:
igaOp = Op::RNDE;
break;
case G4_rndz:
igaOp = Op::RNDZ;
break;
case G4_mac:
igaOp = Op::MAC;
break;
case G4_mach:
igaOp = Op::MACH;
if (inst->getPlatform() >= Xe_PVC && !inst->isAccWrCtrlInst()) {
igaOp = Op::MACL;
}
break;
case G4_lzd:
igaOp = Op::LZD;
break;
case G4_fbh:
igaOp = Op::FBH;
break;
case G4_fbl:
igaOp = Op::FBL;
break;
case G4_cbit:
igaOp = Op::CBIT;
break;
case G4_addc:
igaOp = Op::ADDC;
break;
case G4_subb:
igaOp = Op::SUBB;
break;
case G4_sad2:
igaOp = Op::SAD2;
break;
case G4_sada2:
igaOp = Op::SADA2;
break;
case G4_dp4:
igaOp = Op::DP4;
break;
case G4_dph:
igaOp = Op::DPH;
break;
case G4_dp3:
igaOp = Op::DP3;
break;
case G4_dp2:
igaOp = Op::DP2;
break;
case G4_dp4a:
igaOp = Op::DP4A;
break;
case G4_dpas:
case G4_dpasw: {
igaOp = inst->opcode() == G4_dpasw ? Op::DPASW : Op::DPAS;
G4_InstDpas *dpasInst = inst->asDpasInst();
uint8_t D = dpasInst->getSystolicDepth();
uint8_t C = dpasInst->getRepeatCount();
sf = GetDpasFC(D, C);
break;
}
case G4_add3:
igaOp = Op::ADD3;
break;
case G4_bfn:
igaOp = Op::BFN;
sf = getBfnFC(inst);
break;
case G4_line:
igaOp = Op::LINE;
break;
case G4_pln:
igaOp = Op::PLN;
break;
case G4_mad:
igaOp = Op::MAD;
break;
case G4_lrp:
igaOp = Op::LRP;
break;
case G4_madm:
igaOp = Op::MADM;
break;
case G4_nop:
igaOp = Op::NOP;
break;
case G4_label:
break;
case G4_pseudo_mad:
igaOp = Op::MAD;
break;
case G4_do:
vISA_ASSERT(!allowUnknownOp, "G4_do is not GEN ISA OPCODE.");
break;
case G4_pseudo_and:
igaOp = Op::AND;
break;
case G4_pseudo_or:
igaOp = Op::OR;
break;
case G4_pseudo_xor:
igaOp = Op::XOR;
break;
case G4_pseudo_not:
igaOp = Op::NOT;
break;
case G4_pseudo_fcall:
igaOp = Op::CALL;
break;
case G4_pseudo_fret:
igaOp = Op::RET;
break;
case G4_pseudo_sada2:
igaOp = Op::SADA2;
break;
case G4_pseudo_exit:
vISA_ASSERT(!allowUnknownOp, "G4_pseudo_exit not GEN ISA OPCODE.");
break;
case G4_pseudo_fc_call:
igaOp = Op::CALL;
break;
case G4_pseudo_fc_ret:
igaOp = Op::RET;
break;
case G4_intrinsic:
vISA_ASSERT(!allowUnknownOp, "G4_intrinsic not GEN ISA OPCODE.");
break;
case G4_sync_nop:
igaOp = Op::SYNC;
sf = SyncFC::NOP;
break;
case G4_sync_allrd:
igaOp = Op::SYNC;
sf = SyncFC::ALLRD;
break;
case G4_sync_allwr:
igaOp = Op::SYNC;
sf = SyncFC::ALLWR;
break;
case G4_sync_fence:
igaOp = Op::SYNC;
sf = SyncFC::FENCE;
break;
case G4_fcvt:
igaOp = Op::MOV;
break;
case G4_srnd:
igaOp = Op::SRND;
break;
case G4_NUM_OPCODE:
vISA_ASSERT_UNREACHABLE("G4_NUM_OPCODE unreachable");
break;
case G4_mulh:
vISA_ASSERT(!allowUnknownOp, "G4_mulh is not GEN ISA OPCODE.");
break;
case G4_madw:
vISA_ASSERT(!allowUnknownOp, "G4_madw not GEN ISA OPCODE.");
break;
default:
vISA_ASSERT(!allowUnknownOp, "INVALID opcode.");
break;
}
const OpSpec *os = &m->lookupOpSpec(igaOp);
return std::pair<const OpSpec *, Subfunction>(os, sf);
}
void BinaryEncodingIGA::SetSWSB(G4_INST *inst, SWSB &sw) {
// Set token, e.g. $0
using SWSBTokenType = vISA::G4_INST::SWSBTokenType;
if (inst->tokenHonourInstruction() &&
(inst->getTokenType() == SWSBTokenType::SB_SET)) {
sw.tokenType = SWSB::TokenType::SET;
sw.sbid = inst->getToken();
}
if (inst->opcode() == G4_mad && inst->hasNoACCSBSet()) {
sw.spToken = SWSB::SpecialToken::NOACCSBSET;
}
// Set distance, e.g. A@1
using DistanceType = vISA::G4_INST::DistanceType;
if ((unsigned)inst->getDistance()) {
// check the distance type for multi-dist-pipes
if (kernel.fg.builder->hasThreeALUPipes() ||
kernel.fg.builder->hasFourALUPipes()) {
switch (inst->getDistanceTypeXe()) {
case DistanceType::DIST:
sw.distType = SWSB::DistType::REG_DIST;
break;
case DistanceType::DISTALL:
sw.distType = SWSB::DistType::REG_DIST_ALL;
break;
case DistanceType::DISTINT:
sw.distType = SWSB::DistType::REG_DIST_INT;
break;
case DistanceType::DISTFLOAT:
sw.distType = SWSB::DistType::REG_DIST_FLOAT;
break;
case DistanceType::DISTLONG:
sw.distType = SWSB::DistType::REG_DIST_LONG;
break;
case DistanceType::DISTMATH:
sw.distType = SWSB::DistType::REG_DIST_MATH;
break;
case DistanceType::DISTS0:
sw.distType = SWSB::DistType::REG_DIST_SCALAR;
break;
default:
break;
}
} else {
// there is only one pipe on single-dist-pipe platform,
// must be REG_DIST
sw.distType = SWSB::DistType::REG_DIST;
}
sw.minDist = (uint32_t)inst->getDistance();
}
// Set token dependency, e.g. $1.src
if (inst->getTokenType() == SWSBTokenType::AFTER_READ ||
inst->getTokenType() == SWSBTokenType::AFTER_WRITE) {
uint8_t token = (uint8_t)inst->getToken();
if (inst->getTokenType() == SWSBTokenType::AFTER_READ) {
sw.tokenType = SWSB::TokenType::SRC;
} else if (inst->getTokenType() == SWSBTokenType::AFTER_WRITE) {
sw.tokenType = SWSB::TokenType::DST;
}
sw.sbid = token;
}
// Workaround: adjust send's swsb to align with HW behavior. Only adjust
// when solely token or distance is used on the instruction.
// - send has only SBID.src/dst --> SBID.set
// - send has only distance --> $0 and distance
// This workaround can be removed once vISA doesn't produce such SWSB.
// Currently this could happen only on EOT send.
if (inst->isSend() &&
!sw.verify(getIGASWSBEncodeMode(), SWSB::InstType::SEND)) {
sw.tokenType = SWSB::TokenType::SET;
if (sw.hasDist()) {
// if the distance type cannot be combined with SBID.set, force
// to ALL pipe
if (sw.distType != SWSB::DistType::REG_DIST_ALL &&
sw.distType != SWSB::DistType::REG_DIST_FLOAT &&
sw.distType != SWSB::DistType::REG_DIST_INT)
sw.distType = SWSB::DistType::REG_DIST_ALL;
}
}
return;
}
void BinaryEncodingIGA::getIGAFlagInfo(G4_INST *inst, const OpSpec *opSpec,
Subfunction sf, Predication &pred,
FlagModifier &condMod, RegRef &flagReg) {
G4_Predicate *predG4 = inst->getPredicate();
G4_CondMod *condModG4 = inst->getCondMod();
RegRef predFlag;
[[maybe_unused]] bool hasPredFlag = false;
if (opSpec->supportsPredication() && predG4 != nullptr) {
pred = getIGAPredication(predG4, inst->getExecSize(), *kernel.fg.builder);
predFlag = getIGAFlagReg(predG4->getBase());
flagReg = predFlag;
hasPredFlag = true;
}
bool hasImplicitModifier = opSpec->is(Op::MATH) && IsMacro(sf.math);
if ((opSpec->supportsFlagModifier() || hasImplicitModifier) &&
condModG4 != nullptr) {
condMod = getIGAFlagModifier(condModG4);
// in case for min/max sel instruction, it could have CondMod but has no
// flag registers
if (condModG4->getBase() != nullptr) {
flagReg = getIGAFlagReg(condModG4->getBase());
// pred and condMod Flags must be the same
vISA_ASSERT(!hasPredFlag || predFlag == flagReg,
"pred and condMod flags must be the same");
}
}
}
#if 0
static void DebugCaching(G4_Kernel &kernel)
{
std::map<uint32_t,std::pair<Caching,Caching>> descs;
for (auto bb : kernel.fg) {
for (auto inst : *bb)
{
if (inst->isSend()) {
G4_SendDescRaw *sd = inst->getMsgDescRaw();
if (sd) {
descs[sd->getDesc()] = sd->getCaching();
}
}
}
}
for (const auto &p : descs) {
std::cerr << std::hex << "0x" << p.first <<
" ===>"
" L1=" << std::dec << int(p.second.first) <<
" L3=" << std::dec << int(p.second.second) <<
"\n";
}
}
#endif
void BinaryEncodingIGA::Encode() {
// DebugCaching(this->kernel);
FixInst();
Block *currBB = nullptr;
auto isFirstInstLabel = [this]() {
for (auto bb : kernel.fg) {
for (auto inst : *bb) {
return inst->isLabel();
}
}
return false;
};
// Make the size of the first BB be multiple of 4 instructions, and do not
// compact any instructions in it, so that the size of the first BB is
// multiple of 64 bytes
if (kernel.hasPerThreadPayloadBB() || kernel.hasComputeFFIDProlog()) {
G4_BB *first_bb = *kernel.fg.begin();
size_t num_inst = first_bb->size();
vISA_ASSERT(num_inst != 0, "the first BB must not be empty");
// label instructions don't count. Only the first instruction could be a
// label
if (first_bb->front()->isLabel())
--num_inst;
if (num_inst % 4 != 0) {
size_t num_nop = 4 - (num_inst % 4);
for (size_t i = 0; i < num_nop; ++i)
first_bb->push_back(kernel.fg.builder->createNop(InstOpt_NoCompact));
}
// set all instruction to be NoCompact
for (auto inst : *first_bb) {
inst->setOptionOn(InstOpt_NoCompact);
}
}
if (!isFirstInstLabel()) {
// create a new BB if kernel does not start with label
currBB = IGAKernel->createBlock();
IGAKernel->appendBlock(currBB);
}
auto platformGen = kernel.getPlatformGeneration();
std::list<std::pair<Instruction *, G4_INST *>> encodedInsts;
Block *bbNew = nullptr;
SWSB_ENCODE_MODE swsbEncodeMode = getIGASWSBEncodeMode();
for (auto bb : this->kernel.fg) {
for (auto inst : *bb) {
bbNew = nullptr;
if (inst->isLabel()) {
// note that we create a new IGA BB per label instead of directly
// mapping vISA BB to IGA BB, as some vISA BBs can have multiple labels
// (e.g., multiple endifs)
G4_Label *label = inst->getLabel();
currBB = lookupIGABlock(label, *IGAKernel);
IGAKernel->appendBlock(currBB);
continue;
}
Instruction *igaInst = translateInstruction(inst, bbNew);
if (!igaInst) {
// assertion failure already reported
continue;
}
igaInst->addInstOpts(getIGAInstOptSet(inst));
if (platformGen >= PlatformGen::XE) {
SWSB sw;
SetSWSB(inst, sw);
SWSB::InstType instTy = SWSB::InstType::UNKNOWN;
if (inst->isMathPipeInst())
instTy = SWSB::InstType::MATH;
else if (inst->isDpas())
instTy = SWSB::InstType::DPAS;
else if (inst->isSend())
instTy = SWSB::InstType::SEND;
else
instTy = SWSB::InstType::OTHERS;
// Verify if swsb is in encode-able dist and token combination
if (!sw.verify(swsbEncodeMode, instTy))
IGA_ASSERT_FALSE("Invalid swsb dist and token combination");
igaInst->setSWSB(sw);
}
#if _DEBUG
igaInst->validate();
#endif
vASSERT(currBB);
currBB->appendInstruction(igaInst);
if (inst->requireNopAfter()) {
Instruction *igaInst = IGAKernel->createNopInstruction();
currBB->appendInstruction(igaInst);
}
if (bbNew) {
// Fall through block is created.
// So the new block needs to become current block
// so that jump offsets can be calculated correctly
currBB = bbNew;
}
// If, in future, we generate multiple binary inst
// for a single G4_INST, then it should be safe to
// make pair between the G4_INST and first encoded
// binary inst.
encodedInsts.emplace_back(igaInst, inst);
}
}
kernel.setAsmCount(IGAKernel->getInstructionCount());
if (m_kernelBuffer) {
m_kernelBufferSize = 0;
delete static_cast<uint8_t *>(m_kernelBuffer);
m_kernelBuffer = nullptr;
}
{ // time the encoding
TIME_SCOPE(IGA_ENCODER);
bool autoCompact = kernel.getOption(vISA_Compaction);
if (platform == Xe_PVC)
autoCompact = false; // PVC-A0 compaction is off (IGA only does B0+)
KernelEncoder encoder(IGAKernel, autoCompact);
encoder.setSWSBEncodingMode(swsbEncodeMode);
if (kernel.getOption(vISA_EnableIGASWSB)) {
encoder.enableIGAAutoDeps();
}
encoder.encode(kernel.fg.builder->criticalMsgStream());
m_kernelBufferSize = encoder.getBinarySize();
m_kernelBuffer = allocCodeBlock(m_kernelBufferSize);
memcpy_s(m_kernelBuffer, m_kernelBufferSize, encoder.getBinary(),
m_kernelBufferSize);
}
// encodedPC is available after encoding
for (auto &&inst : encodedInsts) {
inst.second->setGenOffset(inst.first->getPC());
}
if (kernel.hasPerThreadPayloadBB()) {
kernel.fg.builder->getJitInfo()->offsetToSkipPerThreadDataLoad =
kernel.getPerThreadNextOff();
}
if (kernel.hasCrossThreadPayloadBB()) {
kernel.fg.builder->getJitInfo()->offsetToSkipCrossThreadDataLoad =
kernel.getCrossThreadNextOff();
}
if (kernel.hasComputeFFIDProlog()) {
// something weird will happen if kernel has both PerThreadProlog and
// ComputeFFIDProlog
vISA_ASSERT(!kernel.hasPerThreadPayloadBB() &&
!kernel.hasCrossThreadPayloadBB(),
"per thread prolog and compute prolog exist");
kernel.fg.builder->getJitInfo()->offsetToSkipSetFFIDGP =
kernel.getComputeFFIDGPNextOff();
kernel.fg.builder->getJitInfo()->offsetToSkipSetFFIDGP1 =
kernel.getComputeFFIDGP1NextOff();
}
int dumpJSON = kernel.fg.builder->getuint32Option(vISA_dumpIgaJson);
if (dumpJSON) {
EmitJSON(dumpJSON);
}
}
void BinaryEncodingIGA::EmitJSON(int dumpJSON) {
std::string jsonFileName = fileName + ".json";
std::ofstream ofs(jsonFileName, std::ofstream::out);
FormatOpts fos(*platformModel);
fos.printJson = true;
fos.printJsonVersion = 2; // c.f. iga/IGAJSONv2.md
IGAKernel->resetIds();
if (dumpJSON > 1) {
fos.printInstDefs = true;
}
iga::ErrorHandler eh;
FormatKernel(eh, ofs, fos, *IGAKernel);
}
Instruction *BinaryEncodingIGA::translateInstruction(G4_INST *g4inst,
Block *&bbNew) {
Instruction *igaInst = nullptr;
auto opinfo = getIgaOpInfo(g4inst, platformModel, false, *kernel.fg.builder);
// common fields: op, predicate, flag reg, exec size, exec mask offset,
// mask ctrl, conditional modifier
const OpSpec *opSpec = opinfo.first;
if (opSpec == nullptr || !opSpec->isValid()) {
std::cerr << "INVALID opcode " << G4_Inst_Table[g4inst->opcode()].str
<< "\n";
vISA_ASSERT(false, "INVALID OPCODE.");
return nullptr;
}
Op igaOp = opSpec->op;
Subfunction sf = opinfo.second;
Predication pred;
RegRef flagReg{0, 0};
ExecSize execSize = getIGAExecSize(g4inst->getExecSize());
ChannelOffset chOff = getIGAChannelOffset(g4inst->getMaskOffset());
MaskCtrl maskCtrl = getIGAMaskCtrl(g4inst->opcode() == G4_jmpi ||
g4inst->isWriteEnableInst());
FlagModifier condModifier = FlagModifier::NONE;
getIGAFlagInfo(g4inst, opSpec, sf, pred, condModifier, flagReg);
if (opSpec->isBranching()) {
BranchCntrl brnchCtrl = getIGABranchCntrl(g4inst->asCFInst()->isBackward());
igaInst = IGAKernel->createBranchInstruction(
*opSpec, pred, flagReg, execSize, chOff, maskCtrl, brnchCtrl);
} else if (opSpec->isAnySendFormat()) {
{
SendDesc desc = getIGASendDesc(g4inst->asSendInst());
SendExDescOpts sdos = getIGASendExDesc(g4inst->asSendInst());
igaInst = IGAKernel->createSendInstruction(
*opSpec, sf.send, pred, flagReg, execSize, chOff, maskCtrl,
sdos.exDescImmOff,
sdos.exDesc, desc);
vISA_ASSERT(igaInst, "Instruction is NULL");
if (!igaInst) {
return nullptr;
}
igaInst->setSrc1Length(sdos.xlen);
igaInst->addInstOpts(sdos.extraOpts);
}
} else if (opSpec->op == Op::NOP) {
igaInst = IGAKernel->createNopInstruction();
} else if (opSpec->op == Op::ILLEGAL) {
igaInst = IGAKernel->createIllegalInstruction();
} else {
igaInst = IGAKernel->createBasicInstruction(
*opSpec, pred, flagReg, execSize, chOff, maskCtrl, condModifier, sf);
}
vISA_ASSERT(igaInst, "Instruction is NULL");
if (!igaInst) {
// only on asserts; this should only happen if memory allocation
// fails for some reason
return nullptr;
}
int visaOff = g4inst->getVISAId();
igaInst->setLoc(visaOff); // make IGA src off track CISA id
if (opSpec->supportsDestination()) {
translateInstructionDst(g4inst, igaInst);
}
if (opSpec->isBranching() && igaOp != Op::JMPI && igaOp != Op::RET &&
igaOp != Op::CALL && igaOp != Op::CALLA && igaOp != Op::BRC &&
igaOp != Op::BRD) {
translateInstructionBranchSrcs(g4inst, igaInst, bbNew);
} else {
translateInstructionSrcs(g4inst, igaInst);
}
return igaInst;
}
void BinaryEncodingIGA::translateInstructionDst(G4_INST *g4inst,
Instruction *igaInst) {
vISA_ASSERT(g4inst->getDst(), "dst must not be null");
G4_DstRegRegion *dst = g4inst->getDst();
DstModifier dstModifier = getIGADstModifier(g4inst->getSaturate());
Region::Horz hstride = getIGAHorz(dst->getHorzStride());
Type type = getIGAType(g4inst, Opnd_dst, platform);
// workaround for SKL bug
// not all bits are copied from immediate descriptor
if (g4inst->isSend() && platform >= GENX_SKL && platform < GENX_ICLLP) {
const G4_SendDescRaw *msgDesc = g4inst->getMsgDescRaw();
vISA_ASSERT(msgDesc, "expected raw descriptor");
G4_Operand *descOpnd = g4inst->asSendInst()->getMsgDescOperand();
if (!descOpnd->isImm() && msgDesc->is16BitReturn()) {
type = Type::HF;
}
}
if (igaInst->isMacro()) {
RegRef regRef = getIGARegRef(dst);
Region::Horz hstride = getIGAHorz(dst->getHorzStride());
igaInst->setMacroDestination(dstModifier, getIGARegName(dst), regRef,
getIGAImplAcc(dst->getAccRegSel()), hstride,
type);
} else if (dst->getRegAccess() == Direct) {
igaInst->setDirectDestination(dstModifier, getIGARegName(dst),
getIGARegRef(dst), hstride, type);
} else { // Operand::Kind::INDIRECT
RegRef regRef{0, 0};
bool valid;
regRef.subRegNum = (uint8_t)dst->ExIndSubRegNum(valid);
igaInst->setIndirectDestination(dstModifier, regRef, dst->getAddrImm(),
hstride, type);
}
}
void BinaryEncodingIGA::translateInstructionBranchSrcs(G4_INST *inst,
Instruction *igaInst,
Block *&bbNew) {
if (inst->asCFInst()->getJip()) {
// encode jip/uip for branch inst
// note that it does not apply to jmpi/call/ret/brc/brd,
// which may have register sources. Their label appears directly as
// source operand instead.
G4_Operand *uip = inst->asCFInst()->getUip();
G4_Operand *jip = inst->asCFInst()->getJip();
// iga will take care off
if (uip) {
igaInst->setLabelSource(SourceIndex::SRC1,
lookupIGABlock(uip->asLabel(), *IGAKernel),
Type::UD);
}
igaInst->setLabelSource(SourceIndex::SRC0,
lookupIGABlock(jip->asLabel(), *IGAKernel),
Type::UD);
} else {
// Creating a fall through block
bbNew = IGAKernel->createBlock();
igaInst->setLabelSource(SourceIndex::SRC0, bbNew, Type::UD);
IGAKernel->appendBlock(bbNew);
}
}
void BinaryEncodingIGA::translateInstructionSrcs(G4_INST *inst,
Instruction *igaInst) {
// set source operands
int numSrcToEncode = inst->getNumSrc();
if (inst->isSend()) {
// skip desc/exdesc as they are handled separately
numSrcToEncode = inst->asSendInst()->getNumSrcPayloads();
if (numSrcToEncode == 1 && platformModel->platform >= Platform::XE) {
RegRef regTemp(0, 0);
Region rgnTemp = Region::SRC010;
igaInst->setDirectSource(SourceIndex::SRC1, SrcModifier::NONE,
RegName::ARF_NULL, regTemp, rgnTemp,
Type::INVALID);
}
}
if (platform >= GENX_ICLLP && inst->opcode() == G4_movi &&
numSrcToEncode == 1) {
// From ICL, 'movi' becomes a binary instruction with an
// optional immediate operand, which needs encoding as null
// or imm32. So far, within vISA jitter, 'movi' is still
// modeled as unary instruction, setting src1 to null for
// platforms >= CNL.
// The byte/word level cross bar is removed from PVC+ so the movi cannot
// work for less than DW types.
iga::Type ty = platform >= Xe_PVC ? Type::UD : Type::UB;
RegRef regTemp(0, 0);
Region rgnTemp = Region::SRC110;
igaInst->setDirectSource(SourceIndex::SRC1, SrcModifier::NONE,
RegName::ARF_NULL, regTemp, rgnTemp, ty);
}
for (int i = 0; i < numSrcToEncode; i++) {
SourceIndex opIx = SourceIndex::SRC0;
switch (i) {
case 0:
opIx = SourceIndex::SRC0;
break;
case 1:
opIx = SourceIndex::SRC1;
break;
case 2:
opIx = SourceIndex::SRC2;
break;
default:
vISA_ASSERT_UNREACHABLE("invalid source index number");
break;
}
G4_Operand *src = inst->getSrc(i);
if (src->isSrcRegRegion()) {
G4_SrcRegRegion *srcRegion = src->asSrcRegRegion();
SrcModifier srcMod = getIGASrcModifier(srcRegion->getModifier());
Region region = getIGARegion(srcRegion, i);
Type type = Type::INVALID;
// let IGA take care of types for send/s instructions
if (!igaInst->getOpSpec().isAnySendFormat()) {
type = getIGAType(inst, inst->getSrcOperandNum(i), platform);
} else if (i == 0 && platform >= GENX_SKL && platform < GENX_ICLLP) {
// work around for SKL bug
// not all bits are copied from immediate descriptor
G4_SendDescRaw *msgDesc = inst->getMsgDescRaw();
vISA_ASSERT(msgDesc, "expected raw descriptor");
G4_Operand *descOpnd = inst->asSendInst()->getMsgDescOperand();
if (!descOpnd->isImm() && msgDesc->is16BitInput()) {
type = Type::HF;
}
}
if (igaInst->isMacro()) {
auto accRegSel =
srcRegion->isNullReg() ? NOACC : srcRegion->getAccRegSel();
RegRef regRef = getIGARegRef(srcRegion);
igaInst->setMacroSource(opIx, srcMod, getIGARegName(srcRegion), regRef,
getIGAImplAcc(accRegSel), region, type);
} else if (inst->isDpas()) {
vISA_ASSERT(srcRegion->getRegAccess() == Direct,
"dpas does not support indirect GRF operands");
G4_InstDpas *dpasInst = inst->asDpasInst();
RegRef regRef = getIGADpasRegRef(dpasInst, i);
type = getIGADpasType(dpasInst, i);
igaInst->setDirectSource(opIx, srcMod, getIGARegName(srcRegion), regRef,
region, type);
} else if (srcRegion->getRegAccess() == Direct) {
igaInst->setDirectSource(opIx, srcMod, getIGARegName(srcRegion),
getIGARegRef(srcRegion), region, type);
} else {
RegRef regRef{0, 0};
bool valid;
regRef.subRegNum = (uint8_t)srcRegion->ExIndSubRegNum(valid);
// set to GRF for indirect register access
iga::RegName regName = iga::RegName::GRF_R;
if (getIGARegName(srcRegion) == iga::RegName::ARF_S)
regName = iga::RegName::ARF_S;
igaInst->setIndirectSource(opIx, srcMod, regName, regRef,
srcRegion->getAddrImm(), region, type);
}
} else if (src->isLabel()) {
igaInst->setLabelSource(opIx, lookupIGABlock(src->asLabel(), *IGAKernel),
Type::UD);
} else if (src->isImm()) {
Type type = getIGAType(src->getType(), platform);
ImmVal val;
val = src->asImm()->getImm();
val.kind = getIGAImmType(src->getType());
igaInst->setImmediateSource(opIx, val, type);
} else {
IGA_ASSERT_FALSE("unexpected src kind");
}
} // for
}
SendDesc BinaryEncodingIGA::getIGASendDesc(G4_InstSend *sendInst) const {
SendDesc desc;
G4_Operand *msgDesc = sendInst->getMsgDescOperand();
if (msgDesc->isImm()) {
desc.type = SendDesc::Kind::IMM;
desc.imm = (uint32_t)msgDesc->asImm()->getImm();
// This is a WA that needs to add fence.ugm before EOT
// fence.ugm.invalidate.tile works, but performance for raytracing
// might be affected. HW team came out a way to get around of it
// by using reserved Flush TYPE = 6, with the following
// DG2_Backup_Mode = 0
// Flush_Range = 0
// (So, it is fence.ugm.6.tile !)
auto msgDesc = sendInst->getMsgDesc();
if (msgDesc->isLSC() && msgDesc->isFence()) {
// Flush Type : bit[14:12]
uint32_t flushTy = ((desc.imm >> 12) & 0x7);
if (flushTy == 6) {
// DG2 fence desc
// both backupMode(bit[18]) and flushRange(bit[15]) must be zero
constexpr uint32_t backupMode = (1 << 18), flushRange = (1 << 15);
desc.imm = desc.imm & ~(backupMode | flushRange);
}
}
} else {
desc.type = SendDesc::Kind::REG32A;
desc.reg.regNum = 0; // must be a0
bool valid = false;
desc.reg.subRegNum = (uint8_t)msgDesc->asSrcRegRegion()->ExSubRegNum(valid);
vISA_ASSERT(valid, "invalid subreg");
}
return desc;
}
///////////////////////////////////////////////////////////////////////////////
// ExDesc encoding
static SendDesc encodeExDescSendUnary(G4_InstSend *sendInst, int &xlen,
InstOptSet &extraOpts) {
SendDesc exDescIga;
// old unary packed send
// exDesc is stored in SendMsgDesc and must be IMM
const G4_SendDescRaw *descG4 = sendInst->getMsgDescRaw();
vISA_ASSERT(descG4 != nullptr, "expected raw send");
exDescIga.type = SendDesc::Kind::IMM;
uint32_t tVal = descG4->getExtendedDesc();
if (sendInst->getBuilder().getPlatformGeneration() >= PlatformGen::XE) {
// We must clear the funcID in the extended message.
// In Xe+ this is part of the EU encoding, not the descriptor.
// vISA/G4IR still treat it as part of the descriptor.
tVal = tVal & 0xFFFFFFF0;
// clear the EOT bit which is not part of exDesc
tVal &= ~(1 << 5);
}
exDescIga.imm = tVal;
// non-split send implies Src1.Length == 0
xlen = 0;
return exDescIga;
}
// A helper function to print send src/dst length information to a file.
// vISA_ShaderDataBaseStats key a requirement to use
void BinaryEncodingIGA::printSendDataToFile(uint32_t src0Len,
uint32_t src1Len,
uint32_t dstLen,
const char *filePath) const {
FILE *f = fopen(filePath, "a");
if (f) {
uint32_t namePos = fileName.find_last_of('\\', fileName.size());
if (namePos > 0)
namePos++;
fprintf(f, "file=%s src0Len=%d src1Len=%d dstLen=%d \n",
fileName.substr(namePos, fileName.size()).data(), src0Len, src1Len,
dstLen); // Do not remove a white space at the end!
fclose(f);
}
}
////////////////////////////////////////////////////////////////////
// these handle binary sends (old "sends" and Xe+ "send"
//
SendDesc BinaryEncodingIGA::encodeExDescImm(G4_INST *sendInst,
SendExDescOpts &sdos) const {
SendDesc exDescIga;
const G4_Operand *exDescG4 = sendInst->getSrc(3);
const G4_SendDescRaw *descG4 = (G4_SendDescRaw *)sendInst->getMsgDesc();
vISA_ASSERT(descG4 != nullptr, "expected raw descriptor");
if (sendInst->getBuilder().getOption(vISA_ShaderDataBaseStats)) {
auto JitInfo = kernel.fg.builder->getJitInfo();
JitInfo->sendInfo.src0Vec.push_back(descG4->getSrc0LenRegs());
JitInfo->sendInfo.src1Vec.push_back(descG4->getSrc1LenRegs());
JitInfo->sendInfo.destVec.push_back(descG4->getDstLenRegs());
printSendDataToFile(descG4->getSrc0LenRegs(),
descG4->getSrc1LenRegs(),
descG4->getDstLenRegs(),
sendInst->getBuilder().getOptions()->getOptionCstr(
vISA_ShaderDataBaseStatsFilePath));
}
sdos.xlen = (int)descG4->extMessageLength();
exDescIga.type = SendDesc::Kind::IMM;
exDescIga.imm = (uint32_t)exDescG4->asImm()->getImm();
// We must clear the funcID in the extended message for Xe+
// because it is part of the EU instruction, not the descriptor,
// and, vISA/G4-IR still thinks of it as part of the descriptor.
//
// Ditto for the EOT bit which is moved out of extDesc
//
// The extended message format
// struct ExtendedMsgDescLayout {
// uint32_t funcID : 4; // bit 0:3 << not part of ExDesc
// uint32_t unnamed1 : 1; // bit 4
// uint32_t eot : 1; // bit 5 << not part of ExDesc
// uint32_t extMsgLength : 5; // bit 6:10
// uint32_t unnamed2 : 5; // bit 11:15
// uint32_t extFuncCtrl : 16; // bit 16:31
// };
auto platformGen = sendInst->getBuilder().getPlatformGeneration();
if (platformGen >= PlatformGen::XE) {
exDescIga.imm &= 0xFFFFFFC0;
}
if (kernel.fg.builder->encodeSendSrc1Length()) {
// In DG2+ ExDesc[10:6] is no longer Src1.Length even for imm
// and the field is always part of the EU encoding
if (descG4->isCPSEnabled()) {
sdos.extraOpts.add(InstOpt::CPS);
}
// We must keep ExDesc[10:6] as Src1.Len until we fix the
// 100s of uses that assume this.
// IGA expects these bits to be 0's on this platform
//
// FIXME: remove the if guard once we enable DG2
// (i.e. once DG2 targets IGA DG2 and not XE_HP, we should clear
// the bottom 12b for DG2 too) and only use the IR fields above
// (exDescArg.{cps,src1Length})
exDescIga.imm &= 0xFFFFF000;
}
// Note: ExBSO is never permitted for imm descriptors
// clear the EOT bit which is not part of exDesc on XE+
if (platformGen >= PlatformGen::XE)
exDescIga.imm &= ~(1 << 5);
return exDescIga;
}
SendDesc BinaryEncodingIGA::encodeExDescRegA0(G4_INST *sendInst,
SendExDescOpts &sdos) const {
G4_Operand *exDescG4 = sendInst->getSrc(3);
const G4_SendDescRaw *descG4 = sendInst->getMsgDescRaw();
vISA_ASSERT(descG4 != nullptr, "expected raw descriptor");
if (sendInst->getBuilder().getOption(vISA_ShaderDataBaseStats)) {
auto JitInfo = kernel.fg.builder->getJitInfo();
JitInfo->sendInfo.src0Vec.push_back(descG4->getSrc0LenRegs());
JitInfo->sendInfo.src1Vec.push_back(descG4->getSrc1LenRegs());
JitInfo->sendInfo.destVec.push_back(descG4->getDstLenRegs());
printSendDataToFile(descG4->getSrc0LenRegs(),
descG4->getSrc1LenRegs(),
descG4->getDstLenRegs(),
sendInst->getBuilder().getOptions()->getOptionCstr(
vISA_ShaderDataBaseStatsFilePath));
}
SendDesc exDescIga;
exDescIga.type = SendDesc::Kind::REG32A;
exDescIga.reg.regNum = 0; // must be a0
bool valid = false;
exDescIga.reg.subRegNum =
(uint16_t)exDescG4->asSrcRegRegion()->ExSubRegNum(valid);
vISA_ASSERT(valid, "invalid subreg");
if (kernel.fg.builder->useNewExtDescFormat() && descG4->isCPSEnabled()) {
// CPS is an instruction option if using RegDesc+ExBSO
sdos.extraOpts.add(InstOpt::CPS);
}
// By default all RegDesc in the new descriptor format will use
// the ExBSO model if at all possible
bool encodeExBso = kernel.fg.builder->useNewExtDescFormat();
// On DG2 LSC mustn't use ExBSO for BTI (hardware restriction).
// If this is an LSC descriptor, then dig out the LscAddrType field and
// compare to 3 (which means BTI).
const bool isLsc = descG4->isLscOp();
const bool isLscBti = isLsc && descG4->getLscAddrType() == LSC_ADDR_TYPE_BTI;
const bool isUgm = sendInst->getMsgDesc()->getSFID() == vISA::SFID::UGM;
encodeExBso &= !isLscBti;
encodeExBso &= (platform < Xe2 || !isUgm);
if (encodeExBso)
sdos.extraOpts.add(InstOpt::EXBSO);
if (isLsc && isUgm && !encodeExBso) {
bool isLscBssOrSs = descG4->getLscAddrType() == LSC_ADDR_TYPE_BSS ||
descG4->getLscAddrType() == LSC_ADDR_TYPE_SS;
if (isLscBssOrSs && descG4->getBti() != nullptr) {
// BSS/SS with a0 reg (ExDesc.IsReg) with UGM
sdos.exDescImmOff = descG4->getExDescImmOff();
}
}
// G4 IR keeps Src1.Length (xlen) separate. So it's known,
// (even with a reg desc in nonExBSO mode)
sdos.xlen = (int)descG4->extMessageLength();
return exDescIga;
}
SendExDescOpts
BinaryEncodingIGA::getIGASendExDesc(G4_InstSend *sendInst) const {
SendExDescOpts sdos;
vISA_ASSERT(sendInst->isSend(), "expect send inst");
if (sendInst->isEOT())
sdos.extraOpts.add(InstOpt::EOT);
if (sendInst->isSplitSend()) {
const G4_Operand *exDesc = sendInst->getMsgExtDescOperand();
sdos.exDesc = exDesc->isImm() ? encodeExDescImm(sendInst, sdos)
: encodeExDescRegA0(sendInst, sdos);
} else {
sdos.exDesc = encodeExDescSendUnary(sendInst, sdos.xlen, sdos.extraOpts);
}
return sdos;
}
void *BinaryEncodingIGA::EmitBinary(size_t &binarySize) {
binarySize = m_kernelBufferSize;
if (kernel.getOption(vISA_GenerateBinary)) {
std::string binFileName = fileName + ".dat";
if (CisaFramework::allowDump(*kernel.getOptions(), binFileName)) {
std::ofstream os(binFileName, std::ios::binary);
if (!os) {
std::string errStr;
errStr = "BinaryEncodingIGA: unable to open output path for write: " +
binFileName + "\n";
// vISA_ASSERT(false, errStr);
return m_kernelBuffer;
}
os.write((const char *)m_kernelBuffer, binarySize);
}
}
return m_kernelBuffer;
}
ExecSize BinaryEncodingIGA::getIGAExecSize(int execSize) {
switch (execSize) {
case 1:
return ExecSize::SIMD1;
case 2:
return ExecSize::SIMD2;
case 4:
return ExecSize::SIMD4;
case 8:
return ExecSize::SIMD8;
case 16:
return ExecSize::SIMD16;
case 32:
return ExecSize::SIMD32;
default:
vISA_ASSERT_UNREACHABLE("illegal simd size");
return ExecSize::INVALID;
}
}
ChannelOffset BinaryEncodingIGA::getIGAChannelOffset(int offset) {
switch (offset) {
case 0:
return ChannelOffset::M0;
case 4:
return ChannelOffset::M4;
case 8:
return ChannelOffset::M8;
case 12:
return ChannelOffset::M12;
case 16:
return ChannelOffset::M16;
case 20:
return ChannelOffset::M20;
case 24:
return ChannelOffset::M24;
case 28:
return ChannelOffset::M28;
default:
vISA_ASSERT_UNREACHABLE("illegal mask offset");
return ChannelOffset::M0;
}
}
MaskCtrl BinaryEncodingIGA::getIGAMaskCtrl(bool noMask) {
return noMask ? MaskCtrl::NOMASK : MaskCtrl::NORMAL;
}
RegName BinaryEncodingIGA::getIGAARFName(G4_ArchRegKind areg) {
switch (areg) {
case AREG_NULL:
return RegName::ARF_NULL;
case AREG_A0:
return RegName::ARF_A;
case AREG_ACC0:
case AREG_ACC1:
return RegName::ARF_ACC;
case AREG_MASK0:
return RegName::ARF_CE;
case AREG_MSG0:
return RegName::ARF_MSG;
case AREG_DBG:
return RegName::ARF_DBG;
case AREG_SR0:
return RegName::ARF_SR;
case AREG_CR0:
return RegName::ARF_CR;
case AREG_N0:
case AREG_N1:
return RegName::ARF_N;
case AREG_IP:
return RegName::ARF_IP;
case AREG_F0:
case AREG_F1:
case AREG_F2:
case AREG_F3:
return RegName::ARF_F;
case AREG_TM0:
return RegName::ARF_TM;
case AREG_TDR0:
return RegName::ARF_TDR;
case AREG_SP:
return RegName::ARF_SP;
case AREG_S0:
return RegName::ARF_S;
default:
vISA_ASSERT_UNREACHABLE("illegal ARF");
return RegName::INVALID;
}
}
Type BinaryEncodingIGA::getIGAType(const G4_INST *I, Gen4_Operand_Number O,
TARGET_PLATFORM P) {
G4_Type Ty = I->getOperand(O)->getType();
if (I->opcode() == G4_fcvt) {
if (Ty == Type_UB) {
return Type::BF8;
}
if (Ty == Type_B) {
return Type::HF8;
}
if (Ty == Type_UD) {
return Type::TF32;
}
}
if (I->opcode() == G4_srnd) {
if (O == Opnd_dst && Ty == Type_UB) {
return Type::BF8;
}
}
return getIGAType(Ty, P);
}
Type BinaryEncodingIGA::getIGAType(G4_Type type, TARGET_PLATFORM genxPlatform) {
switch (type) {
case Type_UB:
return Type::UB;
case Type_B:
return Type::B;
case Type_UW:
return Type::UW;
case Type_W:
return Type::W;
case Type_UD:
return Type::UD;
case Type_D:
return Type::D;
case Type_UQ:
return Type::UQ;
case Type_Q:
return Type::Q;
case Type_HF:
return Type::HF;
case Type_F:
return Type::F;
case Type_DF:
return Type::DF;
case Type_UV:
return Type::UV;
case Type_V:
return Type::V;
case Type_VF:
return Type::VF;
case Type_NF:
return Type::NF;
case Type_BF:
return Type::BF;
default:
vISA_ASSERT_UNREACHABLE("illegal type");
return Type::INVALID;
}
}
PredCtrl BinaryEncodingIGA::getIGAPredCtrl(G4_Predicate_Control g4PrCtl) {
switch (g4PrCtl) {
case PRED_DEFAULT:
return PredCtrl::SEQ;
case PRED_ANY2H:
return PredCtrl::ANY2H;
case PRED_ANY4H:
return PredCtrl::ANY4H;
case PRED_ANY8H:
return PredCtrl::ANY8H;
case PRED_ANY16H:
return PredCtrl::ANY16H;
case PRED_ANY32H:
return PredCtrl::ANY32H;
case PRED_ALL2H:
return PredCtrl::ALL2H;
case PRED_ALL4H:
return PredCtrl::ALL4H;
case PRED_ALL8H:
return PredCtrl::ALL8H;
case PRED_ALL16H:
return PredCtrl::ALL16H;
case PRED_ALL32H:
return PredCtrl::ALL32H;
case PRED_ANYV:
return PredCtrl::ANYV;
case PRED_ALLV:
return PredCtrl::ALLV;
case PRED_ANY_WHOLE:
return PredCtrl::ANY;
case PRED_ALL_WHOLE:
return PredCtrl::ALL;
default:
vISA_ASSERT_UNREACHABLE("illegal predicate control");
return PredCtrl::NONE;
}
}
Predication BinaryEncodingIGA::getIGAPredication(const G4_Predicate *predG4,
G4_ExecSize execSize,
const IR_Builder &builder) {
// Xe_PVC+ has removed predCtrl width. Only .any, .all and .seq (default) are
// supported. Try to translate PredCtrl values to compatible ones on Xe_PVC+
// Return true on success, false on fail
[[maybe_unused]]
auto translatePredCtrl = [&](G4_Predicate_Control &predCtrl) {
if (builder.predCtrlHasWidth())
return true;
if (predCtrl == PRED_ANY_WHOLE || predCtrl == PRED_ALL_WHOLE ||
predCtrl == PRED_DEFAULT)
return true;
// .any*h/.all*h (pre-pvc) is equal to .any/.all (pvc+) only when the
// instruction execSize match the predCtrl group size
if (execSize != predG4->getPredCtrlGroupSize())
return false;
// translate to equivalent .any or .all whenever possible
switch (predCtrl) {
case PRED_ANY2H:
case PRED_ANY4H:
case PRED_ANY8H:
case PRED_ANY16H:
case PRED_ANY32H:
predCtrl = PRED_ANY_WHOLE;
break;
case PRED_ALL2H:
case PRED_ALL4H:
case PRED_ALL8H:
case PRED_ALL16H:
case PRED_ALL32H:
predCtrl = PRED_ALL_WHOLE;
break;
default:
// PRED_ANY_WHOLE, PRED_ALL_WHOLE, PRED_DEFAULT are handled above
// PRED_ANYV, PRED_ALLV are not supported
return false;
}
return true;
};
Predication pred;
if (predG4) {
G4_Predicate_Control g4PrCtl = predG4->getControl();
vISA_ASSERT(translatePredCtrl(g4PrCtl), "illegal predicate control");
pred.function = getIGAPredCtrl(g4PrCtl);
pred.inverse = predG4->getState() != PredState_Plus;
}
return pred;
}
SrcModifier BinaryEncodingIGA::getIGASrcModifier(G4_SrcModifier srcMod) {
switch (srcMod) {
case Mod_Minus:
return SrcModifier::NEG;
case Mod_Abs:
return SrcModifier::ABS;
case Mod_Minus_Abs:
return SrcModifier::NEG_ABS;
case Mod_Not:
return SrcModifier::NEG;
case Mod_src_undef:
return SrcModifier::NONE;
default:
vISA_ASSERT_UNREACHABLE("illegal source modifier");
return SrcModifier::NONE;
}
}
Region::Vert BinaryEncodingIGA::getIGAVert(int vstride) {
switch (vstride) {
case 0:
return Region::Vert::VT_0;
case 1:
return Region::Vert::VT_1;
case 2:
return Region::Vert::VT_2;
case 4:
return Region::Vert::VT_4;
case 8:
return Region::Vert::VT_8;
case 16:
return Region::Vert::VT_16;
case 32:
return Region::Vert::VT_32;
case UNDEFINED_SHORT:
return Region::Vert::VT_VxH;
default:
vISA_ASSERT_UNREACHABLE("illegal vstride");
return Region::Vert::VT_INVALID;
}
}
Region::Width BinaryEncodingIGA::getIGAWidth(int width) {
switch (width) {
case 1:
return Region::Width::WI_1;
case 2:
return Region::Width::WI_2;
case 4:
return Region::Width::WI_4;
case 8:
return Region::Width::WI_8;
case 16:
return Region::Width::WI_16;
default:
vISA_ASSERT_UNREACHABLE("illegal width");
return Region::Width::WI_INVALID;
}
}
Region::Horz BinaryEncodingIGA::getIGAHorz(int hstride) {
switch (hstride) {
case 0:
return Region::Horz::HZ_0;
case 1:
return Region::Horz::HZ_1;
case 2:
return Region::Horz::HZ_2;
case 4:
return Region::Horz::HZ_4;
default:
vISA_ASSERT_UNREACHABLE("illegal hstride");
return Region::Horz::HZ_INVALID;
}
}
Region BinaryEncodingIGA::getIGARegion(G4_SrcRegRegion *srcRegion, int srcPos) {
Region igaRegion;
const RegionDesc *region = srcRegion->getRegion();
if ((srcRegion->getInst()->getNumSrc() == 3 &&
!srcRegion->getInst()->isSend())) {
// special handling for 3src instructions
if (srcPos != 2) {
// for src0 and src1, IGA/GED does not like width to be set
igaRegion.set(getIGAVert(region->vertStride), Region::Width::WI_INVALID,
getIGAHorz(region->horzStride));
} else {
// for src2, IGA expects both VS and W to be invalid
igaRegion.set(Region::Vert::VT_INVALID, Region::Width::WI_INVALID,
getIGAHorz(region->horzStride));
}
} else {
igaRegion.set(getIGAVert(region->vertStride), getIGAWidth(region->width),
getIGAHorz(region->horzStride));
}
return igaRegion;
}
EncodeResult vISA::EncodeKernelIGA(vISA::G4_Kernel &k,
const std::string &fname) {
EncodeResult r;
BinaryEncodingIGA encoder(k, fname);
encoder.Encode();
r.binary = encoder.EmitBinary(r.binaryLen);
return r;
}
static const Model *GetModel(TARGET_PLATFORM p) {
const Model *m =
Model::LookupModel(BinaryEncodingIGA::getIGAInternalPlatform(p));
return m;
}
bool vISA::InstSupportsSaturationIGA(TARGET_PLATFORM p, const G4_INST &i,
const IR_Builder &builder) {
const Model *m = GetModel(p);
if (m) {
auto oi = BinaryEncodingIGA::getIgaOpInfo(&i, m, true, builder);
return oi.first && oi.first->isValid() && oi.first->supportsSaturation();
} else {
return false;
}
}
bool vISA::InstSupportsSrcModifierIGA(TARGET_PLATFORM p, const G4_INST &i,
const IR_Builder &builder) {
const Model *m = GetModel(p);
if (m) {
auto oi = BinaryEncodingIGA::getIgaOpInfo(&i, m, true, builder);
return oi.first && oi.first->isValid() &&
oi.first->supportsSourceModifiers();
} else {
return false;
}
}
|