-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[IR2Vec] Add llvm-ir2vec tool for generating triplet embeddings #147842
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
+204
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,38 @@ | ||
; RUN: llvm-ir2vec %s | FileCheck %s -check-prefix=TRIPLETS | ||
|
||
define i32 @simple_add(i32 %a, i32 %b) { | ||
entry: | ||
%add = add i32 %a, %b | ||
ret i32 %add | ||
} | ||
|
||
define i32 @simple_mul(i32 %x, i32 %y) { | ||
entry: | ||
%mul = mul i32 %x, %y | ||
ret i32 %mul | ||
} | ||
|
||
define i32 @test_function(i32 %arg1, i32 %arg2) { | ||
entry: | ||
%local1 = alloca i32, align 4 | ||
%local2 = alloca i32, align 4 | ||
store i32 %arg1, ptr %local1, align 4 | ||
store i32 %arg2, ptr %local2, align 4 | ||
%load1 = load i32, ptr %local1, align 4 | ||
%load2 = load i32, ptr %local2, align 4 | ||
%result = add i32 %load1, %load2 | ||
ret i32 %result | ||
} | ||
|
||
; TRIPLETS: Add IntegerTy Variable Variable | ||
; TRIPLETS-NEXT: Ret VoidTy Variable | ||
; TRIPLETS-NEXT: Mul IntegerTy Variable Variable | ||
; TRIPLETS-NEXT: Ret VoidTy Variable | ||
; TRIPLETS-NEXT: Alloca PointerTy Constant | ||
; TRIPLETS-NEXT: Alloca PointerTy Constant | ||
; TRIPLETS-NEXT: Store VoidTy Variable Pointer | ||
; TRIPLETS-NEXT: Store VoidTy Variable Pointer | ||
; TRIPLETS-NEXT: Load IntegerTy Pointer | ||
; TRIPLETS-NEXT: Load IntegerTy Pointer | ||
; TRIPLETS-NEXT: Add IntegerTy Variable Variable | ||
; TRIPLETS-NEXT: Ret VoidTy Variable |
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,10 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Analysis | ||
Core | ||
IRReader | ||
Support | ||
) | ||
|
||
add_llvm_tool(llvm-ir2vec | ||
llvm-ir2vec.cpp | ||
) |
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,154 @@ | ||
//===- llvm-ir2vec.cpp - IR2Vec Embedding Generation Tool -----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file implements the IR2Vec embedding generation tool. | ||
/// | ||
/// Currently supports triplet generation for vocabulary training. | ||
/// Future updates will support embedding generation using trained vocabulary. | ||
/// | ||
/// Usage: llvm-ir2vec input.bc -o triplets.txt | ||
/// | ||
/// TODO: Add embedding generation mode with vocabulary support | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Analysis/IR2Vec.h" | ||
#include "llvm/IR/BasicBlock.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/Type.h" | ||
#include "llvm/IRReader/IRReader.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/Errc.h" | ||
#include "llvm/Support/InitLLVM.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
using namespace llvm; | ||
using namespace ir2vec; | ||
|
||
#define DEBUG_TYPE "ir2vec" | ||
|
||
static cl::OptionCategory IR2VecToolCategory("IR2Vec Tool Options"); | ||
|
||
static cl::opt<std::string> InputFilename(cl::Positional, | ||
cl::desc("<input bitcode file>"), | ||
cl::Required, | ||
cl::cat(IR2VecToolCategory)); | ||
|
||
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), | ||
cl::value_desc("filename"), | ||
cl::init("-"), | ||
cl::cat(IR2VecToolCategory)); | ||
|
||
namespace { | ||
|
||
/// Helper class for collecting IR information and generating triplets | ||
class IR2VecTool { | ||
private: | ||
Module &M; | ||
|
||
public: | ||
explicit IR2VecTool(Module &M) : M(M) {} | ||
|
||
/// Generate triplets for the entire module | ||
void generateTriplets(raw_ostream &OS) const { | ||
for (const Function &F : M) | ||
generateTriplets(F, OS); | ||
} | ||
|
||
/// Generate triplets for a single function | ||
void generateTriplets(const Function &F, raw_ostream &OS) const { | ||
if (F.isDeclaration()) | ||
return; | ||
|
||
std::string LocalOutput; | ||
raw_string_ostream LocalOS(LocalOutput); | ||
|
||
for (const BasicBlock &BB : F) | ||
traverseBasicBlock(BB, LocalOS); | ||
|
||
LocalOS.flush(); | ||
OS << LocalOutput; | ||
} | ||
|
||
private: | ||
/// Process a single basic block for triplet generation | ||
void traverseBasicBlock(const BasicBlock &BB, raw_string_ostream &OS) const { | ||
// Consider only non-debug and non-pseudo instructions | ||
for (const auto &I : BB.instructionsWithoutDebug()) { | ||
StringRef OpcStr = Vocabulary::getVocabKeyForOpcode(I.getOpcode()); | ||
StringRef TypeStr = | ||
Vocabulary::getVocabKeyForTypeID(I.getType()->getTypeID()); | ||
|
||
OS << '\n' << OpcStr << ' ' << TypeStr << ' '; | ||
|
||
LLVM_DEBUG({ | ||
I.print(dbgs()); | ||
dbgs() << "\n"; | ||
I.getType()->print(dbgs()); | ||
dbgs() << " Type\n"; | ||
}); | ||
|
||
for (const Use &U : I.operands()) | ||
OS << Vocabulary::getVocabKeyForOperandKind( | ||
Vocabulary::getOperandKind(U.get())) | ||
<< ' '; | ||
} | ||
} | ||
}; | ||
|
||
Error processModule(Module &M, raw_ostream &OS) { | ||
IR2VecTool Tool(M); | ||
Tool.generateTriplets(OS); | ||
|
||
return Error::success(); | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
int main(int argc, char **argv) { | ||
InitLLVM X(argc, argv); | ||
cl::HideUnrelatedOptions(IR2VecToolCategory); | ||
cl::ParseCommandLineOptions( | ||
argc, argv, | ||
"IR2Vec - Triplet Generation Tool\n" | ||
"Generates triplets for vocabulary training from LLVM IR.\n" | ||
"Future updates will support embedding generation.\n\n" | ||
"Usage:\n" | ||
" llvm-ir2vec input.bc -o triplets.txt\n"); | ||
|
||
// Parse the input LLVM IR file | ||
SMDiagnostic Err; | ||
LLVMContext Context; | ||
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context); | ||
if (!M) { | ||
Err.print(argv[0], errs()); | ||
return 1; | ||
} | ||
|
||
std::error_code EC; | ||
raw_fd_ostream OS(OutputFilename, EC); | ||
if (EC) { | ||
errs() << "Error opening output file: " << EC.message() << "\n"; | ||
return 1; | ||
} | ||
|
||
if (Error Err = processModule(*M, OS)) { | ||
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EIB) { | ||
errs() << "Error: " << EIB.message() << "\n"; | ||
}); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
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.