[llvm] [ConstantFolding] Handle large exponents in ldexp (PR #199309)

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Sat May 23 11:51:38 PDT 2026


https://github.com/jlebar updated https://github.com/llvm/llvm-project/pull/199309

>From e6f9b83d15a277b63b55c8036baa6a48feb27863 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Fri, 22 May 2026 11:56:59 -0700
Subject: [PATCH 1/3] [ConstantFolding] Handle large exponents in ldexp

Previously if you passed a constant exponent to llvm.ldexp greater than
the width of `int`, we would silently truncate it to `int` before
using it in scalbn.  We'd thus generate the incorrect result.

We now clamp it to fit within int.

This bug was found by a large run of Opus 4.7 looking for bugs in LLVM.
---
 llvm/lib/Analysis/ConstantFolding.cpp     |  9 +++++++-
 llvm/test/Transforms/InstCombine/ldexp.ll | 27 +++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 5be3788e872d5..ca4c27775fe86 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3713,9 +3713,16 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
     } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
       switch (IntrinsicID) {
       case Intrinsic::ldexp: {
+        // APFloat::scalbn takes the exponent as `int`. Clamp wider integer
+        // exponents into [INT_MIN, INT_MAX] so values still saturate the
+        // result to +/-inf or +/-0.
+        int64_t Exp = Op2C->getSExtValue();
+        int ClampedExp = static_cast<int>(
+            std::clamp<int64_t>(Exp, std::numeric_limits<int>::min(),
+                                std::numeric_limits<int>::max()));
         return ConstantFP::get(
             Ty->getContext(),
-            scalbn(Op1V, Op2C->getSExtValue(), APFloat::rmNearestTiesToEven));
+            scalbn(Op1V, ClampedExp, APFloat::rmNearestTiesToEven));
       }
       case Intrinsic::is_fpclass: {
         FPClassTest Mask = static_cast<FPClassTest>(Op2C->getZExtValue());
diff --git a/llvm/test/Transforms/InstCombine/ldexp.ll b/llvm/test/Transforms/InstCombine/ldexp.ll
index e1fd5b45190ac..40f22ef12e0e2 100644
--- a/llvm/test/Transforms/InstCombine/ldexp.ll
+++ b/llvm/test/Transforms/InstCombine/ldexp.ll
@@ -4,6 +4,7 @@
 declare float @llvm.ldexp.f32.i32(float, i32)
 declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>)
 declare float @llvm.ldexp.f32.i64(float, i64)
+declare double @llvm.ldexp.f64.i64(double, i64)
 
 ; select c, (ldexp val, e0), (ldexp val, e1) -> ldexp val, (select c, e0, e1)
 define float @select_ldexp_f32_sameval_differentexp(i1 %cond, float %val, i32 %exp0, i32 %exp1) {
@@ -1093,6 +1094,32 @@ define <2 x float> @ldexp_v2f32_mask_select_0_swap(<2 x i1> %cond, <2 x float> %
   ret <2 x float> %ldexp
 }
 
+; Exponent constants that exceed `int` saturate to +/-inf / +/-0 rather than
+; silently narrowing.
+define double @ldexp_i64_exp_above_int_max() {
+; CHECK-LABEL: define double @ldexp_i64_exp_above_int_max() {
+; CHECK-NEXT:    ret double +inf
+;
+  %r = call double @llvm.ldexp.f64.i64(double 1.0, i64 4294967330)
+  ret double %r
+}
+
+define double @ldexp_i64_exp_below_int_min() {
+; CHECK-LABEL: define double @ldexp_i64_exp_below_int_min() {
+; CHECK-NEXT:    ret double 0.000000e+00
+;
+  %r = call double @llvm.ldexp.f64.i64(double 1.0, i64 -4294967330)
+  ret double %r
+}
+
+define double @ldexp_i64_neg_input_exp_above_int_max() {
+; CHECK-LABEL: define double @ldexp_i64_neg_input_exp_above_int_max() {
+; CHECK-NEXT:    ret double -inf
+;
+  %r = call double @llvm.ldexp.f64.i64(double -1.0, i64 4294967330)
+  ret double %r
+}
+
 attributes #0 = { strictfp }
 
 !0 = !{i32 -127, i32 0}

>From bf4ec17189f466734e48e0cb6cb842004057a610 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Sat, 23 May 2026 11:38:28 -0700
Subject: [PATCH 2/3] Don't crash when the constant > 2^64.

---
 llvm/lib/Analysis/ConstantFolding.cpp     | 7 ++-----
 llvm/test/Transforms/InstCombine/ldexp.ll | 9 +++++++++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index ca4c27775fe86..5912f229bdb83 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3716,13 +3716,10 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
         // APFloat::scalbn takes the exponent as `int`. Clamp wider integer
         // exponents into [INT_MIN, INT_MAX] so values still saturate the
         // result to +/-inf or +/-0.
-        int64_t Exp = Op2C->getSExtValue();
-        int ClampedExp = static_cast<int>(
-            std::clamp<int64_t>(Exp, std::numeric_limits<int>::min(),
-                                std::numeric_limits<int>::max()));
+        APInt Exp = Op2C->getValue().truncSSat(32);
         return ConstantFP::get(
             Ty->getContext(),
-            scalbn(Op1V, ClampedExp, APFloat::rmNearestTiesToEven));
+            scalbn(Op1V, Exp.getSExtValue(), APFloat::rmNearestTiesToEven));
       }
       case Intrinsic::is_fpclass: {
         FPClassTest Mask = static_cast<FPClassTest>(Op2C->getZExtValue());
diff --git a/llvm/test/Transforms/InstCombine/ldexp.ll b/llvm/test/Transforms/InstCombine/ldexp.ll
index 40f22ef12e0e2..4824c30324ce4 100644
--- a/llvm/test/Transforms/InstCombine/ldexp.ll
+++ b/llvm/test/Transforms/InstCombine/ldexp.ll
@@ -1120,6 +1120,15 @@ define double @ldexp_i64_neg_input_exp_above_int_max() {
   ret double %r
 }
 
+define double @ldexp_i64_neg_input_exp_above_int64_max() {
+; CHECK-LABEL: define double @ldexp_i64_neg_input_exp_above_int64_max() {
+; CHECK-NEXT:    ret double -inf
+;
+  ; This constant is 2^65, i.e. it exceeds the range of i64.
+  %r = call double @llvm.ldexp.f64.i128(double -1.0, i128 36893488147419103232)
+  ret double %r
+}
+
 attributes #0 = { strictfp }
 
 !0 = !{i32 -127, i32 0}

>From 5e6f17612d83b5f66d67ffd611ef8b9c5024d292 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Sat, 23 May 2026 11:50:36 -0700
Subject: [PATCH 3/3] truncSSat(n) fails if the bit width of the type is less
 than n.

---
 llvm/lib/Analysis/ConstantFolding.cpp     |  3 ++-
 llvm/test/Transforms/InstCombine/ldexp.ll | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 5912f229bdb83..5b9a211a362cf 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3716,7 +3716,8 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
         // APFloat::scalbn takes the exponent as `int`. Clamp wider integer
         // exponents into [INT_MIN, INT_MAX] so values still saturate the
         // result to +/-inf or +/-0.
-        APInt Exp = Op2C->getValue().truncSSat(32);
+        APInt Exp = Op2C->getValue();
+        Exp = Exp.getBitWidth() < 32 ? Exp.sext(32) : Exp.truncSSat(32);
         return ConstantFP::get(
             Ty->getContext(),
             scalbn(Op1V, Exp.getSExtValue(), APFloat::rmNearestTiesToEven));
diff --git a/llvm/test/Transforms/InstCombine/ldexp.ll b/llvm/test/Transforms/InstCombine/ldexp.ll
index 4824c30324ce4..08384f2e75a3c 100644
--- a/llvm/test/Transforms/InstCombine/ldexp.ll
+++ b/llvm/test/Transforms/InstCombine/ldexp.ll
@@ -3,6 +3,7 @@
 
 declare float @llvm.ldexp.f32.i32(float, i32)
 declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>)
+declare double @llvm.ldexp.f64.i16(double, i16)
 declare float @llvm.ldexp.f32.i64(float, i64)
 declare double @llvm.ldexp.f64.i64(double, i64)
 
@@ -1129,6 +1130,22 @@ define double @ldexp_i64_neg_input_exp_above_int64_max() {
   ret double %r
 }
 
+define double @ldexp_i16_exp_narrow_int() {
+; CHECK-LABEL: define double @ldexp_i16_exp_narrow_int() {
+; CHECK-NEXT:    ret double 4.096000e+03
+;
+  %r = call double @llvm.ldexp.f64.i16(double 1.0, i16 12)
+  ret double %r
+}
+
+define double @ldexp_i16_exp_narrow_int_negative() {
+; CHECK-LABEL: define double @ldexp_i16_exp_narrow_int_negative() {
+; CHECK-NEXT:    ret double 4.000000e+00
+;
+  %r = call double @llvm.ldexp.f64.i16(double 8.0, i16 -1)
+  ret double %r
+}
+
 attributes #0 = { strictfp }
 
 !0 = !{i32 -127, i32 0}



More information about the llvm-commits mailing list