Skip to content

Commit 6e23cd2

Browse files
committed
[InstrProf][NFC] Save profile bias to function map
Add a map from functions to load instructions that compute the profile bias. Previously we assumed that if the first instruction in the function was a load instruction, then it must be computing the bias. This was likely to work out because functions usually start with the `llvm.instrprof.increment` instruction, but optimizations could change this. For example, inlining into a non-profiled function. Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D114319
1 parent be7f09f commit 6e23cd2

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class InstrProfiling : public PassInfoMixin<InstrProfiling> {
5656
}
5757
};
5858
DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
59+
/// If runtime relocation is enabled, this maps functions to the load
60+
/// instruction that produces the profile relocation bias.
61+
DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
5962
std::vector<GlobalValue *> CompilerUsedVars;
6063
std::vector<GlobalValue *> UsedVars;
6164
std::vector<GlobalVariable *> ReferencedNames;

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,9 @@ Value *InstrProfiling::getCounterAddress(InstrProfInstBase *I) {
703703

704704
Type *Int64Ty = Type::getInt64Ty(M->getContext());
705705
Function *Fn = I->getParent()->getParent();
706-
Instruction &EntryI = Fn->getEntryBlock().front();
707-
LoadInst *LI = dyn_cast<LoadInst>(&EntryI);
708-
if (!LI) {
709-
IRBuilder<> EntryBuilder(&EntryI);
706+
LoadInst *&BiasLI = FunctionToProfileBiasMap[Fn];
707+
if (!BiasLI) {
708+
IRBuilder<> EntryBuilder(&Fn->getEntryBlock().front());
710709
auto *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName());
711710
if (!Bias) {
712711
// Compiler must define this variable when runtime counter relocation
@@ -723,9 +722,9 @@ Value *InstrProfiling::getCounterAddress(InstrProfInstBase *I) {
723722
if (TT.supportsCOMDAT())
724723
Bias->setComdat(M->getOrInsertComdat(Bias->getName()));
725724
}
726-
LI = EntryBuilder.CreateLoad(Int64Ty, Bias);
725+
BiasLI = EntryBuilder.CreateLoad(Int64Ty, Bias);
727726
}
728-
auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI);
727+
auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
729728
return Builder.CreateIntToPtr(Add, Addr->getType());
730729
}
731730

0 commit comments

Comments
 (0)