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;
137 struct CommonOperandEntry {
141 static constexpr CommonOperandEntry CommonOperands[] = {
163 const TargetInstrInfo &TII;
164 const TargetRegisterInfo &TRI;
166 void generateStorage(
const VocabMap &OpcodeMap,
167 const VocabMap &CommonOperandMap,
168 const VocabMap &PhyRegMap,
const VocabMap &VirtRegMap);
169 void buildCanonicalOpcodeMapping();
170 void buildRegisterOperandMapping();
173 LLVM_ABI unsigned getCanonicalOpcodeIndex(
unsigned Opcode)
const;
184 getRegisterOperandIndex(
Register Reg,
const MachineRegisterInfo &
MRI)
const;
189 std::optional<unsigned> LocalIndex = getCommonOperandIndex(OperandType);
191 return ZeroEmbedding;
192 return Storage[
static_cast<unsigned>(Section::CommonOperands)][*LocalIndex];
196 const MachineRegisterInfo &
MRI)
const {
200 return ZeroEmbedding;
208 return ZeroEmbedding;
212 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg,
MRI);
214 return ZeroEmbedding;
216 Reg.isPhysical() ? Section::PhyRegisters : Section::VirtRegisters;
217 return Storage[
static_cast<unsigned>(SectionID)][*LocalIndex];
222 unsigned getEntityIDForCommonOperand(
224 std::optional<unsigned> LocalIndex = getCommonOperandIndex(OperandType);
225 assert(LocalIndex &&
"Operand type has no vocabulary entry");
226 return Layout.CommonOperandBase + *LocalIndex;
232 const MachineRegisterInfo &
MRI)
const {
233 if (!
Reg.isValid() ||
Reg.isStack())
236 std::optional<unsigned> LocalIndex = getRegisterOperandIndex(
Reg,
MRI);
239 return Layout.VirtRegBase;
241 Reg.isPhysical() ? Layout.PhyRegBase : Layout.VirtRegBase;
242 return BaseOffset + *LocalIndex;
256 bool IsPhysical =
true)
const;
266 return Layout.OpcodeBase + getCanonicalOpcodeIndex(Opcode);
274 return getEntityIDForRegister(MO.
getReg(),
MRI);
275 return getEntityIDForCommonOperand(MO.
getType());
280 unsigned LocalIndex = getCanonicalOpcodeIndex(Opcode);
281 return Storage[
static_cast<unsigned>(Section::Opcodes)][LocalIndex];
286 auto OperandType = Operand.
getType();
288 return getRegisterEmbedding(Operand.
getReg(),
MRI);
290 return operator[](OperandType);
303 create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap,
350 LLVM_ABI static std::unique_ptr<MIREmbedder>
383 static std::unique_ptr<SymbolicMIREmbedder>
405 using VocabMap = std::map<std::string, mir2vec::Embedding>;
424 Provider = std::make_unique<MIR2VecVocabProvider>(MMI);
444 bool doFinalization(
Module &M)
override;
452 return "MIR2Vec Vocabulary Printer Pass";
474 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.
std::map< std::string, Embedding > VocabMap
Register const TargetRegisterInfo * TRI
Promote Memory to Register
MachineRegisterInfo & MRI
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.
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.
MIR2VecPrinterPass(raw_ostream &OS)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
New pass manager analysis that provides the MIR2Vec vocabulary for a machine function.
mir2vec::MIRVocabulary Result
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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_CFIIndex
MCCFIInstruction index.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_Predicate
Generic predicate for ISel.
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_ShuffleMask
Other IR Constant for ISel (shuffle masks)
@ MO_CImmediate
Immediate >64bit operand.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_LaneMask
Mask to represent active parts of registers.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_IntrinsicID
Intrinsic ID for ISel.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
@ MO_TargetIndex
Target-dependent index+offset operand.
@ MO_Metadata
Metadata reference (for debug info)
@ MO_FPImmediate
Floating-point immediate operand.
@ MO_RegisterLiveOut
Mask of live-out registers.
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 ...
A set of analyses that are preserved following a run of a transformation pass.
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...
The instances of the Type class are immutable: once they are created, they are never changed.
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
const Embedding & getOperandEmbedding(const MachineOperand &Operand, const MachineRegisterInfo &MRI) const
LLVM_ABI unsigned getCanonicalIndexForOperandName(StringRef OperandName) const
LLVM_ABI unsigned getCanonicalIndexForRegisterClass(StringRef RegName, bool IsPhysical=true) const
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
static LLVM_ABI Expected< MIRVocabulary > createDummyVocabForTest(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, unsigned Dim=1)
Create a dummy vocabulary for testing purposes.
const Embedding & operator[](unsigned Opcode) const
unsigned getEntityIDForMachineOperand(const MachineOperand &MO, const MachineRegisterInfo &MRI) const
Get entity ID (flat index) for a machine operand This is used for triplet generation.
size_t getCanonicalSize() const
Total number of entries in the vocabulary.
static LLVM_ABI Expected< MIRVocabulary > create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap, VocabMap &&VirtRegMap, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI)
Factory method to create MIRVocabulary from vocabulary map.
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI MachineFunctionPass * createMIR2VecPrinterLegacyPass(raw_ostream &OS)
Create a machine pass that prints MIR2Vec embeddings.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
A CRTP mix-in for passes that should not be skipped.
Embedding is a datatype that wraps std::vector<double>.