Skip to content

Commit 758148a

Browse files
committed
add a bunch of msvc variants
1 parent bf7d755 commit 758148a

File tree

1 file changed

+90
-80
lines changed

1 file changed

+90
-80
lines changed

runtime/platform/compiler.h

Lines changed: 90 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,140 +13,139 @@
1313

1414
#pragma once
1515

16-
/*
17-
* Compiler support checks. Follows the logic used by pytorch/c10/util/C++17.h
18-
* but may support older versions.
19-
*/
16+
// -----------------------------------------------------------------------------
17+
// Compiler version checks
18+
// -----------------------------------------------------------------------------
2019

21-
// https://gcc.gnu.org/projects/cxx-status.html#cxx17
22-
#if !defined(__clang__) && !defined(_MSC_VER) && defined(__GNUC__) && \
23-
__GNUC__ < 7
24-
#error \
25-
"You're trying to build ExecuTorch with a too old version of GCC. We need GCC 7 or later."
20+
// GCC version check
21+
#if !defined(__clang__) && !defined(_MSC_VER) && defined(__GNUC__) && __GNUC__ < 7
22+
#error "You're trying to build ExecuTorch with a too old version of GCC. We need GCC 7 or later."
2623
#endif
2724

28-
// https://clang.llvm.org/cxx_status.html#cxx17
25+
// Clang version check
2926
#if defined(__clang__) && __clang_major__ < 5
30-
#error \
31-
"You're trying to build ExecuTorch with a too old version of Clang. We need Clang 5 or later."
27+
#error "You're trying to build ExecuTorch with a too old version of Clang. We need Clang 5 or later."
3228
#endif
3329

30+
// C++17 check
3431
#if (defined(_MSC_VER) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)) || \
3532
(!defined(_MSC_VER) && __cplusplus < 201703L)
3633
#error "You need C++17 to compile ExecuTorch"
3734
#endif
3835

36+
// Windows min/max macro clash
3937
#if defined(_MSC_VER) && (defined(min) || defined(max))
40-
#error \
41-
"Macro clash with min and max -- define NOMINMAX when compiling your program on Windows"
38+
#error "Macro clash with min and max -- define NOMINMAX when compiling your program on Windows"
4239
#endif
4340

44-
/*
45-
* Define annotations aliasing C++ declaration attributes.
46-
* See all C++ declaration attributes here:
47-
* https://en.cppreference.com/w/cpp/language/attributes
48-
*
49-
* Note that ExecuTorch supports a lower C++ standard version than all standard
50-
* attributes. Therefore, some annotations are defined using their Clang/GNU
51-
* counterparts.
52-
*
53-
* GNU attribute definitions:
54-
* https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
55-
*/
41+
// -----------------------------------------------------------------------------
42+
// Attribute macros
43+
// -----------------------------------------------------------------------------
5644

45+
// [[noreturn]]
5746
#define ET_NORETURN [[noreturn]]
47+
48+
// [[deprecated]]
49+
#define ET_DEPRECATED [[deprecated]]
50+
#define ET_EXPERIMENTAL [[deprecated("This API is experimental and may change without notice.")]]
51+
52+
// [[fallthrough]]
53+
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 7)
54+
#define ET_FALLTHROUGH [[fallthrough]]
55+
#else
56+
#define ET_FALLTHROUGH
57+
#endif
58+
59+
// [[nodiscard]]
60+
#define ET_NODISCARD [[nodiscard]]
61+
62+
// [[maybe_unused]]
63+
#define ET_UNUSED [[maybe_unused]]
64+
65+
// Inline/NoInline
66+
#if defined(_MSC_VER)
67+
#define ET_NOINLINE __declspec(noinline)
68+
#define ET_INLINE __forceinline
69+
#define ET_INLINE_ATTRIBUTE __forceinline
70+
#elif defined(__GNUC__) || defined(__clang__)
5871
#define ET_NOINLINE __attribute__((noinline))
5972
#define ET_INLINE __attribute__((always_inline)) inline
6073
#define ET_INLINE_ATTRIBUTE __attribute__((always_inline))
74+
#else
75+
#define ET_NOINLINE
76+
#define ET_INLINE inline
77+
#define ET_INLINE_ATTRIBUTE
78+
#endif
6179

62-
#if defined(__GNUC__)
63-
80+
// Unreachable
81+
#if defined(__GNUC__) || defined(__clang__)
6482
#define ET_UNREACHABLE() __builtin_unreachable()
65-
6683
#elif defined(_MSC_VER)
67-
6884
#define ET_UNREACHABLE() __assume(0)
85+
#else
86+
#define ET_UNREACHABLE() do {} while (1)
87+
#endif
6988

70-
#else // defined(__GNUC__)
71-
72-
#define ET_UNREACHABLE() \
73-
while (1) \
74-
;
75-
76-
#endif // defined(__GNUC__)
77-
78-
#define ET_DEPRECATED [[deprecated]]
79-
#define ET_EXPERIMENTAL \
80-
[[deprecated("This API is experimental and may change without notice.")]]
81-
#define ET_FALLTHROUGH [[fallthrough]]
82-
#define ET_NODISCARD [[nodiscard]]
83-
#define ET_UNUSED [[maybe_unused]]
84-
85-
// UNLIKELY Macro
86-
// example
87-
// if ET_UNLIKELY(a > 10 && b < 5) {
88-
// do something
89-
// }
89+
// Likely/Unlikely
9090
#if (__cplusplus) >= 202002L
91-
9291
#define ET_LIKELY(expr) (expr) [[likely]]
9392
#define ET_UNLIKELY(expr) (expr) [[unlikely]]
94-
9593
#else
96-
9794
#define ET_LIKELY(expr) (expr)
9895
#define ET_UNLIKELY(expr) (expr)
96+
#endif
9997

100-
#endif // (__cplusplus) >= 202002L
101-
102-
/// Define a C symbol with weak linkage.
103-
#ifdef _MSC_VER
104-
// There currently doesn't seem to be a great way to do this in Windows and
105-
// given that weak linkage is not really critical on Windows, we'll just leave
106-
// it as a stub.
98+
// Weak linkage
99+
#if defined(_MSC_VER)
100+
// No weak linkage in MSVC
107101
#define ET_WEAK
108-
#else
102+
#elif defined(__GNUC__) || defined(__clang__)
109103
#define ET_WEAK __attribute__((weak))
104+
#else
105+
#define ET_WEAK
110106
#endif
111107

112-
/**
113-
* Annotation marking a function as printf-like, providing compiler support
114-
* for format string argument checking.
115-
*/
116-
#ifdef _MSC_VER
108+
// Printf-like format checking
109+
#if defined(_MSC_VER)
117110
#include <sal.h>
118111
#define ET_PRINTFLIKE(_string_index, _va_index) _Printf_format_string_
119-
#else
112+
#elif defined(__GNUC__) || defined(__clang__)
120113
#define ET_PRINTFLIKE(_string_index, _va_index) \
121114
__attribute__((format(printf, _string_index, _va_index)))
115+
#else
116+
#define ET_PRINTFLIKE(_string_index, _va_index)
122117
#endif
123118

119+
// -----------------------------------------------------------------------------
120+
// Builtin/Source location helpers
121+
// -----------------------------------------------------------------------------
122+
124123
#ifndef __has_builtin
125-
#define __has_builtin(x) (0)
124+
#define __has_builtin(x) 0
126125
#endif
127126

128127
#if __has_builtin(__builtin_strrchr)
129-
/// Name of the source file without a directory string.
130128
#define ET_SHORT_FILENAME (__builtin_strrchr("/" __FILE__, '/') + 1)
131129
#else
132130
#define ET_SHORT_FILENAME __FILE__
133131
#endif
134132

135133
#if __has_builtin(__builtin_LINE)
136-
/// Current line as an integer.
137134
#define ET_LINE __builtin_LINE()
138135
#else
139136
#define ET_LINE __LINE__
140-
#endif // __has_builtin(__builtin_LINE)
137+
#endif
141138

142139
#if __has_builtin(__builtin_FUNCTION)
143-
/// Name of the current function as a const char[].
144140
#define ET_FUNCTION __builtin_FUNCTION()
145141
#else
146142
#define ET_FUNCTION __FUNCTION__
147-
#endif // __has_builtin(__builtin_FUNCTION)
143+
#endif
144+
145+
// -----------------------------------------------------------------------------
146+
// Format specifiers for size_t/ssize_t
147+
// -----------------------------------------------------------------------------
148148

149-
// As of G3 RJ-2024.3 toolchain, zu format specifier is not supported for Xtensa
150149
#if defined(__XTENSA__)
151150
#define ET_PRIsize_t "lu"
152151
#define ET_PRIssize_t "ld"
@@ -155,24 +154,33 @@
155154
#define ET_PRIssize_t "zd"
156155
#endif
157156

158-
// Whether the compiler supports GNU statement expressions.
159-
// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
157+
// -----------------------------------------------------------------------------
158+
// GNU statement expressions
159+
// -----------------------------------------------------------------------------
160+
160161
#ifndef ET_HAVE_GNU_STATEMENT_EXPRESSIONS
161162
#if (defined(__GNUC__) && __GNUC__ >= 3) || defined(__clang__)
162163
#define ET_HAVE_GNU_STATEMENT_EXPRESSIONS 1
163164
#else
164165
#define ET_HAVE_GNU_STATEMENT_EXPRESSIONS 0
165166
#endif
166-
#endif // ifndef
167+
#endif
168+
169+
// -----------------------------------------------------------------------------
170+
// ssize_t definition
171+
// -----------------------------------------------------------------------------
167172

168-
// Define size_t and ssize_t.
169173
#ifndef _MSC_VER
170174
#include <sys/types.h>
171175
#else
172176
#include <stddef.h>
173177
using ssize_t = ptrdiff_t;
174178
#endif
175179

180+
// -----------------------------------------------------------------------------
181+
// Exception support
182+
// -----------------------------------------------------------------------------
183+
176184
#ifdef __EXCEPTIONS
177185
#define ET_HAS_EXCEPTIONS 1
178186
#elif defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS
@@ -181,8 +189,10 @@ using ssize_t = ptrdiff_t;
181189
#define ET_HAS_EXCEPTIONS 0
182190
#endif
183191

184-
// DEPRECATED: Use the non-underscore-prefixed versions instead.
185-
// TODO(T199005537): Remove these once all users have stopped using them.
192+
// -----------------------------------------------------------------------------
193+
// Deprecated legacy macros (to be removed)
194+
// -----------------------------------------------------------------------------
195+
186196
#define __ET_DEPRECATED ET_DEPRECATED
187197
#define __ET_FALLTHROUGH ET_FALLTHROUGH
188198
#define __ET_FUNCTION ET_FUNCTION

0 commit comments

Comments
 (0)