Skip to content

Commit 3b765cb

Browse files
committed
[FFI] Replace __attribute__ with C++ standard attributes
1 parent ed4c92c commit 3b765cb

File tree

19 files changed

+213
-217
lines changed

19 files changed

+213
-217
lines changed

ffi/include/tvm/ffi/any.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class AnyView {
175175
* \return Create an AnyView from TVMFFIAny
176176
* \param data the underlying ffi data.
177177
*/
178-
static TVM_FFI_INLINE AnyView CopyFromTVMFFIAny(TVMFFIAny data) {
178+
TVM_FFI_INLINE static AnyView CopyFromTVMFFIAny(TVMFFIAny data) {
179179
AnyView view;
180180
view.data_ = data;
181181
return view;
@@ -482,14 +482,14 @@ struct Type2Str<void> {
482482
// Extra unsafe method to help any manipulation
483483
struct AnyUnsafe : public ObjectUnsafe {
484484
// FFI related operations
485-
static TVM_FFI_INLINE TVMFFIAny MoveAnyToTVMFFIAny(Any&& any) {
485+
TVM_FFI_INLINE static TVMFFIAny MoveAnyToTVMFFIAny(Any&& any) {
486486
TVMFFIAny result = any.data_;
487487
any.data_.type_index = TypeIndex::kTVMFFINone;
488488
any.data_.v_int64 = 0;
489489
return result;
490490
}
491491

492-
static TVM_FFI_INLINE Any MoveTVMFFIAnyToAny(TVMFFIAny&& data) {
492+
TVM_FFI_INLINE static Any MoveTVMFFIAnyToAny(TVMFFIAny&& data) {
493493
Any any;
494494
any.data_ = data;
495495
data.type_index = TypeIndex::kTVMFFINone;
@@ -498,12 +498,12 @@ struct AnyUnsafe : public ObjectUnsafe {
498498
}
499499

500500
template <typename T>
501-
static TVM_FFI_INLINE bool CheckAnyStrict(const Any& ref) {
501+
TVM_FFI_INLINE static bool CheckAnyStrict(const Any& ref) {
502502
return TypeTraits<T>::CheckAnyStrict(&(ref.data_));
503503
}
504504

505505
template <typename T>
506-
static TVM_FFI_INLINE T CopyFromAnyViewAfterCheck(const Any& ref) {
506+
TVM_FFI_INLINE static T CopyFromAnyViewAfterCheck(const Any& ref) {
507507
if constexpr (!std::is_same_v<T, Any>) {
508508
return TypeTraits<T>::CopyFromAnyViewAfterCheck(&(ref.data_));
509509
} else {
@@ -512,24 +512,24 @@ struct AnyUnsafe : public ObjectUnsafe {
512512
}
513513

514514
template <typename T>
515-
static TVM_FFI_INLINE T MoveFromAnyAfterCheck(Any&& ref) {
515+
TVM_FFI_INLINE static T MoveFromAnyAfterCheck(Any&& ref) {
516516
if constexpr (!std::is_same_v<T, Any>) {
517517
return TypeTraits<T>::MoveFromAnyAfterCheck(&(ref.data_));
518518
} else {
519519
return std::move(ref);
520520
}
521521
}
522522

523-
static TVM_FFI_INLINE Object* ObjectPtrFromAnyAfterCheck(const Any& ref) {
523+
TVM_FFI_INLINE static Object* ObjectPtrFromAnyAfterCheck(const Any& ref) {
524524
return reinterpret_cast<Object*>(ref.data_.v_obj);
525525
}
526526

527-
static TVM_FFI_INLINE const TVMFFIAny* TVMFFIAnyPtrFromAny(const Any& ref) {
527+
TVM_FFI_INLINE static const TVMFFIAny* TVMFFIAnyPtrFromAny(const Any& ref) {
528528
return &(ref.data_);
529529
}
530530

531531
template <typename T>
532-
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const Any& ref) {
532+
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const Any& ref) {
533533
return TypeTraits<T>::GetMismatchTypeInfo(&(ref.data_));
534534
}
535535
};

ffi/include/tvm/ffi/base_details.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
#endif
5050

5151
#if defined(_MSC_VER)
52-
#define TVM_FFI_INLINE __forceinline
52+
#define TVM_FFI_INLINE [[msvc::forceinline]] inline
5353
#else
54-
#define TVM_FFI_INLINE inline __attribute__((always_inline))
54+
#define TVM_FFI_INLINE [[gnu::always_inline]] inline
5555
#endif
5656

5757
/*!
@@ -60,9 +60,9 @@
6060
* e.g. some logging functions.
6161
*/
6262
#if defined(_MSC_VER)
63-
#define TVM_FFI_NO_INLINE __declspec(noinline)
63+
#define TVM_FFI_NO_INLINE [[msvc::noinline]]
6464
#else
65-
#define TVM_FFI_NO_INLINE __attribute__((noinline))
65+
#define TVM_FFI_NO_INLINE [[gnu::noinline]]
6666
#endif
6767

6868
#if defined(_MSC_VER)
@@ -72,11 +72,7 @@
7272
#endif
7373

7474
/*! \brief helper macro to suppress unused warning */
75-
#if defined(__GNUC__)
76-
#define TVM_FFI_ATTRIBUTE_UNUSED __attribute__((unused))
77-
#else
78-
#define TVM_FFI_ATTRIBUTE_UNUSED
79-
#endif
75+
#define TVM_FFI_ATTRIBUTE_UNUSED [[maybe_unused]]
8076

8177
#define TVM_FFI_STR_CONCAT_(__x, __y) __x##__y
8278
#define TVM_FFI_STR_CONCAT(__x, __y) TVM_FFI_STR_CONCAT_(__x, __y)
@@ -90,7 +86,7 @@
9086
#endif
9187

9288
#define TVM_FFI_STATIC_INIT_BLOCK_VAR_DEF \
93-
static inline TVM_FFI_ATTRIBUTE_UNUSED int __##TVMFFIStaticInitReg
89+
TVM_FFI_ATTRIBUTE_UNUSED static inline int __##TVMFFIStaticInitReg
9490

9591
/*! \brief helper macro to run code once during initialization */
9692
#define TVM_FFI_STATIC_INIT_BLOCK(Body) \

ffi/include/tvm/ffi/container/array.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> {
10011001
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIArray;
10021002
using ObjectRefTypeTraitsBase<Array<T>>::CopyFromAnyViewAfterCheck;
10031003

1004-
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
1004+
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
10051005
if (src->type_index != TypeIndex::kTVMFFIArray) {
10061006
return TypeTraitsBase::GetMismatchTypeInfo(src);
10071007
}
@@ -1022,7 +1022,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> {
10221022
TVM_FFI_UNREACHABLE();
10231023
}
10241024

1025-
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny* src) {
1025+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
10261026
if (src->type_index != TypeIndex::kTVMFFIArray) return false;
10271027
if constexpr (std::is_same_v<T, Any>) {
10281028
return true;
@@ -1036,7 +1036,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> {
10361036
}
10371037
}
10381038

1039-
static TVM_FFI_INLINE std::optional<Array<T>> TryCastFromAnyView(const TVMFFIAny* src) {
1039+
TVM_FFI_INLINE static std::optional<Array<T>> TryCastFromAnyView(const TVMFFIAny* src) {
10401040
// try to run conversion.
10411041
if (src->type_index != TypeIndex::kTVMFFIArray) return std::nullopt;
10421042
if constexpr (!std::is_same_v<T, Any>) {
@@ -1069,7 +1069,7 @@ struct TypeTraits<Array<T>> : public ObjectRefTypeTraitsBase<Array<T>> {
10691069
}
10701070
}
10711071

1072-
static TVM_FFI_INLINE std::string TypeStr() { return "Array<" + details::Type2Str<T>::v() + ">"; }
1072+
TVM_FFI_INLINE static std::string TypeStr() { return "Array<" + details::Type2Str<T>::v() + ">"; }
10731073
};
10741074

10751075
namespace details {

ffi/include/tvm/ffi/container/map.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> {
15631563
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIMap;
15641564
using ObjectRefTypeTraitsBase<Map<K, V>>::CopyFromAnyViewAfterCheck;
15651565

1566-
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
1566+
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
15671567
if (src->type_index != TypeIndex::kTVMFFIMap) {
15681568
return TypeTraitsBase::GetMismatchTypeInfo(src);
15691569
}
@@ -1590,7 +1590,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> {
15901590
TVM_FFI_UNREACHABLE();
15911591
}
15921592

1593-
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny* src) {
1593+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
15941594
if (src->type_index != TypeIndex::kTVMFFIMap) return false;
15951595
if constexpr (std::is_same_v<K, Any> && std::is_same_v<V, Any>) {
15961596
return true;
@@ -1608,7 +1608,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> {
16081608
}
16091609
}
16101610

1611-
static TVM_FFI_INLINE std::optional<Map<K, V>> TryCastFromAnyView(const TVMFFIAny* src) {
1611+
TVM_FFI_INLINE static std::optional<Map<K, V>> TryCastFromAnyView(const TVMFFIAny* src) {
16121612
if (src->type_index != TypeIndex::kTVMFFIMap) return std::nullopt;
16131613
if constexpr (!std::is_same_v<K, Any> || !std::is_same_v<V, Any>) {
16141614
const MapObj* n = reinterpret_cast<const MapObj*>(src->v_obj);
@@ -1639,7 +1639,7 @@ struct TypeTraits<Map<K, V>> : public ObjectRefTypeTraitsBase<Map<K, V>> {
16391639
}
16401640
}
16411641

1642-
static TVM_FFI_INLINE std::string TypeStr() {
1642+
TVM_FFI_INLINE static std::string TypeStr() {
16431643
return "Map<" + details::Type2Str<K>::v() + ", " + details::Type2Str<V>::v() + ">";
16441644
}
16451645
};

ffi/include/tvm/ffi/container/shape.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ inline constexpr bool use_default_type_traits_v<Shape> = false;
210210
template <>
211211
struct TypeTraits<Shape> : public ObjectRefWithFallbackTraitsBase<Shape, Array<int64_t>> {
212212
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIShape;
213-
static TVM_FFI_INLINE Shape ConvertFallbackValue(Array<int64_t> src) { return Shape(src); }
213+
TVM_FFI_INLINE static Shape ConvertFallbackValue(Array<int64_t> src) { return Shape(src); }
214214
};
215215

216216
} // namespace ffi

ffi/include/tvm/ffi/container/tuple.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ template <typename... Types>
175175
struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types...>> {
176176
using ObjectRefTypeTraitsBase<Tuple<Types...>>::CopyFromAnyViewAfterCheck;
177177

178-
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
178+
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
179179
if (src->type_index != TypeIndex::kTVMFFIArray) {
180180
return TypeTraitsBase::GetMismatchTypeInfo(src);
181181
}
@@ -187,7 +187,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
187187
}
188188

189189
template <size_t I, typename T, typename... Rest>
190-
static TVM_FFI_INLINE std::string GetMismatchTypeInfoHelper(const Any* arr) {
190+
TVM_FFI_INLINE static std::string GetMismatchTypeInfoHelper(const Any* arr) {
191191
if constexpr (!std::is_same_v<T, Any>) {
192192
const Any& any_v = arr[I];
193193
if (!details::AnyUnsafe::CheckAnyStrict<T>(any_v) && !(any_v.try_cast<T>().has_value())) {
@@ -203,7 +203,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
203203
TVM_FFI_UNREACHABLE();
204204
}
205205

206-
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny* src) {
206+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
207207
if (src->type_index != TypeIndex::kTVMFFIArray) return false;
208208
const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj);
209209
if (n->size() != sizeof...(Types)) return false;
@@ -212,7 +212,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
212212
}
213213

214214
template <size_t I, typename T, typename... Rest>
215-
static TVM_FFI_INLINE bool CheckAnyStrictHelper(const TVMFFIAny* src_arr) {
215+
TVM_FFI_INLINE static bool CheckAnyStrictHelper(const TVMFFIAny* src_arr) {
216216
if constexpr (!std::is_same_v<T, Any>) {
217217
if (!TypeTraits<T>::CheckAnyStrict(src_arr + I)) {
218218
return false;
@@ -224,7 +224,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
224224
return true;
225225
}
226226

227-
static TVM_FFI_INLINE std::optional<Tuple<Types...>> TryCastFromAnyView(const TVMFFIAny* src //
227+
TVM_FFI_INLINE static std::optional<Tuple<Types...>> TryCastFromAnyView(const TVMFFIAny* src //
228228
) {
229229
if (src->type_index != TypeIndex::kTVMFFIArray) return std::nullopt;
230230
const ArrayObj* n = reinterpret_cast<const ArrayObj*>(src->v_obj);
@@ -243,7 +243,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
243243
}
244244

245245
template <size_t I, typename T, typename... Rest>
246-
static TVM_FFI_INLINE bool TryConvertElements(Any* arr) {
246+
TVM_FFI_INLINE static bool TryConvertElements(Any* arr) {
247247
if constexpr (!std::is_same_v<T, Any>) {
248248
if (auto opt_convert = arr[I].try_cast<T>()) {
249249
arr[I] = *std::move(opt_convert);
@@ -258,7 +258,7 @@ struct TypeTraits<Tuple<Types...>> : public ObjectRefTypeTraitsBase<Tuple<Types.
258258
}
259259
}
260260

261-
static TVM_FFI_INLINE std::string TypeStr() {
261+
TVM_FFI_INLINE static std::string TypeStr() {
262262
return details::ContainerTypeStr<Types...>("Tuple");
263263
}
264264
};

ffi/include/tvm/ffi/container/variant.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,31 @@ inline constexpr bool use_default_type_traits_v<Variant<V...>> = false;
189189

190190
template <typename... V>
191191
struct TypeTraits<Variant<V...>> : public TypeTraitsBase {
192-
static TVM_FFI_INLINE void CopyToAnyView(const Variant<V...>& src, TVMFFIAny* result) {
192+
TVM_FFI_INLINE static void CopyToAnyView(const Variant<V...>& src, TVMFFIAny* result) {
193193
*result = src.ToAnyView().CopyToTVMFFIAny();
194194
}
195195

196-
static TVM_FFI_INLINE void MoveToAny(Variant<V...> src, TVMFFIAny* result) {
196+
TVM_FFI_INLINE static void MoveToAny(Variant<V...> src, TVMFFIAny* result) {
197197
*result = details::AnyUnsafe::MoveAnyToTVMFFIAny(std::move(src).MoveToAny());
198198
}
199199

200-
static TVM_FFI_INLINE std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
200+
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
201201
return TypeTraitsBase::GetMismatchTypeInfo(src);
202202
}
203203

204-
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny* src) {
204+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
205205
return (TypeTraits<V>::CheckAnyStrict(src) || ...);
206206
}
207207

208-
static TVM_FFI_INLINE Variant<V...> CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
208+
TVM_FFI_INLINE static Variant<V...> CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
209209
return Variant<V...>(Any(AnyView::CopyFromTVMFFIAny(*src)));
210210
}
211211

212-
static TVM_FFI_INLINE Variant<V...> MoveFromAnyAfterCheck(TVMFFIAny* src) {
212+
TVM_FFI_INLINE static Variant<V...> MoveFromAnyAfterCheck(TVMFFIAny* src) {
213213
return Variant<V...>(details::AnyUnsafe::MoveTVMFFIAnyToAny(std::move(*src)));
214214
}
215215

216-
static TVM_FFI_INLINE std::optional<Variant<V...>> TryCastFromAnyView(const TVMFFIAny* src) {
216+
TVM_FFI_INLINE static std::optional<Variant<V...>> TryCastFromAnyView(const TVMFFIAny* src) {
217217
// fast path, storage is already in the right type
218218
if (CheckAnyStrict(src)) {
219219
return CopyFromAnyViewAfterCheck(src);
@@ -223,7 +223,7 @@ struct TypeTraits<Variant<V...>> : public TypeTraitsBase {
223223
}
224224

225225
template <typename VariantType, typename... Rest>
226-
static TVM_FFI_INLINE std::optional<Variant<V...>> TryVariantTypes(const TVMFFIAny* src) {
226+
TVM_FFI_INLINE static std::optional<Variant<V...>> TryVariantTypes(const TVMFFIAny* src) {
227227
if (auto opt_convert = TypeTraits<VariantType>::TryCastFromAnyView(src)) {
228228
return Variant<V...>(*std::move(opt_convert));
229229
}
@@ -233,7 +233,7 @@ struct TypeTraits<Variant<V...>> : public TypeTraitsBase {
233233
return std::nullopt;
234234
}
235235

236-
static TVM_FFI_INLINE std::string TypeStr() { return details::ContainerTypeStr<V...>("Variant"); }
236+
TVM_FFI_INLINE static std::string TypeStr() { return details::ContainerTypeStr<V...>("Variant"); }
237237
};
238238

239239
template <typename... V>

ffi/include/tvm/ffi/dtype.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,29 @@ template <>
130130
struct TypeTraits<DLDataType> : public TypeTraitsBase {
131131
static constexpr int32_t field_static_type_index = TypeIndex::kTVMFFIDataType;
132132

133-
static TVM_FFI_INLINE void CopyToAnyView(const DLDataType& src, TVMFFIAny* result) {
133+
TVM_FFI_INLINE static void CopyToAnyView(const DLDataType& src, TVMFFIAny* result) {
134134
// clear padding part to ensure the equality check can always check the v_uint64 part
135135
result->v_uint64 = 0;
136136
result->type_index = TypeIndex::kTVMFFIDataType;
137137
result->v_dtype = src;
138138
}
139139

140-
static TVM_FFI_INLINE void MoveToAny(DLDataType src, TVMFFIAny* result) {
140+
TVM_FFI_INLINE static void MoveToAny(DLDataType src, TVMFFIAny* result) {
141141
// clear padding part to ensure the equality check can always check the v_uint64 part
142142
result->v_uint64 = 0;
143143
result->type_index = TypeIndex::kTVMFFIDataType;
144144
result->v_dtype = src;
145145
}
146146

147-
static TVM_FFI_INLINE bool CheckAnyStrict(const TVMFFIAny* src) {
147+
TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
148148
return src->type_index == TypeIndex::kTVMFFIDataType;
149149
}
150150

151-
static TVM_FFI_INLINE DLDataType CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
151+
TVM_FFI_INLINE static DLDataType CopyFromAnyViewAfterCheck(const TVMFFIAny* src) {
152152
return src->v_dtype;
153153
}
154154

155-
static TVM_FFI_INLINE std::optional<DLDataType> TryCastFromAnyView(const TVMFFIAny* src) {
155+
TVM_FFI_INLINE static std::optional<DLDataType> TryCastFromAnyView(const TVMFFIAny* src) {
156156
if (src->type_index == TypeIndex::kTVMFFIDataType) {
157157
return src->v_dtype;
158158
}
@@ -163,7 +163,7 @@ struct TypeTraits<DLDataType> : public TypeTraitsBase {
163163
return std::nullopt;
164164
}
165165

166-
static TVM_FFI_INLINE std::string TypeStr() { return ffi::StaticTypeKey::kTVMFFIDataType; }
166+
TVM_FFI_INLINE static std::string TypeStr() { return ffi::StaticTypeKey::kTVMFFIDataType; }
167167
};
168168
} // namespace ffi
169169
} // namespace tvm

0 commit comments

Comments
 (0)