40#ifndef LLVM_CODEGEN_MIR2VEC_H
41#define LLVM_CODEGEN_MIR2VEC_H
89 using VocabMap = std::map<std::string, ir2vec::Embedding>;
120 enum class Section :
unsigned {
129 std::set<std::string> UniqueBaseOpcodeNames;
139 "Immediate",
"CImmediate",
"FPImmediate",
"MBB",
140 "FrameIndex",
"ConstantPoolIndex",
"TargetIndex",
"JumpTableIndex",
141 "ExternalSymbol",
"GlobalAddress",
"BlockAddress",
"RegisterMask",
142 "RegisterLiveOut",
"Metadata",
"MCSymbol",
"CFIIndex",
143 "IntrinsicID",
"Predicate",
"ShuffleMask",
"LaneMask"};
145 "Common operand names size changed, update accordingly");
147 const TargetInstrInfo &
TII;
148 const TargetRegisterInfo &
TRI;
149 const MachineRegisterInfo &MRI;
151 void generateStorage(
const VocabMap &OpcodeMap,
152 const VocabMap &CommonOperandMap,
153 const VocabMap &PhyRegMap,
const VocabMap &VirtRegMap);
154 void buildCanonicalOpcodeMapping();
155 void buildRegisterOperandMapping();
158 LLVM_ABI unsigned getCanonicalOpcodeIndex(
unsigned Opcode)
const;
172 unsigned LocalIndex = getCommonOperandIndex(OperandType);
173 return Storage[
static_cast<unsigned>(Section::CommonOperands)][LocalIndex];
180 return ZeroEmbedding;
188 return ZeroEmbedding;
192 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg);
194 return ZeroEmbedding;
196 Reg.isPhysical() ? Section::PhyRegisters : Section::VirtRegisters;
197 return Storage[
static_cast<unsigned>(SectionID)][*LocalIndex];
202 unsigned getEntityIDForCommonOperand(
204 return Layout.CommonOperandBase + getCommonOperandIndex(OperandType);
209 unsigned getEntityIDForRegister(
Register Reg)
const {
210 if (!
Reg.isValid() ||
Reg.isStack())
213 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg);
216 return Layout.VirtRegBase;
218 Reg.isPhysical() ? Layout.PhyRegBase : Layout.VirtRegBase;
219 return BaseOffset + *LocalIndex;
233 bool IsPhysical =
true)
const;
243 return Layout.OpcodeBase + getCanonicalOpcodeIndex(Opcode);
250 return getEntityIDForRegister(MO.
getReg());
251 return getEntityIDForCommonOperand(MO.
getType());
256 unsigned LocalIndex = getCanonicalOpcodeIndex(Opcode);
257 return Storage[
static_cast<unsigned>(Section::Opcodes)][LocalIndex];
261 auto OperandType = Operand.
getType();
263 return operator[](Operand.
getReg());
265 return operator[](OperandType);
278 create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap,
292 MIRVocabulary(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap,
331 LLVM_ABI static std::unique_ptr<MIREmbedder>
364 static std::unique_ptr<SymbolicMIREmbedder>
379 using VocabMap = std::map<std::string, mir2vec::Embedding>;
387 Error readVocabulary(VocabMap &OpcVocab, VocabMap &CommonOperandVocab,
388 VocabMap &PhyRegVocabMap, VocabMap &VirtRegVocabMap);
394 using VocabVector = std::vector<mir2vec::Embedding>;
395 using VocabMap = std::map<std::string, mir2vec::Embedding>;
414 Provider = std::make_unique<MIR2VecVocabProvider>(MMI);
434 bool doFinalization(
Module &M)
override;
442 return "MIR2Vec Vocabulary Printer Pass";
464 return "MIR2Vec Embedder Printer Pass";
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides ErrorOr<T> smart pointer.
const HexagonInstrInfo * TII
This file defines the IR2Vec vocabulary analysis(IR2VecVocabAnalysis), the core ir2vec::Embedder inte...
This header defines various interfaces for pass management in LLVM.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This is an important class for using LLVM in a threaded context.
MIR2VecPrinterLegacyPass(raw_ostream &OS)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Pass to analyze and populate MIR2Vec vocabulary from a module.
MIR2VecVocabProvider & getProvider()
Expected< mir2vec::MIRVocabulary > getMIR2VecVocabulary(const Module &M)
std::unique_ptr< MIR2VecVocabProvider > Provider
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
MIR2VecVocabLegacyAnalysis()
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
MIR2VecVocabPrinterLegacyPass(raw_ostream &OS)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MIR2Vec vocabulary provider used by pass managers and standalone tools.
MIR2VecVocabProvider(const MachineModuleInfo &MMI)
LLVM_ABI Expected< mir2vec::MIRVocabulary > getVocabulary(const Module &M)
MachineFunctionPass(char &ID)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Representation of each machine instruction.
This class contains meta information specific to a module.
MachineOperand class - Representation of each machine instruction operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Register
Register operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Iterator support for section-based access.
Generic storage class for section-based vocabularies.
Base class for MIR embedders.
const unsigned Dimension
Dimension of the embeddings; Captured from the vocabulary.
Embedding getMFunctionVector() const
Computes and returns the embedding for the current machine function.
const MIRVocabulary & Vocab
Embedding getMInstVector(const MachineInstr &MI) const
Computes and returns the embedding for a given machine instruction MI in the machine function MF.
virtual Embedding computeEmbeddings(const MachineInstr &MI) const =0
Function to compute the embedding for a given machine instruction.
Embedding getMBBVector(const MachineBasicBlock &MBB) const
Computes and returns the embedding for a given machine basic block in the machine function MF.
const float RegOperandWeight
MIREmbedder(const MachineFunction &MF, const MIRVocabulary &Vocab)
const float CommonOperandWeight
LLVM_ABI Embedding computeEmbeddings() const
Function to compute embeddings.
const float OpcWeight
Weight for opcode embeddings.
const MachineFunction & MF
virtual ~MIREmbedder()=default
static LLVM_ABI std::unique_ptr< MIREmbedder > create(MIR2VecKind Mode, const MachineFunction &MF, const MIRVocabulary &Vocab)
Factory method to create an Embedder object of the specified kind Returns nullptr if the requested ki...
Class for storing and accessing the MIR2Vec vocabulary.
unsigned getDimension() const
unsigned getEntityIDForOpcode(unsigned Opcode) const
Get entity ID (flat index) for an opcode This is used for triplet generation.
const_iterator end() const
LLVM_ABI unsigned getCanonicalIndexForOperandName(StringRef OperandName) const
const Embedding & operator[](MachineOperand Operand) const
LLVM_ABI unsigned getCanonicalIndexForRegisterClass(StringRef RegName, bool IsPhysical=true) const
static LLVM_ABI Expected< MIRVocabulary > create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap, VocabMap &&VirtRegMap, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI)
Factory method to create MIRVocabulary from vocabulary map.
static LLVM_ABI std::string extractBaseOpcodeName(StringRef InstrName)
Static method for extracting base opcode names (public for testing)
ir2vec::VocabStorage::const_iterator const_iterator
const_iterator begin() const
const Embedding & operator[](unsigned Opcode) const
size_t getCanonicalSize() const
Total number of entries in the vocabulary.
static LLVM_ABI Expected< MIRVocabulary > createDummyVocabForTest(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, unsigned Dim=1)
Create a dummy vocabulary for testing purposes.
unsigned getEntityIDForMachineOperand(const MachineOperand &MO) const
Get entity ID (flat index) for a machine operand This is used for triplet generation.
LLVM_ABI std::string getStringKey(unsigned Pos) const
Get the string key for a vocabulary entry at the given position.
LLVM_ABI unsigned getCanonicalIndexForBaseName(StringRef BaseName) const
Get indices from opcode or operand names.
Class for computing Symbolic embeddings Symbolic embeddings are constructed based on the entity-level...
static std::unique_ptr< SymbolicMIREmbedder > create(const MachineFunction &MF, const MIRVocabulary &Vocab)
SymbolicMIREmbedder(const MachineFunction &F, const MIRVocabulary &Vocab)
This class implements an extremely fast bulk output stream that can only output to a stream.
DenseMap< const MachineInstr *, Embedding > MachineInstEmbeddingsMap
LLVM_ABI llvm::cl::OptionCategory MIR2VecCategory
LLVM_ABI cl::opt< float > OpcWeight
LLVM_ABI cl::opt< float > RegOperandWeight
ir2vec::Embedding Embedding
DenseMap< const MachineBasicBlock *, Embedding > MachineBlockEmbeddingsMap
LLVM_ABI cl::opt< float > CommonOperandWeight
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MachineFunctionPass * createMIR2VecPrinterLegacyPass(raw_ostream &OS)
Create a machine pass that prints MIR2Vec embeddings.
Embedding is a datatype that wraps std::vector<double>.