From b5b92247ef65a0c3f28091d370734588fd9dafbe Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Thu, 11 Jan 2024 10:30:27 -0500 Subject: [PATCH] Add a DEAL_II_ASSUME macro Co-authored-by: Wolfgang Bangerth --- cmake/setup_compiler_flags_gnu.cmake | 6 ++++ include/deal.II/base/exceptions.h | 43 +++++++++++++++++++++------- source/fe/fe_q_base.cc | 4 --- source/grid/grid_generator.cc | 4 --- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/cmake/setup_compiler_flags_gnu.cmake b/cmake/setup_compiler_flags_gnu.cmake index ea945ce9bf..a5ed07f0dc 100644 --- a/cmake/setup_compiler_flags_gnu.cmake +++ b/cmake/setup_compiler_flags_gnu.cmake @@ -176,6 +176,12 @@ if (CMAKE_BUILD_TYPE MATCHES "Release") # mode. # enable_if_supported(DEAL_II_CXX_FLAGS_RELEASE "-Wno-unused-local-typedefs") + + # + # We are using __builtin_assume in Assert in Release mode and the compiler is + # warning about ignored side effects which we don't care about. + # + enable_if_supported(DEAL_II_CXX_FLAGS_RELEASE "-Wno-assume") endif() diff --git a/include/deal.II/base/exceptions.h b/include/deal.II/base/exceptions.h index ffbe7699de..2453809e53 100644 --- a/include/deal.II/base/exceptions.h +++ b/include/deal.II/base/exceptions.h @@ -1519,6 +1519,37 @@ namespace deal_II_exceptions } /*namespace deal_II_exceptions*/ +#if defined(__clang__) +# define DEAL_II_ASSUME(expr) __builtin_assume(static_cast(expr)) +#elif defined(__GNUC__) && !defined(__ICC) +# if __GNUC__ >= 13 +# define DEAL_II_ASSUME(expr) \ + do \ + { \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"") \ + [[assume(expr)]]; \ + _Pragma("GCC diagnostic pop") \ + } \ + while (false) +# else +/* no way with GCC to express this without evaluating 'expr' */ +# define DEAL_II_ASSUME(expr) \ + do \ + { \ + } \ + while (false) +# endif +#elif defined(_MSC_VER) || defined(__ICC) +# define DEAL_II_ASSUME(expr) __assume(expr); +#else +# define DEAL_II_ASSUME(expr) \ + do \ + { \ + } \ + while (false) +#endif + /** * A macro that serves as the main routine in the exception mechanism for debug @@ -1638,11 +1669,7 @@ namespace deal_II_exceptions # endif /*ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/ # endif /*KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/ #else /*ifdef DEBUG*/ -# define Assert(cond, exc) \ - do \ - { \ - } \ - while (false) +# define Assert(cond, exc) DEAL_II_ASSUME(cond) #endif /*ifdef DEBUG*/ @@ -1694,11 +1721,7 @@ namespace deal_II_exceptions while (false) # endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/ #else -# define AssertNothrow(cond, exc) \ - do \ - { \ - } \ - while (false) +# define AssertNothrow(cond, exc) DEAL_II_ASSUME(cond) #endif /** diff --git a/source/fe/fe_q_base.cc b/source/fe/fe_q_base.cc index 40b21bade6..afabd9881e 100644 --- a/source/fe/fe_q_base.cc +++ b/source/fe/fe_q_base.cc @@ -1532,9 +1532,7 @@ FE_Q_Base::get_restriction_matrix( } unsigned int j_indices[dim]; internal::FE_Q_Base::zero_indices(j_indices); -# ifdef DEBUG double sum_check = 0; -# endif for (unsigned int j = 0; j < q_dofs_per_cell; j += dofs1d) { double val_extra_dim = 1.; @@ -1554,9 +1552,7 @@ FE_Q_Base::get_restriction_matrix( my_restriction(mother_dof, child_dof) = 1.; else if (std::fabs(val) > eps) my_restriction(mother_dof, child_dof) = val; -# ifdef DEBUG sum_check += val; -# endif } internal::FE_Q_Base::increment_indices(j_indices, dofs1d); diff --git a/source/grid/grid_generator.cc b/source/grid/grid_generator.cc index f598404ba2..7f8a3acd94 100644 --- a/source/grid/grid_generator.cc +++ b/source/grid/grid_generator.cc @@ -1424,16 +1424,12 @@ namespace GridGenerator // above, so they are here (unless they are in the interior). Use // this to assign boundary indicators, but also make sure that we // encounter exactly 48 such faces -# ifdef DEBUG unsigned int count = 0; -# endif for (const auto &cell : tria.cell_iterators()) if (cell->face(5)->at_boundary()) { cell->face(5)->set_all_boundary_ids(1); -# ifdef DEBUG ++count; -# endif } Assert(count == 48, ExcInternalError()); } -- 2.39.5