LLVM 24.0.0git
IR2Vec.cpp
Go to the documentation of this file.
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
15#include "AnalysisOptions.h"
16
18#include "llvm/ADT/Sequence.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/IR/CFG.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"
28#include "llvm/Support/Format.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
40 return getAnalysisOptions(C).ir2vec_vocab_path;
41}
45
47
48// ==----------------------------------------------------------------------===//
49// Local helper functions
50//===----------------------------------------------------------------------===//
51namespace llvm::json {
52inline bool fromJSON(const llvm::json::Value &E, Embedding &Out,
54 std::vector<double> TempOut;
55 if (!llvm::json::fromJSON(E, TempOut, P))
56 return false;
57 Out = Embedding(std::move(TempOut));
58 return true;
59}
60} // namespace llvm::json
61
62// ==----------------------------------------------------------------------===//
63// Embedding
64//===----------------------------------------------------------------------===//
66 assert(this->size() == RHS.size() && "Vectors must have the same dimension");
67 std::transform(this->begin(), this->end(), RHS.begin(), this->begin(),
68 std::plus<double>());
69 return *this;
70}
71
73 Embedding Result(*this);
74 Result += RHS;
75 return Result;
76}
77
79 assert(this->size() == RHS.size() && "Vectors must have the same dimension");
80 std::transform(this->begin(), this->end(), RHS.begin(), this->begin(),
81 std::minus<double>());
82 return *this;
83}
84
86 Embedding Result(*this);
87 Result -= RHS;
88 return Result;
89}
90
92 std::transform(this->begin(), this->end(), this->begin(),
93 [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
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((*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
124 OS << " [";
125 for (const auto &Elem : Data)
126 OS << " " << format("%.2f", 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) {
138 return std::make_unique<SymbolicEmbedder>(F, Vocab);
140 return std::make_unique<FlowAwareEmbedder>(F, Vocab);
141 }
142 return nullptr;
143}
144
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(&F))
153 FuncVector += computeEmbeddings(*BB);
154 return FuncVector;
155}
156
158 Embedding BBVector(Dimension, 0);
159
160 // We consider only the non-pseudo instructions
161 for (const auto &I : BB)
163 BBVector += computeEmbeddings(I);
164 return BBVector;
165}
166
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>(&I))
176 InstVector += Vocab[IC->getPredicate()];
177 return InstVector;
178}
179
181 // If we have already computed the embedding for this instruction, return it
182 auto It = InstVecMap.find(&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>(Op)) {
192 auto DefIt = InstVecMap.find(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>(&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(Section.begin(), Section.end(),
254 [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
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
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
285 const const_iterator &Other) const {
286 return Storage == Other.Storage && SectionId == Other.SectionId &&
287 LocalIndex == Other.LocalIndex;
288}
289
291 const const_iterator &Other) const {
292 return !(*this == Other);
293}
294
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)
302 "JSON root is not an object");
303
304 const json::Value *SectionValue = RootObj->get(Key);
305 if (!SectionValue)
307 "Missing '" + std::string(Key) +
308 "' section in vocabulary file");
309 if (!json::fromJSON(*SectionValue, TargetVocab, Path))
311 "Unable to parse '" + std::string(Key) +
312 "' section from vocabulary");
313
314 Dim = TargetVocab.begin()->second.size();
315 if (Dim == 0)
317 "Dimension of '" + std::string(Key) +
318 "' section of the vocabulary is zero");
319
320 if (!std::all_of(TargetVocab.begin(), TargetVocab.end(),
321 [Dim](const std::pair<StringRef, Embedding> &Entry) {
322 return Entry.second.size() == Dim;
323 }))
324 return createStringError(
326 "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
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
359
360unsigned Vocabulary::getPredicateLocalIndex(CmpInst::Predicate P) {
363 else
366}
367
368CmpInst::Predicate Vocabulary::getPredicateFromLocalIndex(unsigned LocalIndex) {
369 unsigned fcmpRange =
371 if (LocalIndex < fcmpRange)
373 LocalIndex);
374 else
376 LocalIndex - fcmpRange);
377}
378
380 static SmallString<16> PredNameBuffer;
382 PredNameBuffer = "FCMP_";
383 else
384 PredNameBuffer = "ICMP_";
385 PredNameBuffer += CmpInst::getPredicateName(Pred);
386 return PredNameBuffer;
387}
388
390 assert(Pos < NumCanonicalEntries && "Position out of bounds in vocabulary");
391 // Opcode
392 if (Pos < MaxOpcodes)
393 return getVocabKeyForOpcode(Pos + 1);
394 // Type
395 if (Pos < OperandBaseOffset)
396 return getVocabKeyForCanonicalTypeID(
397 static_cast<CanonicalTypeID>(Pos - MaxOpcodes));
398 // Operand
399 if (Pos < PredicateBaseOffset)
401 static_cast<OperandKind>(Pos - OperandBaseOffset));
402 // Predicates
403 return getVocabKeyForPredicate(getPredicate(Pos - PredicateBaseOffset));
404}
405
406// For now, assume vocabulary is stable unless explicitly invalidated.
408 ModuleAnalysisManager::Invalidator &Inv) const {
409 auto PAC = PA.getChecker<IR2VecVocabAnalysis>();
410 return !(PAC.preservedWhenStateless());
411}
412
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(static_cast<unsigned>(Section::MaxSections));
420
421 // Opcodes section
422 std::vector<Embedding> OpcodeSec;
423 OpcodeSec.reserve(MaxOpcodes);
424 for (unsigned I = 0; I < MaxOpcodes; ++I) {
425 OpcodeSec.emplace_back(Dim, DummyVal);
426 DummyVal += 0.1f;
427 }
428 Sections.push_back(std::move(OpcodeSec));
429
430 // Types section
431 std::vector<Embedding> TypeSec;
432 TypeSec.reserve(MaxCanonicalTypeIDs);
433 for (unsigned I = 0; I < MaxCanonicalTypeIDs; ++I) {
434 TypeSec.emplace_back(Dim, DummyVal);
435 DummyVal += 0.1f;
436 }
437 Sections.push_back(std::move(TypeSec));
438
439 // Operands section
440 std::vector<Embedding> OperandSec;
441 OperandSec.reserve(MaxOperandKinds);
442 for (unsigned I = 0; I < MaxOperandKinds; ++I) {
443 OperandSec.emplace_back(Dim, DummyVal);
444 DummyVal += 0.1f;
445 }
446 Sections.push_back(std::move(OperandSec));
447
448 // Predicates section
449 std::vector<Embedding> PredicateSec;
450 PredicateSec.reserve(MaxPredicateKinds);
451 for (unsigned I = 0; I < MaxPredicateKinds; ++I) {
452 PredicateSec.emplace_back(Dim, DummyVal);
453 DummyVal += 0.1f;
454 }
455 Sections.push_back(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(VocabFilePath, /*IsText=*/true);
468 if (!BufOrError)
469 return createFileError(VocabFilePath, BufOrError.getError());
470
471 auto Content = BufOrError.get()->getBuffer();
472
473 Expected<json::Value> ParsedVocabValue = json::parse(Content);
474 if (!ParsedVocabValue)
475 return ParsedVocabValue.takeError();
476
477 unsigned OpcodeDim = 0, TypeDim = 0, ArgDim = 0;
478 if (auto Err = VocabStorage::parseVocabSection("Opcodes", *ParsedVocabValue,
479 OpcVocab, OpcodeDim))
480 return Err;
481
482 if (auto Err = VocabStorage::parseVocabSection("Types", *ParsedVocabValue,
483 TypeVocab, TypeDim))
484 return Err;
485
486 if (auto Err = VocabStorage::parseVocabSection("Arguments", *ParsedVocabValue,
487 ArgVocab, ArgDim))
488 return Err;
489
490 if (!(OpcodeDim == TypeDim && TypeDim == ArgDim))
492 "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(0u, Vocabulary::MaxOpcodes)) {
520 StringRef VocabKey = Vocabulary::getVocabKeyForOpcode(Opcode + 1);
521 auto It = OpcVocab.find(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(0u, Vocabulary::MaxCanonicalTypeIDs)) {
532 StringRef VocabKey = Vocabulary::getVocabKeyForCanonicalTypeID(
533 static_cast<Vocabulary::CanonicalTypeID>(CTypeID));
534 if (auto It = TypeVocab.find(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(0u, Vocabulary::MaxOperandKinds)) {
546 StringRef VocabKey = Vocabulary::getVocabKeyForOperandKind(Kind);
547 auto It = ArgVocab.find(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(0u, Vocabulary::MaxPredicateKinds)) {
560 StringRef VocabKey =
561 Vocabulary::getVocabKeyForPredicate(Vocabulary::getPredicate(PK));
562 auto It = ArgVocab.find(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
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(std::move(Err), [&](const ErrorInfoBase &EI) {
618 reportFatalUsageError(Twine("error reading vocabulary: ") + EI.message());
619 });
620}
621
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
633 "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(Opts.ir2vec_vocab_path, Opts.ir2vec_opc_weight,
639 Opts.ir2vec_type_weight, Opts.ir2vec_arg_weight);
640 if (!VocabOrErr)
641 emitError(VocabOrErr.takeError());
642
643 return std::move(*VocabOrErr);
644}
645
646// ==----------------------------------------------------------------------===//
647// Printer Passes
648//===----------------------------------------------------------------------===//
649
652 auto &Vocabulary = MAM.getResult<IR2VecVocabAnalysis>(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(Kind, F, 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(OS);
678 Emb->getInstVector(I).print(OS);
679 }
680 }
681 }
682 return PreservedAnalyses::all();
683}
684
687 auto &IR2VecVocabulary = MAM.getResult<IR2VecVocabAnalysis>(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++) << ": ";
694 Entry.print(OS);
695 }
696 return PreservedAnalyses::all();
697}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
This file defines the IR2Vec vocabulary analysis(IR2VecVocabAnalysis), the core ir2vec::Embedder inte...
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
std::map< std::string, Embedding > VocabMap
Definition MIR2Vec.cpp:463
#define P(N)
ModuleAnalysisManager MAM
Provides some synthesis utilities to produce sequences of values.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
static LLVM_ABI StringRef getPredicateName(Predicate P)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
Base class for error info classes.
Definition Error.h:44
virtual std::string message() const
Return the error message as a string.
Definition Error.h:52
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
Definition IR2Vec.cpp:650
This analysis provides the vocabulary for IR2Vec.
Definition IR2Vec.h:629
ir2vec::Vocabulary Result
Definition IR2Vec.h:639
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &MAM)
Definition IR2Vec.cpp:623
static LLVM_ABI AnalysisKey Key
Definition IR2Vec.h:635
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
Definition IR2Vec.cpp:685
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:276
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
static LLVM_ABI std::unique_ptr< Embedder > create(IR2VecKind Mode, const Function &F, const Vocabulary &Vocab)
Factory method to create an Embedder object.
Definition IR2Vec.cpp:134
const Vocabulary & Vocab
Definition IR2Vec.h:548
const unsigned Dimension
Dimension of the vector representation; captured from the input vocabulary.
Definition IR2Vec.h:551
LLVM_ABI Embedding computeEmbeddings() const
Function to compute embeddings.
Definition IR2Vec.cpp:145
const Function & F
Definition IR2Vec.h:547
Iterator support for section-based access.
Definition IR2Vec.h:199
const_iterator(const VocabStorage *Storage, unsigned SectionId, size_t LocalIndex)
Definition IR2Vec.h:205
LLVM_ABI bool operator!=(const const_iterator &Other) const
Definition IR2Vec.cpp:290
LLVM_ABI const_iterator & operator++()
Definition IR2Vec.cpp:271
LLVM_ABI const Embedding & operator*() const
Definition IR2Vec.cpp:264
LLVM_ABI bool operator==(const const_iterator &Other) const
Definition IR2Vec.cpp:284
Generic storage class for section-based vocabularies.
Definition IR2Vec.h:154
static LLVM_ABI Error parseVocabSection(StringRef Key, const json::Value &ParsedVocabValue, VocabMap &TargetVocab, unsigned &Dim)
Parse a vocabulary section from JSON and populate the target vocabulary map.
Definition IR2Vec.cpp:295
unsigned getNumSections() const
Get number of sections.
Definition IR2Vec.h:182
size_t size() const
Get total number of entries across all sections.
Definition IR2Vec.h:179
VocabStorage()=default
Default constructor creates empty storage (invalid state)
const_iterator begin() const
Definition IR2Vec.h:215
std::map< std::string, Embedding > VocabMap
Definition IR2Vec.h:220
Class for storing and accessing the IR2Vec vocabulary.
Definition IR2Vec.h:245
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv) const
Definition IR2Vec.cpp:407
static LLVM_ABI Expected< Vocabulary > fromFile(StringRef VocabFilePath, float OpcWeight=1.0, float TypeWeight=0.5, float ArgWeight=0.2)
Create a Vocabulary by loading embeddings from a JSON file.
Definition IR2Vec.cpp:591
static LLVM_ABI OperandKind getOperandKind(const Value *Op)
Function to classify an operand into OperandKind.
Definition IR2Vec.cpp:348
static StringRef getVocabKeyForOperandKind(OperandKind Kind)
Function to get vocabulary key for a given OperandKind.
Definition IR2Vec.h:365
friend class llvm::IR2VecVocabAnalysis
Definition IR2Vec.h:246
static LLVM_ABI StringRef getStringKey(unsigned Pos)
Returns the string key for a given index position in the vocabulary.
Definition IR2Vec.cpp:389
static constexpr unsigned MaxCanonicalTypeIDs
Definition IR2Vec.h:317
static constexpr unsigned MaxOperandKinds
Definition IR2Vec.h:319
OperandKind
Operand kinds supported by IR2Vec Vocabulary.
Definition IR2Vec.h:302
static LLVM_ABI StringRef getVocabKeyForPredicate(CmpInst::Predicate P)
Function to get vocabulary key for a given predicate.
Definition IR2Vec.cpp:379
static LLVM_ABI StringRef getVocabKeyForOpcode(unsigned Opcode)
Function to get vocabulary key for a given Opcode.
Definition IR2Vec.cpp:336
bool isValid() const
Definition IR2Vec.h:345
static LLVM_ABI VocabStorage createDummyVocabForTest(unsigned Dim=1)
Create a dummy vocabulary for testing purposes.
Definition IR2Vec.cpp:413
static constexpr unsigned MaxPredicateKinds
Definition IR2Vec.h:323
CanonicalTypeID
Canonical type IDs supported by IR2Vec Vocabulary.
Definition IR2Vec.h:284
An Object is a JSON object, which maps strings to heterogenous JSON values.
Definition JSON.h:98
LLVM_ABI Value * get(StringRef K)
Definition JSON.cpp:30
The root is the trivial Path to the root value.
Definition JSON.h:700
A "cursor" marking a position within a Value.
Definition JSON.h:653
A Value is an JSON value of unknown type.
Definition JSON.h:291
const json::Object * getAsObject() const
Definition JSON.h:465
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI IR2VecKind getEmbeddingKind(const LLVMContext &C)
Return -ir2vec-kind.
Definition IR2Vec.cpp:42
LLVM_ABI StringRef getVocabFile(const LLVMContext &C)
Return -ir2vec-vocab-path.
Definition IR2Vec.cpp:39
LLVM_ABI llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition JSON.cpp:681
bool fromJSON(const Value &E, std::string &Out, Path P)
Definition JSON.h:729
ir2vec::Embedding Embedding
Definition MIR2Vec.h:79
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ illegal_byte_sequence
Definition Errc.h:52
@ invalid_argument
Definition Errc.h:56
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
IR2VecKind
IR2Vec computes two kinds of embeddings: Symbolic and Flow-aware.
Definition IR2Vec.h:70
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:102
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
Embedding is a datatype that wraps std::vector<double>.
Definition IR2Vec.h:85
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...
Definition IR2Vec.cpp:110
LLVM_ABI Embedding & operator+=(const Embedding &RHS)
Arithmetic operators.
Definition IR2Vec.cpp:65
LLVM_ABI Embedding operator-(const Embedding &RHS) const
Definition IR2Vec.cpp:85
LLVM_ABI Embedding & operator-=(const Embedding &RHS)
Definition IR2Vec.cpp:78
LLVM_ABI Embedding operator*(double Factor) const
Definition IR2Vec.cpp:97
size_t size() const
Definition IR2Vec.h:98
LLVM_ABI Embedding & operator*=(double Factor)
Definition IR2Vec.cpp:91
LLVM_ABI Embedding operator+(const Embedding &RHS) const
Definition IR2Vec.cpp:72
LLVM_ABI Embedding & scaleAndAdd(const Embedding &Src, float Factor)
Adds Src Embedding scaled by Factor with the called Embedding.
Definition IR2Vec.cpp:103
LLVM_ABI void print(raw_ostream &OS) const
Definition IR2Vec.cpp:123