[Mlir-commits] [mlir] [mlir][arith] Add comparison integration tests (PR #96974)

Jacob Yu llvmlistbot at llvm.org
Wed Aug 21 05:58:35 PDT 2024


https://github.com/pingshiyu updated https://github.com/llvm/llvm-project/pull/96974

>From d7602dbf472727dee99b128098bc258d7b913999 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Thu, 27 Jun 2024 22:57:37 +0100
Subject: [PATCH 01/12] adding comparison unit tests

---
 .../Dialect/Arith/CPU/comparison.mlir         | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
new file mode 100644
index 00000000000000..85b70b2cad5a00
--- /dev/null
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -0,0 +1,61 @@
+// Tests comparison operations.
+// These tests are intended to be target agnostic: they should yield the same results 
+// regardless of the target platform.
+
+// RUN: mlir-opt %s --convert-scf-to-cf --convert-cf-to-llvm --convert-vector-to-llvm \
+// RUN:             --convert-func-to-llvm --convert-arith-to-llvm | \
+// RUN:   mlir-cpu-runner -e entry -entry-point-result=void \
+// RUN:                   --shared-libs=%mlir_c_runner_utils | \
+// RUN:   FileCheck %s --match-full-lines
+
+func.func @signed_comparison_on_i1s() {
+    // signed comparisons on i1s
+    // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
+    // CHECK:      0
+    // CHECK-NEXT: 0
+    // CHECK-NEXT: 1
+    // CHECK-NEXT: 1
+    %false = arith.constant false
+    %true = arith.constant true
+    %0 = arith.cmpi slt, %false, %true : i1
+    %1 = arith.cmpi sle, %false, %true : i1
+    %2 = arith.cmpi sgt, %false, %true : i1
+    %3 = arith.cmpi sge, %false, %true : i1
+    vector.print %0 : i1
+    vector.print %1 : i1
+    vector.print %2 : i1
+    vector.print %3 : i1
+    return
+}
+
+func.func @sge_0_1_is_true() {
+    // sge 0 -1, sge 0 1, should be true
+    // sge 0 -1 == sge 0 1 == true (1)
+    // CHECK-NEXT: 1
+    // CHECK-NEXT: 1
+    %false = arith.constant 0 : i1
+    %true = arith.constant 1 : i1
+    %true_0 = arith.constant -1 : i1
+    %0 = arith.cmpi sge, %false, %true : i1
+    %1 = arith.cmpi sge, %false, %true_0 : i1
+    vector.print %0 : i1
+    vector.print %1 : i1
+    return
+}
+
+func.func @zero_ult_min_index() {
+    // 0 `ult` -2^63 = true
+    // CHECK-NEXT: 1
+    %c0 = arith.constant 0 : index
+    %c-9223372036854775808 = arith.constant -9223372036854775808 : index
+    %0 = arith.cmpi ult, %c0, %c-9223372036854775808 : index
+    vector.print %0 : i1
+    return
+}
+
+func.func @entry() {
+    func.call @signed_comparison_on_i1s() : () -> ()
+    func.call @sge_0_1_is_true() : () -> ()
+    func.call @zero_ult_min_index() : () -> ()
+    return
+}

>From 4909f9ee497218eb43e25e0395028e95638fb0c9 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Fri, 28 Jun 2024 12:28:15 +0100
Subject: [PATCH 02/12] reindented

---
 .../Dialect/Arith/CPU/comparison.mlir         | 80 +++++++++----------
 1 file changed, 40 insertions(+), 40 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 85b70b2cad5a00..83fa7d7cbe5090 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -9,53 +9,53 @@
 // RUN:   FileCheck %s --match-full-lines
 
 func.func @signed_comparison_on_i1s() {
-    // signed comparisons on i1s
-    // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
-    // CHECK:      0
-    // CHECK-NEXT: 0
-    // CHECK-NEXT: 1
-    // CHECK-NEXT: 1
-    %false = arith.constant false
-    %true = arith.constant true
-    %0 = arith.cmpi slt, %false, %true : i1
-    %1 = arith.cmpi sle, %false, %true : i1
-    %2 = arith.cmpi sgt, %false, %true : i1
-    %3 = arith.cmpi sge, %false, %true : i1
-    vector.print %0 : i1
-    vector.print %1 : i1
-    vector.print %2 : i1
-    vector.print %3 : i1
-    return
+  // signed comparisons on i1s
+  // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
+  // CHECK:      0
+  // CHECK-NEXT: 0
+  // CHECK-NEXT: 1
+  // CHECK-NEXT: 1
+  %false = arith.constant false
+  %true = arith.constant true
+  %0 = arith.cmpi slt, %false, %true : i1
+  %1 = arith.cmpi sle, %false, %true : i1
+  %2 = arith.cmpi sgt, %false, %true : i1
+  %3 = arith.cmpi sge, %false, %true : i1
+  vector.print %0 : i1
+  vector.print %1 : i1
+  vector.print %2 : i1
+  vector.print %3 : i1
+  return
 }
 
 func.func @sge_0_1_is_true() {
-    // sge 0 -1, sge 0 1, should be true
-    // sge 0 -1 == sge 0 1 == true (1)
-    // CHECK-NEXT: 1
-    // CHECK-NEXT: 1
-    %false = arith.constant 0 : i1
-    %true = arith.constant 1 : i1
-    %true_0 = arith.constant -1 : i1
-    %0 = arith.cmpi sge, %false, %true : i1
-    %1 = arith.cmpi sge, %false, %true_0 : i1
-    vector.print %0 : i1
-    vector.print %1 : i1
-    return
+  // sge 0 -1, sge 0 1, should be true
+  // sge 0 -1 == sge 0 1 == true (1)
+  // CHECK-NEXT: 1
+  // CHECK-NEXT: 1
+  %false = arith.constant 0 : i1
+  %true = arith.constant 1 : i1
+  %true_0 = arith.constant -1 : i1
+  %0 = arith.cmpi sge, %false, %true : i1
+  %1 = arith.cmpi sge, %false, %true_0 : i1
+  vector.print %0 : i1
+  vector.print %1 : i1
+  return
 }
 
 func.func @zero_ult_min_index() {
-    // 0 `ult` -2^63 = true
-    // CHECK-NEXT: 1
-    %c0 = arith.constant 0 : index
-    %c-9223372036854775808 = arith.constant -9223372036854775808 : index
-    %0 = arith.cmpi ult, %c0, %c-9223372036854775808 : index
-    vector.print %0 : i1
-    return
+  // 0 `ult` -2^63 = true
+  // CHECK-NEXT: 1
+  %c0 = arith.constant 0 : index
+  %c-9223372036854775808 = arith.constant -9223372036854775808 : index
+  %0 = arith.cmpi ult, %c0, %c-9223372036854775808 : index
+  vector.print %0 : i1
+  return
 }
 
 func.func @entry() {
-    func.call @signed_comparison_on_i1s() : () -> ()
-    func.call @sge_0_1_is_true() : () -> ()
-    func.call @zero_ult_min_index() : () -> ()
-    return
+  func.call @signed_comparison_on_i1s() : () -> ()
+  func.call @sge_0_1_is_true() : () -> ()
+  func.call @zero_ult_min_index() : () -> ()
+  return
 }

>From ca0e890184e51d696d98106e8a1eea0c078c015d Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Tue, 2 Jul 2024 22:19:56 +0100
Subject: [PATCH 03/12] comparison adopting format of previous other tests

---
 .../Dialect/Arith/CPU/comparison.mlir         | 133 +++++++++++++-----
 1 file changed, 99 insertions(+), 34 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 83fa7d7cbe5090..da0b923a27a0c1 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -8,54 +8,119 @@
 // RUN:                   --shared-libs=%mlir_c_runner_utils | \
 // RUN:   FileCheck %s --match-full-lines
 
-func.func @signed_comparison_on_i1s() {
+func.func @slt_cmpi_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@slt_cmpi_i1\n"
+  %res = arith.cmpi slt, %v1, %v2 : i1
+  vector.print %res : i1
+  return
+}
+
+func.func @sle_cmpi_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@sle_cmpi_i1\n"
+  %res = arith.cmpi sle, %v1, %v2 : i1
+  vector.print %res : i1
+  return
+}
+
+func.func @sgt_cmpi_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@sgt_cmpi_i1\n"
+  %res = arith.cmpi sgt, %v1, %v2 : i1
+  vector.print %res : i1
+  return
+}
+
+func.func @sge_cmpi_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@sge_cmpi_i1\n"
+  %res = arith.cmpi sge, %v1, %v2 : i1
+  vector.print %res : i1
+  return
+}
+
+func.func @signed_cmpi() {
+  // ------------------------------------------------
+  // Test i1
+  // ------------------------------------------------
+  %false_i1 = arith.constant 0 : i1
+  %true_i1 = arith.constant 1 : i1
+  %true_i1_n1 = arith.constant -1 : i1
+
+  // sge 0 -1, sge 0 1, should be true
+  // sge 0 -1 == sge 0 1 == true (1)
+
+  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-NEXT:  1
+  func.call @sge_cmpi_i1(%false_i1, %true_i1_n1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-NEXT:  1
+  func.call @sge_cmpi_i1(%false_i1, %true_i1) : (i1, i1) -> ()
+
+  %false = arith.constant false
+  %true = arith.constant true
+
   // signed comparisons on i1s
   // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
-  // CHECK:      0
+
+  // CHECK-LABEL: @slt_cmpi_i1
+  // CHECK-NEXT:  0
+  func.call @slt_cmpi_i1(%false, %true) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @sle_cmpi_i1
+  // CHECK-NEXT:  0
+  func.call @sle_cmpi_i1(%false, %true) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @sgt_cmpi_i1
+  // CHECK-NEXT:  1
+  func.call @sgt_cmpi_i1(%false, %true) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-NEXT:  1
+  func.call @sge_cmpi_i1(%false, %true) : (i1, i1) -> ()
+
+  // check that addui_extended overflow bit is treated as -1 in comparison operations
+  //  in the case of an overflow
+  // addui_extended -1 -1 = (..., overflow_bit) 
+  // assert(overflow_bit <= 0)
+  %n1_i64 = arith.constant -1 : i64
+  %sum, %overflow = arith.addui_extended %n1_i64, %n1_i64 : i64, i1
+
+  // CHECK-LABEL: @sge_cmpi_i1
   // CHECK-NEXT: 0
-  // CHECK-NEXT: 1
-  // CHECK-NEXT: 1
-  %false = arith.constant false
-  %true = arith.constant true
-  %0 = arith.cmpi slt, %false, %true : i1
-  %1 = arith.cmpi sle, %false, %true : i1
-  %2 = arith.cmpi sgt, %false, %true : i1
-  %3 = arith.cmpi sge, %false, %true : i1
-  vector.print %0 : i1
-  vector.print %1 : i1
-  vector.print %2 : i1
-  vector.print %3 : i1
+  func.call @sge_cmpi_i1(%overflow, %false) : (i1, i1) -> ()
+  
+  // ------------------------------------------------
+  // Test i8, i16 etc.. TODO
+  // ------------------------------------------------
   return
 }
 
-func.func @sge_0_1_is_true() {
-  // sge 0 -1, sge 0 1, should be true
-  // sge 0 -1 == sge 0 1 == true (1)
-  // CHECK-NEXT: 1
-  // CHECK-NEXT: 1
-  %false = arith.constant 0 : i1
-  %true = arith.constant 1 : i1
-  %true_0 = arith.constant -1 : i1
-  %0 = arith.cmpi sge, %false, %true : i1
-  %1 = arith.cmpi sge, %false, %true_0 : i1
-  vector.print %0 : i1
-  vector.print %1 : i1
+func.func @ult_cmpi_index(%v1 : index, %v2 : index) {
+  vector.print str "@ult_cmpi_index\n"
+  %res = arith.cmpi ult, %v1, %v2 : index
+  vector.print %res : i1
   return
 }
 
-func.func @zero_ult_min_index() {
+func.func @unsigned_cmpi() {
+  // ------------------------------------------------
+  // Test index
+  // ------------------------------------------------
   // 0 `ult` -2^63 = true
+  %zero = arith.constant 0 : index
+  %index_min = arith.constant -9223372036854775808 : index
+
+  // CHECK-LABEL: @ult_cmpi_index
   // CHECK-NEXT: 1
-  %c0 = arith.constant 0 : index
-  %c-9223372036854775808 = arith.constant -9223372036854775808 : index
-  %0 = arith.cmpi ult, %c0, %c-9223372036854775808 : index
-  vector.print %0 : i1
+  func.call @ult_cmpi_index(%zero, %index_min) : (index, index) -> ()
+  
+  // ------------------------------------------------
+  // Test i1, i8, i16 etc.. TODO
+  // ------------------------------------------------
   return
 }
 
 func.func @entry() {
-  func.call @signed_comparison_on_i1s() : () -> ()
-  func.call @sge_0_1_is_true() : () -> ()
-  func.call @zero_ult_min_index() : () -> ()
+  func.call @signed_cmpi() : () -> ()
+  func.call @unsigned_cmpi() : () -> ()
   return
 }

>From f48f8809e886280de2e0a9b36de76fa221e44a58 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Sat, 6 Jul 2024 15:05:01 +0100
Subject: [PATCH 04/12] updated based on comments

---
 .../Dialect/Arith/CPU/comparison.mlir         | 69 +++++++++----------
 1 file changed, 33 insertions(+), 36 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index da0b923a27a0c1..4a701e2c825a33 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -1,42 +1,38 @@
-// Tests comparison operations.
-// These tests are intended to be target agnostic: they should yield the same results 
-// regardless of the target platform.
-
 // RUN: mlir-opt %s --convert-scf-to-cf --convert-cf-to-llvm --convert-vector-to-llvm \
 // RUN:             --convert-func-to-llvm --convert-arith-to-llvm | \
 // RUN:   mlir-cpu-runner -e entry -entry-point-result=void \
 // RUN:                   --shared-libs=%mlir_c_runner_utils | \
 // RUN:   FileCheck %s --match-full-lines
 
-func.func @slt_cmpi_i1(%v1 : i1, %v2 : i1) {
-  vector.print str "@slt_cmpi_i1\n"
+func.func @cmpi_slt_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@cmpi_slt_i1\n"
   %res = arith.cmpi slt, %v1, %v2 : i1
   vector.print %res : i1
   return
 }
 
-func.func @sle_cmpi_i1(%v1 : i1, %v2 : i1) {
-  vector.print str "@sle_cmpi_i1\n"
+func.func @cmpi_sle_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@cmpi_sle_i1\n"
   %res = arith.cmpi sle, %v1, %v2 : i1
   vector.print %res : i1
   return
 }
 
-func.func @sgt_cmpi_i1(%v1 : i1, %v2 : i1) {
-  vector.print str "@sgt_cmpi_i1\n"
+func.func @cmpi_sgt_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@cmpi_sgt_i1\n"
   %res = arith.cmpi sgt, %v1, %v2 : i1
   vector.print %res : i1
   return
 }
 
-func.func @sge_cmpi_i1(%v1 : i1, %v2 : i1) {
-  vector.print str "@sge_cmpi_i1\n"
+func.func @cmpi_sge_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@cmpi_sge_i1\n"
   %res = arith.cmpi sge, %v1, %v2 : i1
   vector.print %res : i1
   return
 }
 
-func.func @signed_cmpi() {
+func.func @cmpi_signed() {
   // ------------------------------------------------
   // Test i1
   // ------------------------------------------------
@@ -45,15 +41,16 @@ func.func @signed_cmpi() {
   %true_i1_n1 = arith.constant -1 : i1
 
   // sge 0 -1, sge 0 1, should be true
+  // since the bitvector `1` is interpreted as the int value -1 in signed comparisons
   // sge 0 -1 == sge 0 1 == true (1)
 
-  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
-  func.call @sge_cmpi_i1(%false_i1, %true_i1_n1) : (i1, i1) -> ()
+  func.call @cmpi_sge_i1(%false_i1, %true_i1_n1) : (i1, i1) -> ()
 
-  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
-  func.call @sge_cmpi_i1(%false_i1, %true_i1) : (i1, i1) -> ()
+  func.call @cmpi_sge_i1(%false_i1, %true_i1) : (i1, i1) -> ()
 
   %false = arith.constant false
   %true = arith.constant true
@@ -61,21 +58,21 @@ func.func @signed_cmpi() {
   // signed comparisons on i1s
   // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
 
-  // CHECK-LABEL: @slt_cmpi_i1
+  // CHECK-LABEL: @cmpi_slt_i1
   // CHECK-NEXT:  0
-  func.call @slt_cmpi_i1(%false, %true) : (i1, i1) -> ()
+  func.call @cmpi_slt_i1(%false, %true) : (i1, i1) -> ()
 
-  // CHECK-LABEL: @sle_cmpi_i1
+  // CHECK-LABEL: @cmpi_sle_i1
   // CHECK-NEXT:  0
-  func.call @sle_cmpi_i1(%false, %true) : (i1, i1) -> ()
+  func.call @cmpi_sle_i1(%false, %true) : (i1, i1) -> ()
 
-  // CHECK-LABEL: @sgt_cmpi_i1
+  // CHECK-LABEL: @cmpi_sgt_i1
   // CHECK-NEXT:  1
-  func.call @sgt_cmpi_i1(%false, %true) : (i1, i1) -> ()
+  func.call @cmpi_sgt_i1(%false, %true) : (i1, i1) -> ()
 
-  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
-  func.call @sge_cmpi_i1(%false, %true) : (i1, i1) -> ()
+  func.call @cmpi_sge_i1(%false, %true) : (i1, i1) -> ()
 
   // check that addui_extended overflow bit is treated as -1 in comparison operations
   //  in the case of an overflow
@@ -84,24 +81,24 @@ func.func @signed_cmpi() {
   %n1_i64 = arith.constant -1 : i64
   %sum, %overflow = arith.addui_extended %n1_i64, %n1_i64 : i64, i1
 
-  // CHECK-LABEL: @sge_cmpi_i1
+  // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT: 0
-  func.call @sge_cmpi_i1(%overflow, %false) : (i1, i1) -> ()
+  func.call @cmpi_sge_i1(%overflow, %false) : (i1, i1) -> ()
   
   // ------------------------------------------------
-  // Test i8, i16 etc.. TODO
+  // TODO: Test i8, i16 etc..
   // ------------------------------------------------
   return
 }
 
-func.func @ult_cmpi_index(%v1 : index, %v2 : index) {
-  vector.print str "@ult_cmpi_index\n"
+func.func @cmpi_ult_index(%v1 : index, %v2 : index) {
+  vector.print str "@cmpi_ult_index\n"
   %res = arith.cmpi ult, %v1, %v2 : index
   vector.print %res : i1
   return
 }
 
-func.func @unsigned_cmpi() {
+func.func @cmpi_unsigned() {
   // ------------------------------------------------
   // Test index
   // ------------------------------------------------
@@ -109,18 +106,18 @@ func.func @unsigned_cmpi() {
   %zero = arith.constant 0 : index
   %index_min = arith.constant -9223372036854775808 : index
 
-  // CHECK-LABEL: @ult_cmpi_index
+  // CHECK-LABEL: @cmpi_ult_index
   // CHECK-NEXT: 1
-  func.call @ult_cmpi_index(%zero, %index_min) : (index, index) -> ()
+  func.call @cmpi_ult_index(%zero, %index_min) : (index, index) -> ()
   
   // ------------------------------------------------
-  // Test i1, i8, i16 etc.. TODO
+  // TODO: i1, i8, i16, uge, ule etc.. 
   // ------------------------------------------------
   return
 }
 
 func.func @entry() {
-  func.call @signed_cmpi() : () -> ()
-  func.call @unsigned_cmpi() : () -> ()
+  func.call @cmpi_signed() : () -> ()
+  func.call @cmpi_unsigned() : () -> ()
   return
 }

>From 45ccc025e1865c71b7f422d2100ed440b12518a5 Mon Sep 17 00:00:00 2001
From: Jacob Yu <pingshiyu at gmail.com>
Date: Mon, 8 Jul 2024 19:42:51 +0100
Subject: [PATCH 05/12] Update
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir

Co-authored-by: Jakub Kuderski <kubakuderski at gmail.com>
---
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 4a701e2c825a33..255dd87767159f 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -74,8 +74,8 @@ func.func @cmpi_signed() {
   // CHECK-NEXT:  1
   func.call @cmpi_sge_i1(%false, %true) : (i1, i1) -> ()
 
-  // check that addui_extended overflow bit is treated as -1 in comparison operations
-  //  in the case of an overflow
+  // Check that addui_extended overflow bit is treated as -1 in comparison operations
+  // in the case of an overflow.
   // addui_extended -1 -1 = (..., overflow_bit) 
   // assert(overflow_bit <= 0)
   %n1_i64 = arith.constant -1 : i64

>From 13ae1826492aa4490af4ef02c793218f210d2cb6 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Mon, 8 Jul 2024 19:44:43 +0100
Subject: [PATCH 06/12] updates to latest comments #1

---
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 4a701e2c825a33..8d79294c6c2b22 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -55,7 +55,6 @@ func.func @cmpi_signed() {
   %false = arith.constant false
   %true = arith.constant true
 
-  // signed comparisons on i1s
   // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
 
   // CHECK-LABEL: @cmpi_slt_i1

>From 9c6af684b581afb8e830c985fd23896bf901e3f6 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Mon, 8 Jul 2024 19:52:48 +0100
Subject: [PATCH 07/12] comparison updated with new case

---
 .../Integration/Dialect/Arith/CPU/comparison.mlir | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 8d79294c6c2b22..9d43a5c43c2577 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -55,7 +55,7 @@ func.func @cmpi_signed() {
   %false = arith.constant false
   %true = arith.constant true
 
-  // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true
+  // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true, sge 1 0 = false
 
   // CHECK-LABEL: @cmpi_slt_i1
   // CHECK-NEXT:  0
@@ -72,17 +72,10 @@ func.func @cmpi_signed() {
   // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
   func.call @cmpi_sge_i1(%false, %true) : (i1, i1) -> ()
-
-  // check that addui_extended overflow bit is treated as -1 in comparison operations
-  //  in the case of an overflow
-  // addui_extended -1 -1 = (..., overflow_bit) 
-  // assert(overflow_bit <= 0)
-  %n1_i64 = arith.constant -1 : i64
-  %sum, %overflow = arith.addui_extended %n1_i64, %n1_i64 : i64, i1
-
+  
   // CHECK-LABEL: @cmpi_sge_i1
-  // CHECK-NEXT: 0
-  func.call @cmpi_sge_i1(%overflow, %false) : (i1, i1) -> ()
+  // CHECK-NEXT:  0
+  func.call @cmpi_sge_i1(%true, %false) : (i1, i1) -> ()
   
   // ------------------------------------------------
   // TODO: Test i8, i16 etc..

>From 9d509b3b6d3fad17b8abddc9d78fe64ec161e570 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Thu, 11 Jul 2024 17:08:27 +0100
Subject: [PATCH 08/12] more tests added, comments updated

---
 .../Dialect/Arith/CPU/comparison.mlir          | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 9d43a5c43c2577..c5378aa6064313 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -4,6 +4,13 @@
 // RUN:                   --shared-libs=%mlir_c_runner_utils | \
 // RUN:   FileCheck %s --match-full-lines
 
+func.func @cmpi_eq_i1(%v1 : i1, %v2 : i1) {
+  vector.print str "@cmpi_eq_i1\n"
+  %res = arith.cmpi eq, %v1, %v2 : i1
+  vector.print %res : i1
+  return
+}
+
 func.func @cmpi_slt_i1(%v1 : i1, %v2 : i1) {
   vector.print str "@cmpi_slt_i1\n"
   %res = arith.cmpi slt, %v1, %v2 : i1
@@ -40,9 +47,12 @@ func.func @cmpi_signed() {
   %true_i1 = arith.constant 1 : i1
   %true_i1_n1 = arith.constant -1 : i1
 
-  // sge 0 -1, sge 0 1, should be true
-  // since the bitvector `1` is interpreted as the int value -1 in signed comparisons
-  // sge 0 -1 == sge 0 1 == true (1)
+  // int values 1 and -1 represent the same bitvector
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%true_i1, %true_i1_n1) : (i1, i1) -> ()
+
+  // But, bitvector `1` is interpreted as int value -1 in signed comparison
 
   // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
@@ -55,8 +65,6 @@ func.func @cmpi_signed() {
   %false = arith.constant false
   %true = arith.constant true
 
-  // slt 0 1 = false, sle 0 1 = false, sgt 0 1 = true, sge 0 1 = true, sge 1 0 = false
-
   // CHECK-LABEL: @cmpi_slt_i1
   // CHECK-NEXT:  0
   func.call @cmpi_slt_i1(%false, %true) : (i1, i1) -> ()

>From d68f34b6b5b77981dc3eaa6b18d29f03fb77cebc Mon Sep 17 00:00:00 2001
From: Jacob Yu <pingshiyu at gmail.com>
Date: Tue, 23 Jul 2024 14:26:38 +0100
Subject: [PATCH 09/12] Update
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Andrzej WarzyƄski <andrzej.warzynski at gmail.com>
---
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index c5378aa6064313..636ea42e031eec 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -47,7 +47,7 @@ func.func @cmpi_signed() {
   %true_i1 = arith.constant 1 : i1
   %true_i1_n1 = arith.constant -1 : i1
 
-  // int values 1 and -1 represent the same bitvector
+  // int values 1 and -1 are represented with the same bitvector (`0b1`)
   // CHECK-LABEL: @cmpi_eq_i1
   // CHECK-NEXT:  1
   func.call @cmpi_eq_i1(%true_i1, %true_i1_n1) : (i1, i1) -> ()

>From 5c77e4dd96c58bb381d81737fdf0ec13653d5fdf Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Tue, 23 Jul 2024 15:03:32 +0100
Subject: [PATCH 10/12] adding equality tests

---
 .../Dialect/Arith/CPU/comparison.mlir         | 35 ++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index c5378aa6064313..35b3794f4bff83 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -39,7 +39,7 @@ func.func @cmpi_sge_i1(%v1 : i1, %v2 : i1) {
   return
 }
 
-func.func @cmpi_signed() {
+func.func @cmpi_eq() {
   // ------------------------------------------------
   // Test i1
   // ------------------------------------------------
@@ -48,10 +48,42 @@ func.func @cmpi_signed() {
   %true_i1_n1 = arith.constant -1 : i1
 
   // int values 1 and -1 represent the same bitvector
+  
   // CHECK-LABEL: @cmpi_eq_i1
   // CHECK-NEXT:  1
   func.call @cmpi_eq_i1(%true_i1, %true_i1_n1) : (i1, i1) -> ()
 
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  0
+  func.call @cmpi_eq_i1(%false_i1, %true_i1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  0
+  func.call @cmpi_eq_i1(%true_i1, %false_i1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%true_i1, %true_i1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%false_i1, %false_i1) : (i1, i1) -> ()
+
+  // ------------------------------------------------
+  // TODO: Test i8, i16 etc..
+  // ------------------------------------------------
+  return
+}
+
+func.func @cmpi_signed() {
+  // ------------------------------------------------
+  // Test i1
+  // ------------------------------------------------
+  %false_i1 = arith.constant 0 : i1
+  %true_i1 = arith.constant 1 : i1
+  %true_i1_n1 = arith.constant -1 : i1
+
+  // int values 1 and -1 represent the same bitvector
   // But, bitvector `1` is interpreted as int value -1 in signed comparison
 
   // CHECK-LABEL: @cmpi_sge_i1
@@ -117,6 +149,7 @@ func.func @cmpi_unsigned() {
 }
 
 func.func @entry() {
+  func.call @cmpi_eq() : () -> ()
   func.call @cmpi_signed() : () -> ()
   func.call @cmpi_unsigned() : () -> ()
   return

>From e8f8a537bbdfc76858008b32f7eaa19793213d86 Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Tue, 20 Aug 2024 15:20:43 +0100
Subject: [PATCH 11/12] fixup, comments addressed

---
 mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index f816555ba0aa08..64dd624093eed2 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -82,7 +82,7 @@ func.func @cmpi_signed() {
   %true_i1 = arith.constant 1 : i1
   %true_i1_n1 = arith.constant -1 : i1
 
-  // int values 1 and -1 represent the same bitvector
+  // int values 1 and -1 are represented with the same bitvector (`0b1`)
   // But, bitvector `1` is interpreted as int value -1 in signed comparison
 
   // CHECK-LABEL: @cmpi_sge_i1
@@ -92,6 +92,10 @@ func.func @cmpi_signed() {
   // CHECK-LABEL: @cmpi_sge_i1
   // CHECK-NEXT:  1
   func.call @cmpi_sge_i1(%false_i1, %true_i1) : (i1, i1) -> ()
+  
+  // CHECK-LABEL: @cmpi_sge_i1
+  // CHECK-NEXT:  0
+  func.call @cmpi_sge_i1(%true_i1, %false_i1) : (i1, i1) -> ()
 
   %false = arith.constant false
   %true = arith.constant true

>From 5591bc9058bcbcb87e768633c6f644d07fd6768e Mon Sep 17 00:00:00 2001
From: pingshiyu <pingshiyu at gmail.com>
Date: Wed, 21 Aug 2024 13:58:16 +0100
Subject: [PATCH 12/12] addressed comment,s added more tests

---
 .../Dialect/Arith/CPU/comparison.mlir         | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
index 64dd624093eed2..418fbb0c0a94c7 100644
--- a/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
+++ b/mlir/test/Integration/Dialect/Arith/CPU/comparison.mlir
@@ -1,8 +1,8 @@
 // RUN: mlir-opt %s --convert-scf-to-cf --convert-cf-to-llvm --convert-vector-to-llvm \
 // RUN:             --convert-func-to-llvm --convert-arith-to-llvm | \
-// RUN:   mlir-cpu-runner -e entry -entry-point-result=void \
+// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
 // RUN:                   --shared-libs=%mlir_c_runner_utils | \
-// RUN:   FileCheck %s --match-full-lines
+// RUN: FileCheck %s --match-full-lines
 
 func.func @cmpi_eq_i1(%v1 : i1, %v2 : i1) {
   vector.print str "@cmpi_eq_i1\n"
@@ -68,6 +68,21 @@ func.func @cmpi_eq() {
   // CHECK-NEXT:  1
   func.call @cmpi_eq_i1(%false_i1, %false_i1) : (i1, i1) -> ()
 
+  %false = arith.constant false
+  %true = arith.constant true
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%true, %true_i1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%false, %false_i1) : (i1, i1) -> ()
+
+  // CHECK-LABEL: @cmpi_eq_i1
+  // CHECK-NEXT:  1
+  func.call @cmpi_eq_i1(%true, %true_i1_n1) : (i1, i1) -> ()
+
   // ------------------------------------------------
   // TODO: Test i8, i16 etc..
   // ------------------------------------------------



More information about the Mlir-commits mailing list