diff --git a/llvm/include/llvm/Analysis/MemoryProfileInfo.h b/llvm/include/llvm/Analysis/MemoryProfileInfo.h index f75783a4fef50..aa0848db4e68e 100644 --- a/llvm/include/llvm/Analysis/MemoryProfileInfo.h +++ b/llvm/include/llvm/Analysis/MemoryProfileInfo.h @@ -198,7 +198,7 @@ template class CallStack { CallStackIterator begin() const; CallStackIterator end() const { return CallStackIterator(N, /*End*/ true); } - CallStackIterator beginAfterSharedPrefix(CallStack &Other); + CallStackIterator beginAfterSharedPrefix(const CallStack &Other); uint64_t back() const; private: @@ -236,7 +236,7 @@ CallStack::begin() const { template typename CallStack::CallStackIterator -CallStack::beginAfterSharedPrefix(CallStack &Other) { +CallStack::beginAfterSharedPrefix(const CallStack &Other) { CallStackIterator Cur = begin(); for (CallStackIterator OtherCur = Other.begin(); Cur != end() && OtherCur != Other.end(); ++Cur, ++OtherCur) diff --git a/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp b/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp index ec158afb76519..4b2683dc6c2a7 100644 --- a/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp +++ b/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp @@ -5050,6 +5050,45 @@ bool MemProfContextDisambiguation::initializeIndirectCallPromotionInfo( return true; } +#ifndef NDEBUG +// Sanity check that the MIB stack ids match between the summary and +// instruction metadata. +static void checkAllocContextIds( + const AllocInfo &AllocNode, const MDNode *MemProfMD, + const CallStack &CallsiteContext, + const ModuleSummaryIndex *ImportSummary) { + auto MIBIter = AllocNode.MIBs.begin(); + for (auto &MDOp : MemProfMD->operands()) { + assert(MIBIter != AllocNode.MIBs.end()); + auto StackIdIndexIter = MIBIter->StackIdIndices.begin(); + auto *MIBMD = cast(MDOp); + MDNode *StackMDNode = getMIBStackNode(MIBMD); + assert(StackMDNode); + CallStack StackContext(StackMDNode); + auto ContextIterBegin = + StackContext.beginAfterSharedPrefix(CallsiteContext); + // Skip the checking on the first iteration. + uint64_t LastStackContextId = + (ContextIterBegin != StackContext.end() && *ContextIterBegin == 0) ? 1 + : 0; + for (auto ContextIter = ContextIterBegin; ContextIter != StackContext.end(); + ++ContextIter) { + // If this is a direct recursion, simply skip the duplicate + // entries, to be consistent with how the summary ids were + // generated during ModuleSummaryAnalysis. + if (LastStackContextId == *ContextIter) + continue; + LastStackContextId = *ContextIter; + assert(StackIdIndexIter != MIBIter->StackIdIndices.end()); + assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) == + *ContextIter); + StackIdIndexIter++; + } + MIBIter++; + } +} +#endif + bool MemProfContextDisambiguation::applyImport(Module &M) { assert(ImportSummary); bool Changed = false; @@ -5242,40 +5281,10 @@ bool MemProfContextDisambiguation::applyImport(Module &M) { assert(AI != FS->allocs().end()); auto &AllocNode = *(AI++); - // Sanity check that the MIB stack ids match between the summary and - // instruction metadata. - auto MIBIter = AllocNode.MIBs.begin(); - for (auto &MDOp : MemProfMD->operands()) { - assert(MIBIter != AllocNode.MIBs.end()); - LLVM_ATTRIBUTE_UNUSED auto StackIdIndexIter = - MIBIter->StackIdIndices.begin(); - auto *MIBMD = cast(MDOp); - MDNode *StackMDNode = getMIBStackNode(MIBMD); - assert(StackMDNode); - CallStack StackContext(StackMDNode); - auto ContextIterBegin = - StackContext.beginAfterSharedPrefix(CallsiteContext); - // Skip the checking on the first iteration. - uint64_t LastStackContextId = - (ContextIterBegin != StackContext.end() && - *ContextIterBegin == 0) - ? 1 - : 0; - for (auto ContextIter = ContextIterBegin; - ContextIter != StackContext.end(); ++ContextIter) { - // If this is a direct recursion, simply skip the duplicate - // entries, to be consistent with how the summary ids were - // generated during ModuleSummaryAnalysis. - if (LastStackContextId == *ContextIter) - continue; - LastStackContextId = *ContextIter; - assert(StackIdIndexIter != MIBIter->StackIdIndices.end()); - assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) == - *ContextIter); - StackIdIndexIter++; - } - MIBIter++; - } +#ifndef NDEBUG + checkAllocContextIds(AllocNode, MemProfMD, CallsiteContext, + ImportSummary); +#endif // Perform cloning if not yet done. CloneFuncIfNeeded(/*NumClones=*/AllocNode.Versions.size());