LLVM  15.0.0git
CustomBehaviour.h
Go to the documentation of this file.
1 //===---------------------- CustomBehaviour.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 defines the base class CustomBehaviour which can be inherited from
11 /// by specific targets (ex. llvm/tools/llvm-mca/lib/X86CustomBehaviour.h).
12 /// CustomBehaviour is designed to enforce custom behaviour and dependencies
13 /// within the llvm-mca pipeline simulation that llvm-mca isn't already capable
14 /// of extracting from the Scheduling Models.
15 ///
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_MCA_CUSTOMBEHAVIOUR_H
19 #define LLVM_MCA_CUSTOMBEHAVIOUR_H
20 
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MCA/SourceMgr.h"
25 #include "llvm/MCA/View.h"
26 
27 namespace llvm {
28 namespace mca {
29 
30 /// Class which can be overriden by targets to modify the
31 /// mca::Instruction objects before the pipeline starts.
32 /// A common usage of this class is to add immediate operands to certain
33 /// instructions or to remove Defs/Uses from an instruction where the
34 /// schedulinng model is incorrect.
36 protected:
38  const MCInstrInfo &MCII;
39 
40 public:
42  : STI(STI), MCII(MCII) {}
43 
44  virtual ~InstrPostProcess() = default;
45 
46  /// This method can be overriden by targets to modify the mca::Instruction
47  /// object after it has been lowered from the MCInst.
48  /// This is generally a less disruptive alternative to modifying the
49  /// scheduling model.
50  virtual void postProcessInstruction(std::unique_ptr<Instruction> &Inst,
51  const MCInst &MCI) {}
52 
53  // The resetState() method gets invoked at the beginning of each code region
54  // so that targets that override this function can clear any state that they
55  // have left from the previous code region.
56  virtual void resetState() {}
57 };
58 
59 /// Class which can be overriden by targets to enforce instruction
60 /// dependencies and behaviours that aren't expressed well enough
61 /// within the scheduling model for mca to automatically simulate
62 /// them properly.
63 /// If you implement this class for your target, make sure to also implement
64 /// a target specific InstrPostProcess class as well.
66 protected:
69  const MCInstrInfo &MCII;
70 
71 public:
73  const MCInstrInfo &MCII)
74  : STI(STI), SrcMgr(SrcMgr), MCII(MCII) {}
75 
76  virtual ~CustomBehaviour();
77 
78  /// Before the llvm-mca pipeline dispatches an instruction, it first checks
79  /// for any register or resource dependencies / hazards. If it doesn't find
80  /// any, this method will be invoked to determine if there are any custom
81  /// hazards that the instruction needs to wait for.
82  /// The return value of this method is the number of cycles that the
83  /// instruction needs to wait for.
84  /// It's safe to underestimate the number of cycles to wait for since these
85  /// checks will be invoked again before the intruction gets dispatched.
86  /// However, it's not safe (accurate) to overestimate the number of cycles
87  /// to wait for since the instruction will wait for AT LEAST that number of
88  /// cycles before attempting to be dispatched again.
89  virtual unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst,
90  const InstRef &IR);
91 
92  // Functions that target CBs can override to return a list of
93  // target specific Views that need to live within /lib/Target/ so that
94  // they can benefit from the target CB or from backend functionality that is
95  // not already exposed through MC-layer classes. Keep in mind that how this
96  // function is used is that the function is called within llvm-mca.cpp and
97  // then each unique_ptr<View> is passed into the PipelinePrinter::addView()
98  // function. This function will then std::move the View into its own vector of
99  // Views. So any CB that overrides this function needs to make sure that they
100  // are not relying on the current address or reference of the View
101  // unique_ptrs. If you do need the CB and View to be able to communicate with
102  // each other, consider giving the View a reference or pointer to the CB when
103  // the View is constructed. Then the View can query the CB for information
104  // when it needs it.
105  /// Return a vector of Views that will be added before all other Views.
106  virtual std::vector<std::unique_ptr<View>>
108  /// Return a vector of Views that will be added after the InstructionInfoView.
109  virtual std::vector<std::unique_ptr<View>>
112  /// Return a vector of Views that will be added after all other Views.
113  virtual std::vector<std::unique_ptr<View>>
115 };
116 
117 } // namespace mca
118 } // namespace llvm
119 
120 #endif /* LLVM_MCA_CUSTOMBEHAVIOUR_H */
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::mca::SourceMgr
Abstracting the input code sequence (a sequence of MCInst) and assigning unique identifiers to every ...
Definition: SourceMgr.h:29
llvm::mca::InstrPostProcess::resetState
virtual void resetState()
Definition: CustomBehaviour.h:56
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::mca::CustomBehaviour::~CustomBehaviour
virtual ~CustomBehaviour()
SourceMgr.h
MCInstrInfo.h
IP
Definition: NVPTXLowerArgs.cpp:167
llvm::mca::CustomBehaviour::SrcMgr
const mca::SourceMgr & SrcMgr
Definition: CustomBehaviour.h:68
MCInst.h
llvm::mca::CustomBehaviour::MCII
const MCInstrInfo & MCII
Definition: CustomBehaviour.h:69
MCSubtargetInfo.h
llvm::mca::InstrPostProcess::STI
const MCSubtargetInfo & STI
Definition: CustomBehaviour.h:37
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:746
llvm::mca::CustomBehaviour::STI
const MCSubtargetInfo & STI
Definition: CustomBehaviour.h:67
llvm::mca::InstrPostProcess::postProcessInstruction
virtual void postProcessInstruction(std::unique_ptr< Instruction > &Inst, const MCInst &MCI)
This method can be overriden by targets to modify the mca::Instruction object after it has been lower...
Definition: CustomBehaviour.h:50
llvm::MCInstPrinter
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:43
llvm::mca::CustomBehaviour
Class which can be overriden by targets to enforce instruction dependencies and behaviours that aren'...
Definition: CustomBehaviour.h:65
llvm::mca::InstrPostProcess::~InstrPostProcess
virtual ~InstrPostProcess()=default
llvm::mca::InstRef
An InstRef contains both a SourceMgr index and Instruction pair.
Definition: Instruction.h:720
llvm::mca::InstrPostProcess::MCII
const MCInstrInfo & MCII
Definition: CustomBehaviour.h:38
llvm::mca::CustomBehaviour::getStartViews
virtual std::vector< std::unique_ptr< View > > getStartViews(llvm::MCInstPrinter &IP, llvm::ArrayRef< llvm::MCInst > Insts)
Return a vector of Views that will be added before all other Views.
Definition: CustomBehaviour.cpp:28
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::mca::CustomBehaviour::checkCustomHazard
virtual unsigned checkCustomHazard(ArrayRef< InstRef > IssuedInst, const InstRef &IR)
Before the llvm-mca pipeline dispatches an instruction, it first checks for any register or resource ...
Definition: CustomBehaviour.cpp:21
llvm::mca::InstrPostProcess
Class which can be overriden by targets to modify the mca::Instruction objects before the pipeline st...
Definition: CustomBehaviour.h:35
llvm::mca::CustomBehaviour::CustomBehaviour
CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII)
Definition: CustomBehaviour.h:72
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
llvm::mca::InstrPostProcess::InstrPostProcess
InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
Definition: CustomBehaviour.h:41
llvm::mca::CustomBehaviour::getEndViews
virtual std::vector< std::unique_ptr< View > > getEndViews(llvm::MCInstPrinter &IP, llvm::ArrayRef< llvm::MCInst > Insts)
Return a vector of Views that will be added after all other Views.
Definition: CustomBehaviour.cpp:40
View.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
llvm::mca::CustomBehaviour::getPostInstrInfoViews
virtual std::vector< std::unique_ptr< View > > getPostInstrInfoViews(llvm::MCInstPrinter &IP, llvm::ArrayRef< llvm::MCInst > Insts)
Return a vector of Views that will be added after the InstructionInfoView.
Definition: CustomBehaviour.cpp:34