[llvm] [X86] Lower scalar bf16 arithmetic on AVX10.2 via packed ops (PR #212245)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 05:22:18 PDT 2026


https://github.com/tfzee updated https://github.com/llvm/llvm-project/pull/212245

>From fd63d632beb39a5791408b94d7693971d6c9ca15 Mon Sep 17 00:00:00 2001
From: "Ziegler, Tim" <tim.ziegler at intel.com>
Date: Mon, 27 Jul 2026 15:13:18 +0200
Subject: [PATCH 1/4] Added bf16 scalar codegen

---
 llvm/lib/Target/X86/X86ISelLowering.cpp       |  52 +++++
 .../CodeGen/X86/avx10_2bf16-arith-scalar.ll   | 221 ++++++++++++++++++
 llvm/test/CodeGen/X86/avx10_2bf16-fma.ll      |  12 +-
 3 files changed, 274 insertions(+), 11 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/avx10_2bf16-arith-scalar.ll

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index d78c478ab672f..ca80ade655a13 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -2601,6 +2601,15 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
   }
 
   if (!Subtarget.useSoftFloat() && Subtarget.hasAVX10_2()) {
+    // Lower scalar bf16 arithmetic by widening to a vector op and extracting
+    // the low element.
+    setOperationAction(ISD::FADD, MVT::bf16, Custom);
+    setOperationAction(ISD::FSUB, MVT::bf16, Custom);
+    setOperationAction(ISD::FMUL, MVT::bf16, Custom);
+    setOperationAction(ISD::FDIV, MVT::bf16, Custom);
+    setOperationAction(ISD::FSQRT, MVT::bf16, Custom);
+    setOperationAction(ISD::FMA, MVT::bf16, Custom);
+
     setOperationAction(ISD::FADD, MVT::v32bf16, Legal);
     setOperationAction(ISD::FSUB, MVT::v32bf16, Legal);
     setOperationAction(ISD::FMUL, MVT::v32bf16, Legal);
@@ -34513,6 +34522,37 @@ SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
   }
 }
 
+/// Lower a scalar bf16 arithmetic node whose type is being soft-promoted.
+///
+/// AVX10.2 has no scalar bf16 arithmetic instructions, and bf16 is a
+/// soft-promoted-half type, so scalar ops would otherwise be promoted to f32.
+/// Instead widen each operand to a v8bf16 vector, perform the legal packed
+/// operation, and extract the low element afterwards.
+///
+/// Each operand is reinterpreted as f16 before building the vector. bf16 has no
+/// scalar register class of its own, but f16 does (FR16X), and bf16/f16 share
+/// the same bits and vector register class (VR128X). Going through f16 keeps the
+/// value in an XMM register: bf16->f16 bitcast, f16->v8f16 SCALAR_TO_VECTOR
+/// (a free COPY_TO_REGCLASS), and v8f16->v8bf16 bitcast are all free. Feeding a
+/// scalar bf16 to SCALAR_TO_VECTOR directly would instead route through bf16's
+/// i16 soft-promote carrier, forcing xmm<->GPR vmovw roundtrips.
+///
+/// Returns a bf16-typed value.
+static SDValue LowerScalarBF16ArithViaVector(SDNode *N, SelectionDAG &DAG) {
+  SDLoc dl(N);
+  assert(N->getValueType(0) == MVT::bf16 && "Expected scalar bf16 result");
+  SmallVector<SDValue, 3> VecOps;
+  for (const SDValue &Op : N->ops()) {
+    SDValue AsF16 = DAG.getBitcast(MVT::f16, Op);
+    SDValue VecF16 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8f16, AsF16);
+    VecOps.push_back(DAG.getBitcast(MVT::v8bf16, VecF16));
+  }
+  SDValue Vec =
+      DAG.getNode(N->getOpcode(), dl, MVT::v8bf16, VecOps, N->getFlags());
+  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::bf16, Vec,
+                     DAG.getVectorIdxConstant(0, dl));
+}
+
 /// Replace a node with an illegal result type with a new node built out of
 /// custom code.
 void X86TargetLowering::ReplaceNodeResults(SDNode *N,
@@ -34531,6 +34571,18 @@ void X86TargetLowering::ReplaceNodeResults(SDNode *N,
     N->dump(&DAG);
 #endif
     llvm_unreachable("Do not know how to custom type legalize this operation!");
+  case ISD::FADD:
+  case ISD::FSUB:
+  case ISD::FMUL:
+  case ISD::FSQRT:
+  case ISD::FDIV:
+  case ISD::FMA:
+    if (N->getValueType(0) == MVT::bf16) {
+      // Scalar bf16 arithmetic is soft-promoted; widen to a legal packed op
+      // instead of promoting to f32. See LowerScalarBF16ArithViaVector.
+      Results.push_back(LowerScalarBF16ArithViaVector(N, DAG));
+    }
+    return;
   case X86ISD::CVTPH2PS: {
     EVT VT = N->getValueType(0);
     SDValue Lo, Hi;
diff --git a/llvm/test/CodeGen/X86/avx10_2bf16-arith-scalar.ll b/llvm/test/CodeGen/X86/avx10_2bf16-arith-scalar.ll
new file mode 100644
index 0000000000000..cc7f3ad0eb174
--- /dev/null
+++ b/llvm/test/CodeGen/X86/avx10_2bf16-arith-scalar.ll
@@ -0,0 +1,221 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -verify-machineinstrs -mtriple=x86_64-unknown-unknown -mattr=+avx10.2 | FileCheck %s --check-prefixes=AVX10_2
+; RUN: llc < %s -verify-machineinstrs -mtriple=x86_64-unknown-unknown -mattr=+avx512bf16,+avx512vl | FileCheck %s --check-prefixes=AVX512BF16
+; RUN: llc < %s -verify-machineinstrs -mtriple=x86_64-unknown-unknown -mattr=+avxneconvert | FileCheck %s --check-prefixes=AVXNECONVERT
+
+define bfloat @fadd_bf16(bfloat %a, bfloat %b) nounwind {
+; AVX10_2-LABEL: fadd_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vaddbf16 %xmm1, %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: fadd_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %eax
+; AVX512BF16-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm1
+; AVX512BF16-NEXT:    vaddss %xmm0, %xmm1, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: fadd_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %eax
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm1
+; AVXNECONVERT-NEXT:    vaddss %xmm0, %xmm1, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %r = fadd  contract bfloat %a, %b
+  ret bfloat %r
+}
+
+
+define bfloat @dont_fuse_bf16(bfloat %a, bfloat %b, bfloat %c) nounwind {
+; AVX10_2-LABEL: dont_fuse_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vmulbf16 %xmm1, %xmm0, %xmm0
+; AVX10_2-NEXT:    vaddbf16 %xmm2, %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: dont_fuse_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm2, %eax
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %ecx
+; AVX512BF16-NEXT:    vpextrw $0, %xmm1, %edx
+; AVX512BF16-NEXT:    shll $16, %edx
+; AVX512BF16-NEXT:    vmovd %edx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm1
+; AVX512BF16-NEXT:    vmulss %xmm0, %xmm1, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    vmovd %xmm0, %ecx
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm1
+; AVX512BF16-NEXT:    vaddss %xmm1, %xmm0, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: dont_fuse_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm2, %eax
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %ecx
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm1, %edx
+; AVXNECONVERT-NEXT:    shll $16, %edx
+; AVXNECONVERT-NEXT:    vmovd %edx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm1
+; AVXNECONVERT-NEXT:    vmulss %xmm0, %xmm1, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    vmovd %xmm0, %ecx
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm1
+; AVXNECONVERT-NEXT:    vaddss %xmm1, %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %m = fmul bfloat %a, %b
+  %r = fadd bfloat %m, %c
+  ret bfloat %r
+}
+
+define bfloat @fsub_bf16(bfloat %a, bfloat %b) nounwind {
+; AVX10_2-LABEL: fsub_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vsubbf16 %xmm1, %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: fsub_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %eax
+; AVX512BF16-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm1
+; AVX512BF16-NEXT:    vsubss %xmm0, %xmm1, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: fsub_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %eax
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm1
+; AVXNECONVERT-NEXT:    vsubss %xmm0, %xmm1, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %r = fsub bfloat %a, %b
+  ret bfloat %r
+}
+
+define bfloat @fmul_bf16(bfloat %a, bfloat %b) nounwind {
+; AVX10_2-LABEL: fmul_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vmulbf16 %xmm1, %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: fmul_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %eax
+; AVX512BF16-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm1
+; AVX512BF16-NEXT:    vmulss %xmm0, %xmm1, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: fmul_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %eax
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm1
+; AVXNECONVERT-NEXT:    vmulss %xmm0, %xmm1, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %m = fmul bfloat %a, %b
+  ret bfloat %m
+}
+
+define bfloat @fdiv_bf16(bfloat %a, bfloat %b) nounwind {
+; AVX10_2-LABEL: fdiv_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vdivbf16 %xmm1, %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: fdiv_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %eax
+; AVX512BF16-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVX512BF16-NEXT:    shll $16, %ecx
+; AVX512BF16-NEXT:    vmovd %ecx, %xmm0
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm1
+; AVX512BF16-NEXT:    vdivss %xmm0, %xmm1, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: fdiv_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %eax
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm1, %ecx
+; AVXNECONVERT-NEXT:    shll $16, %ecx
+; AVXNECONVERT-NEXT:    vmovd %ecx, %xmm0
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm1
+; AVXNECONVERT-NEXT:    vdivss %xmm0, %xmm1, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %m = fdiv bfloat %a, %b
+  ret bfloat %m
+}
+
+define bfloat @fsqrt_bf16(bfloat %a) nounwind {
+; AVX10_2-LABEL: fsqrt_bf16:
+; AVX10_2:       # %bb.0: # %entry
+; AVX10_2-NEXT:    vsqrtbf16 %xmm0, %xmm0
+; AVX10_2-NEXT:    retq
+;
+; AVX512BF16-LABEL: fsqrt_bf16:
+; AVX512BF16:       # %bb.0: # %entry
+; AVX512BF16-NEXT:    vpextrw $0, %xmm0, %eax
+; AVX512BF16-NEXT:    shll $16, %eax
+; AVX512BF16-NEXT:    vmovd %eax, %xmm0
+; AVX512BF16-NEXT:    vsqrtss %xmm0, %xmm0, %xmm0
+; AVX512BF16-NEXT:    vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16-NEXT:    retq
+;
+; AVXNECONVERT-LABEL: fsqrt_bf16:
+; AVXNECONVERT:       # %bb.0: # %entry
+; AVXNECONVERT-NEXT:    vpextrw $0, %xmm0, %eax
+; AVXNECONVERT-NEXT:    shll $16, %eax
+; AVXNECONVERT-NEXT:    vmovd %eax, %xmm0
+; AVXNECONVERT-NEXT:    vsqrtss %xmm0, %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT-NEXT:    retq
+entry:
+  %m = call bfloat @llvm.sqrt.bf16(bfloat %a)
+  ret bfloat %m
+}
diff --git a/llvm/test/CodeGen/X86/avx10_2bf16-fma.ll b/llvm/test/CodeGen/X86/avx10_2bf16-fma.ll
index d79f0cc79b5f4..4c829cc78b3e5 100644
--- a/llvm/test/CodeGen/X86/avx10_2bf16-fma.ll
+++ b/llvm/test/CodeGen/X86/avx10_2bf16-fma.ll
@@ -6,17 +6,7 @@
 define bfloat @fuse_bf16(bfloat %a, bfloat %b, bfloat %c) nounwind {
 ; AVX10_2-LABEL: fuse_bf16:
 ; AVX10_2:       # %bb.0: # %entry
-; AVX10_2-NEXT:    vmovw %xmm1, %eax
-; AVX10_2-NEXT:    vmovw %xmm0, %ecx
-; AVX10_2-NEXT:    vmovw %xmm2, %edx
-; AVX10_2-NEXT:    shll $16, %edx
-; AVX10_2-NEXT:    vmovd %edx, %xmm0
-; AVX10_2-NEXT:    shll $16, %ecx
-; AVX10_2-NEXT:    vmovd %ecx, %xmm1
-; AVX10_2-NEXT:    shll $16, %eax
-; AVX10_2-NEXT:    vmovd %eax, %xmm2
-; AVX10_2-NEXT:    vfmadd213ss {{.*#+}} xmm2 = (xmm1 * xmm2) + xmm0
-; AVX10_2-NEXT:    vcvtneps2bf16 %xmm2, %xmm0
+; AVX10_2-NEXT:    vfmadd213bf16 %xmm2, %xmm1, %xmm0
 ; AVX10_2-NEXT:    retq
 ;
 ; AVX512BF16-LABEL: fuse_bf16:

>From 37d3a0127f2f32b79714efbdc197a8e3f6244b13 Mon Sep 17 00:00:00 2001
From: "Ziegler, Tim" <tim.ziegler at intel.com>
Date: Wed, 29 Jul 2026 08:39:48 +0200
Subject: [PATCH 2/4] Updated to use getExtractVectorElt and made the comment
 shorter

---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index ca80ade655a13..c941553862a2d 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -34531,11 +34531,7 @@ SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
 ///
 /// Each operand is reinterpreted as f16 before building the vector. bf16 has no
 /// scalar register class of its own, but f16 does (FR16X), and bf16/f16 share
-/// the same bits and vector register class (VR128X). Going through f16 keeps the
-/// value in an XMM register: bf16->f16 bitcast, f16->v8f16 SCALAR_TO_VECTOR
-/// (a free COPY_TO_REGCLASS), and v8f16->v8bf16 bitcast are all free. Feeding a
-/// scalar bf16 to SCALAR_TO_VECTOR directly would instead route through bf16's
-/// i16 soft-promote carrier, forcing xmm<->GPR vmovw roundtrips.
+/// the same bits and vector register class (VR128X).
 ///
 /// Returns a bf16-typed value.
 static SDValue LowerScalarBF16ArithViaVector(SDNode *N, SelectionDAG &DAG) {
@@ -34549,8 +34545,7 @@ static SDValue LowerScalarBF16ArithViaVector(SDNode *N, SelectionDAG &DAG) {
   }
   SDValue Vec =
       DAG.getNode(N->getOpcode(), dl, MVT::v8bf16, VecOps, N->getFlags());
-  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::bf16, Vec,
-                     DAG.getVectorIdxConstant(0, dl));
+  return DAG.getExtractVectorElt(dl, MVT::bf16, Vec, 0);
 }
 
 /// Replace a node with an illegal result type with a new node built out of

>From 4191c5d0b0d32ed6015c1d09feae8807ab54d44d Mon Sep 17 00:00:00 2001
From: "Ziegler, Tim" <tim.ziegler at intel.com>
Date: Wed, 29 Jul 2026 14:17:07 +0200
Subject: [PATCH 3/4] Inlined the funciton

---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 46 +++++++++----------------
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index c941553862a2d..ca73aa8d1ce55 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -34522,32 +34522,6 @@ SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
   }
 }
 
-/// Lower a scalar bf16 arithmetic node whose type is being soft-promoted.
-///
-/// AVX10.2 has no scalar bf16 arithmetic instructions, and bf16 is a
-/// soft-promoted-half type, so scalar ops would otherwise be promoted to f32.
-/// Instead widen each operand to a v8bf16 vector, perform the legal packed
-/// operation, and extract the low element afterwards.
-///
-/// Each operand is reinterpreted as f16 before building the vector. bf16 has no
-/// scalar register class of its own, but f16 does (FR16X), and bf16/f16 share
-/// the same bits and vector register class (VR128X).
-///
-/// Returns a bf16-typed value.
-static SDValue LowerScalarBF16ArithViaVector(SDNode *N, SelectionDAG &DAG) {
-  SDLoc dl(N);
-  assert(N->getValueType(0) == MVT::bf16 && "Expected scalar bf16 result");
-  SmallVector<SDValue, 3> VecOps;
-  for (const SDValue &Op : N->ops()) {
-    SDValue AsF16 = DAG.getBitcast(MVT::f16, Op);
-    SDValue VecF16 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8f16, AsF16);
-    VecOps.push_back(DAG.getBitcast(MVT::v8bf16, VecF16));
-  }
-  SDValue Vec =
-      DAG.getNode(N->getOpcode(), dl, MVT::v8bf16, VecOps, N->getFlags());
-  return DAG.getExtractVectorElt(dl, MVT::bf16, Vec, 0);
-}
-
 /// Replace a node with an illegal result type with a new node built out of
 /// custom code.
 void X86TargetLowering::ReplaceNodeResults(SDNode *N,
@@ -34571,13 +34545,25 @@ void X86TargetLowering::ReplaceNodeResults(SDNode *N,
   case ISD::FMUL:
   case ISD::FSQRT:
   case ISD::FDIV:
-  case ISD::FMA:
+  case ISD::FMA: {
     if (N->getValueType(0) == MVT::bf16) {
-      // Scalar bf16 arithmetic is soft-promoted; widen to a legal packed op
-      // instead of promoting to f32. See LowerScalarBF16ArithViaVector.
-      Results.push_back(LowerScalarBF16ArithViaVector(N, DAG));
+      // AVX10.2 has no scalar bf16 arithmetic instructions, and bf16 is a
+      // soft-promoted-half type, so scalar ops would otherwise be promoted to f32.
+      // Instead widen each operand to a v8bf16 vector, perform the legal packed
+      // operation, and extract the low element afterwards.
+      assert(N->getValueType(0) == MVT::bf16 && "Expected scalar bf16 result");
+      SmallVector<SDValue, 3> VecOps;
+      for (const SDValue &Op : N->ops()) {
+        SDValue AsF16 = DAG.getBitcast(MVT::f16, Op);
+        SDValue VecF16 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8f16, AsF16);
+        VecOps.push_back(DAG.getBitcast(MVT::v8bf16, VecF16));
+      }
+      SDValue Vec =
+          DAG.getNode(N->getOpcode(), dl, MVT::v8bf16, VecOps, N->getFlags());
+      Results.push_back(DAG.getExtractVectorElt(dl, MVT::bf16, Vec, 0));
     }
     return;
+  }
   case X86ISD::CVTPH2PS: {
     EVT VT = N->getValueType(0);
     SDValue Lo, Hi;

>From d68a54bed226e9cf4ddb9e57118882c922934fcb Mon Sep 17 00:00:00 2001
From: "Ziegler, Tim" <tim.ziegler at intel.com>
Date: Wed, 29 Jul 2026 14:21:39 +0200
Subject: [PATCH 4/4] Formatting cleanup

---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index ca73aa8d1ce55..3a305e24af2a1 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -34548,14 +34548,15 @@ void X86TargetLowering::ReplaceNodeResults(SDNode *N,
   case ISD::FMA: {
     if (N->getValueType(0) == MVT::bf16) {
       // AVX10.2 has no scalar bf16 arithmetic instructions, and bf16 is a
-      // soft-promoted-half type, so scalar ops would otherwise be promoted to f32.
-      // Instead widen each operand to a v8bf16 vector, perform the legal packed
-      // operation, and extract the low element afterwards.
+      // soft-promoted-half type, so scalar ops would otherwise be promoted to
+      // f32. Instead widen each operand to a v8bf16 vector, perform the legal
+      // packed operation, and extract the low element afterwards.
       assert(N->getValueType(0) == MVT::bf16 && "Expected scalar bf16 result");
       SmallVector<SDValue, 3> VecOps;
       for (const SDValue &Op : N->ops()) {
         SDValue AsF16 = DAG.getBitcast(MVT::f16, Op);
-        SDValue VecF16 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8f16, AsF16);
+        SDValue VecF16 =
+            DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8f16, AsF16);
         VecOps.push_back(DAG.getBitcast(MVT::v8bf16, VecF16));
       }
       SDValue Vec =



More information about the llvm-commits mailing list