[libcxx-commits] [PATCH] D122397: [libc++] Use __builtin_expect and __builtin_assume in _LIBCPP_ASSERT
Louis Dionne via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Mar 24 12:07:47 PDT 2022
ldionne updated this revision to Diff 418007.
ldionne retitled this revision from "[libc++] Use __builtin_expect in _LIBCPP_ASSERT" to "[libc++] Use __builtin_expect and __builtin_assume in _LIBCPP_ASSERT".
ldionne edited the summary of this revision.
ldionne added a comment.
Address review comments.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D122397/new/
https://reviews.llvm.org/D122397
Files:
libcxx/include/__assert
libcxx/test/libcxx/assertions/single_expression.sh.cpp
Index: libcxx/test/libcxx/assertions/single_expression.sh.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/assertions/single_expression.sh.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Make sure that _LIBCPP_ASSERT is a single expression. This is useful so we can use
+// it in places that require an expression, such as in a constructor initializer list.
+
+// RUN: %{build} -D_LIBCPP_ENABLE_ASSERTIONS=1
+// RUN: %{run}
+
+// RUN: %{build} -D_LIBCPP_ENABLE_ASSERTIONS=0
+// 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}}
+
+#include <__assert>
+#include <cassert>
+
+void f() {
+ int i = (_LIBCPP_ASSERT(true, "message"), 3);
+ assert(i == 3);
+ return _LIBCPP_ASSERT(true, "message");
+}
+
+int main(int, char**) {
+ f();
+ return 0;
+}
Index: libcxx/include/__assert
===================================================================
--- libcxx/include/__assert
+++ libcxx/include/__assert
@@ -34,9 +34,20 @@
#endif
#if _LIBCPP_ENABLE_ASSERTIONS
-# define _LIBCPP_ASSERT(expression, message) ((expression) ? (void)0 : ::std::__libcpp_assertion_handler(__FILE__, __LINE__, #expression, message))
+# define _LIBCPP_ASSERT(expression, message) \
+ (__builtin_expect(static_cast<bool>(expression), 1) ? \
+ (void)0 : \
+ ::std::__libcpp_assertion_handler(__FILE__, __LINE__, #expression, message))
#else
-# define _LIBCPP_ASSERT(x, m) ((void)0)
+# 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
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122397.418007.patch
Type: text/x-patch
Size: 2658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220324/30c6d029/attachment.bin>
More information about the libcxx-commits
mailing list