[Mlir-commits] [mlir] [mlir][memref] Rewrite scalar `memref.copy` through reinterpret_cast into load/store (PR #186118)

Paul Kirth llvmlistbot at llvm.org
Thu Mar 12 10:40:28 PDT 2026


================
@@ -0,0 +1,205 @@
+//===- BypassReinterpretCast.cpp - Expansion patterns for MemRef operations
+//----------===//
+//
+// 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/Arith/IR/Arith.h"
+#include "mlir/Dialect/Arith/Transforms/Passes.h"
+#include "mlir/Dialect/Arith/Utils/Utils.h"
+#include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/Dialect/MemRef/Transforms/Transforms.h"
+#include "mlir/IR/TypeUtilities.h"
+#include "mlir/Transforms/DialectConversion.h"
+
+namespace mlir {
+namespace memref {
+#define GEN_PASS_DEF_BYPASSREINTERPRETCASTPASS
+#include "mlir/Dialect/MemRef/Transforms/Passes.h.inc"
+} // namespace memref
+} // namespace mlir
+
+using namespace mlir;
+
+namespace {
+
+// Checks if strided memref is an (expanded) scalar view
+// of an (expanded) single row/column memref, with the same rank.
+static bool isScalarSlice(memref::ReinterpretCastOp rc) {
+  auto underlyingType = dyn_cast<MemRefType>(rc.getSource().getType());
+  auto stridedType = dyn_cast<MemRefType>(rc.getType());
+  if (!underlyingType || !stridedType)
+    return false;
+
+  unsigned rank = underlyingType.getRank();
+  if (rank != stridedType.getRank())
+    return false;
+
+  ArrayRef<int64_t> sizes = rc.getStaticSizes();
+  ArrayRef<int64_t> strides = rc.getStaticStrides();
+  if (sizes.size() != rank || strides.size() != rank)
+    return false;
+
+  // Rank-1 special case
+  if (rank == 1) {
+    if (underlyingType.getDimSize(0) == 1)
+      return false;
+
+    if (strides[0] != underlyingType.getDimSize(0))
+      return false;
+
+    return true;
----------------
ilovepi wrote:

```suggestion
    return strides[0] != underlyingType.getDimSize(0);
```


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


More information about the Mlir-commits mailing list