Skip to content

Conversation

@Hamlin-Li
Copy link

@Hamlin-Li Hamlin-Li commented Sep 8, 2025

Hi,
Can you help to review this patch?

Previously, the register selection in MacroAssembler:: decode_klass_not_null is misleading, better to refine it to improve the readability.

Thanks!

Running runtime test tier1/2/3...


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-8367066: RISC-V: refine register selection in MacroAssembler:: decode_klass_not_null (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27138

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 8, 2025

👋 Welcome back mli! 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 Sep 8, 2025

@Hamlin-Li 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:

8367066: RISC-V: refine register selection in MacroAssembler:: decode_klass_not_null

Reviewed-by: fyang, fjiang

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 62 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.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Sep 8, 2025

@Hamlin-Li The following label will be automatically applied to this pull request:

  • hotspot

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 hotspot hotspot-dev@openjdk.org rfr Pull request is ready for review labels Sep 8, 2025
@mlbridge
Copy link

mlbridge bot commented Sep 8, 2025

Webrevs

Copy link
Member

@RealFYang RealFYang left a comment

Choose a reason for hiding this comment

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

Hi, seems we can further eliminate the use of t0 replacing it with tmp. How about we go like this?

diff --git a/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp b/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
index 1436bc02113..89e615b1cbb 100644
--- a/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
+++ b/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
@@ -3402,6 +3402,8 @@ void MacroAssembler::decode_klass_not_null(Register r, Register tmp) {

 void MacroAssembler::decode_klass_not_null(Register dst, Register src, Register tmp) {
   assert(UseCompressedClassPointers, "should only be used for compressed headers");
+  assert_different_registers(src, tmp);
+  assert_different_registers(dst, tmp);

   if (CompressedKlassPointers::base() == nullptr) {
     if (CompressedKlassPointers::shift() != 0) {
@@ -3421,7 +3423,7 @@ void MacroAssembler::decode_klass_not_null(Register dst, Register src, Register
   mv(xbase, (uintptr_t)CompressedKlassPointers::base());

   if (CompressedKlassPointers::shift() != 0) {
-    Register t = src == dst ? dst : t0;
+    Register t = dst == xbase ? tmp : dst;
     assert_different_registers(t, xbase);
     shadd(dst, src, xbase, t, CompressedKlassPointers::shift());
   } else {

@Hamlin-Li
Copy link
Author

Hamlin-Li commented Sep 8, 2025

Hi, seems we can further eliminate the use of t0 replacing it with tmp. How about we go like this?

diff --git a/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp b/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
index 1436bc02113..89e615b1cbb 100644
--- a/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
+++ b/src/hotspot/cpu/riscv/macroAssembler_riscv.cpp
@@ -3402,6 +3402,8 @@ void MacroAssembler::decode_klass_not_null(Register r, Register tmp) {

 void MacroAssembler::decode_klass_not_null(Register dst, Register src, Register tmp) {
   assert(UseCompressedClassPointers, "should only be used for compressed headers");
+  assert_different_registers(src, tmp);
+  assert_different_registers(dst, tmp);

   if (CompressedKlassPointers::base() == nullptr) {
     if (CompressedKlassPointers::shift() != 0) {
@@ -3421,7 +3423,7 @@ void MacroAssembler::decode_klass_not_null(Register dst, Register src, Register
   mv(xbase, (uintptr_t)CompressedKlassPointers::base());

   if (CompressedKlassPointers::shift() != 0) {
-    Register t = src == dst ? dst : t0;
+    Register t = dst == xbase ? tmp : dst;
     assert_different_registers(t, xbase);
     shadd(dst, src, xbase, t, CompressedKlassPointers::shift());
   } else {

I applied your suggested patch, and it trigger an assert at assert_different_registers(dst, tmp);,

assert(regs[i] != regs[j]) failed: regs[0] and regs[1] are both: t2

still running tests by removing this assert only.

@RealFYang
Copy link
Member

RealFYang commented Sep 8, 2025

Yes, we still need one extra change to guarantee that dst != tmp, which we can do with a TEMP_DEF effect for dst.
I tried adding this extra change and it seems to work.

EDIT: My local hotspot:tier1 passed with fastdebug build on SG2042.

@@ -8936,7 +8941,7 @@ instruct encodeKlass_not_null(iRegNNoSp dst, iRegP src) %{
 instruct decodeKlass_not_null(iRegPNoSp dst, iRegN src, iRegPNoSp tmp) %{
   match(Set dst (DecodeNKlass src));

-  effect(TEMP tmp);
+  effect(TEMP_DEF dst, TEMP tmp);

   ins_cost(ALU_COST);
   format %{ "decode_klass_not_null  $dst, $src\t#@decodeKlass_not_null" %}

@Hamlin-Li
Copy link
Author

Yes, we still need one extra change to guarantee that dst != tmp, which we can do with a TEMP_DEF effect for dst. I tried adding this extra change and it seems to work.

EDIT: My local hotspot:tier1 passed with fastdebug build on SG2042.

@@ -8936,7 +8941,7 @@ instruct encodeKlass_not_null(iRegNNoSp dst, iRegP src) %{
 instruct decodeKlass_not_null(iRegPNoSp dst, iRegN src, iRegPNoSp tmp) %{
   match(Set dst (DecodeNKlass src));

-  effect(TEMP tmp);
+  effect(TEMP_DEF dst, TEMP tmp);

   ins_cost(ALU_COST);
   format %{ "decode_klass_not_null  $dst, $src\t#@decodeKlass_not_null" %}

In this sense, we're not saving one register (t0), as we require extra register by effect(TEMP_DEF dst, TEMP tmp);, right?

@Hamlin-Li
Copy link
Author

I think we can further simplify the code in decode_klass_not_null, pleaes check the latest commit. Test running...

assert_different_registers(t, xbase);
shadd(dst, src, xbase, t, CompressedKlassPointers::shift());
// dst = (src << shift) + xbase
shadd(dst, src, xbase, dst /* temporary, dst != xbase */, CompressedKlassPointers::shift());
Copy link
Member

@RealFYang RealFYang Sep 10, 2025

Choose a reason for hiding this comment

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

Is it safer to turn the code comment into an assertion? It will trigger if people incorrectly sets xbase.

assert_different_registers(dst, xbase);
// dst = (src << shift) + xbase
shadd(dst, src, xbase, dst, CompressedKlassPointers::shift());

Copy link
Author

Choose a reason for hiding this comment

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

In shadd when tmp (here we use dst as tmp) is used, there is already an assert there.

@RealFYang
Copy link
Member

I think we can further simplify the code in decode_klass_not_null, pleaes check the latest commit. Test running...

Yes, the code now looks more simplified. Thanks.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 10, 2025
@Hamlin-Li
Copy link
Author

I think we can further simplify the code in decode_klass_not_null, pleaes check the latest commit. Test running...

Yes, the code now looks more simplified. Thanks.

Thanks for reviewing.

Copy link
Member

@feilongjiang feilongjiang left a comment

Choose a reason for hiding this comment

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

Thanks!

@Hamlin-Li
Copy link
Author

Thanks!

Thank you for reviewing!

@Hamlin-Li
Copy link
Author

/integrate

@openjdk
Copy link

openjdk bot commented Sep 11, 2025

Going to push as commit 0b3a303.
Since your change was applied there have been 67 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 Sep 11, 2025
@openjdk openjdk bot closed this Sep 11, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 11, 2025
@openjdk
Copy link

openjdk bot commented Sep 11, 2025

@Hamlin-Li Pushed as commit 0b3a303.

💡 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 hotspot-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants