[Mlir-commits] [mlir] [mlir][arith] Improve Lowering of `maxf`/`minf` ops (PR #65213)

Daniil Dudkin llvmlistbot at llvm.org
Wed Sep 6 11:06:45 PDT 2023


https://github.com/unterumarmung updated https://github.com/llvm/llvm-project/pull/65213:

>From fa23dac9fd07b1e06f502e7a6275ae6e894b5d9a Mon Sep 17 00:00:00 2001
From: Daniil Dudkin <unterumarmung at yandex.ru>
Date: Fri, 25 Aug 2023 20:44:06 +0300
Subject: [PATCH] [mlir][arith] Improve Lowering of `maxf`/`minf` ops

This patch is part of a larger initiative aimed at fixing floating-point `max` and `min` operations in MLIR: https://discourse.llvm.org/t/rfc-fix-floating-point-max-and-min-operations-in-mlir/72671.

This patch addresses task 1.1 from the plan. It involves modifying the lowering process for `arith.minf` and `arith.maxf` operations. Specifically, the change replaces the usage of `llvm.minnum` and `llvm.maxnum` with `llvm.minimum` and `llvm.maximum`, respectively. This adjustment is necessary because the `m**num` intrinsics are not suitable for the mentioned MLIR operations due to semantic discrepancies in handling NaNs, positive and negative floating-point zeros.

Differential Revision: https://reviews.llvm.org/D158865
---
 mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp     | 4 ++--
 mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
index bc7e831880753ca..2607017daa55ed4 100644
--- a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
+++ b/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
@@ -55,14 +55,14 @@ using FPToSIOpLowering =
 using FPToUIOpLowering =
     VectorConvertToLLVMPattern<arith::FPToUIOp, LLVM::FPToUIOp>;
 using MaxFOpLowering =
-    VectorConvertToLLVMPattern<arith::MaxFOp, LLVM::MaxNumOp,
+    VectorConvertToLLVMPattern<arith::MaxFOp, LLVM::MaximumOp,
                                arith::AttrConvertFastMathToLLVM>;
 using MaxSIOpLowering =
     VectorConvertToLLVMPattern<arith::MaxSIOp, LLVM::SMaxOp>;
 using MaxUIOpLowering =
     VectorConvertToLLVMPattern<arith::MaxUIOp, LLVM::UMaxOp>;
 using MinFOpLowering =
-    VectorConvertToLLVMPattern<arith::MinFOp, LLVM::MinNumOp,
+    VectorConvertToLLVMPattern<arith::MinFOp, LLVM::MinimumOp,
                                arith::AttrConvertFastMathToLLVM>;
 using MinSIOpLowering =
     VectorConvertToLLVMPattern<arith::MinSIOp, LLVM::SMinOp>;
diff --git a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
index 0762e4c4dbf9d07..cbbe41a1899b7c8 100644
--- a/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
+++ b/mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
@@ -522,9 +522,9 @@ func.func @minmaxi(%arg0 : i32, %arg1 : i32) -> i32 {
 
 // CHECK-LABEL: @minmaxf
 func.func @minmaxf(%arg0 : f32, %arg1 : f32) -> f32 {
-  // CHECK: = llvm.intr.minnum(%arg0, %arg1) : (f32, f32) -> f32
+  // CHECK: = llvm.intr.minimum(%arg0, %arg1) : (f32, f32) -> f32
   %0 = arith.minf %arg0, %arg1 : f32
-  // CHECK: = llvm.intr.maxnum(%arg0, %arg1) : (f32, f32) -> f32
+  // CHECK: = llvm.intr.maximum(%arg0, %arg1) : (f32, f32) -> f32
   %1 = arith.maxf %arg0, %arg1 : f32
   return %0 : f32
 }
@@ -554,9 +554,9 @@ func.func @ops_supporting_fastmath(%arg0: f32, %arg1: f32, %arg2: i32) {
   %0 = arith.addf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fdiv %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
   %1 = arith.divf %arg0, %arg1 fastmath<fast> : f32
-// CHECK: llvm.intr.maxnum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
+// CHECK: llvm.intr.maximum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
   %2 = arith.maxf %arg0, %arg1 fastmath<fast> : f32
-// CHECK: llvm.intr.minnum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
+// CHECK: llvm.intr.minimum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
   %3 = arith.minf %arg0, %arg1 fastmath<fast> : f32
 // CHECK: llvm.fmul %arg0, %arg1  {fastmathFlags = #llvm.fastmath<fast>} : f32
   %4 = arith.mulf %arg0, %arg1 fastmath<fast> : f32



More information about the Mlir-commits mailing list