[llvm] [llubi] Implement the `nsz` flag for `uitofp`/`sitofp` (PR #222858)
Zhige Chen via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 23:02:38 PDT 2026
https://github.com/nofe1248 updated https://github.com/llvm/llvm-project/pull/222858
>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 1/2] [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);
});
}
>From 2ccde5085096cc4fa622bf28673a6f99fe176af2 Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Sat, 12 Sep 2026 14:02:23 +0800
Subject: [PATCH 2/2] [llubi] Implement ninf flag
---
llvm/test/tools/llubi/fp_cast.ll | 10 ++++++++++
llvm/tools/llubi/lib/Interpreter.cpp | 3 +++
2 files changed, 13 insertions(+)
diff --git a/llvm/test/tools/llubi/fp_cast.ll b/llvm/test/tools/llubi/fp_cast.ll
index ffc6530631158..0eda2fa2adef1 100644
--- a/llvm/test/tools/llubi/fp_cast.ll
+++ b/llvm/test/tools/llubi/fp_cast.ll
@@ -19,6 +19,12 @@ define void @main() {
%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>
+ %si2fp_ninf = sitofp ninf i32 -100000 to half
+ %ui2fp_ninf = uitofp ninf i32 100000 to half
+
+ %si2fp_ninf_vec = sitofp ninf <4 x i32> <i32 -100000, i32 -100000, i32 -100000, i32 -100000> to <4 x half>
+ %ui2fp_ninf_vec = uitofp ninf <4 x i32> <i32 100000, i32 100000, i32 100000, i32 100000> to <4 x half>
+
%ui2fp_nneg_pos = uitofp nneg i32 255 to float
%ui2fp_nneg_neg = uitofp nneg i32 -255 to float
@@ -46,6 +52,10 @@ define void @main() {
; 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: %si2fp_ninf = sitofp ninf i32 -100000 to half => poison
+; CHECK-NEXT: %ui2fp_ninf = uitofp ninf i32 100000 to half => poison
+; CHECK-NEXT: %si2fp_ninf_vec = sitofp ninf <4 x i32> splat (i32 -100000) to <4 x half> => { poison, poison, poison, poison }
+; CHECK-NEXT: %ui2fp_ninf_vec = uitofp ninf <4 x i32> splat (i32 100000) to <4 x half> => { poison, poison, poison, poison }
; 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 c4f6db3dcb991..f62e10d461c4c 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.hasNoInfs())
+ return AnyValue::poison();
+
if (I.hasNoSignedZeros() && IOperand.isZero() && Ctx.getRandomBool())
Res.changeSign();
More information about the llvm-commits
mailing list