LLVM  8.0.1
OptimizationRemarkEmitter.h
Go to the documentation of this file.
1 //===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- 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 // Optimization diagnostic interfaces. It's packaged as an analysis pass so
11 // that by using this service passes become dependent on BFI as well. BFI is
12 // used to compute the "hotness" of the diagnostic message.
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
16 #define LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
17 
18 #include "llvm/ADT/Optional.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 
25 namespace llvm {
26 class DebugLoc;
27 class Loop;
28 class Pass;
29 class Twine;
30 class Value;
31 
32 /// The optimization diagnostic interface.
33 ///
34 /// It allows reporting when optimizations are performed and when they are not
35 /// along with the reasons for it. Hotness information of the corresponding
36 /// code region can be included in the remark if DiagnosticsHotnessRequested is
37 /// enabled in the LLVM context.
39 public:
41  : F(F), BFI(BFI) {}
42 
43  /// This variant can be used to generate ORE on demand (without the
44  /// analysis pass).
45  ///
46  /// Note that this ctor has a very different cost depending on whether
47  /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
48  /// the operation is free.
49  ///
50  /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
51  /// operation since BFI and all its required analyses are computed. This is
52  /// for example useful for CGSCC passes that can't use function analyses
53  /// passes in the old PM.
55 
57  : F(Arg.F), BFI(Arg.BFI) {}
58 
60  F = RHS.F;
61  BFI = RHS.BFI;
62  return *this;
63  }
64 
65  /// Handle invalidation events in the new pass manager.
66  bool invalidate(Function &F, const PreservedAnalyses &PA,
68 
69  /// Output the remark via the diagnostic handler and to the
70  /// optimization record file.
71  void emit(DiagnosticInfoOptimizationBase &OptDiag);
72 
73  /// Take a lambda that returns a remark which will be emitted. Second
74  /// argument is only used to restrict this to functions.
75  template <typename T>
76  void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
77  // Avoid building the remark unless we know there are at least *some*
78  // remarks enabled. We can't currently check whether remarks are requested
79  // for the calling pass since that requires actually building the remark.
80 
83  auto R = RemarkBuilder();
85  }
86  }
87 
88  /// Whether we allow for extra compile-time budget to perform more
89  /// analysis to produce fewer false positives.
90  ///
91  /// This is useful when reporting missed optimizations. In this case we can
92  /// use the extra analysis (1) to filter trivial false positives or (2) to
93  /// provide more context so that non-trivial false positives can be quickly
94  /// detected by the user.
95  bool allowExtraAnalysis(StringRef PassName) const {
96  return (F->getContext().getDiagnosticsOutputFile() ||
98  }
99 
100 private:
101  const Function *F;
102 
103  BlockFrequencyInfo *BFI;
104 
105  /// If we generate BFI on demand, we need to free it when ORE is freed.
106  std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
107 
108  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
109  /// available.
110  Optional<uint64_t> computeHotness(const Value *V);
111 
112  /// Similar but use value from \p OptDiag and update hotness there.
113  void computeHotness(DiagnosticInfoIROptimization &OptDiag);
114 
115  /// Only allow verbose messages if we know we're filtering by hotness
116  /// (BFI is only set in this case).
117  bool shouldEmitVerbose() { return BFI != nullptr; }
118 
120  void operator=(const OptimizationRemarkEmitter &) = delete;
121 };
122 
123 /// Add a small namespace to avoid name clashes with the classes used in
124 /// the streaming interface. We want these to be short for better
125 /// write/readability.
126 namespace ore {
130 }
131 
132 /// OptimizationRemarkEmitter legacy analysis pass
133 ///
134 /// Note that this pass shouldn't generally be marked as preserved by other
135 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
136 /// could be freed.
138  std::unique_ptr<OptimizationRemarkEmitter> ORE;
139 
140 public:
142 
143  bool runOnFunction(Function &F) override;
144 
145  void getAnalysisUsage(AnalysisUsage &AU) const override;
146 
148  assert(ORE && "pass not run yet");
149  return *ORE;
150  }
151 
152  static char ID;
153 };
154 
156  : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
158  static AnalysisKey Key;
159 
160 public:
161  /// Provide the result typedef for this analysis pass.
163 
164  /// Run the analysis pass over a function and produce BFI.
165  Result run(Function &F, FunctionAnalysisManager &AM);
166 };
167 }
168 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Used to set IsVerbose via the stream interface.
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Key
PAL metadata keys.
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
DiagnosticInfoOptimizationBase::setIsVerbose setIsVerbose
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
Represent the analysis usage information of a pass.
Common features for diagnostics dealing with optimization remarks that are used by IR passes...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:193
Used in the streaming interface as the general argument type.
print lazy value Lazy Value Info Printer Pass
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file. ...
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
amdgpu Simplify well known AMD library false Value Value * Arg
bool isAnyRemarkEnabled(StringRef PassName) const
Return true if any type of remarks are enabled for this pass.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:642
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
OptimizationRemarkEmitter legacy analysis pass.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
The optimization diagnostic interface.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71