Commit d3f0a283 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Inline the assume() check in debug mode

Summary: Even in debug mode, the branch can likely be coalesced with the actual condition if inlined.

Reviewed By: yfeldblum

Differential Revision: D27285173

fbshipit-source-id: 1662588f82d14a5ab2c9564bab5cec72981418ec
parent 413e451f
...@@ -16,21 +16,22 @@ ...@@ -16,21 +16,22 @@
#pragma once #pragma once
#include <cstdlib> #include <folly/Likely.h>
#include <folly/Portability.h> #include <folly/Portability.h>
namespace folly { namespace folly {
namespace detail { namespace detail {
extern void assume_check(bool cond); [[noreturn]] void assume_terminate();
} // namespace detail } // namespace detail
FOLLY_ALWAYS_INLINE void assume(bool cond) { FOLLY_ALWAYS_INLINE void assume(bool cond) {
if (kIsDebug) { if (kIsDebug) {
detail::assume_check(cond); if (FOLLY_UNLIKELY(!cond)) {
detail::assume_terminate();
}
} else { } else {
#if defined(__clang__) // Must go first because Clang also defines __GNUC__. #if defined(__clang__) // Must go first because Clang also defines __GNUC__.
__builtin_assume(cond); __builtin_assume(cond);
...@@ -47,16 +48,12 @@ FOLLY_ALWAYS_INLINE void assume(bool cond) { ...@@ -47,16 +48,12 @@ FOLLY_ALWAYS_INLINE void assume(bool cond) {
} }
[[noreturn]] FOLLY_ALWAYS_INLINE void assume_unreachable() { [[noreturn]] FOLLY_ALWAYS_INLINE void assume_unreachable() {
assume(false);
// Do a bit more to get the compiler to understand
// that this function really will never return.
#if defined(__GNUC__) #if defined(__GNUC__)
__builtin_unreachable(); __builtin_unreachable();
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
__assume(0); __assume(0);
#else #else
// Well, it's better than nothing. detail::assume_terminate();
std::abort();
#endif #endif
} }
......
...@@ -22,8 +22,8 @@ namespace folly { ...@@ -22,8 +22,8 @@ namespace folly {
namespace detail { namespace detail {
void assume_check(bool cond) { [[noreturn]] void assume_terminate() {
FOLLY_SAFE_CHECK(cond, "compiler-hint assumption fails at runtime"); FOLLY_SAFE_CHECK(false, "compiler-hint assumption fails at runtime");
} }
} // namespace detail } // namespace detail
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment