https://gcc.gnu.org/g:2d5b266733b5bd4157a4be05868ce1f070458e20

commit r17-6505-g2d5b266733b5bd4157a4be05868ce1f070458e20
Author: Jiawei <[email protected]>
Date:   Fri Nov 20 15:56:42 2026 -0700

    [PATCH 4/5] RISC-V: Use Zbkc for wide CRC64 updates on RV64
    
    Use clmul and clmulh for reversed CRC64 updates that consume 32 or
    64 bits on RV64 with Zbkc.  Each update replaces four or eight byte
    table lookups.  Zbc targets already use clmulr.
    
    Shift the reflected polynomial left by one to align the high product.
    If the shift drops its top bit, XOR the multiplicand into the result.
    
    To choose the cutoff, I compared the multiply sequence with table lookups
    at all four input widths on a SpacemiT K1 and in gem5.  The benchmark
    used a warm 4 KiB buffer and -O2 -march=rv64gc_zbkc.  Negative values
    in the table indicate a reduction in cycles.  K1 figures are
    medians over 20 paired runs.  The gem5 Minor and four-wide O3 models
    used their integer multiply units for clmul and clmulh.
    
                      Inlined update             One call per update
      Input bits      K1      Minor       O3           K1       O3
               8   +22.7%    +20.0%   +24.9%       +61.8%   +50.2%
              16   -27.0%    -23.7%   -37.5%       +22.3%   -24.8%
              32   -59.2%    -57.6%   -68.7%       -33.2%   -65.4%
              64   -83.1%    -81.1%   -87.5%       -70.5%   -82.6%
    
    For 16-bit inputs, a separate call materializes the multiply constants
    on each update and regresses on K1.  The byte loop was also sensitive
    to the gem5 execution unit.  With the default ALU mapping it took 24.9%
    fewer cycles on O3.  The 32- and 64-bit updates saved cycles on K1 and in
    both gem5 mappings.
    
    Testing gcc.target/riscv/crc-*.c with make -j4 report-gcc on RV32GC
    and RV64GC showed no regressions.
    
    gcc/ChangeLog:
    
            * config/riscv/bitmanip.md (crc_rev<ANYI1:mode><ANYI:mode>4):
            Use Zbkc for 32- and 64-bit inputs to CRC64.
            * config/riscv/riscv.cc (riscv_optab_supported_p): Update comment.
            (expand_reversed_crc_using_clmul): Correct the high product for
            a full-width polynomial when clmulr is unavailable.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/crc-rev-zbkc.c: New test.

Diff:
---
 gcc/config/riscv/bitmanip.md                  | 13 +++++--------
 gcc/config/riscv/riscv.cc                     | 16 ++++++++++++----
 gcc/testsuite/gcc.target/riscv/crc-rev-zbkc.c | 20 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index 943eb2c485eb..c1feb703e95e 100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -1479,16 +1479,13 @@
   /* We don't support the case when data's size is bigger than CRC's size.  */
   "<ANYI:MODE>mode >= <ANYI1:MODE>mode"
 {
-  /* If we have the ZBC or ZBKC extension (ie, clmul) and
-     it is possible to store the quotient within a single variable
-     (E.g.  CRC64's quotient may need 65 bits,
-     we can't keep it in 64 bit variable.)
-     then use clmul instruction to implement the CRC.
-     If we have the ZBC extension (ie, clmulr) 1 bit can be left
-     implicit, allowing CRC64 using a 64 bit variable.  */
+  /* Zbc has clmulr for full-width CRCs.  For CRC64 with Zbkc, use
+     clmulh when an update replaces at least four byte table lookups.  */
   if (((TARGET_ZBKC || TARGET_ZBC || TARGET_ZVBC)
        && <ANYI:MODE>mode < word_mode)
-      || (TARGET_ZBC && <ANYI:MODE>mode == word_mode))
+      || (TARGET_ZBC && <ANYI:MODE>mode == word_mode)
+      || (TARGET_ZBKC && <ANYI:MODE>mode == DImode
+         && <ANYI1:MODE>mode >= SImode))
     expand_reversed_crc_using_clmul (<ANYI:MODE>mode, <ANYI1:MODE>mode,
                                     operands);
   else
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 2e22e24de4db..3365a8315699 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -13722,8 +13722,9 @@ static bool
 riscv_optab_supported_p (int op, machine_mode, machine_mode result_mode,
                         optimization_type opt_type)
 {
-  /* The second CRC optab mode is the result mode.  The CLMUL expansion
-     requires room for a quotient wider than the CRC value itself.  */
+  /* The second CRC optab mode is the result mode.  For a full-width
+     Zbkc CRC, materializing the constants for clmul can take more
+     space than the original loop.  */
   if (op == crc_rev_optab && opt_type != OPTIMIZE_FOR_SPEED)
     return (((TARGET_ZBKC || TARGET_ZBC || TARGET_ZVBC)
             && result_mode < word_mode)
@@ -16222,8 +16223,7 @@ expand_reversed_crc_using_clmul (scalar_mode crc_mode, 
scalar_mode data_mode,
   ref_polynomial = reflect_hwi (UINTVAL (polynomial),
                                crc_size);
 
-  bool use_clmulr = crc_size == BITS_PER_WORD;
-  gcc_assert (TARGET_ZBC || !use_clmulr);
+  bool use_clmulr = crc_size == BITS_PER_WORD && TARGET_ZBC;
 
   rtx t1 = gen_reg_rtx (word_mode);
   if (use_clmulr)
@@ -16257,6 +16257,14 @@ expand_reversed_crc_using_clmul (scalar_mode crc_mode, 
scalar_mode data_mode,
          else
            emit_insn (gen_riscv_clmulr_si (a0, a0, t1));
        }
+      else if (crc_mode == DImode && (UINTVAL (polynomial) & 1))
+       {
+         /* The left shift of the reflected polynomial discarded its top
+            bit.  XOR the multiplicand back into the high product.  */
+         rtx high = gen_reg_rtx (DImode);
+         emit_insn (gen_riscv_clmulh_di (high, a0, t1));
+         riscv_expand_op (XOR, DImode, a0, high, a0);
+       }
       else
        {
          if (TARGET_64BIT)
diff --git a/gcc/testsuite/gcc.target/riscv/crc-rev-zbkc.c 
b/gcc/testsuite/gcc.target/riscv/crc-rev-zbkc.c
new file mode 100644
index 000000000000..af8d4dcde198
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/crc-rev-zbkc.c
@@ -0,0 +1,20 @@
+/* { dg-do compile { target rv64 } } */
+/* { dg-options "-O2 -march=rv64gc_zbkc -mabi=lp64" } */
+
+#define CRC64(BITS, TYPE, POLY)                                        \
+  unsigned long                                                        \
+  crc##BITS (unsigned long crc, TYPE data)                     \
+  {                                                            \
+    return __builtin_rev_crc64_data##BITS (crc, data, POLY);   \
+  }
+
+#define POLY 0x42f0e1eba9ea3693UL
+
+CRC64 (8, unsigned char, POLY)
+CRC64 (16, unsigned short, POLY)
+CRC64 (32, unsigned int, POLY)
+CRC64 (64, unsigned long, POLY - 1)
+
+/* Only the 32- and 64-bit updates use the carry-less multiply sequence.  */
+/* { dg-final { scan-assembler-times {\mclmul\M} 2 } } */
+/* { dg-final { scan-assembler-times {\mclmulh\M} 2 } } */

Reply via email to