[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:
parent
72915ea145
commit
437f391038
@ -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@"
|
||||
|
||||
7
llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py
Normal file
7
llvm/test/tools/llvm-ir2vec/bindings/ir2vec-bindings.py
Normal file
@ -0,0 +1,7 @@
|
||||
# RUN: env PYTHONPATH=%llvm_lib_dir %python %s
|
||||
|
||||
import ir2vec
|
||||
|
||||
print("SUCCESS: Module imported")
|
||||
|
||||
# CHECK: SUCCESS: Module imported
|
||||
9
llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg
Normal file
9
llvm/test/tools/llvm-ir2vec/bindings/lit.local.cfg
Normal 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']
|
||||
14
llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt
Normal file
14
llvm/tools/llvm-ir2vec/Bindings/CMakeLists.txt
Normal 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")
|
||||
13
llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp
Normal file
13
llvm/tools/llvm-ir2vec/Bindings/PyIR2Vec.cpp
Normal 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"; }
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user