-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[ctx_prof] Add support for ICP #105469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mtrofin
merged 1 commit into
main
from
users/mtrofin/08-20-_ctx_prof_add_support_for_icp
Aug 27, 2024
Merged
[ctx_prof] Add support for ICP #105469
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,16 @@ | |
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Utils/CallPromotionUtils.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Analysis/CtxProfAnalysis.h" | ||
#include "llvm/Analysis/Loads.h" | ||
#include "llvm/Analysis/TypeMetadataUtils.h" | ||
#include "llvm/IR/AttributeMask.h" | ||
#include "llvm/IR/Constant.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/ProfileData/PGOCtxProfReader.h" | ||
#include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||
|
||
using namespace llvm; | ||
|
@@ -572,6 +574,88 @@ CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee, | |
return promoteCall(NewInst, Callee); | ||
} | ||
|
||
CallBase *llvm::promoteCallWithIfThenElse(CallBase &CB, Function &Callee, | ||
PGOContextualProfile &CtxProf) { | ||
assert(CB.isIndirectCall()); | ||
if (!CtxProf.isFunctionKnown(Callee)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be useful to have some statistics on how many promoted / dropped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems to belong in the pass that will exercise this, rather. |
||
return nullptr; | ||
auto &Caller = *CB.getFunction(); | ||
auto *CSInstr = CtxProfAnalysis::getCallsiteInstrumentation(CB); | ||
if (!CSInstr) | ||
return nullptr; | ||
const uint64_t CSIndex = CSInstr->getIndex()->getZExtValue(); | ||
|
||
CallBase &DirectCall = promoteCall( | ||
versionCallSite(CB, &Callee, /*BranchWeights=*/nullptr), &Callee); | ||
CSInstr->moveBefore(&CB); | ||
const auto NewCSID = CtxProf.allocateNextCallsiteIndex(Caller); | ||
auto *NewCSInstr = cast<InstrProfCallsite>(CSInstr->clone()); | ||
NewCSInstr->setIndex(NewCSID); | ||
NewCSInstr->setCallee(&Callee); | ||
NewCSInstr->insertBefore(&DirectCall); | ||
auto &DirectBB = *DirectCall.getParent(); | ||
auto &IndirectBB = *CB.getParent(); | ||
|
||
assert((CtxProfAnalysis::getBBInstrumentation(IndirectBB) == nullptr) && | ||
"The ICP direct BB is new, it shouldn't have instrumentation"); | ||
assert((CtxProfAnalysis::getBBInstrumentation(DirectBB) == nullptr) && | ||
"The ICP indirect BB is new, it shouldn't have instrumentation"); | ||
|
||
// Allocate counters for the new basic blocks. | ||
const uint32_t DirectID = CtxProf.allocateNextCounterIndex(Caller); | ||
const uint32_t IndirectID = CtxProf.allocateNextCounterIndex(Caller); | ||
auto *EntryBBIns = | ||
CtxProfAnalysis::getBBInstrumentation(Caller.getEntryBlock()); | ||
auto *DirectBBIns = cast<InstrProfCntrInstBase>(EntryBBIns->clone()); | ||
DirectBBIns->setIndex(DirectID); | ||
DirectBBIns->insertInto(&DirectBB, DirectBB.getFirstInsertionPt()); | ||
|
||
auto *IndirectBBIns = cast<InstrProfCntrInstBase>(EntryBBIns->clone()); | ||
IndirectBBIns->setIndex(IndirectID); | ||
IndirectBBIns->insertInto(&IndirectBB, IndirectBB.getFirstInsertionPt()); | ||
|
||
const GlobalValue::GUID CalleeGUID = AssignGUIDPass::getGUID(Callee); | ||
const uint32_t NewCountersSize = IndirectID + 1; | ||
|
||
auto ProfileUpdater = [&](PGOCtxProfContext &Ctx) { | ||
assert(Ctx.guid() == AssignGUIDPass::getGUID(Caller)); | ||
assert(NewCountersSize - 2 == Ctx.counters().size()); | ||
// All the ctx-es belonging to a function must have the same size counters. | ||
Ctx.resizeCounters(NewCountersSize); | ||
|
||
// Maybe in this context, the indirect callsite wasn't observed at all | ||
if (!Ctx.hasCallsite(CSIndex)) | ||
return; | ||
auto &CSData = Ctx.callsite(CSIndex); | ||
auto It = CSData.find(CalleeGUID); | ||
|
||
// Maybe we did notice the indirect callsite, but to other targets. | ||
if (It == CSData.end()) | ||
return; | ||
|
||
assert(CalleeGUID == It->second.guid()); | ||
|
||
uint32_t DirectCount = It->second.getEntrycount(); | ||
uint32_t TotalCount = 0; | ||
for (const auto &[_, V] : CSData) | ||
TotalCount += V.getEntrycount(); | ||
assert(TotalCount >= DirectCount); | ||
uint32_t IndirectCount = TotalCount - DirectCount; | ||
// The ICP's effect is as-if the direct BB would have been taken DirectCount | ||
// times, and the indirect BB, IndirectCount times | ||
Ctx.counters()[DirectID] = DirectCount; | ||
Ctx.counters()[IndirectID] = IndirectCount; | ||
|
||
// This particular indirect target needs to be moved to this caller under | ||
// the newly-allocated callsite index. | ||
assert(Ctx.callsites().count(NewCSID) == 0); | ||
Ctx.ingestContext(NewCSID, std::move(It->second)); | ||
CSData.erase(CalleeGUID); | ||
}; | ||
CtxProf.update(ProfileUpdater, &Caller); | ||
return &DirectCall; | ||
} | ||
|
||
CallBase &llvm::promoteCallWithVTableCmp(CallBase &CB, Instruction *VPtr, | ||
Function *Callee, | ||
ArrayRef<Constant *> AddressPoints, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the default be the most verbose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdym?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! I think I understand what you mean. Yes, I think the default should be the most verbose, it's a testing facility.