[flang-commits] [flang] [flang] Eliminate workaround for optimizing maxval Fortran intrinsic (PR #65814)

Daniil Dudkin via flang-commits flang-commits at lists.llvm.org
Fri Sep 8 14:44:51 PDT 2023


https://github.com/unterumarmung created https://github.com/llvm/llvm-project/pull/65814:

Following #65213, the optimization of the `arith.maxf` operation has transitioned from using the `maxnum` LLVM intrinsic to `maximum`. This modification renders the statement in the previous commit obsolete and the associated workaround unnecessary. Consequently, this commit removes the workaround and rectifies related test cases.


>From 0010a1e8ccb553f1be79480bb348139c8f544c83 Mon Sep 17 00:00:00 2001
From: Daniil Dudkin <unterumarmung at yandex.ru>
Date: Sat, 9 Sep 2023 00:44:09 +0300
Subject: [PATCH] [flang] Eliminate workaround for optimizing maxval Fortran
 intrinsic

Following #65213, the optimization of the `arith.maxf` operation has transitioned from using the `maxnum` LLVM intrinsic to `maximum`. This modification renders the statement in the previous commit obsolete and the associated workaround unnecessary. Consequently, this commit removes the workaround and rectifies related test cases.
---
 flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp | 11 +----------
 flang/test/Transforms/simplifyintrinsics.fir          |  3 +--
 2 files changed, 2 insertions(+), 12 deletions(-)

diff --git a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
index 3eddb9e61ae3b3d..cd4506b748d0c19 100644
--- a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
+++ b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
@@ -618,16 +618,7 @@ static void genRuntimeMaxvalBody(fir::FirOpBuilder &builder,
                       mlir::Type elementType, mlir::Value elem1,
                       mlir::Value elem2) -> mlir::Value {
     if (elementType.isa<mlir::FloatType>()) {
-      // arith.maxf later converted to llvm.intr.maxnum does not work
-      // correctly for NaNs and -0.0 (see maxnum/minnum pattern matching
-      // in LLVM's InstCombine pass). Moreover, llvm.intr.maxnum
-      // for F128 operands is lowered into fmaxl call by LLVM.
-      // This libm function may not work properly for F128 arguments
-      // on targets where long double is not F128. It is an LLVM issue,
-      // but we just use normal select here to resolve all the cases.
-      auto compare = builder.create<mlir::arith::CmpFOp>(
-          loc, mlir::arith::CmpFPredicate::OGT, elem1, elem2);
-      return builder.create<mlir::arith::SelectOp>(loc, compare, elem1, elem2);
+      return builder.create<mlir::arith::MaxFOp>(loc, elem1, elem2);
     }
     if (elementType.isa<mlir::IntegerType>())
       return builder.create<mlir::arith::MaxSIOp>(loc, elem1, elem2);
diff --git a/flang/test/Transforms/simplifyintrinsics.fir b/flang/test/Transforms/simplifyintrinsics.fir
index 83359b23e03657e..36585e2cc527f8b 100644
--- a/flang/test/Transforms/simplifyintrinsics.fir
+++ b/flang/test/Transforms/simplifyintrinsics.fir
@@ -899,8 +899,7 @@ module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.targ
 // CHECK:           %[[RES:.*]] = fir.do_loop %[[ITER:.*]] = %[[CINDEX_0]] to %[[EXTENT]] step %[[CINDEX_1]] iter_args(%[[MAX]] = %[[NEG_DBL_MAX]]) -> (f64) {
 // CHECK:             %[[ITEM:.*]] = fir.coordinate_of %[[ARR_BOX_F64]], %[[ITER]] : (!fir.box<!fir.array<?xf64>>, index) -> !fir.ref<f64>
 // CHECK:             %[[ITEM_VAL:.*]] = fir.load %[[ITEM]] : !fir.ref<f64>
-// CHECK:             %[[CMP:.*]] = arith.cmpf ogt, %[[ITEM_VAL]], %[[MAX]] : f64
-// CHECK:             %[[NEW_MAX:.*]] = arith.select %[[CMP]], %[[ITEM_VAL]], %[[MAX]] : f64
+// CHECK:             %[[NEW_MAX:.*]] = arith.maxf %[[ITEM_VAL]], %[[MAX]] : f64
 // CHECK:             fir.result %[[NEW_MAX]] : f64
 // CHECK:           }
 // CHECK:           return %[[RES]] : f64



More information about the flang-commits mailing list