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 9 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
4 changes: 3 additions & 1 deletion .github/actions/setup_base/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ runs:
- name: "Set CMake/ccache env vars"
shell: bash
run: |
echo "CCACHE_DIR=${{ steps.canonicalize-cache-dir.outputs.cache-dir }}/ccache" >> $GITHUB_ENV
export CCACHE_DIR="${{ steps.canonicalize-cache-dir.outputs.cache-dir }}/ccache"
mkdir -p $CCACHE_DIR
echo "CCACHE_DIR=$CCACHE_DIR" >> $GITHUB_ENV
echo "CCACHE_COMPILERCHECK=string:$($CC --version | head -n 1)" >> $GITHUB_ENV
echo $CCACHE_COMPILERCHECK
echo "CCACHE_MAXSIZE=700M" >> $GITHUB_ENV
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 @@ -75,26 +76,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
Expand Up @@ -6,14 +6,17 @@
#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
25 changes: 15 additions & 10 deletions projects/eudsl-llvmpy/eudsl-llvmpy-generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,24 @@ class LLVMMatchType(Generic[_T]):
int_regex = re.compile(r"_i(\d+)")
fp_regex = re.compile(r"_f(\d+)")

for d in intrins.defs:
intr = intrins.defs[d]
if intr.name.startswith("int_amdgcn") and intr.type.as_string != "ClangBuiltin":
defs = intrins.get_defs()
for d in defs:
intr = defs[d]
if (
intr.get_name().startswith("int_amdgcn")
and intr.get_type().get_as_string() != "ClangBuiltin"
):
arg_types = []
ret_types = []
for p in intr.values.ParamTypes.value:
p_s = p.as_string
for p in intr.get_values().ParamTypes.get_value():
p_s = p.get_as_string()
if p_s.startswith("anon"):
p_s = p.type.as_string
p_s = p.get_type().get_as_string()
pdv = p.get_def().get_values()
if p_s == "LLVMMatchType":
p_s += f"[Literal[{p.def_.values.Number.value.value}]]"
p_s += f"[Literal[{pdv.Number.get_value()}]]"
elif p_s == "LLVMQualPointerType":
_, addr_space = p.def_.values.Sig.value.values
kind, addr_space = pdv.Sig.get_value()
p_s += f"[Literal[{addr_space}]]"
else:
raise NotImplemented(f"unsupported {p_s=}")
Expand All @@ -343,8 +348,8 @@ class LLVMMatchType(Generic[_T]):
p_s = "pointer"

arg_types.append(p_s)
for p in intr.values.RetTypes.value:
ret_types.append(p.as_string)
for p in intr.get_values().RetTypes.get_value():
ret_types.append(p.get_as_string())

ret_str = ""
if len(ret_types):
Expand Down
2 changes: 1 addition & 1 deletion projects/eudsl-nbgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ install(
)

install(
DIRECTORY includes/eudsl/
DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../common/eudsl/"
DESTINATION "${EUDSL_NBGEN_INSTALL_DATADIR}/includes/eudsl"
FILES_MATCHING PATTERN "*\.h"
)
Expand Down
Loading
Loading