LLVM  15.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 
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalValue.h"
21 
22 using namespace llvm;
23 
24 /// DiagnosticKind -> remarks::Type
26  switch (Kind) {
27  default:
31  return remarks::Type::Passed;
34  return remarks::Type::Missed;
44  }
45 }
46 
47 /// DiagnosticLocation -> remarks::RemarkLocation.
50  if (!DL.isValid())
51  return None;
52  StringRef File = DL.getRelativePath();
53  unsigned Line = DL.getLine();
54  unsigned Col = DL.getColumn();
55  return remarks::RemarkLocation{File, Line, Col};
56 }
57 
58 /// LLVM Diagnostic -> Remark
60 LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
61  remarks::Remark R; // The result.
62  R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
63  R.PassName = Diag.getPassName();
64  R.RemarkName = Diag.getRemarkName();
65  R.FunctionName =
67  R.Loc = toRemarkLocation(Diag.getLocation());
68  R.Hotness = Diag.getHotness();
69 
71  R.Args.emplace_back();
72  R.Args.back().Key = Arg.Key;
73  R.Args.back().Val = Arg.Val;
74  R.Args.back().Loc = toRemarkLocation(Arg.Loc);
75  }
76 
77  return R;
78 }
79 
81  if (!RS.matchesFilter(Diag.getPassName()))
82  return;
83 
84  // First, convert the diagnostic to a remark.
85  remarks::Remark R = toRemark(Diag);
86  // Then, emit the remark through the serializer.
87  RS.getSerializer().emit(R);
88 }
89 
93 
100 
102 
103  if (RemarksFilename.empty())
104  return nullptr;
105 
107  if (Error E = Format.takeError())
108  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
109 
110  std::error_code EC;
111  auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_TextWithCRLF
113  auto RemarksFile =
114  std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
115  // We don't use llvm::FileError here because some diagnostics want the file
116  // name separately.
117  if (EC)
118  return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
119 
122  *Format, remarks::SerializerMode::Separate, RemarksFile->os());
123  if (Error E = RemarkSerializer.takeError())
124  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
125 
126  // Create the main remark streamer.
127  Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
128  std::move(*RemarkSerializer), RemarksFilename));
129 
130  // Create LLVM's optimization remarks streamer.
132  std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
133 
134  if (!RemarksPasses.empty())
136  return make_error<LLVMRemarkSetupPatternError>(std::move(E));
137 
138  return std::move(RemarksFile);
139 }
140 
145  if (RemarksWithHotness)
147 
149 
151  if (Error E = Format.takeError())
152  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
153 
157  if (Error E = RemarkSerializer.takeError())
158  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
159 
160  // Create the main remark streamer.
162  std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
163 
164  // Create LLVM's optimization remarks streamer.
166  std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
167 
168  if (!RemarksPasses.empty())
170  return make_error<LLVMRemarkSetupPatternError>(std::move(E));
171 
172  return Error::success();
173 }
llvm::RemarksPasses
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"))
Definition: ThinLTOCodeGenerator.cpp:79
llvm::LLVMContext::setDiagnosticsHotnessThreshold
void setDiagnosticsHotnessThreshold(Optional< uint64_t > Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics.
Definition: LLVMContext.cpp:138
llvm::remarks::createRemarkSerializer
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
Definition: RemarkSerializer.cpp:21
llvm::LLVMRemarkSetupPatternError::ID
static char ID
Definition: LLVMRemarkStreamer.h:71
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
llvm::GlobalValue::dropLLVMManglingEscape
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:530
llvm::DK_MachineOptimizationRemarkMissed
@ DK_MachineOptimizationRemarkMissed
Definition: DiagnosticInfo.h:79
llvm::LLVMRemarkSetupFileError::ID
static char ID
Definition: LLVMRemarkStreamer.h:64
llvm::LLVMContext::setLLVMRemarkStreamer
void setLLVMRemarkStreamer(std::unique_ptr< LLVMRemarkStreamer > RemarkStreamer)
Definition: LLVMContext.cpp:179
llvm::remarks::SerializerMode::Separate
@ Separate
llvm::DiagnosticInfoOptimizationBase::getPassName
StringRef getPassName() const
Definition: DiagnosticInfo.h:480
llvm::remarks::RemarkLocation
The debug location used to track a remark back to the source file.
Definition: Remark.h:30
llvm::RemarksFilename
cl::opt< std::string > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
Definition: ThinLTOCodeGenerator.cpp:78
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::DiagnosticInfo::getKind
int getKind() const
Definition: DiagnosticInfo.h:122
llvm::LLVMContext::getMainRemarkStreamer
remarks::RemarkStreamer * getMainRemarkStreamer()
The "main remark streamer" used by all the specialized remark streamers.
Definition: LLVMContext.cpp:162
llvm::DK_OptimizationRemarkAnalysisFPCommute
@ DK_OptimizationRemarkAnalysisFPCommute
Definition: DiagnosticInfo.h:73
llvm::RemarksHotnessThreshold
cl::opt< 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)
Definition: ThinLTOCodeGenerator.cpp:82
llvm::remarks::Type::Missed
@ Missed
llvm::Optional
Definition: APInt.h:33
llvm::DiagnosticInfoOptimizationBase::getRemarkName
StringRef getRemarkName() const
Definition: DiagnosticInfo.h:481
llvm::RemarksWithHotness
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
Definition: ThinLTOCodeGenerator.cpp:80
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::LLVMRemarkStreamer::emit
void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
Definition: LLVMRemarkStreamer.cpp:80
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:414
llvm::DiagnosticKind
DiagnosticKind
Defines the different supported kind of a diagnostic.
Definition: DiagnosticInfo.h:60
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::remarks::RemarkStreamer::getSerializer
remarks::RemarkSerializer & getSerializer()
Return the serializer used for this stream.
Definition: RemarkStreamer.h:63
llvm::DK_MachineOptimizationRemark
@ DK_MachineOptimizationRemark
Definition: DiagnosticInfo.h:78
GlobalValue.h
llvm::setupLLVMOptimizationRemarks
Expected< std::unique_ptr< ToolOutputFile > > setupLLVMOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, Optional< uint64_t > RemarksHotnessThreshold=0)
Setup optimization remarks that output to a file.
Definition: LLVMRemarkStreamer.cpp:94
llvm::DK_MachineOptimizationRemarkAnalysis
@ DK_MachineOptimizationRemarkAnalysis
Definition: DiagnosticInfo.h:80
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DiagnosticInfoOptimizationBase::getHotness
Optional< uint64_t > getHotness() const
Definition: DiagnosticInfo.h:483
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:90
llvm::remarks::parseFormat
Expected< Format > parseFormat(StringRef FormatStr)
Parse and validate a string for the remark format.
Definition: RemarkFormat.cpp:20
llvm::DiagnosticInfoWithLocationBase::getFunction
const Function & getFunction() const
Definition: DiagnosticInfo.h:401
llvm::DK_OptimizationRemarkAnalysis
@ DK_OptimizationRemarkAnalysis
Definition: DiagnosticInfo.h:72
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::DK_OptimizationRemarkMissed
@ DK_OptimizationRemarkMissed
Definition: DiagnosticInfo.h:71
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::None
const NoneType None
Definition: None.h:24
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::RemarksFormat
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"))
Definition: ThinLTOCodeGenerator.cpp:83
llvm::remarks::RemarkSerializer::emit
virtual void emit(const Remark &Remark)=0
Emit a remark to the stream.
llvm::sys::fs::OF_TextWithCRLF
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:427
llvm::remarks::Type::Passed
@ Passed
llvm::remarks::Type::Analysis
@ Analysis
llvm::remarks::Format::YAML
@ YAML
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
ToolOutputFile.h
llvm::LLVMContext::setDiagnosticsHotnessRequested
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
Definition: LLVMContext.cpp:131
llvm::LLVMContext::setMainRemarkStreamer
void setMainRemarkStreamer(std::unique_ptr< remarks::RemarkStreamer > MainRemarkStreamer)
Definition: LLVMContext.cpp:168
llvm::remarks::Type
Type
The type of the remark.
Definition: Remark.h:54
llvm::DK_OptimizationRemarkAnalysisAliasing
@ DK_OptimizationRemarkAnalysisAliasing
Definition: DiagnosticInfo.h:74
toRemarkLocation
static Optional< remarks::RemarkLocation > toRemarkLocation(const DiagnosticLocation &DL)
DiagnosticLocation -> remarks::RemarkLocation.
Definition: LLVMRemarkStreamer.cpp:49
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::DK_OptimizationFailure
@ DK_OptimizationFailure
Definition: DiagnosticInfo.h:75
llvm::DiagnosticLocation
Definition: DiagnosticInfo.h:354
llvm::remarks::RemarkStreamer::setFilter
Error setFilter(StringRef Filter)
Set a pass filter based on a regex Filter.
Definition: RemarkStreamer.cpp:32
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:305
toRemarkType
static remarks::Type toRemarkType(enum DiagnosticKind Kind)
DiagnosticKind -> remarks::Type.
Definition: LLVMRemarkStreamer.cpp:25
llvm::remarks::RemarkStreamer::matchesFilter
bool matchesFilter(StringRef Str)
Check wether the string matches the filter.
Definition: RemarkStreamer.cpp:42
LLVMRemarkStreamer.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
RemarkStreamer.h
DiagnosticInfo.h
Function.h
llvm::DK_OptimizationRemark
@ DK_OptimizationRemark
Definition: DiagnosticInfo.h:70
llvm::LLVMRemarkSetupFormatError::ID
static char ID
Definition: LLVMRemarkStreamer.h:78
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::remarks::Type::Failure
@ Failure
llvm::DiagnosticInfoWithLocationBase::getLocation
void getLocation(StringRef &RelativePath, unsigned &Line, unsigned &Column) const
Return location information for this diagnostic in three parts: the relative source file path,...
Definition: DiagnosticInfo.cpp:139
llvm::remarks::Type::Unknown
@ Unknown
llvm::remarks::Type::AnalysisAliasing
@ AnalysisAliasing
llvm::remarks::Type::AnalysisFPCommute
@ AnalysisFPCommute
llvm::DiagnosticInfoOptimizationBase::getArgs
ArrayRef< Argument > getArgs() const
Definition: DiagnosticInfo.h:488