[llvm] [llubi] Implement the `nsz` flag for `uitofp`/`sitofp` (PR #222858)

Zhige Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 23:46:31 PDT 2026


https://github.com/nofe1248 created https://github.com/llvm/llvm-project/pull/222858

Closes #203773

>From 2e0f402780a95fd6bb1e40e3bb703a9db2a79544 Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhige_chen at outlook.com>
Date: Fri, 11 Sep 2026 14:45:00 +0800
Subject: [PATCH] [llubi] Implement the nsz flag for uitofp/sitofp

---
 llvm/test/tools/llubi/fp_cast.ll     | 19 +++++++++++++++++++
 llvm/tools/llubi/lib/Interpreter.cpp |  3 +++
 2 files changed, 22 insertions(+)

diff --git a/llvm/test/tools/llubi/fp_cast.ll b/llvm/test/tools/llubi/fp_cast.ll
index f7b2ac234db13..ffc6530631158 100644
--- a/llvm/test/tools/llubi/fp_cast.ll
+++ b/llvm/test/tools/llubi/fp_cast.ll
@@ -8,6 +8,17 @@ define void @main() {
   %si2fp = sitofp i32 -2 to float
   %ui2fp = uitofp i32 255 to float
 
+  %si2fp_zero = sitofp i32 0 to float
+  %ui2fp_zero = uitofp i32 0 to float
+
+  %si2fp_nsz_zero_1 = sitofp nsz i32 0 to float
+  %si2fp_nsz_zero_2 = sitofp nsz i32 0 to float
+  %ui2fp_nsz_zero_1 = uitofp nsz i32 0 to float
+  %ui2fp_nsz_zero_2 = uitofp nsz i32 0 to float
+
+  %si2fp_nsz_zero_vec = sitofp nsz <4 x i32> zeroinitializer to <4 x float>
+  %ui2fp_nsz_zero_vec = uitofp nsz <4 x i32> zeroinitializer to <4 x float>
+
   %ui2fp_nneg_pos = uitofp nneg i32 255 to float
   %ui2fp_nneg_neg = uitofp nneg i32 -255 to float
 
@@ -27,6 +38,14 @@ define void @main() {
 ; CHECK-NEXT:   %trunc = fptrunc double 3.500000e+00 to float => float 3.500000e+00
 ; CHECK-NEXT:   %si2fp = sitofp i32 -2 to float => float -2.000000e+00
 ; CHECK-NEXT:   %ui2fp = uitofp i32 255 to float => float 2.550000e+02
+; CHECK-NEXT:   %si2fp_zero = sitofp i32 0 to float => float 0.000000e+00
+; CHECK-NEXT:   %ui2fp_zero = uitofp i32 0 to float => float 0.000000e+00
+; CHECK-NEXT:   %si2fp_nsz_zero_1 = sitofp nsz i32 0 to float => float 0.000000e+00
+; CHECK-NEXT:   %si2fp_nsz_zero_2 = sitofp nsz i32 0 to float => float -0.000000e+00
+; CHECK-NEXT:   %ui2fp_nsz_zero_1 = uitofp nsz i32 0 to float => float -0.000000e+00
+; CHECK-NEXT:   %ui2fp_nsz_zero_2 = uitofp nsz i32 0 to float => float 0.000000e+00
+; CHECK-NEXT:   %si2fp_nsz_zero_vec = sitofp nsz <4 x i32> zeroinitializer to <4 x float> => { float 0.000000e+00, float 0.000000e+00, float -0.000000e+00, float 0.000000e+00 }
+; CHECK-NEXT:   %ui2fp_nsz_zero_vec = uitofp nsz <4 x i32> zeroinitializer to <4 x float> => { float 0.000000e+00, float -0.000000e+00, float -0.000000e+00, float 0.000000e+00 }
 ; CHECK-NEXT:   %ui2fp_nneg_pos = uitofp nneg i32 255 to float => float 2.550000e+02
 ; CHECK-NEXT:   %ui2fp_nneg_neg = uitofp nneg i32 -255 to float => poison
 ; CHECK-NEXT:   %fp2si = fptosi double -4.750000e+00 to i32 => i32 -4
diff --git a/llvm/tools/llubi/lib/Interpreter.cpp b/llvm/tools/llubi/lib/Interpreter.cpp
index 77b7fae4c4ab3..c4f6db3dcb991 100644
--- a/llvm/tools/llubi/lib/Interpreter.cpp
+++ b/llvm/tools/llubi/lib/Interpreter.cpp
@@ -2488,6 +2488,9 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
       Res.convertFromAPInt(Operand.asInteger(), /*IsSigned=*/IsSigned,
                            Ctx.getCurrentRoundingMode());
 
+      if (I.hasNoSignedZeros() && IOperand.isZero() && Ctx.getRandomBool())
+        Res.changeSign();
+
       return AnyValue(Res);
     });
   }



More information about the llvm-commits mailing list