[llvm] [llvm-ir2vec] Adding getFuncNames API to ir2vec python bindings (PR #180473)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 5 10:03:22 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlgo
Author: Nishant Sachdeva (nishant-sachdeva)
<details>
<summary>Changes</summary>
This is more a user convenience thing. But I thought it helpful.
Otherwise, at the moment, the user has to fetch the entire embeddings dict, just to see what all functions a module has
CC - @<!-- -->svkeerthy , @<!-- -->mtrofin , @<!-- -->boomanaiden154 , @<!-- -->nikic
---
Full diff: https://github.com/llvm/llvm-project/pull/180473.diff
2 Files Affected:
- (modified) llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py (+10)
- (modified) llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp (+11)
``````````diff
diff --git a/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py b/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py
index bb29d33dc8ca6..c9c241ccf90c0 100644
--- a/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py
+++ b/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py
@@ -12,6 +12,12 @@
print("SUCCESS: Tool initialized")
print(f"Tool type: {type(tool).__name__}")
+ # Test getFuncNames
+ print("\n=== Function Names ===")
+ func_names = tool.getFuncNames()
+ for func_name in sorted(func_names):
+ print(f"Function: {func_name}")
+
# Test getFuncEmbMap
print("\n=== Function Embeddings ===")
func_emb_map = tool.getFuncEmbMap()
@@ -57,6 +63,10 @@
# CHECK: SUCCESS: Tool initialized
# CHECK: Tool type: IR2VecTool
+# CHECK: === Function Names ===
+# CHECK: Function: add
+# CHECK: Function: conditional
+# CHECK: Function: multiply
# CHECK: === Function Embeddings ===
# CHECK: Function: add
# CHECK-NEXT: Embedding: [38.0, 40.0, 42.0]
diff --git a/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp b/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp
index e4ddaf9c14e5a..df372aedb9b63 100644
--- a/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp
+++ b/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp
@@ -70,6 +70,14 @@ class PyIR2VecTool {
}
}
+ nb::list getFuncNames() {
+ nb::list NbFuncNames;
+ for (const Function &F : M->getFunctionDefs()) {
+ NbFuncNames.append(nb::str(F.getName().str().c_str()));
+ }
+ return NbFuncNames;
+ }
+
nb::dict getFuncEmbMap() {
auto ToolFuncEmbMap = Tool->getFunctionEmbeddingsMap(OutputEmbeddingMode);
@@ -196,6 +204,9 @@ NB_MODULE(ir2vec, m) {
.def(nb::init<const std::string &, const std::string &,
const std::string &>(),
nb::arg("filename"), nb::arg("mode"), nb::arg("vocabPath"))
+ .def("getFuncNames", &PyIR2VecTool::getFuncNames,
+ "Get list of all defined functions in the module\n"
+ "Returns: list[str] - Function names")
.def("getFuncEmbMap", &PyIR2VecTool::getFuncEmbMap,
"Generate function-level embeddings for all functions\n"
"Returns: dict[str, ndarray[float64]] - "
``````````
</details>
https://github.com/llvm/llvm-project/pull/180473
More information about the llvm-commits
mailing list