LLVM  9.0.0svn
DiagnosticInfo.cpp
Go to the documentation of this file.
1 //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- 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 defines the different classes involved in low level diagnostics.
10 //
11 // Diagnostics reporting is still done as part of the LLVMContext.
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/Regex.h"
39 #include <atomic>
40 #include <cassert>
41 #include <memory>
42 #include <string>
43 
44 using namespace llvm;
45 
47  static std::atomic<int> PluginKindID(DK_FirstPluginKind);
48  return ++PluginKindID;
49 }
50 
52 
54  const Twine &MsgStr,
55  DiagnosticSeverity Severity)
56  : DiagnosticInfo(DK_InlineAsm, Severity), MsgStr(MsgStr), Instr(&I) {
57  if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
58  if (SrcLoc->getNumOperands() != 0)
59  if (const auto *CI =
60  mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
61  LocCookie = CI->getZExtValue();
62  }
63 }
64 
66  DP << getMsgStr();
67  if (getLocCookie())
68  DP << " at line " << getLocCookie();
69 }
70 
72  DP << getResourceName() << " limit";
73 
74  if (getResourceLimit() != 0)
75  DP << " of " << getResourceLimit();
76 
77  DP << " exceeded (" << getResourceSize() << ") in " << getFunction();
78 }
79 
81  DP << "ignoring debug info with an invalid version (" << getMetadataVersion()
82  << ") in " << getModule();
83 }
84 
86  DiagnosticPrinter &DP) const {
87  DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier();
88 }
89 
91  if (!FileName.empty()) {
92  DP << getFileName();
93  if (LineNum > 0)
94  DP << ":" << getLineNum();
95  DP << ": ";
96  }
97  DP << getMsg();
98 }
99 
101  if (getFileName())
102  DP << getFileName() << ": ";
103  DP << getMsg();
104 }
105 
106 void DiagnosticInfo::anchor() {}
107 void DiagnosticInfoStackSize::anchor() {}
108 void DiagnosticInfoWithLocationBase::anchor() {}
109 void DiagnosticInfoIROptimization::anchor() {}
110 
112  if (!DL)
113  return;
114  File = DL->getFile();
115  Line = DL->getLine();
116  Column = DL->getColumn();
117 }
118 
120  if (!SP)
121  return;
122 
123  File = SP->getFile();
124  Line = SP->getScopeLine();
125  Column = 0;
126 }
127 
129  return File->getFilename();
130 }
131 
133  StringRef Name = File->getFilename();
134  if (sys::path::is_absolute(Name))
135  return Name;
136 
137  SmallString<128> Path;
138  sys::path::append(Path, File->getDirectory(), Name);
140 }
141 
143  return Loc.getAbsolutePath();
144 }
145 
147  unsigned &Line,
148  unsigned &Column) const {
149  RelativePath = Loc.getRelativePath();
150  Line = Loc.getLine();
151  Column = Loc.getColumn();
152 }
153 
155  StringRef Filename("<unknown>");
156  unsigned Line = 0;
157  unsigned Column = 0;
158  if (isLocationAvailable())
159  getLocation(Filename, Line, Column);
160  return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
161 }
162 
164  : Key(Key) {
165  if (auto *F = dyn_cast<Function>(V)) {
166  if (DISubprogram *SP = F->getSubprogram())
167  Loc = SP;
168  }
169  else if (auto *I = dyn_cast<Instruction>(V))
170  Loc = I->getDebugLoc();
171 
172  // Only include names that correspond to user variables. FIXME: We should use
173  // debug info if available to get the name of the user variable.
174  if (isa<llvm::Argument>(V) || isa<GlobalValue>(V))
176  else if (isa<Constant>(V)) {
178  V->printAsOperand(OS, /*PrintType=*/false);
179  } else if (auto *I = dyn_cast<Instruction>(V))
180  Val = I->getOpcodeName();
181 }
182 
184  : Key(Key) {
186  OS << *T;
187 }
188 
190  : Key(Key), Val(S.str()) {}
191 
193  : Key(Key), Val(itostr(N)) {}
194 
196  : Key(Key), Val(llvm::to_string(N)) {}
197 
199  : Key(Key), Val(itostr(N)) {}
200 
202  : Key(Key), Val(itostr(N)) {}
203 
205  : Key(Key), Val(utostr(N)) {}
206 
208  unsigned long N)
209  : Key(Key), Val(utostr(N)) {}
210 
212  unsigned long long N)
213  : Key(Key), Val(utostr(N)) {}
214 
216  : Key(Key), Loc(Loc) {
217  if (Loc) {
218  Val = (Loc->getFilename() + ":" + Twine(Loc.getLine()) + ":" +
219  Twine(Loc.getCol())).str();
220  } else {
221  Val = "<UNKNOWN LOCATION>";
222  }
223 }
224 
226  DP << getLocationStr() << ": " << getMsg();
227  if (Hotness)
228  DP << " (hotness: " << *Hotness << ")";
229 }
230 
233  const DiagnosticLocation &Loc,
234  const Value *CodeRegion)
236  DK_OptimizationRemark, DS_Remark, PassName, RemarkName,
237  *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
238 
241  const Instruction *Inst)
243  RemarkName, *Inst->getParent()->getParent(),
244  Inst->getDebugLoc(), Inst->getParent()) {}
245 
246 // Helper to allow for an assert before attempting to return an invalid
247 // reference.
248 static const BasicBlock &getFirstFunctionBlock(const Function *Func) {
249  assert(!Func->empty() && "Function does not have a body");
250  return Func->front();
251 }
252 
255  const Function *Func)
257  RemarkName, *Func, Func->getSubprogram(),
258  &getFirstFunctionBlock(Func)) {}
259 
261  const Function &Fn = getFunction();
262  LLVMContext &Ctx = Fn.getContext();
264 }
265 
267  const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
268  const Value *CodeRegion)
270  DK_OptimizationRemarkMissed, DS_Remark, PassName, RemarkName,
271  *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
272 
275  const Instruction *Inst)
277  PassName, RemarkName,
278  *Inst->getParent()->getParent(),
279  Inst->getDebugLoc(), Inst->getParent()) {}
280 
282  const Function &Fn = getFunction();
283  LLVMContext &Ctx = Fn.getContext();
285 }
286 
288  const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
289  const Value *CodeRegion)
291  DK_OptimizationRemarkAnalysis, DS_Remark, PassName, RemarkName,
292  *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
293 
296  const Instruction *Inst)
298  PassName, RemarkName,
299  *Inst->getParent()->getParent(),
300  Inst->getDebugLoc(), Inst->getParent()) {}
301 
303  enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName,
304  const DiagnosticLocation &Loc, const Value *CodeRegion)
305  : DiagnosticInfoIROptimization(Kind, DS_Remark, PassName, RemarkName,
306  *cast<BasicBlock>(CodeRegion)->getParent(),
307  Loc, CodeRegion) {}
308 
310  const Function &Fn = getFunction();
311  LLVMContext &Ctx = Fn.getContext();
314 }
315 
317  DP << Diagnostic;
318 }
319 
321  const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc,
322  const Value *CodeRegion)
324  DK_OptimizationFailure, DS_Warning, PassName, RemarkName,
325  *cast<BasicBlock>(CodeRegion)->getParent(), Loc, CodeRegion) {}
326 
328  // Only print warnings.
329  return getSeverity() == DS_Warning;
330 }
331 
333  std::string Str;
334  raw_string_ostream OS(Str);
335 
336  OS << getLocationStr() << ": in function " << getFunction().getName() << ' '
337  << *getFunction().getFunctionType() << ": " << Msg << '\n';
338  OS.flush();
339  DP << Str;
340 }
341 
343  DP << "Instruction selection used fallback path for " << getFunction();
344 }
345 
347  Args.emplace_back(S);
348 }
349 
351  Args.push_back(std::move(A));
352 }
353 
355  IsVerbose = true;
356 }
357 
359  FirstExtraArgIndex = Args.size();
360 }
361 
363  std::string Str;
364  raw_string_ostream OS(Str);
366  make_range(Args.begin(), FirstExtraArgIndex == -1
367  ? Args.end()
368  : Args.begin() + FirstExtraArgIndex))
369  OS << Arg.Val;
370  return OS.str();
371 }
372 
373 void OptimizationRemarkAnalysisFPCommute::anchor() {}
374 void OptimizationRemarkAnalysisAliasing::anchor() {}
const char * PassName
Name of the pass that triggers this report.
DiagnosticKind
Defines the different supported kind of a diagnostic.
bool empty() const
Definition: Function.h:678
std::string getAbsolutePath() const
Return the absolute path tot the file.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DiagnosticInfoInlineAsm(const Twine &MsgStr, DiagnosticSeverity Severity=DS_Error)
MsgStr is the message to be reported to the frontend.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
OptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const Value *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
DIFile * getFile() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void print(DiagnosticPrinter &DP) const override
This file contains the declarations for metadata subclasses.
Used to set IsVerbose via the stream interface.
unsigned getLine() const
Definition: DebugLoc.cpp:25
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:863
F(f)
int FirstExtraArgIndex
If positive, the index of the first argument that only appear in the optimization records and not in ...
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Definition: Path.cpp:678
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
const Twine & getMsgStr() const
DiagnosticLocation getLocation() const
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:663
Interface for custom diagnostic printing.
Subprogram description.
Key
PAL metadata keys.
std::string itostr(int64_t X)
Definition: StringExtras.h:238
void print(DiagnosticPrinter &DP) const override
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:234
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:258
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:249
SmallVector< Argument, 4 > Args
Arguments collected via the streaming interface.
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
This is the base abstract class for diagnostic reporting in the backend.
const Function & getFunction() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Instrumentation for Order File
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Common features for diagnostics dealing with optimization remarks that are used by IR passes...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static const BasicBlock & getFirstFunctionBlock(const Function *Func)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
Used in the streaming interface as the general argument type.
virtual bool isMissedOptRemarkEnabled(StringRef PassName) const
Return true if missed optimization remarks are enabled, override to provide different implementation...
static DISubprogram * getSubprogram(bool IsDistinct, Ts &&... Args)
Definition: DIBuilder.cpp:745
OptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const Value *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
bool isEnabled() const override
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4306
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:498
bool IsVerbose
The remark is expected to be noisy.
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool isEnabled() const override
OptimizationRemarkMissed(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const Value *CodeRegion)
PassName is the name of the pass emitting this diagnostic.
virtual bool isPassedOptRemarkEnabled(StringRef PassName) const
Return true if passed optimization remarks are enabled, override to provide different implementation...
Module.h This file contains the declarations for the Module class.
StringRef getRelativePath() const
Return the file name relative to the compilation directory.
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:223
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
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
int getNextAvailablePluginDiagnosticKind()
Get the next available kind ID for a plugin diagnostic.
void print(DiagnosticPrinter &DP) const override
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
virtual bool isAnalysisRemarkEnabled(StringRef PassName) const
Return true if analysis remarks are enabled, override to provide different implementation.
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
void print(DiagnosticPrinter &DP) const override
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
DiagnosticSeverity getSeverity() const
void print(DiagnosticPrinter &DP) const override
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
unsigned getCol() const
Definition: DebugLoc.cpp:30
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
static MemoryLocation getLocation(Instruction *I, AliasAnalysis *AA)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BasicBlock & front() const
Definition: Function.h:679
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
LLVM Value Representation.
Definition: Value.h:72
Optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
static const Function * getParent(const Value *V)
void print(DiagnosticPrinter &DP) const override
const std::string getLocationStr() const
Return a string with the location information for this diagnostic in the format "file:line:col".
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void print(DiagnosticPrinter &DP) const override
void print(DiagnosticPrinter &DP) const override
Print using the given DP a user-friendly message.
std::string getAbsolutePath() const
Return the full path to the file.
DiagnosticInfoOptimizationFailure(const Function &Fn, const DiagnosticLocation &Loc, const Twine &Msg)
Fn is the function where the diagnostic is being emitted.