-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
👋 Welcome back svkamath! A progress list of the required criteria for merging this PR into |
@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:
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
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 |
@smita-kamath The following label will be automatically applied to this pull request:
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. |
@smita-kamath this pull request can not be integrated into 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 |
Webrevs
|
The line 1306 in vm_version_x86.cpp needs to be changed from: |
@@ -443,4 +443,5 @@ ATTRIBUTE_ALIGNED(64) const julong StubRoutines::x86::_k512_W[] = | |||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, | |||
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL, | |||
}; | |||
|
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.
Extra blank line could be removed.
assert(VM_Version::supports_avx2() || VM_Version::supports_sha512(), ""); | ||
assert(VM_Version::supports_bmi2(), ""); |
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.
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); |
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.
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(), ""); |
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 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); |
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 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) { |
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.
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);
The following comment is still pending: |
/label hotspot-compiler |
@sviswa7 |
Looks good to me. |
@@ -443,4 +443,5 @@ ATTRIBUTE_ALIGNED(64) const julong StubRoutines::x86::_k512_W[] = | |||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, | |||
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL, | |||
}; | |||
|
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.
Remove this newline.
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); | ||
} |
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.
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); | |
} |
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] |
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 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.
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 can do this change in a separate PR.
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 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.
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 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) { |
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.
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
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.
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)
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] |
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 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.
@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. |
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.
Tier 1-3 passed on windows-x64, linux-x64, and macos-aarch64
/integrate |
@smita-kamath |
/sponsor |
Going to push as commit 18bcbf7.
Your commit was automatically rebased without conflicts. |
@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. |
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
Issue
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