110
110
#include " llvm/Transforms/Instrumentation.h"
111
111
#include " llvm/Transforms/Instrumentation/BlockCoverageInference.h"
112
112
#include " llvm/Transforms/Instrumentation/CFGMST.h"
113
+ #include " llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
113
114
#include " llvm/Transforms/Utils/BasicBlockUtils.h"
114
115
#include " llvm/Transforms/Utils/MisExpect.h"
115
116
#include " llvm/Transforms/Utils/ModuleUtils.h"
@@ -333,6 +334,16 @@ extern cl::opt<bool> EnableVTableValueProfiling;
333
334
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
334
335
} // namespace llvm
335
336
337
+ bool shouldInstrumentEntryBB () {
338
+ return PGOInstrumentEntry ||
339
+ PGOCtxProfLoweringPass::isContextualIRPGOEnabled ();
340
+ }
341
+
342
+ bool isValueProfilingDisabled () {
343
+ return DisableValueProfiling ||
344
+ PGOCtxProfLoweringPass::isContextualIRPGOEnabled ();
345
+ }
346
+
336
347
// Return a string describing the branch condition that can be
337
348
// used in static branch probability heuristics:
338
349
static std::string getBranchCondString (Instruction *TI) {
@@ -379,7 +390,7 @@ static GlobalVariable *createIRLevelProfileFlagVar(Module &M, bool IsCS) {
379
390
uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
380
391
if (IsCS)
381
392
ProfileVersion |= VARIANT_MASK_CSIR_PROF;
382
- if (PGOInstrumentEntry )
393
+ if (shouldInstrumentEntryBB () )
383
394
ProfileVersion |= VARIANT_MASK_INSTR_ENTRY;
384
395
if (DebugInfoCorrelate || ProfileCorrelate == InstrProfCorrelator::DEBUG_INFO)
385
396
ProfileVersion |= VARIANT_MASK_DBG_CORRELATE;
@@ -861,7 +872,7 @@ static void instrumentOneFunc(
861
872
}
862
873
863
874
FuncPGOInstrumentation<PGOEdge, PGOBBInfo> FuncInfo (
864
- F, TLI, ComdatMembers, true , BPI, BFI, IsCS, PGOInstrumentEntry ,
875
+ F, TLI, ComdatMembers, true , BPI, BFI, IsCS, shouldInstrumentEntryBB () ,
865
876
PGOBlockCoverage);
866
877
867
878
auto Name = FuncInfo.FuncNameVar ;
@@ -883,6 +894,33 @@ static void instrumentOneFunc(
883
894
unsigned NumCounters =
884
895
InstrumentBBs.size () + FuncInfo.SIVisitor .getNumOfSelectInsts ();
885
896
897
+ if (PGOCtxProfLoweringPass::isContextualIRPGOEnabled ()) {
898
+ auto *CSIntrinsic =
899
+ Intrinsic::getDeclaration (M, Intrinsic::instrprof_callsite);
900
+ auto Visit = [&](llvm::function_ref<void (CallBase * CB)> Visitor) {
901
+ for (auto &BB : F)
902
+ for (auto &Instr : BB)
903
+ if (auto *CS = dyn_cast<CallBase>(&Instr)) {
904
+ if ((CS->getCalledFunction () &&
905
+ CS->getCalledFunction ()->isIntrinsic ()) ||
906
+ dyn_cast<InlineAsm>(CS->getCalledOperand ()))
907
+ continue ;
908
+ Visitor (CS);
909
+ }
910
+ };
911
+ uint32_t TotalNrCallsites = 0 ;
912
+ Visit ([&TotalNrCallsites](auto *) { ++TotalNrCallsites; });
913
+ uint32_t CallsiteIndex = 0 ;
914
+
915
+ Visit ([&](auto *CB) {
916
+ IRBuilder<> Builder (CB);
917
+ Builder.CreateCall (CSIntrinsic,
918
+ {Name, CFGHash, Builder.getInt32 (TotalNrCallsites),
919
+ Builder.getInt32 (CallsiteIndex++),
920
+ CB->getCalledOperand ()});
921
+ });
922
+ }
923
+
886
924
uint32_t I = 0 ;
887
925
if (PGOTemporalInstrumentation) {
888
926
NumCounters += PGOBlockCoverage ? 8 : 1 ;
@@ -914,7 +952,7 @@ static void instrumentOneFunc(
914
952
FuncInfo.FunctionHash );
915
953
assert (I == NumCounters);
916
954
917
- if (DisableValueProfiling )
955
+ if (isValueProfilingDisabled () )
918
956
return ;
919
957
920
958
NumOfPGOICall += FuncInfo.ValueSites [IPVK_IndirectCallTarget].size ();
@@ -1676,7 +1714,7 @@ void SelectInstVisitor::visitSelectInst(SelectInst &SI) {
1676
1714
1677
1715
// Traverse all valuesites and annotate the instructions for all value kind.
1678
1716
void PGOUseFunc::annotateValueSites () {
1679
- if (DisableValueProfiling )
1717
+ if (isValueProfilingDisabled () )
1680
1718
return ;
1681
1719
1682
1720
// Create the PGOFuncName meta data.
@@ -1779,7 +1817,7 @@ static bool InstrumentAllFunctions(
1779
1817
function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, bool IsCS) {
1780
1818
// For the context-sensitve instrumentation, we should have a separated pass
1781
1819
// (before LTO/ThinLTO linking) to create these variables.
1782
- if (!IsCS)
1820
+ if (!IsCS && ! PGOCtxProfLoweringPass::isContextualIRPGOEnabled () )
1783
1821
createIRLevelProfileFlagVar (M, /* IsCS=*/ false );
1784
1822
1785
1823
Triple TT (M.getTargetTriple ());
@@ -2015,9 +2053,8 @@ static bool annotateAllFunctions(
2015
2053
2016
2054
// If the profile marked as always instrument the entry BB, do the
2017
2055
// same. Note this can be overwritten by the internal option in CFGMST.h
2018
- bool InstrumentFuncEntry = PGOReader->instrEntryBBEnabled ();
2019
- if (PGOInstrumentEntry.getNumOccurrences () > 0 )
2020
- InstrumentFuncEntry = PGOInstrumentEntry;
2056
+ bool InstrumentFuncEntry =
2057
+ PGOReader->instrEntryBBEnabled () || shouldInstrumentEntryBB ();
2021
2058
bool HasSingleByteCoverage = PGOReader->hasSingleByteCoverage ();
2022
2059
for (auto &F : M) {
2023
2060
if (skipPGOUse (F))
0 commit comments