[Mlir-commits] [mlir] [MLIR] Determine contiguousness of memrefs with dynamic dimensions (PR #142421)

Momchil Velikov llvmlistbot at llvm.org
Thu Jun 19 06:56:29 PDT 2025


================
@@ -0,0 +1,210 @@
+//===- LayoutTest.cpp - unit tests related to memref layout ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/IR/AffineMap.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/BuiltinTypes.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+using namespace mlir::memref;
+
+TEST(MemRefLayout, maxContigDim) {
+  MLIRContext ctx;
+  OpBuilder b(&ctx);
+
+  const int64_t _ = ShapedType::kDynamic;
+  const FloatType f32 = b.getF32Type();
+  auto strided = [&ctx](ArrayRef<int64_t> s) {
+    return StridedLayoutAttr::get(&ctx, 0, s);
+  };
+
+  // memref<2x2x2xf32, strided<[4,2,1]>
+  auto m1 = MemRefType::get({2, 2, 2}, f32, strided({4, 2, 1}));
+  EXPECT_EQ(m1.getNumContiguousTrailingDims(), 3);
+
+  // memref<2x2x2xf32, strided<[8,2,1]>
+  auto m2 = MemRefType::get({2, 2, 2}, f32, strided({8, 2, 1}));
+  EXPECT_EQ(m2.getNumContiguousTrailingDims(), 2);
+
+  // memref<2x2x2xf32, strided<[8,4,1]>
+  auto m3 = MemRefType::get({2, 2, 2}, f32, strided({8, 4, 1}));
+  EXPECT_EQ(m3.getNumContiguousTrailingDims(), 1);
+
+  // memref<2x2x2xf32, strided<[8,4,2]>
+  auto m4 = MemRefType::get({2, 2, 2}, f32, strided({8, 4, 2}));
+  EXPECT_EQ(m4.getNumContiguousTrailingDims(), 0);
----------------
momchil-velikov wrote:

The system was described in the (now outdated) commit https://github.com/llvm/llvm-project/pull/142421/commits/145b055b8157443309f8e0879b6c7ba78a44c322


https://github.com/llvm/llvm-project/pull/142421


More information about the Mlir-commits mailing list