LLVM 24.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"
22#include <optional>
23
24using namespace llvm;
25
26/// DiagnosticKind -> remarks::Type
48
49/// DiagnosticLocation -> remarks::RemarkLocation.
50static std::optional<remarks::RemarkLocation>
52 if (!DL.isValid())
53 return std::nullopt;
54 StringRef File = DL.getRelativePath();
55 unsigned Line = DL.getLine();
56 unsigned Col = DL.getColumn();
57 return remarks::RemarkLocation{File, Line, Col};
58}
59
60/// LLVM Diagnostic -> Remark
62LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
63 remarks::Remark R; // The result.
64 R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
65 R.PassName = Diag.getPassName();
66 R.RemarkName = Diag.getRemarkName();
67 R.FunctionName =
69 R.Loc = toRemarkLocation(Diag.getLocation());
70 R.Hotness = Diag.getHotness();
71
72 for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
73 R.Args.emplace_back();
74 R.Args.back().Key = Arg.Key;
75 R.Args.back().Val = Arg.Val;
76 R.Args.back().Loc = toRemarkLocation(Arg.Loc);
77 }
78
79 return R;
80}
81
83 if (!RS.matchesFilter(Diag.getPassName()))
84 return;
85
86 // First, convert the diagnostic to a remark.
87 remarks::Remark R = toRemark(Diag);
88 // Then, emit the remark through the serializer.
89 RS.getSerializer().emit(R);
90}
91
95
99 std::optional<uint64_t> RemarksHotnessThreshold) {
101 Context.setDiagnosticsHotnessRequested(true);
102
103 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
104
105 if (RemarksFilename.empty())
106 return LLVMRemarkFileHandle();
107
109 if (Error E = Format.takeError())
110 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
111
112 // Resolve the filename up front, so that the path recorded in the remarks
113 // section always denotes the file that gets created below.
114 SmallString<128> AbsRemarksFilename(RemarksFilename);
115 if (RemarksFilename != "-") {
116 if (std::error_code EC =
117 Context.getVirtualFileSystem().makeAbsolute(AbsRemarksFilename))
119 RemarksFilename = AbsRemarksFilename;
120 }
121
122 std::error_code EC;
125 auto RemarksFile =
126 std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
127 // We don't use llvm::FileError here because some diagnostics want the file
128 // name separately.
129 if (EC)
131
133 remarks::createRemarkSerializer(*Format, RemarksFile->os());
134 if (Error E = RemarkSerializer.takeError())
135 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
136
137 auto RS = std::make_unique<remarks::RemarkStreamer>(
138 std::move(*RemarkSerializer), RemarksFilename);
139
140 if (!RemarksPasses.empty())
141 if (Error E = RS->setFilter(RemarksPasses)) {
142 RS->releaseSerializer();
143 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
144 }
145
146 // Install the main remark streamer. Only install this after setting the
147 // filter, because this might fail.
148 Context.setMainRemarkStreamer(std::move(RS));
149
150 // Create LLVM's optimization remarks streamer.
151 Context.setLLVMRemarkStreamer(
152 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
153
154 return LLVMRemarkFileHandle{std::move(RemarksFile), Context};
155}
156
157void LLVMRemarkFileHandle::Finalizer::finalize() {
158 if (!Context)
159 return;
161 Context = nullptr;
162}
163
167 std::optional<uint64_t> RemarksHotnessThreshold) {
169 Context.setDiagnosticsHotnessRequested(true);
170
171 Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
172
174 if (Error E = Format.takeError())
175 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
176
179 if (Error E = RemarkSerializer.takeError())
180 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
181
182 auto RS =
183 std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer));
184
185 if (!RemarksPasses.empty())
186 if (Error E = RS->setFilter(RemarksPasses)) {
187 RS->releaseSerializer();
188 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
189 }
190
191 // Install the main remark streamer. Only install this after setting the
192 // filter, because this might fail.
193 Context.setMainRemarkStreamer(std::move(RS));
194
195 // Create LLVM's optimization remarks streamer.
196 Context.setLLVMRemarkStreamer(
197 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
198
199 return Error::success();
200}
201
203 Context.setLLVMRemarkStreamer(nullptr);
204 if (auto *RS = Context.getMainRemarkStreamer()) {
205 RS->releaseSerializer();
206 Context.setMainRemarkStreamer(nullptr);
207 }
208}
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.
Defines the virtual file system interface vfs::FileSystem.
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:76
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.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
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:75
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:802
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:107
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:107