public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tnfchris at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/109156] New: Support Absolute Difference detection in GCC
Date: Thu, 16 Mar 2023 13:09:33 +0000	[thread overview]
Message-ID: <bug-109156-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109156

            Bug ID: 109156
           Summary: Support Absolute Difference detection in GCC
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tnfchris at gcc dot gnu.org
  Target Milestone: ---
            Target: aarch64*

Today we support Sum of Absolute differences

#include <stdlib.h>

#define TYPE_IN signed char
#define TYPE_OUT signed int

TYPE_OUT SABD_example (int n, TYPE_IN *restrict a, TYPE_IN *restrict b)
{
  TYPE_OUT out = 0;
  for (int i = 0; i < n; i++)
    out += abs(b[i] - a[i]);
  return out;
}

which is implemented through the SAD_EXPR tree code.

The goal is to support absolute difference ABD and widening absolute difference
(no reduction).

#include <stdlib.h>

#define TYPE_IN signed int
#define TYPE_OUT signed int

void ABD_example (int n, TYPE_IN *restrict a, TYPE_IN *restrict b, TYPE_OUT
*restrict out)
{
  for (int i = 0; i < n; i++)
    out[i] = abs(b[i] - a[i]);
}


This code shares 90% of the work with the vect_recog_sad_pattern expression
with one difference, the SAD expression starts at a reduction, the ADB
expressions start at an optional cast but otherwise from the abs.

There are two ways we're thinking of implementing ABD, (ABDL we can't do at the
moment because we can't do widening in IFNs).

1. refactor the SAD detection code such that the body of the code that detects
ABD is refactored out and can be used by a new pattern.  This has the down side
of us having to do duplicate work in patterns that may match this.  In general
this doesn't happen often because SAD stops at the + and should be OK if ADB
matches after SAD. though cast_forwardprop may make this hard to do.

2. It looks like all targets that implement SAD do so with an instruction that
does ABD and then perform a reduction.  So it looks like no target has the
semantics for SAD.

So this brings up the question of why the detection wasn't done based on ABD
instead and leaving the reduction explicit in the vectorizer.

So question is, should we create a completely new standalone pattern for ABD or
should be make ABD the thing being detected and change SAD_EXPR to recognize
ADB + reduction.

Removing SAD completely in favor of ABD + reduction means that hand optimized
versions in targets need updating so I'm in favor of still emitting SAD.

             reply	other threads:[~2023-03-16 13:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-16 13:09 tnfchris at gcc dot gnu.org [this message]
2023-03-16 13:25 ` [Bug tree-optimization/109156] " rguenth at gcc dot gnu.org
2023-03-16 13:41 ` tnfchris at gcc dot gnu.org
2023-03-16 13:49 ` tnfchris at gcc dot gnu.org
2023-03-16 16:22 ` pinskia at gcc dot gnu.org
2023-06-15  6:37 ` cvs-commit at gcc dot gnu.org
2023-07-14 11:02 ` tnfchris at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109156-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).