[flang-commits] [flang] [flang] Lower anint with math.round (PR #186039)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Wed Mar 11 22:03:28 PDT 2026


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/186039

Use `math.round` in lowering of `anint` so we can use passes like `MathToNVVM` to target device code differently. 

>From e1e8147e14df74f2fd2a5e8cf14c3193b2e3e9ac Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Wed, 11 Mar 2026 21:46:35 -0700
Subject: [PATCH] [flang] Lower anint with math.round

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp |  6 +++---
 flang/test/Lower/Intrinsics/anint.f90         |  6 +++---
 flang/test/Lower/math-lowering/anint.f90      | 15 ++++++---------
 3 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index d67eebdd8c93c..b4d4d84d4c2e9 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -1198,11 +1198,11 @@ static constexpr MathOperation mathOperations[] = {
     {"aint", RTNAME_STRING(TruncF128), FuncTypeReal16Real16, genLibF128Call},
     // llvm.round behaves the same way as libm's round.
     {"anint", "llvm.round.f32", genFuncType<Ty::Real<4>, Ty::Real<4>>,
-     genMathOp<mlir::LLVM::RoundOp>},
+     genMathOp<mlir::math::RoundOp>},
     {"anint", "llvm.round.f64", genFuncType<Ty::Real<8>, Ty::Real<8>>,
-     genMathOp<mlir::LLVM::RoundOp>},
+     genMathOp<mlir::math::RoundOp>},
     {"anint", "llvm.round.f80", genFuncType<Ty::Real<10>, Ty::Real<10>>,
-     genMathOp<mlir::LLVM::RoundOp>},
+     genMathOp<mlir::math::RoundOp>},
     {"anint", RTNAME_STRING(RoundF128), FuncTypeReal16Real16, genLibF128Call},
     {"asin", "asinf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
      genMathOp<mlir::math::AsinOp>},
diff --git a/flang/test/Lower/Intrinsics/anint.f90 b/flang/test/Lower/Intrinsics/anint.f90
index a7b24648ca0b6..135dccb2b63e9 100644
--- a/flang/test/Lower/Intrinsics/anint.f90
+++ b/flang/test/Lower/Intrinsics/anint.f90
@@ -6,7 +6,7 @@
 ! CHECK:         %[[VAL_0:.*]]:2 = hlfir.declare %[[VAL_0_b]]
 ! CHECK:         %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_1_b]]
 ! CHECK:         %[[VAL_2:.*]] = fir.load %[[VAL_0]]#0 : !fir.ref<f32>
-! CHECK:         %[[VAL_3:.*]] = llvm.intr.round(%[[VAL_2]]) : (f32) -> f32
+! CHECK:         %[[VAL_3:.*]] = math.round %[[VAL_2]] fastmath<contract> : f32 
 ! CHECK:         hlfir.assign %[[VAL_3]] to %[[VAL_1]]#0 : f32, !fir.ref<f32>
 ! CHECK:         return
 ! CHECK:       }
@@ -17,7 +17,7 @@ subroutine anint_test(a, b)
 end subroutine
 
 ! CHECK-LABEL: func.func @_QPanint_test_real8(
-! CHECK:    llvm.intr.round(%{{.*}}) : (f64) -> f64
+! CHECK:    math.round %{{.*}} fastmath<contract> : f64 
 
 subroutine anint_test_real8(a, b)
   real(8) :: a, b
@@ -25,7 +25,7 @@ subroutine anint_test_real8(a, b)
 end subroutine
 
 ! CHECK-KIND10-LABEL: func.func @_QPanint_test_real10(
-! CHECK-KIND10:    llvm.intr.round(%{{.*}}) : (f80) -> f80
+! CHECK-KIND10:    math.round %{{.*}} fastmath<contract> : f80 
 
 subroutine anint_test_real10(a, b)
   integer, parameter :: kind10 = merge(10, 4, selected_real_kind(p=18).eq.10)
diff --git a/flang/test/Lower/math-lowering/anint.f90 b/flang/test/Lower/math-lowering/anint.f90
index f39009de7e4f9..2432e7dfff574 100644
--- a/flang/test/Lower/math-lowering/anint.f90
+++ b/flang/test/Lower/math-lowering/anint.f90
@@ -13,8 +13,8 @@ function test_real4(x)
 end function
 
 ! ALL-LABEL: @_QPtest_real4
-! FAST: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f32) -> f32
-! RELAXED: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f32) -> f32
+! FAST: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f32 
+! RELAXED: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f32 
 ! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @llvm.round.f32({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32
 
 function test_real8(x)
@@ -23,8 +23,8 @@ function test_real8(x)
 end function
 
 ! ALL-LABEL: @_QPtest_real8
-! FAST: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f64) -> f64
-! RELAXED: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f64) -> f64
+! FAST: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f64 
+! RELAXED: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f64 
 ! PRECISE: {{%[A-Za-z0-9._]+}} = fir.call @llvm.round.f64({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64
 
 function test_real10(x)
@@ -34,8 +34,8 @@ function test_real10(x)
 end function
 
 ! ALL-KIND10-LABEL: @_QPtest_real10
-! FAST-KIND10: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f80) -> f80
-! RELAXED-KIND10: {{%[A-Za-z0-9._]+}} = llvm.intr.round({{%[A-Za-z0-9._]+}}) : (f80) -> f80
+! FAST-KIND10: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f80 
+! RELAXED-KIND10: {{%[A-Za-z0-9._]+}} = math.round %{{.*}} fastmath<contract> : f80 
 ! PRECISE-KIND10: {{%[A-Za-z0-9._]+}} = fir.call @llvm.round.f80({{%[A-Za-z0-9._]+}}) {{.*}}: (f80) -> f80
 
 ! TODO: wait until fp128 is supported well in llvm.round
@@ -44,6 +44,3 @@ function test_real10(x)
 !  test_real16 = anint(x)
 !end function
 
-! PRECISE-DAG: func.func private @llvm.round.f32(f32) -> f32 attributes {fir.bindc_name = "llvm.round.f32", fir.runtime}
-! PRECISE-DAG: func.func private @llvm.round.f64(f64) -> f64 attributes {fir.bindc_name = "llvm.round.f64", fir.runtime}
-! PRECISE-KIND10-DAG: func.func private @llvm.round.f80(f80) -> f80 attributes {fir.bindc_name = "llvm.round.f80", fir.runtime}



More information about the flang-commits mailing list