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

8261022: Fix incorrect result of Math.abs() with char type #2419

Closed
wants to merge 2 commits into from

Conversation

pfustc
Copy link
Member

@pfustc pfustc commented Feb 5, 2021

Math.abs() with char type may return incorrect result after C2 superword optimization. It can be reproduced by below Java code and commands.

public class Bug {
    private static int SIZE = 60000;
    private static char[] a = new char[SIZE];
    private static char[] b = new char[SIZE];

    public static void main(String[] args) {
        for (int i = 0; i < SIZE; i++) {
            a[i] = b[i] = (char) i;
        }
        for (int i = 0; i < SIZE; i++) {
            a[i] = (char) Math.abs(a[i]);
        }
        for (int i = 0; i < SIZE; i++) {
            if (a[i] != b[i]) {
                throw new RuntimeException("Broken!");
            }
        }
        System.out.println("OK");
    }
}

// $ java -Xint Bug
// OK

// $ java -Xcomp -XX:-TieredCompilation Bug
// Exception in thread "main" java.lang.RuntimeException: Broken!
//         at Bug.main(Bug.java:15)

In Java, 'char' is a 16-bit unsigned integer type and the abs() method should always return the value of its input. But with C2 vectorization, the sign bit of the 16-bit value is cleared because it's regarded as a signed value.

Root cause is that we get an imprecise vector element type for AbsINode from SuperWord::compute_vector_element_type(). In any Java arithmetic operation, operands of small integer types (boolean, byte, char & short) should be promoted to int first. As vector elements of small types don't have upper bits of int, for RShiftI or AbsI operations, the compiler has to know the precise signedness info of the 1st operand. These operations shouldn't be vectorized if the signedness info is imprecise.

In code SuperWord::compute_vector_element_type(), we have some special handling for right shift. It limited the vectorization of small integer right shift to operations only after loads. The reason is that in the C2 compiler, only LoadNode has precise signedness info of its value. When JDK-8222074 enabled abs vectorization, it didn't involve AbsI operation into the special handling and thus introduced this bug. This patch just does the fix at this point.

Tested hotspot::hotspot_all_no_apps, jdk::jdk_core and langtools::tier1, no new failure is found. Also created a new jtreg with this fix.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8261022: Fix incorrect result of Math.abs() with char type

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/2419/head:pull/2419
$ git checkout pull/2419

Math.abs() with char type may return incorrect result after C2 superword
optimization. It can be reproduced by below Java code and commands.

public class Bug {
    private static int SIZE = 60000;
    private static char[] a = new char[SIZE];
    private static char[] b = new char[SIZE];

    public static void main(String[] args) {
        for (int i = 0; i < SIZE; i++) {
            a[i] = b[i] = (char) i;
        }
        for (int i = 0; i < SIZE; i++) {
            a[i] = (char) Math.abs(a[i]);
        }
        for (int i = 0; i < SIZE; i++) {
            if (a[i] != b[i]) {
                throw new RuntimeException("Broken!");
            }
        }
        System.out.println("OK");
    }
}

// $ java -Xint Bug
// OK

// $ java -Xcomp -XX:-TieredCompilation Bug
// Exception in thread "main" java.lang.RuntimeException: Broken!
//         at Bug.main(Bug.java:15)

In Java, 'char' is a 16-bit unsigned integer type and the abs() method
should always return the value of its input. But with C2 vectorization,
the sign bit of the 16-bit value is cleared because it's regarded as a
signed value.

Root cause is that we get an imprecise vector element type for AbsINode
from SuperWord::compute_vector_element_type(). In any Java arithmetic
operation, operands of small integer types (boolean, byte, char & short)
should be promoted to int first. As vector elements of small types don't
have upper bits of int, for RShiftI or AbsI operations, the compiler has
to know the precise signedness info of the 1st operand. These operations
shouldn't be vectorized if the signedness info is imprecise.

In code SuperWord::compute_vector_element_type(), we have some special
handling for right shift. It limited the vectorization of small integer
right shift to operations only after loads. The reason is that in the C2
compiler, only LoadNode has precise signedness info of its value. When
JDK-8222074 enabled abs vectorization, it didn't involve AbsI operation
into the special handling and thus introduced this bug. This patch just
does the fix at this point.

Tested hotspot::hotspot_all_no_apps, jdk::jdk_core and langtools::tier1,
no new failure is found. Also created a new jtreg with this fix.
@bridgekeeper
Copy link

bridgekeeper bot commented Feb 5, 2021

👋 Welcome back pli! 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 openjdk bot added the rfr Pull request is ready for review label Feb 5, 2021
@openjdk
Copy link

openjdk bot commented Feb 5, 2021

@pfustc 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 Feb 5, 2021
@mlbridge
Copy link

mlbridge bot commented Feb 5, 2021

Webrevs

Copy link
Member

@TobiHartmann TobiHartmann 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 to me.

// Widen type to Int to avoid creation of right shift vector
// (align + data_size(s1) check in stmts_can_pack() will fail).
// Note, left shifts work regardless type.
// Widen type to int to avoid the creation of vector nodes. Note
Copy link
Member

Choose a reason for hiding this comment

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

in->Opcode() can be replaced by op

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, thanks!

@openjdk
Copy link

openjdk bot commented Feb 5, 2021

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

8261022: Fix incorrect result of Math.abs() with char type

Reviewed-by: thartmann, neliasso

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

  • ee2f205: 8260926: Trace resource exhausted events unconditionally
  • 1e0a101: 8259862: MutableSpace's end should be atomic
  • d2bd499: 8163498: Many long-running security libs tests
  • c5bb109: 8260019: Move some Thread subtypes out of thread.hpp

Please see this link for an up-to-date comparison between the source branch of this pull request and 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 openjdk bot added the ready Pull request is ready to be integrated label Feb 5, 2021
Copy link

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

@pfustc
Copy link
Member Author

pfustc commented Feb 7, 2021

/integrate

@openjdk openjdk bot closed this Feb 7, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 7, 2021
@openjdk
Copy link

openjdk bot commented Feb 7, 2021

@pfustc Since your change was applied there have been 23 commits pushed to the master branch:

  • 2c3a86f: 8261280: Remove THREAD argument from compute_loader_lock_object
  • 74d40ab: 8261200: Some code in the ICC_Profile may not close file streams properly
  • d7acfae: Merge
  • 4de3a6b: 8260709: C2: assert(false) failed: unscheduable graph
  • 5307afa: 8260585: AArch64: Wrong code generated for shifting right and accumulating four unsigned short integers
  • fb46d4e: 8259268: Refactor InheritIO shell test as java test
  • 440db35: 8216358: [accessibility] [macos] The focus is invisible when tab to "Image Radio Buttons" and "Image CheckBoxes"
  • fac3c2d: 8254702: jpackage app launcher crashes on CentOS
  • 7a6c176: 8260736: Shenandoah: Cleanup includes in ShenandoahGC and families
  • 4a89733: 8261198: [macOS] Incorrect JNI parameters in number conversion in A11Y code
  • ... and 13 more: https://git.openjdk.java.net/jdk/compare/08f7454fa961e00f9f5ec370a339ae89134dcbf6...master

Your commit was automatically rebased without conflicts.

Pushed as commit 7a2db85.

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

@pfustc pfustc deleted the absfix branch February 7, 2021 01:20
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
3 participants