Skip to content

Conversation

@NagaRohithKumarJakkala
Copy link

@NagaRohithKumarJakkala NagaRohithKumarJakkala commented Nov 9, 2025

  • This pull request is for llvm-ir2vec which implements the call instruction in flowaware.
  • which gets the call graph information
  • and implements call instruction in flowaware instruction without using the funcVectorMap cache.
  • and i want to know how to start with cyclic dependency

@github-actions
Copy link

github-actions bot commented Nov 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlgo llvm:analysis Includes value tracking, cost tables and constant folding labels Nov 9, 2025
@NagaRohithKumarJakkala
Copy link
Author

@svkeerthy please review this

@llvmbot
Copy link
Member

llvmbot commented Nov 9, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-mlgo

Author: Naga Rohith Kumar Jakkala (NagaRohithKumarJakkala)

Changes
  • This pull request is for llvm-ir2vec which implements the call instruction.
  • which gets the call graph information
  • and implements call instruction in flowaware instruction without using the funcVectorMap cache.
  • and i want to know how to start with cyclic dependency

Full diff: https://github.com/llvm/llvm-project/pull/167233.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Analysis/IR2Vec.h (+4)
  • (modified) llvm/lib/Analysis/IR2Vec.cpp (+40-1)
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..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"
@@ -61,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
 
@@ -207,6 +211,23 @@ 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 +266,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
 //===----------------------------------------------------------------------===//

@github-actions
Copy link

github-actions bot commented Nov 9, 2025

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

@github-actions
Copy link

github-actions bot commented Nov 9, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp,h -- llvm/include/llvm/Analysis/IR2Vec.h llvm/lib/Analysis/IR2Vec.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/llvm/include/llvm/Analysis/IR2Vec.h b/llvm/include/llvm/Analysis/IR2Vec.h
index 4f694085a..09a1b00e0 100644
--- a/llvm/include/llvm/Analysis/IR2Vec.h
+++ b/llvm/include/llvm/Analysis/IR2Vec.h
@@ -601,7 +601,7 @@ private:
   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)
diff --git a/llvm/lib/Analysis/IR2Vec.cpp b/llvm/lib/Analysis/IR2Vec.cpp
index 838d689fa..05e1d6c83 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
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding mlgo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants