[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
Tue Mar 29 05:24:29 PDT 2022


ldionne updated this revision to Diff 418848.
ldionne added a comment.

Fix CI failure


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
  libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp


Index: libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
===================================================================
--- libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
+++ libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++03
-// UNSUPPORTED: debug_level=0, debug_level=1, libcpp-has-assertions
 
 // <vector>
 
@@ -41,7 +40,9 @@
     v.erase(v.begin());
     v.erase(v.begin(), v.end());
 #if TEST_STD_VER >= 14
-    v.swap(w);
+    // TODO: vector::swap is not robust against ADL because we compare allocators, and that
+    //       triggers ADL when looking up operator==.
+    // v.swap(w);
 #endif
     return 0;
 }
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} -Wno-macro-redefined -D_LIBCPP_ENABLE_ASSERTIONS=1
+// RUN: %{run}
+
+// RUN: %{build} -Wno-macro-redefined -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.418848.patch
Type: text/x-patch
Size: 3527 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220329/038bd516/attachment-0001.bin>


More information about the libcxx-commits mailing list