[InstCombine] avoid crashing in pow->ldexp
authorSanjay Patel <spatel@rotateright.com>
Fri, 10 Feb 2023 13:01:04 +0000 (08:01 -0500)
committerSanjay Patel <spatel@rotateright.com>
Fri, 10 Feb 2023 13:03:13 +0000 (08:03 -0500)
Similar to 62a0a1b9eea7788c1f9dbae -

We have pow 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

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/pow_fp_int.ll

index d497a9e..53fe3df 100644 (file)
@@ -1939,7 +1939,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
   AttributeList NoAttrs; // Attributes are only meaningful on the original call
 
   // pow(2.0, itofp(x)) -> ldexp(1.0, x)
-  if (match(Base, m_SpecificFP(2.0)) &&
+  // TODO: This does not work for vectors because there is no ldexp intrinsic.
+  if (!Ty->isVectorTy() && match(Base, m_SpecificFP(2.0)) &&
       (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
       hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
index d4d27c2..8718df3 100644 (file)
@@ -484,5 +484,19 @@ define double @powf_exp_const2_int_no_fast(double %base) {
   ret double %res
 }
 
+; TODO: This could be transformed the same as scalar if there is an ldexp intrinsic.
+
+define <2 x float> @pow_sitofp_const_base_2_no_fast_vector(<2 x i8> %x) {
+; CHECK-LABEL: @pow_sitofp_const_base_2_no_fast_vector(
+; CHECK-NEXT:    [[S:%.*]] = sitofp <2 x i8> [[X:%.*]] to <2 x float>
+; CHECK-NEXT:    [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[S]])
+; CHECK-NEXT:    ret <2 x float> [[EXP2]]
+;
+  %s = sitofp <2 x i8> %x to <2 x float>
+  %r = call <2 x float> @llvm.pow.v2f32(<2 x float><float 2.0, float 2.0>, <2 x float> %s)
+  ret <2 x float> %r
+}
+
 declare float @llvm.pow.f32(float, float)
 declare double @llvm.pow.f64(double, double)
+declare <2 x float> @llvm.pow.v2f32(<2 x float>, <2 x float>)