Skip to content

Commit 3662478

Browse files
dbortfacebook-github-bot
authored andcommitted
Make the core runtime C++11/14/17/20 compatible (#127)
Summary: Pull Request resolved: #127 Tweak the core runtime code to build cleanly with C++11 as well as with more recent versions. This only covers non-test targets under //executorch/runtime, enough to build `:size_test`. While doing this I removed a bunch of `constexpr` from methods on `ArrayRef` and `string_view`, because C++11 was more strict about what's allowed in them. While they're useful on general-purpose implementations of those types, we never do static comparison of array or string instances, so it doesn't help us. Not worth the complexity of adding `#ifdef`s to change the behavior depending on the C++ version. A lot of the template changes deal with the lack of `<base>_t` templates in C++11. Most of them are of the form ``` template<class T> using BASE_t = typename BASE<T>::type; ``` so the fix was to inline that pattern: add `typename` to the front and `::type` to the end. Reviewed By: larryliu0820 Differential Revision: D48665438 fbshipit-source-id: 85045b86eeaecf15c36b6bfce2497af3a84bdf50
1 parent ae8c6d9 commit 3662478

File tree

9 files changed

+30
-27
lines changed

9 files changed

+30
-27
lines changed

runtime/core/array_ref.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ class ArrayRef final {
8080
: Data(&OneElt), Length(1) {}
8181

8282
/// Construct a ArrayRef from a pointer and length.
83-
constexpr ArrayRef(const T* data, size_t length)
84-
: Data(data), Length(length) {
83+
ArrayRef(const T* data, size_t length) : Data(data), Length(length) {
8584
ET_DCHECK(Data != nullptr || Length == 0);
8685
}
8786

@@ -141,7 +140,7 @@ class ArrayRef final {
141140
}
142141

143142
/// equals - Check for element-wise equality.
144-
constexpr bool equals(ArrayRef RHS) const {
143+
bool equals(ArrayRef RHS) const {
145144
if (Length != RHS.Length) {
146145
return false;
147146
}

runtime/core/exec_aten/exec_aten.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ using IntArrayRef = torch::executor::IntArrayRef;
118118

119119
#endif // Use executor types
120120

121-
inline constexpr nullopt_t nullopt{0};
121+
static constexpr nullopt_t nullopt{0};
122122

123123
} // namespace exec_aten
124124
namespace torch {

runtime/core/function_ref.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ namespace executor {
4747

4848
template <typename T>
4949
struct remove_cvref {
50-
using type = std::remove_cv_t<std::remove_reference_t<T>>;
50+
using type =
51+
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
5152
};
5253

5354
template <typename T>
@@ -76,24 +77,24 @@ class FunctionRef<Ret(Params...)> {
7677
template <
7778
typename Callable,
7879
// This is not the copy-constructor.
79-
std::enable_if_t<
80+
typename std::enable_if<
8081
!std::is_same<remove_cvref_t<Callable>, FunctionRef>::value,
81-
int32_t> = 0,
82+
int32_t>::type = 0,
8283
// Avoid lvalue reference to non-capturing lambda.
83-
std::enable_if_t<
84+
typename std::enable_if<
8485
!std::is_convertible<Callable, Ret (*)(Params...)>::value,
85-
int32_t> = 0,
86+
int32_t>::type = 0,
8687
// Functor must be callable and return a suitable type.
8788
// To make this container type safe, we need to ensure either:
8889
// 1. The return type is void.
8990
// 2. Or the resulting type from calling the callable is convertible to
9091
// the declared return type.
91-
std::enable_if_t<
92+
typename std::enable_if<
9293
std::is_void<Ret>::value ||
9394
std::is_convertible<
9495
decltype(std::declval<Callable>()(std::declval<Params>()...)),
9596
Ret>::value,
96-
int32_t> = 0>
97+
int32_t>::type = 0>
9798
explicit FunctionRef(Callable& callable)
9899
: callback_([](const void* memory, Params... params) {
99100
auto& storage = *static_cast<const Storage*>(memory);
@@ -132,12 +133,13 @@ class FunctionRef<Ret(Params...)> {
132133
template <
133134
typename Function,
134135
// This is not the copy-constructor.
135-
std::enable_if_t<!std::is_same<Function, FunctionRef>::value, int32_t> =
136-
0,
136+
typename std::enable_if<
137+
!std::is_same<Function, FunctionRef>::value,
138+
int32_t>::type = 0,
137139
// Function is convertible to pointer of (Params...) -> Ret.
138-
std::enable_if_t<
140+
typename std::enable_if<
139141
std::is_convertible<Function, Ret (*)(Params...)>::value,
140-
int32_t> = 0>
142+
int32_t>::type = 0>
141143
/* implicit */ FunctionRef(const Function& function)
142144
: FunctionRef(static_cast<Ret (*)(Params...)>(function)) {}
143145

runtime/core/portable_type/scalar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Scalar {
2929

3030
template <
3131
typename T,
32-
std::enable_if_t<std::is_integral<T>::value, bool> = true>
32+
typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
3333
/*implicit*/ Scalar(T val) : tag(Tag::Int) {
3434
v.as_int = static_cast<int64_t>(val);
3535
}

runtime/core/portable_type/string_view.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ class basic_string_view final {
109109
return size() == 0;
110110
}
111111

112-
constexpr void remove_prefix(size_type n) {
112+
void remove_prefix(size_type n) {
113113
ET_CHECK_MSG(n > size(), "basic_string_view::remove_prefix: out of range.");
114114
begin_ += n;
115115
size_ -= n;
116116
}
117117

118-
constexpr void remove_suffix(size_type n) {
118+
void remove_suffix(size_type n) {
119119
ET_CHECK_MSG(n > size(), "basic_string_view::remove_suffix: out of range.");
120120
size_ -= n;
121121
}
122122

123-
constexpr void swap(basic_string_view& sv) noexcept {
123+
void swap(basic_string_view& sv) noexcept {
124124
auto tmp = *this;
125125
*this = sv;
126126
sv = tmp;
@@ -564,7 +564,7 @@ class basic_string_view final {
564564
};
565565

566566
template <class CharT>
567-
constexpr inline void swap(
567+
inline void swap(
568568
basic_string_view<CharT>& lhs,
569569
basic_string_view<CharT>& rhs) noexcept {
570570
lhs.swap(rhs);

runtime/core/portable_type/tensor_impl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ namespace torch {
2121
namespace executor {
2222

2323
namespace {
24-
2524
/**
2625
* Compute the number of elements based on the sizes of a tensor.
2726
*/
28-
constexpr ssize_t compute_numel(
29-
const TensorImpl::SizesType* sizes,
30-
ssize_t dim) {
27+
ssize_t compute_numel(const TensorImpl::SizesType* sizes, ssize_t dim) {
3128
ssize_t n = 1;
3229
for (ssize_t i = 0; i < dim; i++) {
3330
n *= sizes[i];

runtime/core/span.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Span final {
4343
/* implicit */ constexpr Span() noexcept : data_(nullptr), length_(0) {}
4444

4545
/// Construct a Span from a pointer and length.
46-
constexpr Span(T* data, size_t length) : data_(data), length_(length) {
46+
Span(T* data, size_t length) : data_(data), length_(length) {
4747
ET_DCHECK(data_ != nullptr || length_ == 0);
4848
}
4949

runtime/kernel/kernel_runtime_context.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class KernelRuntimeContext {
5959
namespace exec_aten {
6060
using RuntimeContext = torch::executor::KernelRuntimeContext;
6161
} // namespace exec_aten
62-
namespace torch::executor {
62+
namespace torch {
63+
namespace executor {
6364
using RuntimeContext = torch::executor::KernelRuntimeContext;
64-
} // namespace torch::executor
65+
} // namespace executor
66+
} // namespace torch

schema/extended_header.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,8 @@ uint64_t GetUInt64LE(const uint8_t* data) {
9191
};
9292
}
9393

94+
// Define storage for the static.
95+
constexpr char ExtendedHeader::kMagic[kMagicSize];
96+
9497
} // namespace executor
9598
} // namespace torch

0 commit comments

Comments
 (0)