Skip to content

8290322: Optimize Vector.rearrange over byte vectors for AVX512BW targets. #9498

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

Closed
wants to merge 6 commits into from

Conversation

jatin-bhateja
Copy link
Member

@jatin-bhateja jatin-bhateja commented Jul 14, 2022

Hi All,

Currently re-arrange over 512bit bytevector is optimized for targets supporting AVX512_VBMI feature, this patch generates efficient JIT sequence to handle it for AVX512BW targets. Following performance results with newly added benchmark shows
significant speedup.

System: Intel(R) Xeon(R) Platinum 8280 CPU @ 2.70GHz (CascadeLake 28C 2S)

Baseline:
=========
Benchmark                                     (size)   Mode  Cnt      Score   Error   Units
RearrangeBytesBenchmark.testRearrangeBytes16     512  thrpt    2  16350.330          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes32     512  thrpt    2  15991.346          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes64     512  thrpt    2     34.423          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes8      512  thrpt    2  10873.348          ops/ms


With-opt:
=========
Benchmark                                     (size)   Mode  Cnt      Score   Error   Units
RearrangeBytesBenchmark.testRearrangeBytes16     512  thrpt    2  16062.624          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes32     512  thrpt    2  16028.494          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes64     512  thrpt    2   8741.901          ops/ms
RearrangeBytesBenchmark.testRearrangeBytes8      512  thrpt    2  10983.226          ops/ms

Kindly review and share your feedback.

Best Regards,
Jatin


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-8290322: Optimize Vector.rearrange over byte vectors for AVX512BW targets.

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 9498

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 14, 2022

👋 Welcome back jbhateja! 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.

@jatin-bhateja
Copy link
Member Author

/label add hotspot-compiler-dev

@openjdk openjdk bot added rfr Pull request is ready for review hotspot-compiler hotspot-compiler-dev@openjdk.org labels Jul 14, 2022
@openjdk
Copy link

openjdk bot commented Jul 14, 2022

@jatin-bhateja
The hotspot-compiler label was successfully added.

@mlbridge
Copy link

mlbridge bot commented Jul 14, 2022

Webrevs

Copy link
Contributor

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

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

Testing tier1-3 passed.

@openjdk
Copy link

openjdk bot commented Jul 15, 2022

@jatin-bhateja 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:

8290322: Optimize Vector.rearrange over byte vectors for AVX512BW targets.

Reviewed-by: kvn, sviswanathan

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

  • 45c3e89: 8292316: Tests should not rely on specific JAR file names (jpackage)
  • db77227: 8282684: Obsolete UseContainerCpuShares and PreferContainerQuotaForCPUCount flags
  • 256b523: 8292381: java/net/httpclient/SpecialHeadersTest.java fails with "ERROR: Shutting down connection: HTTP/2 client stopped"
  • e561933: 8292623: Reduce runtime of java.io microbenchmarks
  • dcd7802: 8292708: Rename G1ParScanThreadState::flush to flush_stats
  • 16593cf: 8292717: Clean up checking of testing requirements in configure
  • c59f9b3: 8287828: Fix so that one can select jtreg test case by ID from make
  • 476c484: 8292656: G1: Remove G1HotCardCache::_use_cache
  • a17fce7: 6445283: ProgressMonitorInputStream not large file aware (>2GB)
  • 1ed03d8: 8292226: Prepare make for better Link Time Optimization support
  • ... and 71 more: https://git.openjdk.org/jdk/compare/6e6ae596d6bd73909b90911e01fbd0c16f6335e1...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.

➡️ 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 Jul 15, 2022
Copy link
Member

@JoshuaZhuwj JoshuaZhuwj 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. My Cascade Lake server benefits from this change.

Copy link
Member

@merykitty merykitty left a comment

Choose a reason for hiding this comment

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

Otherwise looks good to me. Thanks.

} else if (bt == T_BYTE && size_in_bits > 256 && !VM_Version::supports_avx512_vbmi()) {
return false; // Implementation limitation
} else if (bt == T_SHORT && size_in_bits > 256 && !VM_Version::supports_avx512bw()) {
} else if (is_subword_type(bt) && size_in_bits > 256 && !VM_Version::supports_avx512bw()) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not needed as a 512-bit subword type vector is only supported on avx512bw anyway.

@@ -5565,3 +5565,65 @@ void C2_MacroAssembler::udivmodL(Register rax, Register divisor, Register rdx, R
}
#endif

void C2_MacroAssembler::rearrange_bytes(XMMRegister dst, XMMRegister shuffle, XMMRegister src, XMMRegister xtmp1,
Copy link
Member

@merykitty merykitty Jul 28, 2022

Choose a reason for hiding this comment

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

Can we use the same approach as that used for 256-bit vector. Something similar to:

vpshufb(xtmp1, src, shuffle); // All elements are at the correct place modulo 16
vpxor(dst, dst, dst);
vpslld(xtmp2, shuffle, 3); // Push the digit signifying the parity of 128-bit lane to the sign digit
vpcmpb(ktmp, xtmp2, dst, lt);
vshufi32x4(xtmp2, xtmp1, xtmp1, 0b10110001); // Shuffle the 128-bit lanes to get 1 - 0 - 3 - 2
vpblendmb(xtmp1, ktmp, xtmp1, xtmp2); // All elements are at the correct place modulo 32
vpslld(xtmp2, shuffle, 2); // Push the digit signifying the parity of 256-bit lane to the sign digit
vpcmpb(ktmp, xtmp2, dst, lt);
vshufi32x4(xtmp2, xtmp1, xtmp1, 0b01001110); // Shuffle the 128-bit lanes to get 2 - 3 - 0 - 1
vpblendmb(dst, ktmp, xtmp1, xtmp2); // All elements are at the correct place modulo 64

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it is my bad, this should not work. Sorry for the noise.

Copy link

@sviswa7 sviswa7 left a comment

Choose a reason for hiding this comment

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

Very nice work. I have some point suggestions which improves the performance further by ~20%. Please take a look.

Comment on lines 5704 to 5705
vpshufb(xtmp3, xtmp3, shuffle, vlen_enc);
evmovdqub(dst, ktmp2, xtmp3, false, vlen_enc);
Copy link

Choose a reason for hiding this comment

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

The avx512 version of vpshufb takes K register and merge as the input.
This can then be replaced by:
evpshufb(dst, ktmp2, xtmp3, shuffle, false, vlen_enc);

Comment on lines 5715 to 5717
vpshufb(xtmp3, xtmp3, shuffle, vlen_enc);
evmovdqub(xtmp4, ktmp2, xtmp3, false, vlen_enc);
vporq(dst, dst, xtmp4, vlen_enc);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

Comment on lines 5728 to 5730
vpshufb(xtmp3, xtmp3, shuffle, vlen_enc);
evmovdqub(xtmp4, ktmp2, xtmp3, false, vlen_enc);
vporq(dst, dst, xtmp4, vlen_enc);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

Comment on lines 5741 to 5743
vpshufb(xtmp3, xtmp3, shuffle, vlen_enc);
evmovdqub(xtmp4, ktmp2, xtmp3, false, vlen_enc);
vporq(dst, dst, xtmp4, vlen_enc);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

Comment on lines 5702 to 5703
vpermq(xtmp3, src, 0x44, vlen_enc);
vinserti64x4(xtmp3, xtmp3, xtmp3, 0x1);
Copy link

Choose a reason for hiding this comment

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

The evshuffi64x2 instruction could be used here:
evshuffi64x2(xtmp3, k0, src, src, 0x0, false, vlen_enc);

Comment on lines 5725 to 5727
vpermq(xtmp3, src, 0x44, vlen_enc);
vextracti64x4_high(xtmp3, xtmp3);
vinserti64x4(xtmp3, xtmp3, xtmp3, 0x1);
Copy link

Choose a reason for hiding this comment

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

The evshuffi64x2 instruction could be used here:
evshuffi64x2(xtmp3, k0, src, src, 0xaa, false, vlen_enc);

Comment on lines 5738 to 5740
vpermq(xtmp3, src, 0xEE, vlen_enc);
vextracti64x4_high(xtmp3, xtmp3);
vinserti64x4(xtmp3, xtmp3, xtmp3, 0x1);
Copy link

Choose a reason for hiding this comment

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

The evshuffi64x2 instruction could be used here:
evshuffi64x2(xtmp3, k0, src, src, 0xff, false, vlen_enc);

Comment on lines 5711 to 5712
evpcmpb(ktmp2, k0, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
kandql(ktmp2, ktmp1, ktmp2);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);

Comment on lines 5723 to 5724
evpcmpb(ktmp2, k0, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
kandql(ktmp2, ktmp1 , ktmp2);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);

Comment on lines 5736 to 5737
evpcmpb(ktmp2, k0, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
kandql(ktmp2, ktmp1 , ktmp2);
Copy link

Choose a reason for hiding this comment

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

This can be replaced by:
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);

You could also use ktmp1 as the destination in the above instruction and its use thereby remove the ktmp2 usage altogether.

@sviswa7
Copy link

sviswa7 commented Aug 17, 2022

With above suggestions, the rearrange_bytes() kernel would look something like below:
movl(rtmp, 16);
evpbroadcastb(xtmp2, rtmp, vlen_enc);

// Compute a mask for shuffle vector by comparing indices with expression INDEX < 16,
// Broadcast first 128 bit lane across entire vector, shuffle the vector lanes using
// original shuffle indices and move the shuffled lanes corresponding to true
// mask to destination vector.
evpcmpb(ktmp2, k0, shuffle, xtmp2, Assembler::lt, true, vlen_enc);
evshuffi64x2(xtmp3, k0, src, src, 0x0, false, vlen_enc);
evpshufb(dst, ktmp2, xtmp3, shuffle, false, vlen_enc);

// Perform above steps with lane comparison expression as INDEX >= 16 && INDEX < 32
// and broadcasting second 128 bit lane.

evpcmpb(ktmp1, k0, shuffle, xtmp2, Assembler::nlt, true, vlen_enc);
vpsllq(xtmp5, xtmp2, 0x1, vlen_enc);
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
evshuffi64x2(xtmp3, k0, src, src, 0x55, false, vlen_enc);
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

// Perform above steps with lane comparison expression as INDEX >= 32 && INDEX < 48
// and broadcasting third 128 bit lane.
evpcmpb(ktmp1, k0, shuffle, xtmp5, Assembler::nlt, true, vlen_enc);
vpaddb(xtmp5, xtmp2, xtmp5, vlen_enc);
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
evshuffi64x2(xtmp3, k0, src, src, 0xaa, false, vlen_enc);
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

// Perform above steps with lane comparison expression as INDEX >= 48 && INDEX < 64
// and broadcasting third 128 bit lane.
evpcmpb(ktmp1, k0, shuffle, xtmp5, Assembler::nlt, true, vlen_enc);
vpsllq(xtmp5, xtmp2, 0x2, vlen_enc);
evpcmpb(ktmp2, ktmp1, shuffle, xtmp5, Assembler::lt, true, vlen_enc);
evshuffi64x2(xtmp3, k0, src, src, 0xff, false, vlen_enc);
evpshufb(dst, ktmp2, xtmp3, shuffle, true, vlen_enc);

The number of xtmp and ktmp registers could also be further reduced.

@jatin-bhateja
Copy link
Member Author

Hi @sviswa7 , your comments have been addressed.
Hi @vnkozlov can you kindly re-run this through your test framework.

Comment on lines 5709 to 5710
evpcmpb(ktmp2, k0, shuffle, xtmp2, Assembler::lt, true, vlen_enc);
kandql(ktmp2, ktmp1, ktmp2);
Copy link

Choose a reason for hiding this comment

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

This sequence could be replaced by:
evpcmpb(ktmp2, ktmp1, shuffle, xtmp2, Assembler::lt, true, vlen_enc);

It could also be further improved to:
evpcmpb(ktmp1, ktmp1, shuffle, xtmp2, Assembler::lt, true, vlen_enc);

Thereby removing the need for ktmp2 altogether.

There are three instances of this in the algorithm.

Rest of the changes look good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @sviswa7 for your comments.

@jatin-bhateja
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Aug 23, 2022

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

  • 27af014: 8292743: Missing include resourceHash.hpp
  • f58aaab: 8292262: adjust timeouts in several M&M tests
  • ab69885: 8292215: java/util/stream/boottest/java.base/java/util/stream/SpinedBufferTest.java times out with slowdebug
  • 54843b7: 8290211: jdk/internal/vm/Continuation/Fuzz.java failed with "AssertionError: Failed to compile int Fuzz.com_int(int,int) in 5000ms"
  • 8a0c3e5: 8292261: adjust timeouts in JLI GetObjectSizeIntrinsicsTest.java
  • 8e8ee4b: 8292596: Make SymbolHashMap a ResourceHashtable
  • aa9b8f0: 8292043: Incorrect decoding near EOF for stateful decoders like UTF-16
  • f95ee79: 8292566: Add reference to the java.nio.file package in java.nio package documentation
  • 45c3e89: 8292316: Tests should not rely on specific JAR file names (jpackage)
  • db77227: 8282684: Obsolete UseContainerCpuShares and PreferContainerQuotaForCPUCount flags
  • ... and 79 more: https://git.openjdk.org/jdk/compare/6e6ae596d6bd73909b90911e01fbd0c16f6335e1...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 23, 2022
@openjdk openjdk bot closed this Aug 23, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 23, 2022
@openjdk
Copy link

openjdk bot commented Aug 23, 2022

@jatin-bhateja Pushed as commit 38a8191.

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

@vnkozlov
Copy link
Contributor

FTR, the latest version passed tier1-4 clean.

@openjdk
Copy link

openjdk bot commented Nov 22, 2022

@jatin-bhateja Could not automatically backport 38a81913 to openjdk/jdk19u due to conflicts in the following files:

  • src/hotspot/cpu/x86/c2_MacroAssembler_x86.hpp

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk19u. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk19u master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b jatin-bhateja-backport-38a81913

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk 38a81913d33c856d64b7c26f934026815a482e43

# Backport the commit
$ git cherry-pick --no-commit 38a81913d33c856d64b7c26f934026815a482e43
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 38a81913d33c856d64b7c26f934026815a482e43'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk19u with the title Backport 38a81913d33c856d64b7c26f934026815a482e43.

@jatin-bhateja jatin-bhateja deleted the JDK-8290322 branch January 20, 2023 21:26
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
Development

Successfully merging this pull request may close these issues.

5 participants