xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/ResourcePressureView.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 //===--------------------- ResourcePressureView.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 define class ResourcePressureView.
11 /// Class ResourcePressureView observes hardware events generated by
12 /// the Pipeline object and collects statistics related to resource usage at
13 /// instruction granularity.
14 /// Resource pressure information is then printed out to a stream in the
15 /// form of a table like the one from the example below:
16 ///
17 /// Resources:
18 /// [0] - JALU0
19 /// [1] - JALU1
20 /// [2] - JDiv
21 /// [3] - JFPM
22 /// [4] - JFPU0
23 /// [5] - JFPU1
24 /// [6] - JLAGU
25 /// [7] - JSAGU
26 /// [8] - JSTC
27 /// [9] - JVIMUL
28 ///
29 /// Resource pressure per iteration:
30 /// [0]    [1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]    [9]
31 /// 0.00   0.00   0.00   0.00   2.00   2.00   0.00   0.00   0.00   0.00
32 ///
33 /// Resource pressure by instruction:
34 /// [0]  [1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]  Instructions:
35 ///  -    -    -    -    -   1.00  -    -    -    -   vpermilpd  $1,    %xmm0,
36 ///  %xmm1
37 ///  -    -    -    -   1.00  -    -    -    -    -   vaddps     %xmm0, %xmm1,
38 ///  %xmm2
39 ///  -    -    -    -    -   1.00  -    -    -    -   vmovshdup  %xmm2, %xmm3
40 ///  -    -    -    -   1.00  -    -    -    -    -   vaddss     %xmm2, %xmm3,
41 ///  %xmm4
42 ///
43 /// In this example, we have AVX code executed on AMD Jaguar (btver2).
44 /// Both shuffles and vector floating point add operations on XMM registers have
45 /// a reciprocal throughput of 1cy.
46 /// Each add is issued to pipeline JFPU0, while each shuffle is issued to
47 /// pipeline JFPU1. The overall pressure per iteration is reported by two
48 /// tables: the first smaller table is the resource pressure per iteration;
49 /// the second table reports resource pressure per instruction. Values are the
50 /// average resource cycles consumed by an instruction.
51 /// Every vector add from the example uses resource JFPU0 for an average of 1cy
52 /// per iteration. Consequently, the resource pressure on JFPU0 is of 2cy per
53 /// iteration.
54 ///
55 //===----------------------------------------------------------------------===//
56 
57 #ifndef LLVM_TOOLS_LLVM_MCA_RESOURCEPRESSUREVIEW_H
58 #define LLVM_TOOLS_LLVM_MCA_RESOURCEPRESSUREVIEW_H
59 
60 #include "Views/View.h"
61 #include "llvm/ADT/ArrayRef.h"
62 #include "llvm/ADT/DenseMap.h"
63 #include "llvm/MC/MCInst.h"
64 #include "llvm/MC/MCInstPrinter.h"
65 #include "llvm/MC/MCSubtargetInfo.h"
66 
67 namespace llvm {
68 namespace mca {
69 
70 /// This class collects resource pressure statistics and it is able to print
71 /// out all the collected information as a table to an output stream.
72 class ResourcePressureView : public View {
73   const llvm::MCSubtargetInfo &STI;
74   llvm::MCInstPrinter &MCIP;
75   llvm::ArrayRef<llvm::MCInst> Source;
76   unsigned LastInstructionIdx;
77 
78   // Map to quickly obtain the ResourceUsage column index from a processor
79   // resource ID.
80   llvm::DenseMap<unsigned, unsigned> Resource2VecIndex;
81 
82   // Table of resources used by instructions.
83   std::vector<ResourceCycles> ResourceUsage;
84   unsigned NumResourceUnits;
85 
86   void printResourcePressurePerIter(llvm::raw_ostream &OS) const;
87   void printResourcePressurePerInst(llvm::raw_ostream &OS) const;
88 
89 public:
90   ResourcePressureView(const llvm::MCSubtargetInfo &sti,
91                        llvm::MCInstPrinter &Printer,
92                        llvm::ArrayRef<llvm::MCInst> S);
93 
94   void onEvent(const HWInstructionEvent &Event) override;
95   void printView(llvm::raw_ostream &OS) const override {
96     printResourcePressurePerIter(OS);
97     printResourcePressurePerInst(OS);
98   }
99 };
100 } // namespace mca
101 } // namespace llvm
102 
103 #endif
104