llvm.org GIT mirror llvm / ccbf1d2
Add support for optimization reports. Summary: This patch adds backend support for -Rpass=, which indicates the name of the optimization pass that should emit remarks stating when it made a transformation to the code. Pass names are taken from their DEBUG_NAME definitions. When emitting an optimization report diagnostic, the lack of debug information causes the diagnostic to use "<unknown>:0:0" as the location string. This is the back end counterpart for http://llvm-reviews.chandlerc.com/D3226 Reviewers: qcolombet CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D3227 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205774 91177308-0d34-0410-b5e6-96231b3b80d8 Diego Novillo 6 years ago
5 changed file(s) with 113 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
2525 class Instruction;
2626 class Twine;
2727 class Value;
28 class DebugLoc;
2829
2930 /// \brief Defines the different supported severity of a diagnostic.
3031 enum DiagnosticSeverity {
4344 DK_StackSize,
4445 DK_DebugMetadataVersion,
4546 DK_SampleProfile,
47 DK_OptimizationRemark,
4648 DK_FirstPluginKind
4749 };
4850
234236 const Twine &Msg;
235237 };
236238
239 /// Diagnostic information for optimization remarks.
240 class DiagnosticInfoOptimizationRemark : public DiagnosticInfo {
241 public:
242 /// \p PassName is the name of the pass emitting this diagnostic. If
243 /// this name matches the regular expression given in -Rpass=, then the
244 /// diagnostic will be emitted. \p Fn is the function where the diagnostic
245 /// is being emitted. \p DLoc is the location information to use in the
246 /// diagnostic. If line table information is available, the diagnostic
247 /// will include the source code location. \p Msg is the message to show.
248 /// Note that this class does not copy this message, so this reference
249 /// must be valid for the whole life time of the diagnostic.
250 DiagnosticInfoOptimizationRemark(const char *PassName, const Function &Fn,
251 const DebugLoc &DLoc, const Twine &Msg)
252 : DiagnosticInfo(DK_OptimizationRemark, DS_Remark), PassName(PassName),
253 Fn(Fn), DLoc(DLoc), Msg(Msg) {}
254
255 /// \see DiagnosticInfo::print.
256 void print(DiagnosticPrinter &DP) const override;
257
258 /// Hand rolled RTTI.
259 static bool classof(const DiagnosticInfo *DI) {
260 return DI->getKind() == DK_OptimizationRemark;
261 }
262
263 /// Return true if location information is available for this diagnostic.
264 bool isLocationAvailable() const;
265
266 /// Return a string with the location information for this diagnostic
267 /// in the format "file:line:col". If location information is not available,
268 /// it returns ":0:0".
269 const StringRef getLocationStr() const;
270
271 /// Return location information for this diagnostic in three parts:
272 /// the source file name, line number and column.
273 void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
274
275 const char *getPassName() const { return PassName; }
276 const Function &getFunction() const { return Fn; }
277 const DebugLoc &getDebugLoc() const { return DLoc; }
278 const Twine &getMsg() const { return Msg; }
279
280 private:
281 /// Name of the pass that triggers this report. If this matches the
282 /// regular expression given in -Rpass=regexp, then the remark will
283 /// be emitted.
284 const char *PassName;
285
286 /// Function where this diagnostic is triggered.
287 const Function &Fn;
288
289 /// Debug location where this diagnostic is triggered.
290 const DebugLoc &DLoc;
291
292 /// Message to report.
293 const Twine &Msg;
294 };
295
237296 } // End namespace llvm
238297
239298 #endif
2828 class SMDiagnostic;
2929 class DiagnosticInfo;
3030 template class SmallVectorImpl;
31 class Function;
32 class DebugLoc;
3133
3234 /// This is an important class for using LLVM in a threaded context. It
3335 /// (opaquely) owns and manages the core "global" data of LLVM's core
124126 void emitError(const Instruction *I, const Twine &ErrorStr);
125127 void emitError(const Twine &ErrorStr);
126128
129 /// emitOptimizationRemark - Emit an optimization remark message. \p PassName
130 /// is the name of the pass emitting the message. If -Rpass= is given
131 /// and \p PassName matches the regular expression in -Rpass, then the
132 /// remark will be emitted. \p Fn is the function triggering the remark,
133 /// \p DLoc is the debug location where the diagnostic is generated.
134 /// \p Msg is the message string to use.
135 void emitOptimizationRemark(const char *PassName, const Function &Fn,
136 const DebugLoc &DLoc, const Twine &Msg);
137
127138 private:
128139 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
129140 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
1313
1414 #include "llvm/ADT/Twine.h"
1515 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DebugInfo.h"
1617 #include "llvm/IR/DiagnosticInfo.h"
1718 #include "llvm/IR/DiagnosticPrinter.h"
1819 #include "llvm/IR/Function.h"
1920 #include "llvm/IR/Instruction.h"
2021 #include "llvm/IR/Metadata.h"
22 #include "llvm/IR/Module.h"
2123 #include "llvm/Support/Atomic.h"
2224 #include
2325
6365 DP << getFileName() << ": ";
6466 DP << getMsg();
6567 }
68
69 bool DiagnosticInfoOptimizationRemark::isLocationAvailable() const {
70 return getFunction().getParent()->getNamedMetadata("llvm.dbg.cu") != 0;
71 }
72
73 void DiagnosticInfoOptimizationRemark::getLocation(StringRef *Filename,
74 unsigned *Line,
75 unsigned *Column) const {
76 DILocation DIL(getDebugLoc().getAsMDNode(getFunction().getContext()));
77 *Filename = DIL.getFilename();
78 *Line = DIL.getLineNumber();
79 *Column = DIL.getColumnNumber();
80 }
81
82 const StringRef DiagnosticInfoOptimizationRemark::getLocationStr() const {
83 StringRef Filename("");
84 unsigned Line = 0;
85 unsigned Column = 0;
86 if (isLocationAvailable())
87 getLocation(&Filename, &Line, &Column);
88 return Twine(Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
89 }
90
91 void DiagnosticInfoOptimizationRemark::print(DiagnosticPrinter &DP) const {
92 DP << getLocationStr() << ": " << getMsg();
93 }
1414 #include "llvm/IR/LLVMContext.h"
1515 #include "LLVMContextImpl.h"
1616 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugLoc.h"
1718 #include "llvm/IR/DiagnosticInfo.h"
1819 #include "llvm/IR/DiagnosticPrinter.h"
1920 #include "llvm/IR/Instruction.h"
154155 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
155156 }
156157
158 void LLVMContext::emitOptimizationRemark(const char *PassName,
159 const Function &Fn,
160 const DebugLoc &DLoc,
161 const Twine &Msg) {
162 diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
163 }
164
157165 //===----------------------------------------------------------------------===//
158166 // Metadata Kind Uniquing
159167 //===----------------------------------------------------------------------===//
2020 #include "llvm/Analysis/InlineCost.h"
2121 #include "llvm/IR/CallSite.h"
2222 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DiagnosticInfo.h"
2324 #include "llvm/IR/Instructions.h"
2425 #include "llvm/IR/IntrinsicInst.h"
2526 #include "llvm/IR/Module.h"
521522 InlineHistoryID, InsertLifetime, DL))
522523 continue;
523524 ++NumInlined;
524
525
526 // Report the inline decision.
527 Caller->getContext().emitOptimizationRemark(
528 DEBUG_TYPE, *Caller, CS.getInstruction()->getDebugLoc(),
529 Twine(Callee->getName() + " inlined into " + Caller->getName()));
530
525531 // If inlining this function gave us any new call sites, throw them
526532 // onto our worklist to process. They are useful inline candidates.
527533 if (!InlineInfo.InlinedCalls.empty()) {