LLVM  16.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/ADT/SmallVector.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MCA/SourceMgr.h"
26 #include "llvm/MCA/View.h"
27 
28 namespace llvm {
29 namespace mca {
30 
31 /// Class which can be overriden by targets to modify the
32 /// mca::Instruction objects before the pipeline starts.
33 /// A common usage of this class is to add immediate operands to certain
34 /// instructions or to remove Defs/Uses from an instruction where the
35 /// schedulinng model is incorrect.
37 protected:
39  const MCInstrInfo &MCII;
40 
41 public:
43  : STI(STI), MCII(MCII) {}
44 
45  virtual ~InstrPostProcess() = default;
46 
47  /// This method can be overriden by targets to modify the mca::Instruction
48  /// object after it has been lowered from the MCInst.
49  /// This is generally a less disruptive alternative to modifying the
50  /// scheduling model.
51  virtual void postProcessInstruction(std::unique_ptr<Instruction> &Inst,
52  const MCInst &MCI) {}
53 
54  // The resetState() method gets invoked at the beginning of each code region
55  // so that targets that override this function can clear any state that they
56  // have left from the previous code region.
57  virtual void resetState() {}
58 };
59 
60 /// Class which can be overriden by targets to enforce instruction
61 /// dependencies and behaviours that aren't expressed well enough
62 /// within the scheduling model for mca to automatically simulate
63 /// them properly.
64 /// If you implement this class for your target, make sure to also implement
65 /// a target specific InstrPostProcess class as well.
67 protected:
70  const MCInstrInfo &MCII;
71 
72 public:
74  const MCInstrInfo &MCII)
75  : STI(STI), SrcMgr(SrcMgr), MCII(MCII) {}
76 
77  virtual ~CustomBehaviour();
78 
79  /// Before the llvm-mca pipeline dispatches an instruction, it first checks
80  /// for any register or resource dependencies / hazards. If it doesn't find
81  /// any, this method will be invoked to determine if there are any custom
82  /// hazards that the instruction needs to wait for.
83  /// The return value of this method is the number of cycles that the
84  /// instruction needs to wait for.
85  /// It's safe to underestimate the number of cycles to wait for since these
86  /// checks will be invoked again before the intruction gets dispatched.
87  /// However, it's not safe (accurate) to overestimate the number of cycles
88  /// to wait for since the instruction will wait for AT LEAST that number of
89  /// cycles before attempting to be dispatched again.
90  virtual unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst,
91  const InstRef &IR);
92 
93  // Functions that target CBs can override to return a list of
94  // target specific Views that need to live within /lib/Target/ so that
95  // they can benefit from the target CB or from backend functionality that is
96  // not already exposed through MC-layer classes. Keep in mind that how this
97  // function is used is that the function is called within llvm-mca.cpp and
98  // then each unique_ptr<View> is passed into the PipelinePrinter::addView()
99  // function. This function will then std::move the View into its own vector of
100  // Views. So any CB that overrides this function needs to make sure that they
101  // are not relying on the current address or reference of the View
102  // unique_ptrs. If you do need the CB and View to be able to communicate with
103  // each other, consider giving the View a reference or pointer to the CB when
104  // the View is constructed. Then the View can query the CB for information
105  // when it needs it.
106  /// Return a vector of Views that will be added before all other Views.
107  virtual std::vector<std::unique_ptr<View>>
109  /// Return a vector of Views that will be added after the InstructionInfoView.
110  virtual std::vector<std::unique_ptr<View>>
113  /// Return a vector of Views that will be added after all other Views.
114  virtual std::vector<std::unique_ptr<View>>
116 };
117 
118 class Instrument {
119  /// The description of Instrument kind
120  const StringRef Desc;
121 
122  /// The instrumentation data
123  const StringRef Data;
124 
125 public:
126  Instrument(StringRef Desc, StringRef Data) : Desc(Desc), Data(Data) {}
127 
128  Instrument() : Instrument("", "") {}
129 
130  virtual ~Instrument() = default;
131 
132  StringRef getDesc() const { return Desc; }
133  StringRef getData() const { return Data; }
134 };
135 
136 using SharedInstrument = std::shared_ptr<Instrument>;
137 
138 /// This class allows targets to optionally customize the logic that resolves
139 /// scheduling class IDs. Targets can use information encoded in Instrument
140 /// objects to make more informed scheduling decisions.
142 protected:
145 
146 public:
148  : STI(STI), MCII(MCII) {}
149 
150  virtual ~InstrumentManager() = default;
151 
152  /// Returns true if llvm-mca should ignore instruments.
153  virtual bool shouldIgnoreInstruments() const { return true; }
154 
155  // Returns true if this supports processing Instrument with
156  // Instrument.Desc equal to Type
157  virtual bool supportsInstrumentType(StringRef Type) const { return false; }
158 
159  /// Allocate an Instrument, and return a shared pointer to it.
161 
162  /// Given an MCInst and a vector of Instrument, a target can
163  /// return a SchedClassID. This can be used by a subtarget to return a
164  /// PseudoInstruction SchedClassID instead of the one that belongs to the
165  /// BaseInstruction This can be useful when a BaseInstruction does not convey
166  /// the correct scheduling information without additional data. By default,
167  /// it returns the SchedClassID that belongs to MCI.
168  virtual unsigned
169  getSchedClassID(const MCInstrInfo &MCII, const MCInst &MCI,
170  const SmallVector<SharedInstrument> &IVec) const;
171 };
172 
173 } // namespace mca
174 } // namespace llvm
175 
176 #endif /* LLVM_MCA_CUSTOMBEHAVIOUR_H */
llvm::mca::Instrument::getData
StringRef getData() const
Definition: CustomBehaviour.h:133
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::mca::InstrumentManager
This class allows targets to optionally customize the logic that resolves scheduling class IDs.
Definition: CustomBehaviour.h:141
llvm::mca::SourceMgr
Abstracting the input code sequence (a sequence of MCInst) and assigning unique identifiers to every ...
Definition: SourceMgr.h:29
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::mca::InstrPostProcess::resetState
virtual void resetState()
Definition: CustomBehaviour.h:57
llvm::mca::InstrumentManager::STI
const MCSubtargetInfo & STI
Definition: CustomBehaviour.h:143
llvm::mca::Instrument::getDesc
StringRef getDesc() const
Definition: CustomBehaviour.h:132
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::mca::InstrumentManager::MCII
const MCInstrInfo & MCII
Definition: CustomBehaviour.h:144
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::mca::InstrumentManager::~InstrumentManager
virtual ~InstrumentManager()=default
llvm::mca::CustomBehaviour::~CustomBehaviour
virtual ~CustomBehaviour()
llvm::mca::InstrumentManager::shouldIgnoreInstruments
virtual bool shouldIgnoreInstruments() const
Returns true if llvm-mca should ignore instruments.
Definition: CustomBehaviour.h:153
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
SourceMgr.h
MCInstrInfo.h
IP
Definition: NVPTXLowerArgs.cpp:168
llvm::mca::CustomBehaviour::SrcMgr
const mca::SourceMgr & SrcMgr
Definition: CustomBehaviour.h:69
MCInst.h
llvm::mca::CustomBehaviour::MCII
const MCInstrInfo & MCII
Definition: CustomBehaviour.h:70
MCSubtargetInfo.h
llvm::mca::InstrPostProcess::STI
const MCSubtargetInfo & STI
Definition: CustomBehaviour.h:38
llvm::mca::SharedInstrument
std::shared_ptr< Instrument > SharedInstrument
Definition: CustomBehaviour.h:136
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:746
llvm::mca::CustomBehaviour::STI
const MCSubtargetInfo & STI
Definition: CustomBehaviour.h:68
llvm::mca::InstrumentManager::createInstrument
virtual SharedInstrument createInstrument(StringRef Desc, StringRef Data)
Allocate an Instrument, and return a shared pointer to it.
Definition: CustomBehaviour.cpp:45
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:51
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:66
llvm::mca::InstrPostProcess::~InstrPostProcess
virtual ~InstrPostProcess()=default
llvm::mca::InstrumentManager::InstrumentManager
InstrumentManager(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
Definition: CustomBehaviour.h:147
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:39
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::Instrument
Definition: CustomBehaviour.h:118
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::mca::Instrument::Instrument
Instrument(StringRef Desc, StringRef Data)
Definition: CustomBehaviour.h:126
llvm::mca::InstrPostProcess
Class which can be overriden by targets to modify the mca::Instruction objects before the pipeline st...
Definition: CustomBehaviour.h:36
llvm::mca::CustomBehaviour::CustomBehaviour
CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII)
Definition: CustomBehaviour.h:73
llvm::MCInstrInfo
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
llvm::mca::Instrument::~Instrument
virtual ~Instrument()=default
llvm::mca::InstrPostProcess::InstrPostProcess
InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
Definition: CustomBehaviour.h:42
SmallVector.h
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
llvm::mca::InstrumentManager::supportsInstrumentType
virtual bool supportsInstrumentType(StringRef Type) const
Definition: CustomBehaviour.h:157
View.h
llvm::mca::Instrument::Instrument
Instrument()
Definition: CustomBehaviour.h:128
llvm::mca::InstrumentManager::getSchedClassID
virtual unsigned getSchedClassID(const MCInstrInfo &MCII, const MCInst &MCI, const SmallVector< SharedInstrument > &IVec) const
Given an MCInst and a vector of Instrument, a target can return a SchedClassID.
Definition: CustomBehaviour.cpp:50
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:77
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