Skip to content
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

8315066: Add unsigned bounds and known bits to TypeInt/Long #17508

Open
wants to merge 57 commits into
base: master
Choose a base branch
from

Conversation

merykitty
Copy link
Member

@merykitty merykitty commented Jan 20, 2024

Hi,

This patch adds unsigned bounds and known bits constraints to TypeInt and TypeLong. This opens more transformation opportunities in an elegant manner as well as helps avoid some ad-hoc rules in Hotspot.

In general, a TypeInt/Long represents a set of values x that satisfies: x s>= lo && x s<= hi && x u>= ulo && x u<= uhi && (x & zeros) == 0 && (x & ones) == ones. These constraints are not independent, e.g. an int that lies in [0, 3] in signed domain must also lie in [0, 3] in unsigned domain and have all bits but the last 2 being unset. As a result, we must canonicalize the constraints (tighten the constraints so that they are optimal) before constructing a TypeInt/Long instance.

This is extracted from #15440 , node value transformations are left for later PRs. I have also added unit tests to verify the soundness of constraint normalization.

Please kindly review, thanks a lot.

Testing

  • GHA
  • Linux x64, tier 1-4

Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8315066: Add unsigned bounds and known bits to TypeInt/Long (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/17508/head:pull/17508
$ git checkout pull/17508

Update a local copy of the PR:
$ git checkout pull/17508
$ git pull https://git.openjdk.org/jdk.git pull/17508/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 17508

View PR using the GUI difftool:
$ git pr show -t 17508

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/17508.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 20, 2024

👋 Welcome back qamai! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 20, 2024
@openjdk
Copy link

openjdk bot commented Jan 20, 2024

@merykitty The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@merykitty
Copy link
Member Author

@TobiHartmann @eme64 I have extracted a part of #15440, could you take a look when you have time, please? Thanks a lot for your help.

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merykitty I just had a quick look. Thanks for spitting out parts and making it more reviewable that way! Since John Rose is generally excited (#15440 (comment)), I'll now put in a bit more effort into reviewing this.

Thanks for adding some gtests.
I would really like to see some IR tests, where we can see that this folds cases, and folds them correctly.
And just some general java-code correctness tests, which test your optimizations in an end-to-end way.

I have a general concern with the math functions. They have quite a few arguments, often 5-10. And on average half of them are passed as a reference. Sometimes it is hard to immediately see which are the arguments that will not be mutated, and which are return values, and which are both arguments and return values, which are simply further constrained/narrowed etc.

I wonder if it might be better to have types like:

SRange<int/long> {lo, hi}
URange<int/long> {lo, hi}
KnownBits<int/long> {ones, zeros}

Make them immutable, i.e. the fields are constant.
Then as function parameters, you always pass in these as const, and return the new values (possibly in some combined type, or a pair or tuple or whatever).

I think it would make the code cleaner, have fewer arguments, and a bit easier to reason about when things are immutable.

Plus, then you can put the range-inference methods inside those classes, you can directly ask such an object if it is empty etc. You could for example have somelthing like:
SRange::constrained_with(KnownBits) -> returns SRange. Basically I'm asking for the code to be a little more object-oriented, and less C-style ;)

@merykitty
Copy link
Member Author

@eme64 Thanks a lot for your reviews, I hope that I have addressed your concerns.

Regarding IR tests, I don't think I can come up with any as there is no node taking advantage of the additional information yet.

Copy link
Member

@jaskarth jaskarth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a quick look through the patch, this is really impressive :)

Early last year I had an attempt at the same idea (extremely rough patch, sorry), where I went with the approach of using a 2-bit value for each bit position to represent 0, 1, BOTTOM, and TOP. My general idea was to create a boolean lattice so that the meet() and dual() operations were easier to implement, before I realized how difficult reasoning about multiple constraints in the meet and dual operations was. I think your idea of marking the dual makes more sense and is cleaner, especially with how the constraints interact.

@merykitty
Copy link
Member Author

@jaskarth Thanks for looking into this patch. I have tried not having an explicit _dual field but in the end it is too hard and cumbersome without any benefits so I end up with this approach. I will address your suggestions in the next iteration. Regarding contains vs higher_equal, it is mainly due to the fact that contains being a much cheaper operation while higher_equal will do a meet followed by a hash table indexing.

@jaskarth
Copy link
Member

Regarding contains vs higher_equal, it is mainly due to the fact that contains being a much cheaper operation while higher_equal will do a meet followed by a hash table indexing.

I forgot that higher_equal requires hashconsing- that makes sense to me!

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 24, 2024

@merykitty This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@merykitty
Copy link
Member Author

Keep alive.

@openjdk
Copy link

openjdk bot commented Mar 13, 2024

@merykitty This change is no longer ready for integration - check the PR body for details.

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 12, 2024

@merykitty This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented May 10, 2024

@merykitty This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@merykitty
Copy link
Member Author

/open

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 17, 2025

@merykitty This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@merykitty
Copy link
Member Author

@eme64 It would be great if you can continue reviewing this PR, please.

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merykitty I've always had this on the list. I'm a little tired today, so won't tough the math heavy stuff, but I have some other suggestions. I'll try to put in a bit of time multiple time a week to get through this eventually.

if (!nbits._is_result_consistent) {
return SimpleCanonicalResult<U>::make_empty();
}
AdjustResult<RangeInt<U>> nbounds{true, true, bounds};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nbounds: what is the n here?

Since you are doing this iteratively, I think nbits -> current_bits, and nbounds -> current_bounds would be more explicit.

Alternatively, you rename the inputs to old_..., and just call the ones you keep updating bits and bounds. Up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed "new", I renamed it to canonicalized_bits and canonicalized_bounds

Comment on lines 360 to 361
if (!nbounds._progress || !nbounds._is_result_consistent) {
return {nbounds._is_result_consistent, nbounds._result, nbits._result};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds a little scary to me:
It sounds like we could have inconsistent results: !nbounds._is_result_consistent and then simply return the nbounds._result. Is that ok, or just confusing wording?
I think again here empty would be our friend: returning an empty result is more sane than returning an inconsistent one 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the _present flag is false then it does not matter what is in the results.

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more for today. More tomorrow or later.

@merykitty
Copy link
Member Author

@eme64 I have added exhaustive tests that scan all the possible inputs for intn_t with n <= 4. Doing this with n == 5 would result in around 2^33 combinations which will take too much time.

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this stuff is really impressive!

I'm dedicated to doing this bit by bit, as it takes a lot of concentration and time.

I'm leaving you with a list of comments, and hope we can interactively get there 😊

Thanks for your patience! If I'm too slow, and there are other reviewers that are willing to reviewer faster, then someone else can take it over, but otherwise I will just keep on reviewing at my speed 😅

@merykitty
Copy link
Member Author

@eme64 Thanks a lot for your effort, don't worry and just take your time.

@merykitty
Copy link
Member Author

@eme64 Ping

@openjdk
Copy link

openjdk bot commented Feb 12, 2025

@merykitty this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout unsignedbounds
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Feb 12, 2025
@eme64
Copy link
Contributor

eme64 commented Feb 13, 2025

@merykitty Sorry, I've been sick for a week and only just catching up with things again slowly...

@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Feb 13, 2025
Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things are looking steadily better! I have a few more suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

7 participants