[llvm] [X86][Hexagon] Remove the INSERT_VECTOR_ELT soft promotion workarounds (PR #223628)
Paweł Bylica via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 01:56:20 PDT 2026
https://github.com/chfast updated https://github.com/llvm/llvm-project/pull/223628
>From a6d1b289a47276606c65242b2cd3771ba9b74dcd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Tue, 15 Sep 2026 01:29:36 +0200
Subject: [PATCH 1/3] [SelectionDAG][test] Add tests for INSERT_VECTOR_ELT with
a soft promoted half
Pre-commit tests demonstrating the crash. When f16/bf16 is soft promoted
but a vector of that type is legal, INSERT_VECTOR_ELT is left with a soft
promoted element operand and SoftPromoteHalfOperand has no case for it:
LLVM ERROR: Do not know how to soft promote this operator's operand!
Reachable on MIPS MSA (fixed vectors) and on RISC-V with Zvfhmin/Zvfbfmin
but without Zfhmin/Zfbfmin (scalable vectors and bf16).
Assisted-by: Claude Code
---
.../CodeGen/Mips/msa/f16vec-insertelement.ll | 33 +++++++++++++
.../RISCV/rvv/insertelt-fp-soft-promote.ll | 47 +++++++++++++++++++
2 files changed, 80 insertions(+)
create mode 100644 llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
create mode 100644 llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
diff --git a/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll b/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
new file mode 100644
index 0000000000000..efd0f33ed50bd
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
@@ -0,0 +1,33 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=mipsel-unknown-linux-gnu -mcpu=mips32r5 -mattr=+fp64,+msa < %s | FileCheck %s --check-prefix=MIPS32
+; RUN: llc -mtriple=mips64el-unknown-linux-gnuabi64 -mcpu=mips64r5 -mattr=+fp64,+msa < %s | FileCheck %s --check-prefix=MIPS64
+
+; Test that a scalar f16 can be inserted into an f16 vector without crashing.
+; f16 is soft promoted on MIPS while v8f16 is a legal MSA type, so the
+; INSERT_VECTOR_ELT ends up with a soft promoted element operand. With a
+; constant index DAGCombine usually folds the insert into a BUILD_VECTOR
+; first; the variable index cases reach the type legalizer directly.
+
+define <8 x half> @insert_v8f16_idx(<8 x half> %v, half %e, i32 %idx) nounwind {
+;
+ %r = insertelement <8 x half> %v, half %e, i32 %idx
+ ret <8 x half> %r
+}
+
+define <8 x half> @insert_v8f16_imm(<8 x half> %v, half %e) nounwind {
+;
+ %r = insertelement <8 x half> %v, half %e, i32 3
+ ret <8 x half> %r
+}
+
+define <4 x half> @insert_v4f16_idx(<4 x half> %v, half %e, i32 %idx) nounwind {
+;
+ %r = insertelement <4 x half> %v, half %e, i32 %idx
+ ret <4 x half> %r
+}
+
+define <2 x half> @insert_v2f16_idx(<2 x half> %v, half %e, i32 %idx) nounwind {
+;
+ %r = insertelement <2 x half> %v, half %e, i32 %idx
+ ret <2 x half> %r
+}
diff --git a/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll b/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
new file mode 100644
index 0000000000000..243ece78a7094
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -mattr=+v,+zvfhmin,+zvfbfmin -target-abi=ilp32d < %s | FileCheck %s
+; RUN: llc -mtriple=riscv64 -mattr=+v,+zvfhmin,+zvfbfmin -target-abi=lp64d < %s | FileCheck %s
+
+; Without Zfhmin/Zfbfmin the scalar f16/bf16 types are soft promoted while the
+; vector types are legal, so INSERT_VECTOR_ELT has to be legalized with a soft
+; promoted element operand.
+
+define <vscale x 4 x half> @insertelt_nxv4f16_0(<vscale x 4 x half> %v, half %elt) {
+ %r = insertelement <vscale x 4 x half> %v, half %elt, i32 0
+ ret <vscale x 4 x half> %r
+}
+
+define <vscale x 4 x half> @insertelt_nxv4f16_imm(<vscale x 4 x half> %v, half %elt) {
+ %r = insertelement <vscale x 4 x half> %v, half %elt, i32 3
+ ret <vscale x 4 x half> %r
+}
+
+define <vscale x 4 x half> @insertelt_nxv4f16_idx(<vscale x 4 x half> %v, half %elt, i32 zeroext %idx) {
+ %r = insertelement <vscale x 4 x half> %v, half %elt, i32 %idx
+ ret <vscale x 4 x half> %r
+}
+
+define <vscale x 4 x bfloat> @insertelt_nxv4bf16_0(<vscale x 4 x bfloat> %v, bfloat %elt) {
+ %r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 0
+ ret <vscale x 4 x bfloat> %r
+}
+
+define <vscale x 4 x bfloat> @insertelt_nxv4bf16_imm(<vscale x 4 x bfloat> %v, bfloat %elt) {
+ %r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 3
+ ret <vscale x 4 x bfloat> %r
+}
+
+define <vscale x 4 x bfloat> @insertelt_nxv4bf16_idx(<vscale x 4 x bfloat> %v, bfloat %elt, i32 zeroext %idx) {
+ %r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 %idx
+ ret <vscale x 4 x bfloat> %r
+}
+
+define <4 x half> @insertelt_v4f16_idx(<4 x half> %v, half %elt, i32 zeroext %idx) {
+ %r = insertelement <4 x half> %v, half %elt, i32 %idx
+ ret <4 x half> %r
+}
+
+define <4 x bfloat> @insertelt_v4bf16_idx(<4 x bfloat> %v, bfloat %elt, i32 zeroext %idx) {
+ %r = insertelement <4 x bfloat> %v, bfloat %elt, i32 %idx
+ ret <4 x bfloat> %r
+}
>From d57ec9d5ee432206d8c23e6c6d3fd8a34202cb02 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Tue, 15 Sep 2026 01:30:06 +0200
Subject: [PATCH 2/3] [SelectionDAG] Soft promote the element operand of
INSERT_VECTOR_ELT
When f16/bf16 is soft promoted but a vector of that type is legal,
INSERT_VECTOR_ELT is left with a soft promoted i16 element operand.
SoftPromoteHalfOperand had no case for it and failed with "Do not know
how to soft promote this operator's operand!".
Legalize it like BUILD_VECTOR and the inverse EXTRACT_VECTOR_ELT: bitcast
the vector to its integer counterpart, insert the promoted element there
and bitcast the result back. This also covers scalable vectors.
This is reachable on MIPS MSA and on RISC-V with Zvfhmin/Zvfbfmin but
without Zfhmin/Zfbfmin. X86, Hexagon and WebAssembly avoid it by custom
lowering INSERT_VECTOR_ELT on the scalar type; that still takes
precedence and can be removed separately.
Fixes #198104.
Assisted-by: Claude Code
---
.../SelectionDAG/LegalizeFloatTypes.cpp | 13 ++
llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h | 1 +
.../CodeGen/Mips/msa/f16vec-insertelement.ll | 137 ++++++++++++++++++
.../RISCV/rvv/insertelt-fp-soft-promote.ll | 62 ++++++++
4 files changed, 213 insertions(+)
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
index b18da75e46bae..b7efd80d93fd4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
@@ -2994,6 +2994,9 @@ bool DAGTypeLegalizer::SoftPromoteHalfOperand(SDNode *N, unsigned OpNo) {
case ISD::BUILD_VECTOR:
Res = SoftPromoteHalfOp_BUILD_VECTOR(N);
break;
+ case ISD::INSERT_VECTOR_ELT:
+ Res = SoftPromoteHalfOp_INSERT_VECTOR_ELT(N, OpNo);
+ break;
case ISD::FAKE_USE:
Res = SoftPromoteHalfOp_FAKE_USE(N, OpNo);
break;
@@ -3070,6 +3073,16 @@ SDValue DAGTypeLegalizer::SoftPromoteHalfOp_BUILD_VECTOR(SDNode *N) {
return DAG.getBitcast(VT, Res);
}
+SDValue DAGTypeLegalizer::SoftPromoteHalfOp_INSERT_VECTOR_ELT(SDNode *N,
+ unsigned OpNo) {
+ assert(OpNo == 1 && "Only Operand 1 must need promotion here");
+ SDValue Vec = BitConvertVectorToIntegerVector(N->getOperand(0));
+ SDValue Elt = GetSoftPromotedHalf(N->getOperand(OpNo));
+ SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N),
+ Vec.getValueType(), Vec, Elt, N->getOperand(2));
+ return DAG.getBitcast(N->getValueType(0), Res);
+}
+
SDValue DAGTypeLegalizer::SoftPromoteHalfOp_FAKE_USE(SDNode *N, unsigned OpNo) {
assert(OpNo == 1 && "Only Operand 1 must need promotion here");
SDValue Op = GetSoftPromotedHalf(N->getOperand(OpNo));
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h b/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h
index 6fc6d61c6a38d..d486b3f50cc90 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h
@@ -787,6 +787,7 @@ class LLVM_LIBRARY_VISIBILITY DAGTypeLegalizer {
bool SoftPromoteHalfOperand(SDNode *N, unsigned OpNo);
SDValue SoftPromoteHalfOp_BITCAST(SDNode *N);
SDValue SoftPromoteHalfOp_BUILD_VECTOR(SDNode *N);
+ SDValue SoftPromoteHalfOp_INSERT_VECTOR_ELT(SDNode *N, unsigned OpNo);
SDValue SoftPromoteHalfOp_FAKE_USE(SDNode *N, unsigned OpNo);
SDValue SoftPromoteHalfOp_FCOPYSIGN(SDNode *N, unsigned OpNo);
SDValue SoftPromoteHalfOp_FP_EXTEND(SDNode *N);
diff --git a/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll b/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
index efd0f33ed50bd..ee24ea9b5d740 100644
--- a/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
+++ b/llvm/test/CodeGen/Mips/msa/f16vec-insertelement.ll
@@ -10,24 +10,161 @@
define <8 x half> @insert_v8f16_idx(<8 x half> %v, half %e, i32 %idx) nounwind {
;
+; MIPS32-LABEL: insert_v8f16_idx:
+; MIPS32: # %bb.0:
+; MIPS32-NEXT: insert.w $w0[0], $6
+; MIPS32-NEXT: insert.w $w0[1], $7
+; MIPS32-NEXT: lw $1, 16($sp)
+; MIPS32-NEXT: insert.w $w0[2], $1
+; MIPS32-NEXT: lw $1, 20($sp)
+; MIPS32-NEXT: insert.w $w0[3], $1
+; MIPS32-NEXT: lhu $1, 24($sp)
+; MIPS32-NEXT: lw $2, 28($sp)
+; MIPS32-NEXT: sll $2, $2, 1
+; MIPS32-NEXT: sld.b $w0, $w0[$2]
+; MIPS32-NEXT: insert.h $w0[0], $1
+; MIPS32-NEXT: neg $1, $2
+; MIPS32-NEXT: sld.b $w0, $w0[$1]
+; MIPS32-NEXT: jr $ra
+; MIPS32-NEXT: st.h $w0, 0($4)
+;
+; MIPS64-LABEL: insert_v8f16_idx:
+; MIPS64: # %bb.0:
+; MIPS64-NEXT: insert.d $w0[0], $4
+; MIPS64-NEXT: insert.d $w0[1], $5
+; MIPS64-NEXT: dext $1, $7, 0, 32
+; MIPS64-NEXT: sll $2, $6, 0
+; MIPS64-NEXT: dsll $1, $1, 1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: insert.h $w0[0], $2
+; MIPS64-NEXT: dneg $1, $1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: copy_s.d $2, $w0[0]
+; MIPS64-NEXT: jr $ra
+; MIPS64-NEXT: copy_s.d $3, $w0[1]
%r = insertelement <8 x half> %v, half %e, i32 %idx
ret <8 x half> %r
}
define <8 x half> @insert_v8f16_imm(<8 x half> %v, half %e) nounwind {
;
+; MIPS32-LABEL: insert_v8f16_imm:
+; MIPS32: # %bb.0:
+; MIPS32-NEXT: insert.w $w0[0], $6
+; MIPS32-NEXT: insert.w $w0[1], $7
+; MIPS32-NEXT: lw $1, 16($sp)
+; MIPS32-NEXT: insert.w $w0[2], $1
+; MIPS32-NEXT: lw $1, 20($sp)
+; MIPS32-NEXT: insert.w $w0[3], $1
+; MIPS32-NEXT: lhu $1, 24($sp)
+; MIPS32-NEXT: insert.h $w0[3], $1
+; MIPS32-NEXT: jr $ra
+; MIPS32-NEXT: st.h $w0, 0($4)
+;
+; MIPS64-LABEL: insert_v8f16_imm:
+; MIPS64: # %bb.0:
+; MIPS64-NEXT: insert.d $w0[0], $4
+; MIPS64-NEXT: insert.d $w0[1], $5
+; MIPS64-NEXT: sll $1, $6, 0
+; MIPS64-NEXT: insert.h $w0[3], $1
+; MIPS64-NEXT: copy_s.d $2, $w0[0]
+; MIPS64-NEXT: jr $ra
+; MIPS64-NEXT: copy_s.d $3, $w0[1]
%r = insertelement <8 x half> %v, half %e, i32 3
ret <8 x half> %r
}
define <4 x half> @insert_v4f16_idx(<4 x half> %v, half %e, i32 %idx) nounwind {
;
+; MIPS32-LABEL: insert_v4f16_idx:
+; MIPS32: # %bb.0:
+; MIPS32-NEXT: addiu $sp, $sp, -32
+; MIPS32-NEXT: sw $ra, 28($sp) # 4-byte Folded Spill
+; MIPS32-NEXT: sw $fp, 24($sp) # 4-byte Folded Spill
+; MIPS32-NEXT: move $fp, $sp
+; MIPS32-NEXT: addiu $1, $zero, -16
+; MIPS32-NEXT: and $sp, $sp, $1
+; MIPS32-NEXT: sw $7, 4($sp)
+; MIPS32-NEXT: sw $6, 0($sp)
+; MIPS32-NEXT: lhu $1, 48($fp)
+; MIPS32-NEXT: lw $2, 52($fp)
+; MIPS32-NEXT: ld.h $w0, 0($sp)
+; MIPS32-NEXT: sll $2, $2, 1
+; MIPS32-NEXT: sld.b $w0, $w0[$2]
+; MIPS32-NEXT: insert.h $w0[0], $1
+; MIPS32-NEXT: neg $1, $2
+; MIPS32-NEXT: sld.b $w0, $w0[$1]
+; MIPS32-NEXT: copy_s.w $1, $w0[0]
+; MIPS32-NEXT: copy_s.w $2, $w0[1]
+; MIPS32-NEXT: sw $2, 4($4)
+; MIPS32-NEXT: sw $1, 0($4)
+; MIPS32-NEXT: move $sp, $fp
+; MIPS32-NEXT: lw $fp, 24($sp) # 4-byte Folded Reload
+; MIPS32-NEXT: lw $ra, 28($sp) # 4-byte Folded Reload
+; MIPS32-NEXT: jr $ra
+; MIPS32-NEXT: addiu $sp, $sp, 32
+;
+; MIPS64-LABEL: insert_v4f16_idx:
+; MIPS64: # %bb.0:
+; MIPS64-NEXT: daddiu $sp, $sp, -16
+; MIPS64-NEXT: sd $4, 0($sp)
+; MIPS64-NEXT: dext $1, $6, 0, 32
+; MIPS64-NEXT: sll $2, $5, 0
+; MIPS64-NEXT: ld.h $w0, 0($sp)
+; MIPS64-NEXT: dsll $1, $1, 1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: insert.h $w0[0], $2
+; MIPS64-NEXT: dneg $1, $1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: copy_s.d $2, $w0[0]
+; MIPS64-NEXT: jr $ra
+; MIPS64-NEXT: daddiu $sp, $sp, 16
%r = insertelement <4 x half> %v, half %e, i32 %idx
ret <4 x half> %r
}
define <2 x half> @insert_v2f16_idx(<2 x half> %v, half %e, i32 %idx) nounwind {
;
+; MIPS32-LABEL: insert_v2f16_idx:
+; MIPS32: # %bb.0:
+; MIPS32-NEXT: addiu $sp, $sp, -32
+; MIPS32-NEXT: sw $ra, 28($sp) # 4-byte Folded Spill
+; MIPS32-NEXT: sw $fp, 24($sp) # 4-byte Folded Spill
+; MIPS32-NEXT: move $fp, $sp
+; MIPS32-NEXT: addiu $1, $zero, -16
+; MIPS32-NEXT: and $sp, $sp, $1
+; MIPS32-NEXT: sw $5, 0($sp)
+; MIPS32-NEXT: ld.h $w0, 0($sp)
+; MIPS32-NEXT: sll $1, $7, 1
+; MIPS32-NEXT: sld.b $w0, $w0[$1]
+; MIPS32-NEXT: insert.h $w0[0], $6
+; MIPS32-NEXT: neg $1, $1
+; MIPS32-NEXT: sld.b $w0, $w0[$1]
+; MIPS32-NEXT: copy_s.w $1, $w0[0]
+; MIPS32-NEXT: sw $1, 0($4)
+; MIPS32-NEXT: move $sp, $fp
+; MIPS32-NEXT: lw $fp, 24($sp) # 4-byte Folded Reload
+; MIPS32-NEXT: lw $ra, 28($sp) # 4-byte Folded Reload
+; MIPS32-NEXT: jr $ra
+; MIPS32-NEXT: addiu $sp, $sp, 32
+;
+; MIPS64-LABEL: insert_v2f16_idx:
+; MIPS64: # %bb.0:
+; MIPS64-NEXT: daddiu $sp, $sp, -16
+; MIPS64-NEXT: sll $1, $5, 0
+; MIPS64-NEXT: sw $1, 0($sp)
+; MIPS64-NEXT: dext $1, $7, 0, 32
+; MIPS64-NEXT: sll $2, $6, 0
+; MIPS64-NEXT: ld.h $w0, 0($sp)
+; MIPS64-NEXT: dsll $1, $1, 1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: insert.h $w0[0], $2
+; MIPS64-NEXT: dneg $1, $1
+; MIPS64-NEXT: sld.b $w0, $w0[$1]
+; MIPS64-NEXT: copy_s.w $1, $w0[0]
+; MIPS64-NEXT: sw $1, 0($4)
+; MIPS64-NEXT: jr $ra
+; MIPS64-NEXT: daddiu $sp, $sp, 16
%r = insertelement <2 x half> %v, half %e, i32 %idx
ret <2 x half> %r
}
diff --git a/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll b/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
index 243ece78a7094..b2a0399dd490e 100644
--- a/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/insertelt-fp-soft-promote.ll
@@ -7,41 +7,103 @@
; promoted element operand.
define <vscale x 4 x half> @insertelt_nxv4f16_0(<vscale x 4 x half> %v, half %elt) {
+; CHECK-LABEL: insertelt_nxv4f16_0:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a0, fa0
+; CHECK-NEXT: vsetvli a1, zero, e16, m1, tu, ma
+; CHECK-NEXT: vmv.s.x v8, a0
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x half> %v, half %elt, i32 0
ret <vscale x 4 x half> %r
}
define <vscale x 4 x half> @insertelt_nxv4f16_imm(<vscale x 4 x half> %v, half %elt) {
+; CHECK-LABEL: insertelt_nxv4f16_imm:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a0, fa0
+; CHECK-NEXT: vsetivli zero, 4, e16, m1, tu, ma
+; CHECK-NEXT: vmv.s.x v9, a0
+; CHECK-NEXT: vslideup.vi v8, v9, 3
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x half> %v, half %elt, i32 3
ret <vscale x 4 x half> %r
}
define <vscale x 4 x half> @insertelt_nxv4f16_idx(<vscale x 4 x half> %v, half %elt, i32 zeroext %idx) {
+; CHECK-LABEL: insertelt_nxv4f16_idx:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a1, fa0
+; CHECK-NEXT: vsetvli a2, zero, e16, m1, ta, ma
+; CHECK-NEXT: vmv.s.x v9, a1
+; CHECK-NEXT: addi a1, a0, 1
+; CHECK-NEXT: vsetvli zero, a1, e16, m1, tu, ma
+; CHECK-NEXT: vslideup.vx v8, v9, a0
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x half> %v, half %elt, i32 %idx
ret <vscale x 4 x half> %r
}
define <vscale x 4 x bfloat> @insertelt_nxv4bf16_0(<vscale x 4 x bfloat> %v, bfloat %elt) {
+; CHECK-LABEL: insertelt_nxv4bf16_0:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a0, fa0
+; CHECK-NEXT: vsetvli a1, zero, e16, m1, tu, ma
+; CHECK-NEXT: vmv.s.x v8, a0
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 0
ret <vscale x 4 x bfloat> %r
}
define <vscale x 4 x bfloat> @insertelt_nxv4bf16_imm(<vscale x 4 x bfloat> %v, bfloat %elt) {
+; CHECK-LABEL: insertelt_nxv4bf16_imm:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a0, fa0
+; CHECK-NEXT: vsetivli zero, 4, e16, m1, tu, ma
+; CHECK-NEXT: vmv.s.x v9, a0
+; CHECK-NEXT: vslideup.vi v8, v9, 3
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 3
ret <vscale x 4 x bfloat> %r
}
define <vscale x 4 x bfloat> @insertelt_nxv4bf16_idx(<vscale x 4 x bfloat> %v, bfloat %elt, i32 zeroext %idx) {
+; CHECK-LABEL: insertelt_nxv4bf16_idx:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a1, fa0
+; CHECK-NEXT: vsetvli a2, zero, e16, m1, ta, ma
+; CHECK-NEXT: vmv.s.x v9, a1
+; CHECK-NEXT: addi a1, a0, 1
+; CHECK-NEXT: vsetvli zero, a1, e16, m1, tu, ma
+; CHECK-NEXT: vslideup.vx v8, v9, a0
+; CHECK-NEXT: ret
%r = insertelement <vscale x 4 x bfloat> %v, bfloat %elt, i32 %idx
ret <vscale x 4 x bfloat> %r
}
define <4 x half> @insertelt_v4f16_idx(<4 x half> %v, half %elt, i32 zeroext %idx) {
+; CHECK-LABEL: insertelt_v4f16_idx:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a1, fa0
+; CHECK-NEXT: vsetivli zero, 4, e16, m1, ta, ma
+; CHECK-NEXT: vmv.s.x v9, a1
+; CHECK-NEXT: addi a1, a0, 1
+; CHECK-NEXT: vsetvli zero, a1, e16, mf2, tu, ma
+; CHECK-NEXT: vslideup.vx v8, v9, a0
+; CHECK-NEXT: ret
%r = insertelement <4 x half> %v, half %elt, i32 %idx
ret <4 x half> %r
}
define <4 x bfloat> @insertelt_v4bf16_idx(<4 x bfloat> %v, bfloat %elt, i32 zeroext %idx) {
+; CHECK-LABEL: insertelt_v4bf16_idx:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.x.w a1, fa0
+; CHECK-NEXT: vsetivli zero, 4, e16, m1, ta, ma
+; CHECK-NEXT: vmv.s.x v9, a1
+; CHECK-NEXT: addi a1, a0, 1
+; CHECK-NEXT: vsetvli zero, a1, e16, mf2, tu, ma
+; CHECK-NEXT: vslideup.vx v8, v9, a0
+; CHECK-NEXT: ret
%r = insertelement <4 x bfloat> %v, bfloat %elt, i32 %idx
ret <4 x bfloat> %r
}
>From 4848e0ccd243e501e441b11dee72f22e512fe2e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Tue, 15 Sep 2026 09:53:49 +0200
Subject: [PATCH 3/3] [X86][Hexagon] Remove the INSERT_VECTOR_ELT soft
promotion workarounds
SoftPromoteHalfOperand now handles INSERT_VECTOR_ELT, so neither target
needs to intercept the node on the scalar type to keep the type legalizer
from failing with "Do not know how to soft promote this operator's
operand!".
X86 did exactly what the generic code does: bitcast the vector to its
integer counterpart, insert an i16 and bitcast the result back. Hexagon
built the same vXi16 insert, and that one is still custom lowered through
the regular HVX path.
Both target hooks become unreachable once the operation actions are gone,
because the element operand is promoted to i16 before LegalizeDAG runs, so
drop them as well.
No codegen change: X86/bfloat.ll, X86/vector-shuffle-combining-avx512bf16.ll
and the autohvx tests that insert into half or bfloat vectors
(hfinsert.ll, build-vector-float-type.ll, splat.ll, calling-conv.ll) all
produce identical output.
Assisted-by: Claude Code
---
.../lib/Target/Hexagon/HexagonISelLoweringHVX.cpp | 15 ++-------------
llvm/lib/Target/X86/X86ISelLowering.cpp | 13 ++-----------
2 files changed, 4 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp b/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp
index cfcf970673155..6a9d506214554 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp
@@ -175,9 +175,8 @@ HexagonTargetLowering::initializeHVXLowering() {
// BUILD_VECTOR with f16 operands cannot be promoted without
// promoting the result, so lower the node to vsplat or constant pool
- setOperationAction(ISD::BUILD_VECTOR, MVT::f16, Custom);
- setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::f16, Custom);
- setOperationAction(ISD::SPLAT_VECTOR, MVT::f16, Custom);
+ setOperationAction(ISD::BUILD_VECTOR, MVT::f16, Custom);
+ setOperationAction(ISD::SPLAT_VECTOR, MVT::f16, Custom);
// Vector shuffle is always promoted to ByteV and a bitcast to f16 is
// generated.
@@ -224,7 +223,6 @@ HexagonTargetLowering::initializeHVXLowering() {
setOperationAction(ISD::CONCAT_VECTORS, MVT::v128bf16, Custom);
setOperationAction(ISD::SPLAT_VECTOR, MVT::bf16, Custom);
- setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::bf16, Custom);
setOperationAction(ISD::BUILD_VECTOR, MVT::bf16, Custom);
}
@@ -1954,7 +1952,6 @@ SDValue
HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
const {
const SDLoc &dl(Op);
- MVT VecTy = ty(Op);
SDValue VecV = Op.getOperand(0);
SDValue ValV = Op.getOperand(1);
SDValue IdxV = Op.getOperand(2);
@@ -1962,14 +1959,6 @@ HexagonTargetLowering::LowerHvxInsertElement(SDValue Op, SelectionDAG &DAG)
if (ElemTy == MVT::i1)
return insertHvxElementPred(VecV, IdxV, ValV, dl, DAG);
- if (ElemTy == MVT::f16 || ElemTy == MVT::bf16) {
- SDValue T0 = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
- tyVector(VecTy, MVT::i16),
- DAG.getBitcast(tyVector(VecTy, MVT::i16), VecV),
- DAG.getBitcast(MVT::i16, ValV), IdxV);
- return DAG.getBitcast(tyVector(VecTy, ElemTy), T0);
- }
-
return insertHvxElementReg(VecV, IdxV, ValV, dl, DAG);
}
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 775fd7642f040..51c93fcaf80b9 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -2580,10 +2580,9 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
addRegisterClass(MVT::v16bf16, Subtarget.hasAVX512() ? &X86::VR256XRegClass
: &X86::VR256RegClass);
// We set the type action of bf16 to TypeSoftPromoteHalf, but we don't
- // provide the method to promote BUILD_VECTOR and INSERT_VECTOR_ELT.
- // Set the operation action Custom to do the customization later.
+ // provide the method to promote BUILD_VECTOR. Set the operation action
+ // Custom to do the customization later.
setOperationAction(ISD::BUILD_VECTOR, MVT::bf16, Custom);
- setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::bf16, Custom);
for (auto VT : {MVT::v8bf16, MVT::v16bf16}) {
setF16Action(VT, Expand);
if (!Subtarget.hasBF16())
@@ -19355,14 +19354,6 @@ SDValue X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
SDValue N2 = Op.getOperand(2);
auto *N2C = dyn_cast<ConstantSDNode>(N2);
- if (EltVT == MVT::bf16) {
- MVT IVT = VT.changeVectorElementTypeToInteger();
- SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, IVT,
- DAG.getBitcast(IVT, N0),
- DAG.getBitcast(MVT::i16, N1), N2);
- return DAG.getBitcast(VT, Res);
- }
-
if (!N2C) {
// Variable insertion indices, usually we're better off spilling to stack,
// but AVX512 can use a variable compare+select by comparing against all
More information about the llvm-commits
mailing list