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

Andrzej Warzyński llvmlistbot at llvm.org
Wed Jun 18 11:34:04 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);
----------------
banach-space wrote:

> they are systematically created

The “system” you used isn’t obvious to me :)

Could you add a brief comment to document it? That would really help both current and future contributors. Thanks!

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


More information about the Mlir-commits mailing list