[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.
This commit is contained in:
Nishant Sachdeva 2026-01-21 13:11:45 +05:30 committed by GitHub
parent 72915ea145
commit 437f391038
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 56 additions and 1 deletions

View File

@ -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@"

View File

@ -0,0 +1,7 @@
# RUN: env PYTHONPATH=%llvm_lib_dir %python %s
import ir2vec
print("SUCCESS: Module imported")
# CHECK: SUCCESS: Module imported

View File

@ -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']

View File

@ -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")

View File

@ -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 <nanobind/nanobind.h>
namespace nb = nanobind;
NB_MODULE(ir2vec, m) { m.doc() = "Python bindings for IR2Vec"; }

View File

@ -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()