Skip to content

Commit 9d32764

Browse files
[memprof] Simplify readMemprof (NFC)
This patch essentially replaces: std::pair<const std::vector<Frame> *, unsigned> with: ArrayRef<Frame> This way, we can store and pass ArrayRef<Frame>, conceptually one item, instead of the pointer and index. The only problem is that std::set<ArrayRef<Frame>> doesn't work because ArrayRef doesn't come with operator<. This patch works around the problem by providing CallStackRef, a thin wrapper around ArrayRef<Frame>.
1 parent 537e0e1 commit 9d32764

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,8 @@ static AllocationType addCallStack(CallStackTrie &AllocTrie,
763763
// non-zero.
764764
static bool
765765
stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack,
766-
ArrayRef<uint64_t> InlinedCallStack,
767-
unsigned StartIndex = 0) {
768-
auto StackFrame = ProfileCallStack.begin() + StartIndex;
766+
ArrayRef<uint64_t> InlinedCallStack) {
767+
auto StackFrame = ProfileCallStack.begin();
769768
auto InlCallStackIter = InlinedCallStack.begin();
770769
for (; StackFrame != ProfileCallStack.end() &&
771770
InlCallStackIter != InlinedCallStack.end();
@@ -969,10 +968,20 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
969968
// Build maps of the location hash to all profile data with that leaf location
970969
// (allocation info and the callsites).
971970
std::map<uint64_t, std::set<const AllocationInfo *>> LocHashToAllocInfo;
972-
// For the callsites we need to record the index of the associated frame in
973-
// the frame array (see comments below where the map entries are added).
974-
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *, unsigned>>>
975-
LocHashToCallSites;
971+
// A thin wrapper around ArrayRef<Frame> to facilitate std::set<CallStackRef>.
972+
struct CallStackRef : public ArrayRef<Frame> {
973+
CallStackRef(ArrayRef<Frame> CS, unsigned Pos)
974+
: ArrayRef<Frame>(CS.drop_front(Pos)) {}
975+
// std::set requires std::less.
976+
bool operator<(const CallStackRef &R) const {
977+
const CallStackRef &L = *this;
978+
return std::make_pair(L.data(), L.size()) <
979+
std::make_pair(R.data(), R.size());
980+
}
981+
};
982+
// For the callsites we need to record slices of the frame array (see comments
983+
// below where the map entries are added).
984+
std::map<uint64_t, std::set<CallStackRef>> LocHashToCallSites;
976985
for (auto &AI : MemProfRec->AllocSites) {
977986
NumOfMemProfAllocContextProfiles++;
978987
// Associate the allocation info with the leaf frame. The later matching
@@ -989,7 +998,7 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
989998
unsigned Idx = 0;
990999
for (auto &StackFrame : CS) {
9911000
uint64_t StackId = computeStackId(StackFrame);
992-
LocHashToCallSites[StackId].insert(std::make_pair(&CS, Idx++));
1001+
LocHashToCallSites[StackId].emplace(CS, Idx++);
9931002
ProfileHasColumns |= StackFrame.Column;
9941003
// Once we find this function, we can stop recording.
9951004
if (StackFrame.Function == FuncGUID)
@@ -1029,8 +1038,7 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
10291038
// and another callsite).
10301039
std::map<uint64_t, std::set<const AllocationInfo *>>::iterator
10311040
AllocInfoIter;
1032-
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *,
1033-
unsigned>>>::iterator CallSitesIter;
1041+
std::map<uint64_t, std::set<CallStackRef>>::iterator CallSitesIter;
10341042
for (const DILocation *DIL = I.getDebugLoc(); DIL != nullptr;
10351043
DIL = DIL->getInlinedAt()) {
10361044
// Use C++ linkage name if possible. Need to compile with
@@ -1121,8 +1129,8 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
11211129
for (auto CallStackIdx : CallSitesIter->second) {
11221130
// If we found and thus matched all frames on the call, create and
11231131
// attach call stack metadata.
1124-
if (stackFrameIncludesInlinedCallStack(
1125-
*CallStackIdx.first, InlinedCallStack, CallStackIdx.second)) {
1132+
if (stackFrameIncludesInlinedCallStack(CallStackIdx,
1133+
InlinedCallStack)) {
11261134
NumOfMemProfMatchedCallSites++;
11271135
addCallsiteMetadata(I, InlinedCallStack, Ctx);
11281136
// Only need to find one with a matching call stack and add a single

0 commit comments

Comments
 (0)