Skip to contentSkip to conversation

lib/crc: Add 4-way folding in NEON crc64_nvme implementation

PATCH Superseded Shreesh Adiga 3 participants 5 messages Nov 18 – Nov 22

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/crc: Add 4-way folding in NEON crc64_nvme implementation

Use 4-way folding in the shared NEON crc64_nvme for AArch64 as well as AArch32. This improves performance by about 2x for 512 byte data size on Cortex-X925.

Add similar optimizations from x86 implementation like reducing the 4x to 2x and doing a tail (len >= 32) 2x fold as well as partial block handling to process lengths which are not multiple of 16 bytes.

The partial block stage is enabled only for AArch64 as it needs the 128-bit TBL instruction not available in AArch32. Modified the loop dispatch on AArch64 to allow any len >= 128 rather than just multiple of 16 bytes.

Modified the pmull intrinsics from crc64-neon.h to use inline assembly as the intrinsic usage was causing the compiler to generate the fold constants via mov+movk+fmov/dup which was about 10 instructions for 16-byte constant instead of expected adrp+ld1.

---
 lib/crc/arm64/crc64-neon.h | 15 +++---
 lib/crc/arm64/crc64.h      | 18 ++++---
 lib/crc/crc64-neon.c       | 96 +++++++++++++++++++++++++++++++++++---
 3 files changed, 110 insertions(+), 19 deletions(-)
lib/crc/arm64/crc64-neon.h +8 -7
index fcd5b1e6f812..700a3a8619f4 100644
@@ -2,20 +2,21 @@
 
 static inline uint64x2_t pmull64(uint64x2_t a, uint64x2_t b)
 {
-	return vreinterpretq_u64_p128(vmull_p64(vgetq_lane_u64(a, 0),
-						vgetq_lane_u64(b, 0)));
+	uint64x2_t r;
+
+	asm("pmull %0.1q, %1.1d, %2.1d" : "=w"(r) : "w" (a), "w" (b));
+	return r;
 }
 
 static inline uint64x2_t pmull64_high(uint64x2_t a, uint64x2_t b)
 {
-	poly64x2_t l = vreinterpretq_p64_u64(a);
-	poly64x2_t m = vreinterpretq_p64_u64(b);
+	uint64x2_t r;
 
-	return vreinterpretq_u64_p128(vmull_high_p64(l, m));
+	asm("pmull2 %0.1q, %1.2d, %2.2d" : "=w"(r) : "w" (a), "w" (b));
+	return r;
 }
 
 static inline uint64x2_t pmull64_hi_lo(uint64x2_t a, uint64x2_t b)
 {
-	return vreinterpretq_u64_p128(vmull_p64(vgetq_lane_u64(a, 1),
-						vgetq_lane_u64(b, 0)));
+	return pmull64(vextq_u64(a, a, 1), b);
 }
lib/crc/arm64/crc64.h +12 -6
index c7a69e1f3d8f..06dd08a21dd6 100644
@@ -10,19 +10,25 @@
 
 u64 crc64_nvme_neon(u64 crc, const u8 *p, size_t len);
 
+static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pmull);
+
 #define crc64_be_arch crc64_be_generic
 
 static inline u64 crc64_nvme_arch(u64 crc, const u8 *p, size_t len)
 {
-	if (len >= 128 && cpu_have_named_feature(PMULL) &&
+	if (static_branch_likely(&have_pmull) && len >= 128 &&
 	    likely(may_use_simd())) {
-		size_t chunk = len & ~15;
-
 		scoped_ksimd()
-			crc = crc64_nvme_neon(crc, p, chunk);
+			crc = crc64_nvme_neon(crc, p, len);
 
-		p += chunk;
-		len &= 15;
+		return crc;
 	}
 	return crc64_nvme_generic(crc, p, len);
 }
+
+#define crc64_mod_init_arch crc64_mod_init_arch
+static void crc64_mod_init_arch(void)
+{
+	if (cpu_have_named_feature(PMULL))
+		static_branch_enable(&have_pmull);
+}
lib/crc/crc64-neon.c +90 -6

119 lines not on this page · raw message

Signature · 1 line
2.56.1
#1 lore raw
Quoted 13 lines · Shreesh Adiga

Use 4-way folding in the shared NEON crc64_nvme for AArch64 as well as AArch32. This improves performance by about 2x for 512 byte data size on Cortex-X925.

Add similar optimizations from x86 implementation like reducing the 4x to 2x and doing a tail (len >= 32) 2x fold as well as partial block handling to process lengths which are not multiple of 16 bytes.

The partial block stage is enabled only for AArch64 as it needs the 128-bit TBL instruction not available in AArch32. Modified the loop dispatch on AArch64 to allow any len >= 128 rather than just multiple of 16 bytes.

Quoted 5 lines

Modified the pmull intrinsics from crc64-neon.h to use inline assembly as the intrinsic usage was causing the compiler to generate the fold constants via mov+movk+fmov/dup which was about 10 instructions for 16-byte constant instead of expected adrp+ld1.

Why is that better?

#2 lore raw
Quoted 16 lines · Ard Biesheuvel
Quoted 13 lines · Shreesh Adiga

Use 4-way folding in the shared NEON crc64_nvme for AArch64 as well as AArch32. This improves performance by about 2x for 512 byte data size on Cortex-X925.

Add similar optimizations from x86 implementation like reducing the 4x to 2x and doing a tail (len >= 32) 2x fold as well as partial block handling to process lengths which are not multiple of 16 bytes.

The partial block stage is enabled only for AArch64 as it needs the 128-bit TBL instruction not available in AArch32. Modified the loop dispatch on AArch64 to allow any len >= 128 rather than just multiple of 16 bytes.

Quoted 7 lines
Quoted 5 lines

Modified the pmull intrinsics from crc64-neon.h to use inline assembly as the intrinsic usage was causing the compiler to generate the fold constants via mov+movk+fmov/dup which was about 10 instructions for 16-byte constant instead of expected adrp+ld1.

Why is that better?

Based on my counting, 10 instructions (mov + movk + movk + movk + dup; mov + movk + movk + movk + fmov) for loading a single 16-byte fold constant = 40 bytes. Whereas now it is 2 instructions = 8 bytes + 16 bytes rodata.
Since we have 4 such fold constants pairs (4x, 2x, 1x, barret) it reduces the instruction count significantly as well.
So lesser read-only memory usage. Also for in-order cores I assume that the instruction sequence has serial dependency as well.

Previous discussion with Demian who had highlighted this point and suggested to use inline asm for PMULL rather than intrinsic:
"Scalar vmull_p64() makes compilers materialize the constants via mov/movk + fmov (42/97 insns on Clang)."
Hopefully the above reasoning is sufficient justification for this change.

Thanks,
Shreesh

maintainer
#3 lore raw
In reply to Shreesh Adiga
Quoted 4 lines · Shreesh Adiga

The partial block stage is enabled only for AArch64 as it needs the 128-bit TBL instruction not available in AArch32. Modified the loop dispatch on AArch64 to allow any len >= 128 rather than just multiple of 16 bytes.

Note that an AArch32 implementation of fold_partial_block() is possible, e.g. vqtbl1q_u8() could be replaced with two vtbl2_u8(). Normally I'd consider an AArch32 specific optimization not worth including, but it might be worthwhile here since it would avoid divergence in the code due to the following arch-specific precondition:

    /* Assumes len >= 64. AArch32 also assumes len is multiple of 16. */
Quoted 4 lines
 lib/crc/arm64/crc64-neon.h | 15 +++---
 lib/crc/arm64/crc64.h      | 18 ++++---
 lib/crc/crc64-neon.c       | 96 +++++++++++++++++++++++++++++++++++---
 3 files changed, 110 insertions(+), 19 deletions(-)

Anyway, I've applied this patch to
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=crc-next

In general I'd prefer better comments in the code though, especially documenting nonobvious decisions like why pmull and pmull2 are accessed using inline asm instead of intrinsics.

So if you'd like to do a follow-up patch, it could be worthwhile to address the above two issues.

Thanks,

- Eric

#4 lore raw
Quoted 16 lines · Eric Biggers

Note that an AArch32 implementation of fold_partial_block() is possible, e.g. vqtbl1q_u8() could be replaced with two vtbl2_u8(). Normally I'd consider an AArch32 specific optimization not worth including, but it might be worthwhile here since it would avoid divergence in the code due to the following arch-specific precondition:

    /* Assumes len >= 64. AArch32 also assumes len is multiple of 16. */
Quoted 4 lines
 lib/crc/arm64/crc64-neon.h | 15 +++---
 lib/crc/arm64/crc64.h      | 18 ++++---
 lib/crc/crc64-neon.c       | 96 +++++++++++++++++++++++++++++++++++---
 3 files changed, 110 insertions(+), 19 deletions(-)

Anyway, I've applied this patch to
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=crc-next

Quoted 6 lines

In general I'd prefer better comments in the code though, especially documenting nonobvious decisions like why pmull and pmull2 are accessed using inline asm instead of intrinsics.

So if you'd like to do a follow-up patch, it could be worthwhile to address the above two issues.

Okay have submitted follow-up patch for implementing the partial block for AArch32 and include some more documentation and making it consistent on both 64 and 32 bit.
Once this is merged then will start looking into EOR3 support. If you have further suggestions, then do let me know!

Thanks,
Shreesh