LLVM 22.0.0git
LLVMRemarkStreamer.h
Go to the documentation of this file.
1//===- llvm/IR/LLVMRemarkStreamer.h - Streamer for LLVM remarks--*- 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 implements the conversion between IR Diagnostics and
10// serializable remarks::Remark objects.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_LLVMREMARKSTREAMER_H
15#define LLVM_IR_LLVMREMARKSTREAMER_H
16
17#include "llvm/Remarks/Remark.h"
19#include "llvm/Support/Error.h"
21#include <memory>
22#include <optional>
23#include <string>
24
25namespace llvm {
26
28class LLVMContext;
29class ToolOutputFile;
30namespace remarks {
31class RemarkStreamer;
32}
33
34/// Streamer for LLVM remarks which has logic for dealing with DiagnosticInfo
35/// objects.
38 /// Convert diagnostics into remark objects.
39 /// The lifetime of the members of the result is bound to the lifetime of
40 /// the LLVM diagnostics.
41 remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag) const;
42
43public:
45 /// Emit a diagnostic through the streamer.
47};
48
49template <typename ThisError>
50struct LLVMRemarkSetupErrorInfo : public ErrorInfo<ThisError> {
51 std::string Msg;
52 std::error_code EC;
53
55 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
56 Msg = EIB.message();
57 EC = EIB.convertToErrorCode();
58 });
59 }
60
61 void log(raw_ostream &OS) const override { OS << Msg; }
62 std::error_code convertToErrorCode() const override { return EC; }
63};
64
71
78
85
86/// RAII handle that manages the lifetime of the ToolOutputFile used to output
87/// remarks. On destruction (or when calling releaseFile()), this handle ensures
88/// that the optimization remarks are finalized and the RemarkStreamer is
89/// correctly deregistered from the LLVMContext.
91 struct Finalizer {
92 LLVMContext *Context;
93
94 Finalizer(LLVMContext *Ctx) : Context(Ctx) {}
95
96 Finalizer(const Finalizer &) = delete;
97 Finalizer &operator=(const Finalizer &) = delete;
98
99 Finalizer(Finalizer &&Other) : Context(Other.Context) {
100 Other.Context = nullptr;
101 }
102
103 Finalizer &operator=(Finalizer &&Other) {
104 std::swap(Context, Other.Context);
105 return *this;
106 }
107
108 ~Finalizer() { finalize(); }
109
110 LLVM_ABI void finalize();
111 };
112
113 std::unique_ptr<ToolOutputFile> OutputFile;
114 Finalizer Finalize;
115
116public:
117 LLVMRemarkFileHandle() : OutputFile(nullptr), Finalize(nullptr) {}
118
119 LLVMRemarkFileHandle(std::unique_ptr<ToolOutputFile> OutputFile,
120 LLVMContext &Ctx)
121 : OutputFile(std::move(OutputFile)), Finalize(&Ctx) {}
122
123 ToolOutputFile *get() { return OutputFile.get(); }
124 explicit operator bool() { return bool(OutputFile); }
125
126 /// Finalize remark emission and release the underlying ToolOutputFile.
127 std::unique_ptr<ToolOutputFile> releaseFile() {
128 finalize();
129 return std::move(OutputFile);
130 }
131
132 void finalize() { Finalize.finalize(); }
133
134 ToolOutputFile &operator*() { return *OutputFile; }
135 ToolOutputFile *operator->() { return &*OutputFile; }
136};
137
138/// Set up optimization remarks that output to a file. The LLVMRemarkFileHandle
139/// manages the lifetime of the underlying ToolOutputFile to ensure \ref
140/// finalizeLLVMOptimizationRemarks() is called before the file is destroyed or
141/// released from the handle. The handle must be kept alive until all remarks
142/// were emitted through the remark streamer.
143LLVM_ABI Expected<LLVMRemarkFileHandle> setupLLVMOptimizationRemarks(
144 LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
145 StringRef RemarksFormat, bool RemarksWithHotness,
146 std::optional<uint64_t> RemarksHotnessThreshold = 0);
147
148/// Set up optimization remarks that output directly to a raw_ostream.
149/// \p OS is managed by the caller and must be open for writing until
150/// \ref finalizeLLVMOptimizationRemarks() is called.
152 LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
153 StringRef RemarksFormat, bool RemarksWithHotness,
154 std::optional<uint64_t> RemarksHotnessThreshold = 0);
155
156/// Finalize optimization remarks and deregister the RemarkStreamer from the \p
157/// Context. This must be called before closing the (file) stream that was used
158/// to set up the remarks.
159LLVM_ABI void finalizeLLVMOptimizationRemarks(LLVMContext &Context);
160
161} // end namespace llvm
162
163#endif // LLVM_IR_LLVMREMARKSTREAMER_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
R600 Control Flow Finalizer
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Base class for error info classes.
Definition Error.h:44
virtual std::string message() const
Return the error message as a string.
Definition Error.h:52
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
Base class for user error types.
Definition Error.h:354
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVMRemarkFileHandle(std::unique_ptr< ToolOutputFile > OutputFile, LLVMContext &Ctx)
std::unique_ptr< ToolOutputFile > releaseFile()
Finalize remark emission and release the underlying ToolOutputFile.
LLVMRemarkStreamer(remarks::RemarkStreamer &RS)
LLVM_ABI void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
LLVM_ABI Expected< LLVMRemarkFileHandle > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0)
Set up optimization remarks that output to a file.
cl::opt< std::string > RemarksPasses("lto-pass-remarks-filter", cl::desc("Only record optimization remarks from passes whose " "names match the given regular expression"), cl::value_desc("regex"))
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
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
@ Other
Any other memory.
Definition ModRef.h:68
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:1847
cl::opt< std::optional< uint64_t >, false, remarks::HotnessThresholdParser > RemarksHotnessThreshold("lto-pass-remarks-hotness-threshold", cl::desc("Minimum profile count required for an " "optimization remark to be output." " Use 'auto' to apply the threshold from profile summary."), cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden)
LLVM_ABI void finalizeLLVMOptimizationRemarks(LLVMContext &Context)
Finalize optimization remarks and deregister the RemarkStreamer from the Context.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
A remark type used for both emission and parsing.
Definition Remark.h:98