LLVM 22.0.0git
InstrBuilder.h
Go to the documentation of this file.
1//===--------------------- InstrBuilder.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/// A builder class for instructions that are statically analyzed by llvm-mca.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MCA_INSTRBUILDER_H
15#define LLVM_MCA_INSTRBUILDER_H
16
17#include "llvm/ADT/Hashing.h"
18#include "llvm/ADT/STLExtras.h"
20#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MCA/Support.h"
27#include "llvm/Support/Error.h"
28
29namespace llvm {
30namespace mca {
31
32class RecycledInstErr : public ErrorInfo<RecycledInstErr> {
33 Instruction *RecycledInst;
34
35public:
36 LLVM_ABI static char ID;
37
38 explicit RecycledInstErr(Instruction *Inst) : RecycledInst(Inst) {}
39 // Always need to carry an Instruction
40 RecycledInstErr() = delete;
41
42 Instruction *getInst() const { return RecycledInst; }
43
44 void log(raw_ostream &OS) const override {
45 OS << "Instruction is recycled\n";
46 }
47
48 std::error_code convertToErrorCode() const override {
50 }
51};
52
53/// A builder class that knows how to construct Instruction objects.
54///
55/// Every llvm-mca Instruction is described by an object of class InstrDesc.
56/// An InstrDesc describes which registers are read/written by the instruction,
57/// as well as the instruction latency and hardware resources consumed.
58///
59/// This class is used by the tool to construct Instructions and instruction
60/// descriptors (i.e. InstrDesc objects).
61/// Information from the machine scheduling model is used to identify processor
62/// resources that are consumed by an instruction.
63class InstrBuilder {
64 const MCSubtargetInfo &STI;
65 const MCInstrInfo &MCII;
66 const MCRegisterInfo &MRI;
67 const MCInstrAnalysis *MCIA;
68 const InstrumentManager &IM;
69 SmallVector<uint64_t, 8> ProcResourceMasks;
70
71 // Key is the MCI.Opcode and SchedClassID the describe the value InstrDesc
73 std::unique_ptr<const InstrDesc>>
74 Descriptors;
75
76 // Key is a hash of the MCInstruction and a SchedClassID that describe the
77 // value InstrDesc
78 DenseMap<std::pair<hash_code, unsigned>, std::unique_ptr<const InstrDesc>>
79 VariantDescriptors;
80
81 // These descriptors are customized for particular instructions and cannot
82 // be reused
84
85 bool FirstCallInst;
86 bool FirstReturnInst;
87 unsigned CallLatency;
88
89 using InstRecycleCallback = std::function<Instruction *(const InstrDesc &)>;
90 InstRecycleCallback InstRecycleCB;
91
92 Expected<unsigned> getVariantSchedClassID(const MCInst &MCI, unsigned SchedClassID);
94 createInstrDescImpl(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
96 getOrCreateInstrDesc(const MCInst &MCI,
97 const SmallVector<Instrument *> &IVec);
98
99 InstrBuilder(const InstrBuilder &) = delete;
100 InstrBuilder &operator=(const InstrBuilder &) = delete;
101
102 void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
103 void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
104 Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
105
106public:
107 LLVM_ABI InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
108 const MCRegisterInfo &RI, const MCInstrAnalysis *IA,
109 const InstrumentManager &IM, unsigned CallLatency);
110
111 void clear() {
112 Descriptors.clear();
113 VariantDescriptors.clear();
114 FirstCallInst = true;
115 FirstReturnInst = true;
116 }
117
118 /// Set a callback which is invoked to retrieve a recycled mca::Instruction
119 /// or null if there isn't any.
120 void setInstRecycleCallback(InstRecycleCallback CB) { InstRecycleCB = CB; }
121
123 createInstruction(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
124};
125} // namespace mca
126} // namespace llvm
127
128#endif // LLVM_MCA_INSTRBUILDER_H
#define LLVM_ABI
Definition Compiler.h:213
This file defines the base class CustomBehaviour which can be inherited from by specific targets (ex.
This file defines abstractions used by the Pipeline to model register reads, register writes and inst...
This file contains some templates that are useful if you are working with the STL at all.
Base class for user error types.
Definition Error.h:354
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
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
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
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.
void setInstRecycleCallback(InstRecycleCallback CB)
Set a callback which is invoked to retrieve a recycled mca::Instruction or null if there isn't any.
LLVM_ABI Expected< std::unique_ptr< Instruction > > createInstruction(const MCInst &MCI, const SmallVector< Instrument * > &IVec)
An instruction propagated through the simulated instruction pipeline.
This class allows targets to optionally customize the logic that resolves scheduling class IDs.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Instruction * getInst() const
RecycledInstErr(Instruction *Inst)
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
static LLVM_ABI char ID
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Helper functions used by various pipeline components.
char InstructionError< T >::ID
Definition Support.h:44
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
An instruction descriptor.