optimization.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef ABSL_BASE_OPTIMIZATION_H_
  22. #define ABSL_BASE_OPTIMIZATION_H_
  23. #include <assert.h>
  24. #include "absl/base/config.h"
  25. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION
  26. //
  27. // Instructs the compiler to avoid optimizing tail-call recursion. This macro is
  28. // useful when you wish to preserve the existing function order within a stack
  29. // trace for logging, debugging, or profiling purposes.
  30. //
  31. // Example:
  32. //
  33. // int f() {
  34. // int result = g();
  35. // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
  36. // return result;
  37. // }
  38. #if defined(__pnacl__)
  39. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  40. #elif defined(__clang__)
  41. // Clang will not tail call given inline volatile assembly.
  42. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  43. #elif defined(__GNUC__)
  44. // GCC will not tail call given inline volatile assembly.
  45. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("")
  46. #elif defined(_MSC_VER)
  47. #include <intrin.h>
  48. // The __nop() intrinsic blocks the optimisation.
  49. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop()
  50. #else
  51. #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; }
  52. #endif
  53. // ABSL_CACHELINE_SIZE
  54. //
  55. // Explicitly defines the size of the L1 cache for purposes of alignment.
  56. // Setting the cacheline size allows you to specify that certain objects be
  57. // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations.
  58. // (See below.)
  59. //
  60. // NOTE: this macro should be replaced with the following C++17 features, when
  61. // those are generally available:
  62. //
  63. // * `std::hardware_constructive_interference_size`
  64. // * `std::hardware_destructive_interference_size`
  65. //
  66. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  67. // for more information.
  68. #if defined(__GNUC__)
  69. // Cache line alignment
  70. #if defined(__i386__) || defined(__x86_64__)
  71. #define ABSL_CACHELINE_SIZE 64
  72. #elif defined(__powerpc64__)
  73. #define ABSL_CACHELINE_SIZE 128
  74. #elif defined(__aarch64__)
  75. // We would need to read special register ctr_el0 to find out L1 dcache size.
  76. // This value is a good estimate based on a real aarch64 machine.
  77. #define ABSL_CACHELINE_SIZE 64
  78. #elif defined(__arm__)
  79. // Cache line sizes for ARM: These values are not strictly correct since
  80. // cache line sizes depend on implementations, not architectures. There
  81. // are even implementations with cache line sizes configurable at boot
  82. // time.
  83. #if defined(__ARM_ARCH_5T__)
  84. #define ABSL_CACHELINE_SIZE 32
  85. #elif defined(__ARM_ARCH_7A__)
  86. #define ABSL_CACHELINE_SIZE 64
  87. #endif
  88. #endif
  89. #ifndef ABSL_CACHELINE_SIZE
  90. // A reasonable default guess. Note that overestimates tend to waste more
  91. // space, while underestimates tend to waste more time.
  92. #define ABSL_CACHELINE_SIZE 64
  93. #endif
  94. // ABSL_CACHELINE_ALIGNED
  95. //
  96. // Indicates that the declared object be cache aligned using
  97. // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to
  98. // load a set of related objects in the L1 cache for performance improvements.
  99. // Cacheline aligning objects properly allows constructive memory sharing and
  100. // prevents destructive (or "false") memory sharing.
  101. //
  102. // NOTE: callers should replace uses of this macro with `alignas()` using
  103. // `std::hardware_constructive_interference_size` and/or
  104. // `std::hardware_destructive_interference_size` when C++17 becomes available to
  105. // them.
  106. //
  107. // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html
  108. // for more information.
  109. //
  110. // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__`
  111. // or `__declspec` attribute. For compilers where this is not known to work,
  112. // the macro expands to nothing.
  113. //
  114. // No further guarantees are made here. The result of applying the macro
  115. // to variables and types is always implementation-defined.
  116. //
  117. // WARNING: It is easy to use this attribute incorrectly, even to the point
  118. // of causing bugs that are difficult to diagnose, crash, etc. It does not
  119. // of itself guarantee that objects are aligned to a cache line.
  120. //
  121. // NOTE: Some compilers are picky about the locations of annotations such as
  122. // this attribute, so prefer to put it at the beginning of your declaration.
  123. // For example,
  124. //
  125. // ABSL_CACHELINE_ALIGNED static Foo* foo = ...
  126. //
  127. // class ABSL_CACHELINE_ALIGNED Bar { ...
  128. //
  129. // Recommendations:
  130. //
  131. // 1) Consult compiler documentation; this comment is not kept in sync as
  132. // toolchains evolve.
  133. // 2) Verify your use has the intended effect. This often requires inspecting
  134. // the generated machine code.
  135. // 3) Prefer applying this attribute to individual variables. Avoid
  136. // applying it to types. This tends to localize the effect.
  137. #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE)))
  138. #elif defined(_MSC_VER)
  139. #define ABSL_CACHELINE_SIZE 64
  140. #define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE))
  141. #else
  142. #define ABSL_CACHELINE_SIZE 64
  143. #define ABSL_CACHELINE_ALIGNED
  144. #endif
  145. // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE
  146. //
  147. // Enables the compiler to prioritize compilation using static analysis for
  148. // likely paths within a boolean branch.
  149. //
  150. // Example:
  151. //
  152. // if (ABSL_PREDICT_TRUE(expression)) {
  153. // return result; // Faster if more likely
  154. // } else {
  155. // return 0;
  156. // }
  157. //
  158. // Compilers can use the information that a certain branch is not likely to be
  159. // taken (for instance, a CHECK failure) to optimize for the common case in
  160. // the absence of better information (ie. compiling gcc with `-fprofile-arcs`).
  161. //
  162. // Recommendation: Modern CPUs dynamically predict branch execution paths,
  163. // typically with accuracy greater than 97%. As a result, annotating every
  164. // branch in a codebase is likely counterproductive; however, annotating
  165. // specific branches that are both hot and consistently mispredicted is likely
  166. // to yield performance improvements.
  167. #if ABSL_HAVE_BUILTIN(__builtin_expect) || \
  168. (defined(__GNUC__) && !defined(__clang__))
  169. #define ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false))
  170. #define ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true))
  171. #else
  172. #define ABSL_PREDICT_FALSE(x) (x)
  173. #define ABSL_PREDICT_TRUE(x) (x)
  174. #endif
  175. // ABSL_INTERNAL_ASSUME(cond)
  176. // Informs the compiler that a condition is always true and that it can assume
  177. // it to be true for optimization purposes. The call has undefined behavior if
  178. // the condition is false.
  179. // In !NDEBUG mode, the condition is checked with an assert().
  180. // NOTE: The expression must not have side effects, as it will only be evaluated
  181. // in some compilation modes and not others.
  182. //
  183. // Example:
  184. //
  185. // int x = ...;
  186. // ABSL_INTERNAL_ASSUME(x >= 0);
  187. // // The compiler can optimize the division to a simple right shift using the
  188. // // assumption specified above.
  189. // int y = x / 16;
  190. //
  191. #if !defined(NDEBUG)
  192. #define ABSL_INTERNAL_ASSUME(cond) assert(cond)
  193. #elif ABSL_HAVE_BUILTIN(__builtin_assume)
  194. #define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond)
  195. #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
  196. #define ABSL_INTERNAL_ASSUME(cond) \
  197. do { \
  198. if (!(cond)) __builtin_unreachable(); \
  199. } while (0)
  200. #elif defined(_MSC_VER)
  201. #define ABSL_INTERNAL_ASSUME(cond) __assume(cond)
  202. #else
  203. #define ABSL_INTERNAL_ASSUME(cond) \
  204. do { \
  205. static_cast<void>(false && (cond)); \
  206. } while (0)
  207. #endif
  208. // ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
  209. // This macro forces small unique name on a static file level symbols like
  210. // static local variables or static functions. This is intended to be used in
  211. // macro definitions to optimize the cost of generated code. Do NOT use it on
  212. // symbols exported from translation unit since it may cause a link time
  213. // conflict.
  214. //
  215. // Example:
  216. //
  217. // #define MY_MACRO(txt)
  218. // namespace {
  219. // char VeryVeryLongVarName[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt;
  220. // const char* VeryVeryLongFuncName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  221. // const char* VeryVeryLongFuncName() { return txt; }
  222. // }
  223. //
  224. #if defined(__GNUC__)
  225. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x
  226. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x)
  227. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() \
  228. asm(ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.absl.__COUNTER__))
  229. #else
  230. #define ABSL_INTERNAL_UNIQUE_SMALL_NAME()
  231. #endif
  232. #endif // ABSL_BASE_OPTIMIZATION_H_