Skip to content

Conversation

@guocju
Copy link
Contributor

@guocju guocju commented Jan 29, 2026

…loormod

This patch fixes an issue in the LowerIntrin pass where incorrect optimizations were applied to floordiv and floormod operations.

The root cause is that the pass attempts to find an equivalent representation for floordiv(a, b) by calculating the expression (op->b - 1) - a_min. This expression, when subjected to constant folding, can potentially overflow the range of int32 or int16. When this overflow occurs, the transformation becomes invalid and no longer equivalent to the original operation.

To fix this, we enhanced the condition under which the transformation is applied. The new condition ensures that the transformation is only performed when (b_max - a_min) is less than INT_MAX + 2. If this condition is not met, the transformation is skipped and the common lowering steps are followed to ensure correctness.

A regression test has been added to cover this case.

Fixes #18684

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guocju, 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 addresses a critical bug within the LowerIntrin pass that caused erroneous optimizations for floordiv and floormod operations. The root cause was an integer overflow during constant folding of an intermediate expression, leading to invalid transformations. The solution involves refining the conditions under which these optimizations are applied, specifically by adding a check to prevent the transformation when the bounds could lead to overflow. This ensures that the lowering steps are correctly followed, and a new test case has been included to validate the fix.

Highlights

  • Bug Fix for Floordiv/Floormod Optimization: This pull request resolves an issue in the LowerIntrin pass where incorrect optimizations were applied to floordiv and floormod operations due to potential integer overflow during constant folding.
  • Improved Optimization Condition: The fix introduces an enhanced condition for applying the optimization, ensuring it only proceeds when (b_max - a_min) is less than INT_MAX + 2, thereby preventing overflow and maintaining correctness.
  • Regression Test Added: A new regression test has been added to specifically cover the scenario that previously led to the incorrect optimization, ensuring the fix is robust.

🧠 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
Contributor

@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 bug in the LowerIntrin pass where an optimization for floordiv and floormod could cause an integer overflow. The fix correctly introduces a new condition to safeguard against this overflow by checking the bounds of the operands before applying the transformation. The logic is sound and has been applied to both floordiv and floormod operations. Additionally, new regression tests have been added to cover the fixed cases. My review includes a couple of minor suggestions to improve code readability by extracting a repeated complex expression into a local variable.

@tqchen
Copy link
Member

tqchen commented Jan 29, 2026

Thanks @guocju please cross check genimi review, and add explicit comment for context, the ci issue should be fixed by #18695 thanks a lot for the contribution

…loormod

This patch fixes an issue in the LowerIntrin pass where incorrect optimizations
were applied to floordiv and floormod operations.

The root cause is that the pass attempts to find an equivalent representation for
floordiv(a, b) by calculating the expression (op->b - 1) - a_min. This expression,
when subjected to constant folding, can potentially overflow the range of int32 or
int16. When this overflow occurs, the transformation becomes invalid and no longer
equivalent to the original operation.

To fix this, we enhanced the condition under which the transformation is applied.
The new condition ensures that the transformation is only performed when (b_max - a_min)
is less than INT_MAX + 2. If this condition is not met, the transformation is skipped
and the common lowering steps are followed to ensure correctness.

A regression test has been added to cover this case.

Fixes apache#18684

add comment
@guocju guocju force-pushed the bugfix/lower_intrin branch from f537177 to d88e041 Compare January 30, 2026 08:26
@guocju
Copy link
Contributor Author

guocju commented Jan 30, 2026

Yes, I’ve updated the check to explicitly extract max_value_of_dtype as a variable, and added a comment documenting why the condition is <= max_value_of_dtype + 1.

@tqchen
Copy link
Member

tqchen commented Jan 30, 2026

/gemini review

Copy link
Contributor

@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 bug in the LowerIntrin pass where an optimization for floordiv and floormod could lead to integer overflows and incorrect results. The fix introduces a more robust safety check that considers the bounds of both the numerator and denominator, ensuring the optimization is only applied when safe. This change is correctly applied to both floordiv and floormod lowering logic. Additionally, new regression tests are added to verify the correctness of the fallback path when the optimization is skipped. The fix appears correct and effectively resolves the issue. I have one suggestion to improve code maintainability by refactoring some duplicated logic.

@tqchen tqchen merged commit b4dc8a5 into apache:main Jan 30, 2026
10 checks passed
@tqchen
Copy link
Member

tqchen commented Jan 30, 2026

Thanks @guocju for the bugfix!

@tqchen
Copy link
Member

tqchen commented Jan 30, 2026

#18699 contains some followups to further clean things up and robustify the scenario. thanks @guocju for bringing up the initial case

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.

[Bug] Incorrect optimization for tir.floordiv/floormod

2 participants