[llvm] 62a0a1b - [InstCombine] avoid crashing in exp2->ldexp

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 10 04:36:46 PST 2023


Author: Sanjay Patel
Date: 2023-02-10T07:35:39-05:00
New Revision: 62a0a1b9eea7788c1f9dbaebee51c95dd2da99e6

URL: https://github.com/llvm/llvm-project/commit/62a0a1b9eea7788c1f9dbaebee51c95dd2da99e6
DIFF: https://github.com/llvm/llvm-project/commit/62a0a1b9eea7788c1f9dbaebee51c95dd2da99e6.diff

LOG: [InstCombine] avoid crashing in exp2->ldexp

We have exp2 math intrinsics in IR, but no ldexp intrinsics
to handle vector types.

A patch for that was proposed in D14327, but it was not completed.

Issue #60605

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/test/Transforms/InstCombine/exp2-1.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 60b417dcb062..d497a9e8f9f0 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2217,11 +2217,16 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
       hasFloatVersion(M, Name))
     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
 
+  // Bail out for vectors because the code below only expects scalars.
+  // TODO: This could be allowed if we had a ldexp intrinsic (D14327).
   Type *Ty = CI->getType();
-  Value *Op = CI->getArgOperand(0);
+  if (Ty->isVectorTy())
+    return Ret;
 
   // exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= IntSize
   // exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < IntSize
+  // TODO: This does not propagate FMF.
+  Value *Op = CI->getArgOperand(0);
   if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) &&
       hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
     if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize()))

diff  --git a/llvm/test/Transforms/InstCombine/exp2-1.ll b/llvm/test/Transforms/InstCombine/exp2-1.ll
index 471f036a2ea3..92a06ee5185f 100644
--- a/llvm/test/Transforms/InstCombine/exp2-1.ll
+++ b/llvm/test/Transforms/InstCombine/exp2-1.ll
@@ -302,3 +302,32 @@ define float @sitofp_scalar_intrinsic_with_FMF(i8 %x) {
   %r = call nnan float @llvm.exp2.f32(float %s)
   ret float %r
 }
+
+; PR60605
+; This would crash because there is no ldexp intrinsic.
+
+define <2 x float> @sitofp_vector_intrinsic_with_FMF(<2 x i8> %x) {
+; LDEXP32-LABEL: @sitofp_vector_intrinsic_with_FMF(
+; LDEXP32-NEXT:    [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
+; LDEXP32-NEXT:    [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
+; LDEXP32-NEXT:    ret <2 x float> [[R]]
+;
+; LDEXP16-LABEL: @sitofp_vector_intrinsic_with_FMF(
+; LDEXP16-NEXT:    [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
+; LDEXP16-NEXT:    [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
+; LDEXP16-NEXT:    ret <2 x float> [[R]]
+;
+; NOLDEXPF-LABEL: @sitofp_vector_intrinsic_with_FMF(
+; NOLDEXPF-NEXT:    [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
+; NOLDEXPF-NEXT:    [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
+; NOLDEXPF-NEXT:    ret <2 x float> [[R]]
+;
+; NOLDEXP-LABEL: @sitofp_vector_intrinsic_with_FMF(
+; NOLDEXP-NEXT:    [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
+; NOLDEXP-NEXT:    [[R:%.*]] = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
+; NOLDEXP-NEXT:    ret <2 x float> [[R]]
+;
+  %s = sitofp <2 x i8> %x to <2 x float>
+  %r = call nnan <2 x float> @llvm.exp2.v2f32(<2 x float> %s)
+  ret <2 x float> %r
+}


        


More information about the llvm-commits mailing list