LLVM 17.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
27 switch (Kind) {
28 default:
29 return remarks::Type::Unknown;
32 return remarks::Type::Passed;
35 return remarks::Type::Missed;
38 return remarks::Type::Analysis;
40 return remarks::Type::AnalysisFPCommute;
42 return remarks::Type::AnalysisAliasing;
44 return remarks::Type::Failure;
45 }
46}
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
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) {
101
103
104 if (RemarksFilename.empty())
105 return nullptr;
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)
119 return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
120
123 *Format, remarks::SerializerMode::Separate, RemarksFile->os());
124 if (Error E = RemarkSerializer.takeError())
125 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
126
127 // Create the main remark streamer.
128 Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
129 std::move(*RemarkSerializer), RemarksFilename));
130
131 // Create LLVM's optimization remarks streamer.
133 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
134
135 if (!RemarksPasses.empty())
137 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
138
139 return std::move(RemarksFile);
140}
141
145 std::optional<uint64_t> RemarksHotnessThreshold) {
148
150
152 if (Error E = Format.takeError())
153 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
154
158 if (Error E = RemarkSerializer.takeError())
159 return make_error<LLVMRemarkSetupFormatError>(std::move(E));
160
161 // Create the main remark streamer.
163 std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
164
165 // Create LLVM's optimization remarks streamer.
167 std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
168
169 if (!RemarksPasses.empty())
171 return make_error<LLVMRemarkSetupPatternError>(std::move(E));
172
173 return Error::success();
174}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static std::optional< remarks::RemarkLocation > toRemarkLocation(const DiagnosticLocation &DL)
DiagnosticLocation -> remarks::RemarkLocation.
static remarks::Type toRemarkType(enum DiagnosticKind Kind)
DiagnosticKind -> remarks::Type.
LLVMContext & Context
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:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:562
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
remarks::RemarkStreamer * getMainRemarkStreamer()
The "main remark streamer" used by all the specialized remark streamers.
void setLLVMRemarkStreamer(std::unique_ptr< LLVMRemarkStreamer > RemarkStreamer)
void setDiagnosticsHotnessThreshold(std::optional< uint64_t > Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
void setMainRemarkStreamer(std::unique_ptr< remarks::RemarkStreamer > MainRemarkStreamer)
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Error setFilter(StringRef Filter)
Set a pass filter based on a regex Filter.
remarks::RemarkSerializer & getSerializer()
Return the serializer used for this stream.
bool matchesFilter(StringRef Str)
Check wether the string matches the filter.
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
Expected< Format > parseFormat(StringRef FormatStr)
Parse and validate a string for the remark format.
Type
The type of the remark.
Definition: Remark.h:54
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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"))
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
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, std::optional< uint64_t > RemarksHotnessThreshold=0)
Setup optimization remarks that output to a file.
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"))
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)
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
Used in the streaming interface as the general argument type.
The debug location used to track a remark back to the source file.
Definition: Remark.h:30
virtual void emit(const Remark &Remark)=0
Emit a remark to the stream.
A remark type used for both emission and parsing.
Definition: Remark.h:67