From 437f3910383369ababcb30ca2a2b831f0bf9e664 Mon Sep 17 00:00:00 2001 From: Nishant Sachdeva <32475507+nishant-sachdeva@users.noreply.github.com> Date: Wed, 21 Jan 2026 13:11:45 +0530 Subject: [PATCH] [IR2Vec] llvm-ir2vec python bindings scaffolding (#176571) This patch adds the build infrastructure for Python bindings to llvm-ir2vec. Addresses https://github.com/llvm/llvm-project/issues/141839 Changes: - Add `LLVM_IR2VEC_ENABLE_PYTHON_BINDINGS` CMake option (default OFF) - Create minimal python module using nanobind - Add lit test configuration that skips when the bindings have not been requested Build requires nanobind and is enabled via `cmake -DLLVM_IR2VEC_ENABLE_PYTHON_BINDINGS=ON ... ` Future patches will add actual IR2Vec API functionality. --- llvm/test/lit.site.cfg.py.in | 1 + .../tools/llvm-ir2vec/bindings/ir2vec-bindings.py | 7 +++++++ llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg | 9 +++++++++ llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt | 14 ++++++++++++++ llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp | 13 +++++++++++++ llvm/tools/llvm-ir2vec/CMakeLists.txt | 13 ++++++++++++- 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py create mode 100644 llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg create mode 100644 llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt create mode 100644 llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp diff --git a/llvm/test/lit.site.cfg.py.in b/llvm/test/lit.site.cfg.py.in index 193f351ef52e..6fe48f21cc68 100644 --- a/llvm/test/lit.site.cfg.py.in +++ b/llvm/test/lit.site.cfg.py.in @@ -7,6 +7,7 @@ config.target_triple = "@LLVM_TARGET_TRIPLE@" config.llvm_src_root = path(r"@LLVM_SOURCE_DIR@") config.llvm_obj_root = path(r"@LLVM_BINARY_DIR@") config.llvm_tools_dir = lit_config.substitute(path(r"@LLVM_TOOLS_DIR@")) +config.llvm_ir2vec_test_python_bindings = @LLVM_IR2VEC_TEST_PYTHON_BINDINGS@ config.llvm_lib_dir = lit_config.substitute(path(r"@LLVM_LIBS_DIR@")) config.llvm_shlib_dir = lit_config.substitute(path(r"@SHLIBDIR@")) config.llvm_shlib_ext = "@SHLIBEXT@" diff --git a/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py b/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py new file mode 100644 index 000000000000..459f353a478c --- /dev/null +++ b/llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py @@ -0,0 +1,7 @@ +# RUN: env PYTHONPATH=%llvm_lib_dir %python %s + +import ir2vec + +print("SUCCESS: Module imported") + +# CHECK: SUCCESS: Module imported diff --git a/llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg b/llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg new file mode 100644 index 000000000000..af02c9e43492 --- /dev/null +++ b/llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg @@ -0,0 +1,9 @@ +import sys + +# Only run if user explicitly enabled bindings +if not config.llvm_ir2vec_test_python_bindings: + config.unsupported = True +else: + config.substitutions.append(('%python', sys.executable)) + config.substitutions.append(('%llvm_lib_dir', config.llvm_lib_dir)) + config.suffixes = ['.py'] diff --git a/llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt b/llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt new file mode 100644 index 000000000000..5df3720a2477 --- /dev/null +++ b/llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(Python COMPONENTS Interpreter Development.Module REQUIRED) + +execute_process( + COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT +) +find_package(nanobind CONFIG REQUIRED) + +set_target_properties(LLVMEmbUtils PROPERTIES POSITION_INDEPENDENT_CODE ON) + +nanobind_add_module(ir2vec MODULE PyIR2Vec.cpp) +target_link_libraries(ir2vec PRIVATE LLVMEmbUtils) + +message(STATUS "Python bindings for llvm-ir2vec will be built with nanobind") diff --git a/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp b/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp new file mode 100644 index 000000000000..b3a46d429b6d --- /dev/null +++ b/llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp @@ -0,0 +1,13 @@ +//===- PyIR2Vec.cpp - Python Bindings for IR2Vec ------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include + +namespace nb = nanobind; + +NB_MODULE(ir2vec, m) { m.doc() = "Python bindings for IR2Vec"; } diff --git a/llvm/tools/llvm-ir2vec/CMakeLists.txt b/llvm/tools/llvm-ir2vec/CMakeLists.txt index 018636aa536e..694dc77e274b 100644 --- a/llvm/tools/llvm-ir2vec/CMakeLists.txt +++ b/llvm/tools/llvm-ir2vec/CMakeLists.txt @@ -19,7 +19,7 @@ set(LLVM_LINK_COMPONENTS TargetParser ) -# Add the utils subdirectory for the library +# Add the lib subdirectory add_subdirectory(lib) # Main executable @@ -31,3 +31,14 @@ add_llvm_tool(llvm-ir2vec ) target_link_libraries(llvm-ir2vec PRIVATE LLVMEmbUtils) + +option(LLVM_IR2VEC_ENABLE_PYTHON_BINDINGS + "Build Python bindings for llvm-ir2vec" + OFF) + +set(LLVM_IR2VEC_TEST_PYTHON_BINDINGS False CACHE INTERNAL "") + +if(LLVM_IR2VEC_ENABLE_PYTHON_BINDINGS) + add_subdirectory(Bindings) + set(LLVM_IR2VEC_TEST_PYTHON_BINDINGS True CACHE INTERNAL "" FORCE) +endif()