1//===- IR2Vec.cpp - Implementation of IR2Vec -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4// Exceptions. See the LICENSE file for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the IR2Vec algorithm.
11///
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/IR2Vec.h"
15#include "AnalysisOptions.h"
16
17#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/Sequence.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/IR/CFG.h"
21#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/PassManager.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/Errc.h"
26#include "llvm/Support/Error.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/Format.h"
29#include "llvm/Support/MemoryBuffer.h"
30
31using namespace llvm;
32using namespace ir2vec;
33
34#define DEBUG_TYPE "ir2vec"
35
36STATISTIC(VocabMissCounter,
37 "Number of lookups to entities not present in the vocabulary");
38
39StringRef llvm::ir2vec::getVocabFile(const LLVMContext &C) {
40 return getAnalysisOptions(C).ir2vec_vocab_path;
41}
42IR2VecKind llvm::ir2vec::getEmbeddingKind(const LLVMContext &C) {
43 return getAnalysisOptions(C).ir2vec_kind;
44}
45
46AnalysisKey IR2VecVocabAnalysis::Key;
47
48// ==----------------------------------------------------------------------===//
49// Local helper functions
50//===----------------------------------------------------------------------===//
51namespace llvm::json {
52inline bool fromJSON(const llvm::json::Value &E, Embedding &Out,
53 llvm::json::Path P) {
54 std::vector<double> TempOut;
55 if (!llvm::json::fromJSON(E, Out&: TempOut, P))
56 return false;
57 Out = Embedding(std::move(TempOut));
58 return true;
59}
60} // namespace llvm::json
61
62// ==----------------------------------------------------------------------===//
63// Embedding
64//===----------------------------------------------------------------------===//
65Embedding &Embedding::operator+=(const Embedding &RHS) {
66 assert(this->size() == RHS.size() && "Vectors must have the same dimension");
67 std::transform(first1: this->begin(), last1: this->end(), first2: RHS.begin(), result: this->begin(),
68 binary_op: std::plus<double>());
69 return *this;
70}
71
72Embedding Embedding::operator+(const Embedding &RHS) const {
73 Embedding Result(*this);
74 Result += RHS;
75 return Result;
76}
77
78Embedding &Embedding::operator-=(const Embedding &RHS) {
79 assert(this->size() == RHS.size() && "Vectors must have the same dimension");
80 std::transform(first1: this->begin(), last1: this->end(), first2: RHS.begin(), result: this->begin(),
81 binary_op: std::minus<double>());
82 return *this;
83}
84
85Embedding Embedding::operator-(const Embedding &RHS) const {
86 Embedding Result(*this);
87 Result -= RHS;
88 return Result;
89}
90
91Embedding &Embedding::operator*=(double Factor) {
92 std::transform(first: this->begin(), last: this->end(), result: this->begin(),
93 unary_op: [Factor](double Elem) { return Elem * Factor; });
94 return *this;
95}
96
97Embedding Embedding::operator*(double Factor) const {
98 Embedding Result(*this);
99 Result *= Factor;
100 return Result;
101}
102
103Embedding &Embedding::scaleAndAdd(const Embedding &Src, float Factor) {
104 assert(this->size() == Src.size() && "Vectors must have the same dimension");
105 for (size_t Itr = 0; Itr < this->size(); ++Itr)
106 (*this)[Itr] += Src[Itr] * Factor;
107 return *this;
108}
109
110bool Embedding::approximatelyEquals(const Embedding &RHS,
111 double Tolerance) const {
112 assert(this->size() == RHS.size() && "Vectors must have the same dimension");
113 for (size_t Itr = 0; Itr < this->size(); ++Itr)
114 if (std::abs(x: (*this)[Itr] - RHS[Itr]) > Tolerance) {
115 LLVM_DEBUG(errs() << "Embedding mismatch at index " << Itr << ": "
116 << (*this)[Itr] << " vs " << RHS[Itr]
117 << "; Tolerance: " << Tolerance << "\n");
118 return false;
119 }
120 return true;
121}
122
123void Embedding::print(raw_ostream &OS) const {
124 OS << " [";
125 for (const auto &Elem : Data)
126 OS << " " << format(Fmt: "%.2f", Vals: Elem) << " ";
127 OS << "]\n";
128}
129
130// ==----------------------------------------------------------------------===//
131// Embedder and its subclasses
132//===----------------------------------------------------------------------===//
133
134std::unique_ptr<Embedder> Embedder::create(IR2VecKind Mode, const Function &F,
135 const Vocabulary &Vocab) {
136 switch (Mode) {
137 case IR2VecKind::Symbolic:
138 return std::make_unique<SymbolicEmbedder>(args: F, args: Vocab);
139 case IR2VecKind::FlowAware:
140 return std::make_unique<FlowAwareEmbedder>(args: F, args: Vocab);
141 }
142 return nullptr;
143}
144
145Embedding Embedder::computeEmbeddings() const {
146 Embedding FuncVector(Dimension, 0.0);
147
148 if (F.isDeclaration())
149 return FuncVector;
150
151 // Consider only the basic blocks that are reachable from entry
152 for (const BasicBlock *BB : depth_first(G: &F))
153 FuncVector += computeEmbeddings(BB: *BB);
154 return FuncVector;
155}
156
157Embedding Embedder::computeEmbeddings(const BasicBlock &BB) const {
158 Embedding BBVector(Dimension, 0);
159
160 // We consider only the non-pseudo instructions
161 for (const auto &I : BB)
162 if (!isa<PseudoProbeInst>(Val: I))
163 BBVector += computeEmbeddings(I);
164 return BBVector;
165}
166
167Embedding SymbolicEmbedder::computeEmbeddings(const Instruction &I) const {
168 // Currently, we always (re)compute the embeddings for symbolic embedder.
169 // This is cheaper than caching the vectors.
170 Embedding ArgEmb(Dimension, 0);
171 for (const auto &Op : I.operands())
172 ArgEmb += Vocab[*Op];
173 auto InstVector =
174 Vocab[I.getOpcode()] + Vocab[I.getType()->getTypeID()] + ArgEmb;
175 if (const auto *IC = dyn_cast<CmpInst>(Val: &I))
176 InstVector += Vocab[IC->getPredicate()];
177 return InstVector;
178}
179
180Embedding FlowAwareEmbedder::computeEmbeddings(const Instruction &I) const {
181 // If we have already computed the embedding for this instruction, return it
182 auto It = InstVecMap.find(Val: &I);
183 if (It != InstVecMap.end())
184 return It->second;
185
186 // TODO: Handle call instructions differently.
187 // For now, we treat them like other instructions
188 Embedding ArgEmb(Dimension, 0);
189 for (const auto &Op : I.operands()) {
190 // If the operand is defined elsewhere, we use its embedding
191 if (const auto *DefInst = dyn_cast<Instruction>(Val: Op)) {
192 auto DefIt = InstVecMap.find(Val: DefInst);
193 // Fixme (#159171): Ideally we should never miss an instruction
194 // embedding here.
195 // But when we have cyclic dependencies (e.g., phi
196 // nodes), we might miss the embedding. In such cases, we fall back to
197 // using the vocabulary embedding. This can be fixed by iterating to a
198 // fixed-point, or by using a simple solver for the set of simultaneous
199 // equations.
200 // Another case when we might miss an instruction embedding is when
201 // the operand instruction is in a different basic block that has not
202 // been processed yet. This can be fixed by processing the basic blocks
203 // in a topological order.
204 if (DefIt != InstVecMap.end())
205 ArgEmb += DefIt->second;
206 else
207 ArgEmb += Vocab[*Op];
208 }
209 // If the operand is not defined by an instruction, we use the
210 // vocabulary
211 else {
212 LLVM_DEBUG(errs() << "Using embedding from vocabulary for operand: "
213 << *Op << "=" << Vocab[*Op][0] << "\n");
214 ArgEmb += Vocab[*Op];
215 }
216 }
217 // Create the instruction vector by combining opcode, type, and arguments
218 // embeddings
219 auto InstVector =
220 Vocab[I.getOpcode()] + Vocab[I.getType()->getTypeID()] + ArgEmb;
221 if (const auto *IC = dyn_cast<CmpInst>(Val: &I))
222 InstVector += Vocab[IC->getPredicate()];
223 InstVecMap[&I] = InstVector;
224 return InstVector;
225}
226
227// ==----------------------------------------------------------------------===//
228// VocabStorage
229//===----------------------------------------------------------------------===//
230
231VocabStorage::VocabStorage(std::vector<std::vector<Embedding>> &&SectionData)
232 : Sections(std::move(SectionData)), TotalSize([&] {
233 assert(!Sections.empty() && "Vocabulary has no sections");
234 // Compute total size across all sections
235 size_t Size = 0;
236 for (const auto &Section : Sections) {
237 assert(!Section.empty() && "Vocabulary section is empty");
238 Size += Section.size();
239 }
240 return Size;
241 }()),
242 Dimension([&] {
243 // Get dimension from the first embedding in the first section - all
244 // embeddings must have the same dimension
245 assert(!Sections.empty() && "Vocabulary has no sections");
246 assert(!Sections[0].empty() && "First section of vocabulary is empty");
247 unsigned ExpectedDim = static_cast<unsigned>(Sections[0][0].size());
248
249 // Verify that all embeddings across all sections have the same
250 // dimension
251 [[maybe_unused]] auto allSameDim =
252 [ExpectedDim](const std::vector<Embedding> &Section) {
253 return std::all_of(first: Section.begin(), last: Section.end(),
254 pred: [ExpectedDim](const Embedding &Emb) {
255 return Emb.size() == ExpectedDim;
256 });
257 };
258 assert(std::all_of(Sections.begin(), Sections.end(), allSameDim) &&
259 "All embeddings must have the same dimension");
260
261 return ExpectedDim;
262 }()) {}
263
264const Embedding &VocabStorage::const_iterator::operator*() const {
265 assert(SectionId < Storage->Sections.size() && "Invalid section ID");
266 assert(LocalIndex < Storage->Sections[SectionId].size() &&
267 "Local index out of range");
268 return Storage->Sections[SectionId][LocalIndex];
269}
270
271VocabStorage::const_iterator &VocabStorage::const_iterator::operator++() {
272 ++LocalIndex;
273 // Check if we need to move to the next section
274 if (SectionId < Storage->getNumSections() &&
275 LocalIndex >= Storage->Sections[SectionId].size()) {
276 assert(LocalIndex == Storage->Sections[SectionId].size() &&
277 "Local index should be at the end of the current section");
278 LocalIndex = 0;
279 ++SectionId;
280 }
281 return *this;
282}
283
284bool VocabStorage::const_iterator::operator==(
285 const const_iterator &Other) const {
286 return Storage == Other.Storage && SectionId == Other.SectionId &&
287 LocalIndex == Other.LocalIndex;
288}
289
290bool VocabStorage::const_iterator::operator!=(
291 const const_iterator &Other) const {
292 return !(*this == Other);
293}
294
295Error VocabStorage::parseVocabSection(StringRef Key,
296 const json::Value &ParsedVocabValue,
297 VocabMap &TargetVocab, unsigned &Dim) {
298 json::Path::Root Path("");
299 const json::Object *RootObj = ParsedVocabValue.getAsObject();
300 if (!RootObj)
301 return createStringError(EC: errc::invalid_argument,
302 S: "JSON root is not an object");
303
304 const json::Value *SectionValue = RootObj->get(K: Key);
305 if (!SectionValue)
306 return createStringError(EC: errc::invalid_argument,
307 S: "Missing '" + std::string(Key) +
308 "' section in vocabulary file");
309 if (!json::fromJSON(E: *SectionValue, Out&: TargetVocab, P: Path))
310 return createStringError(EC: errc::illegal_byte_sequence,
311 S: "Unable to parse '" + std::string(Key) +
312 "' section from vocabulary");
313
314 Dim = TargetVocab.begin()->second.size();
315 if (Dim == 0)
316 return createStringError(EC: errc::illegal_byte_sequence,
317 S: "Dimension of '" + std::string(Key) +
318 "' section of the vocabulary is zero");
319
320 if (!std::all_of(first: TargetVocab.begin(), last: TargetVocab.end(),
321 pred: [Dim](const std::pair<StringRef, Embedding> &Entry) {
322 return Entry.second.size() == Dim;
323 }))
324 return createStringError(
325 EC: errc::illegal_byte_sequence,
326 S: "All vectors in the '" + std::string(Key) +
327 "' section of the vocabulary are not of the same dimension");
328
329 return Error::success();
330}
331
332// ==----------------------------------------------------------------------===//
333// Vocabulary
334//===----------------------------------------------------------------------===//
335
336StringRef Vocabulary::getVocabKeyForOpcode(unsigned Opcode) {
337 assert(Opcode >= 1 && Opcode <= MaxOpcodes && "Invalid opcode");
338#define HANDLE_INST(NUM, OPCODE, CLASS) \
339 if (Opcode == NUM) { \
340 return #OPCODE; \
341 }
342#include "llvm/IR/Instruction.def"
343#undef HANDLE_INST
344 return "UnknownOpcode";
345}
346
347// Helper function to classify an operand into OperandKind
348Vocabulary::OperandKind Vocabulary::getOperandKind(const Value *Op) {
349 if (isa<Function>(Val: Op))
350 return OperandKind::FunctionID;
351 if (isa<PointerType>(Val: Op->getType()))
352 return OperandKind::PointerID;
353 if (isa<Constant>(Val: Op))
354 return OperandKind::ConstantID;
355 if (isa<BasicBlock>(Val: Op))
356 return OperandKind::LabelID;
357 return OperandKind::VariableID;
358}
359
360unsigned Vocabulary::getPredicateLocalIndex(CmpInst::Predicate P) {
361 if (P >= CmpInst::FIRST_FCMP_PREDICATE && P <= CmpInst::LAST_FCMP_PREDICATE)
362 return P - CmpInst::FIRST_FCMP_PREDICATE;
363 else
364 return P - CmpInst::FIRST_ICMP_PREDICATE +
365 (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE + 1);
366}
367
368CmpInst::Predicate Vocabulary::getPredicateFromLocalIndex(unsigned LocalIndex) {
369 unsigned fcmpRange =
370 CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE + 1;
371 if (LocalIndex < fcmpRange)
372 return static_cast<CmpInst::Predicate>(CmpInst::FIRST_FCMP_PREDICATE +
373 LocalIndex);
374 else
375 return static_cast<CmpInst::Predicate>(CmpInst::FIRST_ICMP_PREDICATE +
376 LocalIndex - fcmpRange);
377}
378
379StringRef Vocabulary::getVocabKeyForPredicate(CmpInst::Predicate Pred) {
380 static SmallString<16> PredNameBuffer;
381 if (Pred < CmpInst::FIRST_ICMP_PREDICATE)
382 PredNameBuffer = "FCMP_";
383 else
384 PredNameBuffer = "ICMP_";
385 PredNameBuffer += CmpInst::getPredicateName(P: Pred);
386 return PredNameBuffer;
387}
388
389StringRef Vocabulary::getStringKey(unsigned Pos) {
390 assert(Pos < NumCanonicalEntries && "Position out of bounds in vocabulary");
391 // Opcode
392 if (Pos < MaxOpcodes)
393 return getVocabKeyForOpcode(Opcode: Pos + 1);
394 // Type
395 if (Pos < OperandBaseOffset)
396 return getVocabKeyForCanonicalTypeID(
397 CType: static_cast<CanonicalTypeID>(Pos - MaxOpcodes));
398 // Operand
399 if (Pos < PredicateBaseOffset)
400 return getVocabKeyForOperandKind(
401 Kind: static_cast<OperandKind>(Pos - OperandBaseOffset));
402 // Predicates
403 return getVocabKeyForPredicate(Pred: getPredicate(Index: Pos - PredicateBaseOffset));
404}
405
406// For now, assume vocabulary is stable unless explicitly invalidated.
407bool Vocabulary::invalidate(Module &M, const PreservedAnalyses &PA,
408 ModuleAnalysisManager::Invalidator &Inv) const {
409 auto PAC = PA.getChecker<IR2VecVocabAnalysis>();
410 return !(PAC.preservedWhenStateless());
411}
412
413VocabStorage Vocabulary::createDummyVocabForTest(unsigned Dim) {
414 float DummyVal = 0.1f;
415
416 // Create sections for opcodes, types, operands, and predicates
417 // Order must match Vocabulary::Section enum
418 std::vector<std::vector<Embedding>> Sections;
419 Sections.reserve(n: static_cast<unsigned>(Section::MaxSections));
420
421 // Opcodes section
422 std::vector<Embedding> OpcodeSec;
423 OpcodeSec.reserve(n: MaxOpcodes);
424 for (unsigned I = 0; I < MaxOpcodes; ++I) {
425 OpcodeSec.emplace_back(args&: Dim, args&: DummyVal);
426 DummyVal += 0.1f;
427 }
428 Sections.push_back(x: std::move(OpcodeSec));
429
430 // Types section
431 std::vector<Embedding> TypeSec;
432 TypeSec.reserve(n: MaxCanonicalTypeIDs);
433 for (unsigned I = 0; I < MaxCanonicalTypeIDs; ++I) {
434 TypeSec.emplace_back(args&: Dim, args&: DummyVal);
435 DummyVal += 0.1f;
436 }
437 Sections.push_back(x: std::move(TypeSec));
438
439 // Operands section
440 std::vector<Embedding> OperandSec;
441 OperandSec.reserve(n: MaxOperandKinds);
442 for (unsigned I = 0; I < MaxOperandKinds; ++I) {
443 OperandSec.emplace_back(args&: Dim, args&: DummyVal);
444 DummyVal += 0.1f;
445 }
446 Sections.push_back(x: std::move(OperandSec));
447
448 // Predicates section
449 std::vector<Embedding> PredicateSec;
450 PredicateSec.reserve(n: MaxPredicateKinds);
451 for (unsigned I = 0; I < MaxPredicateKinds; ++I) {
452 PredicateSec.emplace_back(args&: Dim, args&: DummyVal);
453 DummyVal += 0.1f;
454 }
455 Sections.push_back(x: std::move(PredicateSec));
456
457 return VocabStorage(std::move(Sections));
458}
459
460namespace {
461using VocabMap = std::map<std::string, Embedding>;
462
463/// Read vocabulary JSON file and populate the section maps.
464Error readVocabularyFromFile(StringRef VocabFilePath, VocabMap &OpcVocab,
465 VocabMap &TypeVocab, VocabMap &ArgVocab) {
466 auto BufOrError =
467 MemoryBuffer::getFileOrSTDIN(Filename: VocabFilePath, /*IsText=*/true);
468 if (!BufOrError)
469 return createFileError(F: VocabFilePath, EC: BufOrError.getError());
470
471 auto Content = BufOrError.get()->getBuffer();
472
473 Expected<json::Value> ParsedVocabValue = json::parse(JSON: Content);
474 if (!ParsedVocabValue)
475 return ParsedVocabValue.takeError();
476
477 unsigned OpcodeDim = 0, TypeDim = 0, ArgDim = 0;
478 if (auto Err = VocabStorage::parseVocabSection(Key: "Opcodes", ParsedVocabValue: *ParsedVocabValue,
479 TargetVocab&: OpcVocab, Dim&: OpcodeDim))
480 return Err;
481
482 if (auto Err = VocabStorage::parseVocabSection(Key: "Types", ParsedVocabValue: *ParsedVocabValue,
483 TargetVocab&: TypeVocab, Dim&: TypeDim))
484 return Err;
485
486 if (auto Err = VocabStorage::parseVocabSection(Key: "Arguments", ParsedVocabValue: *ParsedVocabValue,
487 TargetVocab&: ArgVocab, Dim&: ArgDim))
488 return Err;
489
490 if (!(OpcodeDim == TypeDim && TypeDim == ArgDim))
491 return createStringError(EC: errc::illegal_byte_sequence,
492 S: "Vocabulary sections have different dimensions");
493
494 return Error::success();
495}
496} // anonymous namespace
497
498/// Generate VocabStorage from vocabulary maps.
499VocabStorage Vocabulary::buildVocabStorage(const VocabMap &OpcVocab,
500 const VocabMap &TypeVocab,
501 const VocabMap &ArgVocab) {
502
503 // Helper for handling missing entities in the vocabulary.
504 // Currently, we use a zero vector. In the future, we will throw an error to
505 // ensure that *all* known entities are present in the vocabulary.
506 auto handleMissingEntity = [](const std::string &Val) {
507 LLVM_DEBUG(errs() << Val
508 << " is not in vocabulary, using zero vector; This "
509 "would result in an error in future.\n");
510 ++VocabMissCounter;
511 };
512
513 unsigned Dim = OpcVocab.begin()->second.size();
514 assert(Dim > 0 && "Vocabulary dimension must be greater than zero");
515
516 // Handle Opcodes
517 std::vector<Embedding> NumericOpcodeEmbeddings(Vocabulary::MaxOpcodes,
518 Embedding(Dim));
519 for (unsigned Opcode : seq(Begin: 0u, End: Vocabulary::MaxOpcodes)) {
520 StringRef VocabKey = Vocabulary::getVocabKeyForOpcode(Opcode: Opcode + 1);
521 auto It = OpcVocab.find(x: VocabKey.str());
522 if (It != OpcVocab.end())
523 NumericOpcodeEmbeddings[Opcode] = It->second;
524 else
525 handleMissingEntity(VocabKey.str());
526 }
527
528 // Handle Types - only canonical types are present in vocabulary
529 std::vector<Embedding> NumericTypeEmbeddings(Vocabulary::MaxCanonicalTypeIDs,
530 Embedding(Dim));
531 for (unsigned CTypeID : seq(Begin: 0u, End: Vocabulary::MaxCanonicalTypeIDs)) {
532 StringRef VocabKey = Vocabulary::getVocabKeyForCanonicalTypeID(
533 CType: static_cast<Vocabulary::CanonicalTypeID>(CTypeID));
534 if (auto It = TypeVocab.find(x: VocabKey.str()); It != TypeVocab.end()) {
535 NumericTypeEmbeddings[CTypeID] = It->second;
536 continue;
537 }
538 handleMissingEntity(VocabKey.str());
539 }
540
541 // Handle Arguments/Operands
542 std::vector<Embedding> NumericArgEmbeddings(Vocabulary::MaxOperandKinds,
543 Embedding(Dim));
544 for (unsigned OpKind : seq(Begin: 0u, End: Vocabulary::MaxOperandKinds)) {
545 Vocabulary::OperandKind Kind = static_cast<Vocabulary::OperandKind>(OpKind);
546 StringRef VocabKey = Vocabulary::getVocabKeyForOperandKind(Kind);
547 auto It = ArgVocab.find(x: VocabKey.str());
548 if (It != ArgVocab.end()) {
549 NumericArgEmbeddings[OpKind] = It->second;
550 continue;
551 }
552 handleMissingEntity(VocabKey.str());
553 }
554
555 // Handle Predicates: part of Operands section. We look up predicate keys
556 // in ArgVocab.
557 std::vector<Embedding> NumericPredEmbeddings(Vocabulary::MaxPredicateKinds,
558 Embedding(Dim, 0));
559 for (unsigned PK : seq(Begin: 0u, End: Vocabulary::MaxPredicateKinds)) {
560 StringRef VocabKey =
561 Vocabulary::getVocabKeyForPredicate(Pred: Vocabulary::getPredicate(Index: PK));
562 auto It = ArgVocab.find(x: VocabKey.str());
563 if (It != ArgVocab.end()) {
564 NumericPredEmbeddings[PK] = It->second;
565 continue;
566 }
567 handleMissingEntity(VocabKey.str());
568 }
569
570 // Create section-based storage instead of flat vocabulary
571 // Order must match Vocabulary::Section enum
572 std::vector<std::vector<Embedding>> Sections(
573 static_cast<unsigned>(Section::MaxSections));
574 Sections[static_cast<unsigned>(Section::Opcodes)] =
575 std::move(NumericOpcodeEmbeddings); // Section::Opcodes
576 Sections[static_cast<unsigned>(Section::CanonicalTypes)] =
577 std::move(NumericTypeEmbeddings); // Section::CanonicalTypes
578 Sections[static_cast<unsigned>(Section::Operands)] =
579 std::move(NumericArgEmbeddings); // Section::Operands
580 Sections[static_cast<unsigned>(Section::Predicates)] =
581 std::move(NumericPredEmbeddings); // Section::Predicates
582
583 // Create VocabStorage from organized sections
584 return VocabStorage(std::move(Sections));
585}
586
587// ==----------------------------------------------------------------------===//
588// Vocabulary
589//===----------------------------------------------------------------------===//
590
591Expected<Vocabulary> Vocabulary::fromFile(StringRef VocabFilePath,
592 float OpcWeight, float TypeWeight,
593 float ArgWeight) {
594 VocabMap OpcVocab, TypeVocab, ArgVocab;
595 if (auto Err =
596 readVocabularyFromFile(VocabFilePath, OpcVocab, TypeVocab, ArgVocab))
597 return std::move(Err);
598
599 // Scale the vocabulary sections based on the provided weights
600 auto scaleVocabSection = [](VocabMap &Vocab, float Weight) {
601 for (auto &Entry : Vocab)
602 Entry.second *= Weight;
603 };
604 scaleVocabSection(OpcVocab, OpcWeight);
605 scaleVocabSection(TypeVocab, TypeWeight);
606 scaleVocabSection(ArgVocab, ArgWeight);
607
608 // Generate the numeric lookup vocabulary
609 return Vocabulary(buildVocabStorage(OpcVocab, TypeVocab, ArgVocab));
610}
611
612// ==----------------------------------------------------------------------===//
613// IR2VecVocabAnalysis
614//===----------------------------------------------------------------------===//
615
616void IR2VecVocabAnalysis::emitError(Error Err) {
617 handleAllErrors(E: std::move(Err), Handlers: [&](const ErrorInfoBase &EI) {
618 reportFatalUsageError(reason: Twine("error reading vocabulary: ") + EI.message());
619 });
620}
621
622IR2VecVocabAnalysis::Result
623IR2VecVocabAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
624 // If vocabulary is already populated by the constructor, use it.
625 if (Vocab.has_value())
626 return Vocabulary(std::move(Vocab.value()));
627
628 // Otherwise, try to read from the vocabulary file specified via CLI.
629 const AnalysisOptions &Opts = getAnalysisOptions(M);
630 if (Opts.ir2vec_vocab_path.empty())
631 // FIXME: Use default vocabulary
632 reportFatalUsageError(
633 reason: "IR2Vec vocabulary file path not specified; You may need to "
634 "set it using --ir2vec-vocab-path");
635
636 // Use the static factory method to load the vocabulary.
637 auto VocabOrErr =
638 Vocabulary::fromFile(VocabFilePath: Opts.ir2vec_vocab_path, OpcWeight: Opts.ir2vec_opc_weight,
639 TypeWeight: Opts.ir2vec_type_weight, ArgWeight: Opts.ir2vec_arg_weight);
640 if (!VocabOrErr)
641 emitError(Err: VocabOrErr.takeError());
642
643 return std::move(*VocabOrErr);
644}
645
646// ==----------------------------------------------------------------------===//
647// Printer Passes
648//===----------------------------------------------------------------------===//
649
650PreservedAnalyses IR2VecPrinterPass::run(Module &M,
651 ModuleAnalysisManager &MAM) {
652 auto &Vocabulary = MAM.getResult<IR2VecVocabAnalysis>(IR&: M);
653 assert(Vocabulary.isValid() && "IR2Vec Vocabulary is invalid");
654
655 IR2VecKind Kind = getAnalysisOptions(M).ir2vec_kind;
656 for (Function &F : M) {
657 auto Emb = Embedder::create(Mode: Kind, F, Vocab: Vocabulary);
658 if (!Emb) {
659 OS << "Error creating IR2Vec embeddings \n";
660 continue;
661 }
662
663 OS << "IR2Vec embeddings for function " << F.getName() << ":\n";
664 OS << "Function vector: ";
665 Emb->getFunctionVector().print(OS);
666
667 OS << "Basic block vectors:\n";
668 for (const BasicBlock &BB : F) {
669 OS << "Basic block: " << BB.getName() << ":\n";
670 Emb->getBBVector(BB).print(OS);
671 }
672
673 OS << "Instruction vectors:\n";
674 for (const BasicBlock &BB : F) {
675 for (const Instruction &I : BB) {
676 OS << "Instruction: ";
677 I.print(O&: OS);
678 Emb->getInstVector(I).print(OS);
679 }
680 }
681 }
682 return PreservedAnalyses::all();
683}
684
685PreservedAnalyses IR2VecVocabPrinterPass::run(Module &M,
686 ModuleAnalysisManager &MAM) {
687 auto &IR2VecVocabulary = MAM.getResult<IR2VecVocabAnalysis>(IR&: M);
688 assert(IR2VecVocabulary.isValid() && "IR2Vec Vocabulary is invalid");
689
690 // Print each entry
691 unsigned Pos = 0;
692 for (const auto &Entry : IR2VecVocabulary) {
693 OS << "Key: " << IR2VecVocabulary.getStringKey(Pos: Pos++) << ": ";
694 Entry.print(OS);
695 }
696 return PreservedAnalyses::all();
697}
698