Skip to content

Commit 7348d7e

Browse files
[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.
1 parent efd805e commit 7348d7e

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

llvm/include/llvm/Analysis/MemoryProfileInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ template <class NodeT, class IteratorT> class CallStack {
166166

167167
CallStackIterator begin() const;
168168
CallStackIterator end() const { return CallStackIterator(N, /*End*/ true); }
169-
CallStackIterator beginAfterSharedPrefix(CallStack &Other);
169+
CallStackIterator beginAfterSharedPrefix(const CallStack &Other);
170170
uint64_t back() const;
171171

172172
private:
@@ -204,7 +204,7 @@ CallStack<NodeT, IteratorT>::begin() const {
204204

205205
template <class NodeT, class IteratorT>
206206
typename CallStack<NodeT, IteratorT>::CallStackIterator
207-
CallStack<NodeT, IteratorT>::beginAfterSharedPrefix(CallStack &Other) {
207+
CallStack<NodeT, IteratorT>::beginAfterSharedPrefix(const CallStack &Other) {
208208
CallStackIterator Cur = begin();
209209
for (CallStackIterator OtherCur = Other.begin();
210210
Cur != end() && OtherCur != Other.end(); ++Cur, ++OtherCur)

llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5050,6 +5050,45 @@ bool MemProfContextDisambiguation::initializeIndirectCallPromotionInfo(
50505050
return true;
50515051
}
50525052

5053+
#ifndef NDEBUG
5054+
// Sanity check that the MIB stack ids match between the summary and
5055+
// instruction metadata.
5056+
static void checkAllocContextIds(
5057+
const AllocInfo &AllocNode, const MDNode *MemProfMD,
5058+
const CallStack<MDNode, MDNode::op_iterator> &CallsiteContext,
5059+
const ModuleSummaryIndex *ImportSummary) {
5060+
auto MIBIter = AllocNode.MIBs.begin();
5061+
for (auto &MDOp : MemProfMD->operands()) {
5062+
assert(MIBIter != AllocNode.MIBs.end());
5063+
auto StackIdIndexIter = MIBIter->StackIdIndices.begin();
5064+
auto *MIBMD = cast<const MDNode>(MDOp);
5065+
MDNode *StackMDNode = getMIBStackNode(MIBMD);
5066+
assert(StackMDNode);
5067+
CallStack<MDNode, MDNode::op_iterator> StackContext(StackMDNode);
5068+
auto ContextIterBegin =
5069+
StackContext.beginAfterSharedPrefix(CallsiteContext);
5070+
// Skip the checking on the first iteration.
5071+
uint64_t LastStackContextId =
5072+
(ContextIterBegin != StackContext.end() && *ContextIterBegin == 0) ? 1
5073+
: 0;
5074+
for (auto ContextIter = ContextIterBegin; ContextIter != StackContext.end();
5075+
++ContextIter) {
5076+
// If this is a direct recursion, simply skip the duplicate
5077+
// entries, to be consistent with how the summary ids were
5078+
// generated during ModuleSummaryAnalysis.
5079+
if (LastStackContextId == *ContextIter)
5080+
continue;
5081+
LastStackContextId = *ContextIter;
5082+
assert(StackIdIndexIter != MIBIter->StackIdIndices.end());
5083+
assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) ==
5084+
*ContextIter);
5085+
StackIdIndexIter++;
5086+
}
5087+
MIBIter++;
5088+
}
5089+
}
5090+
#endif
5091+
50535092
bool MemProfContextDisambiguation::applyImport(Module &M) {
50545093
assert(ImportSummary);
50555094
bool Changed = false;
@@ -5242,40 +5281,10 @@ bool MemProfContextDisambiguation::applyImport(Module &M) {
52425281
assert(AI != FS->allocs().end());
52435282
auto &AllocNode = *(AI++);
52445283

5245-
// Sanity check that the MIB stack ids match between the summary and
5246-
// instruction metadata.
5247-
auto MIBIter = AllocNode.MIBs.begin();
5248-
for (auto &MDOp : MemProfMD->operands()) {
5249-
assert(MIBIter != AllocNode.MIBs.end());
5250-
LLVM_ATTRIBUTE_UNUSED auto StackIdIndexIter =
5251-
MIBIter->StackIdIndices.begin();
5252-
auto *MIBMD = cast<const MDNode>(MDOp);
5253-
MDNode *StackMDNode = getMIBStackNode(MIBMD);
5254-
assert(StackMDNode);
5255-
CallStack<MDNode, MDNode::op_iterator> StackContext(StackMDNode);
5256-
auto ContextIterBegin =
5257-
StackContext.beginAfterSharedPrefix(CallsiteContext);
5258-
// Skip the checking on the first iteration.
5259-
uint64_t LastStackContextId =
5260-
(ContextIterBegin != StackContext.end() &&
5261-
*ContextIterBegin == 0)
5262-
? 1
5263-
: 0;
5264-
for (auto ContextIter = ContextIterBegin;
5265-
ContextIter != StackContext.end(); ++ContextIter) {
5266-
// If this is a direct recursion, simply skip the duplicate
5267-
// entries, to be consistent with how the summary ids were
5268-
// generated during ModuleSummaryAnalysis.
5269-
if (LastStackContextId == *ContextIter)
5270-
continue;
5271-
LastStackContextId = *ContextIter;
5272-
assert(StackIdIndexIter != MIBIter->StackIdIndices.end());
5273-
assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) ==
5274-
*ContextIter);
5275-
StackIdIndexIter++;
5276-
}
5277-
MIBIter++;
5278-
}
5284+
#ifndef NDEBUG
5285+
checkAllocContextIds(AllocNode, MemProfMD, CallsiteContext,
5286+
ImportSummary);
5287+
#endif
52795288

52805289
// Perform cloning if not yet done.
52815290
CloneFuncIfNeeded(/*NumClones=*/AllocNode.Versions.size());

0 commit comments

Comments
 (0)