Skip to content

Commit 4a918f0

Browse files
[memprof] Use std::vector<Frame> instead of llvm::SmallVector<Frame> (NFC) (#94432)
This patch replaces llvm::SmallVector<Frame> with std::vector<Frame>. llvm::SmallVector<Frame> sets aside one inline element. Meanwhile, when I sort all call stacks by their lengths, the length at the first percentile is already 2. That is, 99 percent of call stacks do not take advantage of the inline element. Using std::vector<Frame> reduces the cycle and instruction counts by 11% and 22%, respectively, with "llvm-profdata show" modified to deserialize all MemProfRecords.
1 parent 12ccc24 commit 4a918f0

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ struct IndexedAllocationInfo {
368368
// be used for temporary in-memory instances.
369369
struct AllocationInfo {
370370
// Same as IndexedAllocationInfo::CallStack with the frame contents inline.
371-
llvm::SmallVector<Frame> CallStack;
371+
std::vector<Frame> CallStack;
372372
// Same as IndexedAllocationInfo::Info;
373373
PortableMemInfoBlock Info;
374374

@@ -450,8 +450,7 @@ struct IndexedMemProfRecord {
450450
// Convert IndexedMemProfRecord to MemProfRecord. Callback is used to
451451
// translate CallStackId to call stacks with frames inline.
452452
MemProfRecord toMemProfRecord(
453-
llvm::function_ref<llvm::SmallVector<Frame>(const CallStackId)> Callback)
454-
const;
453+
llvm::function_ref<std::vector<Frame>(const CallStackId)> Callback) const;
455454

456455
// Returns the GUID for the function name after canonicalization. For
457456
// memprof, we remove any .llvm suffix added by LTO. MemProfRecords are
@@ -466,7 +465,7 @@ struct MemProfRecord {
466465
// Same as IndexedMemProfRecord::AllocSites with frame contents inline.
467466
llvm::SmallVector<AllocationInfo> AllocSites;
468467
// Same as IndexedMemProfRecord::CallSites with frame contents inline.
469-
llvm::SmallVector<llvm::SmallVector<Frame>> CallSites;
468+
llvm::SmallVector<std::vector<Frame>> CallSites;
470469

471470
MemProfRecord() = default;
472471
MemProfRecord(
@@ -476,7 +475,7 @@ struct MemProfRecord {
476475
AllocSites.emplace_back(IndexedAI, IdToFrameCallback);
477476
}
478477
for (const ArrayRef<FrameId> Site : Record.CallSites) {
479-
llvm::SmallVector<Frame> Frames;
478+
std::vector<Frame> Frames;
480479
for (const FrameId Id : Site) {
481480
Frames.push_back(IdToFrameCallback(Id));
482481
}
@@ -494,7 +493,7 @@ struct MemProfRecord {
494493

495494
if (!CallSites.empty()) {
496495
OS << " CallSites:\n";
497-
for (const llvm::SmallVector<Frame> &Frames : CallSites) {
496+
for (const std::vector<Frame> &Frames : CallSites) {
498497
for (const Frame &F : Frames) {
499498
OS << " -\n";
500499
F.printYAML(OS);
@@ -848,8 +847,8 @@ template <typename MapTy> struct CallStackIdConverter {
848847
CallStackIdConverter(const CallStackIdConverter &) = delete;
849848
CallStackIdConverter &operator=(const CallStackIdConverter &) = delete;
850849

851-
llvm::SmallVector<Frame> operator()(CallStackId CSId) {
852-
llvm::SmallVector<Frame> Frames;
850+
std::vector<Frame> operator()(CallStackId CSId) {
851+
std::vector<Frame> Frames;
853852
auto CSIter = Map.find(CSId);
854853
if (CSIter == Map.end()) {
855854
LastUnmappedId = CSId;
@@ -890,8 +889,8 @@ struct LinearCallStackIdConverter {
890889
std::function<Frame(LinearFrameId)> FrameIdToFrame)
891890
: CallStackBase(CallStackBase), FrameIdToFrame(FrameIdToFrame) {}
892891

893-
llvm::SmallVector<Frame> operator()(LinearCallStackId LinearCSId) {
894-
llvm::SmallVector<Frame> Frames;
892+
std::vector<Frame> operator()(LinearCallStackId LinearCSId) {
893+
std::vector<Frame> Frames;
895894

896895
const unsigned char *Ptr =
897896
CallStackBase +

llvm/lib/ProfileData/MemProf.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
338338
}
339339

340340
MemProfRecord IndexedMemProfRecord::toMemProfRecord(
341-
llvm::function_ref<llvm::SmallVector<Frame>(const CallStackId)> Callback)
342-
const {
341+
llvm::function_ref<std::vector<Frame>(const CallStackId)> Callback) const {
343342
MemProfRecord Record;
344343

345344
Record.AllocSites.reserve(AllocSites.size());

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ static void readMemprof(Module &M, Function &F,
759759
std::map<uint64_t, std::set<const AllocationInfo *>> LocHashToAllocInfo;
760760
// For the callsites we need to record the index of the associated frame in
761761
// the frame array (see comments below where the map entries are added).
762-
std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *, unsigned>>>
762+
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *, unsigned>>>
763763
LocHashToCallSites;
764764
for (auto &AI : MemProfRec->AllocSites) {
765765
// Associate the allocation info with the leaf frame. The later matching
@@ -815,7 +815,7 @@ static void readMemprof(Module &M, Function &F,
815815
// and another callsite).
816816
std::map<uint64_t, std::set<const AllocationInfo *>>::iterator
817817
AllocInfoIter;
818-
std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *,
818+
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *,
819819
unsigned>>>::iterator CallSitesIter;
820820
for (const DILocation *DIL = I.getDebugLoc(); DIL != nullptr;
821821
DIL = DIL->getInlinedAt()) {

0 commit comments

Comments
 (0)