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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2022 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/Legalizer/PeepholeTypeLegalizer.hpp"
#include "Compiler/CISACodeGen/layout.hpp"
#include "Compiler/CISACodeGen/DeSSA.hpp"
#include "Compiler/CISACodeGen/GenCodeGenModule.h"
#include "Compiler/CISACodeGen/AdvCodeMotion.h"
#include "Compiler/CISACodeGen/RematAddressArithmetic.h"
#include "Compiler/CISACodeGen/AdvMemOpt.h"
#include "Compiler/CISACodeGen/Emu64OpsPass.h"
#include "Compiler/CISACodeGen/PullConstantHeuristics.hpp"
#include "Compiler/CISACodeGen/PushAnalysis.hpp"
#include "Compiler/CISACodeGen/ScalarizerCodeGen.hpp"
#include "Compiler/CISACodeGen/CodeSinking.hpp"
#include "Compiler/CISACodeGen/AddressArithmeticSinking.hpp"
#include "Compiler/CISACodeGen/SinkCommonOffsetFromGEP.h"
#include "Compiler/CISACodeGen/ConstantCoalescing.hpp"
#include "Compiler/CISACodeGen/CheckInstrTypes.hpp"
#include "Compiler/CISACodeGen/EstimateFunctionSize.h"
#include "Compiler/CISACodeGen/PassTimer.hpp"
#include "Compiler/CISACodeGen/FixAddrSpaceCast.h"
#include "Compiler/CISACodeGen/FixupExtractValuePair.h"
#include "Compiler/CISACodeGen/GenIRLowering.h"
#include "Compiler/CISACodeGen/GenSimplification.h"
#include "Compiler/CISACodeGen/LoopDCE.h"
#include "Compiler/CISACodeGen/LdShrink.h"
#include "Compiler/CISACodeGen/MemOpt.h"
#include "Compiler/CISACodeGen/MemOpt2.h"
#include "Compiler/CISACodeGen/PreRARematFlag.h"
#include "Compiler/CISACodeGen/PreRAScheduler.hpp"
#include "Compiler/CISACodeGen/PromoteConstantStructs.hpp"
#include "Compiler/CISACodeGen/ResolveGAS.h"
#include "Compiler/CISACodeGen/ResolvePredefinedConstant.h"
#include "Compiler/CISACodeGen/Simd32Profitability.hpp"
#include "Compiler/CISACodeGen/SimplifyConstant.h"
#include "Compiler/CISACodeGen/TimeStatsCounter.h"
#include "Compiler/CISACodeGen/TypeDemote.h"
#include "Compiler/CISACodeGen/UniformAssumptions.hpp"
#include "Compiler/CISACodeGen/VectorProcess.hpp"
#include "Compiler/CISACodeGen/RuntimeValueLegalizationPass.h"
#include "Compiler/CISACodeGen/InsertGenericPtrArithmeticMetadata.hpp"
#include "Compiler/CISACodeGen/LowerGEPForPrivMem.hpp"
#include "Compiler/CISACodeGen/POSH_RemoveNonPositionOutput.h"
#include "Compiler/CISACodeGen/RegisterEstimator.hpp"
#include "Compiler/CISACodeGen/RayTracingShaderLowering.hpp"
#include "Compiler/CISACodeGen/RayTracingStatefulPass.h"
#include "Compiler/CISACodeGen/LSCCacheOptimizationPass.h"
#include "Compiler/CISACodeGen/LSCControlsAnalysisPass.h"
#include "Compiler/ConvertMSAAPayloadTo16Bit.hpp"
#include "Compiler/MSAAInsertDiscard.hpp"
#include "Compiler/CISACodeGen/PromoteInt8Type.hpp"
#include "Compiler/CISACodeGen/PrepareLoadsStoresPass.h"
#include "Compiler/CISACodeGen/HFpackingOpt.hpp"
#include "Compiler/CISACodeGen/EvaluateFreeze.hpp"
#include "Compiler/CISACodeGen/SLMConstProp.hpp"
#include "Compiler/Optimizer/OpenCLPasses/DebuggerSupport/ImplicitGIDPass.hpp"
#include "Compiler/Optimizer/OpenCLPasses/DebuggerSupport/ImplicitGIDRestoring.hpp"
#include "Compiler/Optimizer/OpenCLPasses/GenericAddressResolution/GenericAddressDynamicResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryUsageAnalysis.hpp"
#include "Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/PrivateMemory/PrivateMemoryToSLM.hpp"
#include "Compiler/Optimizer/OpenCLPasses/ProgramScopeConstants/ProgramScopeConstantResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/WIFuncs/WIFuncResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/BreakConstantExpr/BreakConstantExpr.hpp"
#include "Compiler/Optimizer/OpenCLPasses/ReplaceUnsupportedIntrinsics/ReplaceUnsupportedIntrinsics.hpp"
#include "Compiler/Optimizer/PreCompiledFuncImport.hpp"
#include "Compiler/Optimizer/OpenCLPasses/AddressSpaceAliasAnalysis/AddressSpaceAliasAnalysis.h"
#include "Compiler/Optimizer/OpenCLPasses/UndefinedReferences/UndefinedReferencesPass.hpp"
#include "Compiler/Optimizer/OpenCLPasses/StatelessToStateful/StatelessToStateful.hpp"
#include "Compiler/Optimizer/OpenCLPasses/DisableLoopUnrollOnRetry/DisableLoopUnrollOnRetry.hpp"
#include "Compiler/Optimizer/OpenCLPasses/TransformUnmaskedFunctionsPass.h"
#include "Compiler/Optimizer/OpenCLPasses/UnreachableHandling/UnreachableHandling.hpp"
#include "Compiler/Optimizer/OpenCLPasses/WIFuncs/WIFuncResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/RegPressureLoopControl/RegPressureLoopControl.hpp"
#include "Compiler/Optimizer/MCSOptimization.hpp"
#include "Compiler/Optimizer/RectListOptimizationPass.hpp"
#include "Compiler/Optimizer/GatingSimilarSamples.hpp"
#include "Compiler/Optimizer/IntDivConstantReduction.hpp"
#include "Compiler/Optimizer/IntDivRemCombine.hpp"
#include "Compiler/Optimizer/SynchronizationObjectCoalescing.hpp"
#include "Compiler/Optimizer/RuntimeValueVectorExtractPass.h"
#include "Compiler/MetaDataApi/PurgeMetaDataUtils.hpp"
#include "Compiler/HandleLoadStoreInstructions.hpp"
#include "Compiler/CustomSafeOptPass.hpp"
#include "Compiler/CustomUnsafeOptPass.hpp"
#include "Compiler/CustomLoopOpt.hpp"
#include "Compiler/GenUpdateCB.h"
#include "Compiler/PromoteResourceToDirectAS.h"
#include "Compiler/PromoteStatelessToBindless.h"
#if defined(_DEBUG) && !defined(ANDROID)
#include "Compiler/VerificationPass.hpp"
#endif
#include "Compiler/FixInvalidFuncNamePass.hpp"
#include "Compiler/LegalizationPass.hpp"
#include "Compiler/LowPrecisionOptPass.hpp"
#include "Compiler/WorkaroundAnalysisPass.h"
#include "Compiler/MetaDataApi/MetaDataApi.h"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "Compiler/MetaDataApi/IGCMetaDataHelper.h"
#include "Compiler/CodeGenContextWrapper.hpp"
#include "Compiler/DynamicTextureFolding.h"
#include "Compiler/SampleMultiversioning.hpp"
#include "Compiler/ThreadCombining.hpp"
#include "Compiler/InitializePasses.h"
#include "Compiler/GenRotate.hpp"
#include "Compiler/Optimizer/Scalarizer.h"
#include "Compiler/RemoveCodeAssumptions.hpp"
#include "common/debug/Debug.hpp"
#include "common/igc_regkeys.hpp"
#include "common/debug/Dump.hpp"
#include "common/MemStats.h"
#include <iStdLib/utility.h>
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Config/llvm-config.h"
#include "llvm/ADT/PostOrderIterator.h"
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Analysis/CFGPrinter.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Pass.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/IPO/AlwaysInliner.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Scalar/GVN.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Analysis/ScopedNoAliasAA.h>
#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/MathExtras.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Transforms/IPO/FunctionAttrs.h>
#include <llvmWrapper/Transforms/Utils.h>
#include <llvm/Transforms/Scalar/InstSimplifyPass.h>
#include <llvmWrapper/Transforms/Scalar.h>
#include <llvmWrapper/Bitcode/BitcodeWriter.h>
#include <llvm/Transforms/InstCombine/InstCombine.h>
#include "common/LLVMWarningsPop.hpp"
#include <sstream>
#include "Compiler/CISACodeGen/PatternMatchPass.hpp"
#include "Compiler/CISACodeGen/EmitVISAPass.hpp"
#include "Compiler/CISACodeGen/CoalescingEngine.hpp"
#include "Compiler/GenTTI.h"
#include "Compiler/GenRotate.hpp"
#include "Compiler/SampleCmpToDiscard.h"
#include "Compiler/Optimizer/IGCInstCombiner/IGCInstructionCombining.hpp"
#include "DebugInfo.hpp"
#include "AdaptorCommon/RayTracing/RayTracingPasses.hpp"
#include "AdaptorCommon/RayTracing/RayTracingAddressSpaceAliasAnalysis.h"
#include "Compiler/SamplerPerfOptPass.hpp"
#include "Compiler/CISACodeGen/HalfPromotion.h"
#include "Compiler/CISACodeGen/AnnotateUniformAllocas.h"
#include "Probe/Assertion.h"
#include "Compiler/CISACodeGen/PartialEmuI64OpsPass.h"
/***********************************************************************************
This file contains the generic code generation functions for all the shaders
The class CShader is inherited for each specific type of shaders to add specific
information
************************************************************************************/
using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;
using namespace IGC::Debug;
namespace IGC
{
const int LOOP_ROTATION_HEADER_INST_THRESHOLD = 32;
const int LOOP_NUM_THRESHOLD = 2000;
const int LOOP_INST_THRESHOLD = 65000;
const int INST_THRESHOLD = 80000;
void AddAnalysisPasses(CodeGenContext& ctx, IGCPassManager& mpm)
{
COMPILER_TIME_START(&ctx, TIME_CG_Add_Analysis_Passes);
bool isOptDisabled = ctx.getModuleMetaData()->compOpt.OptDisable;
TODO("remove the following once all IGC passes are registered to PassRegistery in their constructor")
initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
mpm.add(createTimeStatsCounterPass(&ctx, TIME_CG_Analysis, STATS_COUNTER_START));
// transform pull constants and inputs into push constants and inputs
mpm.add(new PushAnalysis());
mpm.add(CreateSampleCmpToDiscardPass());
if (!isOptDisabled)
{
mpm.add(llvm::createDeadCodeEliminationPass());
}
// The 1st thing we do when getting into the IGC middle end is to split critical-edges:
// PushAnalysis requires WIAnalysis
// WIAnalysis requires dominator and post-dominator analysis
// WIAnalysis also requires BreakCriticalEdge because it assumes that
// potential phi-moves will be placed at those blocks
mpm.add(llvm::createBreakCriticalEdgesPass());
if (IGC_IS_FLAG_DISABLED(DisableMemOpt2) &&
(ctx.type == ShaderType::COMPUTE_SHADER || (ctx.m_DriverInfo.WAEnableMemOpt2ForOCL())) &&
!isOptDisabled)
{
mpm.add(createMemOpt2Pass(16));
}
// only limited code-sinking to several shader-type
// vs input has the URB-reuse issue to be resolved.
// Also need to understand the performance benefit better.
mpm.add(new CodeSinking(true));
// Run flag re-materialization if it's beneficial.
if (ctx.m_DriverInfo.benefitFromPreRARematFlag() &&
IGC_IS_FLAG_ENABLED(EnablePreRARematFlag)) {
mpm.add(createPreRARematFlagPass());
}
// Peephole framework for generic type legalization
mpm.add(new Legalizer::PeepholeTypeLegalizer());
if (IGC_IS_FLAG_ENABLED(ForcePromoteI8) ||
(IGC_IS_FLAG_ENABLED(EnablePromoteI8) && !ctx.platform.supportByteALUOperation()))
{
mpm.add(createPromoteInt8TypePass());
}
// need this before WIAnalysis:
// insert phi to prevent changing of WIAnalysis result by later code-motion
mpm.add(llvm::createLCSSAPass());
// Fixup extract value pairs.
mpm.add(createExtractValuePairFixupPass());
if (IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_ENABLED(LateInlineUnmaskedFunc))
{
mpm.add(new InlineUnmaskedFunctionsPass());
// Newly created memcpy intrinsic are lowered
mpm.add(createReplaceUnsupportedIntrinsicsPass());
// Split complex constant expression into 2 simple ones
mpm.add(new BreakConstantExpr());
// Expand newly created allocas
mpm.add(createSROAPass());
// Run legalization pass to expand non-supported instructions
// like shufflevector. The code below is just copied and
// pasted as is.
bool preserveNan = !ctx.getCompilerOption().NoNaNs;
mpm.add(new Legalization(preserveNan));
// Some clean up passes.
mpm.add(llvm::createEarlyCSEPass());
mpm.add(new BreakConstantExpr());
mpm.add(llvm::createCFGSimplificationPass());
mpm.add(createDeadCodeEliminationPass());
// Create functions groups after unmasked functions inlining
mpm.add(createGenXCodeGenModulePass());
// Allocate non-primitive allocas. These peace of code is copied
if (ctx.m_instrTypes.hasNonPrimitiveAlloca)
{
mpm.add(createBreakCriticalEdgesPass());
mpm.add(createAnnotateUniformAllocasPass());
if (IGC_IS_FLAG_DISABLED(DisablePromotePrivMem) &&
ctx.m_retryManager.AllowPromotePrivateMemory())
{
mpm.add(createPromotePrivateArrayToReg());
mpm.add(createCFGSimplificationPass());
}
}
mpm.add(createPromoteMemoryToRegisterPass());
// Resolving private memory allocas
mpm.add(CreatePrivateMemoryResolution());
}
// Evaluates LLVM 10+ freeze instructions so EmitPass does not need to handle them
mpm.add(createEvaluateFreezePass());
// clean up constexpressions after EarlyCSE
mpm.add( new BreakConstantExpr() );
// This is for dumping register pressure info
if (IGC_IS_FLAG_ENABLED(ForceRPE)) {
mpm.add(new RegisterEstimator());
}
mpm.add(createFixInvalidFuncNamePass());
// collect stats after all the optimization. This info can be dumped to the cos file
mpm.add(new CheckInstrTypes(&(ctx.m_instrTypesAfterOpts), nullptr));
//
// Generally, passes that change IR should be prior to this place!
//
// let CleanPHINode be right before Layout
mpm.add(createCleanPHINodePass());
// Let Layout be the last pass before Emit Pass
mpm.add(new Layout());
mpm.add(createTimeStatsCounterPass(&ctx, TIME_CG_Analysis, STATS_COUNTER_END));
COMPILER_TIME_END(&ctx, TIME_CG_Add_Analysis_Passes);
} // AddAnalysisPasses
static void UpdateInstTypeHint(CodeGenContext& ctx)
{
// WA: save original values as preRA heuristic is based on those
// we need to fix the preRA pass heuristic or get rid of preRA pass altogether
unsigned int numBB = ctx.m_instrTypes.numBB;
unsigned int numSample = ctx.m_instrTypes.numSample;
unsigned int numInsts = ctx.m_instrTypes.numInsts;
bool hasUnmaskedRegion = ctx.m_instrTypes.hasUnmaskedRegion;
IGCPassManager mpm(&ctx, "UpdateOptPre");
mpm.add(new CheckInstrTypes(&(ctx.m_instrTypes), nullptr));
mpm.run(*ctx.getModule());
ctx.m_instrTypes.numBB = numBB;
ctx.m_instrTypes.numSample = numSample;
ctx.m_instrTypes.numInsts = numInsts;
ctx.m_instrTypes.hasLoadStore = true;
ctx.m_instrTypes.hasUnmaskedRegion = hasUnmaskedRegion;
}
// forward declaration
llvm::ModulePass* createPruneUnusedArgumentsPass();
void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSignature* pSignature)
{
COMPILER_TIME_START(&ctx, TIME_CG_Add_Legalization_Passes);
mpm.add(createTimeStatsCounterPass(&ctx, TIME_CG_Legalization, STATS_COUNTER_START));
// update type of instructions to know what passes are needed.
UpdateInstTypeHint(ctx);
// check again after full inlining if subroutines are still present
ctx.CheckEnableSubroutine(*ctx.getModule());
MetaDataUtils* pMdUtils = ctx.getMetaDataUtils();
bool isOptDisabled = ctx.getModuleMetaData()->compOpt.OptDisable;
bool fastCompile = ctx.getModuleMetaData()->compOpt.FastCompilation;
bool highAllocaPressure = ctx.m_instrTypes.numAllocaInsts > IGC_GET_FLAG_VALUE(AllocaRAPressureThreshold);
bool isPotentialHPCKernel = (ctx.m_instrTypes.numInsts > IGC_GET_FLAG_VALUE(HPCInstNumThreshold)) ||
(ctx.m_instrTypes.numGlobalInsts > IGC_GET_FLAG_VALUE(HPCGlobalInstNumThreshold)) || IGC_GET_FLAG_VALUE(HPCFastCompilation);
highAllocaPressure = IGC_GET_FLAG_VALUE(DisableFastRAWA) ? false : highAllocaPressure;
isPotentialHPCKernel = IGC_GET_FLAG_VALUE(DisableFastRAWA) ? false : isPotentialHPCKernel;
if (highAllocaPressure || isPotentialHPCKernel)
{
IGC_SET_FLAG_VALUE(FastCompileRA, 1);
IGC_SET_FLAG_VALUE(HybridRAWithSpill, 1);
}
// In case of presence of Unmasked regions disable loop invariant motion after
// Unmasked functions are inlined at the end of optimization phase
if (IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_DISABLED(LateInlineUnmaskedFunc) &&
ctx.m_instrTypes.hasUnmaskedRegion) {
IGC_SET_FLAG_VALUE(allowLICM, false);
}
if (IGC_IS_FLAG_ENABLED(ForceAllPrivateMemoryToSLM) ||
IGC_IS_FLAG_ENABLED(ForcePrivateMemoryToSLMOnBuffers))
{
DummyPass* dummypass = new DummyPass();
TargetIRAnalysis GenTTgetIIRAnalysis([&](const Function& F) {
GenIntrinsicsTTIImpl GTTI(&ctx, dummypass);
return TargetTransformInfo(GTTI);
});
mpm.add(new TargetTransformInfoWrapperPass(GenTTgetIIRAnalysis));
}
// Disable all target library functions.
// right now we don't support any standard function in the code gen
// maybe we want to support some at some point to take advantage of LLVM optimizations
TargetLibraryInfoImpl TLI;
TLI.disableAllFunctions();
mpm.add(new llvm::TargetLibraryInfoWrapperPass(TLI));
// Add Metadata API immutable pass
mpm.add(new MetaDataUtilsWrapper(pMdUtils, ctx.getModuleMetaData()));
// Add CodeGen Context Wrapper immutable pass
mpm.add(new CodeGenContextWrapper(&ctx));
//Add alias analysis pass
mpm.add(createAddressSpaceAAWrapperPass());
if (ctx.type == ShaderType::RAYTRACING_SHADER || ctx.hasSyncRTCalls())
{
if (IGC_IS_FLAG_DISABLED(DisableRTAliasAnalysis))
mpm.add(createRayTracingAddressSpaceAAWrapperPass());
}
mpm.add(createExternalAAWrapperPass(&addAddressSpaceAAResult));
mpm.add(createScopedNoAliasAAWrapperPass());
TODO("remove the following once all IGC passes are registered to PassRegistery in their constructor")
initializeWIAnalysisPass(*PassRegistry::getPassRegistry());
initializeSimd32ProfitabilityAnalysisPass(*PassRegistry::getPassRegistry());
initializeGenXFunctionGroupAnalysisPass(*PassRegistry::getPassRegistry());
if (ctx.m_threadCombiningOptDone)
{
mpm.add(createLoopCanonicalization());
mpm.add(llvm::createLoopDeletionPass());
mpm.add(llvm::createBreakCriticalEdgesPass());
mpm.add(llvm::createLoopRotatePass(LOOP_ROTATION_HEADER_INST_THRESHOLD));
mpm.add(llvm::createLowerSwitchPass());
int LoopUnrollThreshold = ctx.m_DriverInfo.GetLoopUnrollThreshold();
if (LoopUnrollThreshold > 0 && (ctx.m_tempCount < 64))
{
mpm.add(IGCLLVM::createLoopUnrollPass(2, LoopUnrollThreshold, -1, 1));
}
mpm.add(createBarrierNoopPass());
if (ctx.m_retryManager.AllowLICM() && IGC_IS_FLAG_ENABLED(allowLICM))
{
mpm.add(llvm::createLICMPass());
}
mpm.add(llvm::createLoopSimplifyPass());
}
// Lower/Resolve OCL inlined constants.
if (ctx.m_DriverInfo.NeedLoweringInlinedConstants()) {
// Run additional constant breaking which is assumed by the constant
// resolver.
mpm.add(new BreakConstantExpr());
mpm.add(new ProgramScopeConstantResolution());
}
bool needDPEmu = (IGC_IS_FLAG_ENABLED(ForceDPEmulation) ||
(ctx.m_DriverInfo.NeedFP64(ctx.platform.getPlatformInfo().eProductFamily) && ctx.platform.hasNoFP64Inst()));
bool hasDPDivSqrtEmu = !ctx.platform.hasNoFP64Inst() && !ctx.platform.hasCorrectlyRoundedMacros() && ctx.m_DriverInfo.NeedFP64DivSqrt();
uint32_t theEmuKind = (needDPEmu ? EmuKind::EMU_DP : 0);
theEmuKind |= (hasDPDivSqrtEmu ? EmuKind::EMU_DP_DIV_SQRT : 0);
theEmuKind |= (ctx.m_DriverInfo.NeedI64BitDivRem() ? EmuKind::EMU_I64DIVREM : 0);
theEmuKind |=
((IGC_IS_FLAG_ENABLED(ForceSPDivEmulation) ||
(ctx.m_DriverInfo.NeedIEEESPDiv() && !ctx.platform.hasCorrectlyRoundedMacros()))
? EmuKind::EMU_SP_DIV : 0);
if (ctx.platform.preferFP32Emu() && IGC_IS_FLAG_DISABLED(Force32BitIntDivRemEmu)) {
// Prefer using FP32 emulation even though DP support is available
theEmuKind |= EmuKind::EMU_I32DIVREM_SP;
}
else if (!ctx.platform.hasNoFP64Inst() &&
(IGC_IS_FLAG_ENABLED(Force32BitIntDivRemEmu) ||
ctx.getCompilerOption().ForceInt32DivRemEmu ||
(ctx.platform.Enable32BitIntDivRemEmu() &&
!ctx.getCompilerOption().ForceInt32DivRemEmuSP &&
IGC_IS_FLAG_DISABLED(Force32BitIntDivRemEmuSP))))
{
// Use DP (and float) opeations to emulate int32 div/rem
theEmuKind |= EmuKind::EMU_I32DIVREM;
}
else if (ctx.platform.Enable32BitIntDivRemEmu() ||
ctx.getCompilerOption().ForceInt32DivRemEmuSP ||
IGC_IS_FLAG_ENABLED(Force32BitIntDivRemEmuSP))
{
// Use SP floating operations to emulate int32 div/rem
theEmuKind |= EmuKind::EMU_I32DIVREM_SP;
}
if (IGC_IS_FLAG_ENABLED(RayTracingKeepUDivRemWA))
{
theEmuKind &= ~EmuKind::EMU_I32DIVREM;
theEmuKind &= ~EmuKind::EMU_I32DIVREM_SP;
}
if (theEmuKind > 0 || IGC_IS_FLAG_ENABLED(EnableTestIGCBuiltin))
{
// Need to break constant expr as PreCompiledFuncImport does not handle it.
mpm.add(new BreakConstantExpr());
mpm.add(new PreCompiledFuncImport(&ctx, theEmuKind));
mpm.add(createAlwaysInlinerLegacyPass());
// Using DCE here as AlwaysInliner does not completely remove dead functions.
// Once AlwaysInliner can delete all of them, this DCE is no longer needed.
// mpm.add(createDeadCodeEliminationPass());
//
// DCE doesn't remove dead control flow; ADCE does (currently)
// otherwise you'd have to call createCFGSimplificationPass and DCE
// iteratively e.g..
mpm.add(llvm::createAggressiveDCEPass());
// TODO: we probably should be running other passes on the result
if (!IGC::ForceAlwaysInline(&ctx))
{
mpm.add(new PurgeMetaDataUtils());
}
}
// Find rotate pattern.
// Invoked after DP emulation so that it'd handle emulation functions.
if (ctx.platform.supportRotateInstruction()) {
mpm.add(createGenRotatePass());
}
mpm.add(createReplaceUnsupportedIntrinsicsPass());
if (IGC_IS_FLAG_DISABLED(DisablePromoteToDirectAS) &&
!ctx.getModuleMetaData()->compOpt.IsLibraryCompilation)
{
// Promotes indirect resource access to direct
mpm.add(new BreakConstantExpr());
mpm.add(new PromoteResourceToDirectAS());
}
if (ctx.m_instrTypes.hasReadOnlyArray)
{
mpm.add(createDeadCodeEliminationPass());
mpm.add(createSROAPass());
}
if (ctx.m_instrTypes.hasGenericAddressSpacePointers)
{
if (IGC_IS_FLAG_ENABLED(EnableGASResolver))
{
mpm.add(createSROAPass());
mpm.add(createFixAddrSpaceCastPass());
mpm.add(createResolveGASPass());
}
mpm.add(createGenericAddressDynamicResolutionPass());
}
// Resolve the Private memory to register pass
if (!isOptDisabled)
{
// In case of late inlining of Unmasked function allocate non
// primitive Allocas after inlining is done. Otherwise there
// is possibility RegAlloc cannot allocate registers for all
// virtual registers. This piece of code is copied at the place
// where inlining is done.
if (ctx.m_instrTypes.hasNonPrimitiveAlloca &&
!(IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_ENABLED(LateInlineUnmaskedFunc)))
{
mpm.add(createBreakCriticalEdgesPass());
mpm.add(createAnnotateUniformAllocasPass());
if (IGC_IS_FLAG_DISABLED(DisablePromotePrivMem) &&
ctx.m_retryManager.AllowPromotePrivateMemory())
{
mpm.add(createPromotePrivateArrayToReg());
mpm.add(createCFGSimplificationPass());
}
}
mpm.add(createPromoteMemoryToRegisterPass());
}
else
{
if (IGC_IS_FLAG_ENABLED(AllowMem2Reg))
mpm.add(createPromoteMemoryToRegisterPass());
}
if (ctx.type == ShaderType::OPENCL_SHADER ||
ctx.type == ShaderType::COMPUTE_SHADER)
{
if (IGC_IS_FLAG_ENABLED(ForceAllPrivateMemoryToSLM))
{
mpm.add(new PrivateMemoryToSLM(
IGC_IS_FLAG_ENABLED(EnableOptReportPrivateMemoryToSLM)));
mpm.add(createInferAddressSpacesPass());
}
else if (IGC_IS_FLAG_ENABLED(ForcePrivateMemoryToSLMOnBuffers))
{
std::string forcedBuffers(
IGC_GET_REGKEYSTRING(ForcePrivateMemoryToSLMOnBuffers));
mpm.add(new PrivateMemoryToSLM(
forcedBuffers,
IGC_IS_FLAG_ENABLED(EnableOptReportPrivateMemoryToSLM)));
mpm.add(createInferAddressSpacesPass());
}
}
if (ctx.m_instrTypes.numOfLoop)
{
// need to run loop simplify to canonicalize loop and merge latches
mpm.add(createLoopCanonicalization());
mpm.add(createLoopSimplifyPass());
}
if (ctx.enableFunctionCall() || ctx.type == ShaderType::RAYTRACING_SHADER)
{
// Sort functions if subroutine/indirect fcall is enabled.
mpm.add(llvm::createGlobalDCEPass());
mpm.add(new PurgeMetaDataUtils());
mpm.add(createGenXCodeGenModulePass());
}
// Remove all uses of implicit arg instrinsics after inlining by lowering them to kernel args
mpm.add(new LowerImplicitArgIntrinsics());
// Resolving private memory allocas
// In case of late inlining of Unmasked function postpone memory
// resolution till inlining is done as during inlining new Allocas
// are created.
if (!(IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_ENABLED(LateInlineUnmaskedFunc)))
{
mpm.add(CreatePrivateMemoryResolution());
}
// Should help MemOpt pass to merge more loads
mpm.add(createSinkCommonOffsetFromGEPPass());
// Run MemOpt
if (!isOptDisabled &&
ctx.m_instrTypes.hasLoadStore && IGC_IS_FLAG_DISABLED(DisableMemOpt) && !ctx.getModuleMetaData()->disableMemOptforNegativeOffsetLoads) {
if ((ctx.type == ShaderType::RAYTRACING_SHADER || ctx.hasSyncRTCalls()) &&
IGC_IS_FLAG_DISABLED(DisablePrepareLoadsStores))
{
mpm.add(createPrepareLoadsStoresPass());
}
// run AdvMemOpt and MemOPt back-to-back so that we only
// need to run WIAnalysis once
if (IGC_IS_FLAG_ENABLED(EnableAdvMemOpt))
mpm.add(createAdvMemOptPass());
bool AllowNegativeSymPtrsForLoad =
ctx.type == ShaderType::RAYTRACING_SHADER ||
ctx.type == ShaderType::OPENCL_SHADER;
bool AllowVector8LoadStore =
IGC_IS_FLAG_ENABLED(EnableVector8LoadStore) ||
((ctx.type == ShaderType::RAYTRACING_SHADER || ctx.hasSyncRTCalls()) && ctx.platform.supports8DWLSCMessage());
mpm.add(createMemOptPass(AllowNegativeSymPtrsForLoad, AllowVector8LoadStore));
if (ctx.type == ShaderType::RAYTRACING_SHADER &&
static_cast<RayDispatchShaderContext&>(ctx).doSpillWidening())
{
mpm.add(createRTSpillShrinkPass());
mpm.add(createMemOptPass(AllowNegativeSymPtrsForLoad, AllowVector8LoadStore));
}
if ((ctx.type == ShaderType::RAYTRACING_SHADER || ctx.hasSyncRTCalls())
&& IGC_IS_FLAG_ENABLED(EnableLSCCacheOptimization))
{
// Optimize store instructions for utilizing the LSC-L1 cache.
// This only runs for shaders with raytracing functionality.
mpm.add(createLSCCacheOptimizationPass());
}
mpm.add(createIGCInstructionCombiningPass());
if (ctx.type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext&>(ctx).m_InternalOptions.KernelDebugEnable)
{
mpm.add(new ImplicitGIDRestoring());
}
}
if (ctx.type == ShaderType::RAYTRACING_SHADER)
{
if (IGC_IS_FLAG_ENABLED(EnableStackIDReleaseScheduling))
mpm.add(createStackIDSchedulingPass());
// This will help eliminate some redundant loads in some cases. Need
// to run this before create ldraw*/storeraw* intrinsics for raytracing
// memory.
mpm.add(createEarlyCSEPass());
if (IGC_IS_FLAG_DISABLED(DisableRTMemDSE))
mpm.add(createRayTracingMemDSEPass());
// Convert load/store to ldraw*/storeraw*
mpm.add(createRaytracingStatefulPass());
if (IGC_IS_FLAG_DISABLED(DisableRayTracingConstantCoalescing))
{
// block load RTGlobals
mpm.add(createRayTracingConstantCoalescingPass());
}
// lift raygen shader global and local pointer to inline data access.
mpm.add(CreateBindlessInlineDataPass());
}
else if (ctx.hasSyncRTCalls())
{
mpm.add(createRaytracingStatefulPass());
}
if (ctx.type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext&>(ctx).
m_InternalOptions.PromoteStatelessToBindless &&
(static_cast<OpenCLProgramContext&>(ctx).
m_InternalOptions.UseBindlessLegacyMode ||
!ctx.getModuleMetaData()->compOpt.GreaterThan4GBBufferRequired)
)
{
mpm.add(new PromoteStatelessToBindless());
}
if (!isOptDisabled &&
ctx.m_instrTypes.hasLoadStore &&
ctx.m_DriverInfo.SupportsStatelessToStatefulBufferTransformation() &&
!ctx.getModuleMetaData()->compOpt.GreaterThan4GBBufferRequired &&
IGC_IS_FLAG_ENABLED(EnableStatelessToStateful) &&
!ctx.m_instrTypes.hasInlineAsmPointerAccess)
{
bool hasBufOff = (IGC_IS_FLAG_ENABLED(EnableSupportBufferOffset) ||
ctx.getModuleMetaData()->compOpt.HasBufferOffsetArg);
mpm.add(new StatelessToStateful(hasBufOff));
}
// Light cleanup for subroutines after cloning. Note that the constant
// propogation order is reversed, compared to the opt sequence in
// OptimizeIR. There is a substantial gain with CFG simplification after
// interprocedural constant propagation.
if (ctx.m_enableSubroutine && !isOptDisabled)
{
mpm.add(createPruneUnusedArgumentsPass());
#if LLVM_VERSION_MAJOR >= 12
mpm.add(createIPSCCPPass());
#else
if (!ctx.m_hasStackCalls)
{
// Don't run IPConstantProp when stackcalls are present.
// Let global constants be relocated inside stack funcs.
// We cannot process SLM constants inside stackcalls, so don't propagate them.
mpm.add(createIPConstantPropagationPass());
}
mpm.add(createConstantPropagationPass());
#endif
mpm.add(createDeadCodeEliminationPass());
mpm.add(createCFGSimplificationPass());
}
// Since we don't support switch statements, switch lowering is needed after the last CFG simplication
mpm.add(llvm::createLowerSwitchPass());
// There's no particular reason for this exact place, but it should be after LowerGEPForPrivMem
if (IGC_IS_FLAG_ENABLED(EnableSplitIndirectEEtoSel))
{
mpm.add(createSplitIndirectEEtoSelPass());
}
// This pass can create constant expression
if (ctx.m_DriverInfo.HasDoubleLoadStore())
{
mpm.add(new HandleLoadStoreInstructions());
}
// Split big vector & 3-element load/store, etc.
mpm.add(createVectorPreProcessPass());
// Create Gen IR lowering.
// To replace SLM pointer if they are constants, break constants first.
if (ctx.m_instrTypes.hasLocalLoadStore) {
mpm.add(new BreakConstantExpr());
}
bool KeepGEPs;
// In case of late inlining of Unmasked function postpone memory
// resolution till inlining is done as during inlining new Allocas
// are created.
if (IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_ENABLED(LateInlineUnmaskedFunc))
{
KeepGEPs = true;
}
else
{
KeepGEPs = false;
}
mpm.add(createGenIRLowerPass());
if (KeepGEPs)
{
mpm.add(createSeparateConstOffsetFromGEPPass());
}
else
{
// Also break and lower GEP constexpr.
mpm.add(new BreakConstantExpr());
mpm.add(createGEPLoweringPass());
}
mpm.add(new WorkaroundAnalysis());
if (!isOptDisabled) {
// Removing code assumptions can enable some InstructionCombining optimizations.
// Last instruction combining pass needs to be before Legalization pass, as it can produce illegal instructions.
mpm.add(new RemoveCodeAssumptions());
mpm.add(createIGCInstructionCombiningPass());
}
if (!isOptDisabled)
{
// Optimize lower-level IR
if (!fastCompile && !highAllocaPressure && !isPotentialHPCKernel)
{
mpm.add(createIGCInstructionCombiningPass());
if (ctx.type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext&>(ctx).m_InternalOptions.KernelDebugEnable)
{
mpm.add(new ImplicitGIDRestoring());
}
}
mpm.add(new GenSpecificPattern());
// Cases with DPDivSqrtEmu grow significantly.
// We can disable EarlyCSE when m_hasDPDivSqrtEmu is true,
// what causes the values will have shorter lifetime and we can avoid spills.
if (!fastCompile && !highAllocaPressure && !isPotentialHPCKernel && !ctx.m_hasDPDivSqrtEmu)
{
mpm.add(createEarlyCSEPass());
}
else if (highAllocaPressure || isPotentialHPCKernel)
{
mpm.add(createSinkingPass());
}
if (!fastCompile && !highAllocaPressure && !isPotentialHPCKernel &&
IGC_IS_FLAG_ENABLED(allowLICM) && ctx.m_retryManager.AllowLICM())
{
mpm.add(createLICMPass());
if (ctx.type == ShaderType::OPENCL_SHADER ||
ctx.type == ShaderType::COMPUTE_SHADER)
{
mpm.add(new RegPressureLoopControl());
}
}
mpm.add(createAggressiveDCEPass());
// As DPC++ FE apply LICM we cannot reduce register pressure just
// by turning off LICM at IGC in some cases so apply sinking address arithmetic
if (ctx.m_retryManager.AllowAddressArithmeticSinking() &&
ctx.type == ShaderType::OPENCL_SHADER)
{
mpm.add(new AddressArithmeticSinking());
}
}
// Enabling half promotion AIL for compute shaders only at this point.
// If needed ctx.type check can be removed to apply for all shader types
if (IGC_IS_FLAG_ENABLED(ForceHalfPromotion) ||
(ctx.getModuleMetaData()->compOpt.WaForceHalfPromotion && ctx.type == ShaderType::COMPUTE_SHADER) ||
(!ctx.platform.supportFP16() && IGC_IS_FLAG_ENABLED(EnableHalfPromotion)))
{
mpm.add(new HalfPromotion());
mpm.add(createGVNPass());
mpm.add(createDeadCodeEliminationPass());
}
// Run address remat after GVN as it may hoist address calculations and
// create PHI nodes with addresses.
mpm.add(createRematAddressArithmeticPass());
// Run type demotion if it's beneficial.
if (ctx.m_DriverInfo.benefitFromTypeDemotion() &&
IGC_IS_FLAG_ENABLED(EnableTypeDemotion)) {
mpm.add(createTypeDemotePass());
}
// Do Genx strengthreduction (do things like fdiv -> inv + mul)
if (!isOptDisabled)
{
mpm.add(createGenStrengthReductionPass());
mpm.add(createVectorBitCastOptPass());
}
if (ctx.m_instrTypes.hasUniformAssumptions) {
mpm.add(new UniformAssumptions());
}
// NanHandlingPass need to be before Legalization since it might make
// some changes and require Legalization to "legalize"
if (IGC_IS_FLAG_DISABLED(DisableBranchSwaping) && ctx.m_DriverInfo.BranchSwapping())
{
mpm.add(createNanHandlingPass());
}
// TODO: move to use instruction flags
// to figure out if we need to preserve Nan
bool preserveNan = !ctx.getCompilerOption().NoNaNs;
// Legalizer does not handle constant expressions
mpm.add(new BreakConstantExpr());
mpm.add(new Legalization(preserveNan));
// Scalarizer in codegen to handle the vector instructions
mpm.add(new ScalarizerCodeGen());
// coalesce scalar loads into loads of larger quantity.
// This require and preserves uniform analysis we should keep
// other passes using uniformness together to avoid re-running it several times
if (IGC_IS_FLAG_DISABLED(DisableConstantCoalescing) && ctx.m_retryManager.AllowConstantCoalescing() && !ctx.getModuleMetaData()->compOpt.DisableConstantCoalescing)
{
mpm.add(createBreakCriticalEdgesPass());
mpm.add(new ConstantCoalescing());
}
if (ctx.type == ShaderType::RAYTRACING_SHADER || ctx.hasSyncRTCalls())
{
if (IGC_IS_FLAG_DISABLED(DisableLSCControlsForRayTracing))
mpm.add(CreateLSCControlsAnalysisPass());
// We do raytracing lowering a little earlier than the others here
// to take advantage of the instruction simplification below.
mpm.add(CreateRayTracingShaderLowering());
}
if (ctx.type == ShaderType::RAYTRACING_SHADER)
{
if (ctx.platform.WaPredicatedStackIDRelease())
mpm.add(createRayTracingPredicatedStackIDReleasePass());
if (IGC_IS_FLAG_DISABLED(DisableRTFenceElision))
mpm.add(createSynchronizationObjectCoalescing());
}
// Instruction combining may merge instruction back into unsupported intrinsics.
// Therefore last Replace Unsupported Intrinsics Pass must be after last
// Instruction combining pass.
// Replace Unsupported Intrinsics Pass may generate new 64 bit operations.
// Therefore last 64bit emulation pass must be after the last Replace Unsupported Intrinsics Pass.
mpm.add(createReplaceUnsupportedIntrinsicsPass());
// When needDPEmu is true, enable Emu64Ops as well for now until
// DPEmu is able to get rid of all 64bit integer ops fully.
if ((needDPEmu && IGC_IS_FLAG_ENABLED(DPEmuNeedI64Emu)) ||
(ctx.m_DriverInfo.Enable64BitEmu() &&
(IGC_GET_FLAG_VALUE(Enable64BitEmulation) ||
(IGC_GET_FLAG_VALUE(Enable64BitEmulationOnSelectedPlatform) &&
ctx.platform.need64BitEmulation()))) ||
ctx.platform.hasPartialInt64Support()
)
{
mpm.add(new BreakConstantExpr());
// Emu64OpsPass requires that we are working on legal types, specifically
// that i128 uses are expanded to i64. This is why we need to run PeepholeTypeLegalizer
// beforehand.
mpm.add(new Legalizer::PeepholeTypeLegalizer());
// Lower all GEPs now as Emu64 doesn't know how to handle them.
if (KeepGEPs)
{
mpm.add(createGEPLoweringPass());
mpm.add(llvm::createEarlyCSEPass());
}
// Run dead code elimination pass right before Emu64OpsPass,
// as legalization passes do not always clear unused (operating
// on illegal types) instructions.
mpm.add(llvm::createDeadCodeEliminationPass());
if (ctx.platform.hasPartialEmuI64Enabled())
{
mpm.add(createPartialEmuI64OpsPass());
}
else
{
mpm.add(createEmu64OpsPass());
}
ctx.m_hasEmu64BitInsts = true;
if (!isOptDisabled)
{
mpm.add(new GenSpecificPattern());
}
}
if (ctx.m_instrTypes.hasRuntimeValueVector)
{
// Legalize RuntimeValue calls for push analysis
mpm.add(new RuntimeValueLegalizationPass());
}
mpm.add(createInstSimplifyLegacyPass());
// This pass inserts bitcasts for vector loads/stores.
// This pass could be moved further toward EmitPass.
mpm.add(createVectorProcessPass());
// handling constant expressions created by vectorProcess pass
mpm.add(new BreakConstantExpr());
mpm.add(new LowPrecisionOpt());
mpm.add(new WAFMinFMax());
// Preferred to be added after llvm instruction combining, otherwise 'generic.arith'
// metadata may get lost during optimizations.
mpm.add(new InsertGenericPtrArithmeticMetadata());
mpm.add(createTimeStatsCounterPass(&ctx, TIME_CG_Legalization, STATS_COUNTER_END));
COMPILER_TIME_END(&ctx, TIME_CG_Add_Legalization_Passes);
} // AddLegalizationPasses
void AddCodeGenPasses(
CodeGenContext& ctx,
CShaderProgram::KernelShaderMap& shaders,
IGCPassManager& Passes,
SIMDMode simdMode,
bool canAbortOnSpill,
ShaderDispatchMode shaderMode,
PSSignature* pSignature)
{
// Generate CISA
COMPILER_TIME_START(&ctx, TIME_CG_Add_CodeGen_Passes);
Passes.add(new EmitPass(shaders, simdMode, canAbortOnSpill, shaderMode, pSignature));
COMPILER_TIME_END(&ctx, TIME_CG_Add_CodeGen_Passes);
}
// check based on performance measures.
bool SimdEarlyCheck(CodeGenContext* ctx)
{
if (ctx->m_sampler < 11 || ctx->m_inputCount < 16 || ctx->m_tempCount < 40 || ctx->m_dxbcCount < 280 || ctx->m_ConstantBufferCount < 500)
{
if (ctx->m_tempCount < 90 && ctx->m_ConstantBufferCount < 210)
{
return true;
}
}
return false;
}
bool ForceSimdWA(ComputeShaderContext& ctx, SIMDMode & forceSimd, SIMDMode minSimdMode, SIMDMode maxSimdMode)
{
// WA for better utilization of SIMD lanes
if (ctx.platform.needsWAForThreadsUtilization() &&
ctx.getModuleMetaData()->csInfo.waveSize == 0)
{
unsigned sizeX = ctx.GetThreadGroupSizeX();
unsigned sizeY = ctx.GetThreadGroupSizeY();
unsigned sizeZ = ctx.GetThreadGroupSizeZ();
// Force SIMD8 on thread group size 16x1x1
if (sizeX == 16 && sizeY == 1 && sizeZ == 1 &&
minSimdMode >= SIMDMode::SIMD8)
{
forceSimd = SIMDMode::SIMD8;
return true;
}
// Force SIMD16 or lower on thread group size 32x1x1
else if (sizeX == 32 && sizeY == 1 && sizeZ == 1 &&
minSimdMode <= SIMDMode::SIMD16 &&
maxSimdMode >= SIMDMode::SIMD16)
{
forceSimd = SIMDMode::SIMD16;
return true;
}
}
forceSimd = SIMDMode::UNKNOWN;
return false;
}
void destroyShaderMap(CShaderProgram::KernelShaderMap& shaders)
{
for (auto i : shaders)
{
CShaderProgram* shader = i.second;
COMPILER_SHADER_STATS_PRINT(shader->m_shaderStats,
shader->GetContext()->type, shader->GetContext()->hash, "");
COMPILER_SHADER_STATS_SUM(shader->GetContext()->m_sumShaderStats,
shader->m_shaderStats, shader->GetContext()->type);
COMPILER_SHADER_STATS_DEL(shader->m_shaderStats);
delete shader;
}
}
void unify_opt_PreProcess(CodeGenContext* pContext)
{
TODO("hasBuiltin should be calculated based on module");
if (IGC_IS_FLAG_ENABLED(DisableLLVMGenericOptimizations))
{
pContext->getModuleMetaData()->compOpt.OptDisable = true;
}
if (IGC_GET_FLAG_VALUE(StripDebugInfo) == FLAG_DEBUG_INFO_STRIP_ALL)
{
StripDebugInfo(*pContext->getModule());
}
else if (IGC_GET_FLAG_VALUE(StripDebugInfo) == FLAG_DEBUG_INFO_STRIP_NONLINE)
{
stripNonLineTableDebugInfo(*pContext->getModule());
}
IGCPassManager mpm(pContext, "OPTPre");
mpm.add(new CheckInstrTypes(&(pContext->m_instrTypes), &pContext->metrics));
if (pContext->isPOSH())
{
mpm.add(createRemoveNonPositionOutputPass());
}
mpm.run(*pContext->getModule());
// If the module does not contain called function declaration,
// indirect calls are the only way to detect function pointers usage.
if (pContext->m_instrTypes.hasIndirectCall)
pContext->m_enableFunctionPointer = true;
if (pContext->getMetaDataUtils()->size_FunctionsInfo() == 1 &&
!pContext->m_instrTypes.hasSubroutines)
{
pContext->m_instrTypes.numBB =
pContext->getMetaDataUtils()->begin_FunctionsInfo()->first->getBasicBlockList().size();
pContext->m_instrTypes.hasMultipleBB = (pContext->m_instrTypes.numBB != 1);
}
else
{
pContext->m_instrTypes.hasMultipleBB = true;
}
pContext->m_instrTypes.hasLoadStore = true;
pContext->m_instrTypes.CorrelatedValuePropagationEnable =
(pContext->m_instrTypes.hasMultipleBB &&
(pContext->m_instrTypes.hasSel ||
pContext->m_instrTypes.hasCmp ||
pContext->m_instrTypes.hasSwitch ||
pContext->m_instrTypes.hasLoadStore));
}
static bool extensiveShader(CodeGenContext* pContext)
{
return (pContext->type == ShaderType::OPENCL_SHADER &&
pContext->m_instrTypes.numInsts > INST_THRESHOLD &&
pContext->m_instrTypes.numLoopInsts > LOOP_INST_THRESHOLD &&
pContext->m_instrTypes.numOfLoop > LOOP_NUM_THRESHOLD &&
pContext->m_instrTypes.numBB == 0 &&
pContext->m_instrTypes.numSample == 0 &&
pContext->m_instrTypes.hasSubroutines);
}
// When we do not run optimizations, we still need to run always inline
// pass, otherwise codegen will fail.
static void alwaysInlineForNoOpt(CodeGenContext* pContext, bool NoOpt)
{
if (NoOpt)
{
MetaDataUtils* pMdUtils = pContext->getMetaDataUtils();
IGCPassManager mpm(pContext, "OPTPost");
mpm.add(new MetaDataUtilsWrapper(pMdUtils, pContext->getModuleMetaData()));
mpm.add(new CodeGenContextWrapper(pContext));
mpm.add(createAlwaysInlinerLegacyPass());
mpm.add(new PurgeMetaDataUtils());
mpm.run(*pContext->getModule());
}
}
void OptimizeIR(CodeGenContext* const pContext)
{
IGC_ASSERT(nullptr != pContext);
MetaDataUtils* pMdUtils = pContext->getMetaDataUtils();
IGC_ASSERT(nullptr != pContext->getModuleMetaData());
bool NoOpt = pContext->getModuleMetaData()->compOpt.OptDisable;
alwaysInlineForNoOpt(pContext, NoOpt);
if (pContext->type == ShaderType::OPENCL_SHADER)
{
if (((OpenCLProgramContext*)pContext)->m_InternalOptions.KernelDebugEnable)
{
IGCPassManager mpm(pContext, "CleanImplicitId");
mpm.add(new CleanImplicitIds());
mpm.run(*pContext->getModule());
}
}
if (NoOpt)
{
return;
}
IGCPassManager mpm(pContext, "OPT");
#if defined(_DEBUG) || defined(_INTERNAL)
// do verifyModule for debug/release_internal only.
if (false == pContext->m_hasLegacyDebugInfo)
{
IGC_ASSERT(nullptr != pContext->getModule());
IGC_ASSERT(false == llvm::verifyModule(*pContext->getModule(), &dbgs()));
}
#endif
COMPILER_TIME_START(pContext, TIME_OptimizationPasses);
// scope to force destructors before mem usage sampling
{
unify_opt_PreProcess(pContext);
/// Keeps track of the Dump objects so that we can free them after the pass manager has been run
// right now we don't support any standard function in the code gen
// maybe we want to support some at some point to take advantage of LLVM optimizations
TargetLibraryInfoImpl TLI;
TLI.disableAllFunctions();
mpm.add(new MetaDataUtilsWrapper(pMdUtils, pContext->getModuleMetaData()));
mpm.add(new CodeGenContextWrapper(pContext));
DummyPass* dummypass = new DummyPass();
mpm.add(dummypass);
TargetIRAnalysis GenTTgetIIRAnalysis([&](const Function& F) {
GenIntrinsicsTTIImpl GTTI(pContext, dummypass);
return TargetTransformInfo(GTTI);
});
mpm.add(new TargetTransformInfoWrapperPass(GenTTgetIIRAnalysis));
#if defined(_DEBUG) && !defined(__ANDROID__)
// IGC IR Verification pass checks that we get a correct IR after the Unification.
mpm.add(new VerificationPass());
#endif
mpm.add(new llvm::TargetLibraryInfoWrapperPass(TLI));
initializeWIAnalysisPass(*PassRegistry::getPassRegistry());
// Do inter-procedural constant propagation early.
if (pContext->m_enableSubroutine)
{
// Here, we propagate function attributes across calls. Remaining
// function calls that were conservatively marked as 'convergent'
// in ProcessBuiltinMetaData can have that attribute stripped if
// possible which potentially allows late stage code sinking of
// those calls by the instruction combiner.
mpm.add(createPostOrderFunctionAttrsLegacyPass());
#if LLVM_VERSION_MAJOR >= 12
mpm.add(createIPSCCPPass());
#else
mpm.add(createConstantPropagationPass());
// Don't run IPConstantProp if there are stackcalls
if (!pContext->m_hasStackCalls)
mpm.add(createIPConstantPropagationPass());
#endif
}
if (IGC_IS_FLAG_ENABLED(MSAA16BitPayloadEnable) &&
pContext->platform.support16bitMSAAPayload())
{
mpm.add(new ConvertMSAAPayloadTo16Bit());
}
if (IGC_GET_FLAG_VALUE(MSAAClearedKernel) > 0)
{
mpm.add(new MSAAInsertDiscard());
}
mpm.add(createSamplerPerfOptPass());
// enable this only when Pooled EU is not supported
if ((IGC_IS_FLAG_ENABLED(EnableThreadCombiningOpt) ||
IGC_IS_FLAG_ENABLED(EnableForceThreadCombining) ||
IGC_IS_FLAG_ENABLED(EnableForceGroupSize)) &&
(pContext->type == ShaderType::COMPUTE_SHADER) &&
!pContext->platform.supportPooledEU() &&
pContext->platform.supportsThreadCombining()&&
SimdEarlyCheck(pContext))
{
initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
mpm.add(new ThreadCombining());
mpm.add(createAlwaysInlinerLegacyPass());
mpm.add(createPromoteMemoryToRegisterPass());
}
if ((!IGC_IS_FLAG_ENABLED(DisableDynamicTextureFolding) && pContext->getModuleMetaData()->inlineDynTextures.size() > 0) ||
(!IGC_IS_FLAG_ENABLED(DisableDynamicResInfoFolding)))
{
mpm.add(new DynamicTextureFolding());
}
if (IGC_IS_FLAG_ENABLED(EnableSLMConstProp) &&
pContext->type == ShaderType::COMPUTE_SHADER)
{
mpm.add(createSLMConstPropPass());
}
if (pContext->m_DriverInfo.CodeSinkingBeforeCFGSimplification())
{
mpm.add(new CodeSinking(true));
}
mpm.add(llvm::createCFGSimplificationPass());
mpm.add(llvm::createBasicAAWrapperPass());
mpm.add(createAddressSpaceAAWrapperPass());
if (pContext->type == ShaderType::RAYTRACING_SHADER || pContext->hasSyncRTCalls())
{
if (IGC_IS_FLAG_DISABLED(DisableRTAliasAnalysis))
mpm.add(createRayTracingAddressSpaceAAWrapperPass());
}
mpm.add(createExternalAAWrapperPass(&addAddressSpaceAAResult));
if (pContext->m_instrTypes.hasLoadStore)
{
mpm.add(llvm::createDeadStoreEliminationPass());
mpm.add(createMarkReadOnlyLoadPass());
}
mpm.add(createLogicalAndToBranchPass());
mpm.add(llvm::createEarlyCSEPass());
if (IGC_IS_FLAG_ENABLED(EnableHFpacking) &&
pContext->type == ShaderType::COMPUTE_SHADER)
{
mpm.add(createHFpackingOptPass());
}
if (pContext->m_instrTypes.CorrelatedValuePropagationEnable)
{
mpm.add(llvm::createCorrelatedValuePropagationPass());
}
mpm.add(new BreakConstantExpr());
mpm.add(new IGCConstProp());
mpm.add(new CustomSafeOptPass());
if (!pContext->m_DriverInfo.WADisableCustomPass())
{
mpm.add(new CustomUnsafeOptPass());
}
if (IGC_IS_FLAG_ENABLED(EmulateFDIV))
{
mpm.add(createGenFDIVEmulation());
}
mpm.add(createIGCInstructionCombiningPass());
if (pContext->type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext*>(pContext)->m_InternalOptions.KernelDebugEnable)
{
mpm.add(new ImplicitGIDRestoring());
}
mpm.add(new FCmpPaternMatch());
mpm.add(llvm::createDeadCodeEliminationPass()); // this should be done both before/after constant propagation
if (pContext->m_instrTypes.hasGenericAddressSpacePointers &&
IGC_IS_FLAG_ENABLED(EnableGASResolver))
{
mpm.add(createSROAPass());
mpm.add(createFixAddrSpaceCastPass());
mpm.add(createResolveGASPass());
}
if (IGC_IS_FLAG_ENABLED(SampleMultiversioning) || pContext->m_enableSampleMultiversioning)
{
if (pContext->m_instrTypes.numOfLoop == 0)
mpm.add(new SampleMultiversioning(pContext));
}
bool disableGOPT = ( (IsStage1FastestCompile(pContext->m_CgFlag, pContext->m_StagingCtx) ||
IGC_GET_FLAG_VALUE(ForceFastestSIMD)) &&
((IGC_GET_FLAG_VALUE(FastestS1Experiments) & FCEXP_DISABLE_GOPT) ||
IGC_GET_FLAG_VALUE(FastestS1Experiments) == FCEXP_NO_EXPRIMENT ||
pContext->getModuleMetaData()->compOpt.DisableFastestGopt));
if (pContext->m_instrTypes.hasMultipleBB && !disableGOPT)
{
// disable loop unroll for excessive large shaders
if (pContext->m_instrTypes.numOfLoop)
{
mpm.add(createLoopDeadCodeEliminationPass());
mpm.add(createLoopCanonicalization());
mpm.add(llvm::createLoopDeletionPass());
mpm.add(llvm::createBreakCriticalEdgesPass());
mpm.add(llvm::createLoopRotatePass(LOOP_ROTATION_HEADER_INST_THRESHOLD));
mpm.add(llvm::createLCSSAPass());
mpm.add(llvm::createLoopSimplifyPass());
if (pContext->m_retryManager.AllowLICM() && IGC_IS_FLAG_ENABLED(allowLICM))
{
int licmTh = IGC_GET_FLAG_VALUE(LICMStatThreshold);
mpm.add(new InstrStatistic(pContext, LICM_STAT, InstrStatStage::BEGIN, licmTh));
mpm.add(llvm::createLICMPass());
mpm.add(new InstrStatistic(pContext, LICM_STAT, InstrStatStage::END, licmTh));
}
if (!pContext->m_retryManager.IsFirstTry())
{
mpm.add(new DisableLoopUnrollOnRetry());
}
mpm.add(createIGCInstructionCombiningPass());
if (IGC_IS_FLAG_ENABLED(EnableLoopHoistConstant))
{
mpm.add(createLoopHoistConstant());
}
if (pContext->type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext*>(pContext)->m_InternalOptions.KernelDebugEnable)
{
mpm.add(new ImplicitGIDRestoring());
}
if (IGC_IS_FLAG_ENABLED(EnableAdvCodeMotion) &&
pContext->type == ShaderType::OPENCL_SHADER &&
!pContext->m_instrTypes.hasSwitch)
{
mpm.add(createAdvCodeMotionPass(IGC_GET_FLAG_VALUE(AdvCodeMotionControl)));
}
int LoopUnrollThreshold = pContext->m_DriverInfo.GetLoopUnrollThreshold();
// override the LoopUnrollThreshold if the registry key is set
if (IGC_GET_FLAG_VALUE(SetLoopUnrollThreshold) != 0)
{
LoopUnrollThreshold = IGC_GET_FLAG_VALUE(SetLoopUnrollThreshold);
}
// if the shader contains indexable_temp, we'll keep unroll
bool unroll = IGC_IS_FLAG_DISABLED(DisableLoopUnroll);
bool hasIndexTemp = (pContext->m_indexableTempSize[0] > 0);
bool disableLoopUnrollStage1 =
IsStage1FastestCompile(pContext->m_CgFlag, pContext->m_StagingCtx) &&
(IGC_GET_FLAG_VALUE(FastestS1Experiments) == FCEXP_NO_EXPRIMENT ||
(IGC_GET_FLAG_VALUE(FastestS1Experiments) & FCEXP_DISABLE_UNROLL));
if ((LoopUnrollThreshold > 0 &&
unroll &&
!disableLoopUnrollStage1)
|| hasIndexTemp)
{
mpm.add(IGCLLVM::createLoopUnrollPass());
}
// Due to what looks like a bug in LICM, we need to break the LoopPassManager between
// LoopUnroll and LICM.
mpm.add(createBarrierNoopPass());
if (pContext->m_retryManager.AllowLICM() && IGC_IS_FLAG_ENABLED(allowLICM))
{
mpm.add(llvm::createLICMPass());
}
// Second unrolling with the same threshold.
if (LoopUnrollThreshold > 0 && !IGC_IS_FLAG_ENABLED(DisableLoopUnroll))
{
mpm.add(IGCLLVM::createLoopUnrollPass());
}
mpm.add(llvm::createLoopLoadEliminationPass());
if (!extensiveShader(pContext) && pContext->m_instrTypes.hasNonPrimitiveAlloca)
{
if (pContext->m_DriverInfo.NeedCountSROA())
{
mpm.add(new InstrStatistic(pContext, SROA_PROMOTED, InstrStatStage::BEGIN, 300));
mpm.add(createSROAPass());
mpm.add(new InstrStatistic(pContext, SROA_PROMOTED, InstrStatStage::END, 300));
}
else
{
mpm.add(createSROAPass());
}
}
}
// Note:
// call reassociation pass before IGCConstProp(EnableSimplifyGEP)
// to preserve the the expr evaluation order that IGCConstProp
// creates.
// Limit this optimization to GPGPU-only because it tends to have
// more address computation.
// Do not apply reordering on vertex-shader as CustomUnsafeOptPass
// does.
if (IGC_IS_FLAG_ENABLED(OCLEnableReassociate) &&
pContext->type == ShaderType::OPENCL_SHADER)
{
mpm.add(createReassociatePass());
}
mpm.add(createPromoteConstantStructsPass());
if (IGC_IS_FLAG_ENABLED(EnableGVN))
{
mpm.add(llvm::createGVNPass());
}
mpm.add(createGenOptLegalizer());
mpm.add(llvm::createSCCPPass());
mpm.add(llvm::createDeadCodeEliminationPass());
if (!extensiveShader(pContext))
mpm.add(llvm::createAggressiveDCEPass());
mpm.add(new BreakConstantExpr());
mpm.add(new IGCConstProp(IGC_IS_FLAG_ENABLED(EnableSimplifyGEP)));
if (IGC_IS_FLAG_DISABLED(DisableImmConstantOpt))
{
// If we have ICBs, need to emit clamp code so OOB access doesn't occur
if (pContext->getModuleMetaData()->immConstant.zeroIdxs.size())
{
mpm.add(createClampICBOOBAccess());
}
mpm.add(createIGCIndirectICBPropagaionPass());
}
mpm.add(new GenUpdateCB());
if (!pContext->m_instrTypes.hasAtomics && !extensiveShader(pContext))
{
if (pContext->type == ShaderType::OPENCL_SHADER)
{
// Add CFGSimplification for clean-up before JumpThreading.
mpm.add(llvm::createCFGSimplificationPass());
}
// jump threading currently causes the atomic_flag test from c11 conformance to fail. Right now,
// only do jump threading if we don't have atomics as using atomics as locks seems to be the most common
// case of violating the no independent forward progress clause from the spec.
mpm.add(llvm::createJumpThreadingPass());
}
mpm.add(llvm::createCFGSimplificationPass());
mpm.add(llvm::createEarlyCSEPass());
if (pContext->m_instrTypes.hasNonPrimitiveAlloca)
{
// run custom safe opts to potentially get rid of indirect
// addressing of private arrays, see visitLoadInst
mpm.add(new CustomSafeOptPass());
mpm.add(createSROAPass());
}
// Use CFGSimplification to do clean-up. Needs to be invoked before lowerSwitch.
mpm.add(llvm::createCFGSimplificationPass());
if (IGC_IS_FLAG_DISABLED(DisableFlattenSmallSwitch))
{
mpm.add(createFlattenSmallSwitchPass());
}
//some optimization can create switch statement we don't support
mpm.add(llvm::createLowerSwitchPass());
// preferred to be added after all LowerSwitch pass runs, as switch lowering is able
// to benefit from unreachable instruction when it's in default switch case
mpm.add(new UnreachableHandling());
// Conditions apply just as above due to problems with atomics
// (see comment above for details).
if (!pContext->m_instrTypes.hasAtomics && !extensiveShader(pContext))
{
// After lowering 'switch', run jump threading to remove redundant jumps.
mpm.add(llvm::createJumpThreadingPass());
}
// run instruction combining to clean up the code after CFG optimizations
mpm.add(createIGCInstructionCombiningPass());
if (pContext->type == ShaderType::OPENCL_SHADER &&
static_cast<OpenCLProgramContext*>(pContext)->m_InternalOptions.KernelDebugEnable)
{
mpm.add(new ImplicitGIDRestoring());
}
mpm.add(llvm::createDeadCodeEliminationPass());
mpm.add(llvm::createEarlyCSEPass());
// need to be before code sinking
mpm.add(createInsertBranchOptPass());
mpm.add(new CustomSafeOptPass());
if (!pContext->m_DriverInfo.WADisableCustomPass())
{
mpm.add(new CustomUnsafeOptPass());
}
}
else
{
if (pContext->m_instrTypes.hasMultipleBB)
{
assert(disableGOPT);
// disable loop unroll for excessive large shaders
if (pContext->m_instrTypes.numOfLoop)
{
mpm.add(llvm::createLoopRotatePass(LOOP_ROTATION_HEADER_INST_THRESHOLD));
int LoopUnrollThreshold = pContext->m_DriverInfo.GetLoopUnrollThreshold();
// override the LoopUnrollThreshold if the registry key is set
if (IGC_GET_FLAG_VALUE(SetLoopUnrollThreshold) != 0)
{
LoopUnrollThreshold = IGC_GET_FLAG_VALUE(SetLoopUnrollThreshold);
}
// if the shader contains indexable_temp, we'll keep unroll
bool unroll = IGC_IS_FLAG_DISABLED(DisableLoopUnroll);
bool hasIndexTemp = (pContext->m_indexableTempSize[0] > 0);
// Enable loop unrolling for stage 1 for now due to persisting regressions
bool disableLoopUnrollStage1 =
IsStage1FastestCompile(pContext->m_CgFlag, pContext->m_StagingCtx) &&
(//IGC_GET_FLAG_VALUE(FastestS1Experiments) == FCEXP_NO_EXPRIMENT ||
(IGC_GET_FLAG_VALUE(FastestS1Experiments) & FCEXP_DISABLE_UNROLL));
if ((LoopUnrollThreshold > 0 &&
unroll &&
!disableLoopUnrollStage1)
|| hasIndexTemp)
{
mpm.add(IGCLLVM::createLoopUnrollPass());
}
}
if (IGC_IS_FLAG_ENABLED(EnableGVN))
{
mpm.add(llvm::createGVNPass());
}
}
if (IGC_IS_FLAG_DISABLED(DisableImmConstantOpt))
{
// If we have ICBs, need to emit clamp code so OOB access doesn't occur
if (pContext->getModuleMetaData()->immConstant.zeroIdxs.size())
{
mpm.add(createClampICBOOBAccess());
}
mpm.add(createIGCIndirectICBPropagaionPass());
}
//single basic block
if (!pContext->m_DriverInfo.WADisableCustomPass())
{
mpm.add(llvm::createEarlyCSEPass());
mpm.add(new CustomSafeOptPass());
mpm.add(new CustomUnsafeOptPass());
}
mpm.add(createGenOptLegalizer());
mpm.add(createInsertBranchOptPass());
}
// If we have ICBs, need to emit clamp code so OOB access doesn't occur
if (pContext->getModuleMetaData()->immConstant.zeroIdxs.size() && IGC_IS_FLAG_ENABLED(DisableImmConstantOpt))
{
mpm.add(createClampICBOOBAccess());
}
if (pContext->m_instrTypes.hasRuntimeValueVector)
{
// Optimize extracts from RuntimeValue vectors. It should be executed
// after constants propagation and loop unrolling
mpm.add(createVectorBitCastOptPass());
mpm.add(new RuntimeValueVectorExtractPass());
}
if (pContext->m_enableSubroutine &&
getFunctionControl(pContext) == FLAG_FCALL_DEFAULT)
{
mpm.add(createEstimateFunctionSizePass(EstimateFunctionSize::AL_Kernel));
mpm.add(createSubroutineInlinerPass());
}
else
{
// Inline all remaining functions with always inline attribute.
mpm.add(createAlwaysInlinerLegacyPass());
}
if ((pContext->m_DriverInfo.NeedExtraPassesAfterAlwaysInlinerPass() || pContext->m_enableSubroutine)
&& pContext->m_instrTypes.hasNonPrimitiveAlloca)
{
mpm.add(createSROAPass());
}
#if LLVM_VERSION_MAJOR >= 7
mpm.add(new TrivialLocalMemoryOpsElimination());
#endif
mpm.add(createGenSimplificationPass());
if (pContext->m_instrTypes.hasLoadStore)
{
mpm.add(llvm::createDeadStoreEliminationPass());
mpm.add(llvm::createMemCpyOptPass());
mpm.add(createLdShrinkPass());
}
mpm.add(llvm::createDeadCodeEliminationPass());
if (IGC_IS_FLAG_ENABLED(EnableIntDivRemCombine)) {
// simplify rem if the quotient is availble
//
// run GVN first so that stuff like the following can be
// reduced as well:
// = foo / (2*x + 1)
// = foo % (2*x + 1)
// can be reduced as well
if (IGC_IS_FLAG_ENABLED(EnableGVN)) {
mpm.add(llvm::createGVNPass());
}
//
mpm.add(createIntDivRemCombinePass());
}
if (IGC_IS_FLAG_ENABLED(EnableConstIntDivReduction)) {
// reduce division/remainder with a constant divisors/moduli to
// more efficient sequences of multiplies, shifts, and adds
mpm.add(createIntDivConstantReductionPass());
}
mpm.add(createMergeMemFromBranchOptPass());
mpm.add(createConstantMergePass());
mpm.add(CreateMCSOptimization());
if (pContext->type == ShaderType::GEOMETRY_SHADER)
mpm.add(createRectListOptimizationPass());
mpm.add(CreateGatingSimilarSamples());
if (!IGC::ForceAlwaysInline(pContext))
{
mpm.add(new PurgeMetaDataUtils());
}
// mpm.add(llvm::createDeadCodeEliminationPass()); // this should be done both before/after constant propagation
if (IGC_IS_FLAG_ENABLED(EnableUnmaskedFunctions) &&
IGC_IS_FLAG_DISABLED(LateInlineUnmaskedFunc))
{
mpm.add(new InlineUnmaskedFunctionsPass());
}
if (pContext->m_instrTypes.numOfLoop)
{
mpm.add(createDeadPHINodeEliminationPass());
}
if (IGC_IS_FLAG_ENABLED(EnableMadLoopSlice)) {
mpm.add(createMadLoopSlicePass());
}
mpm.run(*pContext->getModule());
} // end scope
COMPILER_TIME_END(pContext, TIME_OptimizationPasses);
//pContext->shaderEntry->viewCFG();
DumpLLVMIR(pContext, "optimized");
MEM_SNAPSHOT(IGC::SMS_AFTER_OPTIMIZER);
} // OptimizeIR
} // namespace IGC
|