[llvm] [LLVM][NVPTX]Add BF16 vector instruction and fix lowering rules (PR #69415)

Han Shen via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 19 23:07:38 PDT 2023


================
@@ -769,14 +783,27 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
   // promoted to f32. v2f16 is expanded to f16, which is then promoted
   // to f32.
   for (const auto &Op :
-       {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FABS}) {
+       {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS}) {
     setOperationAction(Op, MVT::f16, Promote);
     setOperationAction(Op, MVT::bf16, Promote);
     setOperationAction(Op, MVT::f32, Legal);
     setOperationAction(Op, MVT::f64, Legal);
     setOperationAction(Op, MVT::v2f16, Expand);
     setOperationAction(Op, MVT::v2bf16, Expand);
+    if (getOperationAction(Op, MVT::bf16) == Promote)
+      AddPromotedToType(Op, MVT::bf16, MVT::f32);
+  }
+  for (const auto &Op : {ISD::FABS}) {
+    setOperationAction(Op, MVT::f16, Promote);
+    setBF16OperationAction(Op, MVT::bf16, Legal, Promote);
+    setOperationAction(Op, MVT::f32, Legal);
+    setOperationAction(Op, MVT::f64, Legal);
+    setOperationAction(Op, MVT::v2f16, Expand);
+    setBF16OperationAction(Op, MVT::v2bf16, Legal, Expand);
+    if (getOperationAction(Op, MVT::bf16) == Promote)
----------------
shenh10 wrote:

I read the code and figured out the reason why autopromote not working:

In function [getTypeToPromoteTo()](https://github.com/llvm/llvm-project/blob/f2517cbceec0bd9b049ba24f36cb3a2508c988fa/llvm/include/llvm/CodeGen/TargetLowering.h#L1567) of `TargetLowering.h`, the autopromote strategy iteratively try
`NVT = (MVT::SimpleValueType)(NVT.SimpleTy+1);`until `isTypeLegal(NVT)` returns true. 

In `llvm/CodeGen/GenVT.inc` the enum declaration of floating type is `bf16 -> f16->f32->f64->f80`, thus `bf16` would fallback to f16 in this rule, which is not what we expects.


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


More information about the llvm-commits mailing list