Skip to content

[scudo] Add support for LoongArch hardware CRC32 checksumming #83113

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

Merged
merged 1 commit into from
Mar 2, 2024

Conversation

xen0n
Copy link
Contributor

@xen0n xen0n commented Feb 27, 2024

One has to probe for platform capability prior to use with HWCAP, according to LoongArch documentation.

@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: WÁNG Xuěruì (xen0n)

Changes

The support is available for processors implementing the LoongArch64 subset, but one still has to probe for platform capability prior to use, according to LoongArch documentation.


Full diff: https://github.com/llvm/llvm-project/pull/83113.diff

3 Files Affected:

  • (modified) compiler-rt/lib/scudo/standalone/checksum.cpp (+10)
  • (modified) compiler-rt/lib/scudo/standalone/checksum.h (+4)
  • (modified) compiler-rt/lib/scudo/standalone/crc32_hw.cpp (+7)
diff --git a/compiler-rt/lib/scudo/standalone/checksum.cpp b/compiler-rt/lib/scudo/standalone/checksum.cpp
index 2c277391a2ec16..09fa5417653a7a 100644
--- a/compiler-rt/lib/scudo/standalone/checksum.cpp
+++ b/compiler-rt/lib/scudo/standalone/checksum.cpp
@@ -19,6 +19,8 @@
 #else
 #include <sys/auxv.h>
 #endif
+#elif defined(__loongarch_lp64)
+#include <sys/auxv.h>
 #endif
 
 namespace scudo {
@@ -75,6 +77,14 @@ bool hasHardwareCRC32() {
   return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);
 #endif // SCUDO_FUCHSIA
 }
+#elif defined(__loongarch_lp64)
+// Query HWCAP for platform capability, according to *Software Development and
+// Build Convention for LoongArch Architectures* v0.1, Section 9.1.
+//
+// Link: https://github.com/loongson/la-softdev-convention/blob/v0.1/la-softdev-convention.adoc#kernel-development
+bool hasHardwareCRC32() {
+  return !!(getauxval(AT_HWCAP) & HWCAP_LOONGARCH_CRC32);
+}
 #else
 // No hardware CRC32 implemented in Scudo for other architectures.
 bool hasHardwareCRC32() { return false; }
diff --git a/compiler-rt/lib/scudo/standalone/checksum.h b/compiler-rt/lib/scudo/standalone/checksum.h
index f8eda81fd91288..b5f97fe5111f04 100644
--- a/compiler-rt/lib/scudo/standalone/checksum.h
+++ b/compiler-rt/lib/scudo/standalone/checksum.h
@@ -30,6 +30,10 @@
 #include <arm_acle.h>
 #define CRC32_INTRINSIC FIRST_32_SECOND_64(__crc32cw, __crc32cd)
 #endif
+#ifdef __loongarch_lp64
+#include <larchintrin.h>
+#define CRC32_INTRINSIC FIRST_32_SECOND_64(__crcc_w_w_w, __crcc_w_d_w)
+#endif
 
 namespace scudo {
 
diff --git a/compiler-rt/lib/scudo/standalone/crc32_hw.cpp b/compiler-rt/lib/scudo/standalone/crc32_hw.cpp
index 73f2ae000c63d6..3f8c423fa0f49e 100644
--- a/compiler-rt/lib/scudo/standalone/crc32_hw.cpp
+++ b/compiler-rt/lib/scudo/standalone/crc32_hw.cpp
@@ -17,4 +17,11 @@ u32 computeHardwareCRC32(u32 Crc, uptr Data) {
 #endif // defined(__CRC32__) || defined(__SSE4_2__) ||
        // defined(__ARM_FEATURE_CRC32)
 
+#if defined(__loongarch_lp64)
+u32 computeHardwareCRC32(u32 Crc, uptr Data) {
+  // The LoongArch CRC intrinsics have the two input arguments swapped.
+  return static_cast<u32>(CRC32_INTRINSIC(Data, Crc));
+}
+#endif // defined(__loongarch_lp64)
+
 } // namespace scudo

Copy link

github-actions bot commented Feb 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@xen0n
Copy link
Contributor Author

xen0n commented Feb 27, 2024

cc @SixWeining @heiher @xry111

Copy link
Contributor

@ChiaHungDuan ChiaHungDuan left a comment

Choose a reason for hiding this comment

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

Thanks for adding this

Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

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

Thanks for the patch.

@SixWeining
Copy link
Contributor

When I click the merge button, I get this prompt "This commit will be authored by [email protected]". Do you mind to change your email settings before this getting landed?

One has to probe for platform capability prior to use with HWCAP,
according to LoongArch documentation.
@SixWeining SixWeining merged commit 597f976 into llvm:main Mar 2, 2024
@xen0n xen0n deleted the scudo-loong-crc branch March 2, 2024 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants