-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Adding IR2Vec as an analysis pass #134004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
048b61d
Adding IR2Vec as an analysis pass
svkeerthy 9ba9971
Added 75D IR2Vec vocabulary
svkeerthy 657e5b5
Some minor refactoring
svkeerthy b15cab3
Merge branch 'main' into main
svkeerthy bbbd6c9
Merge branch 'main' into main
svkeerthy 0775d5b
Minor refactoring to address review comments
svkeerthy f507272
Merge branch 'llvm:main' into main
svkeerthy 784ee22
Merge branch 'llvm:main' into main
svkeerthy 6e0385e
Adding documentation
svkeerthy 9b4541e
Addressing review comments in doc
svkeerthy be7f312
Addressing review comments
svkeerthy 6a8b450
Exposing weights of Opc, Types, Args
svkeerthy 6bb850a
Minor changes to address comments
svkeerthy fc83ba0
Minor changes to address review comments
svkeerthy d624c13
Added doc to SymbolicEmbedder
svkeerthy 59b8fab
Changing Create factory method to return Expected<> instead of ErrorOr
svkeerthy ca9d8c0
Merge branch 'main' into main
svkeerthy 7519940
Merge branch 'main' into main
mtrofin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| //===- IR2VecAnalysis.h - IR2Vec Analysis Implementation -------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM | ||
| // Exceptions. See the LICENSE file for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file | ||
| /// This file contains the declaration of IR2VecAnalysis that computes | ||
| /// IR2Vec Embeddings of the program. | ||
| /// | ||
| /// Program Embeddings are typically or derived-from a learned | ||
| /// representation of the program. Such embeddings are used to represent the | ||
| /// programs as input to machine learning algorithms. IR2Vec represents the | ||
| /// LLVM IR as embeddings. | ||
| /// | ||
| /// The IR2Vec algorithm is described in the following paper: | ||
| /// | ||
| /// IR2Vec: LLVM IR Based Scalable Program Embeddings, S. VenkataKeerthy, | ||
| /// Rohit Aggarwal, Shalini Jain, Maunendra Sankar Desarkar, Ramakrishna | ||
| /// Upadrasta, and Y. N. Srikant, ACM Transactions on Architecture and | ||
| /// Code Optimization (TACO), 2020. https://doi.org/10.1145/3418463. | ||
| /// https://arxiv.org/abs/1909.06228 | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_ANALYSIS_IR2VECANALYSIS_H | ||
| #define LLVM_ANALYSIS_IR2VECANALYSIS_H | ||
|
|
||
| #include "llvm/ADT/MapVector.h" | ||
| #include "llvm/IR/PassManager.h" | ||
| #include <map> | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class Module; | ||
| class BasicBlock; | ||
| class Instruction; | ||
| class Function; | ||
|
|
||
| namespace ir2vec { | ||
| using Embedding = std::vector<double>; | ||
| // FIXME: Current the keys are strings. This can be changed to | ||
| // use integers for cheaper lookups. | ||
| using Vocab = std::map<std::string, Embedding>; | ||
| } // namespace ir2vec | ||
|
|
||
| class IR2VecVocabResult; | ||
| class IR2VecResult; | ||
|
|
||
| /// This analysis provides the vocabulary for IR2Vec. The vocabulary provides a | ||
| /// mapping between an entity of the IR (like opcode, type, argument, etc.) and | ||
| /// its corresponding embedding. | ||
| class IR2VecVocabAnalysis : public AnalysisInfoMixin<IR2VecVocabAnalysis> { | ||
| unsigned DIM = 0; | ||
| ir2vec::Vocab Vocabulary; | ||
| Error readVocabulary(); | ||
|
|
||
| public: | ||
| static AnalysisKey Key; | ||
| IR2VecVocabAnalysis() = default; | ||
| using Result = IR2VecVocabResult; | ||
| Result run(Module &M, ModuleAnalysisManager &MAM); | ||
| }; | ||
|
|
||
| class IR2VecVocabResult { | ||
| ir2vec::Vocab Vocabulary; | ||
| bool Valid = false; | ||
| unsigned DIM = 0; | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public: | ||
| IR2VecVocabResult() = default; | ||
| IR2VecVocabResult(ir2vec::Vocab &&Vocabulary, unsigned Dim); | ||
|
|
||
| // Helper functions | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bool isValid() const { return Valid; } | ||
| const ir2vec::Vocab &getVocabulary() const; | ||
| unsigned getDimension() const { return DIM; } | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bool invalidate(Module &M, const PreservedAnalyses &PA, | ||
| ModuleAnalysisManager::Invalidator &Inv); | ||
| }; | ||
|
|
||
| class IR2VecResult { | ||
| SmallMapVector<const Instruction *, ir2vec::Embedding, 128> InstVecMap; | ||
| SmallMapVector<const BasicBlock *, ir2vec::Embedding, 16> BBVecMap; | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ir2vec::Embedding FuncVector; | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unsigned DIM = 0; | ||
| bool Valid = false; | ||
|
|
||
| public: | ||
| IR2VecResult() = default; | ||
| IR2VecResult( | ||
| SmallMapVector<const Instruction *, ir2vec::Embedding, 128> &&InstMap, | ||
| SmallMapVector<const BasicBlock *, ir2vec::Embedding, 16> &&BBMap, | ||
| ir2vec::Embedding &&FuncVector, unsigned Dim); | ||
| bool isValid() const { return Valid; } | ||
|
|
||
| const SmallMapVector<const Instruction *, ir2vec::Embedding, 128> & | ||
svkeerthy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getInstVecMap() const; | ||
| const SmallMapVector<const BasicBlock *, ir2vec::Embedding, 16> & | ||
| getBBVecMap() const; | ||
| const ir2vec::Embedding &getFunctionVector() const; | ||
| unsigned getDimension() const; | ||
| }; | ||
|
|
||
| /// This analysis provides the IR2Vec embeddings for instructions, basic blocks, | ||
| /// and functions. | ||
| class IR2VecAnalysis : public AnalysisInfoMixin<IR2VecAnalysis> { | ||
| public: | ||
| IR2VecAnalysis() = default; | ||
| static AnalysisKey Key; | ||
| using Result = IR2VecResult; | ||
| Result run(Function &F, FunctionAnalysisManager &FAM); | ||
| }; | ||
|
|
||
| /// This pass prints the IR2Vec embeddings for instructions, basic blocks, and | ||
| /// functions. | ||
| class IR2VecPrinterPass : public PassInfoMixin<IR2VecPrinterPass> { | ||
| raw_ostream &OS; | ||
| void printVector(const ir2vec::Embedding &Vec) const; | ||
|
|
||
| public: | ||
| explicit IR2VecPrinterPass(raw_ostream &OS) : OS(OS) {} | ||
| PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
| static bool isRequired() { return true; } | ||
| }; | ||
|
|
||
| } // namespace llvm | ||
|
|
||
| #endif // LLVM_ANALYSIS_IR2VECANALYSIS_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.