8329194: Cleanup Type::cmp definition and usage#18533
8329194: Cleanup Type::cmp definition and usage#18533jaskarth wants to merge 2 commits intoopenjdk:masterfrom
Conversation
|
👋 Welcome back jkarthikeyan! A progress list of the required criteria for merging this PR into |
|
@jaskarth This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 405 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@dafedafe, @chhagedorn, @merykitty) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
Webrevs
|
src/hotspot/share/opto/type.cpp
Outdated
|
|
||
| // Map the boolean result of Type::cmp into a comparator result that CmpKey expects. | ||
| auto type_cmp = [](const void* t1, const void* t2) -> int32_t { | ||
| return Type::cmp((Type*) t1, (Type*) t2) ? 0 : 1; |
There was a problem hiding this comment.
Wouldn't return !Type::cmp((Type*) t1, (Type*) t2); be enough? (though it might actually result in the same compiled code)
There was a problem hiding this comment.
I think this would work too, but I wanted to avoid the implicit boolean to integer conversion. I can make this change if it would be fine, though.
dafedafe
left a comment
There was a problem hiding this comment.
Looks good (I also tested tier1-5).
|
Great, thank you for the review and testing! |
chhagedorn
left a comment
There was a problem hiding this comment.
Overall a good improvement and makes it more intuitive. A few comments.
| // Create a new hash-consd type | ||
| static const Type *make(enum TYPES); | ||
| // Test for equivalence of types | ||
| static int cmp( const Type *const t1, const Type *const t2 ); |
There was a problem hiding this comment.
Was it required to remove the consts here?
There was a problem hiding this comment.
Since the second const is referring to the t1/t2 it means that the parameters can't be modified, which IIRC in the header would be functionally the same as not having the additional const. I changed it in type.cpp as well because looking at other parts of the code const Type* val is used a lot more often than const Type* const val for arguments.
src/hotspot/share/opto/type.hpp
Outdated
| static const Type *make(enum TYPES); | ||
| // Test for equivalence of types | ||
| static int cmp( const Type *const t1, const Type *const t2 ); | ||
| static bool cmp(const Type* t1, const Type* t2); |
There was a problem hiding this comment.
I wonder if this will be a bit confusing to people now. Usually, cmp methods tend to have an output that allows sorting, i.e. 0 for equals, and either larger or smaller than 0, depending on which one is larger.
For a bool return, the name equals would be more adequate. But this would also require lots of changes in the code-base, and maybe make backports harder.
Actually, I'd be worried about backports now in general:
Imagine someone now writes if (Type::cmp(...)). And someone else backports this, since the patch seems to cleanly apply. if (Type::cmp(...)) is also correct C++ before your change.
But the semantics now have changed: with your patch, the if succeeds if the types are equal. Without your patch, the if succeeds if the types are not equal. Yikes. This will lead to some very subtle bugs and annoying debugging in old JDK versions.
Hence, I would think you have to rename the method to equals and change all usages accordingly.
What do you think?
There was a problem hiding this comment.
That's actually a good point about the backports. I was also wondering if equals is better due to the cmp convention of using ints but thought it's okay. But with backports in mind, it gives a stronger case to actually go with equals - so, I agree with your suggestion @eme64.
There was a problem hiding this comment.
This is a good point, I hadn't thought about backports! In the top comment I was also thinking about naming it Type::equals, but I worried that it'd be too close to Type::eq. But with this detail I think it should be renamed as well, because of the change of semantics. That way at least it'll cause a compile error instead of silently breaking at runtime, which would be bad.
|
Thanks for the comments @chhagedorn and @eme64! I've pushed a commit that should address the points brought up in review, and renamed the function to |
|
|
||
| static int uhash( const Type *const t ); | ||
| // Structural equality check. Assumes that cmp() has already compared | ||
| // Structural equality check. Assumes that equals() has already compared |
There was a problem hiding this comment.
Is this even correct? Because we use eq inside equals, (before your patch, we used eq inside cmp).
Should this not rather mean that we should have already done the ==?
What do you think?
There was a problem hiding this comment.
I think it's still correct, because from my understanding it's referring to this line in equals:
if (t1->_base != t2->_base) {
return false; // Missed badly
}equals only calls eqafter this, so eq knows that the underlying type of both classes are the same. So TypeInt::eq, for example, is able to safely cast the incoming Type* t to a TypeInt* to do a structural equality check.
|
@jaskarth this looks good. I am running testing again now. @merykitty do you have an opinion on this? You have done quite some work on types. |
merykitty
left a comment
There was a problem hiding this comment.
I think it is a really nice change. It is confusing to have a cmp function on a type with no order.
Cheers,
Quan Anh
|
Thanks for taking another look @eme64, and for the review @merykitty :) |
chhagedorn
left a comment
There was a problem hiding this comment.
Looks good, thanks for the updates!
|
Thanks for taking another look @chhagedorn! /integrate |
|
/sponsor |
|
Going to push as commit b9927aa.
Your commit was automatically rebased without conflicts. |
|
@TobiHartmann @jaskarth Pushed as commit b9927aa. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Hi all, this patch aims to cleanup
Type::cmpby changing it from returning a0when types are equal and1when they are not, to it returning a boolean denoting equality. This makes its usages at various callsites more intuitive. However, as it is passed to the type dictionary as a comparator, a lambda is needed to map the boolean to a comparison value.I was also considering changing the name to
Type::equalsas it's not really returning a comparison value anymore, but I felt it would be too similar toType::eq. If this would be preferred though, I can change it.Tier 1 testing passes on my machine. Reviews and thoughts would be appreciated!
Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/18533/head:pull/18533$ git checkout pull/18533Update a local copy of the PR:
$ git checkout pull/18533$ git pull https://git.openjdk.org/jdk.git pull/18533/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 18533View PR using the GUI difftool:
$ git pr show -t 18533Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/18533.diff
Webrev
Link to Webrev Comment