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

8341052: SHA-512 implementation using SHA-NI #20633

Closed
wants to merge 7 commits into from

Conversation

smita-kamath
Copy link

@smita-kamath smita-kamath commented Aug 19, 2024

Hi, I want to submit an optimization for SHA-512 algorithm using SHA instructions (sha512msg1, sha512msg2 and sha512rnds2) . Kindly review the code and provide feedback. Thank you.


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-8341052: SHA-512 implementation using SHA-NI (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20633

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

Using diff file

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

Webrev

Link to Webrev Comment

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 19, 2024

👋 Welcome back svkamath! 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 Aug 19, 2024

@smita-kamath 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:

8341052: SHA-512 implementation using SHA-NI

Reviewed-by: jbhateja, ascarpino, sviswanathan, sparasa

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

  • 18b55ce: 8342653: Fix minor doc issues in AnnotatedElement
  • 153ad91: 8338126: C2 SuperWord: VectorCastF2HF / vcvtps2ph produces wrong results for vector length 2
  • 80ec552: 8328528: C2 should optimize long-typed parallel iv in an int counted loop
  • 330f2b5: 8342295: compiler/jvmci/TestJVMCISavedProperties.java fails due to garbage in output
  • 1f35748: 8342102: ZGC: Optimize copy constructors in ZPhysicalMemory
  • 66ddaaa: 8340241: RISC-V: Returns mispredicted
  • 07f550b: 8340818: Add a new jtreg test root to test the generated documentation
  • 27ef6c9: 8341470: BigDecimal.stripTrailingZeros() optimization
  • 5d5d88a: 8339570: Add Tidy build support for JDK tests
  • 239d84a: 8342578: GHA: RISC-V: Bootstrap using Debian snapshot is still failing
  • ... and 50 more: https://git.openjdk.org/jdk/compare/b4ab290fd7c3d914154755a1539b48ba33338c26...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 (@sviswa7, @jatin-bhateja, @ascarpino) 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
Copy link

openjdk bot commented Aug 19, 2024

@smita-kamath 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 Aug 19, 2024
@openjdk
Copy link

openjdk bot commented Sep 12, 2024

@smita-kamath 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 sha-512
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 Sep 12, 2024
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Sep 26, 2024
@smita-kamath smita-kamath changed the title SHA-512 implementation using SHA-NI instructions 8341052: SHA-512 implementation using SHA-NI Sep 26, 2024
@smita-kamath smita-kamath marked this pull request as ready for review September 30, 2024 15:30
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 30, 2024
@mlbridge
Copy link

mlbridge bot commented Sep 30, 2024

Webrevs

@sviswa7
Copy link

sviswa7 commented Oct 4, 2024

The line 1306 in vm_version_x86.cpp needs to be changed from:
if (UseSHA && supports_avx2() && supports_bmi2()) {
to
if (UseSHA && supports_avx2() && (supports_bmi2() || supports_sha512())) {

@@ -443,4 +443,5 @@ ATTRIBUTE_ALIGNED(64) const julong StubRoutines::x86::_k512_W[] =
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
};

Copy link

Choose a reason for hiding this comment

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

Extra blank line could be removed.

Comment on lines 1559 to 1560
assert(VM_Version::supports_avx2() || VM_Version::supports_sha512(), "");
assert(VM_Version::supports_bmi2(), "");
Copy link

Choose a reason for hiding this comment

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

The SHA512 algorithm is also using AVX2 instructions but doesn't need bmi2.
So this needs to be changed to:
assert(VM_Version::supports_avx2(), "");
assert(VM_Version::supports_bmi2() || VM_Version::supports_sha512(), "");

Label done_hash, block_loop;
address K512_W = StubRoutines::x86::k512_W_addr();

vbroadcasti128(xmm15, ExternalAddress(StubRoutines::x86::pshuffle_byte_flip_mask_addr_sha512()), rbx);
Copy link

Choose a reason for hiding this comment

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

rbx is save on entry register. Better to use a save on call register as temp, say r10.

@@ -6762,6 +6762,27 @@ void Assembler::sha256msg2(XMMRegister dst, XMMRegister src) {
emit_int16((unsigned char)0xCD, (0xC0 | encode));
}

void Assembler::sha512msg1(XMMRegister dst, XMMRegister src) {
assert(VM_Version::supports_sha512(), "");
Copy link

Choose a reason for hiding this comment

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

For all the new sha512 instructions, this should be:
assert(VM_Version::supports_sha512() && VM_Version::supports_avx(), "");

@@ -6762,6 +6762,27 @@ void Assembler::sha256msg2(XMMRegister dst, XMMRegister src) {
emit_int16((unsigned char)0xCD, (0xC0 | encode));
}

void Assembler::sha512msg1(XMMRegister dst, XMMRegister src) {
assert(VM_Version::supports_sha512(), "");
InstructionAttr attributes(AVX_256bit, /* vex_w */ false, /* legacy_mode */ false, /* no_mask_reg */ true, /* uses_vl */ false);
Copy link

Choose a reason for hiding this comment

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

For all the new sha512 instructions legacy_mode should be true.

@@ -11672,6 +11693,17 @@ void Assembler::evbroadcasti64x2(XMMRegister dst, Address src, int vector_len) {
emit_operand(dst, src, 0);
}

void Assembler::vbroadcasti128(XMMRegister dst, Address src) {
Copy link

Choose a reason for hiding this comment

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

Please see vbroadcastf128, good to take vector_len as argument here also and have an assert to confirm that it is AVX-256 bit.
Also need:
attributes.set_address_attributes(/* tuple_type / EVEX_T4, / input_size_in_bits */ EVEX_32bit);

@sviswa7
Copy link

sviswa7 commented Oct 9, 2024

The following comment is still pending:
The line 1306 in vm_version_x86.cpp needs to be changed from:
if (UseSHA && supports_avx2() && supports_bmi2()) {
to
if (UseSHA && supports_avx2() && (supports_bmi2() || supports_sha512())) {

@sviswa7
Copy link

sviswa7 commented Oct 9, 2024

/label hotspot-compiler

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Oct 9, 2024
@openjdk
Copy link

openjdk bot commented Oct 9, 2024

@sviswa7
The hotspot-compiler label was successfully added.

@sviswa7
Copy link

sviswa7 commented Oct 9, 2024

Looks good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 9, 2024
@@ -443,4 +443,5 @@ ATTRIBUTE_ALIGNED(64) const julong StubRoutines::x86::_k512_W[] =
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
};

Copy link
Member

Choose a reason for hiding this comment

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

Remove this newline.

Comment on lines 1575 to 1587
const XMMRegister msg = xmm0;
const XMMRegister state0 = xmm1;
const XMMRegister state1 = xmm2;
const XMMRegister msgtmp0 = xmm3;
const XMMRegister msgtmp1 = xmm4;
const XMMRegister msgtmp2 = xmm5;
const XMMRegister msgtmp3 = xmm6;
const XMMRegister msgtmp4 = xmm7;

const XMMRegister shuf_mask = xmm8;
__ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
buf, state, ofs, limit, rsp, multi_block, shuf_mask);
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const XMMRegister msg = xmm0;
const XMMRegister state0 = xmm1;
const XMMRegister state1 = xmm2;
const XMMRegister msgtmp0 = xmm3;
const XMMRegister msgtmp1 = xmm4;
const XMMRegister msgtmp2 = xmm5;
const XMMRegister msgtmp3 = xmm6;
const XMMRegister msgtmp4 = xmm7;
const XMMRegister shuf_mask = xmm8;
__ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
buf, state, ofs, limit, rsp, multi_block, shuf_mask);
}
const XMMRegister msg = xmm0;
const XMMRegister state0 = xmm1;
const XMMRegister state1 = xmm2;
const XMMRegister msgtmp0 = xmm3;
const XMMRegister msgtmp1 = xmm4;
const XMMRegister msgtmp2 = xmm5;
const XMMRegister msgtmp3 = xmm6;
const XMMRegister msgtmp4 = xmm7;
const XMMRegister shuf_mask = xmm8;
__ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
buf, state, ofs, limit, rsp, multi_block, shuf_mask);
}

Comment on lines +1600 to +1602
vpermq(xmm8, xmm4, 0x1b, Assembler::AVX_256bit);//ymm8 = W[20] W[21] W[22] W[23]
vpermq(xmm9, xmm3, 0x39, Assembler::AVX_256bit);//ymm9 = W[16] W[19] W[18] W[17]
vpblendd(xmm7, xmm8, xmm9, 0x3f, Assembler::AVX_256bit);//ymm7 = W[20] W[19] W[18] W[17]
Copy link
Member

@jatin-bhateja jatin-bhateja Oct 10, 2024

Choose a reason for hiding this comment

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

I assume Algorithm is specifically crafted for 256 bit vectors and with 512 bit extension we may modify it. Do you think we should factor out following pattern and add an alternative implementation for it ?

    vpermq(xmm8, xmm4, 0x1b, Assembler::AVX_256bit);//ymm8 = W[20] W[21] W[22] W[23]
    vpermq(xmm9, xmm3, 0x39, Assembler::AVX_256bit);//ymm9 = W[16] W[19] W[18] W[17]
    vpblendd(xmm7, xmm8, xmm9, 0x3f, Assembler::AVX_256bit);//ymm7 = W[20] W[19] W[18] W[17]

This is a fixed pattern seen 4 times within computation loop and once outside the loop.
We are permuting two vectors with constant paramutation mask and blending them using immediate mask.
This is a very valid use case for two table permutation instruction VPERMI2Q (available for AVX512VL targets)
We can store permutation pattern outside the loop into a vector and then re-use it within the loop.

Copy link
Author

Choose a reason for hiding this comment

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

We can do this change in a separate PR.

Copy link
Contributor

@vamsi-parasa vamsi-parasa Oct 10, 2024

Choose a reason for hiding this comment

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

I agree with Smita. The current implementation has a one-to-one correspondence with the ipsec implementation. Any new changes or refactoring could be implemented as a separate PR.

Copy link
Member

@jatin-bhateja jatin-bhateja Oct 13, 2024

Choose a reason for hiding this comment

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

I agree, in principle, any optimization crafted to AVX2 is also applicable to AVX512 target, in future with AVX10.2 (converged ISA) we will have a 256bits flavors of two table permute for non-AVX512 targets, for now AVX-SHA512 is only available on client parts (upcoming Arrow lake) and its ok to follow the IPsec algorithm in toto.

@@ -1519,5 +1519,183 @@ void MacroAssembler::sha512_AVX2(XMMRegister msg, XMMRegister state0, XMMRegiste
}
}

void MacroAssembler::sha512_update_ni_x1(Register arg_hash, Register arg_msg, Register ofs, Register limit, bool multi_block) {
Copy link
Member

@jatin-bhateja jatin-bhateja Oct 10, 2024

Choose a reason for hiding this comment

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

Please add a comment on this mentioning the source of algorithm.
https://github.com/intel/intel-ipsec-mb/blob/main/lib/avx2_t4/sha512_x1_ni_avx2.asm

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 10, 2024
Copy link
Contributor

@vamsi-parasa vamsi-parasa left a comment

Choose a reason for hiding this comment

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

This implementation looks good to me.

I went through the implementation of sha512_update_ni_x1. Looked at it line by line and compared it to the ipsec implementation.

Thanks,
Srinivas Vamsi Parasa (Intel)

Comment on lines +1600 to +1602
vpermq(xmm8, xmm4, 0x1b, Assembler::AVX_256bit);//ymm8 = W[20] W[21] W[22] W[23]
vpermq(xmm9, xmm3, 0x39, Assembler::AVX_256bit);//ymm9 = W[16] W[19] W[18] W[17]
vpblendd(xmm7, xmm8, xmm9, 0x3f, Assembler::AVX_256bit);//ymm7 = W[20] W[19] W[18] W[17]
Copy link
Member

@jatin-bhateja jatin-bhateja Oct 13, 2024

Choose a reason for hiding this comment

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

I agree, in principle, any optimization crafted to AVX2 is also applicable to AVX512 target, in future with AVX10.2 (converged ISA) we will have a 256bits flavors of two table permute for non-AVX512 targets, for now AVX-SHA512 is only available on client parts (upcoming Arrow lake) and its ok to follow the IPsec algorithm in toto.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 13, 2024
@smita-kamath
Copy link
Author

@ascarpino, I have approvals for this PR. Would it be possible for you to run tests and let me know the results? I appreciate your help. Thanks.

Copy link
Contributor

@ascarpino ascarpino left a comment

Choose a reason for hiding this comment

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

Tier 1-3 passed on windows-x64, linux-x64, and macos-aarch64

@smita-kamath
Copy link
Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Oct 21, 2024
@openjdk
Copy link

openjdk bot commented Oct 21, 2024

@smita-kamath
Your change (at version af309de) is now ready to be sponsored by a Committer.

@sviswa7
Copy link

sviswa7 commented Oct 21, 2024

/sponsor

@openjdk
Copy link

openjdk bot commented Oct 21, 2024

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

  • 54a744b: 8340553: ZipEntry field validation does not take into account the size of a CEN header
  • 18b55ce: 8342653: Fix minor doc issues in AnnotatedElement
  • 153ad91: 8338126: C2 SuperWord: VectorCastF2HF / vcvtps2ph produces wrong results for vector length 2
  • 80ec552: 8328528: C2 should optimize long-typed parallel iv in an int counted loop
  • 330f2b5: 8342295: compiler/jvmci/TestJVMCISavedProperties.java fails due to garbage in output
  • 1f35748: 8342102: ZGC: Optimize copy constructors in ZPhysicalMemory
  • 66ddaaa: 8340241: RISC-V: Returns mispredicted
  • 07f550b: 8340818: Add a new jtreg test root to test the generated documentation
  • 27ef6c9: 8341470: BigDecimal.stripTrailingZeros() optimization
  • 5d5d88a: 8339570: Add Tidy build support for JDK tests
  • ... and 51 more: https://git.openjdk.org/jdk/compare/b4ab290fd7c3d914154755a1539b48ba33338c26...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Oct 21, 2024

@sviswa7 @smita-kamath Pushed as commit 18bcbf7.

💡 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 hotspot-dev@openjdk.org hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

None yet

5 participants