[all-commits] [llvm/llvm-project] f15977: [mlir][core|ptr] Add `PtrLikeTypeInterface` and ca...

Fabian Mora via All-commits all-commits at lists.llvm.org
Fri Jun 20 11:24:00 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: f159774352c37a75829c04febb89f141175fc2bf
      https://github.com/llvm/llvm-project/commit/f159774352c37a75829c04febb89f141175fc2bf
  Author: Fabian Mora <fmora.dev at gmail.com>
  Date:   2025-06-20 (Fri, 20 Jun 2025)

  Changed paths:
    M mlir/include/mlir/Dialect/Ptr/IR/PtrDialect.td
    M mlir/include/mlir/Dialect/Ptr/IR/PtrOps.td
    M mlir/include/mlir/IR/BuiltinTypeInterfaces.td
    M mlir/include/mlir/IR/BuiltinTypes.h
    M mlir/include/mlir/IR/BuiltinTypes.td
    M mlir/lib/Dialect/Ptr/IR/PtrDialect.cpp
    M mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp
    M mlir/lib/IR/BuiltinTypes.cpp
    M mlir/test/Dialect/Ptr/canonicalize.mlir
    A mlir/test/Dialect/Ptr/invalid.mlir
    M mlir/test/Dialect/Ptr/ops.mlir

  Log Message:
  -----------
  [mlir][core|ptr] Add `PtrLikeTypeInterface` and casting ops to the `ptr` dialect (#137469)

This patch adds the `PtrLikeTypeInterface` type interface to identify
pointer-like types. This interface is defined as:

```
A ptr-like type represents an object storing a memory address. This object
is constituted by:
- A memory address called the base pointer. This pointer is treated as a
  bag of bits without any assumed structure. The bit-width of the base
  pointer must be a compile-time constant. However, the bit-width may remain
  opaque or unavailable during transformations that do not depend on the
  base pointer. Finally, it is considered indivisible in the sense that as
  a `PtrLikeTypeInterface` value, it has no metadata.
- Optional metadata about the pointer. For example, the size of the  memory
  region associated with the pointer.

Furthermore, all ptr-like types have two properties:
- The memory space associated with the address held by the pointer.
- An optional element type. If the element type is not specified, the
  pointer is considered opaque.
```

This patch adds this interface to `!ptr.ptr` and the `memref` type.

Furthermore, this patch adds necessary ops and type to handle casting
between `!ptr.ptr` and ptr-like types.

First, it defines the `!ptr.ptr_metadata` type. An opaque type to
represent the metadata of a ptr-like type. The rationale behind adding
this type, is that at high-level the metadata of a type like `memref`
cannot be specified, as its structure is tied to its lowering.

The `ptr.get_metadata` operation was added to extract the opaque pointer
metadata. The concrete structure of the metadata is only known when the
op is lowered.

Finally, this patch adds the `ptr.from_ptr` and `ptr.to_ptr` operations.
Allowing to cast back and forth between `!ptr.ptr` and ptr-like types.

```mlir
func.func @func(%mr: memref<f32, #ptr.generic_space>) -> memref<f32, #ptr.generic_space> {
  %ptr = ptr.to_ptr %mr : memref<f32, #ptr.generic_space> -> !ptr.ptr<#ptr.generic_space>
  %mda = ptr.get_metadata %mr : memref<f32, #ptr.generic_space>
  %res = ptr.from_ptr %ptr metadata %mda : !ptr.ptr<#ptr.generic_space> -> memref<f32, #ptr.generic_space>
  return %res : memref<f32, #ptr.generic_space>
}
```

It's future work to replace and remove the `bare-ptr-convention` through
the use of these ops.

---------

Co-authored-by: Mehdi Amini <joker.eph at gmail.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list