diff --git a/projects/eudsl-py/CMakeLists.txt b/projects/eudsl-py/CMakeLists.txt index 45347d73..91156d42 100644 --- a/projects/eudsl-py/CMakeLists.txt +++ b/projects/eudsl-py/CMakeLists.txt @@ -95,6 +95,7 @@ set(nanobind_options -Wno-nested-anon-types -Wno-zero-length-array -Wno-c++98-compat-extra-semi + -Wno-c++20-extensions $<$:-fexceptions -frtti> $<$:-fexceptions -frtti> $<$:/EHsc /GR> diff --git a/projects/eudsl-py/src/bind_vec_like.h b/projects/eudsl-py/src/bind_vec_like.h index 6f389faa..5a733f69 100644 --- a/projects/eudsl-py/src/bind_vec_like.h +++ b/projects/eudsl-py/src/bind_vec_like.h @@ -28,12 +28,35 @@ std::tuple>, nanobind::class_>> bind_array_ref(nanobind::handle scope, Args &&...args) { using ArrayRef = llvm::ArrayRef; + using SmallVec = llvm::SmallVector; + using MutableArrayRef = llvm::MutableArrayRef; using ValueRef = Element &; - auto vecClName = "SmallVector[" + std::string(typeid(Element).name()) + "]"; + nanobind::handle array_cl_cur = nanobind::type(); + if (array_cl_cur.is_valid()) { + nanobind::handle smallvec_cl_cur = nanobind::type(); + assert(smallvec_cl_cur.is_valid() && + "expected SmallVec to already have been registered"); + nanobind::handle mutable_cl_cur = nanobind::type(); + assert(mutable_cl_cur.is_valid() && + "expected MutableArrayRef to already have been registered"); + return std::make_tuple( + nanobind::borrow>(array_cl_cur), + nanobind::borrow>(array_cl_cur), + nanobind::borrow>(array_cl_cur)); + } + + std::string typename_; + if (nanobind::type().ptr()) { + typename_ = + std::string(nanobind::type_name(nanobind::type()).c_str()); + + } else + typename_ = '"' + llvm::getTypeName().str() + '"'; + + std::string vecClName = "SmallVector[" + typename_ + "]"; auto _smallVectorOfElement = - nanobind::bind_vector>(scope, - vecClName.c_str()); + nanobind::bind_vector(scope, vecClName.c_str()); smallVector.def_static( "__class_getitem__", @@ -41,12 +64,12 @@ bind_array_ref(nanobind::handle scope, Args &&...args) { return _smallVectorOfElement; }); - auto arrClName = "ArrayRef[" + std::string(typeid(Element).name()) + "]"; + std::string arrClName = "ArrayRef[" + typename_ + "]"; auto cl = nanobind::class_(scope, arrClName.c_str(), std::forward(args)...) - .def(nanobind::init &>()) - .def(nanobind::init_implicit>()) + .def(nanobind::init()) + .def(nanobind::init_implicit()) .def("__len__", [](const ArrayRef &v) { return v.size(); }) .def("__bool__", [](const ArrayRef &v) { return !v.empty(); }) .def("__repr__", @@ -71,39 +94,29 @@ bind_array_ref(nanobind::handle scope, Args &&...args) { arrayRef.def_static("__class_getitem__", [cl](nanobind::type_object_t) { return cl; }); - arrayRef.def(nanobind::new_([](const llvm::SmallVector &sv) { - return llvm::ArrayRef(sv); - })); + arrayRef.def(nanobind::new_([](const SmallVec &sv) { return ArrayRef(sv); })); if constexpr (nanobind::detail::is_equality_comparable_v) { cl.def(nanobind::self == nanobind::self, nanobind::sig("def __eq__(self, arg: object, /) -> bool")) .def(nanobind::self != nanobind::self, nanobind::sig("def __ne__(self, arg: object, /) -> bool")) - .def("__contains__", [](const ArrayRef &v, const Element &x) { return std::find(v.begin(), v.end(), x) != v.end(); }) - - .def("__contains__", // fallback for incompatible types + .def("__contains__", [](const ArrayRef &, nanobind::handle) { return false; }) - - .def( - "count", - [](const ArrayRef &v, const Element &x) { - return std::count(v.begin(), v.end(), x); - }, - "Return number of occurrences of `arg`."); + .def("count", [](const ArrayRef &v, const Element &x) { + return std::count(v.begin(), v.end(), x); + }); } - using MutableArrayRef = llvm::MutableArrayRef; - auto mutableArrClName = - "MutableArrayRef[" + std::string(typeid(Element).name()) + "]"; + std::string mutableArrClName = "MutableArrayRef[" + typename_ + "]"; auto mutableCl = nanobind::class_(scope, arrClName.c_str(), std::forward(args)...) - .def(nanobind::init &>()) + .def(nanobind::init()) .def("__len__", [](const MutableArrayRef &v) { return v.size(); }) .def("__bool__", [](const MutableArrayRef &v) { return !v.empty(); }) .def("__repr__", @@ -136,21 +149,15 @@ bind_array_ref(nanobind::handle scope, Args &&...args) { nanobind::sig("def __eq__(self, arg: object, /) -> bool")) .def(nanobind::self != nanobind::self, nanobind::sig("def __ne__(self, arg: object, /) -> bool")) - .def("__contains__", [](const MutableArrayRef &v, const Element &x) { return std::find(v.begin(), v.end(), x) != v.end(); }) - - .def("__contains__", // fallback for incompatible types + .def("__contains__", [](const MutableArrayRef &, nanobind::handle) { return false; }) - - .def( - "count", - [](const MutableArrayRef &v, const Element &x) { - return std::count(v.begin(), v.end(), x); - }, - "Return number of occurrences of `arg`."); + .def("count", [](const MutableArrayRef &v, const Element &x) { + return std::count(v.begin(), v.end(), x); + }); } return {_smallVectorOfElement, cl, mutableCl}; @@ -162,17 +169,13 @@ template bind_iter_like(nanobind::handle scope, const char *name, Args &&...args) { nanobind::handle cl_cur = nanobind::type(); - if (cl_cur.is_valid()) { - // Binding already exists, don't re-create + if (cl_cur.is_valid()) return nanobind::borrow>(cl_cur); - } auto cl = nanobind::class_(scope, name, std::forward(args)...) .def("__len__", [](const Vector &v) -> int { return v.size(); }) - .def( - "__bool__", [](const Vector &v) { return !v.empty(); }, - "Check whether the vector is nonempty") + .def("__bool__", [](const Vector &v) { return !v.empty(); }) .def("__repr__", [](nanobind::handle_t h) { return nanobind::steal( @@ -212,14 +215,11 @@ nanobind::class_ bind_iter_like(nanobind::handle scope, [](const Vector &v, const Value &x) { return std::find(v.begin(), v.end(), x) != v.end(); }) - .def("__contains__", // fallback for incompatible types + .def("__contains__", [](const Vector &, nanobind::handle) { return false; }) - .def( - "count", - [](const Vector &v, const Value &x) { - return std::count(v.begin(), v.end(), x); - }, - "Return number of occurrences of `arg`."); + .def("count", [](const Vector &v, const Value &x) { + return std::count(v.begin(), v.end(), x); + }); } return cl; @@ -231,17 +231,13 @@ template bind_iter_range(nanobind::handle scope, const char *name, Args &&...args) { nanobind::handle cl_cur = nanobind::type(); - if (cl_cur.is_valid()) { - // Binding already exists, don't re-create + if (cl_cur.is_valid()) return nanobind::borrow>(cl_cur); - } auto cl = nanobind::class_(scope, name, std::forward(args)...) .def("__len__", [](const Vector &v) -> int { return v.size(); }) - .def( - "__bool__", [](const Vector &v) { return !v.empty(); }, - "Check whether the vector is nonempty") + .def("__bool__", [](const Vector &v) { return !v.empty(); }) .def("__repr__", [](nanobind::handle_t h) { return nanobind::steal( @@ -280,12 +276,9 @@ nanobind::class_ bind_iter_range(nanobind::handle scope, }) .def("__contains__", // fallback for incompatible types [](const Vector &, nanobind::handle) { return false; }) - .def( - "count", - [](const Vector &v, const Value &x) { - return std::count(v.begin(), v.end(), x); - }, - "Return number of occurrences of `arg`."); + .def("count", [](const Vector &v, const Value &x) { + return std::count(v.begin(), v.end(), x); + }); } return cl; diff --git a/projects/eudsl-py/src/eudslpy-gen.cpp b/projects/eudsl-py/src/eudslpy-gen.cpp index d4dd59e4..0b65ab64 100644 --- a/projects/eudsl-py/src/eudslpy-gen.cpp +++ b/projects/eudsl-py/src/eudslpy-gen.cpp @@ -305,12 +305,8 @@ static bool emitClass(clang::CXXRecordDecl *decl, clang::CompilerInstance &ci, if (decl->isTemplated()) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "template classes not supported yet")); - // have to force emit because after the fatal error, no more warnings will - // be emitted - // https://github.com/llvm/llvm-project/blob/d74214cc8c03159e5d1f1168a09368cf3b23fd5f/clang/lib/Basic/DiagnosticIDs.cpp#L796 - (void)builder.setForceEmit(); return false; } @@ -320,9 +316,8 @@ static bool emitClass(clang::CXXRecordDecl *decl, clang::CompilerInstance &ci, if (decl->getNumBases() > 1) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "multiple base classes not supported")); - (void)builder.setForceEmit(); } else if (decl->getNumBases() == 1) { // handle some known bases that we've already found a wap to bind clang::CXXBaseSpecifier baseClass = *decl->bases_begin(); @@ -351,10 +346,9 @@ static bool emitClass(clang::CXXRecordDecl *decl, clang::CompilerInstance &ci, "expected class template specialization"); clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( baseClass.getBeginLoc(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "unknown base templated base class: ")); builder << baseName << "\n"; - (void)builder.setForceEmit(); } } @@ -494,9 +488,8 @@ struct BindingsVisitor if (decl->isAnonymousStructOrUnion()) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "anon structs/union fields not supported")); - (void)builder.setForceEmit(); return true; } if (decl->isBitField()) @@ -533,17 +526,15 @@ struct BindingsVisitor decl->isFunctionTemplateSpecialization()) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "template methods not supported yet")); - (void)builder.setForceEmit(); return true; } if (decl->getFriendObjectKind()) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "friend functions not supported")); - (void)builder.setForceEmit(); return true; } emitClassMethodOrFunction(decl, ci, outputFile); @@ -565,9 +556,8 @@ struct BindingsVisitor decl->isFunctionTemplateSpecialization()) { clang::DiagnosticBuilder builder = ci.getDiagnostics().Report( decl->getLocation(), ci.getDiagnostics().getCustomDiagID( - clang::DiagnosticsEngine::Warning, + clang::DiagnosticsEngine::Note, "template functions not supported yet")); - (void)builder.setForceEmit(); return true; } emitClassMethodOrFunction(decl, ci, outputFile); @@ -646,9 +636,7 @@ struct GenerateBindingsAction : clang::ASTFrontendAction { std::unique_ptr CreateASTConsumer(clang::CompilerInstance &compiler, llvm::StringRef inFile) override { - // compiler.getPreprocessor().SetSuppressIncludeNotFoundError(true); compiler.getDiagnosticOpts().ShowLevel = true; - compiler.getDiagnosticOpts().IgnoreWarnings = false; return std::make_unique(compiler, outputFile); } diff --git a/projects/eudsl-py/src/eudslpy_ext.cpp b/projects/eudsl-py/src/eudslpy_ext.cpp index 7a642696..5082ccd0 100644 --- a/projects/eudsl-py/src/eudslpy_ext.cpp +++ b/projects/eudsl-py/src/eudslpy_ext.cpp @@ -381,6 +381,9 @@ NB_MODULE(eudslpy_ext, m) { nb::class_(m, "TypeID"); nb::class_(m, "InterfaceMap"); + auto irModule = m.def_submodule("ir"); + populateIRModule(irModule); + nb::class_>(m, "FailureOr[bool]"); nb::class_>(m, "FailureOr[StringAttr]"); nb::class_>( @@ -436,8 +439,6 @@ NB_MODULE(eudslpy_ext, m) { bind_array_ref(m); auto [smallVectorOfDouble, arrayRefOfDouble, mutableArrayRefOfDouble] = bind_array_ref(m); - auto [smallVectorOfLong, arrayRefOfLong, mutableArrayRefOfLong] = - bind_array_ref(m); auto [smallVectorOfInt16, arrayRefOfInt16, mutableArrayRefOfInt16] = bind_array_ref(m); @@ -490,8 +491,8 @@ NB_MODULE(eudslpy_ext, m) { [smallVectorOfBool, smallVectorOfInt, smallVectorOfFloat, smallVectorOfInt16, smallVectorOfInt32, smallVectorOfInt64, smallVectorOfUInt16, smallVectorOfUInt32, smallVectorOfUInt64, - smallVectorOfChar, smallVectorOfDouble, - smallVectorOfLong](nb::type_object type) -> nb::object { + smallVectorOfChar, + smallVectorOfDouble](nb::type_object type) -> nb::object { PyTypeObject *typeObj = (PyTypeObject *)type.ptr(); nb::print(type); if (typeObj == &PyBool_Type) @@ -538,16 +539,14 @@ NB_MODULE(eudslpy_ext, m) { "__class_getitem__", [smallVectorOfFloat, smallVectorOfInt16, smallVectorOfInt32, smallVectorOfInt64, smallVectorOfUInt16, smallVectorOfUInt32, - smallVectorOfUInt64, smallVectorOfChar, smallVectorOfDouble, - smallVectorOfLong](std::string type) -> nb::object { + smallVectorOfUInt64, smallVectorOfChar, + smallVectorOfDouble](std::string type) -> nb::object { if (type == "char") return smallVectorOfChar; if (type == "float") return smallVectorOfFloat; if (type == "double") return smallVectorOfDouble; - if (type == "long") - return smallVectorOfLong; if (type == "int16") return smallVectorOfInt16; if (type == "int32") @@ -589,8 +588,6 @@ NB_MODULE(eudslpy_ext, m) { bind_iter_like, nb::rv_policy::reference_internal>(m, "iplist[Operation]"); - auto irModule = m.def_submodule("ir"); - populateIRModule(irModule); auto dialectsModule = m.def_submodule("dialects"); // auto accModule = dialectsModule.def_submodule("acc"); diff --git a/projects/eudsl-py/src/type_casters.h b/projects/eudsl-py/src/type_casters.h index 07ca096c..66cc31d4 100644 --- a/projects/eudsl-py/src/type_casters.h +++ b/projects/eudsl-py/src/type_casters.h @@ -3,8 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // Copyright (c) 2024. -#ifndef TYPE_CASTERS_H -#define TYPE_CASTERS_H +#pragma once #include #include @@ -87,5 +86,3 @@ struct nanobind::detail::type_caster { return PyUnicode_FromStringAndSize(s.data(), s.size()); } }; - -#endif // TYPE_CASTERS_H