[MemProf] Avoid assertion checking loop under NDEBUG (NFC) (#138985)
Guard a loop that only exists to do assertion checking of stack ids on
memprof metadata so that it isn't compiled and executed under NDEBUG.
This is similar to how callsite metadata stack id verification is
guarded further below.
diff --git a/llvm/include/llvm/Analysis/MemoryProfileInfo.h b/llvm/include/llvm/Analysis/MemoryProfileInfo.h
index deb7ab1..86c58d1 100644
--- a/llvm/include/llvm/Analysis/MemoryProfileInfo.h
+++ b/llvm/include/llvm/Analysis/MemoryProfileInfo.h
@@ -166,7 +166,7 @@
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:
@@ -204,7 +204,7 @@
template <class NodeT, class IteratorT>
typename CallStack<NodeT, IteratorT>::CallStackIterator
-CallStack<NodeT, IteratorT>::beginAfterSharedPrefix(CallStack &Other) {
+CallStack<NodeT, IteratorT>::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 ec158af..4b2683d 100644
--- a/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp
+++ b/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp
@@ -5050,6 +5050,45 @@
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<MDNode, MDNode::op_iterator> &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<const MDNode>(MDOp);
+ MDNode *StackMDNode = getMIBStackNode(MIBMD);
+ assert(StackMDNode);
+ CallStack<MDNode, MDNode::op_iterator> 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 @@
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<const MDNode>(MDOp);
- MDNode *StackMDNode = getMIBStackNode(MIBMD);
- assert(StackMDNode);
- CallStack<MDNode, MDNode::op_iterator> 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());