]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a DEAL_II_ASSUME macro 16437/head
authorDaniel Arndt <arndtd@ornl.gov>
Thu, 11 Jan 2024 15:30:27 +0000 (10:30 -0500)
committerDaniel Arndt <arndtd@ornl.gov>
Thu, 11 Jan 2024 17:50:15 +0000 (12:50 -0500)
Co-authored-by: Wolfgang Bangerth <bangerth@colostate.edu>
cmake/setup_compiler_flags_gnu.cmake
include/deal.II/base/exceptions.h
source/fe/fe_q_base.cc
source/grid/grid_generator.cc

index ea945ce9bf0095f44d2f4d69ca8b99f2138df8a1..a5ed07f0dc5433eea680fd3ac84d961756c44f40 100644 (file)
@@ -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()
 
 
index ffbe7699de4bd8908d851989a754841bc7515199..2453809e5307cc7704ed43b42f8083c9e7e2306e 100644 (file)
@@ -1519,6 +1519,37 @@ namespace deal_II_exceptions
 } /*namespace deal_II_exceptions*/
 
 
+#if defined(__clang__)
+#  define DEAL_II_ASSUME(expr) __builtin_assume(static_cast<bool>(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
 
 /**
index 40b21bade6d97f86262b59fc60eb29ff5680169a..afabd9881e037288fc202880aacb66f4f4b86446 100644 (file)
@@ -1532,9 +1532,7 @@ FE_Q_Base<dim, spacedim>::get_restriction_matrix(
                   }
               unsigned int j_indices[dim];
               internal::FE_Q_Base::zero_indices<dim>(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<dim, spacedim>::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<dim>(j_indices,
                                                               dofs1d);
index f598404ba2cfddb9e8cb54be800fb5e94bb8591a..7f8a3acd94cee9af43ef885e3cb225c92c14c22a 100644 (file)
@@ -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());
         }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.