[llvm] [NFC][IR2Vec][MIR2Vec] llvm-ir2vec refactor - move Tool class implementations into a separate utils file (PR #174133)
Nishant Sachdeva via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 16 02:47:59 PST 2026
https://github.com/nishant-sachdeva updated https://github.com/llvm/llvm-project/pull/174133
>From e45b3c56b9d503485383861c113dbce50a014c0a Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 21 Dec 2025 01:25:33 +0530
Subject: [PATCH 01/21] Work Commit - Separating all tool implementation from
cli file
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 1 +
llvm/tools/llvm-ir2vec/emb-tool.cpp | 421 ++++++++++++++++++
.../llvm-ir2vec/{llvm-ir2vec.h => emb-tool.h} | 8 +-
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 372 +---------------
4 files changed, 428 insertions(+), 374 deletions(-)
create mode 100644 llvm/tools/llvm-ir2vec/emb-tool.cpp
rename llvm/tools/llvm-ir2vec/{llvm-ir2vec.h => emb-tool.h} (98%)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 2bb6686392907..9d5db8663fb38 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -19,6 +19,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_tool(llvm-ir2vec
llvm-ir2vec.cpp
+ emb-tool.cpp
DEPENDS
intrinsics_gen
diff --git a/llvm/tools/llvm-ir2vec/emb-tool.cpp b/llvm/tools/llvm-ir2vec/emb-tool.cpp
new file mode 100644
index 0000000000000..891b26f8ef763
--- /dev/null
+++ b/llvm/tools/llvm-ir2vec/emb-tool.cpp
@@ -0,0 +1,421 @@
+//===- emb-tool.cpp - IR2Vec/MIR2Vec 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 IR2VecTool and MIR2VecTool classes for
+/// IR2Vec/MIR2Vec embedding generation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "emb-tool.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Analysis/IR2Vec.h"
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassInstrumentation.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "llvm/CodeGen/MIR2Vec.h"
+#include "llvm/CodeGen/MIRParser/MIRParser.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetInstrInfo.h"
+#include "llvm/CodeGen/TargetRegisterInfo.h"
+#include "llvm/Support/WithColor.h"
+#include "llvm/Target/TargetMachine.h"
+
+#define DEBUG_TYPE "ir2vec"
+
+namespace llvm {
+
+namespace ir2vec {
+
+bool IR2VecTool::initializeVocabulary() {
+ // Register and run the IR2Vec vocabulary analysis
+ // The vocabulary file path is specified via --ir2vec-vocab-path global
+ // option
+ MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
+ MAM.registerPass([&] { return IR2VecVocabAnalysis(); });
+ // This will throw an error if vocab is not found or invalid
+ Vocab = &MAM.getResult<IR2VecVocabAnalysis>(M);
+ return Vocab->isValid();
+}
+
+TripletResult IR2VecTool::generateTriplets(const Function &F) const {
+ if (F.isDeclaration())
+ return {};
+
+ TripletResult Result;
+ Result.MaxRelation = 0;
+
+ unsigned MaxRelation = NextRelation;
+ unsigned PrevOpcode = 0;
+ bool HasPrevOpcode = false;
+
+ for (const BasicBlock &BB : F) {
+ for (const auto &I : BB.instructionsWithoutDebug()) {
+ unsigned Opcode = Vocabulary::getIndex(I.getOpcode());
+ unsigned TypeID = Vocabulary::getIndex(I.getType()->getTypeID());
+
+ // Add "Next" relationship with previous instruction
+ if (HasPrevOpcode) {
+ Result.Triplets.push_back({PrevOpcode, Opcode, NextRelation});
+ LLVM_DEBUG(dbgs() << Vocabulary::getVocabKeyForOpcode(PrevOpcode + 1)
+ << '\t'
+ << Vocabulary::getVocabKeyForOpcode(Opcode + 1)
+ << '\t' << "Next\n");
+ }
+
+ // Add "Type" relationship
+ Result.Triplets.push_back({Opcode, TypeID, TypeRelation});
+ LLVM_DEBUG(
+ dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
+ << Vocabulary::getVocabKeyForTypeID(I.getType()->getTypeID())
+ << '\t' << "Type\n");
+
+ // Add "Arg" relationships
+ unsigned ArgIndex = 0;
+ for (const Use &U : I.operands()) {
+ unsigned OperandID = Vocabulary::getIndex(*U.get());
+ unsigned RelationID = ArgRelation + ArgIndex;
+ Result.Triplets.push_back({Opcode, OperandID, RelationID});
+
+ LLVM_DEBUG({
+ StringRef OperandStr = Vocabulary::getVocabKeyForOperandKind(
+ Vocabulary::getOperandKind(U.get()));
+ dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
+ << OperandStr << '\t' << "Arg" << ArgIndex << '\n';
+ });
+
+ ++ArgIndex;
+ }
+ // Only update MaxRelation if there were operands
+ if (ArgIndex > 0)
+ MaxRelation = std::max(MaxRelation, ArgRelation + ArgIndex - 1);
+ PrevOpcode = Opcode;
+ HasPrevOpcode = true;
+ }
+ }
+
+ Result.MaxRelation = MaxRelation;
+ return Result;
+}
+
+TripletResult IR2VecTool::generateTriplets() const {
+ TripletResult Result;
+ Result.MaxRelation = NextRelation;
+
+ for (const Function &F : M.getFunctionDefs()) {
+ TripletResult FuncResult = generateTriplets(F);
+ Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
+ Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
+ FuncResult.Triplets.end());
+ }
+
+ return Result;
+}
+
+void IR2VecTool::writeTripletsToStream(raw_ostream &OS) const {
+ auto Result = generateTriplets();
+ OS << "MAX_RELATION=" << Result.MaxRelation << '\n';
+ for (const auto &T : Result.Triplets)
+ OS << T.Head << '\t' << T.Tail << '\t' << T.Relation << '\n';
+}
+
+EntityList IR2VecTool::collectEntityMappings() {
+ auto EntityLen = Vocabulary::getCanonicalSize();
+ EntityList Result;
+ for (unsigned EntityID = 0; EntityID < EntityLen; ++EntityID)
+ Result.push_back(Vocabulary::getStringKey(EntityID).str());
+ return Result;
+}
+
+void IR2VecTool::writeEntitiesToStream(raw_ostream &OS) {
+ auto Entities = collectEntityMappings();
+ OS << Entities.size() << "\n";
+ for (unsigned EntityID = 0; EntityID < Entities.size(); ++EntityID)
+ OS << Entities[EntityID] << '\t' << EntityID << '\n';
+}
+
+void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
+ EmbeddingLevel Level) const {
+ if (!Vocab->isValid()) {
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary is not valid. IR2VecTool not initialized.\n";
+ return;
+ }
+
+ for (const Function &F : M.getFunctionDefs())
+ writeEmbeddingsToStream(F, OS, Level);
+}
+
+void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
+ EmbeddingLevel Level) const {
+ if (!Vocab || !Vocab->isValid()) {
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary is not valid. IR2VecTool not initialized.\n";
+ return;
+ }
+ if (F.isDeclaration()) {
+ OS << "Function " << F.getName() << " is a declaration, skipping.\n";
+ return;
+ }
+
+ // Create embedder for this function
+ auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab);
+ if (!Emb) {
+ WithColor::error(errs(), ToolName)
+ << "Failed to create embedder for function " << F.getName() << "\n";
+ return;
+ }
+
+ OS << "Function: " << F.getName() << "\n";
+
+ // Generate embeddings based on the specified level
+ switch (Level) {
+ case FunctionLevel:
+ Emb->getFunctionVector().print(OS);
+ break;
+ case BasicBlockLevel:
+ for (const BasicBlock &BB : F) {
+ OS << BB.getName() << ":";
+ Emb->getBBVector(BB).print(OS);
+ }
+ break;
+ case InstructionLevel:
+ for (const Instruction &I : instructions(F)) {
+ OS << I;
+ Emb->getInstVector(I).print(OS);
+ }
+ break;
+ }
+}
+
+} // namespace ir2vec
+
+namespace mir2vec {
+
+bool MIR2VecTool::initializeVocabulary(const Module &M) {
+ MIR2VecVocabProvider Provider(MMI);
+ auto VocabOrErr = Provider.getVocabulary(M);
+ if (!VocabOrErr) {
+ WithColor::error(errs(), ToolName)
+ << "Failed to load MIR2Vec vocabulary - "
+ << toString(VocabOrErr.takeError()) << "\n";
+ return false;
+ }
+ Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
+ return true;
+}
+
+bool MIR2VecTool::initializeVocabularyForLayout(const Module &M) {
+ for (const Function &F : M.getFunctionDefs()) {
+ MachineFunction *MF = MMI.getMachineFunction(F);
+ if (!MF)
+ continue;
+
+ const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
+ const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
+ const MachineRegisterInfo &MRI = MF->getRegInfo();
+
+ auto VocabOrErr = MIRVocabulary::createDummyVocabForTest(TII, TRI, MRI, 1);
+ if (!VocabOrErr) {
+ WithColor::error(errs(), ToolName)
+ << "Failed to create dummy vocabulary - "
+ << toString(VocabOrErr.takeError()) << "\n";
+ return false;
+ }
+ Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
+ return true;
+ }
+
+ WithColor::error(errs(), ToolName)
+ << "No machine functions found to initialize vocabulary\n";
+ return false;
+}
+
+TripletResult MIR2VecTool::generateTriplets(const MachineFunction &MF) const {
+ TripletResult Result;
+ Result.MaxRelation = MIRNextRelation;
+
+ if (!Vocab) {
+ WithColor::error(errs(), ToolName)
+ << "MIR Vocabulary must be initialized for triplet generation.\n";
+ return Result;
+ }
+
+ unsigned PrevOpcode = 0;
+ bool HasPrevOpcode = false;
+ for (const MachineBasicBlock &MBB : MF) {
+ for (const MachineInstr &MI : MBB) {
+ // Skip debug instructions
+ if (MI.isDebugInstr())
+ continue;
+
+ // Get opcode entity ID
+ unsigned OpcodeID = Vocab->getEntityIDForOpcode(MI.getOpcode());
+
+ // Add "Next" relationship with previous instruction
+ if (HasPrevOpcode) {
+ Result.Triplets.push_back({PrevOpcode, OpcodeID, MIRNextRelation});
+ LLVM_DEBUG(dbgs() << Vocab->getStringKey(PrevOpcode) << '\t'
+ << Vocab->getStringKey(OpcodeID) << '\t' << "Next\n");
+ }
+
+ // Add "Arg" relationships for operands
+ unsigned ArgIndex = 0;
+ for (const MachineOperand &MO : MI.operands()) {
+ auto OperandID = Vocab->getEntityIDForMachineOperand(MO);
+ unsigned RelationID = MIRArgRelation + ArgIndex;
+ Result.Triplets.push_back({OpcodeID, OperandID, RelationID});
+ LLVM_DEBUG({
+ std::string OperandStr = Vocab->getStringKey(OperandID);
+ dbgs() << Vocab->getStringKey(OpcodeID) << '\t' << OperandStr << '\t'
+ << "Arg" << ArgIndex << '\n';
+ });
+
+ ++ArgIndex;
+ }
+
+ // Update MaxRelation if there were operands
+ if (ArgIndex > 0)
+ Result.MaxRelation =
+ std::max(Result.MaxRelation, MIRArgRelation + ArgIndex - 1);
+
+ PrevOpcode = OpcodeID;
+ HasPrevOpcode = true;
+ }
+ }
+
+ return Result;
+}
+
+TripletResult MIR2VecTool::generateTriplets(const Module &M) const {
+ TripletResult Result;
+ Result.MaxRelation = MIRNextRelation;
+
+ for (const Function &F : M.getFunctionDefs()) {
+ MachineFunction *MF = MMI.getMachineFunction(F);
+ if (!MF) {
+ WithColor::warning(errs(), ToolName)
+ << "No MachineFunction for " << F.getName() << "\n";
+ continue;
+ }
+
+ TripletResult FuncResult = generateTriplets(*MF);
+ Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
+ Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
+ FuncResult.Triplets.end());
+ }
+
+ return Result;
+}
+
+void MIR2VecTool::writeTripletsToStream(const Module &M,
+ raw_ostream &OS) const {
+ auto Result = generateTriplets(M);
+ OS << "MAX_RELATION=" << Result.MaxRelation << '\n';
+ for (const auto &T : Result.Triplets)
+ OS << T.Head << '\t' << T.Tail << '\t' << T.Relation << '\n';
+}
+
+EntityList MIR2VecTool::collectEntityMappings() const {
+ if (!Vocab) {
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary must be initialized for entity mappings.\n";
+ return {};
+ }
+
+ const unsigned EntityCount = Vocab->getCanonicalSize();
+ EntityList Result;
+ for (unsigned EntityID = 0; EntityID < EntityCount; ++EntityID)
+ Result.push_back(Vocab->getStringKey(EntityID));
+
+ return Result;
+}
+
+void MIR2VecTool::writeEntitiesToStream(raw_ostream &OS) const {
+ auto Entities = collectEntityMappings();
+ if (Entities.empty())
+ return;
+
+ OS << Entities.size() << "\n";
+ for (unsigned EntityID = 0; EntityID < Entities.size(); ++EntityID)
+ OS << Entities[EntityID] << '\t' << EntityID << '\n';
+}
+
+void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
+ EmbeddingLevel Level) const {
+ if (!Vocab) {
+ WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
+ return;
+ }
+
+ for (const Function &F : M.getFunctionDefs()) {
+ MachineFunction *MF = MMI.getMachineFunction(F);
+ if (!MF) {
+ WithColor::warning(errs(), ToolName)
+ << "No MachineFunction for " << F.getName() << "\n";
+ continue;
+ }
+
+ writeEmbeddingsToStream(*MF, OS, Level);
+ }
+}
+
+void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
+ EmbeddingLevel Level) const {
+ if (!Vocab) {
+ WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
+ return;
+ }
+
+ auto Emb = MIREmbedder::create(MIR2VecKind::Symbolic, MF, *Vocab);
+ if (!Emb) {
+ WithColor::error(errs(), ToolName)
+ << "Failed to create embedder for " << MF.getName() << "\n";
+ return;
+ }
+
+ OS << "MIR2Vec embeddings for machine function " << MF.getName() << ":\n";
+
+ // Generate embeddings based on the specified level
+ switch (Level) {
+ case FunctionLevel:
+ OS << "Function vector: ";
+ Emb->getMFunctionVector().print(OS);
+ break;
+ case BasicBlockLevel:
+ OS << "Basic block vectors:\n";
+ for (const MachineBasicBlock &MBB : MF) {
+ OS << "MBB " << MBB.getName() << ": ";
+ Emb->getMBBVector(MBB).print(OS);
+ }
+ break;
+ case InstructionLevel:
+ OS << "Instruction vectors:\n";
+ for (const MachineBasicBlock &MBB : MF) {
+ for (const MachineInstr &MI : MBB) {
+ OS << MI << " -> ";
+ Emb->getMInstVector(MI).print(OS);
+ }
+ }
+ break;
+ }
+}
+
+} // namespace mir2vec
+
+} // namespace llvm
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.h b/llvm/tools/llvm-ir2vec/emb-tool.h
similarity index 98%
rename from llvm/tools/llvm-ir2vec/llvm-ir2vec.h
rename to llvm/tools/llvm-ir2vec/emb-tool.h
index 566c362edbd22..009bcec60108b 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.h
+++ b/llvm/tools/llvm-ir2vec/emb-tool.h
@@ -12,8 +12,8 @@
///
//===----------------------------------------------------------------------===//
-#ifndef LLVM_TOOLS_LLVM_IR2VEC_LLVM_IR2VEC_H
-#define LLVM_TOOLS_LLVM_IR2VEC_LLVM_IR2VEC_H
+#ifndef LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
+#define LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
@@ -44,7 +44,7 @@
#define DEBUG_TYPE "ir2vec"
namespace llvm {
-
+
/// Tool name for error reporting
static const char *ToolName = "llvm-ir2vec";
@@ -198,4 +198,4 @@ struct MIRContext {
} // namespace llvm
-#endif // LLVM_TOOLS_LLVM_IR2VEC_LLVM_IR2VEC_H
\ No newline at end of file
+#endif // LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index b60488b2a1b6b..ca03e79c97c68 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -54,7 +54,7 @@
///
//===----------------------------------------------------------------------===//
-#include "llvm-ir2vec.h"
+#include "emb-tool.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
#include "llvm/IR/BasicBlock.h"
@@ -147,166 +147,7 @@ static cl::opt<EmbeddingLevel>
cl::init(FunctionLevel), cl::sub(EmbeddingsSubCmd),
cl::cat(CommonCategory));
-bool IR2VecTool::initializeVocabulary() {
- // Register and run the IR2Vec vocabulary analysis
- // The vocabulary file path is specified via --ir2vec-vocab-path global
- // option
- MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
- MAM.registerPass([&] { return IR2VecVocabAnalysis(); });
- // This will throw an error if vocab is not found or invalid
- Vocab = &MAM.getResult<IR2VecVocabAnalysis>(M);
- return Vocab->isValid();
-}
-
-TripletResult IR2VecTool::generateTriplets(const Function &F) const {
- if (F.isDeclaration())
- return {};
-
- TripletResult Result;
- Result.MaxRelation = 0;
-
- unsigned MaxRelation = NextRelation;
- unsigned PrevOpcode = 0;
- bool HasPrevOpcode = false;
-
- for (const BasicBlock &BB : F) {
- for (const auto &I : BB.instructionsWithoutDebug()) {
- unsigned Opcode = Vocabulary::getIndex(I.getOpcode());
- unsigned TypeID = Vocabulary::getIndex(I.getType()->getTypeID());
-
- // Add "Next" relationship with previous instruction
- if (HasPrevOpcode) {
- Result.Triplets.push_back({PrevOpcode, Opcode, NextRelation});
- LLVM_DEBUG(dbgs() << Vocabulary::getVocabKeyForOpcode(PrevOpcode + 1)
- << '\t'
- << Vocabulary::getVocabKeyForOpcode(Opcode + 1)
- << '\t' << "Next\n");
- }
-
- // Add "Type" relationship
- Result.Triplets.push_back({Opcode, TypeID, TypeRelation});
- LLVM_DEBUG(
- dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
- << Vocabulary::getVocabKeyForTypeID(I.getType()->getTypeID())
- << '\t' << "Type\n");
-
- // Add "Arg" relationships
- unsigned ArgIndex = 0;
- for (const Use &U : I.operands()) {
- unsigned OperandID = Vocabulary::getIndex(*U.get());
- unsigned RelationID = ArgRelation + ArgIndex;
- Result.Triplets.push_back({Opcode, OperandID, RelationID});
-
- LLVM_DEBUG({
- StringRef OperandStr = Vocabulary::getVocabKeyForOperandKind(
- Vocabulary::getOperandKind(U.get()));
- dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
- << OperandStr << '\t' << "Arg" << ArgIndex << '\n';
- });
-
- ++ArgIndex;
- }
- // Only update MaxRelation if there were operands
- if (ArgIndex > 0)
- MaxRelation = std::max(MaxRelation, ArgRelation + ArgIndex - 1);
- PrevOpcode = Opcode;
- HasPrevOpcode = true;
- }
- }
-
- Result.MaxRelation = MaxRelation;
- return Result;
-}
-
-TripletResult IR2VecTool::generateTriplets() const {
- TripletResult Result;
- Result.MaxRelation = NextRelation;
-
- for (const Function &F : M.getFunctionDefs()) {
- TripletResult FuncResult = generateTriplets(F);
- Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
- Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
- FuncResult.Triplets.end());
- }
-
- return Result;
-}
-
-void IR2VecTool::writeTripletsToStream(raw_ostream &OS) const {
- auto Result = generateTriplets();
- OS << "MAX_RELATION=" << Result.MaxRelation << '\n';
- for (const auto &T : Result.Triplets)
- OS << T.Head << '\t' << T.Tail << '\t' << T.Relation << '\n';
-}
-
-EntityList IR2VecTool::collectEntityMappings() {
- auto EntityLen = Vocabulary::getCanonicalSize();
- EntityList Result;
- for (unsigned EntityID = 0; EntityID < EntityLen; ++EntityID)
- Result.push_back(Vocabulary::getStringKey(EntityID).str());
- return Result;
-}
-
-void IR2VecTool::writeEntitiesToStream(raw_ostream &OS) {
- auto Entities = collectEntityMappings();
- OS << Entities.size() << "\n";
- for (unsigned EntityID = 0; EntityID < Entities.size(); ++EntityID)
- OS << Entities[EntityID] << '\t' << EntityID << '\n';
-}
-
-void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
- EmbeddingLevel Level) const {
- if (!Vocab->isValid()) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary is not valid. IR2VecTool not initialized.\n";
- return;
- }
-
- for (const Function &F : M.getFunctionDefs())
- writeEmbeddingsToStream(F, OS, Level);
-}
-
-void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
- EmbeddingLevel Level) const {
- if (!Vocab || !Vocab->isValid()) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary is not valid. IR2VecTool not initialized.\n";
- return;
- }
- if (F.isDeclaration()) {
- OS << "Function " << F.getName() << " is a declaration, skipping.\n";
- return;
- }
-
- // Create embedder for this function
- auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab);
- if (!Emb) {
- WithColor::error(errs(), ToolName)
- << "Failed to create embedder for function " << F.getName() << "\n";
- return;
- }
-
- OS << "Function: " << F.getName() << "\n";
-
- // Generate embeddings based on the specified level
- switch (Level) {
- case FunctionLevel:
- Emb->getFunctionVector().print(OS);
- break;
- case BasicBlockLevel:
- for (const BasicBlock &BB : F) {
- OS << BB.getName() << ":";
- Emb->getBBVector(BB).print(OS);
- }
- break;
- case InstructionLevel:
- for (const Instruction &I : instructions(F)) {
- OS << I;
- Emb->getInstVector(I).print(OS);
- }
- break;
- }
-}
+namespace ir2vec {
/// Process the module and generate output based on selected subcommand
static Error processModule(Module &M, raw_ostream &OS) {
@@ -338,215 +179,6 @@ static Error processModule(Module &M, raw_ostream &OS) {
return Error::success();
}
-bool MIR2VecTool::initializeVocabulary(const Module &M) {
- MIR2VecVocabProvider Provider(MMI);
- auto VocabOrErr = Provider.getVocabulary(M);
- if (!VocabOrErr) {
- WithColor::error(errs(), ToolName)
- << "Failed to load MIR2Vec vocabulary - "
- << toString(VocabOrErr.takeError()) << "\n";
- return false;
- }
- Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
- return true;
-}
-
-bool MIR2VecTool::initializeVocabularyForLayout(const Module &M) {
- for (const Function &F : M.getFunctionDefs()) {
- MachineFunction *MF = MMI.getMachineFunction(F);
- if (!MF)
- continue;
-
- const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
- const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
- const MachineRegisterInfo &MRI = MF->getRegInfo();
-
- auto VocabOrErr = MIRVocabulary::createDummyVocabForTest(TII, TRI, MRI, 1);
- if (!VocabOrErr) {
- WithColor::error(errs(), ToolName)
- << "Failed to create dummy vocabulary - "
- << toString(VocabOrErr.takeError()) << "\n";
- return false;
- }
- Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
- return true;
- }
-
- WithColor::error(errs(), ToolName)
- << "No machine functions found to initialize vocabulary\n";
- return false;
-}
-
-TripletResult MIR2VecTool::generateTriplets(const MachineFunction &MF) const {
- TripletResult Result;
- Result.MaxRelation = MIRNextRelation;
-
- if (!Vocab) {
- WithColor::error(errs(), ToolName)
- << "MIR Vocabulary must be initialized for triplet generation.\n";
- return Result;
- }
-
- unsigned PrevOpcode = 0;
- bool HasPrevOpcode = false;
- for (const MachineBasicBlock &MBB : MF) {
- for (const MachineInstr &MI : MBB) {
- // Skip debug instructions
- if (MI.isDebugInstr())
- continue;
-
- // Get opcode entity ID
- unsigned OpcodeID = Vocab->getEntityIDForOpcode(MI.getOpcode());
-
- // Add "Next" relationship with previous instruction
- if (HasPrevOpcode) {
- Result.Triplets.push_back({PrevOpcode, OpcodeID, MIRNextRelation});
- LLVM_DEBUG(dbgs() << Vocab->getStringKey(PrevOpcode) << '\t'
- << Vocab->getStringKey(OpcodeID) << '\t' << "Next\n");
- }
-
- // Add "Arg" relationships for operands
- unsigned ArgIndex = 0;
- for (const MachineOperand &MO : MI.operands()) {
- auto OperandID = Vocab->getEntityIDForMachineOperand(MO);
- unsigned RelationID = MIRArgRelation + ArgIndex;
- Result.Triplets.push_back({OpcodeID, OperandID, RelationID});
- LLVM_DEBUG({
- std::string OperandStr = Vocab->getStringKey(OperandID);
- dbgs() << Vocab->getStringKey(OpcodeID) << '\t' << OperandStr << '\t'
- << "Arg" << ArgIndex << '\n';
- });
-
- ++ArgIndex;
- }
-
- // Update MaxRelation if there were operands
- if (ArgIndex > 0)
- Result.MaxRelation =
- std::max(Result.MaxRelation, MIRArgRelation + ArgIndex - 1);
-
- PrevOpcode = OpcodeID;
- HasPrevOpcode = true;
- }
- }
-
- return Result;
-}
-
-TripletResult MIR2VecTool::generateTriplets(const Module &M) const {
- TripletResult Result;
- Result.MaxRelation = MIRNextRelation;
-
- for (const Function &F : M.getFunctionDefs()) {
- MachineFunction *MF = MMI.getMachineFunction(F);
- if (!MF) {
- WithColor::warning(errs(), ToolName)
- << "No MachineFunction for " << F.getName() << "\n";
- continue;
- }
-
- TripletResult FuncResult = generateTriplets(*MF);
- Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
- Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
- FuncResult.Triplets.end());
- }
-
- return Result;
-}
-
-void MIR2VecTool::writeTripletsToStream(const Module &M,
- raw_ostream &OS) const {
- auto Result = generateTriplets(M);
- OS << "MAX_RELATION=" << Result.MaxRelation << '\n';
- for (const auto &T : Result.Triplets)
- OS << T.Head << '\t' << T.Tail << '\t' << T.Relation << '\n';
-}
-
-EntityList MIR2VecTool::collectEntityMappings() const {
- if (!Vocab) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary must be initialized for entity mappings.\n";
- return {};
- }
-
- const unsigned EntityCount = Vocab->getCanonicalSize();
- EntityList Result;
- for (unsigned EntityID = 0; EntityID < EntityCount; ++EntityID)
- Result.push_back(Vocab->getStringKey(EntityID));
-
- return Result;
-}
-
-void MIR2VecTool::writeEntitiesToStream(raw_ostream &OS) const {
- auto Entities = collectEntityMappings();
- if (Entities.empty())
- return;
-
- OS << Entities.size() << "\n";
- for (unsigned EntityID = 0; EntityID < Entities.size(); ++EntityID)
- OS << Entities[EntityID] << '\t' << EntityID << '\n';
-}
-
-void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
- EmbeddingLevel Level) const {
- if (!Vocab) {
- WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
- return;
- }
-
- for (const Function &F : M.getFunctionDefs()) {
- MachineFunction *MF = MMI.getMachineFunction(F);
- if (!MF) {
- WithColor::warning(errs(), ToolName)
- << "No MachineFunction for " << F.getName() << "\n";
- continue;
- }
-
- writeEmbeddingsToStream(*MF, OS, Level);
- }
-}
-
-void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
- EmbeddingLevel Level) const {
- if (!Vocab) {
- WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
- return;
- }
-
- auto Emb = MIREmbedder::create(MIR2VecKind::Symbolic, MF, *Vocab);
- if (!Emb) {
- WithColor::error(errs(), ToolName)
- << "Failed to create embedder for " << MF.getName() << "\n";
- return;
- }
-
- OS << "MIR2Vec embeddings for machine function " << MF.getName() << ":\n";
-
- // Generate embeddings based on the specified level
- switch (Level) {
- case FunctionLevel:
- OS << "Function vector: ";
- Emb->getMFunctionVector().print(OS);
- break;
- case BasicBlockLevel:
- OS << "Basic block vectors:\n";
- for (const MachineBasicBlock &MBB : MF) {
- OS << "MBB " << MBB.getName() << ": ";
- Emb->getMBBVector(MBB).print(OS);
- }
- break;
- case InstructionLevel:
- OS << "Instruction vectors:\n";
- for (const MachineBasicBlock &MBB : MF) {
- for (const MachineInstr &MI : MBB) {
- OS << MI << " -> ";
- Emb->getMInstVector(MI).print(OS);
- }
- }
- break;
- }
-}
-
/// Setup MIR context from input file
static Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
SMDiagnostic Err;
>From 5831e586803a749399a3a1b01fe27ced07e6da29 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 21 Dec 2025 22:32:35 +0530
Subject: [PATCH 02/21] nit commit - code formatting fixup
---
llvm/tools/llvm-ir2vec/emb-tool.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-ir2vec/emb-tool.h b/llvm/tools/llvm-ir2vec/emb-tool.h
index 009bcec60108b..24b8022ed7482 100644
--- a/llvm/tools/llvm-ir2vec/emb-tool.h
+++ b/llvm/tools/llvm-ir2vec/emb-tool.h
@@ -44,7 +44,7 @@
#define DEBUG_TYPE "ir2vec"
namespace llvm {
-
+
/// Tool name for error reporting
static const char *ToolName = "llvm-ir2vec";
>From 76bc40838728536db3b5b74e6528a9786a4e7a66 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Thu, 1 Jan 2026 21:04:03 +0530
Subject: [PATCH 03/21] renaming emb-tool files to utils files
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 2 +-
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 2 +-
llvm/tools/llvm-ir2vec/{emb-tool.cpp => utils.cpp} | 2 +-
llvm/tools/llvm-ir2vec/{emb-tool.h => utils.h} | 0
4 files changed, 3 insertions(+), 3 deletions(-)
rename llvm/tools/llvm-ir2vec/{emb-tool.cpp => utils.cpp} (99%)
rename llvm/tools/llvm-ir2vec/{emb-tool.h => utils.h} (100%)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 9d5db8663fb38..1c9ab9b7fb173 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -19,7 +19,7 @@ set(LLVM_LINK_COMPONENTS
add_llvm_tool(llvm-ir2vec
llvm-ir2vec.cpp
- emb-tool.cpp
+ utils.cpp
DEPENDS
intrinsics_gen
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index ca03e79c97c68..62c904384f18a 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -54,7 +54,7 @@
///
//===----------------------------------------------------------------------===//
-#include "emb-tool.h"
+#include "utils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
#include "llvm/IR/BasicBlock.h"
diff --git a/llvm/tools/llvm-ir2vec/emb-tool.cpp b/llvm/tools/llvm-ir2vec/utils.cpp
similarity index 99%
rename from llvm/tools/llvm-ir2vec/emb-tool.cpp
rename to llvm/tools/llvm-ir2vec/utils.cpp
index 891b26f8ef763..d938ae4abd236 100644
--- a/llvm/tools/llvm-ir2vec/emb-tool.cpp
+++ b/llvm/tools/llvm-ir2vec/utils.cpp
@@ -12,7 +12,7 @@
///
//===----------------------------------------------------------------------===//
-#include "emb-tool.h"
+#include "utils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
#include "llvm/IR/BasicBlock.h"
diff --git a/llvm/tools/llvm-ir2vec/emb-tool.h b/llvm/tools/llvm-ir2vec/utils.h
similarity index 100%
rename from llvm/tools/llvm-ir2vec/emb-tool.h
rename to llvm/tools/llvm-ir2vec/utils.h
>From afb75574138c1d56a0820ec648a3a6906f29882b Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Thu, 1 Jan 2026 22:11:10 +0530
Subject: [PATCH 04/21] Nit commit, formatting fixups
---
llvm/tools/llvm-ir2vec/utils.cpp | 4 ++--
llvm/tools/llvm-ir2vec/utils.h | 6 +++---
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/utils.cpp b/llvm/tools/llvm-ir2vec/utils.cpp
index d938ae4abd236..51c301bc0dbcb 100644
--- a/llvm/tools/llvm-ir2vec/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils.cpp
@@ -1,4 +1,4 @@
-//===- emb-tool.cpp - IR2Vec/MIR2Vec Embedding Generation Tool -----------===//
+//===- utils.cpp - IR2Vec/MIR2Vec 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.
@@ -418,4 +418,4 @@ void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
} // namespace mir2vec
-} // namespace llvm
\ No newline at end of file
+} // namespace llvm
diff --git a/llvm/tools/llvm-ir2vec/utils.h b/llvm/tools/llvm-ir2vec/utils.h
index 24b8022ed7482..1fcddb211259a 100644
--- a/llvm/tools/llvm-ir2vec/utils.h
+++ b/llvm/tools/llvm-ir2vec/utils.h
@@ -12,8 +12,8 @@
///
//===----------------------------------------------------------------------===//
-#ifndef LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
-#define LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
+#ifndef LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
+#define LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
@@ -198,4 +198,4 @@ struct MIRContext {
} // namespace llvm
-#endif // LLVM_TOOLS_LLVM_IR2VEC_EMB_TOOL_H
\ No newline at end of file
+#endif // LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
>From 5f1d1a5809fe48190a0bc8f8ebe4f83fffb8727e Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Tue, 6 Jan 2026 20:21:51 +0530
Subject: [PATCH 05/21] Work Commit - Moving utils files to a different folder,
and using them as a lib object. This will help keep the build process and
code minimal for the upcoming python bindings
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 46 ++++++++++----------
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 2 +-
llvm/tools/llvm-ir2vec/{ => utils}/utils.cpp | 43 ++++++++----------
llvm/tools/llvm-ir2vec/{ => utils}/utils.h | 6 +--
4 files changed, 43 insertions(+), 54 deletions(-)
rename llvm/tools/llvm-ir2vec/{ => utils}/utils.cpp (89%)
rename llvm/tools/llvm-ir2vec/{ => utils}/utils.h (98%)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 1c9ab9b7fb173..a009881c1c58c 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -1,26 +1,24 @@
set(LLVM_LINK_COMPONENTS
- # Core LLVM components for IR processing
- Analysis
- Core
- IRReader
- Support
-
- # Machine IR components (for -mode=mir)
- CodeGen
- MIRParser
-
- # Target initialization (required for MIR parsing)
- AllTargetsAsmParsers
- AllTargetsCodeGens
- AllTargetsDescs
- AllTargetsInfos
- TargetParser
- )
+ Analysis Core Demangle IRReader Support CodeGen MIRParser
+ AllTargetsAsmParsers AllTargetsCodeGens AllTargetsDescs
+ AllTargetsInfos TargetParser
+)
-add_llvm_tool(llvm-ir2vec
- llvm-ir2vec.cpp
- utils.cpp
-
- DEPENDS
- intrinsics_gen
- )
+# Static utility library
+add_library(ir2vec_utils STATIC utils/utils.cpp)
+
+target_include_directories(ir2vec_utils
+ PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/utils
+ PRIVATE ${LLVM_INCLUDE_DIRS}
+)
+
+target_link_libraries(ir2vec_utils PRIVATE
+ LLVMAnalysis LLVMCore LLVMSupport LLVMIRReader
+ LLVMAsmParser LLVMPasses LLVMDemangle
+)
+
+target_compile_definitions(ir2vec_utils PRIVATE ${LLVM_DEFINITIONS})
+
+# Main executable
+add_llvm_tool(llvm-ir2vec llvm-ir2vec.cpp DEPENDS intrinsics_gen)
+target_link_libraries(llvm-ir2vec PRIVATE ir2vec_utils)
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index 62c904384f18a..c850ad1a20765 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -390,4 +390,4 @@ int main(int argc, char **argv) {
}
return 0;
-}
+}
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
similarity index 89%
rename from llvm/tools/llvm-ir2vec/utils.cpp
rename to llvm/tools/llvm-ir2vec/utils/utils.cpp
index 51c301bc0dbcb..26ff2e00ee355 100644
--- a/llvm/tools/llvm-ir2vec/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -10,7 +10,7 @@
/// This file implements the IR2VecTool and MIR2VecTool classes for
/// IR2Vec/MIR2Vec embedding generation.
///
-//===----------------------------------------------------------------------===//
+//===------------------------------------------------------------------Fail----===//
#include "utils.h"
#include "llvm/ADT/ArrayRef.h"
@@ -33,7 +33,6 @@
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
-#include "llvm/Support/WithColor.h"
#include "llvm/Target/TargetMachine.h"
#define DEBUG_TYPE "ir2vec"
@@ -152,8 +151,7 @@ void IR2VecTool::writeEntitiesToStream(raw_ostream &OS) {
void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab->isValid()) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary is not valid. IR2VecTool not initialized.\n";
+ errs() << "Error: Vocabulary is not valid. IR2VecTool not initialized.\n";
return;
}
@@ -164,8 +162,7 @@ void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab || !Vocab->isValid()) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary is not valid. IR2VecTool not initialized.\n";
+ errs() << "Error: Vocabulary is not valid. IR2VecTool not initialized.\n";
return;
}
if (F.isDeclaration()) {
@@ -176,8 +173,8 @@ void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
// Create embedder for this function
auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab);
if (!Emb) {
- WithColor::error(errs(), ToolName)
- << "Failed to create embedder for function " << F.getName() << "\n";
+ errs() << "Error: Failed to create embedder for function " << F.getName()
+ << "\n";
return;
}
@@ -232,17 +229,15 @@ bool MIR2VecTool::initializeVocabularyForLayout(const Module &M) {
auto VocabOrErr = MIRVocabulary::createDummyVocabForTest(TII, TRI, MRI, 1);
if (!VocabOrErr) {
- WithColor::error(errs(), ToolName)
- << "Failed to create dummy vocabulary - "
- << toString(VocabOrErr.takeError()) << "\n";
+ errs() << "Error: Failed to create dummy vocabulary - "
+ << toString(VocabOrErr.takeError()) << "\n";
return false;
}
Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
return true;
}
- WithColor::error(errs(), ToolName)
- << "No machine functions found to initialize vocabulary\n";
+ errs() << "Error: No machine functions found to initialize vocabulary\n";
return false;
}
@@ -251,8 +246,8 @@ TripletResult MIR2VecTool::generateTriplets(const MachineFunction &MF) const {
Result.MaxRelation = MIRNextRelation;
if (!Vocab) {
- WithColor::error(errs(), ToolName)
- << "MIR Vocabulary must be initialized for triplet generation.\n";
+ errs() << "Error: MIR Vocabulary must be initialized for triplet "
+ "generation.\n";
return Result;
}
@@ -309,8 +304,7 @@ TripletResult MIR2VecTool::generateTriplets(const Module &M) const {
for (const Function &F : M.getFunctionDefs()) {
MachineFunction *MF = MMI.getMachineFunction(F);
if (!MF) {
- WithColor::warning(errs(), ToolName)
- << "No MachineFunction for " << F.getName() << "\n";
+ errs() << "Warning: No MachineFunction for " << F.getName() << "\n";
continue;
}
@@ -333,8 +327,7 @@ void MIR2VecTool::writeTripletsToStream(const Module &M,
EntityList MIR2VecTool::collectEntityMappings() const {
if (!Vocab) {
- WithColor::error(errs(), ToolName)
- << "Vocabulary must be initialized for entity mappings.\n";
+ errs() << "Error: Vocabulary must be initialized for entity mappings.\n";
return {};
}
@@ -359,15 +352,14 @@ void MIR2VecTool::writeEntitiesToStream(raw_ostream &OS) const {
void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab) {
- WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
+ errs() << "Error: Vocabulary not initialized.\n";
return;
}
for (const Function &F : M.getFunctionDefs()) {
MachineFunction *MF = MMI.getMachineFunction(F);
if (!MF) {
- WithColor::warning(errs(), ToolName)
- << "No MachineFunction for " << F.getName() << "\n";
+ errs() << "Warning: No MachineFunction for " << F.getName() << "\n";
continue;
}
@@ -378,14 +370,13 @@ void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab) {
- WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
+ errs() << "Error: Vocabulary not initialized.\n";
return;
}
auto Emb = MIREmbedder::create(MIR2VecKind::Symbolic, MF, *Vocab);
if (!Emb) {
- WithColor::error(errs(), ToolName)
- << "Failed to create embedder for " << MF.getName() << "\n";
+ errs() << "Error: Failed to create embedder for " << MF.getName() << "\n";
return;
}
@@ -418,4 +409,4 @@ void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
} // namespace mir2vec
-} // namespace llvm
+} // namespace llvm
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/utils.h b/llvm/tools/llvm-ir2vec/utils/utils.h
similarity index 98%
rename from llvm/tools/llvm-ir2vec/utils.h
rename to llvm/tools/llvm-ir2vec/utils/utils.h
index 1fcddb211259a..13ce3e500ee2d 100644
--- a/llvm/tools/llvm-ir2vec/utils.h
+++ b/llvm/tools/llvm-ir2vec/utils/utils.h
@@ -12,8 +12,8 @@
///
//===----------------------------------------------------------------------===//
-#ifndef LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
-#define LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
+#ifndef LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
+#define LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
@@ -198,4 +198,4 @@ struct MIRContext {
} // namespace llvm
-#endif // LLVM_TOOLS_LLVM_IR2VEC_UTILS_H
+#endif // LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
\ No newline at end of file
>From afc9d623e5a37717bb5ee222e02a4e20f5dfcbdf Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Tue, 6 Jan 2026 20:23:37 +0530
Subject: [PATCH 06/21] Nit commit - formatting commit
---
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 2 +-
llvm/tools/llvm-ir2vec/utils/utils.cpp | 2 +-
llvm/tools/llvm-ir2vec/utils/utils.h | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index c850ad1a20765..62c904384f18a 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -390,4 +390,4 @@ int main(int argc, char **argv) {
}
return 0;
-}
\ No newline at end of file
+}
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
index 26ff2e00ee355..e7b0f02cb4069 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -409,4 +409,4 @@ void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
} // namespace mir2vec
-} // namespace llvm
\ No newline at end of file
+} // namespace llvm
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.h b/llvm/tools/llvm-ir2vec/utils/utils.h
index 13ce3e500ee2d..58a4551947536 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.h
+++ b/llvm/tools/llvm-ir2vec/utils/utils.h
@@ -198,4 +198,4 @@ struct MIRContext {
} // namespace llvm
-#endif // LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
\ No newline at end of file
+#endif // LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
>From a794a590bf4efcb0305b893b97a36088640c68f4 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Tue, 6 Jan 2026 20:27:52 +0530
Subject: [PATCH 07/21] Nit commit - Cmakelists.txt code layout fixup
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index a009881c1c58c..eab03d51d7c6a 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -1,8 +1,21 @@
set(LLVM_LINK_COMPONENTS
- Analysis Core Demangle IRReader Support CodeGen MIRParser
- AllTargetsAsmParsers AllTargetsCodeGens AllTargetsDescs
- AllTargetsInfos TargetParser
-)
+ # Core LLVM components for IR processing
+ Analysis
+ Core
+ IRReader
+ Support
+
+ # Machine IR components (for -mode=mir)
+ CodeGen
+ MIRParser
+
+ # Target initialization (required for MIR parsing)
+ AllTargetsAsmParsers
+ AllTargetsCodeGens
+ AllTargetsDescs
+ AllTargetsInfos
+ TargetParser
+ )
# Static utility library
add_library(ir2vec_utils STATIC utils/utils.cpp)
>From 090112a5f5cadfe516a05f785868ecde47fbc0c9 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Tue, 6 Jan 2026 20:42:32 +0530
Subject: [PATCH 08/21] Nit commit - changing ir2vec_utils to emb_utils
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index eab03d51d7c6a..49c45bd2d345d 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -18,20 +18,26 @@ set(LLVM_LINK_COMPONENTS
)
# Static utility library
-add_library(ir2vec_utils STATIC utils/utils.cpp)
+add_library(emb_utils STATIC utils/utils.cpp)
-target_include_directories(ir2vec_utils
+target_include_directories(emb_utils
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/utils
PRIVATE ${LLVM_INCLUDE_DIRS}
)
-target_link_libraries(ir2vec_utils PRIVATE
+target_link_libraries(emb_utils PRIVATE
LLVMAnalysis LLVMCore LLVMSupport LLVMIRReader
LLVMAsmParser LLVMPasses LLVMDemangle
)
-target_compile_definitions(ir2vec_utils PRIVATE ${LLVM_DEFINITIONS})
+target_compile_definitions(emb_utils PRIVATE ${LLVM_DEFINITIONS})
# Main executable
-add_llvm_tool(llvm-ir2vec llvm-ir2vec.cpp DEPENDS intrinsics_gen)
-target_link_libraries(llvm-ir2vec PRIVATE ir2vec_utils)
\ No newline at end of file
+add_llvm_tool(llvm-ir2vec
+ llvm-ir2vec.cpp
+
+ DEPENDS
+ intrinsics_gen
+ )
+
+target_link_libraries(llvm-ir2vec PRIVATE emb_utils)
>From f8fb6f8510c0a8515472ba599c64337b7b78c733 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Wed, 7 Jan 2026 01:11:44 +0530
Subject: [PATCH 09/21] Fixup commit - warning during llvm-ir2vec build causing
failed CI job. fixed
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 2 --
1 file changed, 2 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 49c45bd2d345d..3eccffd350d5e 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -30,8 +30,6 @@ target_link_libraries(emb_utils PRIVATE
LLVMAsmParser LLVMPasses LLVMDemangle
)
-target_compile_definitions(emb_utils PRIVATE ${LLVM_DEFINITIONS})
-
# Main executable
add_llvm_tool(llvm-ir2vec
llvm-ir2vec.cpp
>From c0e283986ca59a41b6bf96223de5a55052991f7a Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Wed, 7 Jan 2026 14:27:02 +0530
Subject: [PATCH 10/21] Nit commit - stray typo in utils.cpp file
---
llvm/tools/llvm-ir2vec/utils/utils.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
index e7b0f02cb4069..c299b8d490cb4 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -10,7 +10,7 @@
/// This file implements the IR2VecTool and MIR2VecTool classes for
/// IR2Vec/MIR2Vec embedding generation.
///
-//===------------------------------------------------------------------Fail----===//
+//===----------------------------------------------------------------------===//
#include "utils.h"
#include "llvm/ADT/ArrayRef.h"
>From 5810606e63ec28d4a3df669f8a5965d7bc38b5b6 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Fri, 9 Jan 2026 20:32:27 +0530
Subject: [PATCH 11/21] Building llvm-ir2vec utils lib from its own directory
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 19 +++++--------------
llvm/tools/llvm-ir2vec/utils/CMakeLists.txt | 8 ++++++++
2 files changed, 13 insertions(+), 14 deletions(-)
create mode 100644 llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 3eccffd350d5e..55a11a7f66927 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -17,18 +17,8 @@ set(LLVM_LINK_COMPONENTS
TargetParser
)
-# Static utility library
-add_library(emb_utils STATIC utils/utils.cpp)
-
-target_include_directories(emb_utils
- PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/utils
- PRIVATE ${LLVM_INCLUDE_DIRS}
-)
-
-target_link_libraries(emb_utils PRIVATE
- LLVMAnalysis LLVMCore LLVMSupport LLVMIRReader
- LLVMAsmParser LLVMPasses LLVMDemangle
-)
+# Add the utils subdirectory for the library
+add_subdirectory(utils)
# Main executable
add_llvm_tool(llvm-ir2vec
@@ -36,6 +26,7 @@ add_llvm_tool(llvm-ir2vec
DEPENDS
intrinsics_gen
- )
+)
-target_link_libraries(llvm-ir2vec PRIVATE emb_utils)
+target_include_directories(llvm-ir2vec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/utils)
+target_link_libraries(llvm-ir2vec PRIVATE emb_utils)
\ No newline at end of file
diff --git a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
new file mode 100644
index 0000000000000..3b300ce8e6ff8
--- /dev/null
+++ b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_llvm_library(emb_utils STATIC
+ utils.cpp
+)
+
+target_link_libraries(emb_utils PRIVATE
+ LLVMAnalysis LLVMCore LLVMSupport LLVMIRReader
+ LLVMAsmParser LLVMPasses LLVMDemangle
+)
\ No newline at end of file
>From ee8515d6f9a797a875c47a4314560356ce6a123b Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Fri, 9 Jan 2026 20:35:08 +0530
Subject: [PATCH 12/21] Work Commit - restoring WithColor usage for error
messages for writeEmbeddingsToStream methods
---
llvm/tools/llvm-ir2vec/utils/utils.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
index c299b8d490cb4..9539ff3d09da1 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -352,7 +352,7 @@ void MIR2VecTool::writeEntitiesToStream(raw_ostream &OS) const {
void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab) {
- errs() << "Error: Vocabulary not initialized.\n";
+ WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
return;
}
@@ -370,7 +370,7 @@ void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab) {
- errs() << "Error: Vocabulary not initialized.\n";
+ WithColor::error(errs(), ToolName) << "Vocabulary not initialized.\n";
return;
}
>From 2c529ca2e5f01bfb77ed92207a0d4133c2e2e715 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 11 Jan 2026 16:48:03 +0530
Subject: [PATCH 13/21] Nit commit - changes to linking code and formatting
edits
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 2 +-
llvm/tools/llvm-ir2vec/utils/CMakeLists.txt | 14 +++++++++-----
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 55a11a7f66927..dfee676d8a148 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -29,4 +29,4 @@ add_llvm_tool(llvm-ir2vec
)
target_include_directories(llvm-ir2vec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/utils)
-target_link_libraries(llvm-ir2vec PRIVATE emb_utils)
\ No newline at end of file
+target_link_libraries(llvm-ir2vec PRIVATE emb_utils)
diff --git a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
index 3b300ce8e6ff8..1d51fb3cd15fc 100644
--- a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
@@ -1,8 +1,12 @@
add_llvm_library(emb_utils STATIC
utils.cpp
+
+ LINK_COMPONENTS
+ Analysis
+ Core
+ Support
+ IRReader
+ AsmParser
+ Passes
+ Demangle
)
-
-target_link_libraries(emb_utils PRIVATE
- LLVMAnalysis LLVMCore LLVMSupport LLVMIRReader
- LLVMAsmParser LLVMPasses LLVMDemangle
-)
\ No newline at end of file
>From be9ea2c998988feb492595e80a3c3a67052d7a6e Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 11 Jan 2026 22:41:35 +0530
Subject: [PATCH 14/21] Nit commit - removing all errs() dump calls
---
llvm/tools/llvm-ir2vec/utils/utils.cpp | 34 ++++++++++++++++----------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
index 9539ff3d09da1..ded2c651d3de7 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -151,7 +151,8 @@ void IR2VecTool::writeEntitiesToStream(raw_ostream &OS) {
void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab->isValid()) {
- errs() << "Error: Vocabulary is not valid. IR2VecTool not initialized.\n";
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary is not valid. IR2VecTool not initialized.\n";
return;
}
@@ -162,7 +163,8 @@ void IR2VecTool::writeEmbeddingsToStream(raw_ostream &OS,
void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
EmbeddingLevel Level) const {
if (!Vocab || !Vocab->isValid()) {
- errs() << "Error: Vocabulary is not valid. IR2VecTool not initialized.\n";
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary is not valid. IR2VecTool not initialized.\n";
return;
}
if (F.isDeclaration()) {
@@ -173,8 +175,8 @@ void IR2VecTool::writeEmbeddingsToStream(const Function &F, raw_ostream &OS,
// Create embedder for this function
auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab);
if (!Emb) {
- errs() << "Error: Failed to create embedder for function " << F.getName()
- << "\n";
+ WithColor::error(errs(), ToolName)
+ << "Failed to create embedder for function " << F.getName() << "\n";
return;
}
@@ -229,15 +231,17 @@ bool MIR2VecTool::initializeVocabularyForLayout(const Module &M) {
auto VocabOrErr = MIRVocabulary::createDummyVocabForTest(TII, TRI, MRI, 1);
if (!VocabOrErr) {
- errs() << "Error: Failed to create dummy vocabulary - "
- << toString(VocabOrErr.takeError()) << "\n";
+ WithColor::error(errs(), ToolName)
+ << "Failed to create dummy vocabulary - "
+ << toString(VocabOrErr.takeError()) << "\n";
return false;
}
Vocab = std::make_unique<MIRVocabulary>(std::move(*VocabOrErr));
return true;
}
- errs() << "Error: No machine functions found to initialize vocabulary\n";
+ WithColor::error(errs(), ToolName)
+ << "No machine functions found to initialize vocabulary\n";
return false;
}
@@ -246,8 +250,8 @@ TripletResult MIR2VecTool::generateTriplets(const MachineFunction &MF) const {
Result.MaxRelation = MIRNextRelation;
if (!Vocab) {
- errs() << "Error: MIR Vocabulary must be initialized for triplet "
- "generation.\n";
+ WithColor::error(errs(), ToolName)
+ << "MIR Vocabulary must be initialized for triplet generation.\n";
return Result;
}
@@ -304,7 +308,8 @@ TripletResult MIR2VecTool::generateTriplets(const Module &M) const {
for (const Function &F : M.getFunctionDefs()) {
MachineFunction *MF = MMI.getMachineFunction(F);
if (!MF) {
- errs() << "Warning: No MachineFunction for " << F.getName() << "\n";
+ WithColor::warning(errs(), ToolName)
+ << "No MachineFunction for " << F.getName() << "\n";
continue;
}
@@ -327,7 +332,8 @@ void MIR2VecTool::writeTripletsToStream(const Module &M,
EntityList MIR2VecTool::collectEntityMappings() const {
if (!Vocab) {
- errs() << "Error: Vocabulary must be initialized for entity mappings.\n";
+ WithColor::error(errs(), ToolName)
+ << "Vocabulary must be initialized for entity mappings.\n";
return {};
}
@@ -359,7 +365,8 @@ void MIR2VecTool::writeEmbeddingsToStream(const Module &M, raw_ostream &OS,
for (const Function &F : M.getFunctionDefs()) {
MachineFunction *MF = MMI.getMachineFunction(F);
if (!MF) {
- errs() << "Warning: No MachineFunction for " << F.getName() << "\n";
+ WithColor::warning(errs(), ToolName)
+ << "No MachineFunction for " << F.getName() << "\n";
continue;
}
@@ -376,7 +383,8 @@ void MIR2VecTool::writeEmbeddingsToStream(MachineFunction &MF, raw_ostream &OS,
auto Emb = MIREmbedder::create(MIR2VecKind::Symbolic, MF, *Vocab);
if (!Emb) {
- errs() << "Error: Failed to create embedder for " << MF.getName() << "\n";
+ WithColor::error(errs(), ToolName)
+ << "Failed to create embedder for " << MF.getName() << "\n";
return;
}
>From 4b81876df0bd3fe0fb19ec3e0dbd5291298dc75e Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 11 Jan 2026 22:53:07 +0530
Subject: [PATCH 15/21] Work Commit - minimizing header listing for llvm-ir2vec
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index dfee676d8a148..19b8317166dc8 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -1,20 +1,10 @@
set(LLVM_LINK_COMPONENTS
- # Core LLVM components for IR processing
- Analysis
- Core
- IRReader
- Support
-
# Machine IR components (for -mode=mir)
- CodeGen
- MIRParser
+ MIRParser
# Target initialization (required for MIR parsing)
AllTargetsAsmParsers
AllTargetsCodeGens
- AllTargetsDescs
- AllTargetsInfos
- TargetParser
)
# Add the utils subdirectory for the library
>From c5a7b8df5e0ccfc0b7a48195ec22299715936bdc Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Sun, 11 Jan 2026 22:58:02 +0530
Subject: [PATCH 16/21] Work Commit - minimizing library linkage for utils lib
---
llvm/tools/llvm-ir2vec/utils/CMakeLists.txt | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
index 1d51fb3cd15fc..5b50b037486ca 100644
--- a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
@@ -1,12 +1,3 @@
add_llvm_library(emb_utils STATIC
utils.cpp
-
- LINK_COMPONENTS
- Analysis
- Core
- Support
- IRReader
- AsmParser
- Passes
- Demangle
)
>From 853c2396d6ad8352ae408b7b944b0bc7ea057923 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Tue, 13 Jan 2026 19:05:32 +0530
Subject: [PATCH 17/21] Merge conflict resolution fixup, nit changes
---
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 33 ++++++++++++++++----------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index 62c904384f18a..95eaf0cd7fa32 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -89,9 +89,7 @@
#define DEBUG_TYPE "ir2vec"
-using namespace llvm;
-using namespace llvm::ir2vec;
-using namespace llvm::mir2vec;
+namespace llvm {
// Common option category for options shared between IR2Vec and MIR2Vec
static cl::OptionCategory CommonCategory("Common Options",
@@ -150,7 +148,7 @@ static cl::opt<EmbeddingLevel>
namespace ir2vec {
/// Process the module and generate output based on selected subcommand
-static Error processModule(Module &M, raw_ostream &OS) {
+Error processModule(Module &M, raw_ostream &OS) {
IR2VecTool Tool(M);
if (EmbeddingsSubCmd) {
@@ -178,9 +176,12 @@ static Error processModule(Module &M, raw_ostream &OS) {
}
return Error::success();
}
+} // namespace ir2vec
+
+namespace mir2vec {
/// Setup MIR context from input file
-static Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
+Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
SMDiagnostic Err;
auto MIR = createMIRParserFromFile(InputFile, Err, Ctx.Context);
@@ -225,8 +226,8 @@ static Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
/// Generic vocabulary initialization and processing
template <typename ProcessFunc>
-static Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
- bool useLayoutVocab, ProcessFunc processFn) {
+Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
+ bool useLayoutVocab, ProcessFunc processFn) {
MIR2VecTool Tool(*Ctx.MMI);
// Initialize appropriate vocabulary type
@@ -255,7 +256,7 @@ static Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
}
/// Process module for triplet generation
-static Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
+Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(Ctx, OS, /*useLayoutVocab=*/true,
[&](MIR2VecTool &Tool) -> Error {
Tool.writeTripletsToStream(*Ctx.M, OS);
@@ -264,7 +265,7 @@ static Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
}
/// Process module for entity generation
-static Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
+Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(Ctx, OS, /*useLayoutVocab=*/true,
[&](MIR2VecTool &Tool) -> Error {
Tool.writeEntitiesToStream(OS);
@@ -273,7 +274,7 @@ static Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
}
/// Process module for embedding generation
-static Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
+Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(
Ctx, OS, /*useLayoutVocab=*/false, [&](MIR2VecTool &Tool) -> Error {
if (!FunctionName.empty()) {
@@ -304,7 +305,7 @@ static Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
}
/// Main entry point for MIR processing
-static Error processModule(const std::string &InputFile, raw_ostream &OS) {
+Error processModule(const std::string &InputFile, raw_ostream &OS) {
MIRContext Ctx;
// Setup MIR context (parse file, setup target machine, etc.)
@@ -325,7 +326,15 @@ static Error processModule(const std::string &InputFile, raw_ostream &OS) {
}
}
+} // namespace mir2vec
+
+} // namespace llvm
+
int main(int argc, char **argv) {
+ using namespace llvm;
+ using namespace llvm::ir2vec;
+ using namespace llvm::mir2vec;
+
InitLLVM X(argc, argv);
// Show Common, IR2Vec and MIR2Vec option categories
cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{
@@ -379,7 +388,7 @@ int main(int argc, char **argv) {
InitializeAllAsmPrinters();
static codegen::RegisterCodeGenFlags CGF;
- if (Error Err = processModule(InputFilename, OS)) {
+ if (Error Err = mir2vec::processModule(InputFilename, OS)) {
handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EIB) {
WithColor::error(errs(), ToolName) << EIB.message() << "\n";
});
>From 6142b9cd7ba3551b189f1830b8a0307995e77212 Mon Sep 17 00:00:00 2001
From: nishant-sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Thu, 15 Jan 2026 18:26:48 +0530
Subject: [PATCH 18/21] Adding components back to cmakelists files for utils
lib and llvm-ir2vec. The components for the imported headers should be
explicitly listed
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 12 ++++++++++++
llvm/tools/llvm-ir2vec/utils/CMakeLists.txt | 7 +++++++
2 files changed, 19 insertions(+)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index 19b8317166dc8..c5da70d6eb73a 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -1,10 +1,22 @@
set(LLVM_LINK_COMPONENTS
+ # Core LLVM components for IR processing
+ Analysis
+ Core
+ IRReader
+ Support
+
# Machine IR components (for -mode=mir)
+ CodeGen
MIRParser
+ MC
+ Target
# Target initialization (required for MIR parsing)
AllTargetsAsmParsers
AllTargetsCodeGens
+ AllTargetsDescs
+ AllTargetsInfos
+ TargetParser
)
# Add the utils subdirectory for the library
diff --git a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
index 5b50b037486ca..f8042d82e055b 100644
--- a/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/utils/CMakeLists.txt
@@ -1,3 +1,10 @@
add_llvm_library(emb_utils STATIC
utils.cpp
+
+ LINK_COMPONENTS
+ Analysis
+ CodeGen
+ Core
+ Support
+ Target
)
>From 19349c58b3f994ca856d749c49ea75970967a775 Mon Sep 17 00:00:00 2001
From: nishant_sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Fri, 16 Jan 2026 15:11:39 +0530
Subject: [PATCH 19/21] Nit commit - utils.h files header had incorrect name
---
llvm/tools/llvm-ir2vec/utils/utils.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.h b/llvm/tools/llvm-ir2vec/utils/utils.h
index 58a4551947536..d3b606ecdb34c 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.h
+++ b/llvm/tools/llvm-ir2vec/utils/utils.h
@@ -1,4 +1,4 @@
-//===- llvm-ir2vec.h - IR2Vec/MIR2Vec Tool Classes ----------------------===//
+//===- utils.h - IR2Vec/MIR2Vec Tool Classes ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From ca74bd495d23af1e43f59631325d7105f21866cc Mon Sep 17 00:00:00 2001
From: nishant_sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Fri, 16 Jan 2026 16:03:11 +0530
Subject: [PATCH 20/21] Pruning included header files
---
llvm/tools/llvm-ir2vec/CMakeLists.txt | 2 --
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 11 -----------
llvm/tools/llvm-ir2vec/utils/utils.cpp | 7 +------
llvm/tools/llvm-ir2vec/utils/utils.h | 16 ----------------
4 files changed, 1 insertion(+), 35 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt
index c5da70d6eb73a..21bc759706e9c 100644
--- a/llvm/tools/llvm-ir2vec/CMakeLists.txt
+++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt
@@ -8,8 +8,6 @@ set(LLVM_LINK_COMPONENTS
# Machine IR components (for -mode=mir)
CodeGen
MIRParser
- MC
- Target
# Target initialization (required for MIR parsing)
AllTargetsAsmParsers
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index 95eaf0cd7fa32..6e1c8aab9ed18 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -57,15 +57,8 @@
#include "utils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
-#include "llvm/IR/BasicBlock.h"
-#include "llvm/IR/Function.h"
-#include "llvm/IR/InstIterator.h"
-#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
-#include "llvm/IR/PassInstrumentation.h"
-#include "llvm/IR/PassManager.h"
-#include "llvm/IR/Type.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
@@ -79,12 +72,8 @@
#include "llvm/CodeGen/MIRParser/MIRParser.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
-#include "llvm/CodeGen/TargetInstrInfo.h"
-#include "llvm/CodeGen/TargetRegisterInfo.h"
-#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"
-#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#define DEBUG_TYPE "ir2vec"
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.cpp b/llvm/tools/llvm-ir2vec/utils/utils.cpp
index ded2c651d3de7..9797b597dcd8e 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.cpp
+++ b/llvm/tools/llvm-ir2vec/utils/utils.cpp
@@ -13,27 +13,22 @@
//===----------------------------------------------------------------------===//
#include "utils.h"
-#include "llvm/ADT/ArrayRef.h"
#include "llvm/Analysis/IR2Vec.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
-#include "llvm/IR/Instructions.h"
-#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassInstrumentation.h"
-#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/CodeGen/MIR2Vec.h"
-#include "llvm/CodeGen/MIRParser/MIRParser.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
-#include "llvm/Target/TargetMachine.h"
#define DEBUG_TYPE "ir2vec"
diff --git a/llvm/tools/llvm-ir2vec/utils/utils.h b/llvm/tools/llvm-ir2vec/utils/utils.h
index d3b606ecdb34c..abfb880f45bf4 100644
--- a/llvm/tools/llvm-ir2vec/utils/utils.h
+++ b/llvm/tools/llvm-ir2vec/utils/utils.h
@@ -15,27 +15,11 @@
#ifndef LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
#define LLVM_TOOLS_LLVM_IR2VEC_UTILS_UTILS_H
-#include "llvm/ADT/ArrayRef.h"
-#include "llvm/Analysis/IR2Vec.h"
#include "llvm/CodeGen/MIR2Vec.h"
-#include "llvm/CodeGen/MIRParser/MIRParser.h"
-#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
-#include "llvm/CodeGen/TargetInstrInfo.h"
-#include "llvm/CodeGen/TargetRegisterInfo.h"
-#include "llvm/IR/BasicBlock.h"
-#include "llvm/IR/Function.h"
-#include "llvm/IR/InstIterator.h"
-#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
-#include "llvm/IR/PassInstrumentation.h"
#include "llvm/IR/PassManager.h"
-#include "llvm/IR/Type.h"
-#include "llvm/Support/Debug.h"
-#include "llvm/Support/Error.h"
-#include "llvm/Support/WithColor.h"
-#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <memory>
#include <string>
>From 712a207ba27c6e7a4284fb86573269e7dee4da90 Mon Sep 17 00:00:00 2001
From: nishant_sachdeva <nishant.sachdeva at research.iiit.ac.in>
Date: Fri, 16 Jan 2026 16:17:25 +0530
Subject: [PATCH 21/21] Restoring static keyword for class helper functions for
ir2vec and mir2vec
---
llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index 6e1c8aab9ed18..595e348d91251 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -137,7 +137,7 @@ static cl::opt<EmbeddingLevel>
namespace ir2vec {
/// Process the module and generate output based on selected subcommand
-Error processModule(Module &M, raw_ostream &OS) {
+static Error processModule(Module &M, raw_ostream &OS) {
IR2VecTool Tool(M);
if (EmbeddingsSubCmd) {
@@ -170,7 +170,7 @@ Error processModule(Module &M, raw_ostream &OS) {
namespace mir2vec {
/// Setup MIR context from input file
-Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
+static Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
SMDiagnostic Err;
auto MIR = createMIRParserFromFile(InputFile, Err, Ctx.Context);
@@ -215,7 +215,7 @@ Error setupMIRContext(const std::string &InputFile, MIRContext &Ctx) {
/// Generic vocabulary initialization and processing
template <typename ProcessFunc>
-Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
+static Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
bool useLayoutVocab, ProcessFunc processFn) {
MIR2VecTool Tool(*Ctx.MMI);
@@ -245,7 +245,7 @@ Error processWithVocabulary(MIRContext &Ctx, raw_ostream &OS,
}
/// Process module for triplet generation
-Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
+static Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(Ctx, OS, /*useLayoutVocab=*/true,
[&](MIR2VecTool &Tool) -> Error {
Tool.writeTripletsToStream(*Ctx.M, OS);
@@ -254,7 +254,7 @@ Error processModuleForTriplets(MIRContext &Ctx, raw_ostream &OS) {
}
/// Process module for entity generation
-Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
+static Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(Ctx, OS, /*useLayoutVocab=*/true,
[&](MIR2VecTool &Tool) -> Error {
Tool.writeEntitiesToStream(OS);
@@ -263,7 +263,7 @@ Error processModuleForEntities(MIRContext &Ctx, raw_ostream &OS) {
}
/// Process module for embedding generation
-Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
+static Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
return processWithVocabulary(
Ctx, OS, /*useLayoutVocab=*/false, [&](MIR2VecTool &Tool) -> Error {
if (!FunctionName.empty()) {
@@ -294,7 +294,7 @@ Error processModuleForEmbeddings(MIRContext &Ctx, raw_ostream &OS) {
}
/// Main entry point for MIR processing
-Error processModule(const std::string &InputFile, raw_ostream &OS) {
+static Error processModule(const std::string &InputFile, raw_ostream &OS) {
MIRContext Ctx;
// Setup MIR context (parse file, setup target machine, etc.)
More information about the llvm-commits
mailing list