diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h index 9fefefa627b1..a96f83620f8b 100644 --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -222,6 +222,21 @@ public: return ret->second.getSamples(); } + /// Return the total number of call target samples collected at a given + /// location. Each location is specified by \p LineOffset and + /// \p Discriminator. If the location is not found in profile, return error. + ErrorOr findCallSamplesAt(uint32_t LineOffset, + uint32_t Discriminator) const { + const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator)); + if (ret == BodySamples.end()) + return std::error_code(); + uint64_t T = 0; + for (const auto &t_c : ret->second.getCallTargets()) { + T += t_c.second; + } + return T; + } + /// Return the function samples at the given callsite location. FunctionSamples &functionSamplesAt(const LineLocation &Loc) { return CallsiteSamples[Loc]; diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index 9c4a40c44ca7..500686529ad1 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -480,7 +480,9 @@ SampleProfileLoader::getInstWeight(const Instruction &Inst) const { uint32_t LineOffset = getOffset(Lineno, HeaderLineno); uint32_t Discriminator = DIL->getDiscriminator(); - ErrorOr R = FS->findSamplesAt(LineOffset, Discriminator); + ErrorOr R = IsCall + ? FS->findCallSamplesAt(LineOffset, Discriminator) + : FS->findSamplesAt(LineOffset, Discriminator); if (R) { bool FirstMark = CoverageTracker.markSamplesUsed(FS, LineOffset, Discriminator, R.get()); @@ -1272,10 +1274,10 @@ bool SampleProfileLoader::emitAnnotations(Function &F) { char SampleProfileLoaderLegacyPass::ID = 0; INITIALIZE_PASS_BEGIN(SampleProfileLoaderLegacyPass, "sample-profile", - "Sample Profile loader", false, false) + "Sample Profile loader", false, false) INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) INITIALIZE_PASS_END(SampleProfileLoaderLegacyPass, "sample-profile", - "Sample Profile loader", false, false) + "Sample Profile loader", false, false) bool SampleProfileLoader::doInitialization(Module &M) { auto &Ctx = M.getContext(); diff --git a/llvm/test/Transforms/SampleProfile/Inputs/inline-coverage.prof b/llvm/test/Transforms/SampleProfile/Inputs/inline-coverage.prof index 3d792733149a..6f38a1ca8c78 100644 --- a/llvm/test/Transforms/SampleProfile/Inputs/inline-coverage.prof +++ b/llvm/test/Transforms/SampleProfile/Inputs/inline-coverage.prof @@ -4,4 +4,4 @@ main:501438:0 4: 0 0: 0 3: _Z3fool:172746 - 1: 31878 + 1: 31878 rand:31878