optimization.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: optimization.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines portable macros for performance optimization.
  21. //
  22. // This header is included in both C++ code and legacy C code and thus must
  23. // remain compatible with both C and C++. C compatibility will be removed if
  24. // the legacy code is removed or converted to C++. Do not include this header in
  25. // new code that requires C compatibility or assume C compatibility will remain
  26. // indefinitely.
  27. #ifndef Y_ABSL_BASE_OPTIMIZATION_H_
  28. #define Y_ABSL_BASE_OPTIMIZATION_H_
  29. #include <assert.h>
  30. #ifdef __cplusplus
  31. // Included for std::unreachable()
  32. #include <utility>
  33. #endif // __cplusplus
  34. #include "y_absl/base/config.h"
  35. #include "y_absl/base/options.h"
  36. // Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
  37. //
  38. // Instructs the compiler to avoid optimizing tail-call recursion. This macro is
  39. // useful when you wish to preserve the existing function order within a stack
  40. // trace for logging, debugging, or profiling purposes.
  41. //
  42. // Example:
  43. //
  44. // int f() {
  45. // int result = g();
  46. // Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
  47. // return result;
  48. // }
  49. #if defined(__pnacl__)
  50. #define Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  51. #elif defined(__clang__)
  52. // Clang will not tail call given inline volatile assembly.
  53. #define Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  54. #elif defined(__GNUC__)
  55. // GCC will not tail call given inline volatile assembly.
  56. #define Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  57. #elif defined(_MSC_VER)
  58. #include <intrin.h>
  59. // The __nop() intrinsic blocks the optimisation.
  60. #define Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
  61. #else
  62. #define Y_ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  63. #endif
  64. // Y_ABSL_CACHELINE_SIZE
  65. //
  66. // Explicitly defines the size of the L1 cache for purposes of alignment.
  67. // Setting the cacheline size allows you to specify that certain objects be
  68. // aligned on a cacheline boundary with `Y_ABSL_CACHELINE_ALIGNED` declarations.
  69. // (See below.)
  70. //
  71. // NOTE: this macro should be replaced with the following C++17 features, when
  72. // those are generally available:
  73. //
  74. // * `std::hardware_constructive_interference_size`
  75. // * `std::hardware_destructive_interference_size`
  76. //
  77. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  78. // for more information.
  79. #if defined(__GNUC__)
  80. // Cache line alignment
  81. #if defined(__i386__) || defined(__x86_64__)
  82. #define Y_ABSL_CACHELINE_SIZE 64
  83. #elif defined(__powerpc64__)
  84. #define Y_ABSL_CACHELINE_SIZE 128
  85. #elif defined(__aarch64__)
  86. // We would need to read special register ctr_el0 to find out L1 dcache size.
  87. // This value is a good estimate based on a real aarch64 machine.
  88. #define Y_ABSL_CACHELINE_SIZE 64
  89. #elif defined(__arm__)
  90. // Cache line sizes for ARM: These values are not strictly correct since
  91. // cache line sizes depend on implementations, not architectures. There
  92. // are even implementations with cache line sizes configurable at boot
  93. // time.
  94. #if defined(__ARM_ARCH_5T__)
  95. #define Y_ABSL_CACHELINE_SIZE 32
  96. #elif defined(__ARM_ARCH_7A__)
  97. #define Y_ABSL_CACHELINE_SIZE 64
  98. #endif
  99. #endif
  100. #endif
  101. #ifndef Y_ABSL_CACHELINE_SIZE
  102. // A reasonable default guess. Note that overestimates tend to waste more
  103. // space, while underestimates tend to waste more time.
  104. #define Y_ABSL_CACHELINE_SIZE 64
  105. #endif
  106. // Y_ABSL_CACHELINE_ALIGNED
  107. //
  108. // Indicates that the declared object be cache aligned using
  109. // `Y_ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
  110. // load a set of related objects in the L1 cache for performance improvements.
  111. // Cacheline aligning objects properly allows constructive memory sharing and
  112. // prevents destructive (or "false") memory sharing.
  113. //
  114. // NOTE: callers should replace uses of this macro with `alignas()` using
  115. // `std::hardware_constructive_interference_size` and/or
  116. // `std::hardware_destructive_interference_size` when C++17 becomes available to
  117. // them.
  118. //
  119. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  120. // for more information.
  121. //
  122. // On some compilers, `Y_ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
  123. // or `__declspec` attribute. For compilers where this is not known to work,
  124. // the macro expands to nothing.
  125. //
  126. // No further guarantees are made here. The result of applying the macro
  127. // to variables and types is always implementation-defined.
  128. //
  129. // WARNING: It is easy to use this attribute incorrectly, even to the point
  130. // of causing bugs that are difficult to diagnose, crash, etc. It does not
  131. // of itself guarantee that objects are aligned to a cache line.
  132. //
  133. // NOTE: Some compilers are picky about the locations of annotations such as
  134. // this attribute, so prefer to put it at the beginning of your declaration.
  135. // For example,
  136. //
  137. // Y_ABSL_CACHELINE_ALIGNED static Foo* foo = ...
  138. //
  139. // class Y_ABSL_CACHELINE_ALIGNED Bar { ...
  140. //
  141. // Recommendations:
  142. //
  143. // 1) Consult compiler documentation; this comment is not kept in sync as
  144. // toolchains evolve.
  145. // 2) Verify your use has the intended effect. This often requires inspecting
  146. // the generated machine code.
  147. // 3) Prefer applying this attribute to individual variables. Avoid
  148. // applying it to types. This tends to localize the effect.
  149. #if defined(__clang__) || defined(__GNUC__)
  150. #define Y_ABSL_CACHELINE_ALIGNED __attribute__((aligned(Y_ABSL_CACHELINE_SIZE)))
  151. #elif defined(_MSC_VER)
  152. #define Y_ABSL_CACHELINE_ALIGNED __declspec(align(Y_ABSL_CACHELINE_SIZE))
  153. #else
  154. #define Y_ABSL_CACHELINE_ALIGNED
  155. #endif
  156. // Y_ABSL_PREDICT_TRUE, Y_ABSL_PREDICT_FALSE
  157. //
  158. // Enables the compiler to prioritize compilation using static analysis for
  159. // likely paths within a boolean branch.
  160. //
  161. // Example:
  162. //
  163. // if (Y_ABSL_PREDICT_TRUE(expression)) {
  164. // return result; // Faster if more likely
  165. // } else {
  166. // return 0;
  167. // }
  168. //
  169. // Compilers can use the information that a certain branch is not likely to be
  170. // taken (for instance, a CHECK failure) to optimize for the common case in
  171. // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
  172. //
  173. // Recommendation: Modern CPUs dynamically predict branch execution paths,
  174. // typically with accuracy greater than 97%. As a result, annotating every
  175. // branch in a codebase is likely counterproductive; however, annotating
  176. // specific branches that are both hot and consistently mispredicted is likely
  177. // to yield performance improvements.
  178. #if Y_ABSL_HAVE_BUILTIN(__builtin_expect) || \
  179. (defined(__GNUC__) && !defined(__clang__))
  180. #define Y_ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
  181. #define Y_ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
  182. #else
  183. #define Y_ABSL_PREDICT_FALSE(x) (x)
  184. #define Y_ABSL_PREDICT_TRUE(x) (x)
  185. #endif
  186. // `Y_ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL()` aborts the program in the fastest
  187. // possible way, with no attempt at logging. One use is to implement hardening
  188. // aborts with Y_ABSL_OPTION_HARDENED. Since this is an internal symbol, it
  189. // should not be used directly outside of Abseil.
  190. #if Y_ABSL_HAVE_BUILTIN(__builtin_trap) || \
  191. (defined(__GNUC__) && !defined(__clang__))
  192. #define Y_ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() __builtin_trap()
  193. #else
  194. #define Y_ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() abort()
  195. #endif
  196. // `Y_ABSL_INTERNAL_UNREACHABLE_IMPL()` is the platform specific directive to
  197. // indicate that a statement is unreachable, and to allow the compiler to
  198. // optimize accordingly. Clients should use `Y_ABSL_UNREACHABLE()`, which is
  199. // defined below.
  200. #if defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
  201. #define Y_ABSL_INTERNAL_UNREACHABLE_IMPL() std::unreachable()
  202. #elif defined(__GNUC__) || Y_ABSL_HAVE_BUILTIN(__builtin_unreachable)
  203. #define Y_ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_unreachable()
  204. #elif Y_ABSL_HAVE_BUILTIN(__builtin_assume)
  205. #define Y_ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_assume(false)
  206. #elif defined(_MSC_VER)
  207. #define Y_ABSL_INTERNAL_UNREACHABLE_IMPL() __assume(false)
  208. #else
  209. #define Y_ABSL_INTERNAL_UNREACHABLE_IMPL()
  210. #endif
  211. // `Y_ABSL_UNREACHABLE()` is an unreachable statement. A program which reaches
  212. // one has undefined behavior, and the compiler may optimize accordingly.
  213. #if Y_ABSL_OPTION_HARDENED == 1 && defined(NDEBUG)
  214. // Abort in hardened mode to avoid dangerous undefined behavior.
  215. #define Y_ABSL_UNREACHABLE() \
  216. do { \
  217. Y_ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \
  218. Y_ABSL_INTERNAL_UNREACHABLE_IMPL(); \
  219. } while (false)
  220. #else
  221. // The assert only fires in debug mode to aid in debugging.
  222. // When NDEBUG is defined, reaching Y_ABSL_UNREACHABLE() is undefined behavior.
  223. #define Y_ABSL_UNREACHABLE() \
  224. do { \
  225. /* NOLINTNEXTLINE: misc-static-assert */ \
  226. assert(false && "Y_ABSL_UNREACHABLE reached"); \
  227. Y_ABSL_INTERNAL_UNREACHABLE_IMPL(); \
  228. } while (false)
  229. #endif
  230. // Y_ABSL_ASSUME(cond)
  231. //
  232. // Informs the compiler that a condition is always true and that it can assume
  233. // it to be true for optimization purposes.
  234. //
  235. // WARNING: If the condition is false, the program can produce undefined and
  236. // potentially dangerous behavior.
  237. //
  238. // In !NDEBUG mode, the condition is checked with an assert().
  239. //
  240. // NOTE: The expression must not have side effects, as it may only be evaluated
  241. // in some compilation modes and not others. Some compilers may issue a warning
  242. // if the compiler cannot prove the expression has no side effects. For example,
  243. // the expression should not use a function call since the compiler cannot prove
  244. // that a function call does not have side effects.
  245. //
  246. // Example:
  247. //
  248. // int x = ...;
  249. // Y_ABSL_ASSUME(x >= 0);
  250. // // The compiler can optimize the division to a simple right shift using the
  251. // // assumption specified above.
  252. // int y = x / 16;
  253. //
  254. #if !defined(NDEBUG)
  255. #define Y_ABSL_ASSUME(cond) assert(cond)
  256. #elif Y_ABSL_HAVE_BUILTIN(__builtin_assume)
  257. #define Y_ABSL_ASSUME(cond) __builtin_assume(cond)
  258. #elif defined(_MSC_VER)
  259. #define Y_ABSL_ASSUME(cond) __assume(cond)
  260. #elif defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
  261. #define Y_ABSL_ASSUME(cond) \
  262. do { \
  263. if (!(cond)) std::unreachable(); \
  264. } while (false)
  265. #elif defined(__GNUC__) || Y_ABSL_HAVE_BUILTIN(__builtin_unreachable)
  266. #define Y_ABSL_ASSUME(cond) \
  267. do { \
  268. if (!(cond)) __builtin_unreachable(); \
  269. } while (false)
  270. #else
  271. #define Y_ABSL_ASSUME(cond) \
  272. do { \
  273. static_cast<void>(false && (cond)); \
  274. } while (false)
  275. #endif
  276. // Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
  277. // This macro forces small unique name on a static file level symbols like
  278. // static local variables or static functions. This is intended to be used in
  279. // macro definitions to optimize the cost of generated code. Do NOT use it on
  280. // symbols exported from translation unit since it may cause a link time
  281. // conflict.
  282. //
  283. // Example:
  284. //
  285. // #define MY_MACRO(txt)
  286. // namespace {
  287. // char VeryVeryLongVarName[] Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt;
  288. // const char* VeryVeryLongFuncName() Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  289. // const char* VeryVeryLongFuncName() { return txt; }
  290. // }
  291. //
  292. #if defined(__GNUC__)
  293. #define Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x
  294. #define Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x)
  295. #define Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
  296. asm(Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.y_absl.__COUNTER__))
  297. #else
  298. #define Y_ABSL_INTERNAL_UNIQUE_SMALL_NAME()
  299. #endif
  300. #endif // Y_ABSL_BASE_OPTIMIZATION_H_