[libc-commits] [libc] [libc] Add `__builtin_expect` tag on assert conditions; NFC (PR #99498)
via libc-commits
libc-commits at lists.llvm.org
Tue Jul 23 02:23:14 PDT 2024
https://github.com/goldsteinn updated https://github.com/llvm/llvm-project/pull/99498
>From c2c7943cb3a6c51bf4554759ced0d72e3a5093c3 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Thu, 18 Jul 2024 22:26:47 +0800
Subject: [PATCH] [libc] Add `__builtin_expect` tag on assert conditions; NFC
---
libc/src/__support/CMakeLists.txt | 1 +
libc/src/__support/libc_assert.h | 5 +++--
libc/src/assert/assert.h | 16 +++++++++++++---
3 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index d8a192f1ffa57..aa036fbaa6426 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -274,6 +274,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 e21a58a0c8aad..3db179ff67212 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 6f352af1988b3..1ea19ea5554f0 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