Skip to content

Commit

Permalink
[memprof] Use std::vector<Frame> instead of llvm::SmallVector<Frame> …
Browse files Browse the repository at this point in the history
…(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.
  • Loading branch information
kazutakahirata authored Jun 6, 2024
1 parent 12ccc24 commit 4a918f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
19 changes: 9 additions & 10 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ struct IndexedAllocationInfo {
// be used for temporary in-memory instances.
struct AllocationInfo {
// Same as IndexedAllocationInfo::CallStack with the frame contents inline.
llvm::SmallVector<Frame> CallStack;
std::vector<Frame> CallStack;
// Same as IndexedAllocationInfo::Info;
PortableMemInfoBlock Info;

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

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

MemProfRecord() = default;
MemProfRecord(
Expand All @@ -476,7 +475,7 @@ struct MemProfRecord {
AllocSites.emplace_back(IndexedAI, IdToFrameCallback);
}
for (const ArrayRef<FrameId> Site : Record.CallSites) {
llvm::SmallVector<Frame> Frames;
std::vector<Frame> Frames;
for (const FrameId Id : Site) {
Frames.push_back(IdToFrameCallback(Id));
}
Expand All @@ -494,7 +493,7 @@ struct MemProfRecord {

if (!CallSites.empty()) {
OS << " CallSites:\n";
for (const llvm::SmallVector<Frame> &Frames : CallSites) {
for (const std::vector<Frame> &Frames : CallSites) {
for (const Frame &F : Frames) {
OS << " -\n";
F.printYAML(OS);
Expand Down Expand Up @@ -848,8 +847,8 @@ template <typename MapTy> struct CallStackIdConverter {
CallStackIdConverter(const CallStackIdConverter &) = delete;
CallStackIdConverter &operator=(const CallStackIdConverter &) = delete;

llvm::SmallVector<Frame> operator()(CallStackId CSId) {
llvm::SmallVector<Frame> Frames;
std::vector<Frame> operator()(CallStackId CSId) {
std::vector<Frame> Frames;
auto CSIter = Map.find(CSId);
if (CSIter == Map.end()) {
LastUnmappedId = CSId;
Expand Down Expand Up @@ -890,8 +889,8 @@ struct LinearCallStackIdConverter {
std::function<Frame(LinearFrameId)> FrameIdToFrame)
: CallStackBase(CallStackBase), FrameIdToFrame(FrameIdToFrame) {}

llvm::SmallVector<Frame> operator()(LinearCallStackId LinearCSId) {
llvm::SmallVector<Frame> Frames;
std::vector<Frame> operator()(LinearCallStackId LinearCSId) {
std::vector<Frame> Frames;

const unsigned char *Ptr =
CallStackBase +
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/ProfileData/MemProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@ IndexedMemProfRecord::deserialize(const MemProfSchema &Schema,
}

MemProfRecord IndexedMemProfRecord::toMemProfRecord(
llvm::function_ref<llvm::SmallVector<Frame>(const CallStackId)> Callback)
const {
llvm::function_ref<std::vector<Frame>(const CallStackId)> Callback) const {
MemProfRecord Record;

Record.AllocSites.reserve(AllocSites.size());
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ static void readMemprof(Module &M, Function &F,
std::map<uint64_t, std::set<const AllocationInfo *>> LocHashToAllocInfo;
// For the callsites we need to record the index of the associated frame in
// the frame array (see comments below where the map entries are added).
std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *, unsigned>>>
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *, unsigned>>>
LocHashToCallSites;
for (auto &AI : MemProfRec->AllocSites) {
// Associate the allocation info with the leaf frame. The later matching
Expand Down Expand Up @@ -815,7 +815,7 @@ static void readMemprof(Module &M, Function &F,
// and another callsite).
std::map<uint64_t, std::set<const AllocationInfo *>>::iterator
AllocInfoIter;
std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *,
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *,
unsigned>>>::iterator CallSitesIter;
for (const DILocation *DIL = I.getDebugLoc(); DIL != nullptr;
DIL = DIL->getInlinedAt()) {
Expand Down

0 comments on commit 4a918f0

Please sign in to comment.