|
||||
File indexing completed on 2024-11-15 09:00:53
0001 // 0002 // Copyright 2017 The Abseil Authors. 0003 // 0004 // Licensed under the Apache License, Version 2.0 (the "License"); 0005 // you may not use this file except in compliance with the License. 0006 // You may obtain a copy of the License at 0007 // 0008 // https://www.apache.org/licenses/LICENSE-2.0 0009 // 0010 // Unless required by applicable law or agreed to in writing, software 0011 // distributed under the License is distributed on an "AS IS" BASIS, 0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 // See the License for the specific language governing permissions and 0014 // limitations under the License. 0015 // 0016 // ----------------------------------------------------------------------------- 0017 // File: optimization.h 0018 // ----------------------------------------------------------------------------- 0019 // 0020 // This header file defines portable macros for performance optimization. 0021 0022 #ifndef ABSL_BASE_OPTIMIZATION_H_ 0023 #define ABSL_BASE_OPTIMIZATION_H_ 0024 0025 #include <assert.h> 0026 0027 #include "absl/base/config.h" 0028 #include "absl/base/options.h" 0029 0030 // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION 0031 // 0032 // Instructs the compiler to avoid optimizing tail-call recursion. This macro is 0033 // useful when you wish to preserve the existing function order within a stack 0034 // trace for logging, debugging, or profiling purposes. 0035 // 0036 // Example: 0037 // 0038 // int f() { 0039 // int result = g(); 0040 // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION(); 0041 // return result; 0042 // } 0043 #if defined(__pnacl__) 0044 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; } 0045 #elif defined(__clang__) 0046 // Clang will not tail call given inline volatile assembly. 0047 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("") 0048 #elif defined(__GNUC__) 0049 // GCC will not tail call given inline volatile assembly. 0050 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __asm__ __volatile__("") 0051 #elif defined(_MSC_VER) 0052 #include <intrin.h> 0053 // The __nop() intrinsic blocks the optimisation. 0054 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() __nop() 0055 #else 0056 #define ABSL_BLOCK_TAIL_CALL_OPTIMIZATION() if (volatile int x = 0) { (void)x; } 0057 #endif 0058 0059 // ABSL_CACHELINE_SIZE 0060 // 0061 // Explicitly defines the size of the L1 cache for purposes of alignment. 0062 // Setting the cacheline size allows you to specify that certain objects be 0063 // aligned on a cacheline boundary with `ABSL_CACHELINE_ALIGNED` declarations. 0064 // (See below.) 0065 // 0066 // NOTE: this macro should be replaced with the following C++17 features, when 0067 // those are generally available: 0068 // 0069 // * `std::hardware_constructive_interference_size` 0070 // * `std::hardware_destructive_interference_size` 0071 // 0072 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html 0073 // for more information. 0074 #if defined(__GNUC__) 0075 // Cache line alignment 0076 #if defined(__i386__) || defined(__x86_64__) 0077 #define ABSL_CACHELINE_SIZE 64 0078 #elif defined(__powerpc64__) 0079 #define ABSL_CACHELINE_SIZE 128 0080 #elif defined(__aarch64__) 0081 // We would need to read special register ctr_el0 to find out L1 dcache size. 0082 // This value is a good estimate based on a real aarch64 machine. 0083 #define ABSL_CACHELINE_SIZE 64 0084 #elif defined(__arm__) 0085 // Cache line sizes for ARM: These values are not strictly correct since 0086 // cache line sizes depend on implementations, not architectures. There 0087 // are even implementations with cache line sizes configurable at boot 0088 // time. 0089 #if defined(__ARM_ARCH_5T__) 0090 #define ABSL_CACHELINE_SIZE 32 0091 #elif defined(__ARM_ARCH_7A__) 0092 #define ABSL_CACHELINE_SIZE 64 0093 #endif 0094 #endif 0095 #endif 0096 0097 #ifndef ABSL_CACHELINE_SIZE 0098 // A reasonable default guess. Note that overestimates tend to waste more 0099 // space, while underestimates tend to waste more time. 0100 #define ABSL_CACHELINE_SIZE 64 0101 #endif 0102 0103 // ABSL_CACHELINE_ALIGNED 0104 // 0105 // Indicates that the declared object be cache aligned using 0106 // `ABSL_CACHELINE_SIZE` (see above). Cacheline aligning objects allows you to 0107 // load a set of related objects in the L1 cache for performance improvements. 0108 // Cacheline aligning objects properly allows constructive memory sharing and 0109 // prevents destructive (or "false") memory sharing. 0110 // 0111 // NOTE: callers should replace uses of this macro with `alignas()` using 0112 // `std::hardware_constructive_interference_size` and/or 0113 // `std::hardware_destructive_interference_size` when C++17 becomes available to 0114 // them. 0115 // 0116 // See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0154r1.html 0117 // for more information. 0118 // 0119 // On some compilers, `ABSL_CACHELINE_ALIGNED` expands to an `__attribute__` 0120 // or `__declspec` attribute. For compilers where this is not known to work, 0121 // the macro expands to nothing. 0122 // 0123 // No further guarantees are made here. The result of applying the macro 0124 // to variables and types is always implementation-defined. 0125 // 0126 // WARNING: It is easy to use this attribute incorrectly, even to the point 0127 // of causing bugs that are difficult to diagnose, crash, etc. It does not 0128 // of itself guarantee that objects are aligned to a cache line. 0129 // 0130 // NOTE: Some compilers are picky about the locations of annotations such as 0131 // this attribute, so prefer to put it at the beginning of your declaration. 0132 // For example, 0133 // 0134 // ABSL_CACHELINE_ALIGNED static Foo* foo = ... 0135 // 0136 // class ABSL_CACHELINE_ALIGNED Bar { ... 0137 // 0138 // Recommendations: 0139 // 0140 // 1) Consult compiler documentation; this comment is not kept in sync as 0141 // toolchains evolve. 0142 // 2) Verify your use has the intended effect. This often requires inspecting 0143 // the generated machine code. 0144 // 3) Prefer applying this attribute to individual variables. Avoid 0145 // applying it to types. This tends to localize the effect. 0146 #if defined(__clang__) || defined(__GNUC__) 0147 #define ABSL_CACHELINE_ALIGNED __attribute__((aligned(ABSL_CACHELINE_SIZE))) 0148 #elif defined(_MSC_VER) 0149 #define ABSL_CACHELINE_ALIGNED __declspec(align(ABSL_CACHELINE_SIZE)) 0150 #else 0151 #define ABSL_CACHELINE_ALIGNED 0152 #endif 0153 0154 // ABSL_PREDICT_TRUE, ABSL_PREDICT_FALSE 0155 // 0156 // Enables the compiler to prioritize compilation using static analysis for 0157 // likely paths within a boolean branch. 0158 // 0159 // Example: 0160 // 0161 // if (ABSL_PREDICT_TRUE(expression)) { 0162 // return result; // Faster if more likely 0163 // } else { 0164 // return 0; 0165 // } 0166 // 0167 // Compilers can use the information that a certain branch is not likely to be 0168 // taken (for instance, a CHECK failure) to optimize for the common case in 0169 // the absence of better information (ie. compiling gcc with `-fprofile-arcs`). 0170 // 0171 // Recommendation: Modern CPUs dynamically predict branch execution paths, 0172 // typically with accuracy greater than 97%. As a result, annotating every 0173 // branch in a codebase is likely counterproductive; however, annotating 0174 // specific branches that are both hot and consistently mispredicted is likely 0175 // to yield performance improvements. 0176 #if ABSL_HAVE_BUILTIN(__builtin_expect) || \ 0177 (defined(__GNUC__) && !defined(__clang__)) 0178 #define ABSL_PREDICT_FALSE(x) (__builtin_expect(false || (x), false)) 0179 #define ABSL_PREDICT_TRUE(x) (__builtin_expect(false || (x), true)) 0180 #else 0181 #define ABSL_PREDICT_FALSE(x) (x) 0182 #define ABSL_PREDICT_TRUE(x) (x) 0183 #endif 0184 0185 // `ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL()` aborts the program in the fastest 0186 // possible way, with no attempt at logging. One use is to implement hardening 0187 // aborts with ABSL_OPTION_HARDENED. Since this is an internal symbol, it 0188 // should not be used directly outside of Abseil. 0189 #if ABSL_HAVE_BUILTIN(__builtin_trap) || \ 0190 (defined(__GNUC__) && !defined(__clang__)) 0191 #define ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() __builtin_trap() 0192 #else 0193 #define ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL() abort() 0194 #endif 0195 0196 // `ABSL_INTERNAL_UNREACHABLE_IMPL()` is the platform specific directive to 0197 // indicate that a statement is unreachable, and to allow the compiler to 0198 // optimize accordingly. Clients should use `ABSL_UNREACHABLE()`, which is 0199 // defined below. 0200 #if defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L 0201 #define ABSL_INTERNAL_UNREACHABLE_IMPL() std::unreachable() 0202 #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable) 0203 #define ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_unreachable() 0204 #elif ABSL_HAVE_BUILTIN(__builtin_assume) 0205 #define ABSL_INTERNAL_UNREACHABLE_IMPL() __builtin_assume(false) 0206 #elif defined(_MSC_VER) 0207 #define ABSL_INTERNAL_UNREACHABLE_IMPL() __assume(false) 0208 #else 0209 #define ABSL_INTERNAL_UNREACHABLE_IMPL() 0210 #endif 0211 0212 // `ABSL_UNREACHABLE()` is an unreachable statement. A program which reaches 0213 // one has undefined behavior, and the compiler may optimize accordingly. 0214 #if ABSL_OPTION_HARDENED == 1 && defined(NDEBUG) 0215 // Abort in hardened mode to avoid dangerous undefined behavior. 0216 #define ABSL_UNREACHABLE() \ 0217 do { \ 0218 ABSL_INTERNAL_IMMEDIATE_ABORT_IMPL(); \ 0219 ABSL_INTERNAL_UNREACHABLE_IMPL(); \ 0220 } while (false) 0221 #else 0222 // The assert only fires in debug mode to aid in debugging. 0223 // When NDEBUG is defined, reaching ABSL_UNREACHABLE() is undefined behavior. 0224 #define ABSL_UNREACHABLE() \ 0225 do { \ 0226 /* NOLINTNEXTLINE: misc-static-assert */ \ 0227 assert(false && "ABSL_UNREACHABLE reached"); \ 0228 ABSL_INTERNAL_UNREACHABLE_IMPL(); \ 0229 } while (false) 0230 #endif 0231 0232 // ABSL_ASSUME(cond) 0233 // 0234 // Informs the compiler that a condition is always true and that it can assume 0235 // it to be true for optimization purposes. 0236 // 0237 // WARNING: If the condition is false, the program can produce undefined and 0238 // potentially dangerous behavior. 0239 // 0240 // In !NDEBUG mode, the condition is checked with an assert(). 0241 // 0242 // NOTE: The expression must not have side effects, as it may only be evaluated 0243 // in some compilation modes and not others. Some compilers may issue a warning 0244 // if the compiler cannot prove the expression has no side effects. For example, 0245 // the expression should not use a function call since the compiler cannot prove 0246 // that a function call does not have side effects. 0247 // 0248 // Example: 0249 // 0250 // int x = ...; 0251 // ABSL_ASSUME(x >= 0); 0252 // // The compiler can optimize the division to a simple right shift using the 0253 // // assumption specified above. 0254 // int y = x / 16; 0255 // 0256 #if !defined(NDEBUG) 0257 #define ABSL_ASSUME(cond) assert(cond) 0258 #elif ABSL_HAVE_BUILTIN(__builtin_assume) 0259 #define ABSL_ASSUME(cond) __builtin_assume(cond) 0260 #elif defined(_MSC_VER) 0261 #define ABSL_ASSUME(cond) __assume(cond) 0262 #elif defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L 0263 #define ABSL_ASSUME(cond) \ 0264 do { \ 0265 if (!(cond)) std::unreachable(); \ 0266 } while (false) 0267 #elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable) 0268 #define ABSL_ASSUME(cond) \ 0269 do { \ 0270 if (!(cond)) __builtin_unreachable(); \ 0271 } while (false) 0272 #else 0273 #define ABSL_ASSUME(cond) \ 0274 do { \ 0275 static_cast<void>(false && (cond)); \ 0276 } while (false) 0277 #endif 0278 0279 // ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond) 0280 // This macro forces small unique name on a static file level symbols like 0281 // static local variables or static functions. This is intended to be used in 0282 // macro definitions to optimize the cost of generated code. Do NOT use it on 0283 // symbols exported from translation unit since it may cause a link time 0284 // conflict. 0285 // 0286 // Example: 0287 // 0288 // #define MY_MACRO(txt) 0289 // namespace { 0290 // char VeryVeryLongVarName[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = txt; 0291 // const char* VeryVeryLongFuncName() ABSL_INTERNAL_UNIQUE_SMALL_NAME(); 0292 // const char* VeryVeryLongFuncName() { return txt; } 0293 // } 0294 // 0295 0296 #if defined(__GNUC__) 0297 #define ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) #x 0298 #define ABSL_INTERNAL_UNIQUE_SMALL_NAME1(x) ABSL_INTERNAL_UNIQUE_SMALL_NAME2(x) 0299 #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() \ 0300 asm(ABSL_INTERNAL_UNIQUE_SMALL_NAME1(.absl.__COUNTER__)) 0301 #else 0302 #define ABSL_INTERNAL_UNIQUE_SMALL_NAME() 0303 #endif 0304 0305 #endif // ABSL_BASE_OPTIMIZATION_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |