[llvm] Call instr execution in ir2vec (PR #167233)

Naga Rohith Kumar Jakkala via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 9 12:51:59 PST 2025


https://github.com/NagaRohithKumarJakkala updated https://github.com/llvm/llvm-project/pull/167233

>From 8d7390135849efafaa7360039c15b2469c98d5ba Mon Sep 17 00:00:00 2001
From: NagaRohithKumarJakkala <nagarohithkumarjakkala at gmail.com>
Date: Thu, 6 Nov 2025 01:55:01 +0530
Subject: [PATCH 1/4] added funcCallMap and added call instruction without
 funcVectorMap

---
 llvm/include/llvm/Analysis/IR2Vec.h |  4 ++++
 llvm/lib/Analysis/IR2Vec.cpp        | 37 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/llvm/include/llvm/Analysis/IR2Vec.h b/llvm/include/llvm/Analysis/IR2Vec.h
index 7a68773a2643a..09a1b00e0391e 100644
--- a/llvm/include/llvm/Analysis/IR2Vec.h
+++ b/llvm/include/llvm/Analysis/IR2Vec.h
@@ -598,12 +598,16 @@ class LLVM_ABI FlowAwareEmbedder : public Embedder {
   // FlowAware embeddings would benefit from caching instruction embeddings as
   // they are reused while computing the embeddings of other instructions.
   mutable InstEmbeddingsMap InstVecMap;
+  static SmallVector<Function *, 15> FuncStack;
   Embedding computeEmbeddings(const Instruction &I) const override;
+  static SmallMapVector<const Function *, SmallVector<const Function *, 10>, 16>
+      FuncCallMap;
 
 public:
   FlowAwareEmbedder(const Function &F, const Vocabulary &Vocab)
       : Embedder(F, Vocab) {}
   void invalidateEmbeddings() override { InstVecMap.clear(); }
+  static void computeFuncCallMap(Module &M);
 };
 
 } // namespace ir2vec
diff --git a/llvm/lib/Analysis/IR2Vec.cpp b/llvm/lib/Analysis/IR2Vec.cpp
index 85b5372c961c1..030c92d181eb9 100644
--- a/llvm/lib/Analysis/IR2Vec.cpp
+++ b/llvm/lib/Analysis/IR2Vec.cpp
@@ -26,6 +26,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Analysis/CallGraph.h"
 
 using namespace llvm;
 using namespace ir2vec;
@@ -207,6 +208,24 @@ Embedding FlowAwareEmbedder::computeEmbeddings(const Instruction &I) const {
   // TODO: Handle call instructions differently.
   // For now, we treat them like other instructions
   Embedding ArgEmb(Dimension, 0);
+
+    if(isa<CallInst>(I)){
+    const auto* Ci = dyn_cast<CallInst>(&I);
+    Function* Func = Ci->getCalledFunction();
+    if(Func){
+      if(!Func->isDeclaration() &&
+        std::find(FuncStack.begin(),FuncStack.end(),Func)== 
+        FuncStack.end()){
+        FuncStack.push_back(Func);
+        auto Emb = Embedder::create(IR2VecEmbeddingKind, *Func, Vocab);
+        auto FuncVec = Emb->getFunctionVector();
+        std::transform(ArgEmb.begin(),ArgEmb.end(),
+                       FuncVec.begin(),FuncVec.end(),
+                       std::plus<double>());
+        FuncStack.pop_back();
+      }
+    }
+  }
   for (const auto &Op : I.operands()) {
     // If the operand is defined elsewhere, we use its embedding
     if (const auto *DefInst = dyn_cast<Instruction>(Op)) {
@@ -245,6 +264,24 @@ Embedding FlowAwareEmbedder::computeEmbeddings(const Instruction &I) const {
   return InstVector;
 }
 
+void FlowAwareEmbedder::computeFuncCallMap(Module &M){
+  CallGraph Cg = CallGraph(M);
+  for(auto CallItr=Cg.begin();CallItr != Cg.end();CallItr++){
+    if(CallItr->first && !CallItr->first->isDeclaration()){
+      const auto* ParentFunc = CallItr->first;
+      CallGraphNode *Cgn = CallItr->second.get();
+      if(Cgn){
+        for(auto It = Cgn->begin(); It!= Cgn->end(); It++){
+          const auto* Func = It->second->getFunction();
+          if(Func && !Func->isDeclaration()){
+            FuncCallMap[ParentFunc].push_back(Func);
+          }
+        }
+      }
+    }
+  }
+}
+
 // ==----------------------------------------------------------------------===//
 // VocabStorage
 //===----------------------------------------------------------------------===//

>From ebb3edeb43ac7f1693f800b3d8869c2c13673de0 Mon Sep 17 00:00:00 2001
From: NagaRohithKumarJakkala <nagarohithkumarjakkala at gmail.com>
Date: Sun, 9 Nov 2025 22:53:16 +0530
Subject: [PATCH 2/4] compiled and resolve the error regarding the static code

---
 llvm/lib/Analysis/IR2Vec.cpp | 42 +++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Analysis/IR2Vec.cpp b/llvm/lib/Analysis/IR2Vec.cpp
index 030c92d181eb9..2493cb4f75993 100644
--- a/llvm/lib/Analysis/IR2Vec.cpp
+++ b/llvm/lib/Analysis/IR2Vec.cpp
@@ -17,6 +17,7 @@
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/CallGraph.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
@@ -26,7 +27,6 @@
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Analysis/CallGraph.h"
 
 using namespace llvm;
 using namespace ir2vec;
@@ -62,7 +62,10 @@ cl::opt<IR2VecKind> IR2VecEmbeddingKind(
                           "Generate flow-aware embeddings")),
     cl::init(IR2VecKind::Symbolic), cl::desc("IR2Vec embedding kind"),
     cl::cat(IR2VecCategory));
-
+// static members of Flowaware Embeddings
+SmallVector<Function *, 15> FlowAwareEmbedder::FuncStack;
+SmallMapVector<const Function *, SmallVector<const Function *, 10>, 16>
+      FlowAwareEmbedder::FuncCallMap;
 } // namespace ir2vec
 } // namespace llvm
 
@@ -209,19 +212,18 @@ Embedding FlowAwareEmbedder::computeEmbeddings(const Instruction &I) const {
   // For now, we treat them like other instructions
   Embedding ArgEmb(Dimension, 0);
 
-    if(isa<CallInst>(I)){
-    const auto* Ci = dyn_cast<CallInst>(&I);
-    Function* Func = Ci->getCalledFunction();
-    if(Func){
-      if(!Func->isDeclaration() &&
-        std::find(FuncStack.begin(),FuncStack.end(),Func)== 
-        FuncStack.end()){
+  if (isa<CallInst>(I)) {
+    const auto *Ci = dyn_cast<CallInst>(&I);
+    Function *Func = Ci->getCalledFunction();
+    if (Func) {
+      if (!Func->isDeclaration() &&
+          std::find(FuncStack.begin(), FuncStack.end(), Func) ==
+              FuncStack.end()) {
         FuncStack.push_back(Func);
         auto Emb = Embedder::create(IR2VecEmbeddingKind, *Func, Vocab);
         auto FuncVec = Emb->getFunctionVector();
-        std::transform(ArgEmb.begin(),ArgEmb.end(),
-                       FuncVec.begin(),FuncVec.end(),
-                       std::plus<double>());
+        std::transform(ArgEmb.begin(), ArgEmb.end(), FuncVec.begin(),
+                       FuncVec.end(), std::plus<double>());
         FuncStack.pop_back();
       }
     }
@@ -264,16 +266,16 @@ Embedding FlowAwareEmbedder::computeEmbeddings(const Instruction &I) const {
   return InstVector;
 }
 
-void FlowAwareEmbedder::computeFuncCallMap(Module &M){
+void FlowAwareEmbedder::computeFuncCallMap(Module &M) {
   CallGraph Cg = CallGraph(M);
-  for(auto CallItr=Cg.begin();CallItr != Cg.end();CallItr++){
-    if(CallItr->first && !CallItr->first->isDeclaration()){
-      const auto* ParentFunc = CallItr->first;
+  for (auto CallItr = Cg.begin(); CallItr != Cg.end(); CallItr++) {
+    if (CallItr->first && !CallItr->first->isDeclaration()) {
+      const auto *ParentFunc = CallItr->first;
       CallGraphNode *Cgn = CallItr->second.get();
-      if(Cgn){
-        for(auto It = Cgn->begin(); It!= Cgn->end(); It++){
-          const auto* Func = It->second->getFunction();
-          if(Func && !Func->isDeclaration()){
+      if (Cgn) {
+        for (auto It = Cgn->begin(); It != Cgn->end(); It++) {
+          const auto *Func = It->second->getFunction();
+          if (Func && !Func->isDeclaration()) {
             FuncCallMap[ParentFunc].push_back(Func);
           }
         }

>From 00c77a67355e2dd49cae4d3ee9210b8d7aad821f Mon Sep 17 00:00:00 2001
From: Naga Rohith Kumar Jakkala <nagarohithkumarjakkala at gmail.com>
Date: Mon, 10 Nov 2025 02:18:01 +0530
Subject: [PATCH 3/4] Fix formatting in IR2Vec.h for FuncCallMap

---
 llvm/include/llvm/Analysis/IR2Vec.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Analysis/IR2Vec.h b/llvm/include/llvm/Analysis/IR2Vec.h
index 09a1b00e0391e..4f694085aa377 100644
--- a/llvm/include/llvm/Analysis/IR2Vec.h
+++ b/llvm/include/llvm/Analysis/IR2Vec.h
@@ -601,7 +601,7 @@ class LLVM_ABI FlowAwareEmbedder : public Embedder {
   static SmallVector<Function *, 15> FuncStack;
   Embedding computeEmbeddings(const Instruction &I) const override;
   static SmallMapVector<const Function *, SmallVector<const Function *, 10>, 16>
-      FuncCallMap;
+    FuncCallMap;
 
 public:
   FlowAwareEmbedder(const Function &F, const Vocabulary &Vocab)

>From bd8df57ae3815b730b60e3380c1b4e5ad46dc82f Mon Sep 17 00:00:00 2001
From: Naga Rohith Kumar Jakkala <nagarohithkumarjakkala at gmail.com>
Date: Mon, 10 Nov 2025 02:21:50 +0530
Subject: [PATCH 4/4] Fix formatting issue in IR2Vec.cpp

---
 llvm/lib/Analysis/IR2Vec.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/IR2Vec.cpp b/llvm/lib/Analysis/IR2Vec.cpp
index 2493cb4f75993..838d689fa58d4 100644
--- a/llvm/lib/Analysis/IR2Vec.cpp
+++ b/llvm/lib/Analysis/IR2Vec.cpp
@@ -65,7 +65,7 @@ cl::opt<IR2VecKind> IR2VecEmbeddingKind(
 // static members of Flowaware Embeddings
 SmallVector<Function *, 15> FlowAwareEmbedder::FuncStack;
 SmallMapVector<const Function *, SmallVector<const Function *, 10>, 16>
-      FlowAwareEmbedder::FuncCallMap;
+  FlowAwareEmbedder::FuncCallMap;
 } // namespace ir2vec
 } // namespace llvm
 



More information about the llvm-commits mailing list