[Mlir-commits] [mlir] [mlir][memref] Define interfaces for ops that access memrefs at an index (PR #177013)

Krzysztof Drewniak llvmlistbot at llvm.org
Wed Jan 28 10:43:41 PST 2026


================
@@ -0,0 +1,198 @@
+//===-- MemoryAccessOpInterfaces.td ------------------------*- tablegen -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MEMREF_MEMORY_ACCESS_OP_INTERFACES
+#define MEMREF_MEMORY_ACCESS_OP_INTERFACES
+
+include "mlir/IR/OpBase.td"
+
+def IndexedAccessOpInterface : OpInterface<"IndexedAccessOpInterface"> {
+  let description = [{
+    An interface for operations that access (by loading from or
+    storing to, atomically modifying, or otherwise) memory located at an
+    index within a memref whose semantics don't depend on the indexing scheme.
+
+    That is, a direct access op is one where, if `%b[%j0, %j1, ..., %jL]`
+    points to the same memory as `%a[%i0, %i1, ... %iK]`, it would be
+    legal to replace `%a[%i0, ..., %iK]` with %b[%j0, ... %jL]`.
+
+    Operations may impose constraints on allowable reindexings.
+    Returning a non-empty result from `getAccessedShape()` imposes constraints
+    on the dimensions whose strides need to be preserved.
+
+    This interface is intended to enable transformations such as folding in
+    aliasing operations (like `memref.subview` or `memref.collapse_shape`) or
+    linearizing memrefs (making them 1-D) to be generic over in-tree and
+    out-of-tree operations.
+  }];
+  let cppNamespace = "::mlir::memref";
+  let methods =
+    [InterfaceMethod<
+      /*desc=*/[{
+        Return the accessed memref. If the operation is still in tensor form, return
+        the null value.
+      }],
+      /*retType=*/"::mlir::TypedValue<::mlir::MemRefType>",
+      /*methodName=*/"getAccessedMemref",
+      /*args=*/(ins)>,
+    InterfaceMethod<
+      /*desc=*/[{
+        Return the indices that are used to access the memref returned by `getAccessedMemref()`.
+
+        The size of this range must be equal to the rank of the memref returned by
+        `getAccessedMemref()`.
+      }],
+      /*retType=*/"::mlir::Operation::operand_range",
+      /*methodName=*/"getIndices",
+      /*args=*/(ins)>,
+    InterfaceMethod<
+      /*desc=*/[{
+        Return the shape of the portion of the memref that is being accessed by
+        this operation, if known, ignoring leading unit dimensions.
+
+        Reindexing transformations may not modify the *strides* of the trailing
+        N dimensions, where N is the size returned value, and should ensure that
+        at least N indexing dimensions remain after the transformation.
+      }],
+      /*retType=*/"::llvm::SmallVector<int64_t>",
+      /*methodName=*/"getAccessedShape",
+      /*args=*/(ins),
+      /*methodBody=*/[{}],
+      /*defaultImplementation=*/[{
+        return ::llvm::SmallVector<int64_t>{};
+      }]>,
+    InterfaceMethod<
+      /*desc=*/[{
+        Updates the memref being accessed to `newMemref` and the indices to
+        `newIndices`. If `std::nullopt` is returned, the operation was
+        updated in-place (the common case), while if a vector of values
+        is returned, they should be used to replace the operation being
+        updated.
+
+        This implementation of this method shall use the `modifyOpInPlace` method
+        on the provided rewriter when applicable, and may create or clone operations.
----------------
krzysz00 wrote:

Clarified the wording here by breaking up the sentence

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


More information about the Mlir-commits mailing list