On 9/13/24 5:06 AM, Mariam Arutunian wrote:
This patch adds a new compiler pass aimed at identifying naive CRC implementations, characterized by the presence of a loop calculating a CRC (polynomial long division).
Upon detection of a potential CRC, the pass prints an informational message.

Performs CRC optimization if optimization level is >= 2,
besides optimizations for size and if fno_gimple_crc_optimization given.

This pass is added for the detection and optimization of naive CRC implementations,
improving the efficiency of CRC-related computations.

This patch includes only initial fast checks for filtering out non-CRCs,
detected possible CRCs verification and optimization parts will be provided in subsequent patches.

   gcc/

     * Makefile.in (OBJS): Add gimple-crc-optimization.o.
     * common.opt (foptimize-crc): New option.
     * common.opt.urls: Regenerate to add foptimize-crc.
     * doc/invoke.texi (-foptimize-crc): Add documentation.
     * gimple-crc-optimization.cc: New file.
     * opts.cc (default_options_table): Add OPT_foptimize_crc.
     (enable_fdo_optimizations): Enable optimize_crc.
     * passes.def (pass_crc_optimization): Add new pass.
     * timevar.def (TV_GIMPLE_CRC_OPTIMIZATION): New timevar.
    * tree-pass.h (make_pass_crc_optimization): New extern function declaration.

Signed-off-by: Mariam Arutunian <mariamarutun...@gmail.com <mailto:mariamarutun...@gmail.com>>
Mentored-by: Jeff Law <j...@ventanamicro.com <mailto:j...@ventanamicro.com>>
Minor NIT, looks like you added extraenous nwline at the end of enable_fdo_optimizations.

+/* Checks whether the CLMUL (Carry-less Multiplication) instruction
+   is supported by evaluating specific target architecture flags.  */
+static bool
+is_CLMUL_supported ()
+{
+  #ifdef TARGET_ZBC
+    if (TARGET_ZBC)
+      return true;
+  #endif
+  #ifdef TARGET_ZBKC
+     if (TARGET_ZBKC)
+       return true;
+  #endif
+  #ifdef TARGET_AES
+    if (TARGET_AES)
+      return true;
+  #endif
+  #ifdef TARGET_PCLMUL
+    if (TARGET_PCLMUL)
+      return true;
+  #endif
+    return false;
+}
[ ... ]

+       /* If optimize_size is specified, execute the pass only if CLMUL or CRC
+ instruction is supported. */ + if (is_CLMUL_supported ())
+         return true;
+ #ifdef TARGET_CRC32 + if (TARGET_CRC32) + return true;
+       #endif
Can't do that in a target independent file. If you find yourself writing TARGET_* in a target independent file, then that's a sign we need to make some adjustments.

I think to do what you're trying to do here we'd really need to make crc and clmul first class citizens with an optab that we could query.

So my suggestion is to drop this code for now and open a bug report to track the desire to make optabs for clmul/crc32 and once implemented use the optabs-query interface to allow generation of clmul/crc32 based sequences when optimize_size is on. In effect that makes this a follow-up item rather than a prerequisite for integration.

I saw a couple things in the early checks for a possible CRC loop that could be better, but it's really just meant to be a reasonable first pass filter, so I don't think they were worth calling out.


+  /* In CRC implementations with constant polynomial maximum 12 use_def
+     statements may occur.
+     TODO: Find a better solution.  */
+  if (use_defs.length () > 12)
+    return false;
Presumably you're just trying to avoid useless work here? It might be useful to indicate where the "12" comes from as a comment.


So basically OK. You need to avoid the TARGET_* checks and given we don't have an interface to check for crc or clmul instruction availability, I suggest we defer that problem.

Jeff


Reply via email to