[llvm] [ConstantFolding] Handle large exponents in ldexp (PR #199309)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 22 18:19:35 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Justin Lebar (jlebar)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/199309.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+8-1)
- (modified) llvm/test/Transforms/InstCombine/ldexp.ll (+27)
``````````diff
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}
``````````
</details>
https://github.com/llvm/llvm-project/pull/199309
More information about the llvm-commits
mailing list