LLVM  13.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"
19 
20 using namespace llvm;
21 
22 /// DiagnosticKind -> remarks::Type
24  switch (Kind) {
25  default:
29  return remarks::Type::Passed;
32  return remarks::Type::Missed;
42  }
43 }
44 
45 /// DiagnosticLocation -> remarks::RemarkLocation.
48  if (!DL.isValid())
49  return None;
50  StringRef File = DL.getRelativePath();
51  unsigned Line = DL.getLine();
52  unsigned Col = DL.getColumn();
53  return remarks::RemarkLocation{File, Line, Col};
54 }
55 
56 /// LLVM Diagnostic -> Remark
58 LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
59  remarks::Remark R; // The result.
60  R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
61  R.PassName = Diag.getPassName();
62  R.RemarkName = Diag.getRemarkName();
63  R.FunctionName =
65  R.Loc = toRemarkLocation(Diag.getLocation());
66  R.Hotness = Diag.getHotness();
67 
69  R.Args.emplace_back();
70  R.Args.back().Key = Arg.Key;
71  R.Args.back().Val = Arg.Val;
72  R.Args.back().Loc = toRemarkLocation(Arg.Loc);
73  }
74 
75  return R;
76 }
77 
79  if (!RS.matchesFilter(Diag.getPassName()))
80  return;
81 
82  // First, convert the diagnostic to a remark.
83  remarks::Remark R = toRemark(Diag);
84  // Then, emit the remark through the serializer.
85  RS.getSerializer().emit(R);
86 }
87 
91 
98 
100 
101  if (RemarksFilename.empty())
102  return nullptr;
103 
105  if (Error E = Format.takeError())
106  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
107 
108  std::error_code EC;
109  auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_Text
111  auto RemarksFile =
112  std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
113  // We don't use llvm::FileError here because some diagnostics want the file
114  // name separately.
115  if (EC)
116  return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
117 
120  *Format, remarks::SerializerMode::Separate, RemarksFile->os());
121  if (Error E = RemarkSerializer.takeError())
122  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
123 
124  // Create the main remark streamer.
125  Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
126  std::move(*RemarkSerializer), RemarksFilename));
127 
128  // Create LLVM's optimization remarks streamer.
130  std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
131 
132  if (!RemarksPasses.empty())
134  return make_error<LLVMRemarkSetupPatternError>(std::move(E));
135 
136  return std::move(RemarksFile);
137 }
138 
143  if (RemarksWithHotness)
145 
147 
149  if (Error E = Format.takeError())
150  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
151 
155  if (Error E = RemarkSerializer.takeError())
156  return make_error<LLVMRemarkSetupFormatError>(std::move(E));
157 
158  // Create the main remark streamer.
160  std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
161 
162  // Create LLVM's optimization remarks streamer.
164  std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
165 
166  if (!RemarksPasses.empty())
168  return make_error<LLVMRemarkSetupPatternError>(std::move(E));
169 
170  return Error::success();
171 }
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:77
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:135
llvm::EngineKind::Kind
Kind
Definition: ExecutionEngine.h:524
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:65
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:742
llvm::GlobalValue::dropLLVMManglingEscape
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:482
llvm::DK_MachineOptimizationRemarkMissed
@ DK_MachineOptimizationRemarkMissed
Definition: DiagnosticInfo.h:74
llvm::LLVMRemarkSetupFileError::ID
static char ID
Definition: LLVMRemarkStreamer.h:58
llvm::LLVMContext::setLLVMRemarkStreamer
void setLLVMRemarkStreamer(std::unique_ptr< LLVMRemarkStreamer > RemarkStreamer)
Definition: LLVMContext.cpp:164
llvm::remarks::SerializerMode::Separate
@ Separate
llvm::DiagnosticInfoOptimizationBase::getPassName
StringRef getPassName() const
Definition: DiagnosticInfo.h:474
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:76
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
llvm::DiagnosticInfo::getKind
int getKind() const
Definition: DiagnosticInfo.h:115
llvm::LLVMContext::getMainRemarkStreamer
remarks::RemarkStreamer * getMainRemarkStreamer()
The "main remark streamer" used by all the specialized remark streamers.
Definition: LLVMContext.cpp:147
llvm::DK_OptimizationRemarkAnalysisFPCommute
@ DK_OptimizationRemarkAnalysisFPCommute
Definition: DiagnosticInfo.h:68
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:80
llvm::remarks::Type::Missed
@ Missed
llvm::Optional
Definition: APInt.h:33
llvm::DiagnosticInfoOptimizationBase::getRemarkName
StringRef getRemarkName() const
Definition: DiagnosticInfo.h:475
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:78
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::LLVMRemarkStreamer::emit
void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
Definition: LLVMRemarkStreamer.cpp:78
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:408
llvm::DiagnosticKind
DiagnosticKind
Defines the different supported kind of a diagnostic.
Definition: DiagnosticInfo.h:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:129
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:205
llvm::remarks::RemarkStreamer::getSerializer
remarks::RemarkSerializer & getSerializer()
Return the serializer used for this stream.
Definition: RemarkStreamer.h:61
llvm::DK_MachineOptimizationRemark
@ DK_MachineOptimizationRemark
Definition: DiagnosticInfo.h:73
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:92
llvm::DK_MachineOptimizationRemarkAnalysis
@ DK_MachineOptimizationRemarkAnalysis
Definition: DiagnosticInfo.h:75
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DiagnosticInfoOptimizationBase::getHotness
Optional< uint64_t > getHotness() const
Definition: DiagnosticInfo.h:477
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
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:395
llvm::DK_OptimizationRemarkAnalysis
@ DK_OptimizationRemarkAnalysis
Definition: DiagnosticInfo.h:67
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::DK_OptimizationRemarkMissed
@ DK_OptimizationRemarkMissed
Definition: DiagnosticInfo.h:66
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::None
const NoneType None
Definition: None.h:23
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:81
llvm::remarks::RemarkSerializer::emit
virtual void emit(const Remark &Remark)=0
Emit a remark to the stream.
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:421
llvm::remarks::Type::Passed
@ Passed
llvm::remarks::Type::Analysis
@ Analysis
llvm::remarks::Format::YAML
@ YAML
llvm::sys::fs::OF_Text
@ OF_Text
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:747
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
llvm::LLVMContext::setDiagnosticsHotnessRequested
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
Definition: LLVMContext.cpp:128
llvm::LLVMContext::setMainRemarkStreamer
void setMainRemarkStreamer(std::unique_ptr< remarks::RemarkStreamer > MainRemarkStreamer)
Definition: LLVMContext.cpp:153
llvm::remarks::Type
Type
The type of the remark.
Definition: Remark.h:54
llvm::DK_OptimizationRemarkAnalysisAliasing
@ DK_OptimizationRemarkAnalysisAliasing
Definition: DiagnosticInfo.h:69
toRemarkLocation
static Optional< remarks::RemarkLocation > toRemarkLocation(const DiagnosticLocation &DL)
DiagnosticLocation -> remarks::RemarkLocation.
Definition: LLVMRemarkStreamer.cpp:47
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::DK_OptimizationFailure
@ DK_OptimizationFailure
Definition: DiagnosticInfo.h:70
llvm::DiagnosticLocation
Definition: DiagnosticInfo.h:348
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:295
toRemarkType
static remarks::Type toRemarkType(enum DiagnosticKind Kind)
DiagnosticKind -> remarks::Type.
Definition: LLVMRemarkStreamer.cpp:23
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:157
DiagnosticInfo.h
Function.h
llvm::DK_OptimizationRemark
@ DK_OptimizationRemark
Definition: DiagnosticInfo.h:65
llvm::LLVMRemarkSetupFormatError::ID
static char ID
Definition: LLVMRemarkStreamer.h:72
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
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:147
llvm::remarks::Type::Unknown
@ Unknown
llvm::remarks::Type::AnalysisAliasing
@ AnalysisAliasing
llvm::remarks::Type::AnalysisFPCommute
@ AnalysisFPCommute
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:206
llvm::DiagnosticInfoOptimizationBase::getArgs
ArrayRef< Argument > getArgs() const
Definition: DiagnosticInfo.h:482