[PATCH] D75459: [mlir] Add padding to 1-D Vector in CRunnerUtils.h

Nicolas Vasilache via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 07:41:31 PST 2020


nicolasvasilache updated this revision to Diff 247648.
nicolasvasilache added a comment.

Update assertions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75459/new/

https://reviews.llvm.org/D75459

Files:
  mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
  mlir/include/mlir/ExecutionEngine/RunnerUtils.h


Index: mlir/include/mlir/ExecutionEngine/RunnerUtils.h
===================================================================
--- mlir/include/mlir/ExecutionEngine/RunnerUtils.h
+++ mlir/include/mlir/ExecutionEngine/RunnerUtils.h
@@ -92,7 +92,7 @@
   static_assert(sizeof(val) == M * StaticSizeMult<Dims...>::value * sizeof(T),
                 "Incorrect vector size!");
   // First
-  os << "(" << val.vector[0];
+  os << "(" << val[0];
   if (M > 1)
     os << ", ";
   if (sizeof...(Dims) > 1)
@@ -100,14 +100,14 @@
   // Kernel
   for (unsigned i = 1; i + 1 < M; ++i) {
     printSpace(os, 2 * sizeof...(Dims));
-    os << val.vector[i] << ", ";
+    os << val[i] << ", ";
     if (sizeof...(Dims) > 1)
       os << "\n";
   }
   // Last
   if (M > 1) {
     printSpace(os, sizeof...(Dims));
-    os << val.vector[M - 1];
+    os << val[M - 1];
   }
   os << ")";
 }
Index: mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
===================================================================
--- mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
+++ mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
@@ -39,14 +39,41 @@
 //===----------------------------------------------------------------------===//
 // Codegen-compatible structures for Vector type.
 //===----------------------------------------------------------------------===//
-template <typename T, int Dim, int... Dims>
-struct Vector {
+namespace detail {
+  template <unsigned N> constexpr unsigned nextPowerOf2();
+  template <> constexpr unsigned nextPowerOf2<0>() { return 1; }
+  template <> constexpr unsigned nextPowerOf2<1>() { return 1; }
+  template <unsigned N> constexpr unsigned nextPowerOf2() {
+    return (!(N & (N - 1))) ? N : 2 * nextPowerOf2<(N + 1) / 2>();
+  }
+} // end namespace detail
+
+// N-D vectors recurse down to 1-D.
+template <typename T, int Dim, int... Dims> struct Vector {
+  constexpr Vector<T, Dims...> &operator[](unsigned i) { return vector[i]; }
+  constexpr const Vector<T, Dims...> &operator[](unsigned i) const {
+    return vector[i];
+  }
+
+private:
   Vector<T, Dims...> vector[Dim];
 };
 
-template <typename T, int Dim>
-struct Vector<T, Dim> {
+// 1-D vectors in LLVM are automatically padded to the next power of 2.
+// We insert explicit padding in to account for this.
+template <typename T, int Dim> struct Vector<T, Dim> {
+  Vector() {
+    static_assert(detail::nextPowerOf2<sizeof(T[Dim])>() >= sizeof(T[Dim]),
+                  "size error");
+    static_assert(detail::nextPowerOf2<sizeof(T[Dim])>() < 2 * sizeof(T[Dim]),
+                  "size error");
+  }
+  constexpr T &operator[](unsigned i) { return vector[i]; }
+  constexpr const T &operator[](unsigned i) const { return vector[i]; }
+
+private:
   T vector[Dim];
+  char padding[detail::nextPowerOf2<sizeof(T[Dim])>() - sizeof(T[Dim])];
 };
 
 template <int D1, typename T>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75459.247648.patch
Type: text/x-patch
Size: 2858 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200302/92d28bf2/attachment.bin>


More information about the llvm-commits mailing list