Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

This patch adds a helper function to replace an idiom like:

FrameId Id = F.hash();
MemProfData.Frames.try_emplace(Id, F);
// Do something with Id.

This patch adds a helper function to replace an idiom like:

  FrameId Id = F.hash();
  MemProfData.Frames.try_emplace(Id, F);
  // Do something with Id.
@llvmbot llvmbot added the PGO Profile Guided Optimizations label Dec 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 5, 2024

@llvm/pr-subscribers-pgo

Author: Kazu Hirata (kazutakahirata)

Changes

This patch adds a helper function to replace an idiom like:

FrameId Id = F.hash();
MemProfData.Frames.try_emplace(Id, F);
// Do something with Id.


Full diff: https://github.com/llvm/llvm-project/pull/118724.diff

3 Files Affected:

  • (modified) llvm/include/llvm/ProfileData/MemProf.h (+6)
  • (modified) llvm/lib/ProfileData/MemProfReader.cpp (+3-8)
  • (modified) llvm/unittests/ProfileData/MemProfTest.cpp (+4-4)
diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h
index fe017913f6de24..9aaa2af335a239 100644
--- a/llvm/include/llvm/ProfileData/MemProf.h
+++ b/llvm/include/llvm/ProfileData/MemProf.h
@@ -1023,6 +1023,12 @@ struct IndexedMemProfData {
 
   // A map to hold call stack id to call stacks.
   llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;
+
+  FrameId addFrame(const Frame &F) {
+    const FrameId Id = F.hash();
+    Frames.try_emplace(Id, F);
+    return Id;
+  }
 };
 
 struct FrameStat {
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index fdb8e596b0dd3e..9dacf298985937 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -584,9 +584,7 @@ Error RawMemProfReader::symbolizeAndFilterStackFrames(
           GuidToSymbolName.insert({Guid, CanonicalName.str()});
         }
 
-        const FrameId Hash = F.hash();
-        MemProfData.Frames.insert({Hash, F});
-        SymbolizedFrame[VAddr].push_back(Hash);
+        SymbolizedFrame[VAddr].push_back(MemProfData.addFrame(F));
       }
     }
 
@@ -769,11 +767,8 @@ void YAMLMemProfReader::parse(StringRef YAMLData) {
   auto AddCallStack = [&](ArrayRef<Frame> CallStack) -> CallStackId {
     SmallVector<FrameId> IndexedCallStack;
     IndexedCallStack.reserve(CallStack.size());
-    for (const Frame &F : CallStack) {
-      FrameId Id = F.hash();
-      MemProfData.Frames.try_emplace(Id, F);
-      IndexedCallStack.push_back(Id);
-    }
+    for (const Frame &F : CallStack)
+      IndexedCallStack.push_back(MemProfData.addFrame(F));
     CallStackId CSId = hashCallStack(IndexedCallStack);
     MemProfData.CallStacks.try_emplace(CSId, std::move(IndexedCallStack));
     return CSId;
diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp
index 2f8589bbfbb962..3ac64dcac0abed 100644
--- a/llvm/unittests/ProfileData/MemProfTest.cpp
+++ b/llvm/unittests/ProfileData/MemProfTest.cpp
@@ -430,8 +430,8 @@ TEST(MemProf, BaseMemProfReader) {
            /*Column=*/5, /*IsInlineFrame=*/true);
   Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
            /*Column=*/2, /*IsInlineFrame=*/false);
-  MemProfData.Frames.insert({F1.hash(), F1});
-  MemProfData.Frames.insert({F2.hash(), F2});
+  MemProfData.addFrame(F1);
+  MemProfData.addFrame(F2);
 
   llvm::SmallVector<FrameId> CallStack{F1.hash(), F2.hash()};
   CallStackId CSId = hashCallStack(CallStack);
@@ -466,8 +466,8 @@ TEST(MemProf, BaseMemProfReaderWithCSIdMap) {
            /*Column=*/5, /*IsInlineFrame=*/true);
   Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
            /*Column=*/2, /*IsInlineFrame=*/false);
-  MemProfData.Frames.insert({F1.hash(), F1});
-  MemProfData.Frames.insert({F2.hash(), F2});
+  MemProfData.addFrame(F1);
+  MemProfData.addFrame(F2);
 
   llvm::SmallVector<FrameId> CallStack = {F1.hash(), F2.hash()};
   CallStackId CSId = hashCallStack(CallStack);

@kazutakahirata kazutakahirata merged commit 50f8580 into llvm:main Dec 5, 2024
10 checks passed
@kazutakahirata kazutakahirata deleted the memprof_addFrame branch December 5, 2024 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants