From 25b9857012b41149c8e900754077bc2f852fc187 Mon Sep 17 00:00:00 2001 From: "xiang.gao" Date: Wed, 13 Sep 2023 15:13:37 +0800 Subject: [PATCH] PostgreSQL: CRC32C optimization Crc32c Parallel computation optimization Algorithm comes from Intel whitepaper: crc-iscsi-polynomial-crc32-instruction-paper Input data is divided into three equal-sized blocks. Three parallel blocks (crc0, crc1, crc2) for 1024 Bytes. One Block: 42(BLK_LEN) * 8 bytes Crc32c unitest: https://gist.github.com/gaoxyt/138fd53ca1eead8102eeb9204067f7e4 Crc32c benchmark: https://gist.github.com/gaoxyt/4506c10fc06b3501445e32c4257113e9 It gets ~2x speedup compared to linear Arm crc32c instructions. Signed-off-by: xiang.gao Change-Id: If876bbca5bbc3940946a7d72e14fe9fdf54682c1 --- config/c-compiler.m4 | 26 ++++++++ configure | 107 ++++++++++++++++++++++++++++++ configure.ac | 33 +++++++++ meson.build | 34 ++++++++++ src/include/pg_config.h.in | 6 ++ src/include/port/pg_crc32c.h | 14 +++- src/port/Makefile | 5 -- src/port/meson.build | 2 +- src/port/pg_crc32c_armv8.c | 61 ++++++++++++++++- src/port/pg_crc32c_armv8_choose.c | 52 ++++++++++++++- 10 files changed, 326 insertions(+), 14 deletions(-) diff --git a/config/c-compiler.m4 b/config/c-compiler.m4 index 5db02b2ab7..89fc31e12a 100644 --- a/config/c-compiler.m4 +++ b/config/c-compiler.m4 @@ -662,6 +662,32 @@ fi undefine([Ac_cachevar])dnl ])# PGAC_ARMV8_CRC32C_INTRINSICS +# PGAC_ARMV8_CRYPTO_INTRINSICS +# ---------------------------- +# Check if the compiler supports the PMULL instructions +# using the vmull_p64 intrinsic function. These instructions +# were first introduced in ARMv8 CRYPTO Extension. +# +# An optional compiler flag can be passed as argument (e.g. +# -march=armv8-a+crypto). If the intrinsics are supported, sets +# pgac_armv8_crypto_intrinsics, and CFLAGS_CRYPTO. +AC_DEFUN([PGAC_ARMV8_CRYPTO_INTRINSICS], +[define([Ac_cachevar], [AS_TR_SH([pgac_cv_armv8_crypto_intrinsics_$1])])dnl +AC_CACHE_CHECK([for vmull_p64 with CFLAGS=$1], [Ac_cachevar], +[pgac_save_CFLAGS=$CFLAGS +CFLAGS="$pgac_save_CFLAGS $1" +AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], + [return ((uint64_t)vmull_p64(0x12345678, 0x9abcde01) == 0x8860e9abc170678);])], + [Ac_cachevar=yes], + [Ac_cachevar=no]) +CFLAGS="$pgac_save_CFLAGS"]) +if test x"$Ac_cachevar" = x"yes"; then + CFLAGS_CRYPTO="$1" + pgac_armv8_crypto_intrinsics=yes +fi +undefine([Ac_cachevar])dnl +])# PGAC_ARMV8_CRYPTO_INTRINSICS + # PGAC_LOONGARCH_CRC32C_INTRINSICS # --------------------------- # Check if the compiler supports the LoongArch CRCC instructions, using diff --git a/configure b/configure index cfd968235f..85375bfcf8 100755 --- a/configure +++ b/configure @@ -17995,6 +17995,82 @@ fi fi +# Check for ARMv8 CRYPTO Extension intrinsics to do polynomial multiplication +# +# First check if vmull_p64 intrinsics can be used with the default compiler +# flags. If not, check if adding -march=armv8-a+crypto flag helps. +# CFLAGS_CRYPTO is set if the extra flag is required. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for vmull_p64 with CFLAGS=" >&5 +$as_echo_n "checking for vmull_p64 with CFLAGS=... " >&6; } +if ${pgac_cv_armv8_crypto_intrinsics_+:} false; then : + $as_echo_n "(cached) " >&6 +else + pgac_save_CFLAGS=$CFLAGS +CFLAGS="$pgac_save_CFLAGS " +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main () +{ +return ((uint64_t)vmull_p64(0x12345678, 0x9abcde01) == 0x8860e9abc170678); + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + pgac_cv_armv8_crypto_intrinsics_=yes +else + pgac_cv_armv8_crypto_intrinsics_=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +CFLAGS="$pgac_save_CFLAGS" +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_armv8_crypto_intrinsics_" >&5 +$as_echo "$pgac_cv_armv8_crypto_intrinsics_" >&6; } +if test x"$pgac_cv_armv8_crypto_intrinsics_" = x"yes"; then + CFLAGS_CRYPTO="" + pgac_armv8_crypto_intrinsics=yes +fi + +if test x"$pgac_armv8_crypto_intrinsics" != x"yes"; then + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for vmull_p64 with CFLAGS=-march=armv8-a+crypto" >&5 +$as_echo_n "checking for vmull_p64 with CFLAGS=-march=armv8-a+crypto... " >&6; } +if ${pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto+:} false; then : + $as_echo_n "(cached) " >&6 +else + pgac_save_CFLAGS=$CFLAGS +CFLAGS="$pgac_save_CFLAGS -march=armv8-a+crypto" +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +#include +int +main () +{ +return ((uint64_t)vmull_p64(0x12345678, 0x9abcde01) == 0x8860e9abc170678); + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto=yes +else + pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +CFLAGS="$pgac_save_CFLAGS" +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto" >&5 +$as_echo "$pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto" >&6; } +if test x"$pgac_cv_armv8_crypto_intrinsics__march_armv8_apcrypto" = x"yes"; then + CFLAGS_CRYPTO="-march=armv8-a+crypto" + pgac_armv8_crypto_intrinsics=yes +fi + +fi + # Check for LoongArch CRC intrinsics to do CRC calculations. # # Check if __builtin_loongarch_crcc_* intrinsics can be used @@ -18089,6 +18165,17 @@ if test x"$USE_SLICING_BY_8_CRC32C" = x"" && test x"$USE_SSE42_CRC32C" = x"" && fi fi +# Use ARM CRYPTO Extension if available. +if test x"$USE_ARMV8_CRYPTO" = x"" && test x"$USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK" = x""; then + if test x"$pgac_armv8_crypto_intrinsics" = x"yes" && test x"$CFLAGS_CRYPTO" = x""; then + USE_ARMV8_CRYPTO=1 + else + if test x"$pgac_armv8_crypto_intrinsics" = x"yes"; then + USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK=1 + fi + fi +fi + # Set PG_CRC32C_OBJS appropriately depending on the selected implementation. { $as_echo "$as_me:${as_lineno-$LINENO}: checking which CRC-32C implementation to use" >&5 $as_echo_n "checking which CRC-32C implementation to use... " >&6; } @@ -18145,6 +18232,26 @@ $as_echo "slicing-by-8" >&6; } fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to use ARM CRYPTO Extension" >&5 +$as_echo_n "checking whether to use ARM CRYPTO Extension... " >&6; } +if test x"$USE_ARMV8_CRYPTO" = x"1"; then + +$as_echo "#define USE_ARMV8_CRYPTO 1" >>confdefs.h + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } +else + if test x"$USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK" = x"1"; then + +$as_echo "#define USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK 1" >>confdefs.h + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: with a runtime check" >&5 +$as_echo "with a runtime check" >&6; } + else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + fi +fi # Select semaphore implementation type. if test "$PORTNAME" != "win32"; then diff --git a/configure.ac b/configure.ac index f220b379b3..297b4cf81a 100644 --- a/configure.ac +++ b/configure.ac @@ -2099,6 +2099,16 @@ if test x"$pgac_armv8_crc32c_intrinsics" != x"yes"; then PGAC_ARMV8_CRC32C_INTRINSICS([-march=armv8-a+crc]) fi +# Check for ARMv8 CRYPTO Extension intrinsics to do polynomial multiplication +# +# First check if vmull_p64 intrinsics can be used with the default compiler +# flags. If not, check if adding -march=armv8-a+crypto flag helps. +# CFLAGS_CRYPTO is set if the extra flag is required. +PGAC_ARMV8_CRYPTO_INTRINSICS([]) +if test x"$pgac_armv8_crypto_intrinsics" != x"yes"; then + PGAC_ARMV8_CRYPTO_INTRINSICS([-march=armv8-a+crypto]) +fi + # Check for LoongArch CRC intrinsics to do CRC calculations. # # Check if __builtin_loongarch_crcc_* intrinsics can be used @@ -2157,6 +2167,17 @@ if test x"$USE_SLICING_BY_8_CRC32C" = x"" && test x"$USE_SSE42_CRC32C" = x"" && fi fi fi + +# Use ARM CRYPTO Extension if available. +if test x"$USE_ARMV8_CRYPTO" = x"" && test x"$USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK" = x""; then + if test x"$pgac_armv8_crypto_intrinsics" = x"yes" && test x"$CFLAGS_CRYPTO" = x""; then + USE_ARMV8_CRYPTO=1 + else + if test x"$pgac_armv8_crypto_intrinsics" = x"yes"; then + USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK=1 + fi + fi +fi # Set PG_CRC32C_OBJS appropriately depending on the selected implementation. AC_MSG_CHECKING([which CRC-32C implementation to use]) @@ -2195,6 +2216,18 @@ else fi AC_SUBST(PG_CRC32C_OBJS) +AC_MSG_CHECKING([whether to use ARM CRYPTO Extension]) +if test x"$USE_ARMV8_CRYPTO" = x"1"; then + AC_DEFINE(USE_ARMV8_CRYPTO, 1, [Define to 1 to use ARMv8 CRYPTO Extension.]) + AC_MSG_RESULT(yes) +else + if test x"$USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK" = x"1"; then + AC_DEFINE(USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK, 1, [Define to 1 to use ARMv8 CRYPTO Extension with a runtime check.]) + AC_MSG_RESULT(with a runtime check) + else + AC_MSG_RESULT(no) + fi +fi # Select semaphore implementation type. if test "$PORTNAME" != "win32"; then diff --git a/meson.build b/meson.build index 2d516c8f37..bd7a2c824e 100644 --- a/meson.build +++ b/meson.build @@ -2101,6 +2101,40 @@ endif +############################################################### +# Check for ARMv8 CRYPTO Extension intrinsics to do polynomial multiplication +# +# If we are targeting an ARM processor that has the vmull_p64 instruction +# that is part of the ARMv8 CRYPTO Extension, use them. And if we're not +# targeting such a processor, but can nevertheless produce code that +# uses the vmull_p64 instruction, compile both, and select at runtime. +############################################################### + +if (host_cpu == 'arm' or host_cpu == 'aarch64') + + prog = ''' +#include + +int main(void) +{ + return ((uint64_t)vmull_p64(0x12345678, 0x9abcde01) == 0x8860e9abc170678); +} +''' + + if cc.links(prog, name: 'vmull_p64 without -march=armv8-a+crypto', + args: test_c_args) + # Use ARM CRYPTO Extension + cdata.set('USE_ARMV8_CRYPTO', 1) + elif cc.links(prog, name: 'vmull_p64 with -march=armv8-a+crypto', + args: test_c_args + ['-march=armv8-a+crypto']) + # Use ARM CRYPTO Extension, with runtime check + cdata.set('USE_ARMV8_CRYPTO', false) + cdata.set('USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK', 1) + endif +endif + + + ############################################################### # Other CPU specific stuff ############################################################### diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index d8a2985567..b0fceaf2ab 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -689,6 +689,12 @@ /* Define to 1 to use ARMv8 CRC Extension with a runtime check. */ #undef USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK +/* Define to 1 to use ARMv8 CRYPTO Extension. */ +#undef USE_ARMV8_CRYPTO + +/* Define to 1 to use ARMv8 CRYPTO Extension with a runtime check. */ +#undef USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK + /* Define to 1 to build with assertion checks. (--enable-cassert) */ #undef USE_ASSERT_CHECKING diff --git a/src/include/port/pg_crc32c.h b/src/include/port/pg_crc32c.h index d085f1dc00..2c366cbf64 100644 --- a/src/include/port/pg_crc32c.h +++ b/src/include/port/pg_crc32c.h @@ -50,13 +50,20 @@ typedef uint32 pg_crc32c; extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len); #elif defined(USE_ARMV8_CRC32C) + +#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF) + +#ifdef USE_ARMV8_CRYPTO +/* Use ARMv8 CRC Extension and CRYPTO Extentsion instructions. */ +#define COMP_CRC32C(crc, data, len) \ + ((crc) = pg_comp_crc32c_armv8_parallel((crc), (data), (len))) +extern pg_crc32c pg_comp_crc32c_armv8_parallel(pg_crc32c crc, const void *data, size_t len); +#else /* Use ARMv8 CRC Extension instructions. */ - #define COMP_CRC32C(crc, data, len) \ ((crc) = pg_comp_crc32c_armv8((crc), (data), (len))) -#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF) - extern pg_crc32c pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len); +#endif #elif defined(USE_LOONGARCH_CRC32C) /* Use LoongArch CRCC instructions. */ @@ -85,6 +92,7 @@ extern pg_crc32c pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t le #endif #ifdef USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK extern pg_crc32c pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len); +extern pg_crc32c pg_comp_crc32c_armv8_parallel(pg_crc32c crc, const void *data, size_t len); #endif #else diff --git a/src/port/Makefile b/src/port/Makefile index f205c2c9c5..e59e097e03 100644 --- a/src/port/Makefile +++ b/src/port/Makefile @@ -89,11 +89,6 @@ pg_crc32c_sse42.o: CFLAGS+=$(CFLAGS_CRC) pg_crc32c_sse42_shlib.o: CFLAGS+=$(CFLAGS_CRC) pg_crc32c_sse42_srv.o: CFLAGS+=$(CFLAGS_CRC) -# all versions of pg_crc32c_armv8.o need CFLAGS_CRC -pg_crc32c_armv8.o: CFLAGS+=$(CFLAGS_CRC) -pg_crc32c_armv8_shlib.o: CFLAGS+=$(CFLAGS_CRC) -pg_crc32c_armv8_srv.o: CFLAGS+=$(CFLAGS_CRC) - # # Shared library versions of object files # diff --git a/src/port/meson.build b/src/port/meson.build index a0d0a9583a..1686adb531 100644 --- a/src/port/meson.build +++ b/src/port/meson.build @@ -88,7 +88,7 @@ replace_funcs_pos = [ # arm / aarch64 ['pg_crc32c_armv8', 'USE_ARMV8_CRC32C'], - ['pg_crc32c_armv8', 'USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK', 'crc'], + ['pg_crc32c_armv8', 'USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK'], ['pg_crc32c_armv8_choose', 'USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK'], ['pg_crc32c_sb8', 'USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK'], diff --git a/src/port/pg_crc32c_armv8.c b/src/port/pg_crc32c_armv8.c index d8fae510cf..478b51afab 100644 --- a/src/port/pg_crc32c_armv8.c +++ b/src/port/pg_crc32c_armv8.c @@ -2,6 +2,7 @@ * * pg_crc32c_armv8.c * Compute CRC-32C checksum using ARMv8 CRC Extension instructions + * with ARMv8 VMULL Extentsion instructions or not * * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -15,11 +16,13 @@ #include "c.h" #include +#include #include "port/pg_crc32c.h" -pg_crc32c -pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len) +__attribute__((target("+crc+crypto"))) +static inline pg_crc32c +pg_comp_crc32c_helper(pg_crc32c crc, const void *data, size_t len, bool use_vmull) { const unsigned char *p = data; const unsigned char *pend = p + len; @@ -48,6 +51,44 @@ pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len) p += 4; } + if (use_vmull) + { +#if defined(USE_ARMV8_CRYPTO) || defined(USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK) +/* + * Crc32c parallel computation Input data is divided into three + * equal-sized blocks. Block length : 42 words(42 * 8 bytes). + * CRC0: 0 ~ 41 * 8, + * CRC1: 42 * 8 ~ (42 * 2 - 1) * 8, + * CRC2: 42 * 2 * 8 ~ (42 * 3 - 1) * 8. + */ + while (p + 1024 <= pend) + { +#define BLOCK_LEN 42 + const uint64_t *in64 = (const uint64_t *) (p); + uint32_t crc0 = crc, + crc1 = 0, + crc2 = 0; + + for (int i = 0; i < BLOCK_LEN; i++, in64++) + { + crc0 = __crc32cd(crc0, *(in64)); + crc1 = __crc32cd(crc1, *(in64 + BLOCK_LEN)); + crc2 = __crc32cd(crc2, *(in64 + BLOCK_LEN * 2)); + } + in64 += BLOCK_LEN * 2; + crc0 = __crc32cd(0, vmull_p64(crc0, 0xcec3662e)); + crc1 = __crc32cd(0, vmull_p64(crc1, 0xa60ce07b)); + crc = crc0 ^ crc1 ^ crc2; + + crc = __crc32cd(crc, *in64++); + crc = __crc32cd(crc, *in64++); + + p += 1024; +#undef BLOCK_LEN + } +#endif + } + /* Process eight bytes at a time, as far as we can. */ while (p + 8 <= pend) { @@ -73,3 +114,19 @@ pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len) return crc; } + +#if (defined(USE_ARMV8_CRC32C) && defined(USE_ARMV8_CRYPTO)) || defined(USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK) +pg_crc32c +pg_comp_crc32c_armv8_parallel(pg_crc32c crc, const void *data, size_t len) +{ + return pg_comp_crc32c_helper(crc, data, len, true); +} +#endif + +#if (defined(USE_ARMV8_CRC32C) && !defined(USE_ARMV8_CRYPTO)) || defined(USE_ARMV8_CRC32C_WITH_RUNTIME_CHECK) +pg_crc32c +pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len) +{ + return pg_comp_crc32c_helper(crc, data, len, false); +} +#endif diff --git a/src/port/pg_crc32c_armv8_choose.c b/src/port/pg_crc32c_armv8_choose.c index 0fdddccaf7..2229faecad 100644 --- a/src/port/pg_crc32c_armv8_choose.c +++ b/src/port/pg_crc32c_armv8_choose.c @@ -4,8 +4,8 @@ * Choose between ARMv8 and software CRC-32C implementation. * * On first call, checks if the CPU we're running on supports the ARMv8 - * CRC Extension. If it does, use the special instructions for CRC-32C - * computation. Otherwise, fall back to the pure software implementation + * CRC Extension and VMULL Extension. If it does, use the special instructions + * for CRC-32C computation. Otherwise, fall back to the pure software implementation * (slicing-by-8). * * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group @@ -26,6 +26,7 @@ #include #include +#include #include "port/pg_crc32c.h" @@ -77,6 +78,40 @@ pg_crc32c_armv8_available(void) return (result > 0); } +__attribute__((target("+crypto"))) +static bool +pg_vmull_armv8_available(void) +{ +#if defined(USE_ARMV8_CRYPTO) + return true; +#elif defined(USE_ARMV8_CRYPTO_WITH_RUNTIME_CHECK) + int result; + + pqsignal(SIGILL, illegal_instruction_handler); + if (sigsetjmp(illegal_instruction_jump, 1) == 0) + { + result = ((uint64_t) vmull_p64(0x12345678, 0x9abcde01) == 0x8860e9abc170678); + } + else + { + /* We got the SIGILL trap */ + result = -1; + } + pqsignal(SIGILL, SIG_DFL); + +#ifndef FRONTEND + /* We don't expect this case, so complain loudly */ + if (result == 0) + elog(ERROR, "vmull_p64 hardware results error"); + + elog(DEBUG1, "using armv8 vmull_p64 hardware = %d", (result > 0)); +#endif + return (result > 0); +#else + return false; +#endif +} + /* * This gets called on the first call. It replaces the function pointer * so that subsequent calls are routed directly to the chosen implementation. @@ -85,9 +120,20 @@ static pg_crc32c pg_comp_crc32c_choose(pg_crc32c crc, const void *data, size_t len) { if (pg_crc32c_armv8_available()) - pg_comp_crc32c = pg_comp_crc32c_armv8; + { + if (pg_vmull_armv8_available()) + { + pg_comp_crc32c = pg_comp_crc32c_armv8_parallel; + } + else + { + pg_comp_crc32c = pg_comp_crc32c_armv8; + } + } else + { pg_comp_crc32c = pg_comp_crc32c_sb8; + } return pg_comp_crc32c(crc, data, len); } -- 2.34.1