Skip to content

Conversation

@SirYwell
Copy link
Member

@SirYwell SirYwell commented Apr 2, 2025

This change implements constant folding for ReverseBytes nodes.

Currently, byteswap is included transitively by reverse_bits.hpp. I'm not sure if this is fine or if I need to add an explicit include there.

I appreciate any reviews and comments.


Progress

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

Issue

  • JDK-8353551: C2: Constant folding for ReverseBytes nodes (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24382

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 2, 2025

👋 Welcome back hgreule! 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
Copy link

openjdk bot commented Apr 2, 2025

@SirYwell 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:

8353551: C2: Constant folding for ReverseBytes nodes

Reviewed-by: epeter, vlivanov

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 682 new commits pushed to the master branch:

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 (@iwanowww, @eme64) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot changed the title JDK-8353551 8353551: C2: Constant folding for ReverseBytes nodes Apr 2, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 2, 2025
@openjdk
Copy link

openjdk bot commented Apr 2, 2025

@SirYwell 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.

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Apr 2, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 2, 2025

Webrevs

}

template<typename T, BasicType B>
const Type* reverse_bytes(const Node* node, PhaseGVN* phase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be templated with TypeLong/TypeInt instead of BasicType? There is a try_cast that @merykitty added in #17508 that might help.

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 wouldn't change much, but yes. Generally this is an example where #17508 shines, as the code could be generalized to just reverse the bytes of the KnownBits structure. Whether this PR waits until then or we refactor the code afterwards isn't that important to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn’t intend to advocate for waiting for that PR. try_cast is tiny and could be added independently. I have just been looking for things that could generally help with unifying TypeInt/TypeLong implementations.

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I'd prefer to see a unified generic version of ReverseBytesNode::Value() rather than multiple specializations (even templated ones).

Copy link
Member Author

Choose a reason for hiding this comment

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

@j3graham I mainly want to avoid conflicts or duplicated solutions. I meant that after #17508 this code can be further generalized anyway, allowing to use the try_cast function then. This can obviously happen in a separate PR, if this one is integrated before.

@iwanowww I'm not sure if that's possible without more duplication. We need to choose the correct byteswap implementation depending on the node's type. Please let me know if I'm missing something.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I see. That works for me. One problem though, there currently isn't a common ReverseBytesNode type, so I'd need to add that. And I assume that should be a TypeNode then? In that case, as ReverseBytes*Nodes are InvolutionNodes, would InvolutionNode need to be a TypeNode? Or can I use multiple inheritance? (I didn't see any example of that in current Node types). Both are doable, and the first might even make sense, but I'm not sure if it's a bit much for this PR.

Or do you want me to (temporarily) duplicate the Value code (i.e. move more code from the templated function to the Value functions, and only keep the simple reverse_bytes from your snippet)?

Alternatively, I'd also be fine to put this PR on hold and make InvolutionNode a TypeNode first, or whatever you think is best. Please let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I assume that should be a TypeNode then?

Why do you think it benefits from becoming a TypeNode?

In that case, as ReverseBytes*Nodes are InvolutionNodes, would InvolutionNode need to be a TypeNode? Or can I use multiple inheritance?

We try to avoid multiple inheritance in JVM and C2 doesn't use any AFAIK.

Actually, I had an afterthought about InvolutionNode after approving it. It looks a bit weird to model "involution" property through inheritance. (Primarily, because it's hard to mix multiple properties.) Node flags would be a better fit IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

For now, I suggest to just add a superclassReverseBytesNode which extends InvolutionNode and place Value there.

Copy link
Member Author

@SirYwell SirYwell Apr 10, 2025

Choose a reason for hiding this comment

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

Why do you think it benefits from becoming a TypeNode?

Oh yeah I somehow confused myself there successfully.

Actually, I had an afterthought about InvolutionNode after approving it. It looks a bit weird to model "involution" property through inheritance. (Primarily, because it's hard to mix multiple properties.) Node flags would be a better fit IMO.

That would work, although it would make the common implementation more difficult I think. Also AddNode similarly models the "addition in a (semi)-ring" property, but there's clearly more shared code there (and the property can't be modeled as a flag there because the respective multiplicative operation is defined there too).

Copy link
Member Author

Choose a reason for hiding this comment

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

@iwanowww I applied your suggestion now. Please let me know if this is good now.

}

template<typename T, BasicType B>
const Type* reverse_bytes(const Node* node, PhaseGVN* phase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I'd prefer to see a unified generic version of ReverseBytesNode::Value() rather than multiple specializations (even templated ones).


@DontCompile
public void assertResultI() {
Asserts.assertEQ(Integer.reverseBytes(0x04030201), testI1());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, add more test cases (specifically, with negative constants).

Copy link
Member Author

Choose a reason for hiding this comment

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

I added cases with a leading 0x80 byte now

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

@eme64
Copy link
Contributor

eme64 commented Apr 8, 2025

@SirYwell This looks generally good, but I'll let you have the conversation with @iwanowww .
I launched some internal testing.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

Overall, looks good. Submitted it for testing.

return TypeH::make((float)sqrt((double)f));
}

const Type* reverse_bytes(int opcode, const Type* con) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, declare it as static.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

Testing results are good.

@SirYwell
Copy link
Member Author

@iwanowww thanks, I addressed your last comment.

Copy link
Contributor

@iwanowww iwanowww left a comment

Choose a reason for hiding this comment

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

Looks good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 14, 2025
@SirYwell
Copy link
Member Author

Thank you @iwanowww. @eme64 could you have another look too?

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.

Nice, the code looks even better now :)

* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.c2.irTests;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please move the test to a more specific directory?
The irTests directory was a bit of a mistake. I think this would make more sense in test/hotspot/jtreg/compiler/c2/gvn/.

@eme64
Copy link
Contributor

eme64 commented Apr 17, 2025

@iwanowww I see you did some internal testing, but not for what version. Should we re-run testing?

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Apr 25, 2025
@SirYwell
Copy link
Member Author

Sorry for the delay, I moved the test now @eme64. I think @iwanowww ran the tests after I changed the implementation to his suggestion. Please let me know when you think we can integrate.

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.

It looks good to me.

But before you integrate:
I also launched testing again, just in case. Please ping me again in 24h for the results :)

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 8, 2025
@SirYwell
Copy link
Member Author

SirYwell commented May 9, 2025

Thanks for testing again @eme64. Are the results in?

@eme64
Copy link
Contributor

eme64 commented May 9, 2025

@SirYwell They are almost completed, but so far no failure :)

@SirYwell
Copy link
Member Author

@eme64 just to make sure, I assume we can integrate this now?

@eme64
Copy link
Contributor

eme64 commented May 12, 2025

@SirYwell Yes, it is all green 🟢 Ship it :) 🚢

@SirYwell
Copy link
Member Author

Thanks for your reviews!

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label May 12, 2025
@openjdk
Copy link

openjdk bot commented May 12, 2025

@SirYwell
Your change (at version 3a94bbe) is now ready to be sponsored by a Committer.

@eme64
Copy link
Contributor

eme64 commented May 12, 2025

@SirYwell Thanks for the work :)
/sponsor

@openjdk
Copy link

openjdk bot commented May 12, 2025

Going to push as commit de801fe.
Since your change was applied there have been 682 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 12, 2025
@openjdk openjdk bot closed this May 12, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels May 12, 2025
@openjdk
Copy link

openjdk bot commented May 12, 2025

@eme64 @SirYwell Pushed as commit de801fe.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants