@@ -42,43 +42,29 @@ class MemProfReader {
4242 using Iterator = InstrProfIterator<GuidMemProfRecordPair, MemProfReader>;
4343 Iterator end () { return Iterator (); }
4444 Iterator begin () {
45- Iter = FunctionProfileData .begin ();
45+ Iter = MemProfData. Records .begin ();
4646 return Iterator (this );
4747 }
4848
49- // Take the complete profile data.
50- IndexedMemProfData takeMemProfData () {
51- // TODO: Once we replace the three member variables, namely IdToFrame,
52- // CSIdToCallStack, and FunctionProfileData, with MemProfData, replace the
53- // following code with just "return std::move(MemProfData);".
54- IndexedMemProfData MemProfData;
55- // Copy key-value pairs because IdToFrame uses DenseMap, whereas
56- // IndexedMemProfData::Frames uses MapVector.
57- for (const auto &[FrameId, F] : IdToFrame)
58- MemProfData.Frames .try_emplace (FrameId, F);
59- // Copy key-value pairs because CSIdToCallStack uses DenseMap, whereas
60- // IndexedMemProfData::CallStacks uses MapVector.
61- for (const auto &[CSId, CS] : CSIdToCallStack)
62- MemProfData.CallStacks .try_emplace (CSId, CS);
63- MemProfData.Records = FunctionProfileData;
64- return MemProfData;
65- }
49+ // Take the complete profile data. Once this function is invoked,
50+ // MemProfReader no longer owns the MemProf profile.
51+ IndexedMemProfData takeMemProfData () { return std::move (MemProfData); }
6652
6753 virtual Error
6854 readNextRecord (GuidMemProfRecordPair &GuidRecord,
6955 std::function<const Frame (const FrameId)> Callback = nullptr) {
70- if (FunctionProfileData .empty ())
56+ if (MemProfData. Records .empty ())
7157 return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
7258
73- if (Iter == FunctionProfileData .end ())
59+ if (Iter == MemProfData. Records .end ())
7460 return make_error<InstrProfError>(instrprof_error::eof);
7561
7662 if (Callback == nullptr )
7763 Callback =
7864 std::bind (&MemProfReader::idToFrame, this , std::placeholders::_1);
7965
80- CallStackIdConverter<decltype (CSIdToCallStack )> CSIdConv (CSIdToCallStack,
81- Callback);
66+ CallStackIdConverter<decltype (MemProfData. CallStacks )> CSIdConv (
67+ MemProfData. CallStacks , Callback);
8268
8369 const IndexedMemProfRecord &IndexedRecord = Iter->second ;
8470 GuidRecord = {
@@ -97,28 +83,18 @@ class MemProfReader {
9783 virtual ~MemProfReader () = default ;
9884
9985 // Initialize the MemProfReader with the given MemProf profile.
100- MemProfReader (IndexedMemProfData MemProfData) {
101- for (const auto &[FrameId, F] : MemProfData.Frames )
102- IdToFrame.try_emplace (FrameId, F);
103- for (const auto &[CSId, CS] : MemProfData.CallStacks )
104- CSIdToCallStack.try_emplace (CSId, CS);
105- FunctionProfileData = std::move (MemProfData.Records );
106- }
86+ MemProfReader (IndexedMemProfData &&MemProfData)
87+ : MemProfData(std::move(MemProfData)) {}
10788
10889protected:
10990 // A helper method to extract the frame from the IdToFrame map.
11091 const Frame &idToFrame (const FrameId Id) const {
111- auto It = IdToFrame .find (Id);
112- assert (It != IdToFrame .end () && " Id not found in map." );
113- return It->getSecond () ;
92+ auto It = MemProfData. Frames .find (Id);
93+ assert (It != MemProfData. Frames .end () && " Id not found in map." );
94+ return It->second ;
11495 }
115- // A mapping from FrameId (a hash of the contents) to the frame.
116- llvm::DenseMap<FrameId, Frame> IdToFrame;
117- // A mapping from CallStackId to the call stack.
118- llvm::DenseMap<CallStackId, llvm::SmallVector<FrameId>> CSIdToCallStack;
119- // A mapping from function GUID, hash of the canonical function symbol to the
120- // memprof profile data for that function, i.e allocation and callsite info.
121- llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> FunctionProfileData;
96+ // A complete pacakge of the MemProf profile.
97+ IndexedMemProfData MemProfData;
12298 // An iterator to the internal function profile data structure.
12399 llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord>::iterator Iter;
124100};
0 commit comments