[llvm] Refactoring llvm-ir2vec.cpp for better separation of concerns in the Tooling classes (PR #170078)

S. VenkataKeerthy via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 08:12:37 PST 2025


================
@@ -366,40 +337,62 @@ class IR2VecTool {
       return;
     }
 
-    for (const Function &F : M)
+    for (const Function &F : M.getFunctionDefs())
       generateEmbeddings(F, OS);
   }
 
   /// Generate embeddings for a single function
   void generateEmbeddings(const Function &F, raw_ostream &OS) const {
-    assert(Vocab && Vocab->isValid() && "Vocabulary not initialized");
+    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 once for the function
+    auto Emb = Embedder::create(IR2VecEmbeddingKind, F, *Vocab);
+    if (!Emb) {
+      WithColor::error(errs(), ToolName)
+          << "Failed to create embedder for " << F.getName() << "\n";
+      return;
+    }
+
     OS << "Function: " << F.getName() << "\n";
 
     switch (Level) {
     case EmbeddingLevel::FunctionLevel:
-      getFunctionEmbedding(F).print(OS);
+      getFunctionEmbedding(*Emb).print(OS);
----------------
svkeerthy wrote:

IMHO, it may be good to remove the getters for now and refactor it later in a relevant patch.

https://github.com/llvm/llvm-project/pull/170078


More information about the llvm-commits mailing list