You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "tqchen (via GitHub)" <gi...@apache.org> on 2026/01/30 15:04:54 UTC
[PR] [TIR] Further robustify floordiv/mod intrin lowering to prevent overflow [tvm]
tqchen opened a new pull request, #18699:
URL: https://github.com/apache/tvm/pull/18699
This PR further robustifies floordiv/mod intrin lowering in cases where we can to shift negative values to positive range but also need to carefully prevent overflow in intermediate compiler checks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [TIR] Further robustify floordiv/mod intrin lowering to prevent overflow [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18699:
URL: https://github.com/apache/tvm/pull/18699#discussion_r2746726772
##########
src/tir/transforms/lower_intrin.cc:
##########
@@ -388,6 +300,49 @@ class IntrinInjecter : public tvm::arith::IRMutatorWithAnalyzer {
return IRMutatorWithAnalyzer::VisitExpr_(op);
}
+ /*!
+ * \brief Try to find a shift co-efficient c such that a + b*c positive and does not overflow.
+ *
+ * \param a the dividend
+ * \param b_value the divisor
+ * \return the shift co-efficient c, or nullopt if not found
+ */
+ std::optional<int64_t> TryFindShiftCoefficientForPositiveRange(const PrimExpr& a,
+ int64_t b_value) {
+ if (b_value <= 0) {
+ return std::nullopt;
+ }
+ // NOTE: we need to be very careful in the checks below, to make sure
+ // all the intermediate calculations in both compiler checks and runtime checks
+ // do not overflow
+ arith::ConstIntBound const_int_bound_a = analyzer_->const_int_bound(a);
+ if (const_int_bound_a->min_value >= 0) {
+ return std::nullopt;
+ }
+ const int64_t max_value_of_dtype =
+ Downcast<IntImm>(tvm::max_value(a->dtype.element_of()))->value;
+
+ // NOTE: ensures that (b-1) - a_min does not overflow
+ // also note: max_value_of_dtype + const_int_bound_a->min_value won't overflow
+ // since a_min is negative, adding it to a positive value will not overflow
+ if (b_value - 1 > max_value_of_dtype + const_int_bound_a->min_value) {
Review Comment:

The overflow check for the intermediate calculation of `c_value` seems overly conservative. The expression `(b_value - 1) - const_int_bound_a->min_value` is computed using `int64_t` arithmetic, so the overflow check should be against `std::numeric_limits<int64_t>::max()`. However, the current check uses `max_value_of_dtype`, which is the maximum value of the expression's data type and can be smaller than `int64_t` (e.g., for `int32`). This can cause this optimization to be disabled unnecessarily when it could be safely applied.
Using `ConstIntBoundNode::kPosInf` (which is `LLONG_MAX`) would make this check more accurate for the `int64_t` calculation, while subsequent checks correctly validate against `max_value_of_dtype` for parts that are used to construct TIR expressions of that dtype.
```suggestion
if (b_value - 1 > ConstIntBoundNode::kPosInf + const_int_bound_a->min_value) {
return std::nullopt;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [TIR] Further robustify floordiv/mod intrin lowering to prevent overflow [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #18699:
URL: https://github.com/apache/tvm/pull/18699#issuecomment-3824223682
## Summary of Changes
Hello @tqchen, I'm Gemini Code Assist[^1]! 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 focuses on improving the stability and correctness of the floordiv and floormod operations within the TIR compiler. It addresses potential overflow issues that can arise when dealing with negative operands, ensuring that the transformations applied during lowering maintain the original semantics of the code. The changes include refined checks and a new utility function to safely shift values into a positive range, along with new test cases to validate the implemented overflow protections.
### Highlights
* **Floordiv/Mod Intrinsic Lowering**: The PR enhances the robustness of floordiv/mod intrinsic lowering by incorporating additional checks to prevent potential overflow issues during the conversion of negative values to a positive range.
* **Overflow Prevention**: Implements checks to ensure that intermediate calculations, specifically those involving shifts and bounds, do not exceed the maximum value of the data type, thus avoiding non-equivalent transformations due to overflow.
* **Code Optimization**: Replaces the previous logic with a new `TryFindShiftCoefficientForPositiveRange` function to determine a suitable shift coefficient, which ensures the numerator remains positive without causing overflow.
* **Testing**: Adds new test cases to specifically target and validate the overflow checks, ensuring the correctness and stability of the floordiv lowering process under various conditions.
🧠**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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
<details>
<summary><b>Using Gemini Code Assist</b></summary>
<br>
The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
<b>Invoking Gemini</b>
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.
<b>Customization</b>
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](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
<b>Limitations & Feedback</b>
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
<b>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](https://cloud.google.com/products/gemini/code-assist).</b>
</details>
[^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [TIR] Further robustify floordiv/mod intrin lowering to prevent overflow [tvm]
Posted by "spectrometerHBH (via GitHub)" <gi...@apache.org>.
spectrometerHBH merged PR #18699:
URL: https://github.com/apache/tvm/pull/18699
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org