|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 |
|
14 | 14 | #include "llvm/Transforms/Utils/CallPromotionUtils.h"
|
15 |
| -#include "llvm/ADT/STLExtras.h" |
| 15 | +#include "llvm/Analysis/CtxProfAnalysis.h" |
16 | 16 | #include "llvm/Analysis/Loads.h"
|
17 | 17 | #include "llvm/Analysis/TypeMetadataUtils.h"
|
18 | 18 | #include "llvm/IR/AttributeMask.h"
|
19 | 19 | #include "llvm/IR/Constant.h"
|
20 | 20 | #include "llvm/IR/IRBuilder.h"
|
21 | 21 | #include "llvm/IR/Instructions.h"
|
| 22 | +#include "llvm/IR/IntrinsicInst.h" |
22 | 23 | #include "llvm/IR/Module.h"
|
| 24 | +#include "llvm/ProfileData/PGOCtxProfReader.h" |
23 | 25 | #include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
24 | 26 |
|
25 | 27 | using namespace llvm;
|
@@ -572,6 +574,88 @@ CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
|
572 | 574 | return promoteCall(NewInst, Callee);
|
573 | 575 | }
|
574 | 576 |
|
| 577 | +CallBase *llvm::promoteCallWithIfThenElse(CallBase &CB, Function &Callee, |
| 578 | + PGOContextualProfile &CtxProf) { |
| 579 | + assert(CB.isIndirectCall()); |
| 580 | + if (!CtxProf.isFunctionKnown(Callee)) |
| 581 | + return nullptr; |
| 582 | + auto &Caller = *CB.getFunction(); |
| 583 | + auto *CSInstr = CtxProfAnalysis::getCallsiteInstrumentation(CB); |
| 584 | + if (!CSInstr) |
| 585 | + return nullptr; |
| 586 | + const uint64_t CSIndex = CSInstr->getIndex()->getZExtValue(); |
| 587 | + |
| 588 | + CallBase &DirectCall = promoteCall( |
| 589 | + versionCallSite(CB, &Callee, /*BranchWeights=*/nullptr), &Callee); |
| 590 | + CSInstr->moveBefore(&CB); |
| 591 | + const auto NewCSID = CtxProf.allocateNextCallsiteIndex(Caller); |
| 592 | + auto *NewCSInstr = cast<InstrProfCallsite>(CSInstr->clone()); |
| 593 | + NewCSInstr->setIndex(NewCSID); |
| 594 | + NewCSInstr->setCallee(&Callee); |
| 595 | + NewCSInstr->insertBefore(&DirectCall); |
| 596 | + auto &DirectBB = *DirectCall.getParent(); |
| 597 | + auto &IndirectBB = *CB.getParent(); |
| 598 | + |
| 599 | + assert((CtxProfAnalysis::getBBInstrumentation(IndirectBB) == nullptr) && |
| 600 | + "The ICP direct BB is new, it shouldn't have instrumentation"); |
| 601 | + assert((CtxProfAnalysis::getBBInstrumentation(DirectBB) == nullptr) && |
| 602 | + "The ICP indirect BB is new, it shouldn't have instrumentation"); |
| 603 | + |
| 604 | + // Allocate counters for the new basic blocks. |
| 605 | + const uint32_t DirectID = CtxProf.allocateNextCounterIndex(Caller); |
| 606 | + const uint32_t IndirectID = CtxProf.allocateNextCounterIndex(Caller); |
| 607 | + auto *EntryBBIns = |
| 608 | + CtxProfAnalysis::getBBInstrumentation(Caller.getEntryBlock()); |
| 609 | + auto *DirectBBIns = cast<InstrProfCntrInstBase>(EntryBBIns->clone()); |
| 610 | + DirectBBIns->setIndex(DirectID); |
| 611 | + DirectBBIns->insertInto(&DirectBB, DirectBB.getFirstInsertionPt()); |
| 612 | + |
| 613 | + auto *IndirectBBIns = cast<InstrProfCntrInstBase>(EntryBBIns->clone()); |
| 614 | + IndirectBBIns->setIndex(IndirectID); |
| 615 | + IndirectBBIns->insertInto(&IndirectBB, IndirectBB.getFirstInsertionPt()); |
| 616 | + |
| 617 | + const GlobalValue::GUID CalleeGUID = AssignGUIDPass::getGUID(Callee); |
| 618 | + const uint32_t NewCountersSize = IndirectID + 1; |
| 619 | + |
| 620 | + auto ProfileUpdater = [&](PGOCtxProfContext &Ctx) { |
| 621 | + assert(Ctx.guid() == AssignGUIDPass::getGUID(Caller)); |
| 622 | + assert(NewCountersSize - 2 == Ctx.counters().size()); |
| 623 | + // All the ctx-es belonging to a function must have the same size counters. |
| 624 | + Ctx.resizeCounters(NewCountersSize); |
| 625 | + |
| 626 | + // Maybe in this context, the indirect callsite wasn't observed at all |
| 627 | + if (!Ctx.hasCallsite(CSIndex)) |
| 628 | + return; |
| 629 | + auto &CSData = Ctx.callsite(CSIndex); |
| 630 | + auto It = CSData.find(CalleeGUID); |
| 631 | + |
| 632 | + // Maybe we did notice the indirect callsite, but to other targets. |
| 633 | + if (It == CSData.end()) |
| 634 | + return; |
| 635 | + |
| 636 | + assert(CalleeGUID == It->second.guid()); |
| 637 | + |
| 638 | + uint32_t DirectCount = It->second.getEntrycount(); |
| 639 | + uint32_t TotalCount = 0; |
| 640 | + for (const auto &[_, V] : CSData) |
| 641 | + TotalCount += V.getEntrycount(); |
| 642 | + assert(TotalCount >= DirectCount); |
| 643 | + uint32_t IndirectCount = TotalCount - DirectCount; |
| 644 | + // The ICP's effect is as-if the direct BB would have been taken DirectCount |
| 645 | + // times, and the indirect BB, IndirectCount times |
| 646 | + Ctx.counters()[DirectID] = DirectCount; |
| 647 | + Ctx.counters()[IndirectID] = IndirectCount; |
| 648 | + |
| 649 | + // This particular indirect target needs to be moved to this caller under |
| 650 | + // the newly-allocated callsite index. |
| 651 | + assert(Ctx.callsites().count(NewCSID) == 0); |
| 652 | + Ctx.ingestContext(NewCSID, std::move(It->second)); |
| 653 | + CSData.erase(CalleeGUID); |
| 654 | + }; |
| 655 | + CtxProf.update(ProfileUpdater, &Caller); |
| 656 | + return &DirectCall; |
| 657 | +} |
| 658 | + |
575 | 659 | CallBase &llvm::promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr,
|
576 | 660 | Function *Callee,
|
577 | 661 | ArrayRef<Constant *> AddressPoints,
|
|
0 commit comments