Skip to content

Commit

Permalink
[python] generate from libclang
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Dec 2, 2024
1 parent 724bb8c commit 2f4c873
Show file tree
Hide file tree
Showing 14 changed files with 8,110 additions and 1,301 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BasedOnStyle: LLVM
AlwaysBreakTemplateDeclarations: Yes
ReflowComments: false
25 changes: 11 additions & 14 deletions projects/eudsl-py/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.29)
project(eudslpy CXX C)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(LLVM REQUIRED CONFIG)
find_package(MLIR REQUIRED CONFIG)
Expand Down Expand Up @@ -41,28 +42,24 @@ find_package(nanobind CONFIG REQUIRED)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/eudsl)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

nanobind_add_module(eudsl_ext NB_STATIC STABLE_ABI eudsl_ext.cpp)
target_link_libraries(eudsl_ext PRIVATE MLIRIR LLVMSupport MLIRTransforms)
add_compile_options(-Wno-cast-qual)
nanobind_add_module(eudsl_ext NB_STATIC STABLE_ABI eudsl_ext.cpp ir.cpp arith.cpp)
target_link_libraries(eudsl_ext PRIVATE MLIRIR LLVMSupport MLIRTransforms MLIRArithDialect)
target_compile_options(eudsl_ext
PRIVATE
$<$<PLATFORM_ID:Linux>:-fexceptions -frtti>
$<$<PLATFORM_ID:Darwin>:-fexceptions -frtti>
$<$<PLATFORM_ID:Windows>:/EHsc /GR>)

nanobind_add_stub(
eudsl_ext_stub
MODULE eudsl_ext
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/eudsl/eudsl_ext.pyi
PYTHON_PATH $<TARGET_FILE_DIR:eudsl_ext>
DEPENDS eudsl_ext
)
nanobind_add_stub(
eudsl_stub
MODULE eudsl
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/eudsl/__init__.pyi
PYTHON_PATH ${CMAKE_CURRENT_SOURCE_DIR}
set(NB_STUBGEN_CMD "${Python_EXECUTABLE}" "-m" "nanobind.stubgen" -m eudsl -r)
set(NB_STUBGEN_OUTPUTS ${CMAKE_CURRENT_SOURCE_DIR}/eudsl/__init__.pyi)
add_custom_command(
OUTPUT ${NB_STUBGEN_OUTPUTS}
COMMAND ${NB_STUBGEN_CMD}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
DEPENDS eudsl_ext
)
add_custom_target(eudsl_ext_stub ALL DEPENDS ${NB_STUBGEN_OUTPUTS})

install(TARGETS eudsl_ext LIBRARY DESTINATION eudsl)
install(
Expand Down
2,775 changes: 2,775 additions & 0 deletions projects/eudsl-py/src/arith.cpp

Large diffs are not rendered by default.

73 changes: 37 additions & 36 deletions projects/eudsl-py/src/bind_array_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,49 @@ class_<Vector> bind_array_ref(handle scope, const char *name, Args &&...args) {
typename detail::iterator_access<typename Vector::iterator>::result_type;
using Value = std::decay_t<ValueRef>;

static_assert(
!detail::is_base_caster_v<detail::make_caster<Value>> ||
detail::is_copy_constructible_v<Value> ||
(Policy != rv_policy::automatic_reference &&
Policy != rv_policy::copy),
"bind_array_ref(): the generated __getitem__ would copy elements, so the "
"element type must be copy-constructible");

handle cl_cur = type<Vector>();
if (cl_cur.is_valid()) {
// Binding already exists, don't re-create
return borrow<class_<Vector>>(cl_cur);
}

auto cl = class_<Vector>(scope, name, std::forward<Args>(args)...)
.def(init<>(), "Default constructor")

.def("__len__", [](const Vector &v) { return v.size(); })

.def(
"__bool__", [](const Vector &v) { return !v.empty(); },
"Check whether the vector is nonempty")

.def("__repr__",
[](handle_t<Vector> h) {
return steal<str>(detail::repr_list(h.ptr()));
})

.def(
"__iter__",
[](Vector &v) {
return make_iterator<Policy>(type<Vector>(), "Iterator",
v.begin(), v.end());
},
keep_alive<0, 1>())

.def(
"__getitem__",
[](Vector &v, Py_ssize_t i) -> ValueRef {
return v[nanobind::detail::wrap(i, v.size())];
},
Policy);
auto cl =
class_<Vector>(scope, name, std::forward<Args>(args)...)
.def(init<>(), "Default constructor")

.def("__len__", [](const Vector &v) { return v.size(); })

.def(
"__bool__", [](const Vector &v) { return !v.empty(); },
"Check whether the vector is nonempty")

.def("__repr__",
[](handle_t<Vector> h) {
return steal<str>(detail::repr_list(h.ptr()));
})

.def(
"__iter__",
[](Vector &v) {
return make_iterator<Policy>(type<Vector>(), "Iterator",
v.begin(), v.end());
},
keep_alive<0, 1>());

// cl.def(
// "__getitem__",
// [](Vector &v, Py_ssize_t i) -> ValueRef {
// static_assert(
// !detail::is_base_caster_v<detail::make_caster<Value>> ||
// detail::is_copy_constructible_v<Value> ||
// (Policy != rv_policy::automatic_reference &&
// Policy != rv_policy::copy),
// "bind_array_ref(): the generated __getitem__ would copy "
// "elements, so the "
// "element type must be copy-constructible");
// return v[nanobind::detail::wrap(i, v.size())];
// },
// Policy);

if constexpr (detail::is_equality_comparable_v<Value>) {
cl.def(self == self, sig("def __eq__(self, arg: object, /) -> bool"))
Expand Down
163 changes: 0 additions & 163 deletions projects/eudsl-py/src/demo.py

This file was deleted.

1 change: 1 addition & 0 deletions projects/eudsl-py/src/eudsl/dialects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ..eudsl_ext.dialects import *
1 change: 1 addition & 0 deletions projects/eudsl-py/src/eudsl/dialects/arith/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ...eudsl_ext.dialects.arith import *
Loading

0 comments on commit 2f4c873

Please sign in to comment.