40#ifndef LLVM_CODEGEN_MIR2VEC_H
41#define LLVM_CODEGEN_MIR2VEC_H
88 using VocabMap = std::map<std::string, ir2vec::Embedding>;
119 enum class Section :
unsigned {
128 std::set<std::string> UniqueBaseOpcodeNames;
138 "Immediate",
"CImmediate",
"FPImmediate",
"MBB",
139 "FrameIndex",
"ConstantPoolIndex",
"TargetIndex",
"JumpTableIndex",
140 "ExternalSymbol",
"GlobalAddress",
"BlockAddress",
"RegisterMask",
141 "RegisterLiveOut",
"Metadata",
"MCSymbol",
"CFIIndex",
142 "IntrinsicID",
"Predicate",
"ShuffleMask",
"LaneMask"};
144 "Common operand names size changed, update accordingly");
146 const TargetInstrInfo &
TII;
147 const TargetRegisterInfo &
TRI;
148 const MachineRegisterInfo &MRI;
150 void generateStorage(
const VocabMap &OpcodeMap,
151 const VocabMap &CommonOperandMap,
152 const VocabMap &PhyRegMap,
const VocabMap &VirtRegMap);
153 void buildCanonicalOpcodeMapping();
154 void buildRegisterOperandMapping();
157 LLVM_ABI unsigned getCanonicalOpcodeIndex(
unsigned Opcode)
const;
171 unsigned LocalIndex = getCommonOperandIndex(OperandType);
172 return Storage[
static_cast<unsigned>(Section::CommonOperands)][LocalIndex];
179 return ZeroEmbedding;
187 return ZeroEmbedding;
191 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg);
193 return ZeroEmbedding;
195 Reg.isPhysical() ? Section::PhyRegisters : Section::VirtRegisters;
196 return Storage[
static_cast<unsigned>(SectionID)][*LocalIndex];
201 unsigned getEntityIDForCommonOperand(
203 return Layout.CommonOperandBase + getCommonOperandIndex(OperandType);
208 unsigned getEntityIDForRegister(
Register Reg)
const {
209 if (!
Reg.isValid() ||
Reg.isStack())
212 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg);
215 return Layout.VirtRegBase;
217 Reg.isPhysical() ? Layout.PhyRegBase : Layout.VirtRegBase;
218 return BaseOffset + *LocalIndex;
232 bool IsPhysical =
true)
const;
242 return Layout.OpcodeBase + getCanonicalOpcodeIndex(Opcode);
249 return getEntityIDForRegister(MO.
getReg());
250 return getEntityIDForCommonOperand(MO.
getType());
255 unsigned LocalIndex = getCanonicalOpcodeIndex(Opcode);
256 return Storage[
static_cast<unsigned>(Section::Opcodes)][LocalIndex];
260 auto OperandType = Operand.
getType();
262 return operator[](Operand.
getReg());
264 return operator[](OperandType);
277 create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap,
291 MIRVocabulary(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap,
326 LLVM_ABI static std::unique_ptr<MIREmbedder>
359 static std::unique_ptr<SymbolicMIREmbedder>
374 using VocabMap = std::map<std::string, mir2vec::Embedding>;
382 Error readVocabulary(VocabMap &OpcVocab, VocabMap &CommonOperandVocab,
383 VocabMap &PhyRegVocabMap, VocabMap &VirtRegVocabMap);
389 using VocabVector = std::vector<mir2vec::Embedding>;
390 using VocabMap = std::map<std::string, mir2vec::Embedding>;
409 Provider = std::make_unique<MIR2VecVocabProvider>(MMI);
429 bool doFinalization(
Module &M)
override;
437 return "MIR2Vec Vocabulary Printer Pass";
459 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
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...
LLVM_ABI MIREmbedder(const MachineFunction &MF, const MIRVocabulary &Vocab)
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
ir2vec::Embedding Embedding
DenseMap< const MachineBasicBlock *, Embedding > MachineBlockEmbeddingsMap
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>.