From: Yedidya Feldblum Date: Sun, 29 Oct 2017 04:19:52 +0000 (-0700) Subject: Move folly/Assume.h X-Git-Tag: v2017.10.30.00~5 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=2ee091de375639937677453f8f2742f22d3334d4 Move folly/Assume.h Summary: [Folly] Move `folly/Assume.h` to `folly/lang/`. Reviewed By: luciang, ot Differential Revision: D6181983 fbshipit-source-id: 25564bb07daa1a6765651cd919b4778efb931446 --- diff --git a/folly/Assume.cpp b/folly/Assume.cpp deleted file mode 100644 index bf148dfd..00000000 --- a/folly/Assume.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2017 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include - -namespace folly { - -namespace detail { - -void assume_check(bool cond) { - CHECK(cond) << "compiler-hint assumption fails at runtime"; -} - -} // namespace detail - -} // namespace folly diff --git a/folly/Assume.h b/folly/Assume.h index 36470fba..73b66a50 100644 --- a/folly/Assume.h +++ b/folly/Assume.h @@ -14,58 +14,4 @@ * limitations under the License. */ -#pragma once - -#include - -#include - -namespace folly { - -namespace detail { - -extern void assume_check(bool cond); - -} // namespace detail - -/** - * Inform the compiler that the argument can be assumed true. It is - * undefined behavior if the argument is not actually true, so use - * with care. - * - * Implemented as a function instead of a macro because - * __builtin_assume does not evaluate its argument at runtime, so it - * cannot be used with expressions that have side-effects. - */ - -FOLLY_ALWAYS_INLINE void assume(bool cond) { - if (kIsDebug) { - detail::assume_check(cond); - } else { -#if defined(__clang__) // Must go first because Clang also defines __GNUC__. - __builtin_assume(cond); -#elif defined(__GNUC__) - if (!cond) { __builtin_unreachable(); } -#elif defined(_MSC_VER) - __assume(cond); -#else - // Do nothing. -#endif - } -} - -[[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(); -#endif -} - -} // namespace folly +#include // @shim diff --git a/folly/Bits.h b/folly/Bits.h index 942de4dc..529073b2 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -60,8 +60,8 @@ #include #include -#include #include +#include #include namespace folly { diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index eb493fc6..0d028714 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -37,6 +36,7 @@ #include #include #include +#include #ifdef __GNUC__ #pragma GCC diagnostic push diff --git a/folly/Makefile.am b/folly/Makefile.am index f2b89b7e..dafdd730 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -312,6 +312,7 @@ nobase_follyinclude_HEADERS = \ io/async/test/UndelayedDestruction.h \ io/async/test/Util.h \ json.h \ + lang/Assume.h \ Launder.h \ Lazy.h \ LifoSem.h \ @@ -486,7 +487,6 @@ libfollybase_la_SOURCES = \ Unicode.cpp libfolly_la_SOURCES = \ - Assume.cpp \ ClockGettimeWrappers.cpp \ compression/Compression.cpp \ compression/Zlib.cpp \ @@ -560,6 +560,7 @@ libfolly_la_SOURCES = \ io/async/ssl/OpenSSLUtils.cpp \ io/async/ssl/SSLErrors.cpp \ json.cpp \ + lang/Assume.cpp \ detail/MemoryIdler.cpp \ detail/SocketFastOpen.cpp \ MacAddress.cpp \ diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index 77706213..7fb038b2 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -34,12 +34,12 @@ #include -#include #include #include #include #include #include +#include #include #include #include diff --git a/folly/dynamic.cpp b/folly/dynamic.cpp index aee3e87d..f2be8f27 100644 --- a/folly/dynamic.cpp +++ b/folly/dynamic.cpp @@ -16,9 +16,9 @@ #include -#include #include #include +#include #include namespace folly { diff --git a/folly/experimental/EliasFanoCoding.h b/folly/experimental/EliasFanoCoding.h index 628b813d..04a0f158 100644 --- a/folly/experimental/EliasFanoCoding.h +++ b/folly/experimental/EliasFanoCoding.h @@ -28,7 +28,6 @@ #include #include -#include #include #include #include @@ -36,6 +35,7 @@ #include #include #include +#include #include #if !FOLLY_X64 diff --git a/folly/lang/Assume.cpp b/folly/lang/Assume.cpp new file mode 100644 index 00000000..52d62a42 --- /dev/null +++ b/folly/lang/Assume.cpp @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +namespace folly { + +namespace detail { + +void assume_check(bool cond) { + CHECK(cond) << "compiler-hint assumption fails at runtime"; +} + +} // namespace detail + +} // namespace folly diff --git a/folly/lang/Assume.h b/folly/lang/Assume.h new file mode 100644 index 00000000..36470fba --- /dev/null +++ b/folly/lang/Assume.h @@ -0,0 +1,71 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include + +namespace folly { + +namespace detail { + +extern void assume_check(bool cond); + +} // namespace detail + +/** + * Inform the compiler that the argument can be assumed true. It is + * undefined behavior if the argument is not actually true, so use + * with care. + * + * Implemented as a function instead of a macro because + * __builtin_assume does not evaluate its argument at runtime, so it + * cannot be used with expressions that have side-effects. + */ + +FOLLY_ALWAYS_INLINE void assume(bool cond) { + if (kIsDebug) { + detail::assume_check(cond); + } else { +#if defined(__clang__) // Must go first because Clang also defines __GNUC__. + __builtin_assume(cond); +#elif defined(__GNUC__) + if (!cond) { __builtin_unreachable(); } +#elif defined(_MSC_VER) + __assume(cond); +#else + // Do nothing. +#endif + } +} + +[[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(); +#endif +} + +} // namespace folly diff --git a/folly/small_vector.h b/folly/small_vector.h index d8911a07..7287585a 100644 --- a/folly/small_vector.h +++ b/folly/small_vector.h @@ -45,12 +45,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include