diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h --- a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h @@ -87,12 +87,6 @@ class PseudoProbeManager { DenseMap GUIDToProbeDescMap; - const PseudoProbeDescriptor *getDesc(const Function &F) const { - auto I = GUIDToProbeDescMap.find( - Function::getGUID(FunctionSamples::getCanonicalFnName(F))); - return I == GUIDToProbeDescMap.end() ? nullptr : &I->second; - } - public: PseudoProbeManager(const Module &M) { if (NamedMDNode *FuncInfo = @@ -108,10 +102,21 @@ } } + const PseudoProbeDescriptor *getDesc(const Function &F) const { + auto I = GUIDToProbeDescMap.find( + Function::getGUID(FunctionSamples::getCanonicalFnName(F))); + return I == GUIDToProbeDescMap.end() ? nullptr : &I->second; + } + bool moduleIsProbed(const Module &M) const { return M.getNamedMetadata(PseudoProbeDescMetadataName); } + bool profileIsHashMismatched(const PseudoProbeDescriptor &FuncDesc, + const FunctionSamples &Samples) const { + return FuncDesc.getFunctionHash() != Samples.getFunctionHash(); + } + bool profileIsValid(const Function &F, const FunctionSamples &Samples) const { const auto *Desc = getDesc(F); if (!Desc) { diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -1819,7 +1819,14 @@ bool Changed = false; if (FunctionSamples::ProfileIsProbeBased) { - if (!ProbeManager->profileIsValid(F, *Samples)) { + const auto *FuncDesc = ProbeManager->getDesc(F); + if (!FuncDesc) { + LLVM_DEBUG(dbgs() << "There is no probe_desc for " << F.getName() + << " or it's an imported function." + << "\n"); + return false; + } + if (!ProbeManager->profileIsHashMismatched(*FuncDesc, *Samples)) { LLVM_DEBUG( dbgs() << "Profile is invalid due to CFG mismatch for Function " << F.getName() << "\n"); @@ -2268,7 +2275,8 @@ uint64_t Count = FS.getTotalSamples(); TotalFuncHashSamples += Count; TotalProfiledFunc++; - if (!ProbeManager->profileIsValid(F, FS)) { + const auto *FuncDesc = ProbeManager->getDesc(F); + if (FuncDesc && ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) { MismatchedFuncHashSamples += Count; NumMismatchedFuncHash++; IsFuncHashMismatch = true;