[llvm] [WebAssembly] Fix legalization of v8f16 insertelement (PR #212992)
Anutosh Bhat via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 30 04:52:35 PDT 2026
https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/212992
>From 9c7eed8475f79d5c46867e8ca27072d47f322374 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Thu, 30 Jul 2026 16:13:07 +0530
Subject: [PATCH 1/2] [WebAssembly] Fix legalization of v8f16 insertelement
---
.../Target/WebAssembly/WebAssemblyISelLowering.cpp | 14 ++++++++++++++
llvm/test/CodeGen/WebAssembly/f16-intrinsics.ll | 8 ++++++++
2 files changed, 22 insertions(+)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index bd716a90dfbc9..c5ba8fbb167f1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -257,6 +257,7 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
if (Subtarget->hasFP16()) {
setOperationAction(ISD::BUILD_VECTOR, MVT::f16, Custom);
+ setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::f16, Custom);
setOperationAction(ISD::FP_ROUND, MVT::v4f16, Custom);
}
@@ -2818,6 +2819,19 @@ SDValue WebAssemblyTargetLowering::LowerSETCC(SDValue Op,
SDValue
WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op,
SelectionDAG &DAG) const {
+ if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
+ Op.getValueType() == MVT::v8f16) {
+ // INSERT_VECTOR_ELT can't handle FP16 operands since Wasm doesn't have a
+ // scaler FP16 type, so cast them to I16s.
+ SDLoc DL(Op);
+ SDValue IntVector = DAG.getBitcast(MVT::v8i16, Op.getOperand(0));
+ SDValue IntElement = DAG.getBitcast(MVT::i16, Op.getOperand(1));
+ SDValue Inserted =
+ DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, MVT::v8i16, IntVector,
+ IntElement, Op.getOperand(2));
+ return DAG.getBitcast(MVT::v8f16, Inserted);
+ }
+
// Allow constant lane indices, expand variable lane indices
SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode();
if (isa<ConstantSDNode>(IdxNode)) {
diff --git a/llvm/test/CodeGen/WebAssembly/f16-intrinsics.ll b/llvm/test/CodeGen/WebAssembly/f16-intrinsics.ll
index 1eb1ca707a304..264f3601486b8 100644
--- a/llvm/test/CodeGen/WebAssembly/f16-intrinsics.ll
+++ b/llvm/test/CodeGen/WebAssembly/f16-intrinsics.ll
@@ -81,6 +81,14 @@ define <8 x half> @replace_lane_v8f16(<8 x half> %v, float %f) {
ret <8 x half> %r
}
+; CHECK-LABEL: insert_lane_v8f16:
+; CHECK: i16x8.replace_lane $push0=, $0, 3, $1
+; CHECK-NEXT: return $pop0
+define <8 x half> @insert_lane_v8f16(<8 x half> %v, half %x) {
+ %r = insertelement <8 x half> %v, half %x, i32 3
+ ret <8 x half> %r
+}
+
; CHECK-LABEL: add_v8f16:
; CHECK: f16x8.add $push0=, $0, $1
; CHECK-NEXT: return $pop0
>From 36e1b4532662c8710a5fa179d392bb5c12efce23 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Thu, 30 Jul 2026 17:22:13 +0530
Subject: [PATCH 2/2] code format
---
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index c5ba8fbb167f1..f6f4fc7b7c3bc 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -2826,9 +2826,8 @@ WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op,
SDLoc DL(Op);
SDValue IntVector = DAG.getBitcast(MVT::v8i16, Op.getOperand(0));
SDValue IntElement = DAG.getBitcast(MVT::i16, Op.getOperand(1));
- SDValue Inserted =
- DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, MVT::v8i16, IntVector,
- IntElement, Op.getOperand(2));
+ SDValue Inserted = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, MVT::v8i16,
+ IntVector, IntElement, Op.getOperand(2));
return DAG.getBitcast(MVT::v8f16, Inserted);
}
More information about the llvm-commits
mailing list