-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f… #18694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @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 Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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.
…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
f537177 to
d88e041
Compare
|
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. |
|
/gemini review |
There was a problem hiding this 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.
|
Thanks @guocju for the bugfix! |
…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