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

8309685: Fix -Wconversion warnings in assembler and register code #14396

Closed
wants to merge 8 commits into from

Conversation

coleenp
Copy link
Contributor

@coleenp coleenp commented Jun 9, 2023

This change widens some int arguments, narrows some int returns and adds some casts and checked_casts where the compiler was doing the implicit conversion. I maintained existing types - ie if the parameter was short, I used a cast to short. Also int is used in places that might be better served as unsigned int but I didn't fix that because it would be too large and risky. The registers encode an offset in an array, so it's safe to checked_cast<> to get their encoding. This fix is limited so that the types changed and casts added are intentional.
See CR for counts of -Wconversion warnings this resolves.
Tested with tier1-7, also tested with tier1 on all Oracle supported platforms.


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-8309685: Fix -Wconversion warnings in assembler and register code (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 14396

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 9, 2023

👋 Welcome back coleenp! 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 Jun 9, 2023

@coleenp 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 the hotspot hotspot-dev@openjdk.org label Jun 9, 2023
@coleenp coleenp marked this pull request as ready for review June 12, 2023 12:14
@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 12, 2023
@mlbridge
Copy link

mlbridge bot commented Jun 12, 2023

Webrevs

Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

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

This is smaller and less painful than I expected.

src/hotspot/share/asm/assembler.hpp Outdated Show resolved Hide resolved
@@ -30,7 +30,7 @@
// used by SA and jvmti, but it's a leaky abstraction: SA and jvmti
// "know" that stack0 is an integer masquerading as a pointer. For the
// sake of those clients, we preserve this interface.
VMReg VMRegImpl::stack0 = (VMReg)VMRegImpl::stack_0()->value();
VMReg VMRegImpl::stack0 = (VMReg)(intptr_t)VMRegImpl::stack_0()->value();

Choose a reason for hiding this comment

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

So enlighten me - why the two-step cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed value() to return an int rather than intptr_t, so the compiler objects:

src/hotspot/share/code/vmreg.cpp:33:27: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
33 | VMReg VMRegImpl::stack0 = (VMReg)VMRegImpl::stack_0()->value();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Copy link
Contributor Author

@coleenp coleenp left a comment

Choose a reason for hiding this comment

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

Thanks for reviewing this Kim. I tried to limit the changes so that it's not just adding casts. I'm glad this isn't as bad as you thought it would be.

src/hotspot/share/asm/assembler.hpp Outdated Show resolved Hide resolved
@@ -30,7 +30,7 @@
// used by SA and jvmti, but it's a leaky abstraction: SA and jvmti
// "know" that stack0 is an integer masquerading as a pointer. For the
// sake of those clients, we preserve this interface.
VMReg VMRegImpl::stack0 = (VMReg)VMRegImpl::stack_0()->value();
VMReg VMRegImpl::stack0 = (VMReg)(intptr_t)VMRegImpl::stack_0()->value();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed value() to return an int rather than intptr_t, so the compiler objects:

src/hotspot/share/code/vmreg.cpp:33:27: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
33 | VMReg VMRegImpl::stack0 = (VMReg)VMRegImpl::stack_0()->value();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@zifeihan
Copy link
Member

Hi, may I ask if it's OK to add fix for RISC-V please? I checked and found that this port has similar issue.

diff --git a/src/hotspot/cpu/riscv/register_riscv.hpp b/src/hotspot/cpu/riscv/register_riscv.hpp
index 7b547335cd0..7d25875fedd 100644
--- a/src/hotspot/cpu/riscv/register_riscv.hpp
+++ b/src/hotspot/cpu/riscv/register_riscv.hpp
@@ -70,7 +70,7 @@ class Register {
 
    public:
     // accessors
-    constexpr int raw_encoding() const { return this - first(); }
+    constexpr int raw_encoding() const { return checked_cast<int>(this - first()); }
     constexpr int     encoding() const { assert(is_valid(), "invalid register"); return raw_encoding(); }
     constexpr bool    is_valid() const { return 0 <= raw_encoding() && raw_encoding() < number_of_registers; }
 
@@ -187,7 +187,7 @@ class FloatRegister {
 
    public:
     // accessors
-    constexpr int raw_encoding() const { return this - first(); }
+    constexpr int raw_encoding() const { return checked_cast<int>(this - first()); }
     constexpr int     encoding() const { assert(is_valid(), "invalid register"); return raw_encoding(); }
     constexpr bool    is_valid() const { return 0 <= raw_encoding() && raw_encoding() < number_of_registers; }
 
@@ -297,7 +297,7 @@ class VectorRegister {
 
    public:
     // accessors
-    constexpr int raw_encoding() const { return this - first(); }
+    constexpr int raw_encoding() const { return checked_cast<int>(this - first()); }
     constexpr int     encoding() const { assert(is_valid(), "invalid register"); return raw_encoding(); }
     constexpr bool    is_valid() const { return 0 <= raw_encoding() && raw_encoding() < number_of_registers; }

@coleenp
Copy link
Contributor Author

coleenp commented Jun 14, 2023

Thanks @zifeihan I added riscv too.

S1 min = (std::is_signed<T2>::value) ? std::numeric_limits<T2>::min() : -max;
assert(test <= max && test >= min, "out of range of destination type");
#endif
return static_cast<T2>(thing);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this change. Isn't it confusing to treat unsigned types as signed here? Unsigned types don't sign extend. If emit_intXX can take signed values then shouldn't that be reflected in the types of the arguements?
Also, -max should be -max - 1 for 2's complement, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The conversion from an unsigned type to a smaller unsigned type should have just worked if you convert back, like the old checked_cast code did, but callers of emit_intxxx were sign extending so the conversion back didn't work.

Calls like emit_int8() are passed arguments like 0xf0, but there were multiple (~940) that are passed with something that did some bitwise calculation that was promoted up to int.

Maybe the emit_intxx functions should take signed int, and then I could just check range in the checked_cast() without converting the argument to signed. That might work and still provide the range checking that we want.

Yes, it should be max-1. Thank you for looking at this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing checked_cast<> to not convert to a signed type for T1 leads to very puzzling compiler errors. For these emit_int8 etc functions, maybe a straight cast is the best thing, and leaving the definition checked_cast<> alone. I'd be happy to do that as I had in my first patch.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know what checked_cast<> should do for negative values when going between signed --> unsigned and for values greater than signed max for unsigned --> signed. It seems like both should fail because the sign changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Widening the arguments to the emit functions caused the sign extensions, so ideally, the signed bits should be chopped off, but only really in this case. Maybe fixing all the call sites might be better in this case.

@openjdk
Copy link

openjdk bot commented Jun 15, 2023

@coleenp 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 more-conversion
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 Jun 15, 2023
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Jun 16, 2023
@coleenp coleenp closed this Jun 20, 2023
@coleenp
Copy link
Contributor Author

coleenp commented Jun 20, 2023

I'm going to break this up more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org rfr Pull request is ready for review
4 participants