Skip to content

Commit d4fa088

Browse files
[mlir][arith] Improve Lowering of maxf/minf ops (#65213)
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.
1 parent 0735a6e commit d4fa088

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ using FPToSIOpLowering =
5555
using FPToUIOpLowering =
5656
VectorConvertToLLVMPattern<arith::FPToUIOp, LLVM::FPToUIOp>;
5757
using MaxFOpLowering =
58-
VectorConvertToLLVMPattern<arith::MaxFOp, LLVM::MaxNumOp,
58+
VectorConvertToLLVMPattern<arith::MaxFOp, LLVM::MaximumOp,
5959
arith::AttrConvertFastMathToLLVM>;
6060
using MaxSIOpLowering =
6161
VectorConvertToLLVMPattern<arith::MaxSIOp, LLVM::SMaxOp>;
6262
using MaxUIOpLowering =
6363
VectorConvertToLLVMPattern<arith::MaxUIOp, LLVM::UMaxOp>;
6464
using MinFOpLowering =
65-
VectorConvertToLLVMPattern<arith::MinFOp, LLVM::MinNumOp,
65+
VectorConvertToLLVMPattern<arith::MinFOp, LLVM::MinimumOp,
6666
arith::AttrConvertFastMathToLLVM>;
6767
using MinSIOpLowering =
6868
VectorConvertToLLVMPattern<arith::MinSIOp, LLVM::SMinOp>;

mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ func.func @minmaxi(%arg0 : i32, %arg1 : i32) -> i32 {
522522

523523
// CHECK-LABEL: @minmaxf
524524
func.func @minmaxf(%arg0 : f32, %arg1 : f32) -> f32 {
525-
// CHECK: = llvm.intr.minnum(%arg0, %arg1) : (f32, f32) -> f32
525+
// CHECK: = llvm.intr.minimum(%arg0, %arg1) : (f32, f32) -> f32
526526
%0 = arith.minf %arg0, %arg1 : f32
527-
// CHECK: = llvm.intr.maxnum(%arg0, %arg1) : (f32, f32) -> f32
527+
// CHECK: = llvm.intr.maximum(%arg0, %arg1) : (f32, f32) -> f32
528528
%1 = arith.maxf %arg0, %arg1 : f32
529529
return %0 : f32
530530
}
@@ -554,9 +554,9 @@ func.func @ops_supporting_fastmath(%arg0: f32, %arg1: f32, %arg2: i32) {
554554
%0 = arith.addf %arg0, %arg1 fastmath<fast> : f32
555555
// CHECK: llvm.fdiv %arg0, %arg1 {fastmathFlags = #llvm.fastmath<fast>} : f32
556556
%1 = arith.divf %arg0, %arg1 fastmath<fast> : f32
557-
// CHECK: llvm.intr.maxnum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
557+
// CHECK: llvm.intr.maximum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
558558
%2 = arith.maxf %arg0, %arg1 fastmath<fast> : f32
559-
// CHECK: llvm.intr.minnum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
559+
// CHECK: llvm.intr.minimum(%arg0, %arg1) {fastmathFlags = #llvm.fastmath<fast>} : (f32, f32) -> f32
560560
%3 = arith.minf %arg0, %arg1 fastmath<fast> : f32
561561
// CHECK: llvm.fmul %arg0, %arg1 {fastmathFlags = #llvm.fastmath<fast>} : f32
562562
%4 = arith.mulf %arg0, %arg1 fastmath<fast> : f32

0 commit comments

Comments
 (0)