[llvm] Add constant-folding for unary NVVM intrinsics (PR #141233)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Fri May 30 12:10:09 PDT 2025


================
@@ -1944,16 +2006,32 @@ static const APFloat FTZPreserveSign(const APFloat &V) {
   return V;
 }
 
-Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
-                         Type *Ty) {
+// Get only the upper word of the input double in 1.11.20 format
+// by making the lower 32-bits of the mantissa all 0.
+static const APFloat ZeroLower32Bits(const APFloat &V) {
+  assert(V.getSizeInBits(V.getSemantics()) == 64);
+  uint64_t DoubleBits = V.bitcastToAPInt().getZExtValue();
+  DoubleBits &= 0xffffffff00000000;
+  return APFloat(V.getSemantics(), APInt(64, DoubleBits, false, false));
+}
+
+Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V, Type *Ty,
+                         bool ShouldFTZPreservingSign = false) {
----------------
Artem-B wrote:

I'm curious why sign-preserving FTZ is the special case? Is a non-sign-preserving FTZ a thing?

Afaict, all we care here is should we FTZ or not, and sign-preserving FTZ is the only one we have implemented in const-folding code. Perhaps the parameter should be called just `doFTZ` ?

https://github.com/llvm/llvm-project/pull/141233


More information about the llvm-commits mailing list