[Mlir-commits] [mlir] [mlir][core|ptr] Add `PtrLikeTypeInterface` and casting ops to the `ptr` dialect (PR #137469)

Fabian Mora llvmlistbot at llvm.org
Sun Apr 27 05:07:09 PDT 2025


================
@@ -63,6 +64,54 @@ def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
       return $_get(memorySpace.getContext(), memorySpace);
     }]>
   ];
+  let extraClassDeclaration = [{
+    // `PtrLikeTypeInterface` interface methods.
+    /// Returns `Type()` as this pointer type is opaque.
+    Type getElementType() const {
+      return Type();
+    }
+    /// Clones the pointer with specified memory space or returns failure
+    /// if an `elementType` was specified or if the memory space doesn't
+    /// implement `MemorySpaceAttrInterface`.
+    FailureOr<PtrLikeTypeInterface> clonePtrWith(Attribute memorySpace,
+      std::optional<Type> elementType) const {
+      if (elementType)
+        return failure();
+      if (auto ms = dyn_cast<MemorySpaceAttrInterface>(memorySpace))
+        return cast<PtrLikeTypeInterface>(get(ms));
+      return failure();
+    }
+    /// `!ptr.ptr` types are seen as ptr-like objects with no metadata.
+    bool hasPtrMetadata() const {
+      return false;
+    }
+  }];
+}
+
+def Ptr_PtrMetadata : Ptr_Type<"PtrMetadata", "ptr_metadata"> {
----------------
fabianmcg wrote:

> What can you do with a metadata SSA value?

At this moment it's mainly used in conjunction with `from_ptr`.
Also, the main reason this was added is because we don't know what's the metadata of a type until it's lowered.

For example, I could create lowerings where the `memref` only has a `ptr` for the buffer and the size information.

> In particular, are there ways to construct a metadata object?

No. It would be up to each dialect owning the `PtrLikeType` to provide such constructors.

However, for things like memref it's always possible to do:

```mlir
%v = from_ptr %ptr trivial_metadata : !ptr.ptr<0> -> memref<f32, 0>
 ... = memref.reinterpret_cast %v ...
```

> is there a way to extract the sizes, strides, offset from a !ptr.ptr_metadata<memref<?x?xf32>>. Such an operation would presumably have to be added to the memref dialect?

Yes, such operation would need to be added to `memref`. Currently the type is opaque to the `ptr` dialect, and it's not possible to extract information.

> Can the memref dialect depend on the ptr dialect?

That would need to get approval and discussion on discourse. IMO, it should or could depend.

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


More information about the Mlir-commits mailing list