Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 1 | //===- llvm/Support/DiagnosticInfo.cpp - Diagnostic Definitions -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the different classes involved in low level diagnostics. |
| 11 | // |
| 12 | // Diagnostics reporting is still done as part of the LLVMContext. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/IR/Constants.h" |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DebugInfo.h" |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
| 20 | #include "llvm/IR/DiagnosticPrinter.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/Instruction.h" |
| 23 | #include "llvm/IR/Metadata.h" |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Regex.h" |
Benjamin Kramer | 2d22140 | 2015-06-11 17:30:34 +0000 | [diff] [blame] | 27 | #include <atomic> |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 28 | #include <string> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | /// \brief Regular expression corresponding to the value given in one of the |
| 35 | /// -pass-remarks* command line flags. Passes whose name matches this regexp |
| 36 | /// will emit a diagnostic when calling the associated diagnostic function |
| 37 | /// (emitOptimizationRemark, emitOptimizationRemarkMissed or |
| 38 | /// emitOptimizationRemarkAnalysis). |
| 39 | struct PassRemarksOpt { |
| 40 | std::shared_ptr<Regex> Pattern; |
| 41 | |
| 42 | void operator=(const std::string &Val) { |
| 43 | // Create a regexp object to match pass names for emitOptimizationRemark. |
| 44 | if (!Val.empty()) { |
| 45 | Pattern = std::make_shared<Regex>(Val); |
| 46 | std::string RegexError; |
| 47 | if (!Pattern->isValid(RegexError)) |
| 48 | report_fatal_error("Invalid regular expression '" + Val + |
| 49 | "' in -pass-remarks: " + RegexError, |
| 50 | false); |
| 51 | } |
Hans Wennborg | 13958b7 | 2015-07-22 20:46:11 +0000 | [diff] [blame] | 52 | } |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static PassRemarksOpt PassRemarksOptLoc; |
| 56 | static PassRemarksOpt PassRemarksMissedOptLoc; |
| 57 | static PassRemarksOpt PassRemarksAnalysisOptLoc; |
| 58 | |
| 59 | // -pass-remarks |
| 60 | // Command line flag to enable emitOptimizationRemark() |
| 61 | static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> |
| 62 | PassRemarks("pass-remarks", cl::value_desc("pattern"), |
| 63 | cl::desc("Enable optimization remarks from passes whose name match " |
| 64 | "the given regular expression"), |
| 65 | cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired, |
| 66 | cl::ZeroOrMore); |
| 67 | |
| 68 | // -pass-remarks-missed |
| 69 | // Command line flag to enable emitOptimizationRemarkMissed() |
| 70 | static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> PassRemarksMissed( |
| 71 | "pass-remarks-missed", cl::value_desc("pattern"), |
| 72 | cl::desc("Enable missed optimization remarks from passes whose name match " |
| 73 | "the given regular expression"), |
| 74 | cl::Hidden, cl::location(PassRemarksMissedOptLoc), cl::ValueRequired, |
| 75 | cl::ZeroOrMore); |
| 76 | |
| 77 | // -pass-remarks-analysis |
| 78 | // Command line flag to enable emitOptimizationRemarkAnalysis() |
| 79 | static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> |
| 80 | PassRemarksAnalysis( |
| 81 | "pass-remarks-analysis", cl::value_desc("pattern"), |
| 82 | cl::desc( |
| 83 | "Enable optimization analysis remarks from passes whose name match " |
| 84 | "the given regular expression"), |
| 85 | cl::Hidden, cl::location(PassRemarksAnalysisOptLoc), cl::ValueRequired, |
| 86 | cl::ZeroOrMore); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 87 | } |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 88 | |
Tobias Grosser | 84db1e7 | 2013-12-18 10:12:06 +0000 | [diff] [blame] | 89 | int llvm::getNextAvailablePluginDiagnosticKind() { |
Benjamin Kramer | 2d22140 | 2015-06-11 17:30:34 +0000 | [diff] [blame] | 90 | static std::atomic<int> PluginKindID(DK_FirstPluginKind); |
| 91 | return ++PluginKindID; |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Adam Nemet | ad437ff | 2016-06-29 04:55:19 +0000 | [diff] [blame] | 94 | const char *DiagnosticInfoOptimizationRemarkAnalysis::AlwaysPrint = ""; |
Tyler Nowicki | c94d6ad | 2015-08-11 01:09:15 +0000 | [diff] [blame] | 95 | |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 96 | DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I, |
| 97 | const Twine &MsgStr, |
| 98 | DiagnosticSeverity Severity) |
| 99 | : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr), |
| 100 | Instr(&I) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 101 | if (const MDNode *SrcLoc = I.getMetadata("srcloc")) { |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 102 | if (SrcLoc->getNumOperands() != 0) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 103 | if (const auto *CI = |
| 104 | mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0))) |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 105 | LocCookie = CI->getZExtValue(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void DiagnosticInfoInlineAsm::print(DiagnosticPrinter &DP) const { |
| 110 | DP << getMsgStr(); |
| 111 | if (getLocCookie()) |
| 112 | DP << " at line " << getLocCookie(); |
| 113 | } |
| 114 | |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 115 | void DiagnosticInfoResourceLimit::print(DiagnosticPrinter &DP) const { |
| 116 | DP << getResourceName() << " limit"; |
| 117 | |
| 118 | if (getResourceLimit() != 0) |
| 119 | DP << " of " << getResourceLimit(); |
| 120 | |
| 121 | DP << " exceeded (" << getResourceSize() << ") in " << getFunction(); |
Quentin Colombet | b4c44d2 | 2013-12-17 17:47:22 +0000 | [diff] [blame] | 122 | } |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 123 | |
| 124 | void DiagnosticInfoDebugMetadataVersion::print(DiagnosticPrinter &DP) const { |
Manman Ren | 3762808 | 2014-02-04 23:49:02 +0000 | [diff] [blame] | 125 | DP << "ignoring debug info with an invalid version (" << getMetadataVersion() |
| 126 | << ") in " << getModule(); |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 127 | } |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 128 | |
Adrian Prantl | e365618 | 2016-05-09 19:57:29 +0000 | [diff] [blame] | 129 | void DiagnosticInfoIgnoringInvalidDebugMetadata::print( |
| 130 | DiagnosticPrinter &DP) const { |
| 131 | DP << "ignoring invalid debug info in " << getModule().getModuleIdentifier(); |
| 132 | } |
| 133 | |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 134 | void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const { |
David Blaikie | 2297a91 | 2015-11-02 20:01:13 +0000 | [diff] [blame] | 135 | if (!FileName.empty()) { |
| 136 | DP << getFileName(); |
| 137 | if (LineNum > 0) |
| 138 | DP << ":" << getLineNum(); |
| 139 | DP << ": "; |
| 140 | } |
Diego Novillo | a32aa32 | 2014-03-14 21:58:59 +0000 | [diff] [blame] | 141 | DP << getMsg(); |
| 142 | } |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 143 | |
Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 144 | void DiagnosticInfoPGOProfile::print(DiagnosticPrinter &DP) const { |
| 145 | if (getFileName()) |
| 146 | DP << getFileName() << ": "; |
| 147 | DP << getMsg(); |
| 148 | } |
| 149 | |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 150 | bool DiagnosticInfoWithDebugLocBase::isLocationAvailable() const { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 151 | return getDebugLoc(); |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 154 | void DiagnosticInfoWithDebugLocBase::getLocation(StringRef *Filename, |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 155 | unsigned *Line, |
| 156 | unsigned *Column) const { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 157 | DILocation *L = getDebugLoc(); |
Davide Italiano | e197d4f | 2015-05-04 18:08:35 +0000 | [diff] [blame] | 158 | assert(L != nullptr && "debug location is invalid"); |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 159 | *Filename = L->getFilename(); |
| 160 | *Line = L->getLine(); |
| 161 | *Column = L->getColumn(); |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 164 | const std::string DiagnosticInfoWithDebugLocBase::getLocationStr() const { |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 165 | StringRef Filename("<unknown>"); |
| 166 | unsigned Line = 0; |
| 167 | unsigned Column = 0; |
| 168 | if (isLocationAvailable()) |
| 169 | getLocation(&Filename, &Line, &Column); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 170 | return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str(); |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 173 | void DiagnosticInfoOptimizationBase::print(DiagnosticPrinter &DP) const { |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 174 | DP << getLocationStr() << ": " << getMsg(); |
| 175 | } |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 176 | |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 177 | bool DiagnosticInfoOptimizationRemark::isEnabled() const { |
| 178 | return PassRemarksOptLoc.Pattern && |
| 179 | PassRemarksOptLoc.Pattern->match(getPassName()); |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 182 | bool DiagnosticInfoOptimizationRemarkMissed::isEnabled() const { |
| 183 | return PassRemarksMissedOptLoc.Pattern && |
| 184 | PassRemarksMissedOptLoc.Pattern->match(getPassName()); |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Diego Novillo | 0b761a4 | 2014-05-22 17:19:01 +0000 | [diff] [blame] | 187 | bool DiagnosticInfoOptimizationRemarkAnalysis::isEnabled() const { |
Adam Nemet | ad437ff | 2016-06-29 04:55:19 +0000 | [diff] [blame] | 188 | return shouldAlwaysPrint() || |
Tyler Nowicki | c94d6ad | 2015-08-11 01:09:15 +0000 | [diff] [blame] | 189 | (PassRemarksAnalysisOptLoc.Pattern && |
| 190 | PassRemarksAnalysisOptLoc.Pattern->match(getPassName())); |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Alex Lorenz | 735c47e | 2015-06-15 20:30:22 +0000 | [diff] [blame] | 193 | void DiagnosticInfoMIRParser::print(DiagnosticPrinter &DP) const { |
| 194 | DP << Diagnostic; |
| 195 | } |
| 196 | |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 197 | void llvm::emitOptimizationRemark(LLVMContext &Ctx, const char *PassName, |
| 198 | const Function &Fn, const DebugLoc &DLoc, |
| 199 | const Twine &Msg) { |
| 200 | Ctx.diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg)); |
| 201 | } |
| 202 | |
| 203 | void llvm::emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName, |
| 204 | const Function &Fn, |
| 205 | const DebugLoc &DLoc, |
| 206 | const Twine &Msg) { |
| 207 | Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, Fn, DLoc, Msg)); |
| 208 | } |
| 209 | |
| 210 | void llvm::emitOptimizationRemarkAnalysis(LLVMContext &Ctx, |
| 211 | const char *PassName, |
| 212 | const Function &Fn, |
| 213 | const DebugLoc &DLoc, |
| 214 | const Twine &Msg) { |
| 215 | Ctx.diagnose( |
| 216 | DiagnosticInfoOptimizationRemarkAnalysis(PassName, Fn, DLoc, Msg)); |
| 217 | } |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 218 | |
Tyler Nowicki | c1a86f5 | 2015-08-10 19:51:46 +0000 | [diff] [blame] | 219 | void llvm::emitOptimizationRemarkAnalysisFPCommute(LLVMContext &Ctx, |
| 220 | const char *PassName, |
| 221 | const Function &Fn, |
| 222 | const DebugLoc &DLoc, |
| 223 | const Twine &Msg) { |
| 224 | Ctx.diagnose(DiagnosticInfoOptimizationRemarkAnalysisFPCommute(PassName, Fn, |
| 225 | DLoc, Msg)); |
| 226 | } |
| 227 | |
Tyler Nowicki | 652b0da | 2015-08-10 23:01:55 +0000 | [diff] [blame] | 228 | void llvm::emitOptimizationRemarkAnalysisAliasing(LLVMContext &Ctx, |
| 229 | const char *PassName, |
| 230 | const Function &Fn, |
| 231 | const DebugLoc &DLoc, |
| 232 | const Twine &Msg) { |
| 233 | Ctx.diagnose(DiagnosticInfoOptimizationRemarkAnalysisAliasing(PassName, Fn, |
| 234 | DLoc, Msg)); |
| 235 | } |
| 236 | |
Tyler Nowicki | 55454c6 | 2014-07-18 19:36:04 +0000 | [diff] [blame] | 237 | bool DiagnosticInfoOptimizationFailure::isEnabled() const { |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 238 | // Only print warnings. |
| 239 | return getSeverity() == DS_Warning; |
| 240 | } |
| 241 | |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 242 | void DiagnosticInfoUnsupported::print(DiagnosticPrinter &DP) const { |
| 243 | std::string Str; |
| 244 | raw_string_ostream OS(Str); |
| 245 | |
| 246 | OS << getLocationStr() << ": in function " << getFunction().getName() << ' ' |
| 247 | << *getFunction().getFunctionType() << ": " << Msg << '\n'; |
| 248 | OS.flush(); |
| 249 | DP << Str; |
| 250 | } |
| 251 | |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 252 | void llvm::emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn, |
| 253 | const DebugLoc &DLoc, const Twine &Msg) { |
Tyler Nowicki | 55454c6 | 2014-07-18 19:36:04 +0000 | [diff] [blame] | 254 | Ctx.diagnose(DiagnosticInfoOptimizationFailure( |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 255 | Fn, DLoc, Twine("loop not vectorized: " + Msg))); |
| 256 | } |
| 257 | |
| 258 | void llvm::emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn, |
| 259 | const DebugLoc &DLoc, const Twine &Msg) { |
Tyler Nowicki | 55454c6 | 2014-07-18 19:36:04 +0000 | [diff] [blame] | 260 | Ctx.diagnose(DiagnosticInfoOptimizationFailure( |
Tyler Nowicki | 641d8a0 | 2014-07-16 00:36:00 +0000 | [diff] [blame] | 261 | Fn, DLoc, Twine("loop not interleaved: " + Msg))); |
| 262 | } |