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/InstructionView.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 #include "llvm/Support/JSON.h"
67 
68 namespace llvm {
69 namespace mca {
70 
71 /// This class collects resource pressure statistics and it is able to print
72 /// out all the collected information as a table to an output stream.
73 class ResourcePressureView : public InstructionView {
74   unsigned LastInstructionIdx;
75 
76   // Map to quickly obtain the ResourceUsage column index from a processor
77   // resource ID.
78   llvm::DenseMap<unsigned, unsigned> Resource2VecIndex;
79 
80   // Table of resources used by instructions.
81   std::vector<ResourceCycles> ResourceUsage;
82   unsigned NumResourceUnits;
83 
84   void printResourcePressurePerIter(llvm::raw_ostream &OS) const;
85   void printResourcePressurePerInst(llvm::raw_ostream &OS) const;
86 
87 public:
88   ResourcePressureView(const llvm::MCSubtargetInfo &sti,
89                        llvm::MCInstPrinter &Printer,
90                        llvm::ArrayRef<llvm::MCInst> S);
91 
92   void onEvent(const HWInstructionEvent &Event) override;
93   void printView(llvm::raw_ostream &OS) const override {
94     printResourcePressurePerIter(OS);
95     printResourcePressurePerInst(OS);
96   }
97   StringRef getNameAsString() const override { return "ResourcePressureView"; }
98   json::Value toJSON() const override;
99 };
100 } // namespace mca
101 } // namespace llvm
102 
103 #endif
104