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

[KnownBits] Improve minimum denominator bounds for `sdiv(negative, positive-or-zero)`

Open ZERICO2005 opened this issue 2 months ago • 12 comments

For the division functions, we can assume that the denominator is non-zero even if we are unable to rule out a zero denominator (i.e. 0b000?, 0b0???, 0b?000, etc). This occurs when the denominator has no known set bits.

udiv chooses the minimum non-zero denominator by setting the lowest unknown bit. https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1295-L1307

However, sdiv does not perform this trick, and will always choose 1 as the minimum non-zero denominator. https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1253-L1260

We can probably copy this trick over to sdiv.

ZERICO2005 avatar Sep 24 '26 20: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)

For the division functions, we can assume that the denominator is non-zero even if we are unable to rule out a zero denominator (i.e. `0b000?`, `0b0???`, `0b?000`, etc). This occurs when the denominator has no known set bits.

udiv chooses the minimum non-zero denominator by setting the lowest unknown bit. https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1295-L1307

However, sdiv does not perform this trick, and will always choose 1 as the minimum non-zero denominator. https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1253-L1260

We can probably copy this trick over to sdiv.

Can i try this

anandkvinu avatar Sep 26 '26 20:09 anandkvinu

is this open, can i be assigned?

Vara715 avatar Sep 27 '26 12:09 Vara715

@Vara715 I have assigned this issue to @anandkvinu 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

@Vara715 hi im working on this and already added the fix , currently running unit tests.

anandkvinu avatar Sep 27 '26 14:09 anandkvinu

@ZERICO2005 Hi, good day. I have tested the 0b000? 0b0??0, 0b0???, using GoogleTest, but the variant 0b?000 is handled by another condition where both Num and Denom are negative. Should I verify that as well? thanks.

anandkvinu avatar Sep 29 '26 05:09 anandkvinu

0b?000 would either be 0 or INT_MIN, so the only valid denominator would be negative in that case. x / INT_MIN is more of an edge case since the result can only be 0 or 1 (if x == INT_MIN).

You can test that case if there is not any coverage for it currently.

ZERICO2005 avatar Sep 29 '26 05:09 ZERICO2005

@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks

anandkvinu avatar Sep 29 '26 07:09 anandkvinu

@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks

x / INT_MIN probably folds to result = (x == INT_MIN) ? 1 : 0, so it would not go through division in that case.

It is probably okay if we skip testing the unknown signbit case.

ZERICO2005 avatar Sep 29 '26 07:09 ZERICO2005

@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks

x / INT_MIN probably folds to result = (x == INT_MIN) ? 1 : 0, so it would not go through division in that case.

It is probably okay if we skip testing the unknown signbit case.

okey thank you.

anandkvinu avatar Sep 29 '26 07:09 anandkvinu

@ZERICO2005 hi, I have opened a PR. Kindly review. Thanks

anandkvinu avatar Sep 29 '26 09:09 anandkvinu