1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_PLATFORM_MACROS_H_ 17 #define TENSORFLOW_CORE_PLATFORM_MACROS_H_ 18 19 // Compiler attributes 20 #if (defined(__GNUC__) || defined(__APPLE__)) && !defined(SWIG) 21 // Compiler supports GCC-style attributes 22 #define TF_ATTRIBUTE_NORETURN __attribute__((noreturn)) 23 #define TF_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 24 #define TF_ATTRIBUTE_NOINLINE __attribute__((noinline)) 25 #define TF_ATTRIBUTE_UNUSED __attribute__((unused)) 26 #define TF_ATTRIBUTE_COLD __attribute__((cold)) 27 #define TF_ATTRIBUTE_WEAK __attribute__((weak)) 28 #define TF_PACKED __attribute__((packed)) 29 #define TF_MUST_USE_RESULT __attribute__((warn_unused_result)) 30 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) \ 31 __attribute__((__format__(__printf__, string_index, first_to_check))) 32 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) \ 33 __attribute__((__format__(__scanf__, string_index, first_to_check))) 34 #elif defined(_MSC_VER) 35 // Non-GCC equivalents 36 #define TF_ATTRIBUTE_NORETURN __declspec(noreturn) 37 #define TF_ATTRIBUTE_ALWAYS_INLINE __forceinline 38 #define TF_ATTRIBUTE_NOINLINE 39 #define TF_ATTRIBUTE_UNUSED 40 #define TF_ATTRIBUTE_COLD 41 #define TF_ATTRIBUTE_WEAK 42 #define TF_MUST_USE_RESULT 43 #define TF_PACKED 44 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) 45 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) 46 #else 47 // Non-GCC equivalents 48 #define TF_ATTRIBUTE_NORETURN 49 #define TF_ATTRIBUTE_ALWAYS_INLINE 50 #define TF_ATTRIBUTE_NOINLINE 51 #define TF_ATTRIBUTE_UNUSED 52 #define TF_ATTRIBUTE_COLD 53 #define TF_ATTRIBUTE_WEAK 54 #define TF_MUST_USE_RESULT 55 #define TF_PACKED 56 #define TF_PRINTF_ATTRIBUTE(string_index, first_to_check) 57 #define TF_SCANF_ATTRIBUTE(string_index, first_to_check) 58 #endif 59 60 // Control visiblity outside .so 61 #if defined(_WIN32) 62 #ifdef TF_COMPILE_LIBRARY 63 #define TF_EXPORT __declspec(dllexport) 64 #else 65 #define TF_EXPORT __declspec(dllimport) 66 #endif // TF_COMPILE_LIBRARY 67 #else 68 #define TF_EXPORT __attribute__((visibility("default"))) 69 #endif // _WIN32 70 71 #ifdef __has_builtin 72 #define TF_HAS_BUILTIN(x) __has_builtin(x) 73 #else 74 #define TF_HAS_BUILTIN(x) 0 75 #endif 76 77 // Compilers can be told that a certain branch is not likely to be taken 78 // (for instance, a CHECK failure), and use that information in static 79 // analysis. Giving it this information can help it optimize for the 80 // common case in the absence of better information (ie. 81 // -fprofile-arcs). 82 // 83 // We need to disable this for GPU builds, though, since nvcc8 and older 84 // don't recognize `__builtin_expect` as a builtin, and fail compilation. 85 #if (!defined(__NVCC__)) && \ 86 (TF_HAS_BUILTIN(__builtin_expect) || (defined(__GNUC__) && __GNUC__ >= 3)) 87 #define TF_PREDICT_FALSE(x) (__builtin_expect(x, 0)) 88 #define TF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 89 #else 90 #define TF_PREDICT_FALSE(x) (x) 91 #define TF_PREDICT_TRUE(x) (x) 92 #endif 93 94 // A macro to disallow the copy constructor and operator= functions 95 // This is usually placed in the private: declarations for a class. 96 #define TF_DISALLOW_COPY_AND_ASSIGN(TypeName) \ 97 TypeName(const TypeName&) = delete; \ 98 void operator=(const TypeName&) = delete 99 100 // The TF_ARRAYSIZE(arr) macro returns the # of elements in an array arr. 101 // 102 // The expression TF_ARRAYSIZE(a) is a compile-time constant of type 103 // size_t. 104 #define TF_ARRAYSIZE(a) \ 105 ((sizeof(a) / sizeof(*(a))) / \ 106 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 107 108 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ 109 (defined(_MSC_VER) && _MSC_VER >= 1900) 110 // Define this to 1 if the code is compiled in C++11 mode; leave it 111 // undefined otherwise. Do NOT define it to 0 -- that causes 112 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. 113 #define LANG_CXX11 1 114 #endif 115 116 #if defined(__clang__) && defined(LANG_CXX11) && defined(__has_warning) 117 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") 118 #define TF_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT 119 #endif 120 #endif 121 122 #ifndef TF_FALLTHROUGH_INTENDED 123 #define TF_FALLTHROUGH_INTENDED \ 124 do { \ 125 } while (0) 126 #endif 127 128 #endif // TENSORFLOW_CORE_PLATFORM_MACROS_H_ 129