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
22
23namespace llvm {
24
27 std::string LastErrorMsg;
28
29public:
30 CodeGenDataReader() = default;
31 virtual ~CodeGenDataReader() = default;
32
33 /// Read the header. Required before reading first record.
34 virtual Error read() = 0;
35 /// Return the codegen data version.
36 virtual uint32_t getVersion() const = 0;
37 /// Return the codegen data kind.
38 virtual CGDataKind getDataKind() const = 0;
39 /// Return true if the data has an outlined hash tree.
40 virtual bool hasOutlinedHashTree() const = 0;
41 /// Return true if the data has a stable function map.
42 virtual bool hasStableFunctionMap() const = 0;
43 /// Return the outlined hash tree that is released from the reader.
44 std::unique_ptr<OutlinedHashTree> releaseOutlinedHashTree() {
45 return std::move(HashTreeRecord.HashTree);
46 }
47 std::unique_ptr<StableFunctionMap> releaseStableFunctionMap() {
48 return std::move(FunctionMapRecord.FunctionMap);
49 }
50
51 /// Factory method to create an appropriately typed reader for the given
52 /// codegen data file path and file system.
54 create(const Twine &Path, vfs::FileSystem &FS);
55
56 /// Factory method to create an appropriately typed reader for the given
57 /// memory buffer.
59 create(std::unique_ptr<MemoryBuffer> Buffer);
60
61 /// Extract the cgdata embedded in sections from the given object file and
62 /// merge them into the GlobalOutlineRecord. This is a static helper that
63 /// is used by `llvm-cgdata --merge` or ThinLTO's two-codegen rounds.
64 /// Optionally, \p CombinedHash can be used to compuate the combined hash of
65 /// the merged data.
66 LLVM_ABI static Error
68 OutlinedHashTreeRecord &GlobalOutlineRecord,
69 StableFunctionMapRecord &GlobalFunctionMapRecord,
70 stable_hash *CombinedHash = nullptr);
71
72protected:
73 /// The outlined hash tree that has been read. When it's released by
74 /// releaseOutlinedHashTree(), it's no longer valid.
76
77 /// The stable function map that has been read. When it's released by
78 // releaseStableFunctionMap(), it's no longer valid.
80
81 /// Set the current error and return same.
82 Error error(cgdata_error Err, const std::string &ErrMsg = "") {
83 LastError = Err;
84 LastErrorMsg = ErrMsg;
85 if (Err == cgdata_error::success)
86 return Error::success();
87 return make_error<CGDataError>(Err, ErrMsg);
88 }
89
91 handleAllErrors(std::move(E), [&](const CGDataError &IPE) {
92 LastError = IPE.get();
93 LastErrorMsg = IPE.getMessage();
94 });
95 return make_error<CGDataError>(LastError, LastErrorMsg);
96 }
97
98 /// Clear the current error and return a successful one.
100};
101
103 /// The codegen data file contents.
104 std::unique_ptr<MemoryBuffer> DataBuffer;
105 /// The header
107
108public:
109 IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer)
110 : DataBuffer(std::move(DataBuffer)) {}
114
115 /// Return true if the given buffer is in binary codegen data format.
116 static bool hasFormat(const MemoryBuffer &Buffer);
117 /// Read the contents including the header.
118 Error read() override;
119 /// Return the codegen data version.
120 uint32_t getVersion() const override { return Header.Version; }
121 /// Return the codegen data kind.
122 CGDataKind getDataKind() const override {
123 return static_cast<CGDataKind>(Header.DataKind);
124 }
125 /// Return true if the header indicates the data has an outlined hash tree.
126 /// This does not mean that the data is still available.
127 bool hasOutlinedHashTree() const override {
128 return Header.DataKind &
129 static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
130 }
131 /// Return true if the header indicates the data has a stable function map.
132 bool hasStableFunctionMap() const override {
133 return Header.DataKind &
134 static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
135 }
136};
137
138/// This format is a simple text format that's suitable for test data.
139/// The header is a custom format starting with `:` per line to indicate which
140/// codegen data is recorded. `#` is used to indicate a comment.
141/// The subsequent data is a YAML format per each codegen data in order.
142/// Currently, it only has a function outlined hash tree.
144 /// The codegen data file contents.
145 std::unique_ptr<MemoryBuffer> DataBuffer;
146 /// Iterator over the profile data.
147 line_iterator Line;
148 /// Describe the kind of the codegen data.
149 CGDataKind DataKind = CGDataKind::Unknown;
150
151public:
152 TextCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
153 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
156
157 /// Return true if the given buffer is in text codegen data format.
158 static bool hasFormat(const MemoryBuffer &Buffer);
159 /// Read the contents including the header.
160 Error read() override;
161 /// Text format does not have version, so return 0.
162 uint32_t getVersion() const override { return 0; }
163 /// Return the codegen data kind.
164 CGDataKind getDataKind() const override { return DataKind; }
165 /// Return true if the header indicates the data has an outlined hash tree.
166 /// This does not mean that the data is still available.
167 bool hasOutlinedHashTree() const override {
168 return static_cast<uint32_t>(DataKind) &
169 static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
170 }
171 /// Return true if the header indicates the data has a stable function map.
172 /// This does not mean that the data is still available.
173 bool hasStableFunctionMap() const override {
174 return static_cast<uint32_t>(DataKind) &
175 static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
176 }
177};
178
179} // end namespace llvm
180
181#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
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...
Definition: MemoryBuffer.h:52
This format is a simple text format that's suitable for test data.
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.
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.
Definition: LineIterator.h:34
This class is the base class for all object file types.
Definition: ObjectFile.h:231
The virtual file system interface.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
CGDataKind
Definition: CodeGenData.h:42
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
cgdata_error
Definition: CodeGenData.h:53
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
std::unique_ptr< OutlinedHashTree > HashTree
The structure of the serialized stable function map is as follows:
std::unique_ptr< StableFunctionMap > FunctionMap