29#ifndef LLVM_ANALYSIS_IR2VEC_H
30#define LLVM_ANALYSIS_IR2VEC_H
51class IR2VecVocabAnalysis;
81 std::vector<double> Data;
85 Embedding(
const std::vector<double> &V) : Data(V) {}
87 Embedding(std::initializer_list<double> IL) : Data(IL) {}
92 size_t size()
const {
return Data.size(); }
93 bool empty()
const {
return Data.empty(); }
96 assert(Itr < Data.size() &&
"Index out of bounds");
101 assert(Itr < Data.size() &&
"Index out of bounds");
105 using iterator =
typename std::vector<double>::iterator;
115 const std::vector<double> &
getData()
const {
return Data; }
132 double Tolerance = 1e-4)
const;
159 using VocabVector = std::vector<ir2vec::Embedding>;
196#define LAST_OTHER_INST(NUM) static constexpr unsigned MaxOpcodes = NUM;
197#include "llvm/IR/Instruction.def"
198#undef LAST_OTHER_INST
200 static constexpr unsigned MaxTypeIDs = Type::TypeID::TargetExtTyID + 1;
240 assert(Valid &&
"IR2Vec Vocabulary is invalid");
241 return Vocab.begin();
245 assert(Valid &&
"IR2Vec Vocabulary is invalid");
246 return Vocab.cbegin();
250 assert(Valid &&
"IR2Vec Vocabulary is invalid");
255 assert(Valid &&
"IR2Vec Vocabulary is invalid");
271 constexpr static unsigned NumCanonicalEntries =
276 "FloatTy",
"VoidTy",
"LabelTy",
"MetadataTy",
277 "VectorTy",
"TokenTy",
"IntegerTy",
"FunctionTy",
278 "PointerTy",
"StructTy",
"ArrayTy",
"UnknownTy"};
279 static_assert(std::size(CanonicalTypeNames) ==
281 "CanonicalTypeNames array size must match MaxCanonicalType");
284 static constexpr StringLiteral OperandKindNames[] = {
"Function",
"Pointer",
285 "Constant",
"Variable"};
286 static_assert(std::size(OperandKindNames) ==
288 "OperandKindNames array size must match MaxOperandKind");
292 static constexpr std::array<CanonicalTypeID, MaxTypeIDs> TypeIDMapping = {{
315 static_assert(TypeIDMapping.size() ==
MaxTypeIDs,
316 "TypeIDMapping must cover all Type::TypeID values");
364 LLVM_ABI static std::unique_ptr<Embedder>
391 void computeEmbeddings(
const BasicBlock &BB)
const override;
403 void computeEmbeddings(
const BasicBlock &BB)
const override;
416 using VocabVector = std::vector<ir2vec::Embedding>;
417 using VocabMap = std::map<std::string, ir2vec::Embedding>;
418 VocabMap OpcVocab, TypeVocab, ArgVocab;
421 Error readVocabulary();
423 VocabMap &TargetVocab,
unsigned &Dim);
424 void generateNumMappedVocab();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
Provides ErrorOr<T> smart pointer.
This header defines various interfaces for pass management in LLVM.
This file supports working with JSON data.
Machine Check Debug Module
ModuleAnalysisManager MAM
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")))
API to communicate dependencies between analyses during invalidation.
A container for analyses that lazily runs them and caches their results.
LLVM Basic Block Representation.
This class represents an Operation in the Expression.
Lightweight error class with error context and mandatory checking.
This pass prints the IR2Vec embeddings for instructions, basic blocks, and functions.
IR2VecPrinterPass(raw_ostream &OS)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
This analysis provides the vocabulary for IR2Vec.
IR2VecVocabAnalysis()=default
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &MAM)
static LLVM_ABI AnalysisKey Key
This pass prints the embeddings in the vocabulary.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
IR2VecVocabPrinterPass(raw_ostream &OS)
This is an important class for using LLVM in a threaded context.
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
TypeID
Definitions of all of the base types for the Type system.
LLVM Value Representation.
Embedder provides the interface to generate embeddings (vector representations) for instructions,...
LLVM_ABI const Embedding & getBBVector(const BasicBlock &BB) const
Returns the embedding for a given basic block in the function F if it has been computed.
static LLVM_ABI std::unique_ptr< Embedder > create(IR2VecKind Mode, const Function &F, const Vocabulary &Vocab)
Factory method to create an Embedder object.
LLVM_ABI const BBEmbeddingsMap & getBBVecMap() const
Returns a map containing basic block and the corresponding embeddings for the function F if it has be...
void computeEmbeddings() const
Function to compute embeddings.
virtual ~Embedder()=default
LLVM_ABI const InstEmbeddingsMap & getInstVecMap() const
Returns a map containing instructions and the corresponding embeddings for the function F if it has b...
const float OpcWeight
Weights for different entities (like opcode, arguments, types) in the IR instructions to generate the...
const unsigned Dimension
Dimension of the vector representation; captured from the input vocabulary.
virtual void computeEmbeddings(const BasicBlock &BB) const =0
Function to compute the embedding for a given basic block.
LLVM_ABI const Embedding & getFunctionVector() const
Computes and returns the embedding for the current function.
InstEmbeddingsMap InstVecMap
Class for computing the Flow-aware embeddings of IR2Vec.
FlowAwareEmbedder(const Function &F, const Vocabulary &Vocab)
Class for computing the Symbolic embeddings of IR2Vec.
SymbolicEmbedder(const Function &F, const Vocabulary &Vocab)
Class for storing and accessing the IR2Vec vocabulary.
static LLVM_ABI unsigned getSlotIndex(unsigned Opcode)
Functions to return the slot index or position of a given Opcode, TypeID, or OperandKind in the vocab...
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv) const
LLVM_ABI const ir2vec::Embedding & operator[](unsigned Opcode) const
Accessors to get the embedding for a given entity.
const_iterator begin() const
static LLVM_ABI OperandKind getOperandKind(const Value *Op)
Function to classify an operand into OperandKind.
LLVM_ABI bool isValid() const
VocabVector::const_iterator const_iterator
Const Iterator type aliases.
static LLVM_ABI StringRef getStringKey(unsigned Pos)
Returns the string key for a given index position in the vocabulary.
static constexpr unsigned MaxCanonicalTypeIDs
static LLVM_ABI VocabVector createDummyVocabForTest(unsigned Dim=1)
Create a dummy vocabulary for testing purposes.
static constexpr unsigned MaxOperandKinds
const_iterator cbegin() const
OperandKind
Operand kinds supported by IR2Vec Vocabulary.
static constexpr size_t getCanonicalSize()
Total number of entries (opcodes + canonicalized types + operand kinds)
static constexpr unsigned MaxTypeIDs
static LLVM_ABI StringRef getVocabKeyForTypeID(Type::TypeID TypeID)
Function to get vocabulary key for a given TypeID.
const_iterator end() const
static LLVM_ABI StringRef getVocabKeyForOpcode(unsigned Opcode)
Function to get vocabulary key for a given Opcode.
const_iterator cend() const
LLVM_ABI unsigned getDimension() const
CanonicalTypeID
Canonical type IDs supported by IR2Vec Vocabulary.
static LLVM_ABI StringRef getVocabKeyForOperandKind(OperandKind Kind)
Function to get vocabulary key for a given OperandKind.
A Value is an JSON value of unknown type.
This class implements an extremely fast bulk output stream that can only output to a stream.
@ BasicBlock
Various leaf nodes.
LLVM_ABI cl::opt< float > ArgWeight
LLVM_ABI cl::opt< float > OpcWeight
LLVM_ABI cl::opt< float > TypeWeight
LLVM_ABI cl::opt< IR2VecKind > IR2VecEmbeddingKind
llvm::cl::OptionCategory IR2VecCategory
This is an optimization pass for GlobalISel generic memory operations.
IR2VecKind
IR2Vec computes two kinds of embeddings: Symbolic and Flow-aware.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
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 to automatically provide informational APIs needed for passes.
Embedding is a datatype that wraps std::vector<double>.
const_iterator end() const
LLVM_ABI bool approximatelyEquals(const Embedding &RHS, double Tolerance=1e-4) const
Returns true if the embedding is approximately equal to the RHS embedding within the specified tolera...
const_iterator cbegin() const
LLVM_ABI Embedding & operator+=(const Embedding &RHS)
Arithmetic operators.
LLVM_ABI Embedding operator-(const Embedding &RHS) const
const std::vector< double > & getData() const
typename std::vector< double >::const_iterator const_iterator
Embedding(size_t Size, double InitialValue)
LLVM_ABI Embedding & operator-=(const Embedding &RHS)
const_iterator cend() const
LLVM_ABI Embedding operator*(double Factor) const
LLVM_ABI Embedding & operator*=(double Factor)
Embedding(std::initializer_list< double > IL)
Embedding(const std::vector< double > &V)
LLVM_ABI Embedding operator+(const Embedding &RHS) const
typename std::vector< double >::iterator iterator
LLVM_ABI Embedding & scaleAndAdd(const Embedding &Src, float Factor)
Adds Src Embedding scaled by Factor with the called Embedding.
Embedding(std::vector< double > &&V)
const double & operator[](size_t Itr) const
LLVM_ABI void print(raw_ostream &OS) const
const_iterator begin() const
double & operator[](size_t Itr)