[llvm] [llubi] Fix floating-point denormal flushing behavior (PR #229698)
Zhige Chen via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 01:57:56 PDT 2026
https://github.com/nofe1248 created https://github.com/llvm/llvm-project/pull/229698
Fix `handleDenormal()` function to match the LangRef and correct FP conversions to apply output denormal handling to their results.
>From 094808ca2462584ae18ea4fbf9fa28024e8f6924 Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Wed, 7 Oct 2026 16:54:55 +0800
Subject: [PATCH] [llubi] Fix floating-point denormal flushing behavior
---
llvm/test/tools/llubi/fp_denorm_input.ll | 74 +++++++++++++++++++++++
llvm/test/tools/llubi/fp_denorm_output.ll | 70 +++++++++++++++++++++
llvm/tools/llubi/lib/Interpreter.cpp | 16 ++---
3 files changed, 153 insertions(+), 7 deletions(-)
create mode 100644 llvm/test/tools/llubi/fp_denorm_input.ll
create mode 100644 llvm/test/tools/llubi/fp_denorm_output.ll
diff --git a/llvm/test/tools/llubi/fp_denorm_input.ll b/llvm/test/tools/llubi/fp_denorm_input.ll
new file mode 100644
index 0000000000000..8f32129da7d92
--- /dev/null
+++ b/llvm/test/tools/llubi/fp_denorm_input.ll
@@ -0,0 +1,74 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: llubi --verbose --seed=0 < %s 2>&1 | FileCheck %s
+; RUN: llubi --verbose --seed=1 < %s 2>&1 | FileCheck %s
+; RUN: llubi --verbose --deterministic < %s 2>&1 | FileCheck %s
+
+define void @main() {
+ call void @positivezero()
+ call void @preservesign()
+ call void @ieee()
+ ret void
+}
+
+define void @positivezero() denormal_fpenv(ieee|positivezero) {
+ %positive = fmul float f0x00400000, 2.0
+ %negative = fmul float f0x80400000, 2.0
+ %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, <float 2.0, float 2.0>
+ %compare = fcmp oeq float f0x00400000, 0.0
+ %extend_positive = fpext float f0x00400000 to double
+ %extend_negative = fpext float f0x80400000 to double
+ ret void
+}
+
+define void @preservesign() denormal_fpenv(ieee|preservesign) {
+ %positive = fmul float f0x00400000, 2.0
+ %negative = fmul float f0x80400000, 2.0
+ %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, <float 2.0, float 2.0>
+ %compare = fcmp oeq float f0x00400000, 0.0
+ %extend_positive = fpext float f0x00400000 to double
+ %extend_negative = fpext float f0x80400000 to double
+ ret void
+}
+
+define void @ieee() denormal_fpenv(ieee|ieee) {
+ %positive = fmul float f0x00400000, 2.0
+ %negative = fmul float f0x80400000, 2.0
+ %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, <float 2.0, float 2.0>
+ %compare = fcmp oeq float f0x00400000, 0.0
+ %extend_positive = fpext float f0x00400000 to double
+ %extend_negative = fpext float f0x80400000 to double
+ ret void
+}
+; CHECK: Entering function: main
+; CHECK-NEXT: Entering function: positivezero
+; CHECK-NEXT: %positive = fmul float f0x00400000, 2.000000e+00 => float 0.000000e+00
+; CHECK-NEXT: %negative = fmul float f0x80400000, 2.000000e+00 => float 0.000000e+00
+; CHECK-NEXT: %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, splat (float 2.000000e+00) => { float 0.000000e+00, float 0.000000e+00 }
+; CHECK-NEXT: %compare = fcmp oeq float f0x00400000, 0.000000e+00 => T
+; CHECK-NEXT: %extend_positive = fpext float f0x00400000 to double => double 0.000000e+00
+; CHECK-NEXT: %extend_negative = fpext float f0x80400000 to double => double 0.000000e+00
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: positivezero
+; CHECK-NEXT: call void @positivezero()
+; CHECK-NEXT: Entering function: preservesign
+; CHECK-NEXT: %positive = fmul float f0x00400000, 2.000000e+00 => float 0.000000e+00
+; CHECK-NEXT: %negative = fmul float f0x80400000, 2.000000e+00 => float -0.000000e+00
+; CHECK-NEXT: %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, splat (float 2.000000e+00) => { float 0.000000e+00, float -0.000000e+00 }
+; CHECK-NEXT: %compare = fcmp oeq float f0x00400000, 0.000000e+00 => T
+; CHECK-NEXT: %extend_positive = fpext float f0x00400000 to double => double 0.000000e+00
+; CHECK-NEXT: %extend_negative = fpext float f0x80400000 to double => double -0.000000e+00
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: preservesign
+; CHECK-NEXT: call void @preservesign()
+; CHECK-NEXT: Entering function: ieee
+; CHECK-NEXT: %positive = fmul float f0x00400000, 2.000000e+00 => float 0x00800000
+; CHECK-NEXT: %negative = fmul float f0x80400000, 2.000000e+00 => float 0x80800000
+; CHECK-NEXT: %vector = fmul <2 x float> <float f0x00400000, float f0x80400000>, splat (float 2.000000e+00) => { float 0x00800000, float 0x80800000 }
+; CHECK-NEXT: %compare = fcmp oeq float f0x00400000, 0.000000e+00 => F
+; CHECK-NEXT: %extend_positive = fpext float f0x00400000 to double => double 0x3800000000000000
+; CHECK-NEXT: %extend_negative = fpext float f0x80400000 to double => double 0xB800000000000000
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: ieee
+; CHECK-NEXT: call void @ieee()
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: main
diff --git a/llvm/test/tools/llubi/fp_denorm_output.ll b/llvm/test/tools/llubi/fp_denorm_output.ll
new file mode 100644
index 0000000000000..8cd6720897210
--- /dev/null
+++ b/llvm/test/tools/llubi/fp_denorm_output.ll
@@ -0,0 +1,70 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: llubi --verbose < %s 2>&1 | FileCheck %s
+
+define void @main() {
+ call void @positivezero()
+ call void @preservesign()
+ call void @ieee()
+ ret void
+}
+
+define void @positivezero() denormal_fpenv(positivezero|ieee) {
+ %positive = fmul float f0x1F800000, f0x1F800000
+ %negative = fmul float f0x9F800000, f0x1F800000
+ %vector = fmul <4 x float> <float f0x1F800000, float f0x9F800000, float f0x1F800000, float f0x9F800000>, <float f0x1F800000, float f0x1F800000, float f0x1F800000, float f0x1F800000>
+ %truncate_positive = fptrunc double 0x37F0000000000000 to float
+ %truncate_negative = fptrunc double 0xB7F0000000000000 to float
+ %truncate_vector = fptrunc <4 x double> <double 0x37F0000000000000, double 0xB7F0000000000000, double 0x37F0000000000000, double 0xB7F0000000000000> to <4 x float>
+ ret void
+}
+
+define void @preservesign() denormal_fpenv(preservesign|ieee) {
+ %positive = fmul float f0x1F800000, f0x1F800000
+ %negative = fmul float f0x9F800000, f0x1F800000
+ %vector = fmul <4 x float> <float f0x1F800000, float f0x9F800000, float f0x1F800000, float f0x9F800000>, <float f0x1F800000, float f0x1F800000, float f0x1F800000, float f0x1F800000>
+ %truncate_positive = fptrunc double 0x37F0000000000000 to float
+ %truncate_negative = fptrunc double 0xB7F0000000000000 to float
+ %truncate_vector = fptrunc <4 x double> <double 0x37F0000000000000, double 0xB7F0000000000000, double 0x37F0000000000000, double 0xB7F0000000000000> to <4 x float>
+ ret void
+}
+
+define void @ieee() denormal_fpenv(ieee|ieee) {
+ %positive = fmul float f0x1F800000, f0x1F800000
+ %negative = fmul float f0x9F800000, f0x1F800000
+ %vector = fmul <2 x float> <float f0x1F800000, float f0x9F800000>, <float f0x1F800000, float f0x1F800000>
+ %truncate_positive = fptrunc double 0x37F0000000000000 to float
+ %truncate_negative = fptrunc double 0xB7F0000000000000 to float
+ ret void
+}
+; CHECK: Entering function: main
+; CHECK-NEXT: Entering function: positivezero
+; CHECK-NEXT: %positive = fmul float f0x1F800000, f0x1F800000 => float 0.000000e+00
+; CHECK-NEXT: %negative = fmul float f0x9F800000, f0x1F800000 => float 0x80200000
+; CHECK-NEXT: %vector = fmul <4 x float> <float f0x1F800000, float f0x9F800000, float f0x1F800000, float f0x9F800000>, splat (float f0x1F800000) => { float 0x00200000, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00 }
+; CHECK-NEXT: %truncate_positive = fptrunc double f0x37F0000000000000 to float => float 0x00200000
+; CHECK-NEXT: %truncate_negative = fptrunc double f0xB7F0000000000000 to float => float 0.000000e+00
+; CHECK-NEXT: %truncate_vector = fptrunc <4 x double> <double f0x37F0000000000000, double f0xB7F0000000000000, double f0x37F0000000000000, double f0xB7F0000000000000> to <4 x float> => { float 0.000000e+00, float 0x80200000, float 0x00200000, float 0.000000e+00 }
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: positivezero
+; CHECK-NEXT: call void @positivezero()
+; CHECK-NEXT: Entering function: preservesign
+; CHECK-NEXT: %positive = fmul float f0x1F800000, f0x1F800000 => float 0.000000e+00
+; CHECK-NEXT: %negative = fmul float f0x9F800000, f0x1F800000 => float -0.000000e+00
+; CHECK-NEXT: %vector = fmul <4 x float> <float f0x1F800000, float f0x9F800000, float f0x1F800000, float f0x9F800000>, splat (float f0x1F800000) => { float 0.000000e+00, float 0x80200000, float 0x00200000, float 0x80200000 }
+; CHECK-NEXT: %truncate_positive = fptrunc double f0x37F0000000000000 to float => float 0x00200000
+; CHECK-NEXT: %truncate_negative = fptrunc double f0xB7F0000000000000 to float => float -0.000000e+00
+; CHECK-NEXT: %truncate_vector = fptrunc <4 x double> <double f0x37F0000000000000, double f0xB7F0000000000000, double f0x37F0000000000000, double f0xB7F0000000000000> to <4 x float> => { float 0x00200000, float 0x80200000, float 0.000000e+00, float -0.000000e+00 }
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: preservesign
+; CHECK-NEXT: call void @preservesign()
+; CHECK-NEXT: Entering function: ieee
+; CHECK-NEXT: %positive = fmul float f0x1F800000, f0x1F800000 => float 0x00200000
+; CHECK-NEXT: %negative = fmul float f0x9F800000, f0x1F800000 => float 0x80200000
+; CHECK-NEXT: %vector = fmul <2 x float> <float f0x1F800000, float f0x9F800000>, splat (float f0x1F800000) => { float 0x00200000, float 0x80200000 }
+; CHECK-NEXT: %truncate_positive = fptrunc double f0x37F0000000000000 to float => float 0x00200000
+; CHECK-NEXT: %truncate_negative = fptrunc double f0xB7F0000000000000 to float => float 0x80200000
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: ieee
+; CHECK-NEXT: call void @ieee()
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: main
diff --git a/llvm/tools/llubi/lib/Interpreter.cpp b/llvm/tools/llubi/lib/Interpreter.cpp
index 76f2535abbce4..03a71090b7ea8 100644
--- a/llvm/tools/llubi/lib/Interpreter.cpp
+++ b/llvm/tools/llubi/lib/Interpreter.cpp
@@ -160,12 +160,13 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
bool IsInput) {
if (!Val.isDenormal())
return Val;
- if (IsInput) {
- // Non-deterministically choose between flushing or preserving the
- // denormal value.
- if (Ctx.getRandomBool())
- return Val;
- }
+ // Input flushing is required, but output flushing is optional. Model both
+ // permitted output behaviors when the mode allows flushing.
+ if (!IsInput &&
+ (Mode == DenormalMode::PositiveZero ||
+ Mode == DenormalMode::PreserveSign) &&
+ Ctx.getRandomBool())
+ return Val;
if (Mode == DenormalMode::PositiveZero)
return APFloat::getZero(Val.getSemantics(), false);
if (Mode == DenormalMode::PreserveSign)
@@ -2517,7 +2518,8 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
ValidateRes.isPoison())
return ValidateRes;
- FOperand = handleDenormal(std::move(FOperand), DenormMode.Output, true);
+ FOperand = handleDenormal(std::move(FOperand), DenormMode.Output,
+ /*IsInput=*/false);
return AnyValue(applyNaNPropagation(FOperand, {&SourceNaN}));
});
More information about the llvm-commits
mailing list