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 @@
#pragma once
#include <cstdlib>
#include <folly/Likely.h>
#include <folly/Portability.h>
namespace folly {
namespace detail {
extern void assume_check(bool cond);
[[noreturn]] void assume_terminate();
} // namespace detail
FOLLY_ALWAYS_INLINE void assume(bool cond) {
if (kIsDebug) {
detail::assume_check(cond);
if (FOLLY_UNLIKELY(!cond)) {
detail::assume_terminate();
}
} else {
#if defined(__clang__) // Must go first because Clang also defines __GNUC__.
__builtin_assume(cond);
......@@ -47,16 +48,12 @@ FOLLY_ALWAYS_INLINE void assume(bool cond) {
}
[[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__)
__builtin_unreachable();
#elif defined(_MSC_VER)
__assume(0);
#else
// Well, it's better than nothing.
std::abort();
detail::assume_terminate();
#endif
}
......
......@@ -22,8 +22,8 @@ namespace folly {
namespace detail {
void assume_check(bool cond) {
FOLLY_SAFE_CHECK(cond, "compiler-hint assumption fails at runtime");
[[noreturn]] void assume_terminate() {
FOLLY_SAFE_CHECK(false, "compiler-hint assumption fails at runtime");
}
} // 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