Skip to content

Commit

Permalink
refactor common nb impls
Browse files Browse the repository at this point in the history
  • Loading branch information
makslevental committed Jan 6, 2025
1 parent 7171e7e commit 2d83ee8
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 443 deletions.
21 changes: 1 addition & 20 deletions build_tools/cmake/llvm_cache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,7 @@ set(LLVM_INSTALL_TOOLCHAIN_ONLY OFF CACHE BOOL "")

set(LLVM_DISTRIBUTIONS MlirDevelopment CACHE STRING "")
set(LLVM_MlirDevelopment_DISTRIBUTION_COMPONENTS
clangAPINotes
clangAST
clangASTMatchers
clangAnalysis
clangBasic
clangDriver
clangDriver
clangEdit
clangFormat
clangFrontend
clangLex
clangParse
clangRewrite
clangSema
clangSerialization
clangSupport
clangTooling
clangToolingCore
clangToolingInclusions

clang-libraries
clang-headers
# triggers ClangConfig.cmake and etc
clang-cmake-exports
Expand Down
2 changes: 2 additions & 0 deletions projects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Copyright (c) 2024.

include_directories(common)

if(NOT WIN32)
add_subdirectory(eudsl-py)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// 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
// Copyright (c) 2024.
// Copyright (c) 2024-2025.

#pragma once

#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/TypeName.h"

#include <algorithm>
#include <nanobind/make_iterator.h>
#include <nanobind/nanobind.h>
#include <nanobind/operators.h>
#include <nanobind/stl/bind_vector.h>
#include <nanobind/stl/detail/traits.h>
#include <nanobind/typing.h>

namespace eudsl {
struct _ArrayRef {};
struct _MutableArrayRef {};
struct _SmallVector {};
Expand Down Expand Up @@ -283,3 +286,18 @@ nanobind::class_<Vector> bind_iter_range(nanobind::handle scope,

return cl;
}

inline void bind_array_ref_smallvector(nanobind::handle scope) {
scope.attr("T") = nanobind::type_var("T");
arrayRef =
nanobind::class_<_ArrayRef>(scope, "ArrayRef", nanobind::is_generic(),
nanobind::sig("class ArrayRef[T]"));
mutableArrayRef = nanobind::class_<_MutableArrayRef>(
scope, "MutableArrayRef", nanobind::is_generic(),
nanobind::sig("class MutableArrayRef[T]"));
smallVector = nanobind::class_<_SmallVector>(
scope, "SmallVector", nanobind::is_generic(),
nanobind::sig("class SmallVector[T]"));
}

} // namespace eudsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// 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
// Copyright (c) 2024.
// Copyright (c) 2024-2025.

#pragma once

#include <nanobind/nanobind.h>
#include <nanobind/stl/optional.h>
// ReSharper disable once CppUnusedIncludeDirective
#include <nanobind/stl/pair.h>
#include <nanobind/stl/string.h>
// ReSharper disable once CppUnusedIncludeDirective
#include <nanobind/stl/unique_ptr.h>
// ReSharper disable once CppUnusedIncludeDirective
#include <nanobind/stl/optional.h>
// ReSharper disable once CppUnusedIncludeDirective
#include "eudsl/bind_vec_like.h"

template <>
struct nanobind::detail::type_caster<llvm::StringRef> {
Expand Down
103 changes: 103 additions & 0 deletions projects/common/eudsl/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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
// Copyright (c) 2025.

#pragma once

#include <nanobind/nanobind.h>

namespace eudsl {
template <typename T, typename... Ts>
struct non_copying_non_moving_class_ : nanobind::class_<T, Ts...> {
template <typename... Extra>
NB_INLINE non_copying_non_moving_class_(nanobind::handle scope,
const char *name,
const Extra &...extra) {
nanobind::detail::type_init_data d;

d.flags = 0;
d.align = (uint8_t)alignof(typename nanobind::class_<T, Ts...>::Alias);
d.size = (uint32_t)sizeof(typename nanobind::class_<T, Ts...>::Alias);
d.name = name;
d.scope = scope.ptr();
d.type = &typeid(T);

if constexpr (!std::is_same_v<typename nanobind::class_<T, Ts...>::Base,
T>) {
d.base = &typeid(typename nanobind::class_<T, Ts...>::Base);
d.flags |= (uint32_t)nanobind::detail::type_init_flags::has_base;
}

if constexpr (std::is_destructible_v<T>) {
d.flags |= (uint32_t)nanobind::detail::type_flags::is_destructible;

if constexpr (!std::is_trivially_destructible_v<T>) {
d.flags |= (uint32_t)nanobind::detail::type_flags::has_destruct;
d.destruct = nanobind::detail::wrap_destruct<T>;
}
}

if constexpr (nanobind::detail::has_shared_from_this_v<T>) {
d.flags |= (uint32_t)nanobind::detail::type_flags::has_shared_from_this;
d.keep_shared_from_this_alive = [](PyObject *self) noexcept {
if (auto sp = nanobind::inst_ptr<T>(self)->weak_from_this().lock()) {
nanobind::detail::keep_alive(
self, new auto(std::move(sp)),
[](void *p) noexcept { delete (decltype(sp) *)p; });
return true;
}
return false;
};
}

(nanobind::detail::type_extra_apply(d, extra), ...);

this->m_ptr = nanobind::detail::nb_type_new(&d);
}
};

template <typename NewReturn, typename Return, typename... Args>
constexpr auto coerceReturn(Return (*pf)(Args...)) noexcept {
return [&pf](Args &&...args) -> NewReturn {
return pf(std::forward<Args>(args)...);
};
}

template <typename NewReturn, typename Return, typename Class, typename... Args>
constexpr auto coerceReturn(Return (Class::*pmf)(Args...),
std::false_type = {}) noexcept {
return [&pmf](Class *cls, Args &&...args) -> NewReturn {
return (cls->*pmf)(std::forward<Args>(args)...);
};
}

/*
* If you get
* ```
* Called object type 'void(MyClass::*)(vector<Item>&,int)' is not a function or
* function pointer
* ```
* it's because you're calling a member function without
* passing the `this` pointer as the first arg
*/
template <typename NewReturn, typename Return, typename Class, typename... Args>
constexpr auto coerceReturn(Return (Class::*pmf)(Args...) const,
std::true_type) noexcept {
// copy the *pmf, not capture by ref
return [pmf](const Class &cls, Args &&...args) -> NewReturn {
return (cls.*pmf)(std::forward<Args>(args)...);
};
}

inline size_t wrap(Py_ssize_t i, size_t n) {
if (i < 0)
i += (Py_ssize_t)n;

if (i < 0 || (size_t)i >= n)
throw nanobind::index_error();

return (size_t)i;
}

} // namespace eudsl
2 changes: 2 additions & 0 deletions projects/eudsl-nbgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ if(EUDSL_NBGEN_STANDALONE_BUILD)
include(AddLLVM)
include(AddClang)
include(HandleLLVMOptions)

include_directories(${CMAKE_CURRENT_LIST_DIR}/../common)
endif()

include_directories(${LLVM_INCLUDE_DIRS})
Expand Down
15 changes: 15 additions & 0 deletions projects/eudsl-nbgen/cmake/eudsl_nbgen-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

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

set(EUDSL_NBGEN_NANOBIND_OPTIONS
-Wno-cast-qual
-Wno-deprecated-literal-operator
-Wno-covered-switch-default
-Wno-nested-anon-types
-Wno-zero-length-array
-Wno-c++98-compat-extra-semi
-Wno-c++20-extensions
$<$<PLATFORM_ID:Linux>:-fexceptions -frtti>
$<$<PLATFORM_ID:Darwin>:-fexceptions -frtti>
$<$<PLATFORM_ID:Windows>:/EHsc /GR>
)

function(eudsl_nbgen target input_file)
set(EUDSL_NBGEN_TARGET_DEFINITIONS ${input_file})
cmake_parse_arguments(ARG "" "" "LINK_LIBS;EXTRA_INCLUDES;NAMESPACES" ${ARGN})
Expand Down Expand Up @@ -89,6 +102,7 @@ function(eudsl_nbgen target input_file)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${EUDSL_NBGEN_EXE} ${global_tds}
DEPFILE ${_depfile}
DEPENDS ${EUDSL_NBGEN_EXE}
COMMENT "eudsl-nbgen: Generating ${_full_gen_file}..."
)
# epic hack to specify all shards that will be generated even though we don't know them before hand
Expand Down Expand Up @@ -137,6 +151,7 @@ function(eudsl_nbgen target input_file)
endif()

add_library(${target} STATIC "${_full_gen_file}.sharded.cpp" ${_shards})
target_compile_options(${target} PUBLIC ${EUDSL_NBGEN_NANOBIND_OPTIONS})
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --include_dir
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_include_dir
Expand Down
Loading

0 comments on commit 2d83ee8

Please sign in to comment.