[llvm] [X86] Lower mathlib call ldexp into scalef when avx512 is enabled (PR #166839)
Kavin Gnanapandithan via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 18 09:12:39 PST 2025
================
@@ -19153,6 +19155,81 @@ SDValue X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
return SDValue();
}
+static SDValue LowerFLDEXP(SDValue Op, const X86Subtarget &Subtarget,
+ SelectionDAG &DAG) {
+ SDLoc DL(Op);
+ SDValue X = Op.getOperand(0);
+ MVT XTy = X.getSimpleValueType();
+ SDValue Exp = Op.getOperand(1);
+ MVT ExtVT;
+
+ switch (XTy.SimpleTy) {
+ default:
+ return SDValue();
+ case MVT::f16:
+ if (!Subtarget.hasFP16())
+ X = DAG.getFPExtendOrRound(X, DL, MVT::f32);
+ [[fallthrough]];
+ case MVT::f32:
+ case MVT::f64: {
+ MVT VT = MVT::getVectorVT(X.getSimpleValueType(),
+ 128 / X.getSimpleValueType().getSizeInBits());
+ Exp = DAG.getNode(ISD::SINT_TO_FP, DL, X.getValueType(), Exp);
+ SDValue VX = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, X);
+ SDValue VExp = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Exp);
+ SDValue Scalefs = DAG.getNode(X86ISD::SCALEFS, DL, VT, VX, VExp, VX);
+ SDValue Final = DAG.getExtractVectorElt(DL, X.getValueType(), Scalefs, 0);
+ if (X.getValueType() != XTy)
+ Final = DAG.getFPExtendOrRound(Final, DL, XTy);
+ return Final;
+ }
+ case MVT::v4f32:
+ case MVT::v2f64:
+ case MVT::v8f32:
+ case MVT::v4f64:
+ case MVT::v16f32:
+ case MVT::v8f64:
+ Exp = DAG.getNode(ISD::SINT_TO_FP, DL, XTy, Exp);
----------------
KavinTheG wrote:
Alternatively for the vector fp16 cases, I realized it's possible to convert Exp to fp after extending it to i32. So Exp would be fp for all cases after the switch statement.
```
ExtVT = XTy.changeVectorElementType(MVT::f32);
X = DAG.getFPExtendOrRound(X, DL, ExtVT);
Exp = DAG.getSExtOrTrunc(Exp, DL, ExtVT.changeTypeToInteger());
Exp = DAG.getNode(ISD::SINT_TO_FP, DL, ExtVT, Exp);
```
https://github.com/llvm/llvm-project/pull/166839
More information about the llvm-commits
mailing list