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