From 27e7fd18f1c337c409f2a567697b27bcd0c09f8b Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Wed, 7 Jul 2021 17:12:12 -0400 Subject: Support manually disabling exceptions Rename EIGEN_EXCEPTIONS to EIGEN_USE_EXCEPTIONS, and allow disabling exceptions with -DEIGEN_USE_EXCEPTIONS=0. --- Eigen/Core | 2 +- Eigen/src/Core/CommaInitializer.h | 2 +- Eigen/src/Core/arch/SYCL/SyclMemoryModel.h | 2 +- Eigen/src/Core/util/Macros.h | 15 +++++++++------ Eigen/src/Core/util/Memory.h | 2 +- Eigen/src/SparseLU/SparseLU_Memory.h | 12 ++++-------- test/ctorleak.cpp | 13 +++++-------- test/main.h | 18 +++++++++++------- unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h | 6 +++--- unsupported/test/cxx11_maxsizevector.cpp | 10 +++++----- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Eigen/Core b/Eigen/Core index 5921e15f9..7fb033a86 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -30,7 +30,7 @@ #endif -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS #include #endif diff --git a/Eigen/src/Core/CommaInitializer.h b/Eigen/src/Core/CommaInitializer.h index c0e29c75c..eb23dedd4 100644 --- a/Eigen/src/Core/CommaInitializer.h +++ b/Eigen/src/Core/CommaInitializer.h @@ -103,7 +103,7 @@ struct CommaInitializer EIGEN_DEVICE_FUNC inline ~CommaInitializer() -#if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS +#if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && EIGEN_USE_EXCEPTIONS EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception) #endif { diff --git a/Eigen/src/Core/arch/SYCL/SyclMemoryModel.h b/Eigen/src/Core/arch/SYCL/SyclMemoryModel.h index f81e59db5..9e908fed9 100644 --- a/Eigen/src/Core/arch/SYCL/SyclMemoryModel.h +++ b/Eigen/src/Core/arch/SYCL/SyclMemoryModel.h @@ -25,7 +25,7 @@ #define EIGEN_CXX11_TENSOR_TENSOR_SYCL_STORAGE_MEMORY_H #include -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS #include #endif #include diff --git a/Eigen/src/Core/util/Macros.h b/Eigen/src/Core/util/Macros.h index fca94a989..1f90847f9 100644 --- a/Eigen/src/Core/util/Macros.h +++ b/Eigen/src/Core/util/Macros.h @@ -959,9 +959,8 @@ #undef EIGEN_INTERNAL_DEBUGGING #endif - #ifdef EIGEN_EXCEPTIONS - #undef EIGEN_EXCEPTIONS - #endif + #undef EIGEN_USE_EXCEPTIONS + #define EIGEN_USE_EXCEPTIONS 0 #endif #if defined(SYCL_DEVICE_ONLY) @@ -1378,12 +1377,16 @@ namespace Eigen { EIGEN_MAKE_SCALAR_BINARY_OP_ONTHERIGHT(METHOD,OPNAME) -#if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(EIGEN_CUDA_ARCH) && !defined(EIGEN_EXCEPTIONS) && !defined(EIGEN_USE_SYCL) && !defined(EIGEN_HIP_DEVICE_COMPILE) - #define EIGEN_EXCEPTIONS +#ifndef EIGEN_USE_EXCEPTIONS + #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(EIGEN_CUDA_ARCH) && !defined(EIGEN_USE_SYCL) && !defined(EIGEN_HIP_DEVICE_COMPILE) + #define EIGEN_USE_EXCEPTIONS 1 + #else + #define EIGEN_USE_EXCEPTIONS 0 + #endif #endif -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS # define EIGEN_THROW_X(X) throw X # define EIGEN_THROW throw # define EIGEN_TRY try diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h index 875318cdb..7a8afba99 100644 --- a/Eigen/src/Core/util/Memory.h +++ b/Eigen/src/Core/util/Memory.h @@ -66,7 +66,7 @@ namespace internal { EIGEN_DEVICE_FUNC inline void throw_std_bad_alloc() { - #ifdef EIGEN_EXCEPTIONS + #if EIGEN_USE_EXCEPTIONS throw std::bad_alloc(); #else std::size_t huge = static_cast(-1); diff --git a/Eigen/src/SparseLU/SparseLU_Memory.h b/Eigen/src/SparseLU/SparseLU_Memory.h index 349bfd585..711cb7ec3 100644 --- a/Eigen/src/SparseLU/SparseLU_Memory.h +++ b/Eigen/src/SparseLU/SparseLU_Memory.h @@ -76,13 +76,11 @@ Index SparseLUImpl::expand(VectorType& vec, Index& length, old_vec = vec.segment(0,nbElts); //Allocate or expand the current vector -#ifdef EIGEN_EXCEPTIONS - try -#endif + EIGEN_TRY { vec.resize(new_len); } -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS catch(std::bad_alloc& ) #else if(!vec.size()) @@ -107,13 +105,11 @@ Index SparseLUImpl::expand(VectorType& vec, Index& length, { alpha = (alpha + 1)/2; new_len = (std::max)(length+1,Index(alpha * length)); -#ifdef EIGEN_EXCEPTIONS - try -#endif + EIGEN_TRY { vec.resize(new_len); } -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS catch(std::bad_alloc& ) #else if (!vec.size()) diff --git a/test/ctorleak.cpp b/test/ctorleak.cpp index 73904176b..e76a4a7ba 100644 --- a/test/ctorleak.cpp +++ b/test/ctorleak.cpp @@ -10,7 +10,7 @@ struct Foo Foo() : dummy(0) { -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS // TODO: Is this the correct way to handle this? if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); } #endif @@ -46,10 +46,7 @@ EIGEN_DECLARE_TEST(ctorleak) MatrixX r(rows, cols); Foo::object_limit = r.size()+internal::random(0, rows*cols - 2); std::cout << "object_limit =" << Foo::object_limit << std::endl; -#ifdef EIGEN_EXCEPTIONS - try - { -#endif + EIGEN_TRY { if(internal::random()) { std::cout << "\nMatrixX m(" << rows << ", " << cols << ");\n"; MatrixX m(rows, cols); @@ -58,11 +55,11 @@ EIGEN_DECLARE_TEST(ctorleak) std::cout << "\nMatrixX m(r);\n"; MatrixX m(r); } -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS VERIFY(false); // not reached if exceptions are enabled - } - catch (const Foo::Fail&) { /* ignore */ } #endif + } + EIGEN_CATCH(const Foo::Fail&) { /* ignore */ } } VERIFY_IS_EQUAL(Index(0), Foo::object_count); diff --git a/test/main.h b/test/main.h index 786673dea..77943040b 100644 --- a/test/main.h +++ b/test/main.h @@ -209,8 +209,12 @@ namespace Eigen #define EIGEN_DEFAULT_IO_FORMAT IOFormat(4, 0, " ", "\n", "", "", "", "") -#if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__) && !defined(__SYCL_DEVICE_ONLY__) - #define EIGEN_EXCEPTIONS +#ifndef EIGEN_USE_EXCEPTIONS + #if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__) && !defined(__SYCL_DEVICE_ONLY__) + #define EIGEN_USE_EXCEPTIONS 1 + #else + #define EIGEN_USE_EXCEPTIONS 0 + #endif #endif #ifndef EIGEN_NO_ASSERTION_CHECKING @@ -267,7 +271,7 @@ namespace Eigen eigen_assert_list.push_back(std::string(EIGEN_MAKESTRING(__FILE__) " (" EIGEN_MAKESTRING(__LINE__) ") : " #a) ); \ } - #ifdef EIGEN_EXCEPTIONS + #if EIGEN_USE_EXCEPTIONS #define VERIFY_RAISES_ASSERT(a) \ { \ Eigen::no_more_assert = false; \ @@ -286,7 +290,7 @@ namespace Eigen Eigen::report_on_cerr_on_assert_failure = true; \ Eigen::internal::push_assert = false; \ } - #endif //EIGEN_EXCEPTIONS + #endif //EIGEN_USE_EXCEPTIONS #elif !defined(__CUDACC__) && !defined(__HIPCC__) && !defined(SYCL_DEVICE_ONLY) // EIGEN_DEBUG_ASSERTS // see bug 89. The copy_bool here is working around a bug in gcc <= 4.3 @@ -300,7 +304,7 @@ namespace Eigen EIGEN_THROW_X(Eigen::eigen_assert_exception()); \ } - #ifdef EIGEN_EXCEPTIONS + #if EIGEN_USE_EXCEPTIONS #define VERIFY_RAISES_ASSERT(a) { \ Eigen::no_more_assert = false; \ Eigen::report_on_cerr_on_assert_failure = false; \ @@ -311,10 +315,10 @@ namespace Eigen catch (Eigen::eigen_assert_exception&) { VERIFY(true); } \ Eigen::report_on_cerr_on_assert_failure = true; \ } - #endif // EIGEN_EXCEPTIONS + #endif // EIGEN_USE_EXCEPTIONS #endif // EIGEN_DEBUG_ASSERTS - #if defined(TEST_CHECK_STATIC_ASSERTIONS) && defined(EIGEN_EXCEPTIONS) + #if defined(TEST_CHECK_STATIC_ASSERTIONS) && EIGEN_USE_EXCEPTIONS #define EIGEN_STATIC_ASSERT(a,MSG) \ if( (!Eigen::internal::copy_bool(a)) && (!no_more_assert) )\ { \ diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h index df591c21d..3ccac604f 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h @@ -506,7 +506,7 @@ class QueueInterface { EIGEN_STRONG_INLINE void synchronize() const { -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS m_queue.wait_and_throw(); #else m_queue.wait(); @@ -749,7 +749,7 @@ class QueueInterface { set_latest_event(e); if (callback) { auto callback_ = [=]() { -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS cl::sycl::event(e).wait_and_throw(); #else cl::sycl::event(e).wait(); @@ -758,7 +758,7 @@ class QueueInterface { }; m_thread_pool.Schedule(std::move(callback_)); } else { -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS m_queue.wait_and_throw(); #else m_queue.wait(); diff --git a/unsupported/test/cxx11_maxsizevector.cpp b/unsupported/test/cxx11_maxsizevector.cpp index 46b689a8e..24064c4ee 100644 --- a/unsupported/test/cxx11_maxsizevector.cpp +++ b/unsupported/test/cxx11_maxsizevector.cpp @@ -12,7 +12,7 @@ struct Foo Foo(int x=0) : dummy(x) { -#ifdef EIGEN_EXCEPTIONS +#if EIGEN_USE_EXCEPTIONS // TODO: Is this the correct way to handle this? if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); } #endif @@ -50,17 +50,17 @@ EIGEN_DECLARE_TEST(cxx11_maxsizevector) Foo::object_limit = internal::random(0, rows - 2); std::cout << "object_limit = " << Foo::object_limit << std::endl; bool exception_raised = false; -#ifdef EIGEN_EXCEPTIONS - try + EIGEN_TRY { -#endif std::cout << "\nVectorX m(" << rows << ");\n"; VectorX vect(rows); for(int i=0; i