Skip to content

Conversation

@dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Nov 12, 2025

When convertToInteger fails, the integer result is undefined. In this case, we cannot use it in the subsequent steps.
Close #167627.

@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

When convertToInteger fails, the integer result is undefined. In this case, we cannot use it in the subsequent steps.
Close #167627.


Full diff: https://github.com/llvm/llvm-project/pull/167699.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/Float2Int.cpp (+8-4)
  • (added) llvm/test/Transforms/Float2Int/pr167627.ll (+18)
diff --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index 14686ce8c2ab6..37822cf05f144 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -237,10 +237,14 @@ std::optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
       // OK, it's representable. Now get it.
       APSInt Int(MaxIntegerBW+1, false);
       bool Exact;
-      CF->getValueAPF().convertToInteger(Int,
-                                         APFloat::rmNearestTiesToEven,
-                                         &Exact);
-      OpRanges.push_back(ConstantRange(Int));
+      APFloat::opStatus Status = CF->getValueAPF().convertToInteger(
+          Int, APFloat::rmNearestTiesToEven, &Exact);
+      // Although the round above is loseless, we still need to check if the
+      // floating-point value can be represented in the integer type.
+      if (Status == APFloat::opOK || Status == APFloat::opInexact)
+        OpRanges.push_back(ConstantRange(Int));
+      else
+        return badRange();
     } else {
       llvm_unreachable("Should have already marked this as badRange!");
     }
diff --git a/llvm/test/Transforms/Float2Int/pr167627.ll b/llvm/test/Transforms/Float2Int/pr167627.ll
new file mode 100644
index 0000000000000..a170c21af7a2a
--- /dev/null
+++ b/llvm/test/Transforms/Float2Int/pr167627.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=float2int < %s | FileCheck %s
+
+; Make sure that we don't demote constant floating-point values when
+; it cannot be represented by target integer type.
+
+define i1 @pr167627() {
+; CHECK-LABEL: define i1 @pr167627() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[FADD:%.*]] = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp one float [[FADD]], 0.000000e+00
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %fadd = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
+  %cmp = fcmp one float %fadd, 0.000000e+00
+  ret i1 %cmp
+}

Copy link
Contributor

@antoniofrighetto antoniofrighetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does make sense, thanks.

@dtcxzyw dtcxzyw merged commit edd8b29 into llvm:main Nov 13, 2025
12 checks passed
@dtcxzyw dtcxzyw deleted the fix-167627 branch November 13, 2025 12:54
@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 27, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-windows running on linaro-armv8-windows-msvc-05 while building llvm at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/12965

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: functionalities/breakpoint/move_nearest/TestMoveNearest.py (345 of 2352)
PASS: lldb-api :: functionalities/breakpoint/nested_breakpoint_commands/TestNestedBreakpointCommands.py (346 of 2352)
UNSUPPORTED: lldb-api :: functionalities/breakpoint/objc/TestObjCBreakpoints.py (347 of 2352)
PASS: lldb-api :: functionalities/breakpoint/same_cu_name/TestFileBreakpointsSameCUName.py (348 of 2352)
XFAIL: lldb-api :: functionalities/breakpoint/scripted_bkpt/was_hit/TestWasHit.py (349 of 2352)
PASS: lldb-api :: functionalities/breakpoint/scripted_bkpt/TestScriptedResolver.py (350 of 2352)
PASS: lldb-api :: functionalities/breakpoint/source_regexp/TestSourceRegexBreakpoints.py (351 of 2352)
PASS: lldb-api :: functionalities/breakpoint/serialize/TestBreakpointSerialization.py (352 of 2352)
PASS: lldb-api :: functionalities/breakpoint/step_over_breakpoint/TestStepOverBreakpoint.py (353 of 2352)
PASS: lldb-api :: functionalities/breakpoint/two_hits_one_actual/TestTwoHitsOneActual.py (354 of 2352)
FAIL: lldb-api :: functionalities/bt-interrupt/TestInterruptBacktrace.py (355 of 2352)
******************** TEST 'lldb-api :: functionalities/bt-interrupt/TestInterruptBacktrace.py' FAILED ********************
Script:
--
C:/Users/tcwg/scoop/apps/python/current/python.exe C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/llvm-project/lldb\test\API\dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./lib --env LLVM_INCLUDE_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/include --env LLVM_TOOLS_DIR=C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin --arch aarch64 --build-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex --lldb-module-cache-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex/module-cache-lldb\lldb-api --clang-module-cache-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/lldb-test-build.noindex/module-cache-clang\lldb-api --executable C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/lldb.exe --compiler C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/clang.exe --dsymutil C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin/dsymutil.exe --make C:/Users/tcwg/scoop/shims/make.exe --llvm-tools-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./bin --lldb-obj-root C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/tools/lldb --lldb-libs-dir C:/Users/tcwg/llvm-worker/lldb-aarch64-windows/build/./lib --cmake-build-type Release --skip-category=watchpoint C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\API\functionalities\bt-interrupt -p TestInterruptBacktrace.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision edd8b29667716101ed026baf3aa4befb37ac132a)
  clang revision edd8b29667716101ed026baf3aa4befb37ac132a
  llvm revision edd8b29667716101ed026baf3aa4befb37ac132a
Skipping the following test categories: ['watchpoint', 'libc++', 'libstdcxx', 'dwo', 'dsym', 'gmodules', 'debugserver', 'objc', 'fork', 'pexpect']

Number of frames: 1


--
Command Output (stderr):
--
FAIL: LLDB (C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\bin\clang.exe-aarch64) :: test_backtrace_interrupt (TestInterruptBacktrace.TestInterruptingBacktrace.test_backtrace_interrupt)

======================================================================

FAIL: test_backtrace_interrupt (TestInterruptBacktrace.TestInterruptingBacktrace.test_backtrace_interrupt)

   Use RequestInterrupt followed by stack operations

----------------------------------------------------------------------

Traceback (most recent call last):

  File "C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\packages\Python\lldbsuite\test\decorators.py", line 156, in wrapper

    return func(*args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Float2Int] Miscompilation at O3

4 participants