LLVM 22.0.0git
CodeGenDataReader.h
Go to the documentation of this file.
1//===- CodeGenDataReader.h --------------------------------------*- 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 contains support for reading codegen data.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CGDATA_CODEGENDATAREADER_H
14#define LLVM_CGDATA_CODEGENDATAREADER_H
15
23
24namespace llvm {
25
28 std::string LastErrorMsg;
29
30public:
31 CodeGenDataReader() = default;
32 virtual ~CodeGenDataReader() = default;
33
34 /// Read the header. Required before reading first record.
35 virtual Error read() = 0;
36 /// Return the codegen data version.
37 virtual uint32_t getVersion() const = 0;
38 /// Return the codegen data kind.
39 virtual CGDataKind getDataKind() const = 0;
40 /// Return true if the data has an outlined hash tree.
41 virtual bool hasOutlinedHashTree() const = 0;
42 /// Return true if the data has a stable function map.
43 virtual bool hasStableFunctionMap() const = 0;
44 /// Return the outlined hash tree that is released from the reader.
45 std::unique_ptr<OutlinedHashTree> releaseOutlinedHashTree() {
46 return std::move(HashTreeRecord.HashTree);
47 }
48 std::unique_ptr<StableFunctionMap> releaseStableFunctionMap() {
49 return std::move(FunctionMapRecord.FunctionMap);
50 }
51
52 /// Factory method to create an appropriately typed reader for the given
53 /// codegen data file path and file system.
55 create(const Twine &Path, vfs::FileSystem &FS);
56
57 /// Factory method to create an appropriately typed reader for the given
58 /// memory buffer.
60 create(std::unique_ptr<MemoryBuffer> Buffer);
61
62 /// Extract the cgdata embedded in sections from the given object file and
63 /// merge them into the GlobalOutlineRecord. This is a static helper that
64 /// is used by `llvm-cgdata --merge` or ThinLTO's two-codegen rounds.
65 /// Optionally, \p CombinedHash can be used to compuate the combined hash of
66 /// the merged data.
67 LLVM_ABI static Error
69 OutlinedHashTreeRecord &GlobalOutlineRecord,
70 StableFunctionMapRecord &GlobalFunctionMapRecord,
71 stable_hash *CombinedHash = nullptr);
72
73protected:
74 /// The outlined hash tree that has been read. When it's released by
75 /// releaseOutlinedHashTree(), it's no longer valid.
77
78 /// The stable function map that has been read. When it's released by
79 // releaseStableFunctionMap(), it's no longer valid.
81
82 /// Set the current error and return same.
83 Error error(cgdata_error Err, const std::string &ErrMsg = "") {
84 LastError = Err;
85 LastErrorMsg = ErrMsg;
86 if (Err == cgdata_error::success)
87 return Error::success();
88 return make_error<CGDataError>(Err, ErrMsg);
89 }
90
92 handleAllErrors(std::move(E), [&](const CGDataError &IPE) {
93 LastError = IPE.get();
94 LastErrorMsg = IPE.getMessage();
95 });
96 return make_error<CGDataError>(LastError, LastErrorMsg);
97 }
98
99 /// Clear the current error and return a successful one.
101};
102
104
106 /// The codegen data file contents.
107 std::unique_ptr<MemoryBuffer> DataBuffer;
108 /// The header
110
111public:
112 IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer)
113 : DataBuffer(std::move(DataBuffer)) {}
117
118 /// Return true if the given buffer is in binary codegen data format.
119 static bool hasFormat(const MemoryBuffer &Buffer);
120 /// Read the contents including the header.
121 Error read() override;
122 /// Return the codegen data version.
123 uint32_t getVersion() const override { return Header.Version; }
124 /// Return the codegen data kind.
125 CGDataKind getDataKind() const override {
126 return static_cast<CGDataKind>(Header.DataKind);
127 }
128 /// Return true if the header indicates the data has an outlined hash tree.
129 /// This does not mean that the data is still available.
130 bool hasOutlinedHashTree() const override {
131 return Header.DataKind &
133 }
134 /// Return true if the header indicates the data has a stable function map.
135 bool hasStableFunctionMap() const override {
136 return Header.DataKind &
138 }
139};
140
141/// This format is a simple text format that's suitable for test data.
142/// The header is a custom format starting with `:` per line to indicate which
143/// codegen data is recorded. `#` is used to indicate a comment.
144/// The subsequent data is a YAML format per each codegen data in order.
145/// Currently, it only has a function outlined hash tree.
147 /// The codegen data file contents.
148 std::unique_ptr<MemoryBuffer> DataBuffer;
149 /// Iterator over the profile data.
150 line_iterator Line;
151 /// Describe the kind of the codegen data.
153
154public:
155 TextCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
156 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
159
160 /// Return true if the given buffer is in text codegen data format.
161 static bool hasFormat(const MemoryBuffer &Buffer);
162 /// Read the contents including the header.
163 Error read() override;
164 /// Text format does not have version, so return 0.
165 uint32_t getVersion() const override { return 0; }
166 /// Return the codegen data kind.
167 CGDataKind getDataKind() const override { return DataKind; }
168 /// Return true if the header indicates the data has an outlined hash tree.
169 /// This does not mean that the data is still available.
170 bool hasOutlinedHashTree() const override {
171 return static_cast<uint32_t>(DataKind) &
173 }
174 /// Return true if the header indicates the data has a stable function map.
175 /// This does not mean that the data is still available.
176 bool hasStableFunctionMap() const override {
177 return static_cast<uint32_t>(DataKind) &
179 }
180};
181
182} // end namespace llvm
183
184#endif // LLVM_CGDATA_CODEGENDATAREADER_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define error(X)
Defines the virtual file system interface vfs::FileSystem.
const std::string & getMessage() const
Definition CodeGenData.h:83
cgdata_error get() const
Definition CodeGenData.h:82
virtual bool hasOutlinedHashTree() const =0
Return true if the data has an outlined hash tree.
Error success()
Clear the current error and return a successful one.
virtual ~CodeGenDataReader()=default
virtual bool hasStableFunctionMap() const =0
Return true if the data has a stable function map.
virtual uint32_t getVersion() const =0
Return the codegen data version.
OutlinedHashTreeRecord HashTreeRecord
The outlined hash tree that has been read.
static LLVM_ABI Expected< std::unique_ptr< CodeGenDataReader > > create(const Twine &Path, vfs::FileSystem &FS)
Factory method to create an appropriately typed reader for the given codegen data file path and file ...
Error error(cgdata_error Err, const std::string &ErrMsg="")
Set the current error and return same.
std::unique_ptr< StableFunctionMap > releaseStableFunctionMap()
StableFunctionMapRecord FunctionMapRecord
The stable function map that has been read. When it's released by.
virtual CGDataKind getDataKind() const =0
Return the codegen data kind.
virtual Error read()=0
Read the header. Required before reading first record.
static LLVM_ABI Error mergeFromObjectFile(const object::ObjectFile *Obj, OutlinedHashTreeRecord &GlobalOutlineRecord, StableFunctionMapRecord &GlobalFunctionMapRecord, stable_hash *CombinedHash=nullptr)
Extract the cgdata embedded in sections from the given object file and merge them into the GlobalOutl...
std::unique_ptr< OutlinedHashTree > releaseOutlinedHashTree()
Return the outlined hash tree that is released from the reader.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in binary codegen data format.
Error read() override
Read the contents including the header.
bool hasStableFunctionMap() const override
Return true if the header indicates the data has a stable function map.
IndexedCodeGenDataReader(const IndexedCodeGenDataReader &)=delete
bool hasOutlinedHashTree() const override
Return true if the header indicates the data has an outlined hash tree.
IndexedCodeGenDataReader(std::unique_ptr< MemoryBuffer > DataBuffer)
CGDataKind getDataKind() const override
Return the codegen data kind.
IndexedCodeGenDataReader & operator=(const IndexedCodeGenDataReader &)=delete
uint32_t getVersion() const override
Return the codegen data version.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text codegen data format.
bool hasStableFunctionMap() const override
Return true if the header indicates the data has a stable function map.
bool hasOutlinedHashTree() const override
Return true if the header indicates the data has an outlined hash tree.
Error read() override
Read the contents including the header.
TextCodeGenDataReader & operator=(const TextCodeGenDataReader &)=delete
uint32_t getVersion() const override
Text format does not have version, so return 0.
TextCodeGenDataReader(const TextCodeGenDataReader &)=delete
TextCodeGenDataReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
CGDataKind getDataKind() const override
Return the codegen data kind.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
A forward iterator which reads text lines from a buffer.
This class is the base class for all object file types.
Definition ObjectFile.h:231
The virtual file system interface.
template class LLVM_TEMPLATE_ABI opt< bool >
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI cl::opt< bool > IndexedCodeGenDataLazyLoading
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:990
uint64_t stable_hash
An opaque object representing a stable hash code.
cgdata_error
Definition CodeGenData.h:53
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
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:1888
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
The structure of the serialized stable function map is as follows: