Bug 91293 - [8 Regression] Wrong code with -O3 -mavx2
Summary: [8 Regression] Wrong code with -O3 -mavx2
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 10.0
: P2 normal
Target Milestone: 8.4
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks: yarpgen
  Show dependency treegraph
 
Reported: 2019-07-30 00:45 UTC by Vsevolod Livinskii
Modified: 2021-11-01 23:07 UTC (History)
3 users (show)

See Also:
Host:
Target: x86_64-*-*, i?86-*-*
Build:
Known to work: 10.0, 7.4.0, 8.3.1, 9.1.1
Known to fail: 8.1.0, 8.3.0, 9.1.0
Last reconfirmed: 2019-07-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vsevolod Livinskii 2019-07-30 00:45:05 UTC
GCC produces wrong code with -O3 option.

Reproducer:
#include <stdio.h>

long long a;
unsigned b, c;
int d = 62;

void e(long long *f, int p2) { *f = p2; }
int main() {
  for (int g = 2; g <= d; g++) {
    c += 5 - g;
    b += g + 4;
  }
  e(&a, b);
  printf("%llu\n", a);
}

Error:
>$ gcc -march=skylake-avx512 -O3 small.c ; ./a.out
18446744073709550508
>$ gcc -march=skylake-avx512 -O0 small.c ; ./a.out
2196

GCC version: 10.0.0 (rev. 273839)
Comment 1 Richard Biener 2019-07-30 07:05:17 UTC
Confirmed with -O3 -mavx2.  GCC 7 seems fine.  Disabling vectorization fixes it.
Comment 2 Jakub Jelinek 2019-07-30 11:24:31 UTC
Started with r257581.
Comment 3 Richard Biener 2019-07-30 12:08:40 UTC
Mine then.
Comment 4 Richard Biener 2019-07-31 12:37:25 UTC
Equivalent testcase that doesn't run into the operator swapping and thus works:

long long a;
unsigned b, c;
int d = 62;
void e(long long *f, int p2) { *f = p2; }
int xx = 5, yy = 4;
int main()
{
  for (int g = 2; g <= d; g++)
    {
      c += xx - g;
      b += yy + g;
    }
  e(&a, b);
  if (a != 2196)
    __builtin_abort ();
  return 0;
}

swapping operands confuses reduction code-generation.  It would probably
be "easiest" to not swap operands in this case. This results in no longer vectorizing this in an optimal way but is probably the way to go for release branches.
Comment 5 Richard Biener 2019-07-31 14:15:42 UTC
Fixed on trunk sofar.
Comment 6 Richard Biener 2019-07-31 14:16:09 UTC
Author: rguenth
Date: Wed Jul 31 14:15:37 2019
New Revision: 273934

URL: https://gcc.gnu.org/viewcvs?rev=273934&root=gcc&view=rev
Log:
2019-07-31  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91293
	* tree-vect-slp.c (vect_build_slp_tree_2): Do not swap operands
	of reduction stmts.

	* gcc.dg/vect/pr91293-1.c: New testcase.
	* gcc.dg/vect/pr91293-2.c: Likewise.
	* gcc.dg/vect/pr91293-3.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.dg/vect/pr91293-1.c
    trunk/gcc/testsuite/gcc.dg/vect/pr91293-2.c
    trunk/gcc/testsuite/gcc.dg/vect/pr91293-3.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vect-slp.c
Comment 7 Richard Biener 2019-08-02 12:08:05 UTC
Author: rguenth
Date: Fri Aug  2 12:07:33 2019
New Revision: 274007

URL: https://gcc.gnu.org/viewcvs?rev=274007&root=gcc&view=rev
Log:
2019-08-02  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2019-07-31  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91280
	* tree-ssa-structalias.c (get_constraint_for_component_ref):
	Decompose MEM_REF manually for offset handling.

	* g++.dg/torture/pr91280.C: New testcase.

	2019-07-31  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91293
	* tree-vect-slp.c (vect_build_slp_tree_2): Do not swap operands
	of reduction stmts.

	* gcc.dg/vect/pr91293-1.c: New testcase.
	* gcc.dg/vect/pr91293-2.c: Likewise.
	* gcc.dg/vect/pr91293-3.c: Likewise.

Added:
    branches/gcc-9-branch/gcc/testsuite/g++.dg/torture/pr91280.C
    branches/gcc-9-branch/gcc/testsuite/gcc.dg/vect/pr91293-1.c
    branches/gcc-9-branch/gcc/testsuite/gcc.dg/vect/pr91293-2.c
    branches/gcc-9-branch/gcc/testsuite/gcc.dg/vect/pr91293-3.c
Modified:
    branches/gcc-9-branch/gcc/ChangeLog
    branches/gcc-9-branch/gcc/testsuite/ChangeLog
    branches/gcc-9-branch/gcc/tree-ssa-structalias.c
    branches/gcc-9-branch/gcc/tree-vect-slp.c
Comment 8 Richard Biener 2019-08-30 11:39:50 UTC
Author: rguenth
Date: Fri Aug 30 11:39:19 2019
New Revision: 275100

URL: https://gcc.gnu.org/viewcvs?rev=275100&root=gcc&view=rev
Log:
2019-08-30  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2019-08-12  Richard Biener  <rguenther@suse.de>

	PR lto/91375
	* tree.c (free_lang_data_in_type): Do not free TYPE_BINFO dependent on
	flag_devirtualize.

	2019-07-31  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91293
	* tree-vect-slp.c (vect_build_slp_tree_2): Do not swap operands
	of reduction stmts.

	* gcc.dg/vect/pr91293-1.c: New testcase.
	* gcc.dg/vect/pr91293-2.c: Likewise.
	* gcc.dg/vect/pr91293-3.c: Likewise.

	2019-07-31  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91280
	* tree-ssa-structalias.c (get_constraint_for_component_ref):
	Decompose MEM_REF manually for offset handling.

	* g++.dg/torture/pr91280.C: New testcase.

	2019-07-19  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91200
	* tree-ssa-phiopt.c (cond_store_replacement): Check we have
	no PHI nodes in middle-bb.

	* gcc.dg/torture/pr91200.c: New testcase.

	2019-07-15  Richard Biener  <rguenther@suse.de>

	PR middle-end/91162
	* tree-cfg.c (move_block_to_fn): When releasing a virtual PHI
	node make sure to replace all uses with something valid.

	* gcc.dg/autopar/pr91162.c: New testcase.

	2019-07-11  Richard Biener  <rguenther@suse.de>

	PR middle-end/91131
	* gimplify.c (gimplify_compound_literal_expr): Force a temporary
	when the object is volatile and we have not cleared it even though
	there are no nonzero elements.

	* gcc.target/i386/pr91131.c: New testcase.

	2019-07-10  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/91126
	* tree-ssa-sccvn.c (vn_reference_lookup_3): Adjust
	native encoding offset for BYTES_BIG_ENDIAN.

	* gcc.dg/torture/pr91126.c: New testcase.

Added:
    branches/gcc-8-branch/gcc/testsuite/g++.dg/torture/pr91280.C
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/autopar/pr91162.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr91126.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/torture/pr91200.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/vect/pr91293-1.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/vect/pr91293-2.c
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/vect/pr91293-3.c
    branches/gcc-8-branch/gcc/testsuite/gcc.target/i386/pr91131.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/gimplify.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
    branches/gcc-8-branch/gcc/tree-cfg.c
    branches/gcc-8-branch/gcc/tree-ssa-phiopt.c
    branches/gcc-8-branch/gcc/tree-ssa-sccvn.c
    branches/gcc-8-branch/gcc/tree-ssa-structalias.c
    branches/gcc-8-branch/gcc/tree-vect-slp.c
    branches/gcc-8-branch/gcc/tree.c
Comment 9 Richard Biener 2019-08-30 11:49:41 UTC
Fixed.