[llvm] [IR] Allow fast math flags on fptosi, fptoui and sitofp (PR #160475)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 24 02:28:34 PDT 2025


https://github.com/paperchalice created https://github.com/llvm/llvm-project/pull/160475

This allow optimizations like `FoldIntToFPToInt` in CodeGen to use fast math flags.

>From ea2a7e330143a53b311f7efab48c1ba444ec33e5 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Wed, 24 Sep 2025 17:26:32 +0800
Subject: [PATCH] [IR] Allow fast math flags on fptosi, fptoui and sitofp

---
 llvm/docs/LangRef.rst                  |  3 +-
 llvm/include/llvm/IR/Operator.h        |  3 ++
 llvm/lib/AsmParser/LLParser.cpp        |  6 +--
 llvm/test/Assembler/fast-math-flags.ll | 66 ++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 4 deletions(-)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index e6713c827d6ab..76a296e599d47 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -3960,7 +3960,8 @@ Fast-Math Flags
 LLVM IR floating-point operations (:ref:`fneg <i_fneg>`, :ref:`fadd <i_fadd>`,
 :ref:`fsub <i_fsub>`, :ref:`fmul <i_fmul>`, :ref:`fdiv <i_fdiv>`,
 :ref:`frem <i_frem>`, :ref:`fcmp <i_fcmp>`, :ref:`fptrunc <i_fptrunc>`,
-:ref:`fpext <i_fpext>`), and :ref:`phi <i_phi>`, :ref:`select <i_select>`, or
+:ref:`fpext <i_fpext>`), :ref:`fptoui <i_fptoui>`, :ref:`fptosi <i_fptosi>`,
+:ref:`sitofp <i_sitofp>`, and :ref:`phi <i_phi>`, :ref:`select <i_select>`, or
 :ref:`call <i_call>` instructions that return floating-point types may use the
 following flags to enable otherwise unsafe floating-point transformations.
 
diff --git a/llvm/include/llvm/IR/Operator.h b/llvm/include/llvm/IR/Operator.h
index 10816c0e62c29..b725e01462e70 100644
--- a/llvm/include/llvm/IR/Operator.h
+++ b/llvm/include/llvm/IR/Operator.h
@@ -362,6 +362,9 @@ class FPMathOperator : public Operator {
     case Instruction::FRem:
     case Instruction::FPTrunc:
     case Instruction::FPExt:
+    case Instruction::FPToUI:
+    case Instruction::FPToSI:
+    case Instruction::SIToFP:
     // FIXME: To clean up and correct the semantics of fast-math-flags, FCmp
     //        should not be treated as a math op, but the other opcodes should.
     //        This would make things consistent with Select/PHI (FP value type
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 897e679095906..c2f856aa65bf6 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -7319,13 +7319,13 @@ int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
   case lltok::kw_sext:
   case lltok::kw_bitcast:
   case lltok::kw_addrspacecast:
-  case lltok::kw_sitofp:
-  case lltok::kw_fptoui:
-  case lltok::kw_fptosi:
   case lltok::kw_inttoptr:
   case lltok::kw_ptrtoaddr:
   case lltok::kw_ptrtoint:
     return parseCast(Inst, PFS, KeywordVal);
+  case lltok::kw_sitofp:
+  case lltok::kw_fptoui:
+  case lltok::kw_fptosi:
   case lltok::kw_fptrunc:
   case lltok::kw_fpext: {
     FastMathFlags FMF = EatFastMathFlagsIfPresent();
diff --git a/llvm/test/Assembler/fast-math-flags.ll b/llvm/test/Assembler/fast-math-flags.ll
index 9c08e9da1d19e..2a725cd445182 100644
--- a/llvm/test/Assembler/fast-math-flags.ll
+++ b/llvm/test/Assembler/fast-math-flags.ll
@@ -56,6 +56,24 @@ entry:
   %h_vec = fptrunc <3 x float> %vec to <3 x half>
 ; CHECK: %h_scalable = fptrunc <vscale x 3 x float> %scalable to <vscale x 3 x half>
   %h_scalable = fptrunc <vscale x 3 x float> %scalable to <vscale x 3 x half>
+; CHECK: %i = fptoui float %x to i32
+  %i = fptoui float %x to i32
+; CHECK: %i_vec = fptoui <3 x float> %vec to <3 x i32>
+  %i_vec = fptoui <3 x float> %vec to <3 x i32>
+; CHECK: %i_scalable = fptoui <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %i_scalable = fptoui <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %j = fptosi float %x to i32
+  %j = fptosi float %x to i32
+; CHECK: %j_vec = fptosi <3 x float> %vec to <3 x i32>
+  %j_vec = fptosi <3 x float> %vec to <3 x i32>
+; CHECK: %j_scalable = fptosi <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %j_scalable = fptosi <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %k = sitofp i32 %j to float
+  %k = sitofp i32 %j to float
+; CHECK: %k_vec = sitofp <3 x i32> %j_vec to <3 x float>
+  %k_vec = sitofp <3 x i32> %j_vec to <3 x float>
+; CHECK: %k_scalable = sitofp <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
+  %k_scalable = sitofp <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
 ; CHECK:  ret float %f
   ret  float %f
 }
@@ -108,6 +126,24 @@ entry:
   %h_vec = fptrunc nnan <3 x float> %vec to <3 x half>
 ; CHECK: %h_scalable = fptrunc nnan <vscale x 3 x float> %scalable to <vscale x 3 x half>
   %h_scalable = fptrunc nnan <vscale x 3 x float> %scalable to <vscale x 3 x half>
+; CHECK: %i = fptoui nnan float %x to i32
+  %i = fptoui nnan float %x to i32
+; CHECK: %i_vec = fptoui nnan <3 x float> %vec to <3 x i32>
+  %i_vec = fptoui nnan <3 x float> %vec to <3 x i32>
+; CHECK: %i_scalable = fptoui nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %i_scalable = fptoui nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %j = fptosi nnan float %x to i32
+  %j = fptosi nnan float %x to i32
+; CHECK: %j_vec = fptosi nnan <3 x float> %vec to <3 x i32>
+  %j_vec = fptosi nnan <3 x float> %vec to <3 x i32>
+; CHECK: %j_scalable = fptosi nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %j_scalable = fptosi nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %k = sitofp nnan i32 %j to float
+  %k = sitofp nnan i32 %j to float
+; CHECK: %k_vec = sitofp nnan <3 x i32> %j_vec to <3 x float>
+  %k_vec = sitofp nnan <3 x i32> %j_vec to <3 x float>
+; CHECK: %k_scalable = sitofp nnan <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
+  %k_scalable = sitofp nnan <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
 ; CHECK:  ret float %f
   ret float %f
 }
@@ -125,6 +161,12 @@ entry:
   %d = fpext contract float %x to double
 ; CHECK: %e = fptrunc contract float %x to half
   %e = fptrunc contract float %x to half
+; CHECK: %f = fptoui contract float %x to i32
+  %f = fptoui contract float %x to i32
+; CHECK: %g = fptosi contract float %x to i32
+  %g = fptosi contract float %x to i32
+; CHECK: %h = sitofp contract i32 %g to float
+  %h = sitofp contract i32 %g to float
   ret float %c
 }
 
@@ -140,6 +182,12 @@ define float @reassoc(float %x, float %y) {
   %d = fpext reassoc float %x to double
 ; CHECK: %e = fptrunc reassoc float %x to half
   %e = fptrunc reassoc float %x to half
+; CHECK: %f = fptoui reassoc float %x to i32
+  %f = fptoui reassoc float %x to i32
+; CHECK: %g = fptosi reassoc float %x to i32
+  %g = fptosi reassoc float %x to i32
+; CHECK: %h = sitofp reassoc i32 %g to float
+  %h = sitofp reassoc i32 %g to float
   ret float %c
 }
 
@@ -198,6 +246,24 @@ entry:
   %g_vec = fptrunc ninf nnan <3 x float> %vec to <3 x half>
 ; CHECK: %g_scalable = fptrunc nnan ninf <vscale x 3 x float> %scalable to <vscale x 3 x half>
   %g_scalable = fptrunc ninf nnan <vscale x 3 x float> %scalable to <vscale x 3 x half>
+; CHECK: %i = fptoui nnan ninf float %x to i32
+  %i = fptoui ninf nnan float %x to i32
+; CHECK: %i_vec = fptoui nnan ninf <3 x float> %vec to <3 x i32>
+  %i_vec = fptoui ninf nnan <3 x float> %vec to <3 x i32>
+; CHECK: %i_scalable = fptoui nnan ninf <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %i_scalable = fptoui ninf nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %j = fptosi nnan ninf float %x to i32
+  %j = fptosi ninf nnan float %x to i32
+; CHECK: %j_vec = fptosi nnan ninf <3 x float> %vec to <3 x i32>
+  %j_vec = fptosi ninf nnan <3 x float> %vec to <3 x i32>
+; CHECK: %j_scalable = fptosi nnan ninf <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+  %j_scalable = fptosi ninf nnan <vscale x 3 x float> %scalable to <vscale x 3 x i32>
+; CHECK: %k = sitofp nnan ninf i32 %j to float
+  %k = sitofp ninf nnan i32 %j to float
+; CHECK: %k_vec = sitofp nnan ninf <3 x i32> %j_vec to <3 x float>
+  %k_vec = sitofp ninf nnan <3 x i32> %j_vec to <3 x float>
+; CHECK: %k_scalable = sitofp nnan ninf <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
+  %k_scalable = sitofp ninf nnan <vscale x 3 x i32> %j_scalable to <vscale x 3 x float>
 ; CHECK:  ret float %e
   ret float %e
 }



More information about the llvm-commits mailing list