LLVM 20.0.0git
PGOCtxProfReader.h
Go to the documentation of this file.
1//===--- PGOCtxProfReader.h - Contextual profile reader ---------*- 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/// \file
10///
11/// Reader for contextual iFDO profile, which comes in bitstream format.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_PROFILEDATA_CTXINSTRPROFILEREADER_H
16#define LLVM_PROFILEDATA_CTXINSTRPROFILEREADER_H
17
18#include "llvm/ADT/DenseSet.h"
20#include "llvm/IR/GlobalValue.h"
22#include "llvm/Support/Error.h"
23#include <map>
24#include <vector>
25
26namespace llvm {
27/// A node (context) in the loaded contextual profile, suitable for mutation
28/// during IPO passes. We generally expect a fraction of counters and
29/// callsites to be populated. We continue to model counters as vectors, but
30/// callsites are modeled as a map of a map. The expectation is that, typically,
31/// there is a small number of indirect targets (usually, 1 for direct calls);
32/// but potentially a large number of callsites, and, as inlining progresses,
33/// the callsite count of a caller will grow.
34class PGOCtxProfContext final {
35public:
36 using CallTargetMapTy = std::map<GlobalValue::GUID, PGOCtxProfContext>;
38
39private:
40 friend class PGOCtxProfileReader;
41 GlobalValue::GUID GUID = 0;
43 CallsiteMapTy Callsites;
44
46 : GUID(G), Counters(std::move(Counters)) {}
47
49 getOrEmplace(uint32_t Index, GlobalValue::GUID G,
50 SmallVectorImpl<uint64_t> &&Counters);
51
52public:
57
58 GlobalValue::GUID guid() const { return GUID; }
59 const SmallVectorImpl<uint64_t> &counters() const { return Counters; }
60 const CallsiteMapTy &callsites() const { return Callsites; }
61 CallsiteMapTy &callsites() { return Callsites; }
62
63 bool hasCallsite(uint32_t I) const {
64 return Callsites.find(I) != Callsites.end();
65 }
66
68 assert(hasCallsite(I) && "Callsite not found");
69 return Callsites.find(I)->second;
70 }
72};
73
75 StringRef Magic;
76 BitstreamCursor Cursor;
78 Error readMetadata();
79 Error wrongValue(const Twine &);
80 Error unsupported(const Twine &);
81
83 readContext(bool ExpectIndex);
84 bool canReadContext();
85
86public:
88 : Magic(Buffer.substr(0, PGOCtxProfileWriter::ContainerMagic.size())),
89 Cursor(Buffer.substr(PGOCtxProfileWriter::ContainerMagic.size())) {}
90
92};
93} // namespace llvm
94#endif
This file defines the DenseSet and SmallDenseSet classes.
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringRef substr(StringRef Str, uint64_t Len)
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
A node (context) in the loaded contextual profile, suitable for mutation during IPO passes.
PGOCtxProfContext & operator=(PGOCtxProfContext &&)=default
bool hasCallsite(uint32_t I) const
void getContainedGuids(DenseSet< GlobalValue::GUID > &Guids) const
GlobalValue::GUID guid() const
PGOCtxProfContext & operator=(const PGOCtxProfContext &)=delete
const CallTargetMapTy & callsite(uint32_t I) const
const SmallVectorImpl< uint64_t > & counters() const
CallsiteMapTy & callsites()
PGOCtxProfContext(const PGOCtxProfContext &)=delete
PGOCtxProfContext(PGOCtxProfContext &&)=default
std::map< GlobalValue::GUID, PGOCtxProfContext > CallTargetMapTy
const CallsiteMapTy & callsites() const
Expected< std::map< GlobalValue::GUID, PGOCtxProfContext > > loadContexts()
PGOCtxProfileReader(StringRef Buffer)
Write one or more ContextNodes to the provided raw_fd_stream.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858