LLVM  10.0.0svn
RemarkStreamer.cpp
Go to the documentation of this file.
1 //===- llvm/IR/RemarkStreamer.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 remark outputting as part of
10 // LLVMContext.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/RemarkStreamer.h"
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalValue.h"
21 
22 using namespace llvm;
23 
25  StringRef Filename,
26  std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer)
27  : Filename(Filename), PassFilter(),
28  RemarkSerializer(std::move(RemarkSerializer)) {
29  assert(!Filename.empty() && "This needs to be a real filename.");
30 }
31 
33  Regex R = Regex(Filter);
34  std::string RegexError;
35  if (!R.isValid(RegexError))
36  return createStringError(std::make_error_code(std::errc::invalid_argument),
37  RegexError.data());
38  PassFilter = std::move(R);
39  return Error::success();
40 }
41 
42 /// DiagnosticKind -> remarks::Type
44  switch (Kind) {
45  default:
49  return remarks::Type::Passed;
52  return remarks::Type::Missed;
62  }
63 }
64 
65 /// DiagnosticLocation -> remarks::RemarkLocation.
68  if (!DL.isValid())
69  return None;
71  unsigned Line = DL.getLine();
72  unsigned Col = DL.getColumn();
73  return remarks::RemarkLocation{File, Line, Col};
74 }
75 
76 /// LLVM Diagnostic -> Remark
78 RemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) {
79  remarks::Remark R; // The result.
80  R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
81  R.PassName = Diag.getPassName();
82  R.RemarkName = Diag.getRemarkName();
83  R.FunctionName =
85  R.Loc = toRemarkLocation(Diag.getLocation());
86  R.Hotness = Diag.getHotness();
87 
89  R.Args.emplace_back();
90  R.Args.back().Key = Arg.Key;
91  R.Args.back().Val = Arg.Val;
92  R.Args.back().Loc = toRemarkLocation(Arg.Loc);
93  }
94 
95  return R;
96 }
97 
99  if (Optional<Regex> &Filter = PassFilter)
100  if (!Filter->match(Diag.getPassName()))
101  return;
102 
103  // First, convert the diagnostic to a remark.
104  remarks::Remark R = toRemark(Diag);
105  // Then, emit the remark through the serializer.
106  RemarkSerializer->emit(R);
107 }
108 
109 char RemarkSetupFileError::ID = 0;
112 
116  bool RemarksWithHotness,
117  unsigned RemarksHotnessThreshold) {
118  if (RemarksWithHotness)
119  Context.setDiagnosticsHotnessRequested(true);
120 
121  if (RemarksHotnessThreshold)
122  Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
123 
124  if (RemarksFilename.empty())
125  return nullptr;
126 
127  std::error_code EC;
128  auto RemarksFile =
129  std::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::OF_None);
130  // We don't use llvm::FileError here because some diagnostics want the file
131  // name separately.
132  if (EC)
133  return make_error<RemarkSetupFileError>(errorCodeToError(EC));
134 
136  if (Error E = Format.takeError())
137  return make_error<RemarkSetupFormatError>(std::move(E));
138 
141  if (Error E = RemarkSerializer.takeError())
142  return make_error<RemarkSetupFormatError>(std::move(E));
143 
144  Context.setRemarkStreamer(std::make_unique<RemarkStreamer>(
145  RemarksFilename, std::move(*RemarkSerializer)));
146 
147  if (!RemarksPasses.empty())
148  if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))
149  return make_error<RemarkSetupPatternError>(std::move(E));
150 
151  return std::move(RemarksFile);
152 }
Error setFilter(StringRef Filter)
Set a pass filter based on a regex Filter.
DiagnosticKind
Defines the different supported kind of a diagnostic.
void setRemarkStreamer(std::unique_ptr< RemarkStreamer > RemarkStreamer)
Set the diagnostics output used for optimization diagnostics.
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringRef PassName
Name of the pass that triggers the emission of this remark.
Definition: Remark.h:72
cl::opt< bool > RemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
RemarkStreamer * getRemarkStreamer()
Return the streamer used by the backend to save remark diagnostics.
unsigned getLine() const
Optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
Definition: Remark.h:87
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static remarks::Type toRemarkType(enum DiagnosticKind Kind)
DiagnosticKind -> remarks::Type.
void setDiagnosticsHotnessThreshold(uint64_t Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics...
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: BitVector.h:937
A remark type used for both emission and parsing.
Definition: Remark.h:67
std::error_code make_error_code(BitcodeError E)
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
Definition: Remark.h:77
Expected< Format > parseFormat(StringRef FormatStr)
Parse and validate a string for the remark format.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
Optional< RemarkLocation > Loc
The location in the source file of the remark.
Definition: Remark.h:83
Type
The type of the remark.
Definition: Remark.h:54
const Function & getFunction() const
RemarkStreamer(StringRef Filename, std::unique_ptr< remarks::RemarkSerializer > RemarkSerializer)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
The debug location used to track a remark back to the source file.
Definition: Remark.h:30
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void getLocation(StringRef &RelativePath, unsigned &Line, unsigned &Column) const
Return location information for this diagnostic in three parts: the relative source file path...
Used in the streaming interface as the general argument type.
static Optional< remarks::RemarkLocation > toRemarkLocation(const DiagnosticLocation &DL)
DiagnosticLocation -> remarks::RemarkLocation.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, or matching, if any...
Definition: Regex.cpp:55
StringRef getRelativePath() const
Return the file name relative to the compilation directory.
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character &#39;\1&#39;, drop it.
Definition: GlobalValue.h:481
Type RemarkType
The type of the remark.
Definition: Remark.h:69
StringRef FunctionName
Mangled name of the function that triggers the emssion of this remark.
Definition: Remark.h:80
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses, StringRef RemarksFormat, bool RemarksWithHotness, unsigned RemarksHotnessThreshold=0)
Setup optimization remarks.
ArrayRef< Argument > getArgs() const
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
Optional< uint64_t > getHotness() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 > RemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
SmallVector< Argument, 5 > Args
Arguments collected via the streaming interface.
Definition: Remark.h:90
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1163
void emit(const DiagnosticInfoOptimizationBase &Diag)
Emit a diagnostic through the streamer.
unsigned getColumn() const