[llvm] [llubi] Implement fast-math flags for `uitofp`/`sitofp` (PR #222858)
Zhige Chen via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 12 09:10:52 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/3] [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/3] [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();
>From 2b49e7403cb69f294ce2483ee3b89a668def5e00 Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Sun, 13 Sep 2026 00:10:31 +0800
Subject: [PATCH 3/3] [llubi] A silly mistake :(
---
llvm/test/tools/llubi/fp_cast.ll | 26 ++++++++++++++++++--------
llvm/tools/llubi/lib/Interpreter.cpp | 10 +++-------
2 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/llvm/test/tools/llubi/fp_cast.ll b/llvm/test/tools/llubi/fp_cast.ll
index 0eda2fa2adef1..879d93a9b2620 100644
--- a/llvm/test/tools/llubi/fp_cast.ll
+++ b/llvm/test/tools/llubi/fp_cast.ll
@@ -19,11 +19,17 @@ 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_oob = sitofp ninf i32 -100000 to half
+ %ui2fp_ninf_oob = 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>
+ %si2fp_ninf_vec_oob = sitofp ninf <4 x i32> <i32 -100000, i32 -100000, i32 -100000, i32 -100000> to <4 x half>
+ %ui2fp_ninf_vec_oob = uitofp ninf <4 x i32> <i32 100000, i32 100000, i32 100000, i32 100000> to <4 x half>
+
+ %si2fp_ninf = sitofp ninf i32 -100000 to float
+ %ui2fp_ninf = uitofp ninf i32 100000 to float
+
+ %si2fp_ninf_vec = sitofp ninf <4 x i32> <i32 -100000, i32 -100000, i32 -100000, i32 -100000> to <4 x float>
+ %ui2fp_ninf_vec = uitofp ninf <4 x i32> <i32 100000, i32 100000, i32 100000, i32 100000> to <4 x float>
%ui2fp_nneg_pos = uitofp nneg i32 255 to float
%ui2fp_nneg_neg = uitofp nneg i32 -255 to float
@@ -52,10 +58,14 @@ 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: %si2fp_ninf_oob = sitofp ninf i32 -100000 to half => poison
+; CHECK-NEXT: %ui2fp_ninf_oob = uitofp ninf i32 100000 to half => poison
+; CHECK-NEXT: %si2fp_ninf_vec_oob = sitofp ninf <4 x i32> splat (i32 -100000) to <4 x half> => { poison, poison, poison, poison }
+; CHECK-NEXT: %ui2fp_ninf_vec_oob = uitofp ninf <4 x i32> splat (i32 100000) to <4 x half> => { poison, poison, poison, poison }
+; CHECK-NEXT: %si2fp_ninf = sitofp ninf i32 -100000 to float => float -1.000000e+05
+; CHECK-NEXT: %ui2fp_ninf = uitofp ninf i32 100000 to float => float 1.000000e+05
+; CHECK-NEXT: %si2fp_ninf_vec = sitofp ninf <4 x i32> splat (i32 -100000) to <4 x float> => { float -1.000000e+05, float -1.000000e+05, float -1.000000e+05, float -1.000000e+05 }
+; CHECK-NEXT: %ui2fp_ninf_vec = uitofp ninf <4 x i32> splat (i32 100000) to <4 x float> => { float 1.000000e+05, float 1.000000e+05, float 1.000000e+05, float 1.000000e+05 }
; 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 f62e10d461c4c..cce896a5f6676 100644
--- a/llvm/tools/llubi/lib/Interpreter.cpp
+++ b/llvm/tools/llubi/lib/Interpreter.cpp
@@ -2473,6 +2473,7 @@ class InstExecutor : public InstVisitor<InstExecutor, void>,
void visitIntToFPInst(Instruction &I, bool IsSigned) {
const fltSemantics &DstSem =
I.getType()->getScalarType()->getFltSemantics();
+ FastMathFlags FMF = cast<FPMathOperator>(I).getFastMathFlags();
visitUnOp(I, [&](const AnyValue &Operand) -> AnyValue {
if (Operand.isPoison())
@@ -2488,13 +2489,8 @@ 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();
-
- return AnyValue(Res);
+ // We need IsInput=true here because the nsz flag applies to the output.
+ return handleFMFFlags(Res, FMF, /*IsInput=*/true);
});
}
More information about the llvm-commits
mailing list