1313
1414#pragma once
1515
16- // -----------------------------------------------------------------------------
17- // Compiler version checks
18- // -----------------------------------------------------------------------------
16+ /*
17+ * Compiler support checks. Follows the logic used by pytorch/c10/util/C++17.h
18+ * but may support older versions.
19+ */
1920
20- // GCC version check
21+ // https://gcc.gnu.org/projects/cxx-status.html#cxx17
2122#if !defined(__clang__ ) && !defined(_MSC_VER ) && defined(__GNUC__ ) && \
2223 __GNUC__ < 7
2324#error \
2425 "You're trying to build ExecuTorch with a too old version of GCC. We need GCC 7 or later."
2526#endif
2627
27- // Clang version check
28+ // https://clang.llvm.org/cxx_status.html#cxx17
2829#if defined(__clang__ ) && __clang_major__ < 5
2930#error \
3031 "You're trying to build ExecuTorch with a too old version of Clang. We need Clang 5 or later."
4243 "Macro clash with min and max -- define NOMINMAX when compiling your program on Windows"
4344#endif
4445
45- // -----------------------------------------------------------------------------
46- // Attribute macros
47- // -----------------------------------------------------------------------------
46+ /*
47+ * Define annotations aliasing C++ declaration attributes.
48+ * See all C++ declaration attributes here:
49+ * https://en.cppreference.com/w/cpp/language/attributes
50+ *
51+ * Note that ExecuTorch supports a lower C++ standard version than all standard
52+ * attributes. Therefore, some annotations are defined using their Clang/GNU
53+ * counterparts.
54+ *
55+ * GNU attribute definitions:
56+ * https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
57+ */
4858
49- // [[noreturn]]
5059#define ET_NORETURN [[noreturn]]
5160
52- // [[deprecated]]
5361#define ET_DEPRECATED [[deprecated]]
5462#define ET_EXPERIMENTAL \
5563 [[deprecated("This API is experimental and may change without notice.")]]
5664
57- // [[fallthrough]]
5865#if defined(__clang__ ) || (defined(__GNUC__ ) && __GNUC__ >= 7 )
5966#define ET_FALLTHROUGH [[fallthrough]]
6067#else
6168#define ET_FALLTHROUGH
6269#endif
6370
64- // [[nodiscard]]
6571#define ET_NODISCARD [[nodiscard]]
6672
67- // [[maybe_unused]]
6873#define ET_UNUSED [[maybe_unused]]
6974
7075// Inline/NoInline
7883#define ET_INLINE_ATTRIBUTE __attribute__((always_inline))
7984#endif
8085
81- // Unreachable
82- #if defined(_MSC_VER )
83- #define ET_UNREACHABLE () __assume(0)
84- #else
86+ #if defined(__GNUC__ )
87+
8588#define ET_UNREACHABLE () __builtin_unreachable()
86- #endif
8789
88- // Likely/Unlikely
90+ #elif defined(_MSC_VER )
91+
92+ #define ET_UNREACHABLE () __assume(0)
93+
94+ #else // defined(__GNUC__)
95+
96+ #define ET_UNREACHABLE () \
97+ while (1) \
98+ ;
99+
100+ #endif // defined(__GNUC__)
101+
102+ // UNLIKELY Macro
103+ // example
104+ // if ET_UNLIKELY(a > 10 && b < 5) {
105+ // do something
106+ // }
89107#if (__cplusplus ) >= 202002L
90108#define ET_LIKELY (expr ) (expr) [[likely]]
91109#define ET_UNLIKELY (expr ) (expr) [[unlikely]]
94112#define ET_UNLIKELY (expr ) (expr)
95113#endif
96114
97- // Weak linkage
98- #if defined(_MSC_VER )
115+ /// Define a C symbol with weak linkage.
116+ #ifdef _MSC_VER
117+ // There currently doesn't seem to be a great way to do this in Windows and
118+ // given that weak linkage is not really critical on Windows, we'll just leave
119+ // it as a stub.
99120// No weak linkage in MSVC
100121#define ET_WEAK
101122#else
102123#define ET_WEAK __attribute__((weak))
103124#endif
104125
105- // Printf-like format checking
106- #if defined(_MSC_VER )
126+ /**
127+ * Annotation marking a function as printf-like, providing compiler support
128+ * for format string argument checking.
129+ */
130+ #ifdef _MSC_VER
107131#include <sal.h>
108132#define ET_PRINTFLIKE (_string_index , _va_index ) _Printf_format_string_
109133#else
116140// -----------------------------------------------------------------------------
117141
118142#ifndef __has_builtin
119- #define __has_builtin (x ) 0
143+ #define __has_builtin (x ) (0)
120144#endif
121145
122146#if __has_builtin (__builtin_strrchr )
147+ /// Name of the source file without a directory string.
123148#define ET_SHORT_FILENAME (__builtin_strrchr("/" __FILE__, '/') + 1)
124149#else
125150#define ET_SHORT_FILENAME __FILE__
126151#endif
127152
128153#if __has_builtin (__builtin_LINE )
154+ /// Current line as an integer.
129155#define ET_LINE __builtin_LINE()
130156#else
131157#define ET_LINE __LINE__
132- #endif
158+ #endif // __has_builtin(__builtin_LINE)
133159
134160#if __has_builtin (__builtin_FUNCTION )
161+ /// Name of the current function as a const char[].
135162#define ET_FUNCTION __builtin_FUNCTION()
136163#else
137164#define ET_FUNCTION __FUNCTION__
138- #endif
139-
140- // -----------------------------------------------------------------------------
141- // Format specifiers for size_t/ssize_t
142- // -----------------------------------------------------------------------------
165+ #endif // __has_builtin(__builtin_FUNCTION)
143166
167+ // As of G3 RJ-2024.3 toolchain, zu format specifier is not supported for Xtensa
144168#if defined(__XTENSA__ )
145169#define ET_PRIsize_t "lu"
146170#define ET_PRIssize_t "ld"
149173#define ET_PRIssize_t "zd"
150174#endif
151175
152- // -----------------------------------------------------------------------------
153- // GNU statement expressions
154- // -----------------------------------------------------------------------------
176+ // Whether the compiler supports GNU statement expressions.
177+ // https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
155178
156179#ifndef ET_HAVE_GNU_STATEMENT_EXPRESSIONS
157180#if (defined(__GNUC__ ) && __GNUC__ >= 3 ) || defined(__clang__ )
161184#endif
162185#endif
163186
164- // -----------------------------------------------------------------------------
165- // ssize_t definition
166- // -----------------------------------------------------------------------------
167-
168187#ifndef _MSC_VER
169188#include <sys/types.h>
170189#else
171190#include <stddef.h>
172191using ssize_t = ptrdiff_t ;
173192#endif
174193
175- // -----------------------------------------------------------------------------
176- // Exception support
177- // -----------------------------------------------------------------------------
178-
179194#ifdef __EXCEPTIONS
180195#define ET_HAS_EXCEPTIONS 1
181196#elif defined(_MSC_VER ) && defined(_HAS_EXCEPTIONS ) && _HAS_EXCEPTIONS
@@ -184,9 +199,8 @@ using ssize_t = ptrdiff_t;
184199#define ET_HAS_EXCEPTIONS 0
185200#endif
186201
187- // -----------------------------------------------------------------------------
188- // Deprecated legacy macros (to be removed)
189- // -----------------------------------------------------------------------------
202+ // DEPRECATED: Use the non-underscore-prefixed versions instead.
203+ // TODO(T199005537): Remove these once all users have stopped using them.
190204
191205#define __ET_DEPRECATED ET_DEPRECATED
192206#define __ET_FALLTHROUGH ET_FALLTHROUGH
0 commit comments