[Mlir-commits] [mlir] [mlir][memref] Rewrite scalar `memref.copy` through reinterpret_cast into load/store (PR #186118)
Andrzej WarzyĆski
llvmlistbot at llvm.org
Fri Mar 13 02:42:14 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;
----------------
banach-space wrote:
This will capture `memref.reinterpret_cast` on e.g. `memref<1xf32>`, right? Why reject such cases?
https://github.com/llvm/llvm-project/pull/186118
More information about the Mlir-commits
mailing list