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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "GenCodeGenModule.h"
#include "EstimateFunctionSize.h"
#include "AdaptorCommon/ImplicitArgs.hpp"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/DebugInfo/ScalarVISAModule.h"
#include "Compiler/CodeGenContextWrapper.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/MetaDataApi/PurgeMetaDataUtils.hpp"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "common/igc_regkeys.hpp"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Argument.h"
#include "llvmWrapper/IR/Attributes.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvmWrapper/IR/CallSite.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/Inliner.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvmWrapper/Transforms/Utils/Cloning.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DIBuilder.h"
#include "common/LLVMWarningsPop.hpp"
#include "DebugInfo/VISADebugEmitter.hpp"
#include <numeric>
#include <utility>
#include <iostream>
#include "Probe/Assertion.h"
using namespace llvm;
using namespace IGC;
char GenXCodeGenModule::ID = 0;
IGC_INITIALIZE_PASS_BEGIN(GenXCodeGenModule, "GenXCodeGenModule", "GenXCodeGenModule", false, false)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(GenXFunctionGroupAnalysis)
IGC_INITIALIZE_PASS_END(GenXCodeGenModule, "GenXCodeGenModule", "GenXCodeGenModule", false, false)
llvm::ModulePass* IGC::createGenXCodeGenModulePass()
{
initializeGenXCodeGenModulePass(*PassRegistry::getPassRegistry());
return new GenXCodeGenModule;
}
GenXCodeGenModule::GenXCodeGenModule()
: llvm::ModulePass(ID), FGA(nullptr), pMdUtils(nullptr), Modified(false)
{
initializeGenXCodeGenModulePass(*PassRegistry::getPassRegistry());
}
GenXCodeGenModule::~GenXCodeGenModule() {}
void GenXCodeGenModule::getAnalysisUsage(AnalysisUsage& AU) const
{
AU.addRequired<MetaDataUtilsWrapper>();
AU.addRequired<GenXFunctionGroupAnalysis>();
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<llvm::CallGraphWrapperPass>();
}
// Update cloned function's metadata.
static inline void CloneFuncMetadata(IGCMD::MetaDataUtils* pM,
llvm::Function* ClonedF,
llvm::Function* F)
{
using namespace IGC::IGCMD;
auto Info = pM->getFunctionsInfoItem(F);
auto NewInfo = FunctionInfoMetaDataHandle(new FunctionInfoMetaData());
// Copy function type info.
if (Info->isTypeHasValue())
{
NewInfo->setType(Info->getType());
}
// Copy explicit argument info, if any.
unsigned i = 0;
for (auto AI = Info->begin_ArgInfoList(), AE = Info->begin_ArgInfoList();
AI != AE; ++AI)
{
NewInfo->addArgInfoListItem(Info->getArgInfoListItem(i));
i++;
}
// Copy implicit argument info, if any.
i = 0;
for (auto AI = Info->begin_ImplicitArgInfoList(),
AE = Info->end_ImplicitArgInfoList();
AI != AE; ++AI)
{
NewInfo->addImplicitArgInfoListItem(Info->getImplicitArgInfoListItem(i));
i++;
}
pM->setFunctionsInfoItem(ClonedF, Info);
}
static Function* cloneFunc(IGCMD::MetaDataUtils* pM, Function* F, string prefix = "", string postfix = "_GenXClone")
{
ValueToValueMapTy VMap;
Function* ClonedFunc = CloneFunction(F, VMap);
ClonedFunc->setName(prefix + F->getName().str() + postfix);
//if the function is not added as part of clone, add it
if (!F->getParent()->getFunction(ClonedFunc->getName()))
F->getParent()->getFunctionList().push_back(ClonedFunc);
CloneFuncMetadata(pM, ClonedFunc, F);
return ClonedFunc;
}
inline Function* getCallerFunc(Value* user)
{
IGC_ASSERT(nullptr != user);
Function* caller = nullptr;
if (CallInst * CI = dyn_cast<CallInst>(user))
{
IGC_ASSERT(nullptr != CI->getParent());
caller = CI->getParent()->getParent();
}
IGC_ASSERT_MESSAGE(caller, "cannot be indirect call");
return caller;
}
void GenXCodeGenModule::detectUnpromotableFunctions(Module* pM)
{
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
SmallSet<Function*, 32> tempFuncs;
// Find functions that have uses of "localSLM" globals
for (auto gi = pM->global_begin(), ge = pM->global_end(); gi != ge; gi++)
{
GlobalVariable* GV = dyn_cast<GlobalVariable>(gi);
if (GV && GV->hasSection() && GV->getSection().equals("localSLM"))
{
for (auto user : GV->users())
{
if (Instruction* U = dyn_cast<Instruction>(user))
{
Function* pF = U->getParent()->getParent();
if (!isEntryFunc(pMdUtils, pF))
tempFuncs.insert(pF);
}
}
}
}
// Find functions that uses instructions that can't be handled for indirect call
for (auto& F : *pM)
{
// Only look at non-entry functions
if (F.isDeclaration() || isEntryFunc(pMdUtils, &F) || tempFuncs.count(&F) != 0)
continue;
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
{
if (auto* GII = dyn_cast<GenIntrinsicInst>(&*I))
{
// Can't make indirect if threadgroupbarrier intrinsic is set
if (GII->getIntrinsicID() == GenISAIntrinsic::GenISA_threadgroupbarrier)
{
tempFuncs.insert(&F);
break;
}
}
}
}
// Recursively add callers, as the whole chain of calls cannot be promoted
std::function<void(Function*)> AddCallerRecursive = [&](Function* F)
{
if (isEntryFunc(pMdUtils, F))
return;
m_UnpromotableFuncs.insert(F);
for (auto user : F->users())
{
if (Instruction* I = dyn_cast<Instruction>(user))
{
Function* parentF = I->getParent()->getParent();
AddCallerRecursive(parentF);
}
}
};
for (auto F : tempFuncs)
{
AddCallerRecursive(F);
}
}
void GenXCodeGenModule::processFunction(Function& F)
{
// See what FunctionGroups this Function is called from.
SetVector<std::pair<FunctionGroup*, Function*>> CallerFGs;
std::vector<llvm::Function*> Callers;
if (IGC_IS_FLAG_ENABLED(StackOverflowDetection)) {
if (F.getName().equals("__stackoverflow_detection")) {
// Mark all stack calls as users of this detection function.
// It will be used as a subroutine, so it needs to be cloned for
// each of stack call functions.
for (auto& Func : F.getParent()->getFunctionList()) {
if (Func.hasFnAttribute("visaStackCall")) {
Callers.push_back(&Func);
}
}
}
}
for (auto U : F.users()) {
Callers.push_back(getCallerFunc(U));
}
for (auto Caller : Callers)
{
FunctionGroup* FG = FGA->getGroup(Caller);
Function* SubGrpH = FGA->useStackCall(&F) ? (&F) : FGA->getSubGroupMap(Caller);
if (FG == nullptr || SubGrpH == nullptr)
continue;
CallerFGs.insert(std::make_pair(FG, SubGrpH));
}
IGC_ASSERT(CallerFGs.size() >= 1);
auto pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
auto CanMakeIndirectFunc = [&](llvm::Function* F)
{
// Don't convert subroutines, builtins, or invoke_simd_target
if (F->hasFnAttribute("visaStackCall") == false ||
F->hasFnAttribute("OclBuiltin") ||
F->hasFnAttribute("invoke_simd_target"))
return false;
// If SIMD Variant Compilation is not enabled, we have to make sure all callers
// have the same SIMD sizes, otherwise we cannot make it an indirect call
int simd_size = 0;
unsigned CallersPerSIMD[3] = { 0, 0, 0 };
for (const auto &iter : CallerFGs)
{
Function* callerKernel = iter.first->getHead();
auto funcInfoMD = pMdUtils->getFunctionsInfoItem(callerKernel);
int sz = funcInfoMD->getSubGroupSize()->getSIMDSize();
if (sz != 0) {
if (simd_size == 0)
simd_size = sz;
CallersPerSIMD[(sz >> 4)]++;
}
// Callers have varying SIMD size requirements, do not promote
if (IGC_IS_FLAG_DISABLED(EnableSIMDVariantCompilation) && simd_size != sz)
return false;
}
if (IGC_IS_FLAG_ENABLED(EnableSIMDVariantCompilation))
{
// For MultiSIMD compile, check profitability.
// Since we need to clone to each SIMD variant kernel, only promote if the
// number of FGs per SIMD is greater than the threshold.
if (m_FunctionCloningThreshold > 0 &&
CallersPerSIMD[0] <= m_FunctionCloningThreshold &&
CallersPerSIMD[1] <= m_FunctionCloningThreshold &&
CallersPerSIMD[2] <= m_FunctionCloningThreshold)
{
return false;
}
}
// If CallWA is needed, we should not convert to indirect call when requiring SIMD32,
// as we can potentially avoid CallWA if there are no indirect calls.
if (pCtx->platform.requireCallWA() /* && simd_size == 32*/)
{
return false;
}
// Detect if function is part of the unpromotable set
if (m_UnpromotableFuncs.count(F) != 0)
{
return false;
}
return true;
};
// Make the function indirect if cloning is required to decrease compile time
if (m_FunctionCloningThreshold > 0 &&
CallerFGs.size() > m_FunctionCloningThreshold &&
CanMakeIndirectFunc(&F))
{
auto pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
auto IFG = FGA->getOrCreateIndirectCallGroup(F.getParent());
if (IFG)
{
// If EnableSIMDVariantCompilation=0, we only compile a single variant of the dummy kernel.
// By default, getOrCreateIndirectCallGroup will create a dummy kernel with the lowest allowed SIMD size,
// but if only a single variant exists, and we determine that the caller has a required subgroup size
// different from the default size, we need to change the default subgroup size here to match the caller.
if (IGC_IS_FLAG_DISABLED(EnableSIMDVariantCompilation))
{
int req_subgroup = 0;
for (const auto &FG : CallerFGs)
{
auto FHead = FG.first->getHead();
auto subGrpSz = pMdUtils->getFunctionsInfoItem(FHead)->getSubGroupSize();
if (subGrpSz->isSIMDSizeHasValue())
{
// We can assume all callers have the same subgroup size requirement, otherwise
// CanMakeIndirectFunc will return false when EnableSIMDVariantCompilation=0
req_subgroup = subGrpSz->getSIMDSize();
break;
}
}
if (req_subgroup)
{
auto defaultKernel = IFG->getHead();
pMdUtils->getFunctionsInfoItem(defaultKernel)->getSubGroupSize()->setSIMDSize(req_subgroup);
pMdUtils->save(pCtx->getModule()->getContext());
}
}
if (IGC_IS_FLAG_ENABLED(PrintStackCallDebugInfo))
{
dbgs() << "Make Indirect: " << F.getName().str() << "\n";
}
F.addFnAttr("referenced-indirectly");
pCtx->m_enableFunctionPointer = true;
FGA->addToFunctionGroup(&F, IFG, &F);
return;
}
}
bool FirstPair = true;
for (const auto &FGPair : CallerFGs)
{
if (FirstPair)
{
FGA->addToFunctionGroup(&F, FGPair.first, FGPair.second);
FirstPair = false;
}
else
{
// clone the function, add to this function group
auto FCloned = cloneFunc(pMdUtils, &F);
// Copy F's property over
copyFuncProperties(FCloned, &F);
Function* SubGrpH = FGA->useStackCall(&F) ? FCloned : FGPair.second;
FGA->addToFunctionGroup(FCloned, FGPair.first, SubGrpH);
Modified = true;
// update the edge after clone, it can handle self-recursion
for (auto UI = F.use_begin(), UE = F.use_end(); UI != UE; /*empty*/)
{
// Increment iterator after setting U to change the use.
Use* U = &*UI++;
Function* Caller = cast<CallInst>(U->getUser())->getParent()->getParent();
FunctionGroup* InFG = FGA->getGroup(Caller);
Function* InSubGrpH = FGA->useStackCall(&F) ? FCloned : FGA->getSubGroupMap(Caller);
if (InFG == FGPair.first && InSubGrpH == SubGrpH)
{
*U = FCloned;
}
}
}
}
}
void GenXCodeGenModule::processSCC(std::vector<llvm::CallGraphNode*>* SCCNodes)
{
// force stack-call for every function in SCC
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
F->addFnAttr("visaStackCall");
}
// See what FunctionGroups this SCC is called from.
SetVector<FunctionGroup*> CallerFGs;
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
for (auto U : F->users())
{
Function* Caller = getCallerFunc(U);
FunctionGroup* FG = FGA->getGroup(Caller);
if (FG == nullptr)
continue;
CallerFGs.insert(FG);
}
}
IGC_ASSERT(CallerFGs.size() >= 1);
bool FirstPair = true;
for (auto FG : CallerFGs)
{
if (FirstPair)
{
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
FGA->addToFunctionGroup(F, FG, F);
}
FirstPair = false;
}
else
{
// clone the functions in scc, add to this function group
llvm::DenseMap<Function*, Function*> CloneMap;
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
auto FCloned = cloneFunc(pMdUtils, F);
// Copy properties
copyFuncProperties(FCloned, F);
FGA->addToFunctionGroup(FCloned, FG, FCloned);
CloneMap.insert(std::make_pair(F, FCloned));
}
Modified = true;
// update the call-edges for every function in SCC,
// move edges to the cloned SCC, including the recursion edge
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
for (auto UI = F->use_begin(), UE = F->use_end(); UI != UE; /*empty*/)
{
// Increment iterator after setting U to change the use.
Use* U = &*UI++;
Function* Caller = cast<CallInst>(U->getUser())->getParent()->getParent();
FunctionGroup* InFG = FGA->getGroup(Caller);
if (InFG == FG)
{
*U = CloneMap[F];
}
}
}
}
}
}
void GenXCodeGenModule::setFuncProperties(CallGraph& CG)
{
for (auto I = scc_begin(&CG), IE = scc_end(&CG); I != IE; ++I)
{
const std::vector<CallGraphNode*>& SCCNodes = (*I);
for (CallGraphNode* Node : SCCNodes)
{
Function* F = Node->getFunction();
if (F != nullptr && !F->isDeclaration())
{
if (Node->empty()) {
FGA->setLeafFunc(F);
}
}
}
}
}
void GenXCodeGenModule::copyFuncProperties(llvm::Function* To, llvm::Function* From)
{
FGA->copyFuncProperties(To, From);
}
/// Deduce non-null argument attributes for subroutines.
static bool DeduceNonNullAttribute(Module& M)
{
bool Modifided = false;
for (auto& F : M.getFunctionList())
{
if (F.isDeclaration() || F.hasExternalLinkage() || F.hasAddressTaken())
continue;
bool Skip = false;
for (auto U : F.users())
{
if (!isa<CallInst>(U))
{
Skip = true;
break;
}
}
if (Skip)
continue;
for (auto& Arg : F.args())
{
// Only for used pointer arguments.
if (Arg.use_empty() || !Arg.getType()->isPointerTy())
continue;
// If all call sites are passing a non-null value to this argument, then
// this argument cannot be a null ptr.
bool NotNull = true;
for (auto U : F.users())
{
auto CI = cast<CallInst>(U);
Value* V = CI->getArgOperand(Arg.getArgNo());
auto DL = F.getParent()->getDataLayout();
if (!isKnownNonZero(V, DL))
{
NotNull = false;
break;
}
}
if (NotNull) {
// FIXME: Below lines possibly can be refactored to be simpler.
AttributeList attrSet = AttributeList::get(Arg.getParent()->getContext(), Arg.getArgNo() + 1, llvm::Attribute::NonNull);
llvm::Attribute attr = IGCLLVM::getAttribute(attrSet, Arg.getArgNo() + 1, llvm::Attribute::NonNull);
IGC_ASSERT(IGCLLVM::hasParentContext(attr, Arg.getParent()->getContext()));
Arg.addAttr(attr);
Modifided = true;
}
}
}
return Modifided;
}
bool GenXCodeGenModule::runOnModule(Module& M)
{
FGA = &getAnalysis<GenXFunctionGroupAnalysis>();
// Already built.
if (FGA->getModule())
return false;
// Set the cloning threshold. This determins the number of times a function called from multiple
// function groups can be cloned. If the number exceeds the threshold, instead of cloning the
// function N times, make it an indirect call and use relocation instead. The function will only be
// compiled once and runtime must relocate its address for each caller.
m_FunctionCloningThreshold = 0;
if (IGC_IS_FLAG_ENABLED(EnableFunctionCloningControl))
{
if (getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->enableZEBinary())
{
// Avoid cloning by default on zebin
m_FunctionCloningThreshold = 1;
}
if (IGC_GET_FLAG_VALUE(FunctionCloningThreshold) != 0)
{
// Overwrite with debug flag
m_FunctionCloningThreshold = IGC_GET_FLAG_VALUE(FunctionCloningThreshold);
}
}
pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
CallGraph& CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
setFuncProperties(CG);
std::vector<std::vector<CallGraphNode*>*> SCCVec;
for (auto I = scc_begin(&CG), IE = scc_end(&CG); I != IE; ++I)
{
std::vector<CallGraphNode*>* SCCNodes = new std::vector<CallGraphNode*>((*I));
SCCVec.push_back(SCCNodes);
}
// Add all indirect functions to the default kernel group
FGA->addIndirectFuncsToKernelGroup(&M);
// If FunctionCloningThreshold is enabled, detect functions that cannot be promoted to indirect
if (m_FunctionCloningThreshold > 0)
{
detectUnpromotableFunctions(&M);
}
for (auto I = SCCVec.rbegin(), IE = SCCVec.rend(); I != IE; ++I)
{
std::vector<CallGraphNode*>* SCCNodes = (*I);
for (CallGraphNode* Node : (*SCCNodes))
{
Function* F = Node->getFunction();
if (!F || F->isDeclaration()) continue;
// skip functions belonging to the indirect call group
if (FGA->isIndirectCallGroup(F)) continue;
if (isEntryFunc(pMdUtils, F))
{
FGA->setSubGroupMap(F, F);
FGA->createFunctionGroup(F);
}
else if (SCCNodes->size() == 1)
{
processFunction(*F);
}
else
{
processSCC(SCCNodes);
break;
}
}
delete SCCNodes;
}
// Clone indirect funcs if SIMD variants are required
FGA->CloneFunctionGroupForMultiSIMDCompile(&M);
this->pMdUtils->save(M.getContext());
// Check and set FG attribute flags
FGA->setGroupAttributes();
// By swapping, we sort the function list to ensure codegen order for
// functions. This relies on llvm module pass manager's implementation detail.
SmallVector<Function*, 16> OrderedList;
for (auto GI = FGA->begin(), GE = FGA->end(); GI != GE; ++GI)
{
for (auto SubGI = (*GI)->Functions.begin(), SubGE = (*GI)->Functions.end();
SubGI != SubGE; ++SubGI)
{
for (auto FI = (*SubGI)->begin(), FE = (*SubGI)->end(); FI != FE; ++FI)
{
OrderedList.push_back(*FI);
}
}
}
// Input L1 = [A, B, C, D, E] // Functions in groups
// Input L2 = [A, C, G, B, D, E, F] // Functions in the module
// Output L2 = [A, B, C, D, E, G, F] // Ordered functions in the module
IGC_ASSERT_MESSAGE(OrderedList.size() <= M.size(), "out of sync");
Function* CurF = &(*M.begin());
for (auto I = OrderedList.begin(), E = OrderedList.end(); I != E; ++I)
{
IGC_ASSERT(nullptr != CurF);
Function* F = *I;
if (CurF != F)
// Move F before CurF. This just works See BasicBlock::moveBefore.
// CurF remains the same for the next iteration.
M.getFunctionList().splice(CurF->getIterator(), M.getFunctionList(), F);
else
{
auto it = CurF->getIterator();
CurF = &*(++it);
}
}
IGC_ASSERT(FGA->verify());
FGA->setModule(&M);
Modified |= DeduceNonNullAttribute(M);
return Modified;
}
////////////////////////////////////////////////////////////////////////////////
/// GenXFunctionGroupAnalysis implementation detail
////////////////////////////////////////////////////////////////////////////////
char GenXFunctionGroupAnalysis::ID = 0;
IGC_INITIALIZE_PASS_BEGIN(GenXFunctionGroupAnalysis, "GenXFunctionGroupAnalysis", "GenXFunctionGroupAnalysis", false, true)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(GenXFunctionGroupAnalysis, "GenXFunctionGroupAnalysis", "GenXFunctionGroupAnalysis", false, true)
llvm::ImmutablePass* IGC::createGenXFunctionGroupAnalysisPass() {
initializeGenXFunctionGroupAnalysisPass(*PassRegistry::getPassRegistry());
return new GenXFunctionGroupAnalysis;
}
GenXFunctionGroupAnalysis::GenXFunctionGroupAnalysis()
: ImmutablePass(ID), M(nullptr) {
initializeGenXFunctionGroupAnalysisPass(*PassRegistry::getPassRegistry());
}
void GenXFunctionGroupAnalysis::getAnalysisUsage(AnalysisUsage& AU) const {
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<MetaDataUtilsWrapper>();
AU.setPreservesAll();
}
bool GenXFunctionGroupAnalysis::verify()
{
for (auto GI = begin(), GE = end(); GI != GE; ++GI)
{
for (auto SubGI = (*GI)->Functions.begin(), SubGE = (*GI)->Functions.end();
SubGI != SubGE; ++SubGI)
{
for (auto FI = (*SubGI)->begin(), FE = (*SubGI)->end(); FI != FE; ++FI)
{
Function* F = *FI;
if (F->hasFnAttribute("referenced-indirectly")
)
{
continue;
}
// If F is an unused non-kernel function, although it should have been
// deleted, that is fine.
for (auto U : F->users())
{
Function* Caller = getCallerFunc(U);
FunctionGroup* CallerFG = getGroup(Caller);
// Caller's FG should be the same as FG. Otherwise, something is wrong.
if (CallerFG != (*GI))
{
printf("%s\n", F->getName().data());
printf("%s\n", Caller->getName().data());
return false;
}
Function* SubGrpH = getSubGroupMap(F);
// Caller's sub-group header must be the first element of the sub-vector
if (SubGrpH != (*SubGI)->front())
return false;
}
}
}
}
// Everything is OK.
return true;
}
// Returns the default SIMD size to compile Indirect Functions to
static inline int getDefaultSIMDSize(CodeGenContext* ctx)
{
if (ctx->getModuleMetaData()->csInfo.forcedSIMDSize != 0)
{
// Forcing SIMD size
return ctx->getModuleMetaData()->csInfo.forcedSIMDSize;
}
// By default, use the minimum allowed SIMD for the HW platform
SIMDMode simdMode = ctx->platform.getMinDispatchMode();
int defaultSz = numLanes(simdMode);
if (ctx->getModuleMetaData()->compOpt.IsLibraryCompilation)
{
unsigned sz = ctx->getModuleMetaData()->compOpt.LibraryCompileSIMDSize;
if (sz > 0)
{
IGC_ASSERT(sz == 8 || sz == 16 || sz == 32);
defaultSz = sz;
}
}
return defaultSz;
}
FunctionGroup* GenXFunctionGroupAnalysis::getOrCreateIndirectCallGroup(Module* pModule, int SimdSize)
{
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
auto pMdUtil = ctx->getMetaDataUtils();
auto pModMD = ctx->getModuleMetaData();
int defaultSimd = getDefaultSIMDSize(ctx);
if (SimdSize == 0)
{
SimdSize = defaultSimd;
}
IGC_ASSERT(SimdSize == 8 || SimdSize == 16 || SimdSize == 32);
int idx = SimdSize >> 4;
if (IndirectCallGroup[idx] != nullptr) return IndirectCallGroup[idx];
llvm::Function* defaultKernel = IGC::getIntelSymbolTableVoidProgram(pModule);
// No default kernel found
if (!defaultKernel) return nullptr;
if (SimdSize != defaultSimd)
{
// Require a SIMD variant version of the dummy kernel.
// Create a new dummy kernel clone here to attach all functions with the required SimdSize.
std::string fName = std::string(IGC::INTEL_SYMBOL_TABLE_VOID_PROGRAM) + "_GenXSIMD" + std::to_string(SimdSize);
Function* pNewFunc = Function::Create(defaultKernel->getFunctionType(), GlobalValue::ExternalLinkage, fName, pModule);
BasicBlock* entry = BasicBlock::Create(pModule->getContext(), "entry", pNewFunc);
IRBuilder<> builder(entry);
builder.CreateRetVoid();
// Set spirv calling convention and kernel metadata
pNewFunc->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
IGCMD::FunctionInfoMetaDataHandle fHandle = IGCMD::FunctionInfoMetaDataHandle(new IGCMD::FunctionInfoMetaData());
FunctionMetaData* funcMD = &pModMD->FuncMD[pNewFunc];
funcMD->functionType = IGC::FunctionTypeMD::KernelFunction;
fHandle->setType(FunctionTypeMD::KernelFunction);
pMdUtil->setFunctionsInfoItem(pNewFunc, fHandle);
defaultKernel = pNewFunc;
}
// Set the requested sub_group_size value for this kernel
pMdUtil->getFunctionsInfoItem(defaultKernel)->getSubGroupSize()->setSIMDSize(SimdSize);
pMdUtil->save(pModule->getContext());
auto FG = getGroup(defaultKernel);
if (!FG)
{
setSubGroupMap(defaultKernel, defaultKernel);
FG = createFunctionGroup(defaultKernel);
}
IndirectCallGroup[idx] = FG;
return FG;
}
bool GenXFunctionGroupAnalysis::useStackCall(llvm::Function* F)
{
return (F->hasFnAttribute("visaStackCall"));
}
void GenXFunctionGroupAnalysis::setGroupAttributes()
{
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
for (auto FG : Groups)
{
if (isIndirectCallGroup(FG))
{
// The dummy kernel group is not a true function group, in that the functions in this group does not have
// a valid callgraph that connects them. It's a dummy group where all indirectly called functions are contained.
// Therefore, the group attributes are not valid here, since they are not connected to the real groupHead, which
// is the caller kernel. We don't set any of the FG attribute flags for this group.
//
// Note, indirect functions in this group can still directly call stackcalls or subroutines, which may also belong
// to this group due to cloning. However we still can't associate all functions in this group with a single callgraph.
continue;
}
for (const Function* F : *FG)
{
if (F->hasFnAttribute("referenced-indirectly"))
{
IGC_ASSERT_MESSAGE(0, "Indirectly referenced function not moved to IndirectCallGroup!");
continue;
}
if (F->hasFnAttribute("visaStackCall"))
{
FG->m_hasStackCall = true;
if (!isLeafFunc(F))
{
FG->m_hasNestedCall = true;
}
}
else if (!isEntryFunc(pMdUtils, F))
{
FG->m_hasSubroutine = true;
}
// check all functions in the group to see if there's an vla alloca
// function attribute "hasVLA" should be set at ProcessFuncAttributes pass
if (F->hasFnAttribute("hasVLA"))
{
FG->m_hasVariableLengthAlloca = true;
}
// check if FG uses recursion. The "hasRecursion" attribute is set in
// ProcessFuncAttributes pass by using Tarjan's algorithm to find recursion.
if (F->hasFnAttribute("hasRecursion"))
{
FG->m_hasRecursion = true;
}
// For the remaining attributes we need to loop through all the call instructions
for (auto ii = inst_begin(F), ei = inst_end(F); ii != ei; ii++)
{
if (const CallInst* call = dyn_cast<CallInst>(&*ii))
{
Function* calledF = call->getCalledFunction();
bool hasStackCall = calledF && calledF->hasFnAttribute("visaStackCall");
if (call->isInlineAsm())
{
// Uses inline asm call
FG->m_hasInlineAsm = true;
}
else if (!calledF || (calledF->isDeclaration() && calledF->hasFnAttribute("referenced-indirectly")))
{
// This is the true indirect call case, where either the callee's address is taken, or it belongs
// to an external module. We do not know the callgraph in this case.
hasStackCall = true;
FG->m_hasIndirectCall = true;
FG->m_hasPartialCallGraph = true;
}
else if (calledF && calledF->hasFnAttribute("referenced-indirectly"))
{
// This is the case where the callee has the "referenced-indirectly" attribute, but we still
// see the callgraph. The callee may not belong to the same FG as the caller, but it's CG is still available.
hasStackCall = true;
FG->m_hasIndirectCall = true;
}
else if (calledF && calledF->isDeclaration() && calledF->hasFnAttribute("invoke_simd_target"))
{
// Invoke_simd targets use stack call by convention.
// Calling a func decl indicates unknown CG
hasStackCall = true;
FG->m_hasPartialCallGraph = true;
}
FG->m_hasStackCall |= hasStackCall;
FG->m_hasNestedCall |= (!isEntryFunc(pMdUtils, F) && hasStackCall);
}
}
}
}
}
void GenXFunctionGroupAnalysis::addIndirectFuncsToKernelGroup(llvm::Module* pModule)
{
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
// Find all indirectly called functions that require a symbol
SmallVector<Function*, 16> indirectFunctions;
for (auto I = pModule->begin(), E = pModule->end(); I != E; ++I)
{
Function* F = &(*I);
if (F->isDeclaration() || isEntryFunc(pMdUtils, F)) continue;
if (F->hasFnAttribute("referenced-indirectly") ||
F->hasFnAttribute("variant-function-def"))
{
IGC_ASSERT(getGroup(F) == nullptr);
indirectFunctions.push_back(F);
}
}
// Add them to the indirect call group
if (!indirectFunctions.empty())
{
FunctionGroup* IFG = nullptr;
for (auto F : indirectFunctions)
{
if (F->hasFnAttribute("variant-function-def"))
{
// If function variant is already created (meaning this is a rebuild), add them to the correct SIMD group
auto [symStr, fName, vecLen] = IGC::ParseVectorVariantFunctionString(F->getName().str());
IFG = getOrCreateIndirectCallGroup(pModule, vecLen);
}
else
{
IFG = getOrCreateIndirectCallGroup(pModule);
}
IGC_ASSERT(IFG);
addToFunctionGroup(F, IFG, F);
}
}
}
void GenXFunctionGroupAnalysis::CloneFunctionGroupForMultiSIMDCompile(llvm::Module* pModule)
{
CodeGenContext* pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
// Don't clone if forcing SIMD size
if (pCtx->getModuleMetaData()->csInfo.forcedSIMDSize != 0)
return;
int default_size = getDefaultSIMDSize(pCtx);
// All "referenced-indirectly" funcs should already be added to the default group
FunctionGroup* ICG = getOrCreateIndirectCallGroup(pModule);
if (!ICG || ICG->isSingle())
return;
// For the first iteration, clone only functions called from other function groups
// that have multiple SIMD size requirements. This is to get a baseline of how many variant
// dummy kernels are required.
for (Function* F : *ICG)
{
// Check if there are multiple callers with variant SIMD size requirements.
int hasReqdSIMD = 0;
if (!F->hasFnAttribute("referenced-indirectly"))
continue;
// Assume that if a FG uses the address of a function in the IndirectCallGroup,
// then we should compile the IndirectCallGroup with the subgroup size of the user FG.
DenseMap<Instruction*, int> UsersMap;
for (auto U : F->users())
{
if (Instruction* inst = dyn_cast<Instruction>(U))
{
Function* callerF = inst->getParent()->getParent();
if (!isIndirectCallGroup(callerF))
{
auto FG = getGroup(callerF);
auto funcInfoMD = pMdUtils->getFunctionsInfoItem(FG->getHead());
int sz = funcInfoMD->getSubGroupSize()->getSIMDSize();
if (sz != 0)
{
IGC_ASSERT(sz == 8 || sz == 16 || sz == 32);
hasReqdSIMD |= sz;
UsersMap[inst] = sz;
}
}
}
}
if (hasReqdSIMD > 0)
{
bool ReqMultipleSIMD = hasReqdSIMD != 8 && hasReqdSIMD != 16 && hasReqdSIMD != 32;
if (IGC_IS_FLAG_ENABLED(EnableSIMDVariantCompilation) && ReqMultipleSIMD)
{
for (int i = 0; i < 3; i++)
{
int simdsz = 1 << (i + 3);
if ((hasReqdSIMD & simdsz) && simdsz != default_size)
{
// Clone the function
// Use the function vector variant syntax for mangling func name
string prefix = "_ZGVxN" + std::to_string(simdsz) + "_";
Function* FCloned = cloneFunc(pMdUtils, F, prefix, "");
copyFuncProperties(FCloned, F);
FCloned->addFnAttr("variant-function-def");
auto pNewICG = getOrCreateIndirectCallGroup(pModule, simdsz);
addToFunctionGroup(FCloned, pNewICG, FCloned);
for (const auto &iter : UsersMap)
{
if (iter.second == simdsz)
{
iter.first->replaceUsesOfWith(F, FCloned);
}
}
}
}
}
else
{
IGC_ASSERT_MESSAGE(!ReqMultipleSIMD, "SIMD variant compilation not supported");
continue;
}
}
}
auto hasIndirectCaller = [this](Function* F, FunctionGroup* ICG)
{
for (auto U : F->users()) {
if (Instruction* I = dyn_cast<Instruction>(U)) {
Function* callerF = I->getParent()->getParent();
if (getGroup(callerF) == ICG)
return true;
}
}
return false;
};
// In second iteration, clone functions that have callers inside the indirect call group.
// These functions may have been missed in the first iteration if they are not used outside
// the indirect call group, but need to be cloned to each variant dummy kernel we create.
for (Function* F : *ICG)
{
if (F == ICG->getHead() || !hasIndirectCaller(F, ICG))
continue;
for (int i = 0; i < 3; i++)
{
int simdsz = 1 << (i + 3);
auto ICG = IndirectCallGroup[i];
if (ICG && simdsz != default_size)
{
string prefix = "_ZGVxN" + std::to_string(simdsz) + "_";
// If mangled function already exist, don't re-clone it
if (pModule->getFunction(prefix + F->getName().str()) != nullptr)
continue;
Function* FCloned = cloneFunc(pMdUtils, F, prefix, "");
copyFuncProperties(FCloned, F);
FCloned->addFnAttr("variant-function-def");
Function* SubGrpH = useStackCall(F) ? FCloned : ICG->getHead();
addToFunctionGroup(FCloned, ICG, SubGrpH);
// update the edge after clone
for (auto UI = F->use_begin(), UE = F->use_end(); UI != UE; /*empty*/)
{
// Increment iterator after setting U to change the use.
Use* U = &*UI++;
Function* Caller = cast<CallInst>(U->getUser())->getParent()->getParent();
FunctionGroup* InFG = getGroup(Caller);
Function* InSubGrpH = useStackCall(F) ? FCloned : getSubGroupMap(Caller);
if (InFG == ICG && InSubGrpH == SubGrpH)
{
*U = FCloned;
}
}
}
}
}
}
bool GenXFunctionGroupAnalysis::rebuild(llvm::Module* Mod) {
clear();
auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
// Re-add all indirect functions to the default kernel group
addIndirectFuncsToKernelGroup(Mod);
// Build and verify function list layout.
// Given a list of functions, [K1, A, B, K2, C, K3, D, E, F], we build groups
// [K1, A, B], [K2, C], [K3, D, E, F] and verify that none of CallInst escapes
// from its group. It is rather cheap to build and verify when there is no
// subroutine in this module.
//
FunctionGroup* CurFG = nullptr;
Function* CurSubGrpH = nullptr;
for (auto I = Mod->begin(), E = Mod->end(); I != E; ++I)
{
Function* F = &(*I);
// Skip declarations.
if (F->empty())
continue;
// Skip functions belonging to the indirect call group
if (isIndirectCallGroup(F))
continue;
if (isEntryFunc(pMdUtils, F))
{
CurFG = createFunctionGroup(F);
CurSubGrpH = F;
}
else
{
if (useStackCall(F))
CurSubGrpH = F;
if (CurFG && CurSubGrpH)
addToFunctionGroup(F, CurFG, CurSubGrpH);
else
{
clear();
return false;
}
}
}
// Reset attribute flags
setGroupAttributes();
// Verification.
if (!verify())
{
clear();
return false;
}
// Commit.
M = Mod;
return true;
}
void GenXFunctionGroupAnalysis::replaceEntryFunc(Function* OF, Function* NF)
{
auto groupMapIter = GroupMap.find(OF);
FunctionGroup* FG = groupMapIter->second;
GroupMap.erase(groupMapIter);
GroupMap.insert(std::make_pair(NF, FG));
FG->replaceGroupHead(OF, NF);
// For Entry func, SubGroupMap needs to be updated as well
auto SGIter = SubGroupMap.find(OF);
if (SGIter != SubGroupMap.end())
{
SubGroupMap.erase(SGIter);
SubGroupMap.insert(std::make_pair(NF, NF));
}
DenseMap<const Function*, Function*>::iterator SGII, SGIE;
for (SGII = SubGroupMap.begin(), SGIE = SubGroupMap.end();
SGII != SGIE; ++SGII)
{
Function* SGH = SGII->second;
if (SGH == OF)
{
SGII->second = NF;
}
}
}
void GenXFunctionGroupAnalysis::clear()
{
GroupMap.clear();
SubGroupMap.clear();
for (auto I = begin(), E = end(); I != E; ++I)
delete* I;
Groups.clear();
for (auto FG : IndirectCallGroup) FG = nullptr;
M = nullptr;
}
FunctionGroup* GenXFunctionGroupAnalysis::getGroup(const Function* F)
{
auto I = GroupMap.find(F);
if (I == GroupMap.end())
return nullptr;
return I->second;
}
FunctionGroup* GenXFunctionGroupAnalysis::getGroupForHead(const Function* F)
{
auto FG = getGroup(F);
if (FG->getHead() == F)
return FG;
return nullptr;
}
void GenXFunctionGroupAnalysis::addToFunctionGroup(Function* F,
FunctionGroup* FG,
Function* SubGrpH)
{
IGC_ASSERT_MESSAGE(!GroupMap[F], "Function already attached to FunctionGroup");
GroupMap[F] = FG;
setSubGroupMap(F, SubGrpH);
if (F == SubGrpH)
{
auto* SubGrp = new llvm::SmallVector<llvm::AssertingVH<llvm::Function>, 8>();
SubGrp->push_back(F);
FG->Functions.push_back(SubGrp);
}
else
{
for (auto I = FG->Functions.begin(), E = FG->Functions.end(); I != E; I++)
{
auto* SubGrp = (*I);
IGC_ASSERT(nullptr != SubGrp);
if (SubGrp->front() == SubGrpH)
{
SubGrp->push_back(F);
return;
}
}
IGC_ASSERT(0);
}
}
FunctionGroup* GenXFunctionGroupAnalysis::createFunctionGroup(Function* F)
{
auto FG = new FunctionGroup;
Groups.push_back(FG);
addToFunctionGroup(F, FG, F);
return FG;
}
void GenXFunctionGroupAnalysis::print(raw_ostream& os)
{
if (!M)
{
os << "(nil)\n";
return;
}
unsigned TotalSize = 0;
for (auto GI = begin(), GE = end(); GI != GE; ++GI)
{
for (auto SubGI = (*GI)->Functions.begin(), SubGE = (*GI)->Functions.end();
SubGI != SubGE; ++SubGI)
{
for (auto FI = (*SubGI)->begin(), FE = (*SubGI)->end(); FI != FE; ++FI)
{
Function* F = *FI;
unsigned Size = std::accumulate(
F->begin(), F->end(), 0,
[](unsigned s, BasicBlock& BB) { return (unsigned)BB.size() + s; });
TotalSize += Size;
if (F == (*GI)->getHead())
os << "K: " << F->getName() << " [" << Size << "]\n";
else if (F == (*SubGI)->front())
os << " F: " << F->getName() << " [" << Size << "]\n";
else
os << " " << F->getName() << " [" << Size << "]\n";
}
}
}
os << "Module: " << M->getModuleIdentifier() << " [" << TotalSize << "]\n";
}
#if defined(_DEBUG)
void GenXFunctionGroupAnalysis::dump() {
print(llvm::errs());
}
#endif
namespace {
/// \brief Custom inliner for subroutines.
class SubroutineInliner : public LegacyInlinerBase, public llvm::InstVisitor<SubroutineInliner>
{
EstimateFunctionSize* FSA = nullptr;
MetaDataUtilsWrapper* MDUW = nullptr;
public:
static char ID; // Pass identification, replacement for typeid
// Use extremely low threshold.
SubroutineInliner()
: LegacyInlinerBase(ID, /*InsertLifetime*/ false),
FSA(nullptr) {}
InlineCost getInlineCost(IGCLLVM::CallSiteRef CS) override;
void getAnalysisUsage(AnalysisUsage& AU) const override;
bool runOnSCC(CallGraphSCC& SCC) override;
void verifyAddrSpaceMismatch(CallGraphSCC& SCC);
void visitGetElementPtrInst(GetElementPtrInst& I);
void visitMemCpyInst(MemCpyInst& I);
using llvm::Pass::doFinalization;
bool doFinalization(CallGraph& CG) override {
bool Changed = removeDeadFunctions(CG);
Changed |= purgeMetaDataUtils(CG.getModule(), MDUW);
return Changed;
}
};
} // namespace
IGC_INITIALIZE_PASS_BEGIN(SubroutineInliner, "SubroutineInliner", "SubroutineInliner", false, false)
IGC_INITIALIZE_PASS_DEPENDENCY(EstimateFunctionSize)
IGC_INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(SubroutineInliner, "SubroutineInliner", "SubroutineInliner", false, false)
char SubroutineInliner::ID = 0;
void SubroutineInliner::getAnalysisUsage(AnalysisUsage& AU) const
{
AU.addRequired<EstimateFunctionSize>();
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<MetaDataUtilsWrapper>();
LegacyInlinerBase::getAnalysisUsage(AU);
}
void SubroutineInliner::visitGetElementPtrInst(GetElementPtrInst& GEPI)
{
for (auto* useOfGEPI : GEPI.users())
{
if (LoadInst* loadInst = dyn_cast<LoadInst>(useOfGEPI))
{
auto GepReturnValueAS = GEPI.getPointerAddressSpace();
auto LoadOperandAS = loadInst->getPointerAddressSpace();
if (GepReturnValueAS != LoadOperandAS)
{
auto* GEPIPointerOperand = GEPI.getPointerOperand();
SmallVector<llvm::Value*, 8> Idx(GEPI.idx_begin(), GEPI.idx_end());
// we need to create a new GEPI because the old one has coded old AS,
// and we can not create new load instruction with the old GEPI with the correct AS
// This is WA for a bug in LLVM 11.
GetElementPtrInst* newGEPI = GetElementPtrInst::Create(GEPI.getSourceElementType(), GEPIPointerOperand, Idx, "", &GEPI);
newGEPI->setIsInBounds(GEPI.isInBounds());
newGEPI->setDebugLoc(GEPI.getDebugLoc());
auto* newLoad = new LoadInst(loadInst->getType(), newGEPI, "", loadInst);
newLoad->setAlignment(IGCLLVM::getAlign(*loadInst));
loadInst->replaceAllUsesWith(newLoad);
newLoad->setDebugLoc(loadInst->getDebugLoc());
}
}
}
}
void SubroutineInliner::visitMemCpyInst(MemCpyInst& I)
{
Value* Src = I.getRawSource();
Value* Dst = I.getRawDest();
Value* origSrc = I.getSource();
Value* origDst = I.getDest();
// Copying from alloca to alloca, but has addrspace mismatch due to incorrect bitcast
if (isa<AllocaInst>(origSrc) && isa<AllocaInst>(origDst))
{
if (origSrc->getType()->getPointerAddressSpace() != Src->getType()->getPointerAddressSpace())
{
Value* SrcCast = BitCastInst::Create(Instruction::BitCast, origSrc,
IGCLLVM::getWithSamePointeeType(dyn_cast<PointerType>(Src->getType()), origSrc->getType()->getPointerAddressSpace()),
"", &I);
I.replaceUsesOfWith(Src, SrcCast);
}
if (origDst->getType()->getPointerAddressSpace() != Dst->getType()->getPointerAddressSpace())
{
Value* DstCast = BitCastInst::Create(Instruction::BitCast, origDst,
IGCLLVM::getWithSamePointeeType(dyn_cast<PointerType>(Dst->getType()), origDst->getType()->getPointerAddressSpace()),
"", &I);
I.replaceUsesOfWith(Dst, DstCast);
}
}
}
// When this pass encounters a byVal argument, it creates an alloca to then copy the data from global memory to local memory.
// When creating a new alloca, it replaces all occurrences of the argument in the function with that alloca.
// Problems arises when the pointer operant (or more precisely its address space) is replaced:
// 1. In GetElementPtrInst, the resulting pointer of this instruction is in a different address space.
// On the other hand, a load instruction that uses the returned GetElementPtrInst pointer still operates on the old address space.
// By which we are referring to the wrong area of memory. The resolution for this problem is to create new load instruction.
// 2. In MemCpyInst, specifically generated for structs used in loops, where two allocas of the same struct type are created used
// to save and restore struct values. When one is copied to another, this pass incorrectly uses the addrspace of the ByVal argument
// instead of the local addrspace of the alloca. We fix this by casting the src and dst of the memcpy to the correct addrspace.
// This is WA for a bug in LLVM 11.
void SubroutineInliner::verifyAddrSpaceMismatch(CallGraphSCC& SCC)
{
for (CallGraphNode* Node : SCC)
{
Function* F = Node->getFunction();
if (F) visit(F);
}
}
bool SubroutineInliner::runOnSCC(CallGraphSCC& SCC)
{
FSA = &getAnalysis<EstimateFunctionSize>();
MDUW = &getAnalysis<MetaDataUtilsWrapper>();
bool changed = LegacyInlinerBase::runOnSCC(SCC);
if (changed) verifyAddrSpaceMismatch(SCC);
return changed;
}
/// \brief Get the inline cost for the subroutine-inliner.
///
InlineCost SubroutineInliner::getInlineCost(IGCLLVM::CallSiteRef CS)
{
Function* Callee = CS.getCalledFunction();
Function* Caller = CS.getCaller();
CodeGenContext* pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
// Inline direct calls to functions with always inline attribute or a function
// whose estimated size is under certain predefined limit.
if (Callee && !Callee->isDeclaration() && isInlineViable(*Callee)
#if LLVM_VERSION_MAJOR >= 11
.isSuccess()
#endif
)
{
if (CS.hasFnAttr(llvm::Attribute::AlwaysInline))
return llvm::InlineCost::getAlways("Per AlwaysInline function attribute");
int FCtrl = getFunctionControl(pCtx);
if (IGC::ForceAlwaysInline(pCtx))
return llvm::InlineCost::getAlways("IGC set force always inline");
if (pCtx->m_enableSubroutine == false)
return llvm::InlineCost::getAlways("Disabled subroutines/stackcalls");
if (Callee->hasFnAttribute(llvm::Attribute::NoInline))
return llvm::InlineCost::getNever("Per NoInline function attribute");
if (Callee->hasFnAttribute("KMPLOCK"))
return llvm::InlineCost::getNever("Has KMPLOCK function attribute");
if (Callee->hasFnAttribute("igc-force-stackcall"))
return llvm::InlineCost::getNever("Has igc-force-stackcall function attribute");
if (FCtrl == FLAG_FCALL_DEFAULT)
{
std::size_t PerFuncThreshold = IGC_GET_FLAG_VALUE(SubroutineInlinerThreshold);
// A single block function containing only a few instructions.
auto isTrivialCall = [](const llvm::Function* F) {
if (!F->empty() && F->size() == 1)
return F->front().size() <= 5;
return false;
};
if (FSA->getExpandedSize(Caller) <= PerFuncThreshold)
{
return llvm::InlineCost::getAlways("Caller size smaller than per func. threshold");
}
else if (isTrivialCall(Callee) || FSA->onlyCalledOnce(Callee))
{
return llvm::InlineCost::getAlways("Callee is called only once");
}
else if (!FSA->shouldEnableSubroutine())
{
// This function returns true if the estimated total inlining size exceeds some module threshold.
// If we don't exceed it, and there's no preference on inline vs noinline, we just inline.
return llvm::InlineCost::getAlways("Did not meet inline per function size threshold");
}
}
}
return llvm::InlineCost::getNever("Did not meet any inlining conditions");
}
Pass* IGC::createSubroutineInlinerPass()
{
initializeSubroutineInlinerPass(*PassRegistry::getPassRegistry());
return new SubroutineInliner();
}
bool FunctionGroup::checkSimdModeValid(SIMDMode Mode) const {
switch (Mode) {
default:
break;
case SIMDMode::SIMD8: return SIMDModeValid[0];
case SIMDMode::SIMD16: return SIMDModeValid[1];
case SIMDMode::SIMD32: return SIMDModeValid[2];
}
return true;
}
void FunctionGroup::setSimdModeInvalid(SIMDMode Mode) {
switch (Mode) {
default:
break;
case SIMDMode::SIMD8: SIMDModeValid[0] = false; break;
case SIMDMode::SIMD16: SIMDModeValid[1] = false; break;
case SIMDMode::SIMD32: SIMDModeValid[2] = false; break;
}
}
|