LLVM 22.0.0git
IndexedMemProfData.h
Go to the documentation of this file.
1//===- IndexedMemProfData.h - MemProf format support ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements IndexedMemProfData, a data structure to hold MemProf
10// in a space optimized format. It also provides utility methods for writing
11// MemProf data.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
16#define LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
17
21#include "llvm/Support/BLAKE3.h"
24
25namespace llvm {
26namespace memprof {
27class MemProfSummary;
29 // A map to hold memprof data per function. The lower 64 bits obtained from
30 // the md5 hash of the function name is used to index into the map.
32
33 // A map to hold frame id to frame mappings. The mappings are used to
34 // convert IndexedMemProfRecord to MemProfRecords with frame information
35 // inline.
37
38 // A map to hold call stack id to call stacks.
40
42 const FrameId Id = hashFrame(F);
43 Frames.try_emplace(Id, F);
44 return Id;
45 }
46
48 CallStackId CSId = hashCallStack(CS);
49 CallStacks.try_emplace(CSId, CS);
50 return CSId;
51 }
52
54 CallStackId CSId = hashCallStack(CS);
55 CallStacks.try_emplace(CSId, std::move(CS));
56 return CSId;
57 }
58
59private:
60 // Return a hash value based on the contents of the frame. Here we use a
61 // cryptographic hash function to minimize the chance of hash collisions. We
62 // do persist FrameIds as part of memprof formats up to Version 2, inclusive.
63 // However, the deserializer never calls this function; it uses FrameIds
64 // merely as keys to look up Frames proper.
65 FrameId hashFrame(const Frame &F) const {
68 HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
70 FrameId Id;
71 std::memcpy(&Id, Hash.data(), sizeof(Hash));
72 return Id;
73 }
74
75 // Compute a CallStackId for a given call stack.
76 CallStackId hashCallStack(ArrayRef<FrameId> CS) const {
79 for (FrameId F : CS)
82 CallStackId CSId;
83 std::memcpy(&CSId, Hash.data(), sizeof(Hash));
84 return CSId;
85 }
86};
87} // namespace memprof
88
89// Write the MemProf data to OS.
91 ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
92 memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema,
93 std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData,
94 std::unique_ptr<memprof::MemProfSummary> MemProfSum);
95} // namespace llvm
96#endif
#define LLVM_ABI
Definition: Compiler.h:213
#define F(x, y, z)
Definition: MD5.cpp:55
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
HashResultTy< HasherT_ > final()
Forward to HasherT::final() if available.
Definition: HashBuilder.h:66
Interface to help hash various types through a hasher type.
Definition: HashBuilder.h:139
std::enable_if_t< hashbuilder_detail::IsHashableData< T >::value, HashBuilder & > add(T Value)
Implement hashing for hashable data types, e.g. integral or enum values.
Definition: HashBuilder.h:149
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
uint64_t FrameId
Definition: MemProf.h:236
uint64_t CallStackId
Definition: MemProf.h:352
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::array< uint8_t, NumBytes > BLAKE3Result
The constant LLVM_BLAKE3_OUT_LEN provides the default output length, 32 bytes, which is recommended f...
Definition: BLAKE3.h:35
LLVM_ABI Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData, memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema, std::unique_ptr< memprof::DataAccessProfData > DataAccessProfileData, std::unique_ptr< memprof::MemProfSummary > MemProfSum)
llvm::MapVector< CallStackId, llvm::SmallVector< FrameId > > CallStacks
CallStackId addCallStack(SmallVector< FrameId > &&CS)
llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > Records
CallStackId addCallStack(ArrayRef< FrameId > CS)
FrameId addFrame(const Frame &F)
llvm::MapVector< FrameId, Frame > Frames