[libc-commits] [libc] 45c84d5 - [libc] Add `__builtin_expect` tag on assert conditions; NFC (#99498)

via libc-commits libc-commits at lists.llvm.org
Sun Oct 27 06:49:23 PDT 2024


Author: goldsteinn
Date: 2024-10-27T08:49:20-05:00
New Revision: 45c84d59c454ba2b57affcd09a74f8d91e93bff7

URL: https://github.com/llvm/llvm-project/commit/45c84d59c454ba2b57affcd09a74f8d91e93bff7
DIFF: https://github.com/llvm/llvm-project/commit/45c84d59c454ba2b57affcd09a74f8d91e93bff7.diff

LOG: [libc] Add `__builtin_expect` tag on assert conditions; NFC (#99498)

Added: 
    

Modified: 
    libc/src/__support/CMakeLists.txt
    libc/src/__support/libc_assert.h
    libc/src/assert/assert.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 4785895b562b5e..14a3acff8fae93 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -277,6 +277,7 @@ add_header_library(
   DEPENDS
     .integer_to_string
     libc.src.__support.OSUtil.osutil
+    libc.src.__support.macros.optimization
 )
 
 add_header_library(

diff  --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h
index e21a58a0c8aad9..3db179ff672124 100644
--- a/libc/src/__support/libc_assert.h
+++ b/libc/src/__support/libc_assert.h
@@ -24,7 +24,8 @@
 #include "src/__support/OSUtil/exit.h"
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/integer_to_string.h"
-#include "src/__support/macros/attributes.h" // For LIBC_INLINE
+#include "src/__support/macros/attributes.h"   // For LIBC_INLINE
+#include "src/__support/macros/optimization.h" // For LIBC_UNLIKELY
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -71,7 +72,7 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,
 
 #define LIBC_ASSERT(COND)                                                      \
   do {                                                                         \
-    if (!(COND)) {                                                             \
+    if (LIBC_UNLIKELY(!(COND))) {                                              \
       LIBC_NAMESPACE::write_to_stderr(__FILE__ ":" __LIBC_LINE_STR__           \
                                                ": Assertion failed: '" #COND   \
                                                "' in function: '");            \

diff  --git a/libc/src/assert/assert.h b/libc/src/assert/assert.h
index 6f352af1988b37..1ea19ea5554f0a 100644
--- a/libc/src/assert/assert.h
+++ b/libc/src/assert/assert.h
@@ -18,8 +18,18 @@
 #ifdef NDEBUG
 #define assert(e) (void)0
 #else
+
+#ifdef __has_builtin
+#if __has_builtin(__builtin_expect)
+#define __LIBC_ASSERT_LIKELY(e) __builtin_expect(e, 1)
+#endif
+#endif
+#ifndef __LIBC_ASSERT_LIKELY
+#define __LIBC_ASSERT_LIKELY(e) e
+#endif
+
 #define assert(e)                                                              \
-  ((e) ? (void)0                                                               \
-       : LIBC_NAMESPACE::__assert_fail(#e, __FILE__, __LINE__,                 \
-                                       __PRETTY_FUNCTION__))
+  (__LIBC_ASSERT_LIKELY(e) ? (void)0                                           \
+                           : LIBC_NAMESPACE::__assert_fail(                    \
+                                 #e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
 #endif // NDEBUG


        


More information about the libc-commits mailing list