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