Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eudsl-tblgen] bind MLIRTableGen #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build_test_release_eudsl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ jobs:

# just to/make sure total build continues to work
- name: "Build all of eudsl"
if: ${{ github.event_name == 'pull_request' }}
run: |

pip install -r requirements.txt
Expand All @@ -173,7 +174,7 @@ jobs:

- name: "Save cache"
uses: actions/cache/save@v3
if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == 'main' }}
if: ${{ !cancelled() && (github.event_name == 'push' || github.event_name == 'pull_request') && github.ref_name == 'main' }}
with:
path: ${{ steps.setup_base.outputs.cache-dir }}
key: ${{ env.cache-key }}
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if(EUDSL_STANDALONE_BUILD)
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
set(MLIR_INCLUDE_DIR ${MLIR_INCLUDE_DIRS})

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
Expand All @@ -40,8 +41,6 @@ if(EUDSL_STANDALONE_BUILD)
include(AddLLVM)
include(AddMLIR)
include(AddClang)

set(MLIR_INCLUDE_DIR ${MLIR_INCLUDE_DIRS})
else()
# turning LLVM -DLLVM_OPTIMIZED_TABLEGEN=ON builds some stuff in the NATIVE dir
# but not everything so LLVM_BINARY_DIR isn't correct
Expand Down
22 changes: 2 additions & 20 deletions build_tools/cmake/llvm_cache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(LLVM_BUILD_TOOLS ON CACHE BOOL "")
set(LLVM_BUILD_UTILS ON CACHE BOOL "")
set(LLVM_INCLUDE_TOOLS ON CACHE BOOL "")
set(LLVM_INSTALL_UTILS ON CACHE BOOL "")
set(LLVM_ENABLE_DUMP ON CACHE BOOL "")

set(LLVM_BUILD_LLVM_DYLIB ON CACHE BOOL "")
# All the tools will use libllvm shared library
Expand Down Expand Up @@ -74,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
Loading