[Mlir-commits] [mlir] [mlir][Ptr] Add the `MemorySpaceAttrInterface` interface and dependencies. (PR #86870)

Fabian Mora llvmlistbot at llvm.org
Sat Aug 31 09:34:50 PDT 2024


================
@@ -0,0 +1,142 @@
+//===-- MemorySpaceInterfaces.td - Memory space interfaces ----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines memory space attribute interfaces.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef PTR_MEMORYSPACEINTERFACES
+#define PTR_MEMORYSPACEINTERFACES
+
+include "mlir/IR/AttrTypeBase.td"
+include "mlir/IR/OpBase.td"
+
+//===----------------------------------------------------------------------===//
+// Memory space attribute interface.
+//===----------------------------------------------------------------------===//
+
+def MemorySpaceAttrInterface : AttrInterface<"MemorySpaceAttrInterface"> {
+  let description = [{
+    This interface defines a common API for interacting with the memory model of
+    a memory space and the operations in the pointer dialect, giving proper
+    semantical meaning to the ops.
+
+    Furthermore, this interface allows concepts such as read-only memory to be
+    adequately modeled and enforced.
+  }];
+  let cppNamespace = "::mlir::ptr";
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/        [{
+        Returns the default memory space as an attribute.
+      }],
+      /*returnType=*/  "::mlir::ptr::MemorySpaceAttrInterface",
+      /*methodName=*/  "getDefaultMemorySpace"
+    >,
----------------
fabianmcg wrote:

The reasoning behind it is: Let's suppose we have memory spaces `read_only` and `write_only`. The `write_only` space represents a fat pointer with a different DL spec.:
```mlir
dlti.dl_spec = #dlti.dl_spec<
  #dlti.dl_entry<!ptr.ptr<#test.read_only>, #ptr.spec<size = 32, abi = 32, preferred = 64>>,
  #dlti.dl_entry<!ptr.ptr<#test.write_only>, #ptr.spec<size = 128, abi = 128, preferred = 128>>
>
```
Currently a query to the DL with `!ptr.ptr<#test.write_only<1>>` returns:
```mlir
 #dlti.dl_entry<!ptr.ptr<#test.write_only>, #ptr.spec<size = 128, abi = 128, preferred = 128>>
```
If there's no default spec per attribute class, then queries to either `!ptr.ptr<#test.read_only<1>>` or `!ptr.ptr<#test.write_only<1>>` will be invalid. Hence, one would need to list all possible specs for a memory space in the DL, which is possible but not desirable.

So it's not intrinsically relevant for all attributes, but simplifies querying the DL. However, we can remove it.

An alternative is adding an additional interface: `MemorySpaceWithDefaultSpace`

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


More information about the Mlir-commits mailing list