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

Lewis Crawford via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 01:13:32 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) {
----------------
LewisCrawford wrote:

I've changed it now to allow for folding with different denormal modes (which may be different for inputs vs outputs). I've only changed the code for the NVVM intrinsics calling this, as fixing subnormal behaviour for other intrinsics seems a bit outside the scope of this patch, but I've written it in such a way that it should be fairly easy to add in a follow-up patch.

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


More information about the llvm-commits mailing list