xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/BottleneckAnalysis.h (revision ee0fe82ee2892f5ece189db0eab38913aaab5f0f)
1 //===--------------------- BottleneckAnalysis.h -----------------*- 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 /// \file
9 ///
10 /// This file implements the bottleneck analysis view.
11 ///
12 /// This view internally observes backend pressure increase events in order to
13 /// identify problematic data dependencies and processor resource interferences.
14 ///
15 /// Example of bottleneck analysis report for a dot-product on X86 btver2:
16 ///
17 /// Cycles with backend pressure increase [ 40.76% ]
18 /// Throughput Bottlenecks:
19 ///   Resource Pressure       [ 39.34% ]
20 ///   - JFPA  [ 39.34% ]
21 ///   - JFPU0  [ 39.34% ]
22 ///   Data Dependencies:      [ 1.42% ]
23 ///   - Register Dependencies [ 1.42% ]
24 ///   - Memory Dependencies   [ 0.00% ]
25 ///
26 /// According to the example, backend pressure increased during the 40.76% of
27 /// the simulated cycles.  In particular, the major cause of backend pressure
28 /// increases was the contention on floating point adder JFPA accessible from
29 /// pipeline resource JFPU0.
30 ///
31 /// At the end of each cycle, if pressure on the simulated out-of-order buffers
32 /// has increased, a backend pressure event is reported.
33 /// In particular, this occurs when there is a delta between the number of uOps
34 /// dispatched and the number of uOps issued to the underlying pipelines.
35 ///
36 /// The bottleneck analysis view is also responsible for identifying and printing
37 /// the most "critical" sequence of dependent instructions according to the
38 /// simulated run.
39 ///
40 /// Below is the critical sequence computed for the dot-product example on
41 /// btver2:
42 ///
43 ///              Instruction                     Dependency Information
44 /// +----< 2.    vhaddps %xmm3, %xmm3, %xmm4
45 /// |
46 /// |    < loop carried >
47 /// |
48 /// |      0.    vmulps	 %xmm0, %xmm0, %xmm2
49 /// +----> 1.    vhaddps %xmm2, %xmm2, %xmm3     ## RESOURCE interference:  JFPA [ probability: 73% ]
50 /// +----> 2.    vhaddps %xmm3, %xmm3, %xmm4     ## REGISTER dependency:  %xmm3
51 /// |
52 /// |    < loop carried >
53 /// |
54 /// +----> 1.    vhaddps %xmm2, %xmm2, %xmm3     ## RESOURCE interference:  JFPA [ probability: 73% ]
55 ///
56 ///
57 /// The algorithm that computes the critical sequence is very similar to a
58 /// critical path analysis.
59 ///
60 /// A dependency graph is used internally to track dependencies between nodes.
61 /// Nodes of the graph represent instructions from the input assembly sequence,
62 /// and edges of the graph represent data dependencies or processor resource
63 /// interferences.
64 ///
65 /// Edges are dynamically 'discovered' by observing instruction state transitions
66 /// and backend pressure increase events. Edges are internally ranked based on
67 /// their "criticality". A dependency is considered to be critical if it takes a
68 /// long time to execute, and if it contributes to backend pressure increases.
69 /// Criticality is internally measured in terms of cycles; it is computed for
70 /// every edge in the graph as a function of the edge latency and the number of
71 /// backend pressure increase cycles contributed by that edge.
72 ///
73 /// At the end of simulation, costs are propagated to nodes through the edges of
74 /// the graph, and the most expensive path connecting the root-set (a
75 /// set of nodes with no predecessors) to a leaf node is reported as critical
76 /// sequence.
77 //
78 //===----------------------------------------------------------------------===//
79 
80 #ifndef LLVM_TOOLS_LLVM_MCA_BOTTLENECK_ANALYSIS_H
81 #define LLVM_TOOLS_LLVM_MCA_BOTTLENECK_ANALYSIS_H
82 
83 #include "Views/View.h"
84 #include "llvm/ADT/DenseMap.h"
85 #include "llvm/ADT/SmallVector.h"
86 #include "llvm/MC/MCInstPrinter.h"
87 #include "llvm/MC/MCSchedule.h"
88 #include "llvm/MC/MCSubtargetInfo.h"
89 #include "llvm/Support/raw_ostream.h"
90 
91 namespace llvm {
92 namespace mca {
93 
94 class PressureTracker {
95   const MCSchedModel &SM;
96 
97   // Resource pressure distribution. There is an element for every processor
98   // resource declared by the scheduling model. Quantities are number of cycles.
99   SmallVector<unsigned, 4> ResourcePressureDistribution;
100 
101   // Each processor resource is associated with a so-called processor resource
102   // mask. This vector allows to correlate processor resource IDs with processor
103   // resource masks. There is exactly one element per each processor resource
104   // declared by the scheduling model.
105   SmallVector<uint64_t, 4> ProcResID2Mask;
106 
107   // Maps processor resource state indices (returned by calls to
108   // `getResourceStateIndex(Mask)` to processor resource identifiers.
109   SmallVector<unsigned, 4> ResIdx2ProcResID;
110 
111   // Maps Processor Resource identifiers to ResourceUsers indices.
112   SmallVector<unsigned, 4> ProcResID2ResourceUsersIndex;
113 
114   // Identifies the last user of a processor resource unit.
115   // This vector is updated on every instruction issued event.
116   // There is one entry for every processor resource unit declared by the
117   // processor model. An all_ones value is treated like an invalid instruction
118   // identifier.
119   using User = std::pair<unsigned, unsigned>;
120   SmallVector<User, 4> ResourceUsers;
121 
122   struct InstructionPressureInfo {
123     unsigned RegisterPressureCycles;
124     unsigned MemoryPressureCycles;
125     unsigned ResourcePressureCycles;
126   };
127   DenseMap<unsigned, InstructionPressureInfo> IPI;
128 
129   void updateResourcePressureDistribution(uint64_t CumulativeMask);
130 
131   User getResourceUser(unsigned ProcResID, unsigned UnitID) const {
132     unsigned Index = ProcResID2ResourceUsersIndex[ProcResID];
133     return ResourceUsers[Index + UnitID];
134   }
135 
136 public:
137   PressureTracker(const MCSchedModel &Model);
138 
139   ArrayRef<unsigned> getResourcePressureDistribution() const {
140     return ResourcePressureDistribution;
141   }
142 
143   void getResourceUsers(uint64_t ResourceMask,
144                         SmallVectorImpl<User> &Users) const;
145 
146   unsigned getRegisterPressureCycles(unsigned IID) const {
147     assert(IPI.find(IID) != IPI.end() && "Instruction is not tracked!");
148     const InstructionPressureInfo &Info = IPI.find(IID)->second;
149     return Info.RegisterPressureCycles;
150   }
151 
152   unsigned getMemoryPressureCycles(unsigned IID) const {
153     assert(IPI.find(IID) != IPI.end() && "Instruction is not tracked!");
154     const InstructionPressureInfo &Info = IPI.find(IID)->second;
155     return Info.MemoryPressureCycles;
156   }
157 
158   unsigned getResourcePressureCycles(unsigned IID) const {
159     assert(IPI.find(IID) != IPI.end() && "Instruction is not tracked!");
160     const InstructionPressureInfo &Info = IPI.find(IID)->second;
161     return Info.ResourcePressureCycles;
162   }
163 
164   const char *resolveResourceName(uint64_t ResourceMask) const {
165     unsigned Index = getResourceStateIndex(ResourceMask);
166     unsigned ProcResID = ResIdx2ProcResID[Index];
167     const MCProcResourceDesc &PRDesc = *SM.getProcResource(ProcResID);
168     return PRDesc.Name;
169   }
170 
171   void onInstructionDispatched(unsigned IID);
172   void onInstructionExecuted(unsigned IID);
173 
174   void handlePressureEvent(const HWPressureEvent &Event);
175   void handleInstructionIssuedEvent(const HWInstructionIssuedEvent &Event);
176 };
177 
178 // A dependency edge.
179 struct DependencyEdge {
180   enum DependencyType { DT_INVALID, DT_REGISTER, DT_MEMORY, DT_RESOURCE };
181 
182   // Dependency edge descriptor.
183   //
184   // It specifies the dependency type, as well as the edge cost in cycles.
185   struct Dependency {
186     DependencyType Type;
187     uint64_t ResourceOrRegID;
188     uint64_t Cost;
189   };
190   Dependency Dep;
191 
192   unsigned FromIID;
193   unsigned ToIID;
194 
195   // Used by the bottleneck analysis to compute the interference
196   // probability for processor resources.
197   unsigned Frequency;
198 };
199 
200 // A dependency graph used by the bottleneck analysis to describe data
201 // dependencies and processor resource interferences between instructions.
202 //
203 // There is a node (an instance of struct DGNode) for every instruction in the
204 // input assembly sequence. Edges of the graph represent dependencies between
205 // instructions.
206 //
207 // Each edge of the graph is associated with a cost value which is used
208 // internally to rank dependency based on their impact on the runtime
209 // performance (see field DependencyEdge::Dependency::Cost). In general, the
210 // higher the cost of an edge, the higher the impact on performance.
211 //
212 // The cost of a dependency is a function of both the latency and the number of
213 // cycles where the dependency has been seen as critical (i.e. contributing to
214 // back-pressure increases).
215 //
216 // Loop carried dependencies are carefully expanded by the bottleneck analysis
217 // to guarantee that the graph stays acyclic. To this end, extra nodes are
218 // pre-allocated at construction time to describe instructions from "past and
219 // future" iterations. The graph is kept acyclic mainly because it simplifies the
220 // complexity of the algorithm that computes the critical sequence.
221 class DependencyGraph {
222   struct DGNode {
223     unsigned NumPredecessors;
224     unsigned NumVisitedPredecessors;
225     uint64_t Cost;
226     unsigned Depth;
227 
228     DependencyEdge CriticalPredecessor;
229     SmallVector<DependencyEdge, 8> OutgoingEdges;
230   };
231   SmallVector<DGNode, 16> Nodes;
232 
233   DependencyGraph(const DependencyGraph &) = delete;
234   DependencyGraph &operator=(const DependencyGraph &) = delete;
235 
236   void addDependency(unsigned From, unsigned To,
237                      DependencyEdge::Dependency &&DE);
238 
239   void initializeRootSet(SmallVectorImpl<unsigned> &RootSet) const;
240   void propagateThroughEdges(SmallVectorImpl<unsigned> &RootSet);
241 
242 #ifndef NDEBUG
243   void dumpDependencyEdge(raw_ostream &OS, const DependencyEdge &DE,
244                           MCInstPrinter &MCIP) const;
245 #endif
246 
247 public:
248   DependencyGraph(unsigned Size) : Nodes(Size) {}
249 
250   void addRegisterDep(unsigned From, unsigned To, unsigned RegID,
251                       unsigned Cost) {
252     addDependency(From, To, {DependencyEdge::DT_REGISTER, RegID, Cost});
253   }
254 
255   void addMemoryDep(unsigned From, unsigned To, unsigned Cost) {
256     addDependency(From, To, {DependencyEdge::DT_MEMORY, /* unused */ 0, Cost});
257   }
258 
259   void addResourceDep(unsigned From, unsigned To, uint64_t Mask,
260                       unsigned Cost) {
261     addDependency(From, To, {DependencyEdge::DT_RESOURCE, Mask, Cost});
262   }
263 
264   // Called by the bottleneck analysis at the end of simulation to propagate
265   // costs through the edges of the graph, and compute a critical path.
266   void finalizeGraph() {
267     SmallVector<unsigned, 16> RootSet;
268     initializeRootSet(RootSet);
269     propagateThroughEdges(RootSet);
270   }
271 
272   // Returns a sequence of edges representing the critical sequence based on the
273   // simulated run. It assumes that the graph has already been finalized (i.e.
274   // method `finalizeGraph()` has already been called on this graph).
275   void getCriticalSequence(SmallVectorImpl<const DependencyEdge *> &Seq) const;
276 
277 #ifndef NDEBUG
278   void dump(raw_ostream &OS, MCInstPrinter &MCIP) const;
279 #endif
280 };
281 
282 /// A view that collects and prints a few performance numbers.
283 class BottleneckAnalysis : public View {
284   const MCSubtargetInfo &STI;
285   MCInstPrinter &MCIP;
286   PressureTracker Tracker;
287   DependencyGraph DG;
288 
289   ArrayRef<MCInst> Source;
290   unsigned Iterations;
291   unsigned TotalCycles;
292 
293   bool PressureIncreasedBecauseOfResources;
294   bool PressureIncreasedBecauseOfRegisterDependencies;
295   bool PressureIncreasedBecauseOfMemoryDependencies;
296   // True if throughput was affected by dispatch stalls.
297   bool SeenStallCycles;
298 
299   struct BackPressureInfo {
300     // Cycles where backpressure increased.
301     unsigned PressureIncreaseCycles;
302     // Cycles where backpressure increased because of pipeline pressure.
303     unsigned ResourcePressureCycles;
304     // Cycles where backpressure increased because of data dependencies.
305     unsigned DataDependencyCycles;
306     // Cycles where backpressure increased because of register dependencies.
307     unsigned RegisterDependencyCycles;
308     // Cycles where backpressure increased because of memory dependencies.
309     unsigned MemoryDependencyCycles;
310   };
311   BackPressureInfo BPI;
312 
313   // Used to populate the dependency graph DG.
314   void addRegisterDep(unsigned From, unsigned To, unsigned RegID, unsigned Cy);
315   void addMemoryDep(unsigned From, unsigned To, unsigned Cy);
316   void addResourceDep(unsigned From, unsigned To, uint64_t Mask, unsigned Cy);
317 
318   // Prints a bottleneck message to OS.
319   void printBottleneckHints(raw_ostream &OS) const;
320   void printCriticalSequence(raw_ostream &OS) const;
321 
322 public:
323   BottleneckAnalysis(const MCSubtargetInfo &STI, MCInstPrinter &MCIP,
324                      ArrayRef<MCInst> Sequence, unsigned Iterations);
325 
326   void onCycleEnd() override;
327   void onEvent(const HWStallEvent &Event) override { SeenStallCycles = true; }
328   void onEvent(const HWPressureEvent &Event) override;
329   void onEvent(const HWInstructionEvent &Event) override;
330 
331   void printView(raw_ostream &OS) const override;
332 
333 #ifndef NDEBUG
334   void dump(raw_ostream &OS, MCInstPrinter &MCIP) const { DG.dump(OS, MCIP); }
335 #endif
336 };
337 
338 } // namespace mca
339 } // namespace llvm
340 
341 #endif
342