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

8298244: AArch64: Optimize vector implementation of AddReduction for floating point #11663

Closed
wants to merge 2 commits into from

Conversation

fg1417
Copy link
Contributor

@fg1417 fg1417 commented Dec 14, 2022

The patch optimizes floating-point AddReduction for Vector API on NEON via faddp instructions [1].

Take AddReductionVF with 128-bit as an example.

Here is the assembly code before the patch:

fadd    s18, s17, s16
mov     v19.s[0], v16.s[1]
fadd    s18, s18, s19
mov     v19.s[0], v16.s[2]
fadd    s18, s18, s19
mov     v19.s[0], v16.s[3]
fadd    s18, s18, s19

Here is the assembly code after the patch:

faddp   v19.4s, v16.4s, v16.4s
faddp   s18, v19.2s
fadd    s18, s18, s17

As we can see, the patch adds all vector elements via faddp instructions and then adds beginning value, which is different from the old code, i.e., adding vector elements sequentially from beginning to end. It helps reduce four instructions for each AddReductionVF.

But it may concern us that the patch will cause precision loss and generate incorrect results if superword vectorizes these java operations, because Java specifies a clear standard about precision for floating-point add reduction, which requires that we must add vector elements sequentially from beginning to end. Fortunately, we can enjoy the benefit but don't need to pay for the precision loss. Here are the reasons:

  1. JDK-8275275 disabled AddReductionVF/D for superword on NEON since no direct NEON instructions support them and, consequently, it's not profitable to auto-vectorize them. So, the vector implementation of these two vector nodes is only used by Vector API.

  2. Vector API relaxes the requirement for floating-point precision of ADD [2]. "The result of such an operation is a function both of the input values (vector and mask) as well as the order of the scalar operations applied to combine lane values. In such cases the order is intentionally not defined." "If the platform supports a vector instruction to add or multiply all values in the vector, or if there is some other efficient machine code sequence, then the JVM has the option of generating this machine code." To sum up, Vector API allows us to add all vector elements in an arbitrary order and then add the beginning value, to generate optimal machine code.

Tier 1~3 passed with no new failures on Linux AArch64 platform.

Here is the perf data of jmh benchmark [3] for the patch:

Benchmark size Mode Cnt Before After Units
Double128Vector.addReduction 1024 thrpt 5 2167.146 2717.873 ops/ms
Float128Vector.addReduction 1024 thrpt 5 1706.253 4890.909 ops/ms
Float64Vector.addReduction 1024 thrpt 5 1907.425 2732.577 ops/ms

[1] https://developer.arm.com/documentation/ddi0602/2022-06/SIMD-FP-Instructions/FADDP--scalar---Floating-point-Add-Pair-of-elements--scalar--
https://developer.arm.com/documentation/ddi0602/2022-06/SIMD-FP-Instructions/FADDP--vector---Floating-point-Add-Pairwise--vector--
[2] https://docs.oracle.com/en/java/javase/19/docs/api/jdk.incubator.vector/jdk/incubator/vector/VectorOperators.html#fp_assoc
[3] https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Float128Vector.java#L316
https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Float64Vector.java#L316
https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Double128Vector.java#L316


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-8298244: AArch64: Optimize vector implementation of AddReduction for floating point

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11663

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

Using diff file

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

…floating point

The patch optimizes floating-point AddReduction for Vector API on
NEON via faddp instructions [1].

Take AddReductionVF with 128-bit as an example.

Here is the assembly code before the patch:
```
fadd    s18, s17, s16
mov     v19.s[0], v16.s[1]
fadd    s18, s18, s19
mov     v19.s[0], v16.s[2]
fadd    s18, s18, s19
mov     v19.s[0], v16.s[3]
fadd    s18, s18, s19
```

Here is the assembly code after the patch:
```
faddp   v19.4s, v16.4s, v16.4s
faddp   s18, v19.2s
fadd    s18, s18, s17
```

As we can see, the patch adds all vector elements via faddp
instructions and then adds beginning value, which is different
from the old code, i.e., adding vector elements sequentially
from beginning to end. It helps reduce four instructions for
each AddReductionVF.

But it may concern us that the patch will cause precision loss
and generate incorrect results if superword vectorizes these
java operations, because Java specifies a clear standard about
precision for floating-point add reduction, which requires that
we must add vector elements sequentially from beginning to end.
Fortunately, we can enjoy the benefit but don't need to pay for
the precision loss. Here are the reasons:

1. JDK-8275275 disabled AddReductionVF/D for superword on NEON
since no direct NEON instructions support them and, consequently,
it's not profitable to auto-vectorize them. So, the vector
implementation of these two vector nodes is only used by
Vector API.

2. Vector API relaxes the requirement for floating-point
precision of `ADD` [2]. "The result of such an operation is a
function both of the input values (vector and mask) as well as
the order of the scalar operations applied to combine lane values.
In such cases the order is intentionally not defined." "If the
platform supports a vector instruction to add or multiply all
values in the vector, or if there is some other efficient machine
code sequence, then the JVM has the option of generating this
machine code." To sum up, Vector API allows us to add all vector
elements in an arbitrary order and then add the beginning value,
to generate optimal machine code.

Tier 1~3 passed with no new failures on Linux AArch64 platform.

Here is the perf data of jmh benchmark [3] for the patch:

Benchmark                      size  Mode  Cnt  Before     After     Units
Double128Vector.addReduction   1024  thrpt  5   2167.146  2717.873   ops/ms
Float128Vector.addReduction    1024  thrpt  5   1706.253  4890.909   ops/ms
Float64Vector.addReduction     1024  thrpt  5   1907.425  2732.577   ops/ms

[1] https://developer.arm.com/documentation/ddi0602/2022-06/SIMD-FP-Instructions/FADDP--scalar---Floating-point-Add-Pair-of-elements--scalar--
    https://developer.arm.com/documentation/ddi0602/2022-06/SIMD-FP-Instructions/FADDP--vector---Floating-point-Add-Pairwise--vector--
[2] https://docs.oracle.com/en/java/javase/19/docs/api/jdk.incubator.vector/jdk/incubator/vector/VectorOperators.html#fp_assoc
[3] https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Float128Vector.java#L316
    https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Float64Vector.java#L316
    https://github.com/openjdk/panama-vector/blob/2aade73adeabdf6a924136b17fd96ccc95c1d160/test/micro/org/openjdk/bench/jdk/incubator/vector/operation/Double128Vector.java#L316
@bridgekeeper
Copy link

bridgekeeper bot commented Dec 14, 2022

👋 Welcome back fgao! 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 Dec 14, 2022

@fg1417 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 Dec 14, 2022
@fg1417 fg1417 changed the title AArch64: Optimize vector implementation of AddReduction for floating point 8298244: AArch64: Optimize vector implementation of AddReduction for floating point Dec 14, 2022
@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 14, 2022
@mlbridge
Copy link

mlbridge bot commented Dec 14, 2022

Webrevs

Comment on lines 2922 to 2923
// Specially, the current vector implementation of Op_AddReductionVD works for
// Vector API only because of the non-sequential order of element addition.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Specially, the current vector implementation of Op_AddReductionVD works for
// Vector API only because of the non-sequential order of element addition.
// Floating-point addition is not associative, so we cannot auto-vectorize
// floating-point reduce-add. AddReductionVD is only generated by. explicit
// vector operations.

Comment on lines 1814 to 1815
// Specially, the current vector implementation of Op_AddReductionVF works for
// Vector API only because of the non-sequential order of element addition.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Specially, the current vector implementation of Op_AddReductionVF works for
// Vector API only because of the non-sequential order of element addition.
// Floating-point addition is not associative, so we cannot auto-vectorize
// floating-point reduce-add. AddReductionVD is only generated by. explicit
// vector operations.

Comment on lines 127 to 129
// Specially, the current vector implementation of Op_AddReductionVD/F works for
// Vector API only. If re-enabling them for superword, precision loss will happen
// because current generated code does not add elements sequentially from beginning to end.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Specially, the current vector implementation of Op_AddReductionVD/F works for
// Vector API only. If re-enabling them for superword, precision loss will happen
// because current generated code does not add elements sequentially from beginning to end.
// The vector implementation of Op_AddReductionVD/F is for the Vector API only.
// It is not suitable for auto-vectorization because it does not add the elements
// in the same order as sequential code, and FPaddition is non-associative.

Comment on lines 1859 to 1860
// Specially, the current vector implementation of Op_AddReductionVD works for
// Vector API only because of the non-sequential order of element addition.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@fg1417
Copy link
Contributor Author

fg1417 commented Dec 15, 2022

Thanks for your kind review and comments, @theRealAph. I addressed them in the new commit.

Could you please help review it? Thanks for your time!

@openjdk
Copy link

openjdk bot commented Dec 15, 2022

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

8298244: AArch64: Optimize vector implementation of AddReduction for floating point

Reviewed-by: aph, xgong

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:

  • fa322e4: 8298709: Fix typos in src/java.desktop/ and various test classes of client component
  • e41686b: 8298710: Fix typos in test/jdk/sun/security/tools/jarsigner/
  • a336461: 8298081: DiagnoseSyncOnValueBasedClasses doesn't report useful information for virtual threads
  • 2bb727c: 8290899: java/lang/String/StringRepeat.java test requests too much heap on windows x86
  • 5412439: 8298187: (fs) BsdFileAttributeViews::setTimes does not support lastAccessTime on HFS+
  • 3cdbd87: 8298241: Replace C-style casts with JavaThread::cast
  • 10737e1: 8298468: Clean up class_loader parameters
  • 4b313b5: 8297798: Timeout with DTLSOverDatagram test template
  • ae8988e: 8297912: HotSpot Style Guide should permit alignas (Second Proposal Attempt)
  • 0ef3539: 8298416: Console should be declared sealed
  • ... and 52 more: https://git.openjdk.org/jdk/compare/173778e2fee58e47d35197b78eb23f46154b5b2b...master

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 (@theRealAph, @XiaohongGong) 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 added the ready Pull request is ready to be integrated label Dec 15, 2022
Copy link
Contributor

@XiaohongGong XiaohongGong 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!

@fg1417
Copy link
Contributor Author

fg1417 commented Dec 16, 2022

Thanks for your review, @theRealAph @XiaohongGong. I'll integrate it.

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Dec 16, 2022
@openjdk
Copy link

openjdk bot commented Dec 16, 2022

@fg1417
Your change (at version 87ac174) is now ready to be sponsored by a Committer.

@nsjian
Copy link

nsjian commented Dec 19, 2022

/sponsor

@openjdk
Copy link

openjdk bot commented Dec 19, 2022

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

  • 7938f8c: 8298639: Perform I/O operations in bulk for RandomAccessFile
  • bfa921a: 8160404: RelocationHolder constructors have bugs
  • bf9a8ce: 8249826: 5 javax/net/ssl/SSLEngine tests use @ignore w/o bug-id
  • 0eeaeb8: 8298808: Check script code on detecting the base locales
  • 81e23ab: 8298809: Clean up vm/compiler/InterfaceCalls JMH
  • 3696711: Merge
  • f771c56: 8298797: Specification of some restricted methods is incorrect
  • 0ba4734: 8287699: jdk/jfr/api/consumer/TestRecordingFileWrite.java fails with exception: java.lang.Exception: Found event that should not be there.
  • c47e64e: 8297979: ZGC: Ensure consistent MemoryUsage from MemoryMXBean.getHeapMemoryUsage()
  • 03a694a: 8298083: The "CheckBox/RadioButton[Enabled/Disabled].textForeground" stoped working
  • ... and 72 more: https://git.openjdk.org/jdk/compare/173778e2fee58e47d35197b78eb23f46154b5b2b...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 19, 2022
@openjdk openjdk bot closed this Dec 19, 2022
@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 Dec 19, 2022
@openjdk
Copy link

openjdk bot commented Dec 19, 2022

@nsjian @fg1417 Pushed as commit ba942c2.

💡 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
4 participants