LLVM 17.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"
27
28namespace llvm {
29namespace 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.
37protected:
40
41public:
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.
67protected:
71
72public:
74 const MCInstrInfo &MCII)
75 : STI(STI), SrcMgr(SrcMgr), MCII(MCII) {}
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
136using 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.
142protected:
145
146public:
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 */
Statically lint checks LLVM IR
Definition: Lint.cpp:746
This file contains abstract class SourceMgr and the default implementation, CircularSourceMgr.
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...
Definition: MCInstPrinter.h:44
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
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.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Class which can be overriden by targets to enforce instruction dependencies and behaviours that aren'...
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.
Definition: Instruction.h:720
Class which can be overriden by targets to modify the mca::Instruction objects before the pipeline st...
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
const MCInstrInfo & MCII
InstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
This class allows targets to optionally customize the logic that resolves scheduling class IDs.
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.
virtual bool supportsInstrumentType(StringRef Type) const
virtual SharedInstrument createInstrument(StringRef Desc, StringRef Data)
Allocate an Instrument, and return a shared pointer to it.
InstrumentManager(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
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
std::shared_ptr< Instrument > SharedInstrument
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Abstracting the input code sequence (a sequence of MCInst) and assigning unique identifiers to every ...
Definition: SourceMgr.h:29