[Mlir-commits] [mlir] [MLIR][Arith] Add ExpandOps to convertArithToLLVM (PR #117305)

Hugo Trachino llvmlistbot at llvm.org
Thu Nov 28 05:41:56 PST 2024


https://github.com/nujaa updated https://github.com/llvm/llvm-project/pull/117305

>From 56988d29e36b1a40b9bee778e4b20c3e5fe84b10 Mon Sep 17 00:00:00 2001
From: Hugo <hugo.trachino at huawei.com>
Date: Fri, 15 Nov 2024 17:32:45 +0800
Subject: [PATCH 1/5] [MLIR][Arith] Add ExpandOps to convertArithToLLVM

---
 .../Conversion/ArithToLLVM/ArithToLLVM.cpp    |  3 +++
 .../Conversion/ArithToLLVM/arith-to-llvm.mlir | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index aac24f113d891f..b0960c0868b7ae 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -13,6 +13,7 @@
 #include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
 #include "mlir/Conversion/LLVMCommon/VectorPattern.h"
 #include "mlir/Dialect/Arith/IR/Arith.h"
+#include "mlir/Dialect/Arith/Transforms/Passes.h"
 #include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/IR/TypeUtilities.h"
@@ -477,6 +478,7 @@ struct ArithToLLVMConversionPass
       options.overrideIndexBitwidth(indexBitwidth);
 
     LLVMTypeConverter converter(&getContext(), options);
+    mlir::arith::populateArithExpandOpsPatterns(patterns);
     mlir::arith::populateArithToLLVMConversionPatterns(converter, patterns);
 
     if (failed(applyPartialConversion(getOperation(), target,
@@ -503,6 +505,7 @@ struct ArithToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
   void populateConvertToLLVMConversionPatterns(
       ConversionTarget &target, LLVMTypeConverter &typeConverter,
       RewritePatternSet &patterns) const final {
+    mlir::arith::populateArithExpandOpsPatterns(patterns);
     arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
   }
 };
diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index 64c40f1aba43bc..6e45e74e191219 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -568,6 +568,25 @@ func.func @minmaxf(%arg0 : f32, %arg1 : f32) -> f32 {
 
 // -----
 
+// CHECK-LABEL: @ceilops
+func.func @ceilops(%arg0 : index, %arg1 : i32) -> index {
+  // CHECK-NOT: = arith.
+  %0 = arith.ceildivsi %arg0, %arg0 : index
+  %1 = arith.ceildivui %arg1, %arg1 : i32
+  return %0 : index
+}
+
+// -----
+
+// CHECK-LABEL: @floordivsi
+func.func @floordivsi(%arg0 : i32, %arg1 : i32) -> i32 {
+  // CHECK-NOT: = arith.
+  %0 = arith.floordivsi %arg0, %arg1 : i32
+  return %0 : i32
+}
+
+// -----
+
 // CHECK-LABEL: @fastmath
 func.func @fastmath(%arg0: f32, %arg1: f32, %arg2: i32) {
 // CHECK: llvm.fadd %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32

>From 721de90a558c4281f4b0fe6f324a2608a77a8028 Mon Sep 17 00:00:00 2001
From: Hugo <hugo.trachino at huawei.com>
Date: Wed, 20 Nov 2024 10:08:03 +0800
Subject: [PATCH 2/5] fixup! [MLIR][Arith] Add ExpandOps to convertArithToLLVM

---
 .../Conversion/ArithToLLVM/arith-to-llvm.mlir | 58 +++++++++++++------
 1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index 6e45e74e191219..a5a3ae248a3276 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -540,6 +540,45 @@ func.func @select(%arg0 : i1, %arg1 : i32, %arg2 : i32) -> i32 {
 
 // -----
 
+// CHECK-LABEL: @ceildivsi
+func.func @ceildivsi(%arg0 : i64) -> i64 {
+  // CHECK: llvm.icmp "sgt" %arg0, %1 : i64
+  // CHECK: llvm.select %3, %2, %0 : i1, i64
+  // CHECK: llvm.add %4, %arg0 : i64
+  // CHECK: llvm.sdiv %5, %arg0 : i64
+  // CHECK: llvm.add %6, %0 : i64
+  // CHECK: llvm.sub %1, %arg0 : i64
+  // CHECK: llvm.sdiv %8, %arg0 : i64
+  %0 = arith.ceildivsi %arg0, %arg0 : i64
+  return %0: i64
+}
+
+// CHECK-LABEL: @ceildivui
+func.func @ceildivui(%arg0 : i32) -> i32 {
+  // CHECK: llvm.icmp "eq" %arg0, %0 : i32
+  // CHECK: llvm.sub %arg0, %2 : i32
+  // CHECK: llvm.udiv %3, %arg0 : i32
+  // CHECK: llvm.add %4, %2 : i32
+  // CHECK: llvm.select %1, %0, %5 : i1, i32
+  %0 = arith.ceildivui %arg0, %arg0 : i32
+  return %0: i32
+}
+
+// -----
+
+// CHECK-LABEL: @floordivsi
+func.func @floordivsi(%arg0 : i32, %arg1 : i32) -> i32 {
+  // CHECK: %0 = llvm.sdiv %arg0, %arg1 : i32
+  // CHECK: %1 = llvm.mul %0, %arg1 : i32
+  // CHECK: %2 = llvm.icmp "ne" %arg0, %1 : i32
+  // CHECK: %3 = llvm.mlir.constant(0 : i32) : i32
+  // CHECK: %4 = llvm.icmp "slt" %arg0, %3 : i32
+  %0 = arith.floordivsi %arg0, %arg1 : i32
+  return %0 : i32
+}
+
+// -----
+
 // CHECK-LABEL: @minmaxi
 func.func @minmaxi(%arg0 : i32, %arg1 : i32) -> i32 {
   // CHECK: = llvm.intr.smin(%arg0, %arg1) : (i32, i32) -> i32
@@ -568,25 +607,6 @@ func.func @minmaxf(%arg0 : f32, %arg1 : f32) -> f32 {
 
 // -----
 
-// CHECK-LABEL: @ceilops
-func.func @ceilops(%arg0 : index, %arg1 : i32) -> index {
-  // CHECK-NOT: = arith.
-  %0 = arith.ceildivsi %arg0, %arg0 : index
-  %1 = arith.ceildivui %arg1, %arg1 : i32
-  return %0 : index
-}
-
-// -----
-
-// CHECK-LABEL: @floordivsi
-func.func @floordivsi(%arg0 : i32, %arg1 : i32) -> i32 {
-  // CHECK-NOT: = arith.
-  %0 = arith.floordivsi %arg0, %arg1 : i32
-  return %0 : i32
-}
-
-// -----
-
 // CHECK-LABEL: @fastmath
 func.func @fastmath(%arg0: f32, %arg1: f32, %arg2: i32) {
 // CHECK: llvm.fadd %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32

>From a828f5b995c636cade3989ea42ca17c026972f5f Mon Sep 17 00:00:00 2001
From: Hugo <hugo.trachino at huawei.com>
Date: Wed, 20 Nov 2024 10:10:16 +0800
Subject: [PATCH 3/5] fixup! fixup! [MLIR][Arith] Add ExpandOps to
 convertArithToLLVM

---
 mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index b0960c0868b7ae..b1a971e3e9e8eb 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -478,8 +478,8 @@ struct ArithToLLVMConversionPass
       options.overrideIndexBitwidth(indexBitwidth);
 
     LLVMTypeConverter converter(&getContext(), options);
-    mlir::arith::populateArithExpandOpsPatterns(patterns);
-    mlir::arith::populateArithToLLVMConversionPatterns(converter, patterns);
+    arith::populateArithExpandOpsPatterns(patterns);
+    arith::populateArithToLLVMConversionPatterns(converter, patterns);
 
     if (failed(applyPartialConversion(getOperation(), target,
                                       std::move(patterns))))
@@ -505,7 +505,7 @@ struct ArithToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
   void populateConvertToLLVMConversionPatterns(
       ConversionTarget &target, LLVMTypeConverter &typeConverter,
       RewritePatternSet &patterns) const final {
-    mlir::arith::populateArithExpandOpsPatterns(patterns);
+    arith::populateArithExpandOpsPatterns(patterns);
     arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
   }
 };

>From ef9a1ffa04e397d9292f07ee2887476afe4d4f5b Mon Sep 17 00:00:00 2001
From: Hugo <hugo.trachino at huawei.com>
Date: Thu, 21 Nov 2024 10:30:23 +0800
Subject: [PATCH 4/5] fixup! fixup! fixup! [MLIR][Arith] Add ExpandOps to
 convertArithToLLVM

---
 .../Conversion/ArithToLLVM/arith-to-llvm.mlir | 57 +++++++++++++------
 1 file changed, 40 insertions(+), 17 deletions(-)

diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index a5a3ae248a3276..a9dcc0a16b3dbd 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -541,25 +541,41 @@ func.func @select(%arg0 : i1, %arg1 : i32, %arg2 : i32) -> i32 {
 // -----
 
 // CHECK-LABEL: @ceildivsi
+// CHECK-SAME: %[[ARG0:.*]]: i64) -> i64
 func.func @ceildivsi(%arg0 : i64) -> i64 {
-  // CHECK: llvm.icmp "sgt" %arg0, %1 : i64
-  // CHECK: llvm.select %3, %2, %0 : i1, i64
-  // CHECK: llvm.add %4, %arg0 : i64
-  // CHECK: llvm.sdiv %5, %arg0 : i64
-  // CHECK: llvm.add %6, %0 : i64
-  // CHECK: llvm.sub %1, %arg0 : i64
-  // CHECK: llvm.sdiv %8, %arg0 : i64
+  // CHECK: %[[CST0:.*]] = llvm.mlir.constant(1 : i64) : i64
+  // CHECK: %[[CST1:.*]] = llvm.mlir.constant(0 : i64) : i64
+  // CHECK: %[[CST2:.*]] = llvm.mlir.constant(-1 : i64) : i64
+  // CHECK: %[[CMP0:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
+  // CHECK: %[[SEL0:.*]] = llvm.select %[[CMP0]], %[[CST2]], %[[CST0]] : i1, i64
+  // CHECK: %[[ADD0:.*]] = llvm.add %[[SEL0]], %[[ARG0]] : i64
+  // CHECK: %[[DIV0:.*]] = llvm.sdiv %[[ADD0]], %[[ARG0]] : i64
+  // CHECK: %[[ADD1:.*]] = llvm.add %[[DIV0]], %[[CST0]] : i64
+  // CHECK: %[[SUB0:.*]] = llvm.sub %[[CST1]], %[[ARG0]] : i64
+  // CHECK: %[[DIV1:.*]] = llvm.sdiv %[[SUB0]], %[[ARG0]] : i64
+  // CHECK: %[[SUB1:.*]] = llvm.sub %[[CST1]], %[[DIV1]] : i64
+  // CHECK: %[[CMP1:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
+  // CHECK: %[[CMP2:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
+  // CHECK: %[[CMP3:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST1]] : i64
+  // CHECK: %[[CMP4:.*]] = llvm.icmp "sgt" %[[ARG0]], %[[CST1]] : i64
+  // CHECK: %[[AND0:.*]] = llvm.and %[[CMP1]], %[[CMP3]] : i1
+  // CHECK: %[[AND1:.*]] = llvm.and %[[CMP2]], %[[CMP4]] : i1
+  // CHECK: %[[OR:.*]] = llvm.or %[[AND0]], %[[AND1]] : i1
+  // CHECK: %[[SEL1:.*]] = llvm.select %[[OR]], %[[ADD1]], %[[SUB1]] : i1, i64
   %0 = arith.ceildivsi %arg0, %arg0 : i64
   return %0: i64
 }
 
 // CHECK-LABEL: @ceildivui
+// CHECK-SAME: %[[ARG0:.*]]: i32) -> i32
 func.func @ceildivui(%arg0 : i32) -> i32 {
-  // CHECK: llvm.icmp "eq" %arg0, %0 : i32
-  // CHECK: llvm.sub %arg0, %2 : i32
-  // CHECK: llvm.udiv %3, %arg0 : i32
-  // CHECK: llvm.add %4, %2 : i32
-  // CHECK: llvm.select %1, %0, %5 : i1, i32
+// CHECK: %[[CST0:.*]] = llvm.mlir.constant(0 : i32) : i32
+// CHECK: %[[CMP0:.*]] = llvm.icmp "eq" %[[ARG0]], %[[CST0]] : i32
+// CHECK: %[[CST1:.*]] = llvm.mlir.constant(1 : i32) : i32
+// CHECK: %[[SUB0:.*]] = llvm.sub %[[ARG0]], %[[CST1]] : i32
+// CHECK: %[[DIV0:.*]] = llvm.udiv %[[SUB0]], %[[ARG0]] : i32
+// CHECK: %[[ADD0:.*]] = llvm.add %[[DIV0]], %[[CST1]] : i32
+// CHECK: %[[SEL0:.*]] = llvm.select %[[CMP0]], %[[CST0]], %[[ADD0]] : i1, i32
   %0 = arith.ceildivui %arg0, %arg0 : i32
   return %0: i32
 }
@@ -567,12 +583,19 @@ func.func @ceildivui(%arg0 : i32) -> i32 {
 // -----
 
 // CHECK-LABEL: @floordivsi
+// CHECK-SAME: %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32) -> i32
 func.func @floordivsi(%arg0 : i32, %arg1 : i32) -> i32 {
-  // CHECK: %0 = llvm.sdiv %arg0, %arg1 : i32
-  // CHECK: %1 = llvm.mul %0, %arg1 : i32
-  // CHECK: %2 = llvm.icmp "ne" %arg0, %1 : i32
-  // CHECK: %3 = llvm.mlir.constant(0 : i32) : i32
-  // CHECK: %4 = llvm.icmp "slt" %arg0, %3 : i32
+  // CHECK: %[[SDIV:.*]] = llvm.sdiv %[[ARG0]], %[[ARG1]] : i32
+  // CHECK: %[[MUL0:.*]] = llvm.mul %[[SDIV]], %[[ARG1]] : i32
+  // CHECK: %[[CMP0:.*]] = llvm.icmp "ne" %[[ARG0]], %[[MUL0]] : i32
+  // CHECK: %[[CST0:.*]] = llvm.mlir.constant(0 : i32) : i32
+  // CHECK: %[[CMP1:.*]] = llvm.icmp "slt" %[[ARG0]], %[[CST0]] : i32
+  // CHECK: %[[CMP2:.*]] = llvm.icmp "slt" %[[ARG1]], %[[CST0]] : i32
+  // CHECK: %[[CMP3:.*]] = llvm.icmp "ne" %[[CMP1]], %[[CMP2]] : i1
+  // CHECK: %[[AND:.*]] = llvm.and %[[CMP0]], %[[CMP3]] : i1
+  // CHECK: %[[CST1:.*]] = llvm.mlir.constant(-1 : i32) : i32
+  // CHECK: %[[ADD:.*]] = llvm.add %[[SDIV]], %[[CST1]] : i32
+  // CHECK: %[[SEL:.*]] = llvm.select %[[AND]], %[[ADD]], %[[SDIV]] : i1, i32
   %0 = arith.floordivsi %arg0, %arg1 : i32
   return %0 : i32
 }

>From e8935794e0360ab602b275c7ea94211fc4e44c52 Mon Sep 17 00:00:00 2001
From: Hugo <hugo.trachino at huawei.com>
Date: Thu, 28 Nov 2024 21:41:41 +0800
Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! [MLIR][Arith] Add ExpandOps
 to convertArithToLLVM

---
 mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index b1a971e3e9e8eb..531166265544e5 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -478,7 +478,7 @@ struct ArithToLLVMConversionPass
       options.overrideIndexBitwidth(indexBitwidth);
 
     LLVMTypeConverter converter(&getContext(), options);
-    arith::populateArithExpandOpsPatterns(patterns);
+    arith::populateCeilFloorDivExpandOpsPatterns(patterns);
     arith::populateArithToLLVMConversionPatterns(converter, patterns);
 
     if (failed(applyPartialConversion(getOperation(), target,
@@ -505,7 +505,7 @@ struct ArithToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
   void populateConvertToLLVMConversionPatterns(
       ConversionTarget &target, LLVMTypeConverter &typeConverter,
       RewritePatternSet &patterns) const final {
-    arith::populateArithExpandOpsPatterns(patterns);
+    arith::populateCeilFloorDivExpandOpsPatterns (patterns);
     arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
   }
 };



More information about the Mlir-commits mailing list