LLVM 22.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
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MCA/SourceMgr.h"
26#include "llvm/MCA/View.h"
28
29namespace llvm {
30namespace mca {
31
32/// Class which can be overriden by targets to modify the
33/// mca::Instruction objects before the pipeline starts.
34/// A common usage of this class is to add immediate operands to certain
35/// instructions or to remove Defs/Uses from an instruction where the
36/// schedulinng model is incorrect.
38protected:
41
42public:
45
46 virtual ~InstrPostProcess() = default;
47
48 /// This method can be overriden by targets to modify the mca::Instruction
49 /// object after it has been lowered from the MCInst.
50 /// This is generally a less disruptive alternative to modifying the
51 /// scheduling model.
52 virtual void postProcessInstruction(std::unique_ptr<Instruction> &Inst,
53 const MCInst &MCI) {}
54
55 // The resetState() method gets invoked at the beginning of each code region
56 // so that targets that override this function can clear any state that they
57 // have left from the previous code region.
58 virtual void resetState() {}
59};
60
61/// Class which can be overriden by targets to enforce instruction
62/// dependencies and behaviours that aren't expressed well enough
63/// within the scheduling model for mca to automatically simulate
64/// them properly.
65/// If you implement this class for your target, make sure to also implement
66/// a target specific InstrPostProcess class as well.
68protected:
72
73public:
77
79
80 /// Before the llvm-mca pipeline dispatches an instruction, it first checks
81 /// for any register or resource dependencies / hazards. If it doesn't find
82 /// any, this method will be invoked to determine if there are any custom
83 /// hazards that the instruction needs to wait for.
84 /// The return value of this method is the number of cycles that the
85 /// instruction needs to wait for.
86 /// It's safe to underestimate the number of cycles to wait for since these
87 /// checks will be invoked again before the intruction gets dispatched.
88 /// However, it's not safe (accurate) to overestimate the number of cycles
89 /// to wait for since the instruction will wait for AT LEAST that number of
90 /// cycles before attempting to be dispatched again.
91 virtual unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst,
92 const InstRef &IR);
93
94 // Functions that target CBs can override to return a list of
95 // target specific Views that need to live within /lib/Target/ so that
96 // they can benefit from the target CB or from backend functionality that is
97 // not already exposed through MC-layer classes. Keep in mind that how this
98 // function is used is that the function is called within llvm-mca.cpp and
99 // then each unique_ptr<View> is passed into the PipelinePrinter::addView()
100 // function. This function will then std::move the View into its own vector of
101 // Views. So any CB that overrides this function needs to make sure that they
102 // are not relying on the current address or reference of the View
103 // unique_ptrs. If you do need the CB and View to be able to communicate with
104 // each other, consider giving the View a reference or pointer to the CB when
105 // the View is constructed. Then the View can query the CB for information
106 // when it needs it.
107 /// Return a vector of Views that will be added before all other Views.
108 virtual std::vector<std::unique_ptr<View>>
110 /// Return a vector of Views that will be added after the InstructionInfoView.
111 virtual std::vector<std::unique_ptr<View>>
114 /// Return a vector of Views that will be added after all other Views.
115 virtual std::vector<std::unique_ptr<View>>
117};
118
120 /// The description of Instrument kind
121 const StringRef Desc;
122
123 /// The instrumentation data
124 const StringRef Data;
125
126public:
127 Instrument(StringRef Desc, StringRef Data) : Desc(Desc), Data(Data) {}
128
129 Instrument() : Instrument("", "") {}
130
131 virtual ~Instrument() = default;
132
133 StringRef getDesc() const { return Desc; }
134 StringRef getData() const { return Data; }
135};
136
138 std::optional<unsigned> Latency;
139
140public:
141 static const StringRef DESC_NAME;
143 // Skip spaces and tabs.
144 Data = Data.trim();
145 if (Data.empty()) // Empty description. Bail out.
146 return;
147 unsigned L = 0;
148 if (!Data.getAsInteger(10, L))
149 Latency = L;
150 }
151
152 bool hasValue() const { return bool(Latency); }
153 unsigned getLatency() const { return *Latency; }
154};
155
156using UniqueInstrument = std::unique_ptr<Instrument>;
157
158/// This class allows targets to optionally customize the logic that resolves
159/// scheduling class IDs. Targets can use information encoded in Instrument
160/// objects to make more informed scheduling decisions.
162protected:
166
167public:
171
172 virtual ~InstrumentManager() = default;
173
174 /// Returns true if llvm-mca should ignore instruments.
175 virtual bool shouldIgnoreInstruments() const { return !EnableInstruments; }
176
177 // Returns true if this supports processing Instrument with
178 // Instrument.Desc equal to Type
179 virtual bool supportsInstrumentType(StringRef Type) const;
180
181 /// Allocate an Instrument, and return a unique pointer to it. This function
182 /// may be useful to create instruments coming from comments in the assembly.
183 /// See createInstruments to create Instruments from MCInst
184 virtual UniqueInstrument createInstrument(StringRef Desc, StringRef Data);
185
186 /// Return a list of unique pointers to Instruments, where each Instrument
187 /// is allocated by this function. See createInstrument to create Instrument
188 /// from a description and data.
189 virtual SmallVector<UniqueInstrument> createInstruments(const MCInst &Inst);
190
191 /// Given an MCInst and a vector of Instrument, a target can
192 /// return a SchedClassID. This can be used by a subtarget to return a
193 /// PseudoInstruction SchedClassID instead of the one that belongs to the
194 /// BaseInstruction This can be useful when a BaseInstruction does not convey
195 /// the correct scheduling information without additional data. By default,
196 /// it returns the SchedClassID that belongs to MCI.
197 virtual unsigned getSchedClassID(const MCInstrInfo &MCII, const MCInst &MCI,
198 const SmallVector<Instrument *> &IVec) const;
199
200 // Return true if instruments can modify instruction description
201 virtual bool canCustomize(const ArrayRef<Instrument *> IVec) const;
202
203 // Customize instruction description
204 virtual void customize(const ArrayRef<Instrument *> IVec,
206};
207
208} // namespace mca
209} // namespace llvm
210
211#endif /* LLVM_MCA_CUSTOMBEHAVIOUR_H */
#define LLVM_ABI
Definition Compiler.h:213
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:80
This file contains abstract class SourceMgr and the default implementation, CircularSourceMgr.
if(PassOpts->AAPipeline)
This file defines the SmallVector class.
This file defines the main interface for Views.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
Generic base class for all target subtargets.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
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.
const MCInstrInfo & MCII
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 ...
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.
const mca::SourceMgr & SrcMgr
const MCSubtargetInfo & STI
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.
CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII)
An InstRef contains both a SourceMgr index and Instruction pair.
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...
virtual ~InstrPostProcess()=default
const MCSubtargetInfo & STI
InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
InstrumentManager(const MCSubtargetInfo &STI, const MCInstrInfo &MCII, bool EnableInstruments=true)
virtual ~InstrumentManager()=default
virtual bool shouldIgnoreInstruments() const
Returns true if llvm-mca should ignore instruments.
const MCSubtargetInfo & STI
StringRef getData() const
Instrument(StringRef Desc, StringRef Data)
virtual ~Instrument()=default
StringRef getDesc() const
static const StringRef DESC_NAME
std::unique_ptr< Instrument > UniqueInstrument
This is an optimization pass for GlobalISel generic memory operations.
Op::Description Desc
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
An instruction descriptor.
Abstracting the input code sequence (a sequence of MCInst) and assigning unique identifiers to every ...
Definition SourceMgr.h:29