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

feat: use feature [compress, asm] of sha2 for aarch64 #1404

Merged
merged 1 commit into from
Mar 8, 2021

Conversation

dgbo
Copy link
Contributor

@dgbo dgbo commented Jan 30, 2021

Hi,

As of now, if FIL_PROOFS_USE_MULTICORE_SDR == 1, sha2::compress256 is used to hash the data in Phase 1.
The function sha2::compress256 is provided with feature compress of sha2.
But for aarch64, only with feature compress, the sha2::compress256 does not use the aarch64 accelerators, e.g. sha256su0, but degradats to soft_compress.
According to our tests, that ~x6 slower than the ASM one.
We should add feature asm to enable those aarch64 accelerators as the code [1] in sha2 shows.

Due to the compile BUG of sha2 fixed by PR https://github.com/RustCrypto/hashes/pull/207, commited as 78f00c5f6d218, we need a git dependency here.

In addition, to keep x86 use only compress of sha2 as before, we need add -Zfeatures=itarget to support conditional compilation of dependency feature based on target.
I've raise a separate PR (filecoin-project/filecoin-ffi#163) in filecoin-ffi for this.

Regards.

[1] https://github.com/RustCrypto/hashes/blob/a18e0bdaac45ad97419292b24fb1e3d470336d11/sha2/src/sha256.rs#L145

@@ -41,6 +40,11 @@ libc = "0.2"
fdlimit = "0.2.0"
fr32 = { path = "../fr32", default-features = false }

[target."cfg(target_arch = \"aarch64\")".dependencies]
sha2 = { git = "https://github.com/RustCrypto/hashes.git", rev = "78f00c5f6d218", features = ["compress", "asm"] }
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the recent 0.9.3 release contains that commit, so you can now point to that version instead of a Git commit.

Copy link
Contributor Author

@dgbo dgbo Feb 2, 2021

Choose a reason for hiding this comment

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

Thank you for the review.

I guess we don't need -Zfeatures=itarget [1] either, since the sha2-0.9.3 has updated to use the instrinsic backend for x86 platforms even if asm is enbled [2].
Use sha2-0.9.3 with feature ["compress", "asm"] would be enough, I will withdraw [1] if this PR can be integerated.

Regards.

[1] filecoin-project/filecoin-ffi#163
[2] RustCrypto/hashes#224

@dgbo
Copy link
Contributor Author

dgbo commented Feb 8, 2021

Hi, can I get a review for this?

Regards.

@cryptonemo
Copy link
Collaborator

@dgbo Thanks, this looks good! Are you able to rebase it against master?

@dgbo
Copy link
Contributor Author

dgbo commented Feb 8, 2021

@dgbo Thanks, this looks good! Are you able to rebase it against master?

Thank you! I've rebased this against commit 35ca38a of master in the newest version.

cryptonemo
cryptonemo previously approved these changes Feb 8, 2021
@@ -19,7 +19,7 @@ merkletree = "0.21.0"
mapr = "0.8.0"
num-bigint = "0.2"
num-traits = "0.2"
sha2 = { version = "0.9.1", features = ["compress"] }
sha2 = { version = "0.9.3", features = ["compress", "asm"] }
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not enable "asm" by default, this will result in build issues and compatibility accross other platforms

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, good catch. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comments.

I run local tests with lotus-bench with this PR on AArch64 and AMD7742 to reproduce the issues as mentioned.
Lotus builds on both platforms are fine. On AMD7742, the x86 instrinsic backend, i.e. sha2::sha256::x86::compress, is used as before.

Could you please be more specific about the build and compatibility issues? I must miss something. :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

The issue is that the default flags are used to generate prebuilts, which sometimes end up not being portable anymore when asm is enabled by default. So instead this should be threaded through as a general asm feature and only be enabled in filecoin-ffi in the native/local builds.

Copy link
Contributor Author

@dgbo dgbo Feb 18, 2021

Choose a reason for hiding this comment

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

filecoin-ffi

Sorry for being silent last few days, it's Spring festival.

Thanks for clarifying this.
I see we have some platform dependent code in this repository, e.g. prefetch [1].
How do you think if we only enable asm feature for aarch64 here? Something like:

## rust-fil-proofs changes
diff --git a/storage-proofs/porep/Cargo.toml b/storage-proofs/porep/Cargo.toml
index e52f30d..f6549c4 100644
+[target."cfg(target_arch = \"aarch64\")".dependencies]
+sha2 = { version = "0.9.3", features = ["compress", "asm"] }
+[target."cfg(not(target_arch = \"aarch64\"))".dependencies]
+sha2 = { version = "0.9.3", features = ["compress"] }

##  filecoin-ffi changes
diff --git a/install-filcrypto b/install-filcrypto
index dac54b7..dc56a4d 100755
--- a/install-filcrypto
+++ b/install-filcrypto
@@ -150,12 +150,13 @@ build_from_source() {
     # Add feature specific rust flags as needed here.
+    additional_flags="-Zfeatures=itarget"
     if [ "${FFI_USE_BLST_PORTABLE}" == "1" ]; then
-        additional_flags="--no-default-features --features blst --features blst-portable"
+        additional_flags="--no-default-features --features blst --features blst-portable ${additional_flags}"
     elif [ "${FFI_USE_BLST}" == "1" ]; then
-        additional_flags="--no-default-features --features blst"
+        additional_flags="--no-default-features --features blst ${additional_flags}"
     else
-        additional_flags="--no-default-features --features pairing"
+        additional_flags="--no-default-features --features pairing ${additional_flags}"
     fi

[1]

Copy link
Contributor

Choose a reason for hiding this comment

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

yes I believe something like that should work

Copy link
Contributor Author

@dgbo dgbo Mar 6, 2021

Choose a reason for hiding this comment

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

OK, updated a commit. Only enable asm feature for aarch64 now.
Reopened filecoin-project/filecoin-ffi#163 in filecoin-ffi for the changes we discussed, which supports conditional compilation of dependency feature based on target.
Both PRs have been rebased against master.

Suggestions?
Thanks. :-)

@dgbo
Copy link
Contributor Author

dgbo commented Mar 6, 2021

Oops, this PR seems broken when I was trying to rebase it. Sorry about that. Reopening...

@dgbo dgbo reopened this Mar 6, 2021
Copy link
Contributor

@dignifiedquire dignifiedquire 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

@cryptonemo cryptonemo merged commit dbb5bdd into filecoin-project:master Mar 8, 2021
@dgbo
Copy link
Contributor Author

dgbo commented Mar 9, 2021

@cryptonemo @dignifiedquire Thanks for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants