From f8c8cdae089f906ba69b75b038ffc9a1d28cce44 Mon Sep 17 00:00:00 2001 From: wk Date: Mon, 12 Jun 2023 16:39:37 +0200 Subject: [PATCH 1/3] Compile- and run-time assertions for the construction of Ref. --- Eigen/src/Core/Ref.h | 18 +++++++++++++++++- failtest/CMakeLists.txt | 2 ++ failtest/ref_6.cpp | 15 +++++++++++++++ failtest/ref_7.cpp | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 failtest/ref_6.cpp create mode 100644 failtest/ref_7.cpp diff --git a/Eigen/src/Core/Ref.h b/Eigen/src/Core/Ref.h index 81de5f9d9..c872749ea 100644 --- a/Eigen/src/Core/Ref.h +++ b/Eigen/src/Core/Ref.h @@ -332,6 +332,16 @@ template class Ref< : public RefBase > { typedef internal::traits Traits; + + static constexpr bool may_map_m_object_successfully = + (StrideType::InnerStrideAtCompileTime == 0 || + StrideType::InnerStrideAtCompileTime == 1 || + StrideType::InnerStrideAtCompileTime == Dynamic) && + (TPlainObjectType::IsVectorAtCompileTime || + StrideType::OuterStrideAtCompileTime == 0 || + StrideType::OuterStrideAtCompileTime == Dynamic || + StrideType::OuterStrideAtCompileTime == TPlainObjectType::InnerSizeAtCompileTime || + TPlainObjectType::InnerSizeAtCompileTime == Dynamic); public: typedef RefBase Base; @@ -344,6 +354,8 @@ template class Ref< // std::cout << match_helper::HasDirectAccess << "," << match_helper::OuterStrideMatch << "," << match_helper::InnerStrideMatch << "\n"; // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n"; // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n"; + EIGEN_STATIC_ASSERT(typename Traits::template match::type::value || may_map_m_object_successfully, + STORAGE_LAYOUT_DOES_NOT_MATCH); construct(expr.derived(), typename Traits::template match::type()); } @@ -353,6 +365,8 @@ template class Ref< template EIGEN_DEVICE_FUNC inline Ref(const RefBase& other) { + EIGEN_STATIC_ASSERT(typename Traits::template match::type::value || may_map_m_object_successfully, + STORAGE_LAYOUT_DOES_NOT_MATCH); construct(other.derived(), typename Traits::template match::type()); } @@ -371,7 +385,9 @@ template class Ref< EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type) { internal::call_assignment_no_alias(m_object,expr,internal::assign_op()); - Base::construct(m_object); + const bool success = Base::construct(m_object); + EIGEN_UNUSED_VARIABLE(success) + eigen_assert(success); } protected: diff --git a/failtest/CMakeLists.txt b/failtest/CMakeLists.txt index 2c5fc3351..107c9e2c9 100644 --- a/failtest/CMakeLists.txt +++ b/failtest/CMakeLists.txt @@ -36,6 +36,8 @@ ei_add_failtest("ref_2") ei_add_failtest("ref_3") ei_add_failtest("ref_4") ei_add_failtest("ref_5") +ei_add_failtest("ref_6") +ei_add_failtest("ref_7") ei_add_failtest("swap_1") ei_add_failtest("swap_2") diff --git a/failtest/ref_6.cpp b/failtest/ref_6.cpp new file mode 100644 index 000000000..b246f97ee --- /dev/null +++ b/failtest/ref_6.cpp @@ -0,0 +1,15 @@ +#include "../Eigen/Core" + +using namespace Eigen; + +void call_ref(Ref>) {} + +int main() { + VectorXf a(10); + Map> m(a.data(), 5); +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD + call_ref(a); +#else + call_ref(m); +#endif +} diff --git a/failtest/ref_7.cpp b/failtest/ref_7.cpp new file mode 100644 index 000000000..7d6a3cb5c --- /dev/null +++ b/failtest/ref_7.cpp @@ -0,0 +1,16 @@ +#include "../Eigen/Core" + +using namespace Eigen; + +void call_ref(Ref>) {} + +int main() { + MatrixXf a(6, 2); + Map> md(a.data(), OuterStride(2)); + Map> m2(a.data()); +#ifdef EIGEN_SHOULD_FAIL_TO_BUILD + call_ref(md); +#else + call_ref(m2); +#endif +} -- GitLab From d28ca34e37ee623245e42e799b25bb5ae3c0f13c Mon Sep 17 00:00:00 2001 From: wk Date: Mon, 12 Jun 2023 17:04:55 +0200 Subject: [PATCH 2/3] Misplaced "typename" removed. --- Eigen/src/Core/Ref.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Eigen/src/Core/Ref.h b/Eigen/src/Core/Ref.h index c872749ea..23fbac6d1 100644 --- a/Eigen/src/Core/Ref.h +++ b/Eigen/src/Core/Ref.h @@ -354,7 +354,7 @@ template class Ref< // std::cout << match_helper::HasDirectAccess << "," << match_helper::OuterStrideMatch << "," << match_helper::InnerStrideMatch << "\n"; // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n"; // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n"; - EIGEN_STATIC_ASSERT(typename Traits::template match::type::value || may_map_m_object_successfully, + EIGEN_STATIC_ASSERT(Traits::template match::type::value || may_map_m_object_successfully, STORAGE_LAYOUT_DOES_NOT_MATCH); construct(expr.derived(), typename Traits::template match::type()); } @@ -365,7 +365,7 @@ template class Ref< template EIGEN_DEVICE_FUNC inline Ref(const RefBase& other) { - EIGEN_STATIC_ASSERT(typename Traits::template match::type::value || may_map_m_object_successfully, + EIGEN_STATIC_ASSERT(Traits::template match::type::value || may_map_m_object_successfully, STORAGE_LAYOUT_DOES_NOT_MATCH); construct(other.derived(), typename Traits::template match::type()); } -- GitLab From 9446b82de931114af12a37de5ea233ef989cdeb8 Mon Sep 17 00:00:00 2001 From: wk Date: Tue, 13 Jun 2023 19:02:21 +0200 Subject: [PATCH 3/3] https://gitlab.com/libeigen/eigen/-/merge_requests/1347#note_1427441510 --- Eigen/src/Core/Ref.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Eigen/src/Core/Ref.h b/Eigen/src/Core/Ref.h index 23fbac6d1..11866f7e6 100644 --- a/Eigen/src/Core/Ref.h +++ b/Eigen/src/Core/Ref.h @@ -386,7 +386,7 @@ template class Ref< { internal::call_assignment_no_alias(m_object,expr,internal::assign_op()); const bool success = Base::construct(m_object); - EIGEN_UNUSED_VARIABLE(success) + EIGEN_ONLY_USED_FOR_DEBUG(success) eigen_assert(success); } -- GitLab