Skip to content

Commit 6427b2e

Browse files
committed
refactor common nb impls
1 parent 7171e7e commit 6427b2e

File tree

13 files changed

+570
-443
lines changed

13 files changed

+570
-443
lines changed

build_tools/cmake/llvm_cache.cmake

+1-20
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,7 @@ set(LLVM_INSTALL_TOOLCHAIN_ONLY OFF CACHE BOOL "")
7575

7676
set(LLVM_DISTRIBUTIONS MlirDevelopment CACHE STRING "")
7777
set(LLVM_MlirDevelopment_DISTRIBUTION_COMPONENTS
78-
clangAPINotes
79-
clangAST
80-
clangASTMatchers
81-
clangAnalysis
82-
clangBasic
83-
clangDriver
84-
clangDriver
85-
clangEdit
86-
clangFormat
87-
clangFrontend
88-
clangLex
89-
clangParse
90-
clangRewrite
91-
clangSema
92-
clangSerialization
93-
clangSupport
94-
clangTooling
95-
clangToolingCore
96-
clangToolingInclusions
97-
78+
clang-libraries
9879
clang-headers
9980
# triggers ClangConfig.cmake and etc
10081
clang-cmake-exports

projects/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
# Copyright (c) 2024.
55

6+
include_directories(common)
7+
68
if(NOT WIN32)
79
add_subdirectory(eudsl-py)
810
endif()

projects/eudsl-py/src/bind_vec_like.h projects/common/eudsl/bind_vec_like.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
22
// See https://llvm.org/LICENSE.txt for license information.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
// Copyright (c) 2024.
4+
// Copyright (c) 2024-2025.
55

66
#pragma once
77

88
#include "llvm/ADT/ArrayRef.h"
9+
#include "llvm/Support/TypeName.h"
910

1011
#include <algorithm>
1112
#include <nanobind/make_iterator.h>
1213
#include <nanobind/nanobind.h>
1314
#include <nanobind/operators.h>
1415
#include <nanobind/stl/bind_vector.h>
1516
#include <nanobind/stl/detail/traits.h>
17+
#include <nanobind/typing.h>
1618

19+
namespace eudsl {
1720
struct _ArrayRef {};
1821
struct _MutableArrayRef {};
1922
struct _SmallVector {};
@@ -283,3 +286,18 @@ nanobind::class_<Vector> bind_iter_range(nanobind::handle scope,
283286

284287
return cl;
285288
}
289+
290+
inline void bind_array_ref_smallvector(nanobind::handle scope) {
291+
scope.attr("T") = nanobind::type_var("T");
292+
arrayRef =
293+
nanobind::class_<_ArrayRef>(scope, "ArrayRef", nanobind::is_generic(),
294+
nanobind::sig("class ArrayRef[T]"));
295+
mutableArrayRef = nanobind::class_<_MutableArrayRef>(
296+
scope, "MutableArrayRef", nanobind::is_generic(),
297+
nanobind::sig("class MutableArrayRef[T]"));
298+
smallVector = nanobind::class_<_SmallVector>(
299+
scope, "SmallVector", nanobind::is_generic(),
300+
nanobind::sig("class SmallVector[T]"));
301+
}
302+
303+
} // namespace eudsl

projects/eudsl-py/src/type_casters.h projects/common/eudsl/type_casters.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
22
// See https://llvm.org/LICENSE.txt for license information.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
// Copyright (c) 2024.
4+
// Copyright (c) 2024-2025.
55

66
#pragma once
77

88
#include <nanobind/nanobind.h>
9-
#include <nanobind/stl/optional.h>
9+
// ReSharper disable once CppUnusedIncludeDirective
1010
#include <nanobind/stl/pair.h>
11+
#include <nanobind/stl/string.h>
12+
// ReSharper disable once CppUnusedIncludeDirective
1113
#include <nanobind/stl/unique_ptr.h>
14+
// ReSharper disable once CppUnusedIncludeDirective
15+
#include <nanobind/stl/optional.h>
16+
// ReSharper disable once CppUnusedIncludeDirective
17+
#include "eudsl/bind_vec_like.h"
1218

1319
template <>
1420
struct nanobind::detail::type_caster<llvm::StringRef> {

projects/common/eudsl/util.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
// See https://llvm.org/LICENSE.txt for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
// Copyright (c) 2025.
5+
6+
#pragma once
7+
8+
#include <nanobind/nanobind.h>
9+
10+
namespace eudsl {
11+
template <typename T, typename... Ts>
12+
struct non_copying_non_moving_class_ : nanobind::class_<T, Ts...> {
13+
template <typename... Extra>
14+
NB_INLINE non_copying_non_moving_class_(nanobind::handle scope,
15+
const char *name,
16+
const Extra &...extra) {
17+
nanobind::detail::type_init_data d;
18+
19+
d.flags = 0;
20+
d.align = (uint8_t)alignof(typename nanobind::class_<T, Ts...>::Alias);
21+
d.size = (uint32_t)sizeof(typename nanobind::class_<T, Ts...>::Alias);
22+
d.name = name;
23+
d.scope = scope.ptr();
24+
d.type = &typeid(T);
25+
26+
if constexpr (!std::is_same_v<typename nanobind::class_<T, Ts...>::Base,
27+
T>) {
28+
d.base = &typeid(typename nanobind::class_<T, Ts...>::Base);
29+
d.flags |= (uint32_t)nanobind::detail::type_init_flags::has_base;
30+
}
31+
32+
if constexpr (std::is_destructible_v<T>) {
33+
d.flags |= (uint32_t)nanobind::detail::type_flags::is_destructible;
34+
35+
if constexpr (!std::is_trivially_destructible_v<T>) {
36+
d.flags |= (uint32_t)nanobind::detail::type_flags::has_destruct;
37+
d.destruct = nanobind::detail::wrap_destruct<T>;
38+
}
39+
}
40+
41+
if constexpr (nanobind::detail::has_shared_from_this_v<T>) {
42+
d.flags |= (uint32_t)nanobind::detail::type_flags::has_shared_from_this;
43+
d.keep_shared_from_this_alive = [](PyObject *self) noexcept {
44+
if (auto sp = nanobind::inst_ptr<T>(self)->weak_from_this().lock()) {
45+
nanobind::detail::keep_alive(
46+
self, new auto(std::move(sp)),
47+
[](void *p) noexcept { delete (decltype(sp) *)p; });
48+
return true;
49+
}
50+
return false;
51+
};
52+
}
53+
54+
(nanobind::detail::type_extra_apply(d, extra), ...);
55+
56+
this->m_ptr = nanobind::detail::nb_type_new(&d);
57+
}
58+
};
59+
60+
template <typename NewReturn, typename Return, typename... Args>
61+
constexpr auto coerceReturn(Return (*pf)(Args...)) noexcept {
62+
return [&pf](Args &&...args) -> NewReturn {
63+
return pf(std::forward<Args>(args)...);
64+
};
65+
}
66+
67+
template <typename NewReturn, typename Return, typename Class, typename... Args>
68+
constexpr auto coerceReturn(Return (Class::*pmf)(Args...),
69+
std::false_type = {}) noexcept {
70+
return [&pmf](Class *cls, Args &&...args) -> NewReturn {
71+
return (cls->*pmf)(std::forward<Args>(args)...);
72+
};
73+
}
74+
75+
/*
76+
* If you get
77+
* ```
78+
* Called object type 'void(MyClass::*)(vector<Item>&,int)' is not a function or
79+
* function pointer
80+
* ```
81+
* it's because you're calling a member function without
82+
* passing the `this` pointer as the first arg
83+
*/
84+
template <typename NewReturn, typename Return, typename Class, typename... Args>
85+
constexpr auto coerceReturn(Return (Class::*pmf)(Args...) const,
86+
std::true_type) noexcept {
87+
// copy the *pmf, not capture by ref
88+
return [pmf](const Class &cls, Args &&...args) -> NewReturn {
89+
return (cls.*pmf)(std::forward<Args>(args)...);
90+
};
91+
}
92+
93+
inline size_t wrap(Py_ssize_t i, size_t n) {
94+
if (i < 0)
95+
i += (Py_ssize_t)n;
96+
97+
if (i < 0 || (size_t)i >= n)
98+
throw nanobind::index_error();
99+
100+
return (size_t)i;
101+
}
102+
103+
} // namespace eudsl

projects/eudsl-nbgen/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ if(EUDSL_NBGEN_STANDALONE_BUILD)
3131
include(AddLLVM)
3232
include(AddClang)
3333
include(HandleLLVMOptions)
34+
35+
include_directories(${CMAKE_CURRENT_LIST_DIR}/../common)
3436
endif()
3537

3638
include_directories(${LLVM_INCLUDE_DIRS})

projects/eudsl-nbgen/cmake/eudsl_nbgen-config.cmake

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
# copy-pasta from AddMLIR.cmake/AddLLVM.cmake/TableGen.cmake
77

8+
set(EUDSL_NBGEN_NANOBIND_OPTIONS
9+
-Wno-cast-qual
10+
-Wno-deprecated-literal-operator
11+
-Wno-covered-switch-default
12+
-Wno-nested-anon-types
13+
-Wno-zero-length-array
14+
-Wno-c++98-compat-extra-semi
15+
-Wno-c++20-extensions
16+
$<$<PLATFORM_ID:Linux>:-fexceptions -frtti>
17+
$<$<PLATFORM_ID:Darwin>:-fexceptions -frtti>
18+
$<$<PLATFORM_ID:Windows>:/EHsc /GR>
19+
)
20+
821
function(eudsl_nbgen target input_file)
922
set(EUDSL_NBGEN_TARGET_DEFINITIONS ${input_file})
1023
cmake_parse_arguments(ARG "" "" "LINK_LIBS;EXTRA_INCLUDES;NAMESPACES" ${ARGN})
@@ -89,6 +102,7 @@ function(eudsl_nbgen target input_file)
89102
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
90103
DEPENDS ${EUDSL_NBGEN_EXE} ${global_tds}
91104
DEPFILE ${_depfile}
105+
DEPENDS ${EUDSL_NBGEN_EXE}
92106
COMMENT "eudsl-nbgen: Generating ${_full_gen_file}..."
93107
)
94108
# epic hack to specify all shards that will be generated even though we don't know them before hand
@@ -137,6 +151,7 @@ function(eudsl_nbgen target input_file)
137151
endif()
138152

139153
add_library(${target} STATIC "${_full_gen_file}.sharded.cpp" ${_shards})
154+
target_compile_options(${target} PUBLIC ${EUDSL_NBGEN_NANOBIND_OPTIONS})
140155
execute_process(
141156
COMMAND "${Python_EXECUTABLE}" -m nanobind --include_dir
142157
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_include_dir

0 commit comments

Comments
 (0)