LLVM 22.0.0git
LLVMRemarkStreamer.cpp
Go to the documentation of this file.
1//===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- 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 the implementation of the conversion between IR
10// Diagnostics and serializable remarks::Remark objects.
11//
12//===----------------------------------------------------------------------===//
13
16#include "llvm/IR/Function.h"
17#include "llvm/IR/GlobalValue.h"
21#include <optional>
22
23using namespace llvm;
24
25/// DiagnosticKind -> remarks::Type
47
48/// DiagnosticLocation -> remarks::RemarkLocation.
49static std::optional<remarks::RemarkLocation>
51 if (!DL.isValid())
52 return std::nullopt;
53 StringRef File = DL.getRelativePath();
54 unsigned Line = DL.getLine();
55 unsigned Col = DL.getColumn();
56 return remarks::RemarkLocation{File, Line, Col};
57}
58
59/// LLVM Diagnostic -> Remark
61LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
62 remarks::Remark R; // The result.
63 R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
64 R.PassName = Diag.getPassName();
65 R.RemarkName = Diag.getRemarkName();
66 R.FunctionName =
68 R.Loc = toRemarkLocation(Diag.getLocation());
69 R.Hotness = Diag.getHotness();
70
71 for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
72 R.Args.emplace_back();
73 R.Args.back().Key = Arg.Key;
74 R.Args.back().Val = Arg.Val;
75 R.Args.back().Loc = toRemarkLocation(Arg.Loc);
76 }
77
78 return R;
79}
80
82 if (!RS.matchesFilter(Diag.getPassName()))
83 return;
84
85 // First, convert the diagnostic to a remark.
86 remarks::Remark R = toRemark(Diag);
87 // Then, emit the remark through the serializer.
88 RS.getSerializer().emit(R);
89}
90
94
98 std::optional<uint64_t> RemarksHotnessThreshold) {
100 Context.setDiagnosticsHotnessRequested(true);
101
102 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
103
104 if (RemarksFilename.empty())
105 return LLVMRemarkFileHandle();
106
108 if (Error E = Format.takeError())
109 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
110
111 std::error_code EC;
114 auto RemarksFile =
115 std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
116 // We don't use llvm::FileError here because some diagnostics want the file
117 // name separately.
118 if (EC)
120
122 remarks::createRemarkSerializer(*Format, RemarksFile->os());
123 if (Error E = RemarkSerializer.takeError())
124 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
125
126 auto RS = std::make_unique<remarks::RemarkStreamer>(
127 std::move(*RemarkSerializer), RemarksFilename);
128
129 if (!RemarksPasses.empty())
130 if (Error E = RS->setFilter(RemarksPasses)) {
131 RS->releaseSerializer();
132 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
133 }
134
135 // Install the main remark streamer. Only install this after setting the
136 // filter, because this might fail.
137 Context.setMainRemarkStreamer(std::move(RS));
138
139 // Create LLVM's optimization remarks streamer.
140 Context.setLLVMRemarkStreamer(
141 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
142
143 return LLVMRemarkFileHandle{std::move(RemarksFile), Context};
144}
145
146void LLVMRemarkFileHandle::Finalizer::finalize() {
147 if (!Context)
148 return;
150 Context = nullptr;
151}
152
156 std::optional<uint64_t> RemarksHotnessThreshold) {
158 Context.setDiagnosticsHotnessRequested(true);
159
160 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
161
163 if (Error E = Format.takeError())
164 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
165
168 if (Error E = RemarkSerializer.takeError())
169 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
170
171 auto RS =
172 std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer));
173
174 if (!RemarksPasses.empty())
175 if (Error E = RS->setFilter(RemarksPasses)) {
176 RS->releaseSerializer();
177 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
178 }
179
180 // Install the main remark streamer. Only install this after setting the
181 // filter, because this might fail.
182 Context.setMainRemarkStreamer(std::move(RS));
183
184 // Create LLVM's optimization remarks streamer.
185 Context.setLLVMRemarkStreamer(
186 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
187
188 return Error::success();
189}
190
192 Context.setLLVMRemarkStreamer(nullptr);
193 if (auto *RS = Context.getMainRemarkStreamer()) {
194 RS->releaseSerializer();
195 Context.setMainRemarkStreamer(nullptr);
196 }
197}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static std::optional< remarks::RemarkLocation > toRemarkLocation(const DiagnosticLocation &DL)
DiagnosticLocation -> remarks::RemarkLocation.
static remarks::Type toRemarkType(enum DiagnosticKind Kind)
DiagnosticKind -> remarks::Type.
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
ArrayRef< Argument > getArgs() const
std::optional< uint64_t > getHotness() const
const Function & getFunction() const
void getLocation(StringRef &RelativePath, unsigned &Line, unsigned &Column) const
Return location information for this diagnostic in three parts: the relative source file path,...
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
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
RAII handle that manages the lifetime of the ToolOutputFile used to output remarks.
LLVM_ABI void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI Expected< Format > parseFormat(StringRef FormatStr)
Parse and validate a string for the remark format.
LLVM_ABI Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, raw_ostream &OS)
Create a remark serializer.
Type
The type of the remark.
Definition Remark.h:66
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:776
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"))
DiagnosticKind
Defines the different supported kind of a diagnostic.
@ DK_OptimizationRemarkAnalysis
@ DK_OptimizationRemarkAnalysisAliasing
@ DK_MachineOptimizationRemark
@ DK_OptimizationRemarkMissed
@ DK_MachineOptimizationRemarkAnalysis
@ DK_OptimizationRemark
@ DK_MachineOptimizationRemarkMissed
@ DK_OptimizationFailure
@ DK_OptimizationRemarkAnalysisFPCommute
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"))
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
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 Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:111
LLVM_ABI void finalizeLLVMOptimizationRemarks(LLVMContext &Context)
Finalize optimization remarks and deregister the RemarkStreamer from the Context.
The debug location used to track a remark back to the source file.
Definition Remark.h:32
A remark type used for both emission and parsing.
Definition Remark.h:98