LLVM 20.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"
26#include "llvm/Support/Error.h"
27
28namespace llvm {
29namespace mca {
30
31class RecycledInstErr : public ErrorInfo<RecycledInstErr> {
32 Instruction *RecycledInst;
33
34public:
35 static char ID;
36
37 explicit RecycledInstErr(Instruction *Inst) : RecycledInst(Inst) {}
38 // Always need to carry an Instruction
39 RecycledInstErr() = delete;
40
41 Instruction *getInst() const { return RecycledInst; }
42
43 void log(raw_ostream &OS) const override {
44 OS << "Instruction is recycled\n";
45 }
46
47 std::error_code convertToErrorCode() const override {
49 }
50};
51
52/// A builder class that knows how to construct Instruction objects.
53///
54/// Every llvm-mca Instruction is described by an object of class InstrDesc.
55/// An InstrDesc describes which registers are read/written by the instruction,
56/// as well as the instruction latency and hardware resources consumed.
57///
58/// This class is used by the tool to construct Instructions and instruction
59/// descriptors (i.e. InstrDesc objects).
60/// Information from the machine scheduling model is used to identify processor
61/// resources that are consumed by an instruction.
63 const MCSubtargetInfo &STI;
64 const MCInstrInfo &MCII;
65 const MCRegisterInfo &MRI;
66 const MCInstrAnalysis *MCIA;
67 const InstrumentManager &IM;
68 SmallVector<uint64_t, 8> ProcResourceMasks;
69
70 // Key is the MCI.Opcode and SchedClassID the describe the value InstrDesc
72 std::unique_ptr<const InstrDesc>>
73 Descriptors;
74
75 // Key is a hash of the MCInstruction and a SchedClassID that describe the
76 // value InstrDesc
77 DenseMap<std::pair<hash_code, unsigned>, std::unique_ptr<const InstrDesc>>
78 VariantDescriptors;
79
80 bool FirstCallInst;
81 bool FirstReturnInst;
82 unsigned CallLatency;
83
84 using InstRecycleCallback = std::function<Instruction *(const InstrDesc &)>;
85 InstRecycleCallback InstRecycleCB;
86
87 Expected<unsigned> getVariantSchedClassID(const MCInst &MCI, unsigned SchedClassID);
89 createInstrDescImpl(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
91 getOrCreateInstrDesc(const MCInst &MCI,
92 const SmallVector<Instrument *> &IVec);
93
94 InstrBuilder(const InstrBuilder &) = delete;
95 InstrBuilder &operator=(const InstrBuilder &) = delete;
96
97 void populateWrites(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
98 void populateReads(InstrDesc &ID, const MCInst &MCI, unsigned SchedClassID);
99 Error verifyInstrDesc(const InstrDesc &ID, const MCInst &MCI) const;
100
101public:
102 InstrBuilder(const MCSubtargetInfo &STI, const MCInstrInfo &MCII,
103 const MCRegisterInfo &RI, const MCInstrAnalysis *IA,
104 const InstrumentManager &IM, unsigned CallLatency);
105
106 void clear() {
107 Descriptors.clear();
108 VariantDescriptors.clear();
109 FirstCallInst = true;
110 FirstReturnInst = true;
111 }
112
113 /// Set a callback which is invoked to retrieve a recycled mca::Instruction
114 /// or null if there isn't any.
115 void setInstRecycleCallback(InstRecycleCallback CB) { InstRecycleCB = CB; }
116
118 createInstruction(const MCInst &MCI, const SmallVector<Instrument *> &IVec);
119};
120} // namespace mca
121} // namespace llvm
122
123#endif // LLVM_MCA_INSTRBUILDER_H
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.
raw_pwrite_stream & OS
Base class for user error types.
Definition: Error.h:355
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
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.
Definition: SmallVector.h:1196
A builder class that knows how to construct Instruction objects.
Definition: InstrBuilder.h:62
void setInstRecycleCallback(InstRecycleCallback CB)
Set a callback which is invoked to retrieve a recycled mca::Instruction or null if there isn't any.
Definition: InstrBuilder.h:115
Expected< std::unique_ptr< Instruction > > createInstruction(const MCInst &MCI, const SmallVector< Instrument * > &IVec)
An instruction propagated through the simulated instruction pipeline.
Definition: Instruction.h:600
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.
Definition: InstrBuilder.h:43
Instruction * getInst() const
Definition: InstrBuilder.h:41
RecycledInstErr(Instruction *Inst)
Definition: InstrBuilder.h:37
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: InstrBuilder.h:47
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Helper functions used by various pipeline components.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Definition: Instruction.h:447