xref: /llvm-project/llvm/tools/llvm-mca/Views/ResourcePressureView.h (revision 85e3875ad7461fa8320db6895e0189cffb91ae7d)
110aa09f0SMatt Davis //===--------------------- ResourcePressureView.h ---------------*- C++ -*-===//
210aa09f0SMatt Davis //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610aa09f0SMatt Davis //
710aa09f0SMatt Davis //===----------------------------------------------------------------------===//
810aa09f0SMatt Davis /// \file
910aa09f0SMatt Davis ///
1010aa09f0SMatt Davis /// This file define class ResourcePressureView.
1110aa09f0SMatt Davis /// Class ResourcePressureView observes hardware events generated by
1210aa09f0SMatt Davis /// the Pipeline object and collects statistics related to resource usage at
1310aa09f0SMatt Davis /// instruction granularity.
1410aa09f0SMatt Davis /// Resource pressure information is then printed out to a stream in the
1510aa09f0SMatt Davis /// form of a table like the one from the example below:
1610aa09f0SMatt Davis ///
1710aa09f0SMatt Davis /// Resources:
1810aa09f0SMatt Davis /// [0] - JALU0
1910aa09f0SMatt Davis /// [1] - JALU1
2010aa09f0SMatt Davis /// [2] - JDiv
2110aa09f0SMatt Davis /// [3] - JFPM
2210aa09f0SMatt Davis /// [4] - JFPU0
2310aa09f0SMatt Davis /// [5] - JFPU1
2410aa09f0SMatt Davis /// [6] - JLAGU
2510aa09f0SMatt Davis /// [7] - JSAGU
2610aa09f0SMatt Davis /// [8] - JSTC
2710aa09f0SMatt Davis /// [9] - JVIMUL
2810aa09f0SMatt Davis ///
2910aa09f0SMatt Davis /// Resource pressure per iteration:
3010aa09f0SMatt Davis /// [0]    [1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]    [9]
3110aa09f0SMatt Davis /// 0.00   0.00   0.00   0.00   2.00   2.00   0.00   0.00   0.00   0.00
3210aa09f0SMatt Davis ///
3310aa09f0SMatt Davis /// Resource pressure by instruction:
3410aa09f0SMatt Davis /// [0]  [1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]  Instructions:
3510aa09f0SMatt Davis ///  -    -    -    -    -   1.00  -    -    -    -   vpermilpd  $1,    %xmm0,
3610aa09f0SMatt Davis ///  %xmm1
3710aa09f0SMatt Davis ///  -    -    -    -   1.00  -    -    -    -    -   vaddps     %xmm0, %xmm1,
3810aa09f0SMatt Davis ///  %xmm2
3910aa09f0SMatt Davis ///  -    -    -    -    -   1.00  -    -    -    -   vmovshdup  %xmm2, %xmm3
4010aa09f0SMatt Davis ///  -    -    -    -   1.00  -    -    -    -    -   vaddss     %xmm2, %xmm3,
4110aa09f0SMatt Davis ///  %xmm4
4210aa09f0SMatt Davis ///
4310aa09f0SMatt Davis /// In this example, we have AVX code executed on AMD Jaguar (btver2).
4410aa09f0SMatt Davis /// Both shuffles and vector floating point add operations on XMM registers have
4510aa09f0SMatt Davis /// a reciprocal throughput of 1cy.
4610aa09f0SMatt Davis /// Each add is issued to pipeline JFPU0, while each shuffle is issued to
4710aa09f0SMatt Davis /// pipeline JFPU1. The overall pressure per iteration is reported by two
4810aa09f0SMatt Davis /// tables: the first smaller table is the resource pressure per iteration;
4910aa09f0SMatt Davis /// the second table reports resource pressure per instruction. Values are the
5010aa09f0SMatt Davis /// average resource cycles consumed by an instruction.
5110aa09f0SMatt Davis /// Every vector add from the example uses resource JFPU0 for an average of 1cy
5210aa09f0SMatt Davis /// per iteration. Consequently, the resource pressure on JFPU0 is of 2cy per
5310aa09f0SMatt Davis /// iteration.
5410aa09f0SMatt Davis ///
5510aa09f0SMatt Davis //===----------------------------------------------------------------------===//
5610aa09f0SMatt Davis 
5710aa09f0SMatt Davis #ifndef LLVM_TOOLS_LLVM_MCA_RESOURCEPRESSUREVIEW_H
5810aa09f0SMatt Davis #define LLVM_TOOLS_LLVM_MCA_RESOURCEPRESSUREVIEW_H
5910aa09f0SMatt Davis 
60d38be2baSWolfgang Pieb #include "Views/InstructionView.h"
6184d00513SAndrea Di Biagio #include "llvm/ADT/ArrayRef.h"
6210aa09f0SMatt Davis #include "llvm/ADT/DenseMap.h"
6384d00513SAndrea Di Biagio #include "llvm/MC/MCInst.h"
6410aa09f0SMatt Davis #include "llvm/MC/MCInstPrinter.h"
6510aa09f0SMatt Davis #include "llvm/MC/MCSubtargetInfo.h"
66d38be2baSWolfgang Pieb #include "llvm/Support/JSON.h"
6710aa09f0SMatt Davis 
685a8fd657SFangrui Song namespace llvm {
6910aa09f0SMatt Davis namespace mca {
7010aa09f0SMatt Davis 
7110aa09f0SMatt Davis /// This class collects resource pressure statistics and it is able to print
7210aa09f0SMatt Davis /// out all the collected information as a table to an output stream.
73e02920feSWolfgang Pieb class ResourcePressureView : public InstructionView {
7484d00513SAndrea Di Biagio   unsigned LastInstructionIdx;
7510aa09f0SMatt Davis 
7610aa09f0SMatt Davis   // Map to quickly obtain the ResourceUsage column index from a processor
7710aa09f0SMatt Davis   // resource ID.
7810aa09f0SMatt Davis   llvm::DenseMap<unsigned, unsigned> Resource2VecIndex;
7910aa09f0SMatt Davis 
8010aa09f0SMatt Davis   // Table of resources used by instructions.
81*85e3875aSMichael Maitland   std::vector<ReleaseAtCycles> ResourceUsage;
8210aa09f0SMatt Davis   unsigned NumResourceUnits;
8310aa09f0SMatt Davis 
8477c26aebSAndrea Di Biagio   void printResourcePressurePerIter(llvm::raw_ostream &OS) const;
8577c26aebSAndrea Di Biagio   void printResourcePressurePerInst(llvm::raw_ostream &OS) const;
8610aa09f0SMatt Davis 
8710aa09f0SMatt Davis public:
8810aa09f0SMatt Davis   ResourcePressureView(const llvm::MCSubtargetInfo &sti,
8984d00513SAndrea Di Biagio                        llvm::MCInstPrinter &Printer,
9084d00513SAndrea Di Biagio                        llvm::ArrayRef<llvm::MCInst> S);
9110aa09f0SMatt Davis 
9210aa09f0SMatt Davis   void onEvent(const HWInstructionEvent &Event) override;
printView(llvm::raw_ostream & OS)9310aa09f0SMatt Davis   void printView(llvm::raw_ostream &OS) const override {
9477c26aebSAndrea Di Biagio     printResourcePressurePerIter(OS);
9577c26aebSAndrea Di Biagio     printResourcePressurePerInst(OS);
9610aa09f0SMatt Davis   }
getNameAsString()97d38be2baSWolfgang Pieb   StringRef getNameAsString() const override { return "ResourcePressureView"; }
98c6e8f814SWolfgang Pieb   json::Value toJSON() const override;
9910aa09f0SMatt Davis };
10010aa09f0SMatt Davis } // namespace mca
1015a8fd657SFangrui Song } // namespace llvm
10210aa09f0SMatt Davis 
10310aa09f0SMatt Davis #endif
104