[Mlir-commits] [mlir] [MLIR][EmitC] Allow ptrdiff_t as result in sub op (PR #104921)

Marius Brehler llvmlistbot at llvm.org
Tue Aug 20 06:26:08 PDT 2024


https://github.com/marbre updated https://github.com/llvm/llvm-project/pull/104921

>From 7531206bbcd0c290abdf19354691f443669fe2c4 Mon Sep 17 00:00:00 2001
From: Marius Brehler <marius.brehler at amd.com>
Date: Tue, 20 Aug 2024 12:22:27 +0000
Subject: [PATCH 1/2] [MLIR][EmitC] Allow ptrdiff_t as result in sub op

This explicitly allows the `emitc.ptrdiff_t` type for the result of
substrating two pointers and changes the example accordingly.
---
 mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 2 +-
 mlir/lib/Dialect/EmitC/IR/EmitC.cpp         | 6 +++---
 mlir/test/Dialect/EmitC/invalid_ops.mlir    | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 40903b4e288ca6..3221888cb328df 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -895,7 +895,7 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
     %0 = emitc.sub %arg0, %arg1 : (i32, i32) -> i32
     %1 = emitc.sub %arg2, %arg3 : (!emitc.ptr<f32>, i32) -> !emitc.ptr<f32>
     %2 = emitc.sub %arg4, %arg5 : (!emitc.ptr<i32>, !emitc.ptr<i32>)
-        -> !emitc.opaque<"ptrdiff_t">
+        -> !emitc.ptrdiff_t
     ```
     ```c++
     // Code emitted for the operations above.
diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index d731a6756ff630..e30dc04d2f3e94 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -798,9 +798,9 @@ LogicalResult SubOp::verify() {
                        "type if lhs is a pointer");
 
   if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType) &&
-      !isa<IntegerType, emitc::OpaqueType>(resultType))
-    return emitOpError("requires that the result is an integer or of opaque "
-                       "type if lhs and rhs are pointers");
+      !isa<IntegerType, emitc::PtrDiffTType, emitc::OpaqueType>(resultType))
+    return emitOpError("requires that the result is an integer, ptrdiff_t or "
+                       "of opaque type if lhs and rhs are pointers");
   return success();
 }
 
diff --git a/mlir/test/Dialect/EmitC/invalid_ops.mlir b/mlir/test/Dialect/EmitC/invalid_ops.mlir
index 4b5bcf46c1aab9..982cbe2f01ecdc 100644
--- a/mlir/test/Dialect/EmitC/invalid_ops.mlir
+++ b/mlir/test/Dialect/EmitC/invalid_ops.mlir
@@ -219,7 +219,7 @@ func.func @sub_pointer_float(%arg0: !emitc.ptr<f32>, %arg1: f32) {
 // -----
 
 func.func @sub_pointer_pointer(%arg0: !emitc.ptr<f32>, %arg1: !emitc.ptr<f32>) {
-    // expected-error @+1 {{'emitc.sub' op requires that the result is an integer or of opaque type if lhs and rhs are pointers}}
+    // expected-error @+1 {{'emitc.sub' op requires that the result is an integer, ptrdiff_t or of opaque type if lhs and rhs are pointers}}
     %1 = "emitc.sub" (%arg0, %arg1) : (!emitc.ptr<f32>, !emitc.ptr<f32>) -> !emitc.ptr<f32>
     return
 }

>From 02d75b3fb2733c68a6b86db1ce6c888b9e76ef31 Mon Sep 17 00:00:00 2001
From: Marius Brehler <marius.brehler at amd.com>
Date: Tue, 20 Aug 2024 13:25:07 +0000
Subject: [PATCH 2/2] Add positive test

---
 mlir/test/Dialect/EmitC/ops.mlir | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/test/Dialect/EmitC/ops.mlir b/mlir/test/Dialect/EmitC/ops.mlir
index 64f22e8ad6b983..9d5543db0adc00 100644
--- a/mlir/test/Dialect/EmitC/ops.mlir
+++ b/mlir/test/Dialect/EmitC/ops.mlir
@@ -114,6 +114,7 @@ func.func @sub_pointer(%arg0: !emitc.ptr<f32>, %arg1: i32, %arg2: !emitc.opaque<
   %2 = "emitc.sub" (%arg0, %arg2) : (!emitc.ptr<f32>, !emitc.opaque<"unsigned int">) -> !emitc.ptr<f32>
   %3 = "emitc.sub" (%arg0, %arg3) : (!emitc.ptr<f32>, !emitc.ptr<f32>) -> !emitc.opaque<"ptrdiff_t">
   %4 = "emitc.sub" (%arg0, %arg3) : (!emitc.ptr<f32>, !emitc.ptr<f32>) -> i32
+  %5 = "emitc.sub" (%arg0, %arg3) : (!emitc.ptr<f32>, !emitc.ptr<f32>) -> !emitc.ptrdiff_t
   return
 }
 



More information about the Mlir-commits mailing list