[llvm] [AMDGPU] Add custom lowering of llvm.convert.from.arbitrary.fp for FP8 (PR #194144)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 16:04:35 PDT 2026


================
@@ -10569,6 +10579,78 @@ SDValue SITargetLowering::lowerWorkitemID(SelectionDAG &DAG, SDValue Op,
                      DAG.getValueType(SmallVT));
 }
 
+SDValue
+SITargetLowering::LowerCONVERT_FROM_ARBITRARY_FP(SDValue Op,
+                                                 SelectionDAG &DAG) const {
+  EVT DstVT = Op.getValueType();
+  if (DstVT != MVT::f32 && DstVT != MVT::v2f32 && DstVT != MVT::v4f32)
+    return SDValue();
+
+  bool IsBF8 = false;
+  switch (static_cast<APFloatBase::Semantics>(Op.getConstantOperandVal(1))) {
+  case APFloatBase::S_Float8E4M3FN:
+    IsBF8 = false;
+    break;
+  case APFloatBase::S_Float8E5M2:
+    IsBF8 = true;
+    break;
+  default:
+    return SDValue();
+  }
+
+  SDLoc SL(Op);
+  SDValue Src = Op.getOperand(0);
+
+  // Defer constant inputs to generic bit-twiddling expansion.
+  if (DAG.isConstantIntBuildVectorOrConstantInt(Src))
+    return SDValue();
+
+  // Pack vector inputs as hw instruction is packed.
+  // FIXME: note, packing loses lane-wise poison. Should we do something about
+  // it?
+  auto PackBytes = [&](unsigned NumBytes, unsigned FirstLane) {
+    EVT EltVT = Src.getValueType().getVectorElementType();
+    SDValue PackedI32;
+    for (unsigned I = 0; I != NumBytes; ++I) {
+      SDValue Elt = DAG.getExtractVectorElt(SL, EltVT, Src, FirstLane + I);
+      SDValue Byte = DAG.getZExtOrTrunc(Elt, SL, MVT::i32);
+      Byte = DAG.getNode(ISD::AND, SL, MVT::i32, Byte,
+                         DAG.getConstant(0xFF, SL, MVT::i32));
+      if (I != 0)
+        Byte = DAG.getNode(ISD::SHL, SL, MVT::i32, Byte,
+                           DAG.getConstant(I * 8, SL, MVT::i32));
+      PackedI32 =
+          I == 0 ? Byte : DAG.getNode(ISD::OR, SL, MVT::i32, PackedI32, Byte);
+    }
+    return PackedI32;
+  };
+
+  auto EmitPk = [&](SDValue PackedI32, unsigned WordSel) {
+    unsigned IntrID = IsBF8 ? Intrinsic::amdgcn_cvt_pk_f32_bf8
+                            : Intrinsic::amdgcn_cvt_pk_f32_fp8;
+    return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::v2f32,
+                       DAG.getTargetConstant(IntrID, SL, MVT::i32), PackedI32,
+                       DAG.getTargetConstant(WordSel, SL, MVT::i1));
+  };
+
+  if (DstVT == MVT::f32) {
+    unsigned IntrID =
----------------
MrSidims wrote:

Removed lowering to gfx942 for now

https://github.com/llvm/llvm-project/pull/194144


More information about the llvm-commits mailing list