Skip to content

Commit

Permalink
[InstCombine] avoid crashing in pow->ldexp
Browse files Browse the repository at this point in the history
Similar to 62a0a1b -

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
  • Loading branch information
rotateright committed Feb 10, 2023
1 parent 5cec69b commit 9dcd719
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Expand Up @@ -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()))
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Transforms/InstCombine/pow_fp_int.ll
Expand Up @@ -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>)

0 comments on commit 9dcd719

Please sign in to comment.