Bug 60276 - [4.7 Regression] -O3 autovectorizer breaks on a particular loop
Summary: [4.7 Regression] -O3 autovectorizer breaks on a particular loop
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: 4.8.3
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-02-19 19:53 UTC by Hans-Kristian Arntzen
Modified: 2024-05-13 12:55 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 4.8.3, 4.9.0
Known to fail: 4.7.3, 4.7.4, 4.8.2
Last reconfirmed: 2014-02-20 00:00:00


Attachments
Reduced testcase (384 bytes, text/x-c)
2014-02-19 19:53 UTC, Hans-Kristian Arntzen
Details
patch (1.81 KB, patch)
2014-02-20 15:13 UTC, Richard Biener
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Hans-Kristian Arntzen 2014-02-19 19:53:31 UTC
Created attachment 32174 [details]
Reduced testcase

GCC 4.8.2 with -O3 breaks a certain loop I'm compiling. Disabling the vectorizer with -fno-tree-vectorize fixes the issue, as well as compiling with -O2.

I've reduced the problem to a small test case which is attached as legall.c.

Distro: Arch Linux x86_64
gcc -v: 
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-4.8-20140206/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --disable-cloog-version-check --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 4.8.2 20140206 (prerelease) (GCC)

Miscompile: gcc -o legall legall.c -std=gnu99 -O3
Working: gcc -o legall legall.c -std=gnu99 -O3 -fno-tree-vectorize

Expected output:
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17) (18) (19) (20) (21) (22) (23) (24) (25) (26) (27) (28) (29) (30) (31) (32) (33) (34) (35) (36) (37) (38) (39) (40) (41) (42) (43) (44) (45) (46) (47) (48)

Wrong output by -O3:
(0) (1) (2) (2) (4) (3) (6) (4) (8) (5) (10) (6) (12) (7) (14) (8) (16) (17) (18) (10) (20) (11) (22) (12) (24) (13) (26) (14) (28) (15) (30) (16) (32) (33) (34) (35) (36) (37) (38) (39) (40) (41) (42) (43) (44) (45) (46) (47) (48)
Comment 1 Richard Biener 2014-02-20 10:24:25 UTC
Works with GCC 4.9, still fails on the 4.8 branch and the 4.7 branch, works with 4.3 so a regression.

Curious what fixed it on trunk.
Comment 2 Marek Polacek 2014-02-20 11:34:34 UTC
It fails for me with trunk: r207224 works, but starting with r207225 it fails again.
Comment 3 Richard Biener 2014-02-20 11:53:49 UTC
indeed.  I will have a look.
Comment 4 Richard Biener 2014-02-20 12:00:55 UTC
extern void abort (void);

static void
foo (short *out, const short *lp, const short *hp, unsigned samples)
{
  int x, target;
  for (x = 0, target = 0; x < (int)samples; x += 2, target++)
    {
      out[x + 0] = lp[target] - ((hp[target] + hp[target - 1] + 2) >> 2);
      out[x - 1] = hp[target - 1] + ((out[x - 2] + out[x]) >> 1);
    }
}

int main(void)
{
  const short lp[25] = {
      0, 2, 4, 6, 8,
      10, 12, 14, 16,
      18, 20, 22, 24,
      26, 28, 30, 32,
      34, 36, 38, 40,
      42, 44, 46, 48,
  };
  const short hp[25] = {0};
  short out[49] = {0};
  int s;

  out[0] = lp[0] - (((hp[0] << 1) + 2) >> 2);
  foo (out + 2, lp + 1, hp + 1, 48);

  for (s = 0; s < 49; s++)
    if (out[s] != s)
      abort ();

  return 0;
}
Comment 5 Richard Biener 2014-02-20 12:09:51 UTC
And with less convoluted vectorized code but still broken (avoids integer promotions):

extern void abort (void);

static void
foo (int *out, const int *lp, const int *hp, unsigned samples)
{
  int x, target;
  for (x = 0, target = 0; x < (int)samples; x += 2, target++)
    {
      out[x + 0] = lp[target] - ((hp[target] + hp[target - 1] + 2) >> 2);
      out[x - 1] = hp[target - 1] + ((out[x - 2] + out[x]) >> 1);
    }
}

int main(void)
{
  const int lp[25] = {
      0, 2, 4, 6, 8,
      10, 12, 14, 16,
      18, 20, 22, 24,
      26, 28, 30, 32,
      34, 36, 38, 40,
      42, 44, 46, 48,
  };
  const int hp[25] = {0};
  int out[49] = {0};
  int s;

  out[0] = lp[0] - (((hp[0] << 1) + 2) >> 2);
  foo (out + 2, lp + 1, hp + 1, 48);

  for (s = 0; s < 49; s++)
    if (out[s] != s)
      abort ();

  return 0;
}
Comment 6 Richard Biener 2014-02-20 12:30:24 UTC
But you can clearly see that this isn't valid vectorization because of the
read-after-write dependence of out[x].
Comment 7 Richard Biener 2014-02-20 12:56:54 UTC
(In reply to Richard Biener from comment #6)
> But you can clearly see that this isn't valid vectorization because of the
> read-after-write dependence of out[x].

Well, only after unrolling which happens here - then the introduced dependence
(we CSE the in-source one) becomes explicit:

      tem0 = lp[target] - ((hp[target] + hp[target - 1] + 2) >> 2);
      out[x] = tem0;
      out[x - 1] = hp[target - 1] + ((out[x - 2] + tem0) >> 1);
      tem2 = lp[target + 1] - ((hp[target + 1] + hp[target] + 2) >> 2);
      out[x + 2] = tem2;
      out[x + 1] = hp[target] + ((out[x] + tem2) >> 1);

thus

static bool
vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
                                  loop_vec_info loop_vinfo, int *max_vf)
{
...
      if (dist > 0 && DDR_REVERSED_P (ddr))
        {
          /* If DDR_REVERSED_P the order of the data-refs in DDR was
             reversed (to make distance vector positive), and the actual
             distance is negative.  */
          if (dump_enabled_p ())
            dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
                             "dependence distance negative.\n");
          continue;
        }

becomes invalid once we unroll if the distance is less than the
vectorization factor.  Unless we apply the same CSE during vectorization
transform.
Comment 8 Marek Polacek 2014-02-20 13:03:07 UTC
Note that in r196871 this was broken, but r196872 is fine.
Comment 9 Richard Biener 2014-02-20 15:13:05 UTC
Created attachment 32183 [details]
patch

Patch I am testing.
Comment 10 Richard Biener 2014-02-21 13:19:26 UTC
Author: rguenth
Date: Fri Feb 21 13:18:54 2014
New Revision: 207992

URL: http://gcc.gnu.org/viewcvs?rev=207992&root=gcc&view=rev
Log:
2014-02-21  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/60276
	* tree-vectorizer.h (struct _stmt_vec_info): Add min_neg_dist field.
	(STMT_VINFO_MIN_NEG_DIST): New macro.
	* tree-vect-data-refs.c (vect_analyze_data_ref_dependence): Record
	STMT_VINFO_MIN_NEG_DIST.
	* tree-vect-stmts.c (vectorizable_load): Verify if assumptions
	made for negative dependence distances still hold.

	* gcc.dg/vect/pr60276.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/vect/pr60276.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vect-data-refs.c
    trunk/gcc/tree-vect-stmts.c
    trunk/gcc/tree-vectorizer.h
Comment 11 Richard Biener 2014-02-21 13:19:42 UTC
Fixed on trunk sofar.
Comment 12 Richard Biener 2014-02-25 12:43:31 UTC
Author: rguenth
Date: Tue Feb 25 12:42:59 2014
New Revision: 208119

URL: http://gcc.gnu.org/viewcvs?rev=208119&root=gcc&view=rev
Log:
2014-02-25  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2014-02-21  Richard Biener  <rguenther@suse.de>

        PR tree-optimization/60276
        * tree-vectorizer.h (struct _stmt_vec_info): Add min_neg_dist field.
        (STMT_VINFO_MIN_NEG_DIST): New macro.
        * tree-vect-data-refs.c (vect_analyze_data_ref_dependence): Record
        STMT_VINFO_MIN_NEG_DIST.
        * tree-vect-stmts.c (vectorizable_load): Verify if assumptions
        made for negative dependence distances still hold.

        * gcc.dg/vect/pr60276.c: New testcase.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr60276.c
Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_8-branch/gcc/tree-vect-data-refs.c
    branches/gcc-4_8-branch/gcc/tree-vect-stmts.c
    branches/gcc-4_8-branch/gcc/tree-vectorizer.h
Comment 13 Jakub Jelinek 2014-03-06 08:24:07 UTC
Author: jakub
Date: Thu Mar  6 08:23:36 2014
New Revision: 208373

URL: http://gcc.gnu.org/viewcvs?rev=208373&root=gcc&view=rev
Log:
	PR tree-optimization/60276
	* tree-vect-data-refs.c (vect_analyze_data_ref_dependence): Avoid
	a -Wsign-compare warning.

Modified:
    branches/gcc-4_8-branch/gcc/ChangeLog
    branches/gcc-4_8-branch/gcc/tree-vect-data-refs.c
Comment 14 Richard Biener 2014-06-12 13:35:50 UTC
Fixed for 4.8.3.
Comment 15 Richard Biener 2014-09-03 09:02:20 UTC
*** Bug 63148 has been marked as a duplicate of this bug. ***
Comment 16 GCC Commits 2024-05-13 12:55:35 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:8a9b159a86081053289be0c44339623ff59717a2

commit r15-431-g8a9b159a86081053289be0c44339623ff59717a2
Author: Richard Biener <rguenther@suse.de>
Date:   Thu Nov 9 11:30:22 2023 +0100

    PR60276 fix for single-lane SLP
    
    When enabling single-lane SLP and not splitting groups the fix for
    PR60276 is no longer effective since it for unknown reason exempted
    pure SLP.  The following removes this exemption, making
    gcc.dg/vect/pr60276.c PASS even with --param vect-single-lane-slp=1
    
            PR tree-optimization/60276
            * tree-vect-stmts.cc (vectorizable_load): Do not exempt
            pure_slp grouped loads from the STMT_VINFO_MIN_NEG_DIST
            restriction.