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_TSL_PLATFORM_MACROS_H_ |
17 | #define TENSORFLOW_TSL_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 visibility 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 | // C++11-style attributes (N2761) |
78 | #if defined(__has_cpp_attribute) |
79 | // Safely checks if an attribute is supported. Equivalent to |
80 | // ABSL_HAVE_CPP_ATTRIBUTE. |
81 | #define TF_HAS_CPP_ATTRIBUTE(n) __has_cpp_attribute(n) |
82 | #else |
83 | #define TF_HAS_CPP_ATTRIBUTE(n) 0 |
84 | #endif |
85 | |
86 | // [[clang::annotate("x")]] allows attaching custom strings (e.g. "x") to |
87 | // declarations (variables, functions, fields, etc.) for use by tools. They are |
88 | // represented in the Clang AST (as AnnotateAttr nodes) and in LLVM IR, but not |
89 | // in final output. |
90 | #if TF_HAS_CPP_ATTRIBUTE(clang::annotate) |
91 | #define TF_ATTRIBUTE_ANNOTATE(str) [[clang::annotate(str)]] |
92 | #else |
93 | #define TF_ATTRIBUTE_ANNOTATE(str) |
94 | #endif |
95 | |
96 | // A variable declaration annotated with the `TF_CONST_INIT` attribute will |
97 | // not compile (on supported platforms) unless the variable has a constant |
98 | // initializer. |
99 | #if TF_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization) |
100 | #define TF_CONST_INIT [[clang::require_constant_initialization]] |
101 | #else |
102 | #define TF_CONST_INIT |
103 | #endif |
104 | |
105 | // Compilers can be told that a certain branch is not likely to be taken |
106 | // (for instance, a CHECK failure), and use that information in static |
107 | // analysis. Giving it this information can help it optimize for the |
108 | // common case in the absence of better information (ie. |
109 | // -fprofile-arcs). |
110 | #if TF_HAS_BUILTIN(__builtin_expect) || (defined(__GNUC__) && __GNUC__ >= 3) |
111 | #define TF_PREDICT_FALSE(x) (__builtin_expect(x, 0)) |
112 | #define TF_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) |
113 | #else |
114 | #define TF_PREDICT_FALSE(x) (x) |
115 | #define TF_PREDICT_TRUE(x) (x) |
116 | #endif |
117 | |
118 | // A macro to disallow the copy constructor and operator= functions |
119 | // This is usually placed in the private: declarations for a class. |
120 | #define TF_DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
121 | TypeName(const TypeName&) = delete; \ |
122 | void operator=(const TypeName&) = delete |
123 | |
124 | // The TF_ARRAYSIZE(arr) macro returns the # of elements in an array arr. |
125 | // |
126 | // The expression TF_ARRAYSIZE(a) is a compile-time constant of type |
127 | // size_t. |
128 | #define TF_ARRAYSIZE(a) \ |
129 | ((sizeof(a) / sizeof(*(a))) / \ |
130 | static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
131 | |
132 | #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \ |
133 | (defined(_MSC_VER) && _MSC_VER >= 1900) |
134 | // Define this to 1 if the code is compiled in C++11 mode; leave it |
135 | // undefined otherwise. Do NOT define it to 0 -- that causes |
136 | // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'. |
137 | #define LANG_CXX11 1 |
138 | #endif |
139 | |
140 | #if defined(__clang__) && defined(LANG_CXX11) && defined(__has_warning) |
141 | #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") |
142 | #define TF_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT |
143 | #endif |
144 | #endif |
145 | |
146 | #ifndef TF_FALLTHROUGH_INTENDED |
147 | #define TF_FALLTHROUGH_INTENDED \ |
148 | do { \ |
149 | } while (0) |
150 | #endif |
151 | |
152 | namespace tsl { |
153 | namespace internal { |
154 | template <typename T> |
155 | void remove_unused_variable_compiler_warning(const T&){}; |
156 | } // namespace internal |
157 | } // namespace tsl |
158 | #define TF_UNUSED_VARIABLE(x) \ |
159 | tensorflow::internal::remove_unused_variable_compiler_warning(x) |
160 | |
161 | #endif // TENSORFLOW_TSL_PLATFORM_MACROS_H_ |
162 | |