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
20
21namespace llvm {
22
25 std::string LastErrorMsg;
26
27public:
28 CodeGenDataReader() = default;
29 virtual ~CodeGenDataReader() = default;
30
31 /// Read the header. Required before reading first record.
32 virtual Error read() = 0;
33 /// Return the codegen data version.
34 virtual uint32_t getVersion() const = 0;
35 /// Return the codegen data kind.
36 virtual CGDataKind getDataKind() const = 0;
37 /// Return true if the data has an outlined hash tree.
38 virtual bool hasOutlinedHashTree() const = 0;
39 /// Return the outlined hash tree that is released from the reader.
40 std::unique_ptr<OutlinedHashTree> releaseOutlinedHashTree() {
41 return std::move(HashTreeRecord.HashTree);
42 }
43
44 /// Factory method to create an appropriately typed reader for the given
45 /// codegen data file path and file system.
47 create(const Twine &Path, vfs::FileSystem &FS);
48
49 /// Factory method to create an appropriately typed reader for the given
50 /// memory buffer.
52 create(std::unique_ptr<MemoryBuffer> Buffer);
53
54 /// Extract the cgdata embedded in sections from the given object file and
55 /// merge them into the GlobalOutlineRecord. This is a static helper that
56 /// is used by `llvm-cgdata --merge` or ThinLTO's two-codegen rounds.
58 OutlinedHashTreeRecord &GlobalOutlineRecord);
59
60protected:
61 /// The outlined hash tree that has been read. When it's released by
62 /// releaseOutlinedHashTree(), it's no longer valid.
64
65 /// Set the current error and return same.
66 Error error(cgdata_error Err, const std::string &ErrMsg = "") {
67 LastError = Err;
68 LastErrorMsg = ErrMsg;
69 if (Err == cgdata_error::success)
70 return Error::success();
71 return make_error<CGDataError>(Err, ErrMsg);
72 }
73
75 handleAllErrors(std::move(E), [&](const CGDataError &IPE) {
76 LastError = IPE.get();
77 LastErrorMsg = IPE.getMessage();
78 });
79 return make_error<CGDataError>(LastError, LastErrorMsg);
80 }
81
82 /// Clear the current error and return a successful one.
84};
85
87 /// The codegen data file contents.
88 std::unique_ptr<MemoryBuffer> DataBuffer;
89 /// The header
91
92public:
93 IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer)
94 : DataBuffer(std::move(DataBuffer)) {}
98
99 /// Return true if the given buffer is in binary codegen data format.
100 static bool hasFormat(const MemoryBuffer &Buffer);
101 /// Read the contents including the header.
102 Error read() override;
103 /// Return the codegen data version.
104 uint32_t getVersion() const override { return Header.Version; }
105 /// Return the codegen data kind.
106 CGDataKind getDataKind() const override {
107 return static_cast<CGDataKind>(Header.DataKind);
108 }
109 /// Return true if the header indicates the data has an outlined hash tree.
110 /// This does not mean that the data is still available.
111 bool hasOutlinedHashTree() const override {
112 return Header.DataKind &
114 }
115};
116
117/// This format is a simple text format that's suitable for test data.
118/// The header is a custom format starting with `:` per line to indicate which
119/// codegen data is recorded. `#` is used to indicate a comment.
120/// The subsequent data is a YAML format per each codegen data in order.
121/// Currently, it only has a function outlined hash tree.
123 /// The codegen data file contents.
124 std::unique_ptr<MemoryBuffer> DataBuffer;
125 /// Iterator over the profile data.
126 line_iterator Line;
127 /// Describe the kind of the codegen data.
129
130public:
131 TextCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
132 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
135
136 /// Return true if the given buffer is in text codegen data format.
137 static bool hasFormat(const MemoryBuffer &Buffer);
138 /// Read the contents including the header.
139 Error read() override;
140 /// Text format does not have version, so return 0.
141 uint32_t getVersion() const override { return 0; }
142 /// Return the codegen data kind.
143 CGDataKind getDataKind() const override { return DataKind; }
144 /// Return true if the header indicates the data has an outlined hash tree.
145 /// This does not mean that the data is still available.
146 bool hasOutlinedHashTree() const override {
147 return static_cast<uint32_t>(DataKind) &
149 }
150};
151
152} // end namespace llvm
153
154#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:77
cgdata_error get() const
Definition: CodeGenData.h:76
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 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.
static Error mergeFromObjectFile(const object::ObjectFile *Obj, OutlinedHashTreeRecord &GlobalOutlineRecord)
Extract the cgdata embedded in sections from the given object file and merge them into the GlobalOutl...
virtual CGDataKind getDataKind() const =0
Return the codegen data kind.
virtual Error read()=0
Read the header. Required before reading first record.
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.
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 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:38
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:47
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
std::unique_ptr< OutlinedHashTree > HashTree