-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8372978: [VectorAPI] Fix incorrect identity values in UMIN/UMAX reductions #28692
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
base: master
Are you sure you want to change the base?
Conversation
…tions The original implementation of UMIN/UMAX reductions in JDK-8346174 used incorrect identity values in the Java implementation and test code. Problem: -------- UMIN was using MAX_OR_INF (signed maximum value) as the identity: - Byte.MAX_VALUE (127) instead of max unsigned byte (255) - Short.MAX_VALUE (32767) instead of max unsigned short (65535) - Integer.MAX_VALUE instead of max unsigned int (-1) - Long.MAX_VALUE instead of max unsigned long (-1) UMAX was using MIN_OR_INF (signed minimum value) as the identity: - Byte.MIN_VALUE (-128) instead of 0 - Short.MIN_VALUE (-32768) instead of 0 - Integer.MIN_VALUE instead of 0 - Long.MIN_VALUE instead of 0 This caused incorrect result. For example: UMAX([42,42,...,42]) returned 128 instead of 42 Solution: --------- Use correct unsigned identity values: - UMIN: ($type$)-1 (maximum unsigned value) - UMAX: ($type$)0 (minimum unsigned value) Changes: -------- - X-Vector.java.template: Fixed identity values in reductionOperations - gen-template.sh: Fixed identity values for test code generation - templates/Unit-header.template: Updated copyright year to 2025 - Regenerated all Vector classes and test files Testing: -------- All types (byte/short/int/long) now return correct results in both interpreter mode (-Xint) and compiled mode.
|
👋 Welcome back erfang! A progress list of the required criteria for merging this PR into |
|
❗ This change is not yet ready to be integrated. |
| @@ -4189,7 +4189,7 @@ static void MAXReduceByte128VectorTestsMasked(IntFunction<byte[]> fa, IntFunctio | |||
| } | |||
|
|
|||
| static byte UMINReduce(byte[] a, int idx) { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test the reduction operations in a better manner by using a[idx] as the starting value instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @merykitty thanks for the review, a[idx] is already the starting value for the reduction operation of this function, see line 4193. What do you mean by a better manner ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the starting value is -1, this test depends on the fact that we choose the correct identity value for this particular operation as the starting value, which this issue is about. As a result, it would be better to write the test so that we do not depend on the identity value.
byte res = a[idx];
for (int i = idx + 1; i < (idx + SPECIES.length()); i++) {
res = (byte) VectorMath.minUnsigned(res, a[i]);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I got your point. I think this might be to maintain consistency with UMINReduceMasked; for the masked version, if no element is selected, it returns the identity value. I'm okay with both approaches, maybe let’s hear what @PaulSandoz thinks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For masking we need to start with the identity or otherwise use the identify when no mask bits are set. It would be better to declare as constants and refer to them e.g., UMAX_VALUE, UMIN_VALUE. There are also other cases where we use identity values for reduction and they follow the same pattern of declaration and use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I understand, thank you for your insight! I'll wait for @PaulSandoz 's comment and see if we should add two public constants to this PR. Then I'll modify it accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to declare identity constants, but only in tests [1] , such as MUL_IDENTITY, UMAX_IDENTITY etc, that we consistently refer to and then add basic tests to verify that identity with respect to the scalar operation. The identity values are also embedded in the JDK and HotSpot compiler and i want to ensure they are clearly compared against the expected identity when an all-false mask is used.
[1] later we could place these in some internal JDK class so we can use the same values in the JDK code, then adjust the tests to grant access to the internal JDK class to use those values. A more general place to surface up scalar identity values is in VectorOperators for the associative operators, something to consider later on perhaps, and that would require a CSR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PaulSandoz Thanks for your suggestion! I declared some identity constants in both the tests and the implementations. And added some tests to verify the correctness of these constants.
@merykitty Now we're using a correct constant to represent the identity value, eliminating the dependency on incorrect literals. So I've chosen to keep the current coding style. I tried the style you suggested, but I feel the original style is more readable and maintainable. Do you think this is okay?
Please help take another look, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, please add tests to verify the correctness of these identity value (i.e. x + i == x).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I have added two tests for these constants, see https://github.com/openjdk/jdk/pull/28692/changes#diff-18d929c1615a4dbad7c65daa66edc98fa1537968fa14a2b8105145d18e9eb12fR6471.
|
@erifan this pull request can not be integrated into git checkout JDK-8372978-fix-umin-umax-identity
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 |
|
Since this issue https://bugs.openjdk.org/browse/JDK-8373390, I didn't merge the latest master branch into this PR. After that issue is fixed, I'll resolve the merge conflicts. |
|
Hi, could you please take another look at this PR? I've already processed all your comments. @merykitty @PaulSandoz |
| r[i] = av.reduceLanes(VectorOperators.[[TEST]], vmask); | ||
| ra = [[TEST_OP]](ra, r[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably ok to merge the two cases, but to be conservative assign to a local variable rather than reading from the array e.g.,
$type$ v = av.reduceLanes(VectorOperators.[[TEST]], vmask);
r[i] = v;
ra = [[TEST_OP]](ra, v);
| @Test(dataProvider = "$type$UnaryOpProvider") | ||
| static void testMaskedReductionIdentityAllFalse(IntFunction<$type$[]> fa) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this test since we already have boolean data sets with all false values that is used for the mask tests
| @Test(dataProvider = "$type$UnaryOpProvider") | ||
| static void testIdentityValues(IntFunction<$type$[]> fa) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already have the scalar operation we can generalize and reuse e.g., to test the following: (id, x) == x, (x, id) == x, (id, id) == id. I believe we could add the additional test to Unit-Reduction-op.template and Unit-Reduction-op-func.template. WDYT?
|
@merykitty @PaulSandoz thanks for your feedbacks, I will make the necessary revisions based on your suggestions shortly. Please continue to review the PR if you have any further suggestions. Thank you! |
The original implementation of UMIN/UMAX reductions in JDK-8346174 used incorrect identity values in the Java implementation and test code.
Problem:
UMIN was using MAX_OR_INF (signed maximum value) as the identity:
UMAX was using MIN_OR_INF (signed minimum value) as the identity:
This caused incorrect result. For example:
UMAX([42,42,...,42]) returned 128 instead of 42
Solution:
Use correct unsigned identity values:
Changes:
Testing:
All types (byte/short/int/long) now return correct results in both interpreter mode (-Xint) and compiled mode.
Progress
Issue
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28692/head:pull/28692$ git checkout pull/28692Update a local copy of the PR:
$ git checkout pull/28692$ git pull https://git.openjdk.org/jdk.git pull/28692/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 28692View PR using the GUI difftool:
$ git pr show -t 28692Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28692.diff
Using Webrev
Link to Webrev Comment