Skip to content

Commit 216ef5b

Browse files
author
Frank Laub
committed
[MLIR][NFC] Fix for VS2017 bug.
Apparently there is a bug in VS2017 concerning compile-time constant expressions. https://developercommunityapi.westus.cloudapp.azure.com/content/problem/110435/not-compile-time-constant-expression.html This change works with any C++11 compiler (including VS2017) and is a little bit easier to read. Reviewers: stella.stamenova, nicolasvasilache Tags: #llvm Differential Revision: https://reviews.llvm.org/D75584
1 parent 1a5da3f commit 216ef5b

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

mlir/include/mlir/ExecutionEngine/CRunnerUtils.h

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
#include <cstdint>
3333

34-
template <int N> void dropFront(int64_t arr[N], int64_t *res) {
34+
template <int N>
35+
void dropFront(int64_t arr[N], int64_t *res) {
3536
for (unsigned i = 1; i < N; ++i)
3637
*(res + i - 1) = arr[i];
3738
}
@@ -40,23 +41,13 @@ template <int N> void dropFront(int64_t arr[N], int64_t *res) {
4041
// Codegen-compatible structures for Vector type.
4142
//===----------------------------------------------------------------------===//
4243
namespace detail {
43-
template <unsigned N>
44-
constexpr bool isPowerOf2() {
45-
return (!(N & (N - 1)));
46-
}
47-
48-
template <unsigned N>
49-
constexpr unsigned nextPowerOf2();
50-
template <>
51-
constexpr unsigned nextPowerOf2<0>() {
52-
return 1;
53-
}
54-
template <>
55-
constexpr unsigned nextPowerOf2<1>() {
56-
return 1;
57-
}
58-
template <unsigned N> constexpr unsigned nextPowerOf2() {
59-
return isPowerOf2<N>() ? N : 2 * nextPowerOf2<(N + 1) / 2>();
44+
45+
constexpr bool isPowerOf2(int N) { return (!(N & (N - 1))); }
46+
47+
constexpr unsigned nextPowerOf2(int N) {
48+
if (N <= 1)
49+
return 1;
50+
return isPowerOf2(N) ? N : 2 * nextPowerOf2((N + 1) / 2);
6051
}
6152

6253
template <typename T, int Dim, bool IsPowerOf2>
@@ -65,7 +56,7 @@ struct Vector1D;
6556
template <typename T, int Dim>
6657
struct Vector1D<T, Dim, /*IsPowerOf2=*/true> {
6758
Vector1D() {
68-
static_assert(detail::nextPowerOf2<sizeof(T[Dim])>() == sizeof(T[Dim]),
59+
static_assert(detail::nextPowerOf2(sizeof(T[Dim])) == sizeof(T[Dim]),
6960
"size error");
7061
}
7162
constexpr T &operator[](unsigned i) { return vector[i]; }
@@ -80,17 +71,17 @@ struct Vector1D<T, Dim, /*IsPowerOf2=*/true> {
8071
template <typename T, int Dim>
8172
struct Vector1D<T, Dim, /*IsPowerOf2=*/false> {
8273
Vector1D() {
83-
static_assert(detail::nextPowerOf2<sizeof(T[Dim])>() > sizeof(T[Dim]),
74+
static_assert(detail::nextPowerOf2(sizeof(T[Dim])) > sizeof(T[Dim]),
8475
"size error");
85-
static_assert(detail::nextPowerOf2<sizeof(T[Dim])>() < 2 * sizeof(T[Dim]),
76+
static_assert(detail::nextPowerOf2(sizeof(T[Dim])) < 2 * sizeof(T[Dim]),
8677
"size error");
8778
}
8879
constexpr T &operator[](unsigned i) { return vector[i]; }
8980
constexpr const T &operator[](unsigned i) const { return vector[i]; }
9081

9182
private:
9283
T vector[Dim];
93-
char padding[detail::nextPowerOf2<sizeof(T[Dim])>() - sizeof(T[Dim])];
84+
char padding[detail::nextPowerOf2(sizeof(T[Dim])) - sizeof(T[Dim])];
9485
};
9586
} // end namespace detail
9687

@@ -110,7 +101,7 @@ struct Vector {
110101
// We insert explicit padding in to account for this.
111102
template <typename T, int Dim>
112103
struct Vector<T, Dim>
113-
: public detail::Vector1D<T, Dim, detail::isPowerOf2<sizeof(T[Dim])>()> {};
104+
: public detail::Vector1D<T, Dim, detail::isPowerOf2(sizeof(T[Dim]))> {};
114105

115106
template <int D1, typename T>
116107
using Vector1D = Vector<T, D1>;
@@ -125,7 +116,8 @@ using Vector4D = Vector<T, D1, D2, D3, D4>;
125116
// Codegen-compatible structures for StridedMemRef type.
126117
//===----------------------------------------------------------------------===//
127118
/// StridedMemRef descriptor type with static rank.
128-
template <typename T, int N> struct StridedMemRefType {
119+
template <typename T, int N>
120+
struct StridedMemRefType {
129121
T *basePtr;
130122
T *data;
131123
int64_t offset;
@@ -144,7 +136,8 @@ template <typename T, int N> struct StridedMemRefType {
144136
};
145137

146138
/// StridedMemRef descriptor type specialized for rank 1.
147-
template <typename T> struct StridedMemRefType<T, 1> {
139+
template <typename T>
140+
struct StridedMemRefType<T, 1> {
148141
T *basePtr;
149142
T *data;
150143
int64_t offset;
@@ -154,7 +147,8 @@ template <typename T> struct StridedMemRefType<T, 1> {
154147
};
155148

156149
/// StridedMemRef descriptor type specialized for rank 0.
157-
template <typename T> struct StridedMemRefType<T, 0> {
150+
template <typename T>
151+
struct StridedMemRefType<T, 0> {
158152
T *basePtr;
159153
T *data;
160154
int64_t offset;
@@ -164,7 +158,8 @@ template <typename T> struct StridedMemRefType<T, 0> {
164158
// Codegen-compatible structure for UnrankedMemRef type.
165159
//===----------------------------------------------------------------------===//
166160
// Unranked MemRef
167-
template <typename T> struct UnrankedMemRefType {
161+
template <typename T>
162+
struct UnrankedMemRefType {
168163
int64_t rank;
169164
void *descriptor;
170165
};

0 commit comments

Comments
 (0)