diff options
author | Dehao Chen <dehao@google.com> | 2015-10-21 01:22:27 +0000 |
---|---|---|
committer | Dehao Chen <dehao@google.com> | 2015-10-21 01:22:27 +0000 |
commit | 100424124bf1f2845c8cac18e39333cf5f04b1ae (patch) | |
tree | 92f5b929fba79726bf7d69543d9fa218ecb27963 /llvm/lib/Transforms | |
parent | 4c3f2b944689bdb23e929ab27815bbc87c20c035 (diff) | |
download | bcm5719-llvm-100424124bf1f2845c8cac18e39333cf5f04b1ae.tar.gz bcm5719-llvm-100424124bf1f2845c8cac18e39333cf5f04b1ae.zip |
Tolerate negative offset when matching sample profile.
In some cases (as illustrated in the unittest), lineno can be less than the heade_lineno because the function body are included from some other files. In this case, offset will be negative. This patch makes clang still able to match the profile to IR in this situation.
http://reviews.llvm.org/D13914
llvm-svn: 250873
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r-- | llvm/lib/Transforms/IPO/SampleProfile.cpp | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index e0691515c95..b797321b2f8 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -122,6 +122,7 @@ protected: void buildEdges(Function &F); bool propagateThroughEdges(Function &F); void computeDominanceAndLoopInfo(Function &F); + unsigned getOffset(unsigned L, unsigned H) const; /// \brief Map basic blocks to their computed weights. /// @@ -174,6 +175,17 @@ protected: }; } +/// \brief Returns the offset of lineno \p L to head_lineno \p H +/// +/// \param L Lineno +/// \param H Header lineno of the function +/// +/// \returns offset to the header lineno. 16 bits are used to represent offset. +/// We assume that a single function will not exceed 65535 LOC. +unsigned SampleProfileLoader::getOffset(unsigned L, unsigned H) const { + return (L - H) & 0xffff; +} + /// \brief Print the weight of edge \p E on stream \p OS. /// /// \param OS Stream to emit the output to. @@ -229,11 +241,9 @@ SampleProfileLoader::getInstWeight(const Instruction &Inst) const { const DILocation *DIL = DLoc; unsigned Lineno = DLoc.getLine(); unsigned HeaderLineno = DIL->getScope()->getSubprogram()->getLine(); - if (Lineno < HeaderLineno) - return std::error_code(); - ErrorOr<uint64_t> R = - FS->findSamplesAt(Lineno - HeaderLineno, DIL->getDiscriminator()); + ErrorOr<uint64_t> R = FS->findSamplesAt(getOffset(Lineno, HeaderLineno), + DIL->getDiscriminator()); if (R) DEBUG(dbgs() << " " << Lineno << "." << DIL->getDiscriminator() << ":" << Inst << " (line offset: " << Lineno - HeaderLineno << "." @@ -308,7 +318,7 @@ SampleProfileLoader::findCalleeFunctionSamples(const CallInst &Inst) const { return nullptr; } DISubprogram *SP = DIL->getScope()->getSubprogram(); - if (!SP || DIL->getLine() < SP->getLine()) + if (!SP) return nullptr; Function *CalleeFunc = Inst.getCalledFunction(); @@ -321,8 +331,9 @@ SampleProfileLoader::findCalleeFunctionSamples(const CallInst &Inst) const { if (FS == nullptr) return nullptr; - return FS->findFunctionSamplesAt(CallsiteLocation( - DIL->getLine() - SP->getLine(), DIL->getDiscriminator(), CalleeName)); + return FS->findFunctionSamplesAt( + CallsiteLocation(getOffset(DIL->getLine(), SP->getLine()), + DIL->getDiscriminator(), CalleeName)); } /// \brief Get the FunctionSamples for an instruction. @@ -345,10 +356,10 @@ SampleProfileLoader::findFunctionSamples(const Instruction &Inst) const { for (const DILocation *DIL = Inst.getDebugLoc(); DIL; DIL = DIL->getInlinedAt()) { DISubprogram *SP = DIL->getScope()->getSubprogram(); - if (!SP || DIL->getLine() < SP->getLine()) + if (!SP) return nullptr; if (!CalleeName.empty()) { - S.push_back(CallsiteLocation(DIL->getLine() - SP->getLine(), + S.push_back(CallsiteLocation(getOffset(DIL->getLine(), SP->getLine()), DIL->getDiscriminator(), CalleeName)); } CalleeName = SP->getLinkageName(); |