[libcxx-commits] [PATCH] D123175: [libcxx] Add flag to turn on/off __builtin_assume in _LIBCPP_ASSERT

Arthur Eubanks via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 5 19:20:49 PDT 2022


aeubanks created this revision.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Introduce _LIBCPP_ASSERTIONS_ENABLE_ASSUME which makes _LIBCPP_ASSERT
not call __builtin_assume when _LIBCPP_ENABLE_ASSERTIONS == 0.

Calling __builtin_assume was introduced in D122397 <https://reviews.llvm.org/D122397>.

__builtin_assume is generally supposed to improve optimizations, but can
cause regressions when LLVM has trouble handling the calls to
`llvm.assume()` (see comments in D122397 <https://reviews.llvm.org/D122397>).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123175

Files:
  libcxx/include/__assert
  libcxx/test/libcxx/assertions/single_expression.sh.cpp


Index: libcxx/test/libcxx/assertions/single_expression.sh.cpp
===================================================================
--- libcxx/test/libcxx/assertions/single_expression.sh.cpp
+++ libcxx/test/libcxx/assertions/single_expression.sh.cpp
@@ -15,6 +15,12 @@
 // RUN: %{build} -Wno-macro-redefined -D_LIBCPP_ENABLE_ASSERTIONS=0
 // RUN: %{run}
 
+// RUN: %{build} -Wno-macro-redefined -D_LIBCPP_ENABLE_ASSERTIONS=0 -D_LIBCPP_ASSERTIONS_ENABLE_ASSUME=0
+// RUN: %{run}
+
+// RUN: %{build} -Wno-macro-redefined -D_LIBCPP_ENABLE_ASSERTIONS=0 -D_LIBCPP_ASSERTIONS_ENABLE_ASSUME=1
+// RUN: %{run}
+
 // We flag uses of the assertion handler in older dylibs at compile-time to avoid runtime
 // failures when back-deploying.
 // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11|12}}
Index: libcxx/include/__assert
===================================================================
--- libcxx/include/__assert
+++ libcxx/include/__assert
@@ -33,21 +33,29 @@
 # error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1"
 #endif
 
+// Set -D_LIBCPP_ASSERTIONS_ENABLE_ASSUME=0 to turn off calling
+// __builtin_assume() in _LIBCPP_ASSERT() when _LIBCPP_ENABLE_ASSERTIONS == 0.
+#ifndef _LIBCPP_ASSERTIONS_ENABLE_ASSUME
+# define _LIBCPP_ASSERTIONS_ENABLE_ASSUME 1
+#endif
+
+#if _LIBCPP_ASSERTIONS_ENABLE_ASSUME != 0 && _LIBCPP_ASSERTIONS_ENABLE_ASSUME != 1
+# error "_LIBCPP_ASSERTIONS_ENABLE_ASSUME must be set to 0 or 1"
+#endif
+
 #if _LIBCPP_ENABLE_ASSERTIONS
 # define _LIBCPP_ASSERT(expression, message)                                        \
     (__builtin_expect(static_cast<bool>(expression), 1) ?                           \
       (void)0 :                                                                     \
       ::std::__libcpp_assertion_handler(__FILE__, __LINE__, #expression, message))
+#elif _LIBCPP_ASSERTIONS_ENABLE_ASSUME && __has_builtin(__builtin_assume)
+# define _LIBCPP_ASSERT(expression, message)                                        \
+    (_LIBCPP_DIAGNOSTIC_PUSH                                                        \
+    _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume")                                    \
+    __builtin_assume(static_cast<bool>(expression))                                 \
+    _LIBCPP_DIAGNOSTIC_POP)
 #else
-# if __has_builtin(__builtin_assume)
-#   define _LIBCPP_ASSERT(expression, message)                                      \
-      (_LIBCPP_DIAGNOSTIC_PUSH                                                      \
-      _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume")                                  \
-      __builtin_assume(static_cast<bool>(expression))                               \
-      _LIBCPP_DIAGNOSTIC_POP)
-# else
-#   define _LIBCPP_ASSERT(expression, message) ((void)0)
-# endif
+# define _LIBCPP_ASSERT(expression, message) ((void)0)
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123175.420686.patch
Type: text/x-patch
Size: 2911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220406/c9bca8ae/attachment.bin>


More information about the libcxx-commits mailing list