llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[KnownBits] Tighten division bounds if the division is exact

Open ZERICO2005 opened this issue 2 months ago • 8 comments

https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1301-L1307

If the division is exact, then we can tighten the bounds on minimum result (or equivalently the minimum numerator).

The theory is if the minimum numerator is not a multiple of the denominator, then we should should round up the minimum numerator to next multiple of the denominator.

I think the best way to implement this would be to increase the minimum result by one if the minimum numerator is not a multiple of the denominator.

We do not need to adjust the maximum numerator since x / y and (x - x % y) / y return the same result.

In other words:

  • Current implementation: udiv_floor(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator)
  • Optimized: udiv_ceil(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator)

When the denominator is not a constant and the numerator is known non-zero, then we know that the numerator must be at least as large as the denominator.

Considerations:

  • Increasing the minimum result might make it greater than the maximum result in poison cases.
  • Similarly: Rounding up the minimum numerator could make it overflow, or otherwise become larger than maximum numerator in poison cases.

ZERICO2005 avatar Sep 24 '26 19:09 ZERICO2005

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Eric Ross (ZERICO2005)

https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1301-L1307

If the division is exact, then we can tighten the bounds on minimum result (or equivalently the minimum numerator).

The theory if the minimum numerator is not a multiple of the denominator, then we should should round up the minimum numerator to next multiple of the denominator.

I think the best way to implement this would be to increase the minimum result by one if the minimum numerator is not a multiple of the denominator.

We do not need to adjust the maximum numerator since x / y and (x - x % y) / y return the same result.

In other words:

  • Current implementation: udiv_floor(min-numerator, max-denominator) &lt;= udiv_exact(x, y) &lt;= udiv_floor(max-numerator, min-denominator)
  • Optimized: udiv_ceil(min-numerator, max-denominator) &lt;= udiv_exact(x, y) &lt;= udiv_floor(max-numerator, min-denominator)

When the denominator is not a constant and the numerator is known non-zero, then we know that the numerator must be at least as large as the denominator.

Considerations:

  • Increasing the minimum result might make it greater than the maximum result in poison cases.
  • Similarly: Rounding up the minimum numerator could make it overflow, or otherwise become larger than maximum numerator in poison cases.

Hi, I would like to be assigned to this issue

Talb057 avatar Sep 27 '26 12:09 Talb057

i would like to work on this

Vara715 avatar Sep 27 '26 13:09 Vara715

@Vara715 I have assigned this issue to @Talb057 since they were the first to reply. You can ask them in a week if they are still working on this issue.

ZERICO2005 avatar Sep 27 '26 13:09 ZERICO2005

Hi is anyone working on this issue? If not, i would like to take it up. Thanks!

KaustAbhinand avatar Oct 06 '26 15:10 KaustAbhinand

@Talb057 Are you still working on this issue? Feel free to ask if you have any questions.

ZERICO2005 avatar Oct 06 '26 16:10 ZERICO2005

Hi, yes I am still actively working on it

Talb057 avatar Oct 06 '26 16:10 Talb057