Skip to content

Commit 70dcd29

Browse files
committed
Vocab Changes
1 parent 3b05edf commit 70dcd29

19 files changed

+1307
-404
lines changed

llvm/include/llvm/Analysis/FunctionPropertiesAnalysis.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class FunctionPropertiesInfo {
3434
void reIncludeBB(const BasicBlock &BB);
3535

3636
ir2vec::Embedding FunctionEmbedding = ir2vec::Embedding(0.0);
37-
std::optional<ir2vec::Vocab> IR2VecVocab;
37+
const ir2vec::Vocabulary *IR2VecVocab = nullptr;
3838

3939
public:
4040
LLVM_ABI static FunctionPropertiesInfo
4141
getFunctionPropertiesInfo(const Function &F, const DominatorTree &DT,
4242
const LoopInfo &LI,
43-
const IR2VecVocabResult *VocabResult);
43+
const ir2vec::Vocabulary *Vocabulary);
4444

4545
LLVM_ABI static FunctionPropertiesInfo
4646
getFunctionPropertiesInfo(Function &F, FunctionAnalysisManager &FAM);
@@ -145,9 +145,7 @@ class FunctionPropertiesInfo {
145145
return FunctionEmbedding;
146146
}
147147

148-
const std::optional<ir2vec::Vocab> &getIR2VecVocab() const {
149-
return IR2VecVocab;
150-
}
148+
const ir2vec::Vocabulary *getIR2VecVocab() const { return IR2VecVocab; }
151149

152150
// Helper intended to be useful for unittests
153151
void setFunctionEmbeddingForTest(const ir2vec::Embedding &Embedding) {

llvm/include/llvm/Analysis/IR2Vec.h

Lines changed: 106 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "llvm/ADT/DenseMap.h"
3333
#include "llvm/IR/PassManager.h"
34+
#include "llvm/IR/Type.h"
3435
#include "llvm/Support/CommandLine.h"
3536
#include "llvm/Support/Compiler.h"
3637
#include "llvm/Support/ErrorOr.h"
@@ -43,10 +44,10 @@ class Module;
4344
class BasicBlock;
4445
class Instruction;
4546
class Function;
46-
class Type;
4747
class Value;
4848
class raw_ostream;
4949
class LLVMContext;
50+
class IR2VecVocabAnalysis;
5051

5152
/// IR2Vec computes two kinds of embeddings: Symbolic and Flow-aware.
5253
/// Symbolic embeddings capture the "syntactic" and "statistical correlation"
@@ -128,9 +129,95 @@ struct Embedding {
128129

129130
using InstEmbeddingsMap = DenseMap<const Instruction *, Embedding>;
130131
using BBEmbeddingsMap = DenseMap<const BasicBlock *, Embedding>;
131-
// FIXME: Current the keys are strings. This can be changed to
132-
// use integers for cheaper lookups.
133-
using Vocab = std::map<std::string, Embedding>;
132+
133+
/// Class for storing and accessing the IR2Vec vocabulary.
134+
/// Encapsulates all vocabulary-related constants, logic, and access methods.
135+
class Vocabulary {
136+
friend class llvm::IR2VecVocabAnalysis;
137+
using VocabVector = std::vector<ir2vec::Embedding>;
138+
VocabVector Vocab;
139+
bool Valid = false;
140+
141+
/// Operand kinds supported by IR2Vec Vocabulary
142+
#define OPERAND_KINDS \
143+
OPERAND_KIND(FunctionID, "Function") \
144+
OPERAND_KIND(PointerID, "Pointer") \
145+
OPERAND_KIND(ConstantID, "Constant") \
146+
OPERAND_KIND(VariableID, "Variable")
147+
148+
enum class OperandKind : unsigned {
149+
#define OPERAND_KIND(Name, Str) Name,
150+
OPERAND_KINDS
151+
#undef OPERAND_KIND
152+
MaxOperandKind
153+
};
154+
155+
#undef OPERAND_KINDS
156+
157+
/// Vocabulary layout constants
158+
#define LAST_OTHER_INST(NUM) static constexpr unsigned MaxOpcodes = NUM;
159+
#include "llvm/IR/Instruction.def"
160+
#undef LAST_OTHER_INST
161+
162+
static constexpr unsigned MaxTypeIDs = Type::TypeID::TargetExtTyID + 1;
163+
static constexpr unsigned MaxOperandKinds =
164+
static_cast<unsigned>(OperandKind::MaxOperandKind);
165+
166+
/// Helper function to get vocabulary key for a given OperandKind
167+
static StringRef getVocabKeyForOperandKind(OperandKind Kind);
168+
169+
/// Helper function to classify an operand into OperandKind
170+
static OperandKind getOperandKind(const Value *Op);
171+
172+
/// Helper function to get vocabulary key for a given TypeID
173+
static StringRef getVocabKeyForTypeID(Type::TypeID TypeID);
174+
175+
public:
176+
Vocabulary() = default;
177+
Vocabulary(VocabVector &&Vocab);
178+
179+
bool isValid() const;
180+
unsigned getDimension() const;
181+
size_t size() const;
182+
183+
/// Accessors to get the embedding for a given entity.
184+
const ir2vec::Embedding &operator[](unsigned Opcode) const;
185+
const ir2vec::Embedding &operator[](Type::TypeID TypeId) const;
186+
const ir2vec::Embedding &operator[](const Value *Arg) const;
187+
188+
/// Const Iterator type aliases
189+
using const_iterator = VocabVector::const_iterator;
190+
const_iterator begin() const {
191+
assert(Valid && "IR2Vec Vocabulary is invalid");
192+
return Vocab.begin();
193+
}
194+
195+
const_iterator cbegin() const {
196+
assert(Valid && "IR2Vec Vocabulary is invalid");
197+
return Vocab.cbegin();
198+
}
199+
200+
const_iterator end() const {
201+
assert(Valid && "IR2Vec Vocabulary is invalid");
202+
return Vocab.end();
203+
}
204+
205+
const_iterator cend() const {
206+
assert(Valid && "IR2Vec Vocabulary is invalid");
207+
return Vocab.cend();
208+
}
209+
210+
/// Returns the string key for a given index position in the vocabulary.
211+
/// This is useful for debugging or printing the vocabulary. Do not use this
212+
/// for embedding generation as string based lookups are inefficient.
213+
static StringRef getStringKey(unsigned Pos);
214+
215+
/// Create a dummy vocabulary for testing purposes.
216+
static VocabVector createDummyVocabForTest(unsigned Dim = 1);
217+
218+
bool invalidate(Module &M, const PreservedAnalyses &PA,
219+
ModuleAnalysisManager::Invalidator &Inv) const;
220+
};
134221

135222
/// Embedder provides the interface to generate embeddings (vector
136223
/// representations) for instructions, basic blocks, and functions. The
@@ -141,7 +228,7 @@ using Vocab = std::map<std::string, Embedding>;
141228
class Embedder {
142229
protected:
143230
const Function &F;
144-
const Vocab &Vocabulary;
231+
const Vocabulary &Vocab;
145232

146233
/// Dimension of the vector representation; captured from the input vocabulary
147234
const unsigned Dimension;
@@ -156,7 +243,7 @@ class Embedder {
156243
mutable BBEmbeddingsMap BBVecMap;
157244
mutable InstEmbeddingsMap InstVecMap;
158245

159-
LLVM_ABI Embedder(const Function &F, const Vocab &Vocabulary);
246+
LLVM_ABI Embedder(const Function &F, const Vocabulary &Vocab);
160247

161248
/// Helper function to compute embeddings. It generates embeddings for all
162249
/// the instructions and basic blocks in the function F. Logic of computing
@@ -167,16 +254,12 @@ class Embedder {
167254
/// Specific to the kind of embeddings being computed.
168255
virtual void computeEmbeddings(const BasicBlock &BB) const = 0;
169256

170-
/// Lookup vocabulary for a given Key. If the key is not found, it returns a
171-
/// zero vector.
172-
LLVM_ABI Embedding lookupVocab(const std::string &Key) const;
173-
174257
public:
175258
virtual ~Embedder() = default;
176259

177260
/// Factory method to create an Embedder object.
178261
LLVM_ABI static std::unique_ptr<Embedder>
179-
create(IR2VecKind Mode, const Function &F, const Vocab &Vocabulary);
262+
create(IR2VecKind Mode, const Function &F, const Vocabulary &Vocab);
180263

181264
/// Returns a map containing instructions and the corresponding embeddings for
182265
/// the function F if it has been computed. If not, it computes the embeddings
@@ -202,56 +285,39 @@ class Embedder {
202285
/// representations obtained from the Vocabulary.
203286
class LLVM_ABI SymbolicEmbedder : public Embedder {
204287
private:
205-
/// Utility function to compute the embedding for a given type.
206-
Embedding getTypeEmbedding(const Type *Ty) const;
207-
208-
/// Utility function to compute the embedding for a given operand.
209-
Embedding getOperandEmbedding(const Value *Op) const;
210-
211288
void computeEmbeddings() const override;
212289
void computeEmbeddings(const BasicBlock &BB) const override;
213290

214291
public:
215-
SymbolicEmbedder(const Function &F, const Vocab &Vocabulary)
216-
: Embedder(F, Vocabulary) {
292+
SymbolicEmbedder(const Function &F, const Vocabulary &Vocab)
293+
: Embedder(F, Vocab) {
217294
FuncVector = Embedding(Dimension, 0);
218295
}
219296
};
220297

221298
} // namespace ir2vec
222299

223-
/// Class for storing the result of the IR2VecVocabAnalysis.
224-
class IR2VecVocabResult {
225-
ir2vec::Vocab Vocabulary;
226-
bool Valid = false;
227-
228-
public:
229-
IR2VecVocabResult() = default;
230-
LLVM_ABI IR2VecVocabResult(ir2vec::Vocab &&Vocabulary);
231-
232-
bool isValid() const { return Valid; }
233-
LLVM_ABI const ir2vec::Vocab &getVocabulary() const;
234-
LLVM_ABI unsigned getDimension() const;
235-
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA,
236-
ModuleAnalysisManager::Invalidator &Inv) const;
237-
};
238-
239300
/// This analysis provides the vocabulary for IR2Vec. The vocabulary provides a
240301
/// mapping between an entity of the IR (like opcode, type, argument, etc.) and
241302
/// its corresponding embedding.
242303
class IR2VecVocabAnalysis : public AnalysisInfoMixin<IR2VecVocabAnalysis> {
243-
ir2vec::Vocab Vocabulary;
304+
using VocabVector = std::vector<ir2vec::Embedding>;
305+
using VocabMap = std::map<std::string, ir2vec::Embedding>;
306+
VocabMap OpcVocab, TypeVocab, ArgVocab;
307+
VocabVector Vocab;
308+
244309
Error readVocabulary();
245310
Error parseVocabSection(StringRef Key, const json::Value &ParsedVocabValue,
246-
ir2vec::Vocab &TargetVocab, unsigned &Dim);
311+
VocabMap &TargetVocab, unsigned &Dim);
312+
void generateNumMappedVocab();
247313
void emitError(Error Err, LLVMContext &Ctx);
248314

249315
public:
250316
LLVM_ABI static AnalysisKey Key;
251317
IR2VecVocabAnalysis() = default;
252-
LLVM_ABI explicit IR2VecVocabAnalysis(const ir2vec::Vocab &Vocab);
253-
LLVM_ABI explicit IR2VecVocabAnalysis(ir2vec::Vocab &&Vocab);
254-
using Result = IR2VecVocabResult;
318+
LLVM_ABI explicit IR2VecVocabAnalysis(const VocabVector &Vocab);
319+
LLVM_ABI explicit IR2VecVocabAnalysis(VocabVector &&Vocab);
320+
using Result = ir2vec::Vocabulary;
255321
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &MAM);
256322
};
257323

llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,20 @@ FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
242242
// We use the cached result of the IR2VecVocabAnalysis run by
243243
// InlineAdvisorAnalysis. If the IR2VecVocabAnalysis is not run, we don't
244244
// use IR2Vec embeddings.
245-
auto VocabResult = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
246-
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
245+
auto Vocabulary = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
246+
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
247247
return getFunctionPropertiesInfo(F, FAM.getResult<DominatorTreeAnalysis>(F),
248-
FAM.getResult<LoopAnalysis>(F), VocabResult);
248+
FAM.getResult<LoopAnalysis>(F), Vocabulary);
249249
}
250250

251251
FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
252252
const Function &F, const DominatorTree &DT, const LoopInfo &LI,
253-
const IR2VecVocabResult *VocabResult) {
253+
const ir2vec::Vocabulary *Vocabulary) {
254254

255255
FunctionPropertiesInfo FPI;
256-
if (VocabResult && VocabResult->isValid()) {
257-
FPI.IR2VecVocab = VocabResult->getVocabulary();
258-
FPI.FunctionEmbedding = ir2vec::Embedding(VocabResult->getDimension(), 0.0);
256+
if (Vocabulary && Vocabulary->isValid()) {
257+
FPI.IR2VecVocab = Vocabulary;
258+
FPI.FunctionEmbedding = ir2vec::Embedding(Vocabulary->getDimension(), 0.0);
259259
}
260260
for (const auto &BB : F)
261261
if (DT.isReachableFromEntry(&BB))
@@ -588,9 +588,9 @@ bool FunctionPropertiesUpdater::isUpdateValid(Function &F,
588588
return false;
589589
DominatorTree DT(F);
590590
LoopInfo LI(DT);
591-
auto VocabResult = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
592-
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
591+
auto Vocabulary = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
592+
.getCachedResult<IR2VecVocabAnalysis>(*F.getParent());
593593
auto Fresh =
594-
FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI, VocabResult);
594+
FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI, Vocabulary);
595595
return FPI == Fresh;
596596
}

0 commit comments

Comments
 (0)