Skip to content

fix(cl):same float-to-int cast overflow behavior with go#1544

Merged
xushiwei merged 6 commits intogoplus:mainfrom
luoliwoshang:fix-float-to-int-cast
Jan 14, 2026
Merged

fix(cl):same float-to-int cast overflow behavior with go#1544
xushiwei merged 6 commits intogoplus:mainfrom
luoliwoshang:fix-float-to-int-cast

Conversation

@luoliwoshang
Copy link
Member

@luoliwoshang luoliwoshang commented Jan 12, 2026

Summary

  • Fix float->int conversions for narrow integer targets by routing through 64-bit conversion and truncation.
  • Preserve Go wrap/truncate semantics for out-of-range float values.
  • Avoid LLVM poison results from direct fptosi/fptoui on narrow types.

Fixed

Problem

The repro from issue #1538 panics because LLVM's fptosi/fptoui on narrow integer types yields poison for out-of-range inputs. That poison is later used and results diverge from Go's expected behavior.

Root Cause

ssa/expr.go used llvm.CreateFPToSI/FPToUI directly with the destination integer type. For small widths, the conversion is undefined/poison when the input float is outside the representable range.

Fix

  • Introduce a helper that converts float->int via 64-bit first, then truncates to the target width.
  • For unsigned targets, split negative vs non-negative paths to match Go's behavior before truncation.
  • Keep existing behavior for >=64-bit integer targets (documented inline in ssa/expr.go).

References

Files Touched

  • ssa/expr.go
  • _demo/go/issue1538/main.go
  • _demo/go/issue1538-floatcvtuint-over/main.go
  • cl/_testrt/cast/out.ll

Verification

  • llgo run _demo/go/issue1538-floatcvtuint-over/main.go

Notes

  • This change is intentionally scoped to float->int conversions; int->int and int->float paths are unchanged.

@gemini-code-assist
Copy link

Summary of Changes

Hello @luoliwoshang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the float-to-integer conversion mechanism within the compiler's SSA (Static Single Assignment) form. The primary goal is to prevent undefined behavior in LLVM when converting out-of-range floating-point values to narrow integer types. By introducing an intermediate 64-bit conversion and subsequent truncation, the change guarantees that Go's specified wrap/truncate semantics are consistently applied, improving the reliability and correctness of type conversions.

Highlights

  • Float-to-Int Conversion Fix: Addressed and fixed the behavior of float-to-integer conversions, specifically for narrow integer targets.
  • Undefined Behavior Avoidance: Eliminated LLVM undefined behavior that could occur during out-of-range float-to-integer casts by routing conversions through a 64-bit intermediate step.
  • Go Semantics Compliance: Ensured that the conversions now correctly implement Go's expected wrap/truncate semantics after conversion.
  • New Helper Function: Introduced a new helper function, castFloatToInt, to encapsulate the robust float-to-integer conversion logic.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a critical issue with float-to-integer conversions that could lead to undefined behavior in LLVM. The approach of routing conversions for narrow integer types through a 64-bit intermediate representation before truncation is sound and correctly implements Go's wrap/truncate semantics while avoiding LLVM's UB. The new castFloatToInt function neatly encapsulates this logic. I have one suggestion to further improve the code's maintainability.

@xgopilot
Copy link
Contributor

xgopilot bot commented Jan 12, 2026

Code Review Summary

This PR successfully fixes the LLVM undefined behavior for float-to-int conversions on narrow integer types. The implementation is functionally correct and improves security by ensuring deterministic, specification-compliant conversion semantics.

Key strengths:

  • Correctly routes narrow integer conversions through int64 to avoid undefined behavior
  • Properly handles edge cases (NaN, Infinity, negative-to-unsigned conversions)
  • Matches Go's wrap/truncate semantics

Main suggestion: Add function documentation to explain the rationale for the two-step conversion approach, especially given this addresses issue #1538. See inline comment for details.

@luoliwoshang luoliwoshang force-pushed the fix-float-to-int-cast branch from 3bdcc83 to 081d4cb Compare January 12, 2026 06:45
@codecov
Copy link

codecov bot commented Jan 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.01%. Comparing base (01ee853) to head (422b996).
⚠️ Report is 40 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1544   +/-   ##
=======================================
  Coverage   91.01%   91.01%           
=======================================
  Files          45       45           
  Lines       11958    11971   +13     
=======================================
+ Hits        10883    10896   +13     
  Misses        899      899           
  Partials      176      176           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@luoliwoshang luoliwoshang requested a review from visualfc January 13, 2026 14:52
@luoliwoshang luoliwoshang requested a review from xushiwei January 14, 2026 04:12
@luoliwoshang
Copy link
Member Author

@xushiwei

@luoliwoshang luoliwoshang changed the title Fix float-to-int cast overflow behavior fix:fix float-to-int cast overflow behavior Jan 14, 2026
@luoliwoshang luoliwoshang changed the title fix:fix float-to-int cast overflow behavior fix:same float-to-int cast overflow behavior with go Jan 14, 2026
@luoliwoshang luoliwoshang changed the title fix:same float-to-int cast overflow behavior with go fix(cl):same float-to-int cast overflow behavior with go Jan 14, 2026
@xushiwei xushiwei merged commit 475fe56 into goplus:main Jan 14, 2026
45 checks passed
xgopilot bot pushed a commit that referenced this pull request Jan 15, 2026
Now that issue #1538 is fixed by PR #1544, remove the skip marker (;)
from expect.txt to enable the cast test. The test runs successfully
with no output (it only panics on error).

Closes #1561

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <[email protected]>
@xgopilot xgopilot bot mentioned this pull request Jan 15, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cl/_testrt/cast: error with panic

3 participants