[llvm] fa43610 - [RISCV] Expand scalar FP undef/poison to 0.0 with F extension. (#225567)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 22 21:50:59 PDT 2026
Author: Craig Topper
Date: 2026-09-22T21:50:51-07:00
New Revision: fa43610d69293a36e54accc5d7864b93ced9c8a4
URL: https://github.com/llvm/llvm-project/commit/fa43610d69293a36e54accc5d7864b93ced9c8a4
DIFF: https://github.com/llvm/llvm-project/commit/fa43610d69293a36e54accc5d7864b93ced9c8a4.diff
LOG: [RISCV] Expand scalar FP undef/poison to 0.0 with F extension. (#225567)
This ensures the undef/poison value is always nan-boxed. This is
important if the value ends up being used by a freeze. If the value
isn't properly nan-boxed, it will be treated as a nan in FP contexts
regardless of its lower bits. If the freeze is also cast to an integer,
the lack of nan-boxing will be noticed and the integer will see the real
value of the lower bits.
Fixes #225455.
Added:
Modified:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/bfloat-imm.ll
llvm/test/CodeGen/RISCV/double-imm.ll
llvm/test/CodeGen/RISCV/float-imm.ll
llvm/test/CodeGen/RISCV/half-imm.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 387d96dcda3ab..3e610a3842c6b 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -729,6 +729,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
}
if (Subtarget.hasStdExtZfbfmin()) {
+ setOperationAction({ISD::UNDEF, ISD::POISON}, MVT::bf16, Expand);
setOperationAction(ISD::BITCAST, MVT::i16, Custom);
setOperationAction(ISD::ConstantFP, MVT::bf16, Expand);
setOperationAction(ISD::SELECT_CC, MVT::bf16, Expand);
@@ -744,6 +745,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
}
if (Subtarget.hasStdExtZfhminOrZhinxmin()) {
+ if (Subtarget.hasStdExtZfhmin())
+ setOperationAction({ISD::UNDEF, ISD::POISON}, MVT::f16, Expand);
if (Subtarget.hasStdExtZfhOrZhinx()) {
setOperationAction(FPLegalNodeTypes, MVT::f16, Legal);
setOperationAction(FPRndMode, MVT::f16,
@@ -808,6 +811,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
}
if (Subtarget.hasStdExtFOrZfinx()) {
+ if (Subtarget.hasStdExtF())
+ setOperationAction({ISD::UNDEF, ISD::POISON}, MVT::f32, Expand);
setOperationAction(FPLegalNodeTypes, MVT::f32, Legal);
setOperationAction(FPRndMode, MVT::f32,
Subtarget.hasStdExtZfa() ? Legal : Custom);
@@ -843,6 +848,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
setOperationAction(ISD::BITCAST, MVT::i32, Custom);
if (Subtarget.hasStdExtDOrZdinx()) {
+ if (Subtarget.hasStdExtD())
+ setOperationAction({ISD::UNDEF, ISD::POISON}, MVT::f64, Expand);
setOperationAction(FPLegalNodeTypes, MVT::f64, Legal);
if (!Subtarget.is64Bit())
diff --git a/llvm/test/CodeGen/RISCV/bfloat-imm.ll b/llvm/test/CodeGen/RISCV/bfloat-imm.ll
index 3c94b7bd70ea3..d8564b0b43f49 100644
--- a/llvm/test/CodeGen/RISCV/bfloat-imm.ll
+++ b/llvm/test/CodeGen/RISCV/bfloat-imm.ll
@@ -43,3 +43,19 @@ define bfloat @bfloat_negative_zero() nounwind {
; CHECK-NEXT: ret
ret bfloat -0.0
}
+
+define bfloat @poison() nounwind {
+; CHECK-LABEL: poison:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.h.x fa0, zero
+; CHECK-NEXT: ret
+ ret bfloat poison
+}
+
+define bfloat @undef() nounwind {
+; CHECK-LABEL: undef:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.h.x fa0, zero
+; CHECK-NEXT: ret
+ ret bfloat undef
+}
diff --git a/llvm/test/CodeGen/RISCV/double-imm.ll b/llvm/test/CodeGen/RISCV/double-imm.ll
index 9f31c95f64dc3..36dcc565b22f3 100644
--- a/llvm/test/CodeGen/RISCV/double-imm.ll
+++ b/llvm/test/CodeGen/RISCV/double-imm.ll
@@ -170,3 +170,45 @@ entry:
%d. = select i1 %tobool.not, double %d, double -0.000000e+00
ret double %d.
}
+
+define double @poison() nounwind {
+; CHECK32D-LABEL: poison:
+; CHECK32D: # %bb.0:
+; CHECK32D-NEXT: fcvt.d.w fa0, zero
+; CHECK32D-NEXT: ret
+;
+; CHECK64D-LABEL: poison:
+; CHECK64D: # %bb.0:
+; CHECK64D-NEXT: fmv.d.x fa0, zero
+; CHECK64D-NEXT: ret
+;
+; CHECKRV32ZDINX-LABEL: poison:
+; CHECKRV32ZDINX: # %bb.0:
+; CHECKRV32ZDINX-NEXT: ret
+;
+; CHECKRV64ZDINX-LABEL: poison:
+; CHECKRV64ZDINX: # %bb.0:
+; CHECKRV64ZDINX-NEXT: ret
+ ret double poison
+}
+
+define double @undef() nounwind {
+; CHECK32D-LABEL: undef:
+; CHECK32D: # %bb.0:
+; CHECK32D-NEXT: fcvt.d.w fa0, zero
+; CHECK32D-NEXT: ret
+;
+; CHECK64D-LABEL: undef:
+; CHECK64D: # %bb.0:
+; CHECK64D-NEXT: fmv.d.x fa0, zero
+; CHECK64D-NEXT: ret
+;
+; CHECKRV32ZDINX-LABEL: undef:
+; CHECKRV32ZDINX: # %bb.0:
+; CHECKRV32ZDINX-NEXT: ret
+;
+; CHECKRV64ZDINX-LABEL: undef:
+; CHECKRV64ZDINX: # %bb.0:
+; CHECKRV64ZDINX-NEXT: ret
+ ret double undef
+}
diff --git a/llvm/test/CodeGen/RISCV/float-imm.ll b/llvm/test/CodeGen/RISCV/float-imm.ll
index 610c72b5f932e..a4f2b4493f90c 100644
--- a/llvm/test/CodeGen/RISCV/float-imm.ll
+++ b/llvm/test/CodeGen/RISCV/float-imm.ll
@@ -68,3 +68,27 @@ define float @float_negative_zero(ptr %pf) nounwind {
; CHECKZFINX-NEXT: ret
ret float -0.0
}
+
+define float @poison() nounwind {
+; CHECK-LABEL: poison:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.w.x fa0, zero
+; CHECK-NEXT: ret
+;
+; CHECKZFINX-LABEL: poison:
+; CHECKZFINX: # %bb.0:
+; CHECKZFINX-NEXT: ret
+ ret float poison
+}
+
+define float @undef() nounwind {
+; CHECK-LABEL: undef:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.w.x fa0, zero
+; CHECK-NEXT: ret
+;
+; CHECKZFINX-LABEL: undef:
+; CHECKZFINX: # %bb.0:
+; CHECKZFINX-NEXT: ret
+ ret float undef
+}
diff --git a/llvm/test/CodeGen/RISCV/half-imm.ll b/llvm/test/CodeGen/RISCV/half-imm.ll
index 8fbaca5c8adc0..3e1180b8b2b6c 100644
--- a/llvm/test/CodeGen/RISCV/half-imm.ll
+++ b/llvm/test/CodeGen/RISCV/half-imm.ll
@@ -134,3 +134,45 @@ define half @half_negative_zero(ptr %pf) nounwind {
; CHECKIZHINXMIN-NEXT: ret
ret half -0.0
}
+
+define half @poison() nounwind {
+; CHECK-LABEL: poison:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.h.x fa0, zero
+; CHECK-NEXT: ret
+;
+; CHECKIZHINX-LABEL: poison:
+; CHECKIZHINX: # %bb.0:
+; CHECKIZHINX-NEXT: ret
+;
+; CHECKIZFHMIN-LABEL: poison:
+; CHECKIZFHMIN: # %bb.0:
+; CHECKIZFHMIN-NEXT: fmv.h.x fa0, zero
+; CHECKIZFHMIN-NEXT: ret
+;
+; CHECKIZHINXMIN-LABEL: poison:
+; CHECKIZHINXMIN: # %bb.0:
+; CHECKIZHINXMIN-NEXT: ret
+ ret half poison
+}
+
+define half @undef() nounwind {
+; CHECK-LABEL: undef:
+; CHECK: # %bb.0:
+; CHECK-NEXT: fmv.h.x fa0, zero
+; CHECK-NEXT: ret
+;
+; CHECKIZHINX-LABEL: undef:
+; CHECKIZHINX: # %bb.0:
+; CHECKIZHINX-NEXT: ret
+;
+; CHECKIZFHMIN-LABEL: undef:
+; CHECKIZFHMIN: # %bb.0:
+; CHECKIZFHMIN-NEXT: fmv.h.x fa0, zero
+; CHECKIZFHMIN-NEXT: ret
+;
+; CHECKIZHINXMIN-LABEL: undef:
+; CHECKIZHINXMIN: # %bb.0:
+; CHECKIZHINXMIN-NEXT: ret
+ ret half undef
+}
More information about the llvm-commits
mailing list