LLVM 20.0.0git
CodeGenData.h
Go to the documentation of this file.
1//===- CodeGenData.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 codegen data that has stable summary which
10// can be used to optimize the code in the subsequent codegen.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CGDATA_CODEGENDATA_H
15#define LLVM_CGDATA_CODEGENDATA_H
16
21#include "llvm/IR/Module.h"
25#include <mutex>
26
27namespace llvm {
28
30#define CG_DATA_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind,
32};
33
36 bool AddSegmentInfo = true);
37
38enum class CGDataKind {
39 Unknown = 0x0,
40 // A function outlining info.
43};
44
45const std::error_category &cgdata_category();
46
47enum class cgdata_error {
48 success = 0,
49 eof,
55};
56
57inline std::error_code make_error_code(cgdata_error E) {
58 return std::error_code(static_cast<int>(E), cgdata_category());
59}
60
61class CGDataError : public ErrorInfo<CGDataError> {
62public:
63 CGDataError(cgdata_error Err, const Twine &ErrStr = Twine())
64 : Err(Err), Msg(ErrStr.str()) {
65 assert(Err != cgdata_error::success && "Not an error");
66 }
67
68 std::string message() const override;
69
70 void log(raw_ostream &OS) const override { OS << message(); }
71
72 std::error_code convertToErrorCode() const override {
73 return make_error_code(Err);
74 }
75
76 cgdata_error get() const { return Err; }
77 const std::string &getMessage() const { return Msg; }
78
79 /// Consume an Error and return the raw enum value contained within it, and
80 /// the optional error message. The Error must either be a success value, or
81 /// contain a single CGDataError.
82 static std::pair<cgdata_error, std::string> take(Error E) {
83 auto Err = cgdata_error::success;
84 std::string Msg;
85 handleAllErrors(std::move(E), [&Err, &Msg](const CGDataError &IPE) {
86 assert(Err == cgdata_error::success && "Multiple errors encountered");
87 Err = IPE.get();
88 Msg = IPE.getMessage();
89 });
90 return {Err, Msg};
91 }
92
93 static char ID;
94
95private:
96 cgdata_error Err;
97 std::string Msg;
98};
99
104};
105
107 /// Global outlined hash tree that has oulined hash sequences across modules.
108 std::unique_ptr<OutlinedHashTree> PublishedHashTree;
109
110 /// This flag is set when -fcodegen-data-generate is passed.
111 /// Or, it can be mutated with -fcodegen-data-thinlto-two-rounds.
112 bool EmitCGData;
113
114 /// This is a singleton instance which is thread-safe. Unlike profile data
115 /// which is largely function-based, codegen data describes the whole module.
116 /// Therefore, this can be initialized once, and can be used across modules
117 /// instead of constructing the same one for each codegen backend.
118 static std::unique_ptr<CodeGenData> Instance;
119 static std::once_flag OnceFlag;
120
121 CodeGenData() = default;
122
123public:
124 ~CodeGenData() = default;
125
126 static CodeGenData &getInstance();
127
128 /// Returns true if we have a valid outlined hash tree.
130 return PublishedHashTree && !PublishedHashTree->empty();
131 }
132
133 /// Returns the outlined hash tree. This can be globally used in a read-only
134 /// manner.
136 return PublishedHashTree.get();
137 }
138
139 /// Returns true if we should write codegen data.
140 bool emitCGData() { return EmitCGData; }
141
142 /// Publish the (globally) merged or read outlined hash tree.
143 void publishOutlinedHashTree(std::unique_ptr<OutlinedHashTree> HashTree) {
144 PublishedHashTree = std::move(HashTree);
145 // Ensure we disable emitCGData as we do not want to read and write both.
146 EmitCGData = false;
147 }
148};
149
150namespace cgdata {
151
152inline bool hasOutlinedHashTree() {
154}
155
158}
159
160inline bool emitCGData() { return CodeGenData::getInstance().emitCGData(); }
161
162inline void
163publishOutlinedHashTree(std::unique_ptr<OutlinedHashTree> HashTree) {
165}
166
167void warn(Error E, StringRef Whence = "");
168void warn(Twine Message, std::string Whence = "", std::string Hint = "");
169
170} // end namespace cgdata
171
172namespace IndexedCGData {
173
174// A signature for data validation, representing "\xffcgdata\x81" in
175// little-endian order
176const uint64_t Magic = 0x81617461646763ff;
177
179 // Version 1 is the first version. This version supports the outlined
180 // hash tree.
182 CurrentVersion = CG_DATA_INDEX_VERSION
185
186struct Header {
191
192 // New fields should only be added at the end to ensure that the size
193 // computation is correct. The methods below need to be updated to ensure that
194 // the new field is read correctly.
195
196 // Reads a header struct from the buffer.
197 static Expected<Header> readFromBuffer(const unsigned char *Curr);
198};
199
200} // end namespace IndexedCGData
201
202} // end namespace llvm
203
204#endif // LLVM_CODEGEN_PREPARE_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
const std::string & getMessage() const
Definition: CodeGenData.h:77
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: CodeGenData.h:72
static std::pair< cgdata_error, std::string > take(Error E)
Consume an Error and return the raw enum value contained within it, and the optional error message.
Definition: CodeGenData.h:82
static char ID
Definition: CodeGenData.h:93
std::string message() const override
Return the error message as a string.
Definition: CodeGenData.cpp:83
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: CodeGenData.h:70
CGDataError(cgdata_error Err, const Twine &ErrStr=Twine())
Definition: CodeGenData.h:63
cgdata_error get() const
Definition: CodeGenData.h:76
bool emitCGData()
Returns true if we should write codegen data.
Definition: CodeGenData.h:140
void publishOutlinedHashTree(std::unique_ptr< OutlinedHashTree > HashTree)
Publish the (globally) merged or read outlined hash tree.
Definition: CodeGenData.h:143
bool hasOutlinedHashTree()
Returns true if we have a valid outlined hash tree.
Definition: CodeGenData.h:129
const OutlinedHashTree * getOutlinedHashTree()
Returns the outlined hash tree.
Definition: CodeGenData.h:135
~CodeGenData()=default
static CodeGenData & getInstance()
Base class for user error types.
Definition: Error.h:355
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
ObjectFormatType
Definition: Triple.h:297
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
const uint64_t Version
Definition: CodeGenData.h:184
const uint64_t Magic
Definition: CodeGenData.h:176
void publishOutlinedHashTree(std::unique_ptr< OutlinedHashTree > HashTree)
Definition: CodeGenData.h:163
bool hasOutlinedHashTree()
Definition: CodeGenData.h:152
void warn(Error E, StringRef Whence="")
const OutlinedHashTree * getOutlinedHashTree()
Definition: CodeGenData.h:156
bool emitCGData()
Definition: CodeGenData.h:160
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
CGDataKind
Definition: CodeGenData.h:38
CGDataMode
Definition: CodeGenData.h:100
@ Read
Definition: CodeGenData.h:102
@ Write
Definition: CodeGenData.h:103
std::error_code make_error_code(BitcodeError E)
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
@ None
Definition: CodeGenData.h:101
const std::error_category & cgdata_category()
Definition: CodeGenData.cpp:78
CGDataSectKind
Definition: CodeGenData.h:29
std::string getCodeGenDataSectionName(CGDataSectKind CGSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
static Expected< Header > readFromBuffer(const unsigned char *Curr)