[all-commits] [llvm/llvm-project] 607a3e: [mlir][ptr] Add `ptr.ptr_diff` op

Fabian Mora via All-commits all-commits at lists.llvm.org
Sun Sep 14 10:08:51 PDT 2025


  Branch: refs/heads/users/fabianmcg/ptr-diffop
  Home:   https://github.com/llvm/llvm-project
  Commit: 607a3e0a4d697a8c1562c62be126e5fc32fdd330
      https://github.com/llvm/llvm-project/commit/607a3e0a4d697a8c1562c62be126e5fc32fdd330
  Author: Fabian Mora <6982088+fabianmcg at users.noreply.github.com>
  Date:   2025-09-14 (Sun, 14 Sep 2025)

  Changed paths:
    M mlir/include/mlir/Dialect/Ptr/IR/PtrEnums.td
    M mlir/include/mlir/Dialect/Ptr/IR/PtrOps.td
    M mlir/lib/Dialect/Ptr/IR/PtrDialect.cpp
    M mlir/lib/Target/LLVMIR/Dialect/Ptr/PtrToLLVMIRTranslation.cpp
    M mlir/test/Dialect/Ptr/invalid.mlir
    M mlir/test/Dialect/Ptr/ops.mlir
    M mlir/test/Target/LLVMIR/ptr.mlir

  Log Message:
  -----------
  [mlir][ptr] Add `ptr.ptr_diff` op

Thi patch introduces the `ptr.ptr_diff` operation for computing pointer
differences. The semantics of the operation are given by:
```
The `ptr_diff` operation computes the difference between two pointers,
returning an integer or index value representing the number of bytes
between them. This difference is always computed using signed arithmetic.

The operation supports both scalar and shaped types with value semantics:
- When both operands are scalar: produces a single difference value
- When both are shaped: performs element-wise subtraction,
  shapes must be the same

The operation also supports the following flags:
- `none`: No flags are set.
- `nuw`: No Unsigned Wrap, if the subtraction causes an unsigned overflow,
  the result is a poison value.
- `nsw`: No Signed Wrap, if the subtraction causes a signed overflow, the
  result is a poison value.

NOTE: The pointer difference is calculated using an integer type specified
by the data layout. The final result will be sign-extended or truncated to
fit the result type as necessary.
```

This patch also adds translation to LLVM IR hooks for the `ptr_diff` op.
This translation uses the `ptrtoaddr` builder to compute only index
bits difference.

Example:
```mlir
llvm.func @ptr_diff_vector_i32(%ptrs1: vector<8x!ptr.ptr<#llvm.address_space<0>>>, %ptrs2: vector<8x!ptr.ptr<#llvm.address_space<0>>>) -> vector<8xi32> {
  %diffs = ptr.ptr_diff %ptrs1, %ptrs2 : vector<8x!ptr.ptr<#llvm.address_space<0>>> -> vector<8xi32>
  llvm.return %diffs : vector<8xi32>
}
```
Translation to LLVM IR:
```llvm
define <8 x i32> @ptr_diff_vector_i32(<8 x ptr> %0, <8 x ptr> %1) {
  %3 = ptrtoint <8 x ptr> %0 to <8 x i64>
  %4 = ptrtoint <8 x ptr> %1 to <8 x i64>
  %5 = sub <8 x i64> %3, %4
  %6 = trunc <8 x i64> %5 to <8 x i32>
  ret <8 x i32> %6
}
```


  Commit: 5ea857101e564d493dc11c516a98c3152a6ec204
      https://github.com/llvm/llvm-project/commit/5ea857101e564d493dc11c516a98c3152a6ec204
  Author: Fabian Mora <6982088+fabianmcg at users.noreply.github.com>
  Date:   2025-09-14 (Sun, 14 Sep 2025)

  Changed paths:
    M mlir/lib/Target/LLVMIR/Dialect/Ptr/PtrToLLVMIRTranslation.cpp

  Log Message:
  -----------
  fix non-deterministic IR generation


  Commit: c25dd35390c59203d8f1755d6b1e7c4c12b3c8b5
      https://github.com/llvm/llvm-project/commit/c25dd35390c59203d8f1755d6b1e7c4c12b3c8b5
  Author: Fabian Mora <6982088+fabianmcg at users.noreply.github.com>
  Date:   2025-09-14 (Sun, 14 Sep 2025)

  Changed paths:
    M mlir/lib/Target/LLVMIR/Dialect/Ptr/PtrToLLVMIRTranslation.cpp

  Log Message:
  -----------
  use translate instead of convert


Compare: https://github.com/llvm/llvm-project/compare/a144c1bc399f...c25dd35390c5

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