Thanks to visit codestin.com
Credit goes to llvm.org

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(Instruction &Inst, 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.
67protected:
71
72public:
76
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
119 /// The description of Instrument kind
120 const StringRef Desc;
121
122 /// The instrumentation data
123 const StringRef Data;
124
125public:
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
137 std::optional<unsigned> Latency;
138
139public:
140 static const StringRef DESC_NAME;
142 // Skip spaces and tabs.
143 Data = Data.trim();
144 if (Data.empty()) // Empty description. Bail out.
145 return;
146 unsigned L = 0;
147 if (!Data.getAsInteger(10, L))
148 Latency = L;
149 }
150
151 bool hasValue() const { return bool(Latency); }
152 unsigned getLatency() const { return *Latency; }
153};
154
155using UniqueInstrument = std::unique_ptr<Instrument>;
156
157/// This class allows targets to optionally customize the logic that resolves
158/// scheduling class IDs. Targets can use information encoded in Instrument
159/// objects to make more informed scheduling decisions.
161protected:
165
166public:
170
171 virtual ~InstrumentManager() = default;
172
173 /// Returns true if llvm-mca should ignore instruments.
174 virtual bool shouldIgnoreInstruments() const { return !EnableInstruments; }
175
176 // Returns true if this supports processing Instrument with
177 // Instrument.Desc equal to Type
178 virtual bool supportsInstrumentType(StringRef Type) const;
179
180 /// Allocate an Instrument, and return a unique pointer to it. This function
181 /// may be useful to create instruments coming from comments in the assembly.
182 /// See createInstruments to create Instruments from MCInst
183 virtual UniqueInstrument createInstrument(StringRef Desc, StringRef Data);
184
185 /// Return a list of unique pointers to Instruments, where each Instrument
186 /// is allocated by this function. See createInstrument to create Instrument
187 /// from a description and data.
188 virtual SmallVector<UniqueInstrument> createInstruments(const MCInst &Inst);
189
190 /// Given an MCInst and a vector of Instrument, a target can
191 /// return a SchedClassID. This can be used by a subtarget to return a
192 /// PseudoInstruction SchedClassID instead of the one that belongs to the
193 /// BaseInstruction This can be useful when a BaseInstruction does not convey
194 /// the correct scheduling information without additional data. By default,
195 /// it returns the SchedClassID that belongs to MCI.
196 virtual unsigned getSchedClassID(const MCInstrInfo &MCII, const MCInst &MCI,
197 const SmallVector<Instrument *> &IVec) const;
198
199 // Return true if instruments can modify instruction description
200 virtual bool canCustomize(const ArrayRef<Instrument *> IVec) const;
201
202 // Customize instruction description
203 virtual void customize(const ArrayRef<Instrument *> IVec,
205};
206
207} // namespace mca
208} // namespace llvm
209
210#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 ~InstrPostProcess()=default
virtual void postProcessInstruction(Instruction &Inst, const MCInst &MCI)
This method can be overriden by targets to modify the mca::Instruction object after it has been lower...
const MCSubtargetInfo & STI
InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
An instruction propagated through the simulated instruction pipeline.
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