[llvm] [ConstantFolding] Fix nvvm_round folding on PPC (PR #149837)

Lewis Crawford via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 08:27:45 PDT 2025


https://github.com/LewisCrawford created https://github.com/llvm/llvm-project/pull/149837

Fix a failing test for constant-folding the nvvm_round intrinsic. The original implementation added in #141233 used a native libm call to the "round" function, but on PPC this produces +0.0 if the input is -0.0, which caused a test failure.

This patch updates it to use APFloat functions instead of native libm calls to ensure cross-platform consistency.

>From 310d89d1835e5726bece32a59787b85853209ad3 Mon Sep 17 00:00:00 2001
From: Lewis Crawford <lcrawford at nvidia.com>
Date: Mon, 21 Jul 2025 15:23:03 +0000
Subject: [PATCH] [ConstantFolding] Fix nvvm_round folding on PPC

Fix a failing test for constant-folding the nvvm_round intrinsic.
The original implementation added in #141233 used a native libm
call to the "round" function, but on PPC this produces +0.0 if the
input is -0.0, which caused a test failure.

This patch updates it to use APFloat functions instead of native
libm calls to ensure cross-platform consistency.
---
 llvm/lib/Analysis/ConstantFolding.cpp | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index f5a88b6e0368e..eb7369fcc7513 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2677,11 +2677,15 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
 
       case Intrinsic::nvvm_round_ftz_f:
       case Intrinsic::nvvm_round_f:
-      case Intrinsic::nvvm_round_d:
-        return ConstantFoldFP(
-            round, APF, Ty,
-            nvvm::GetNVVMDenromMode(
-                nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
+      case Intrinsic::nvvm_round_d: {
+        // Use APFloat implementation instead of native libm call, as some
+        // implementations (e.g. on PPC) do not preserve the sign of negative 0.
+        APFloat Res = nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)
+                          ? FTZPreserveSign(APF)
+                          : APF;
+        Res.roundToIntegral(APFloat::rmNearestTiesToAway);
+        return ConstantFP::get(Ty->getContext(), U);
+      }
 
       case Intrinsic::nvvm_saturate_ftz_f:
       case Intrinsic::nvvm_saturate_d:



More information about the llvm-commits mailing list