Bug 93868

Summary: [10 Regression] wrong-code with permuted SLP reduction
Product: gcc Reporter: Richard Biener <rguenth>
Component: tree-optimizationAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: normal Keywords: wrong-code
Priority: P1    
Version: 10.0   
Target Milestone: 10.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2020-02-21 00:00:00
Bug Depends on:    
Bug Blocks: 53947    

Description Richard Biener 2020-02-21 11:23:06 UTC
The SLP graph representation made vect_attempt_slp_rearrange_stmts unsafe since
it fails to verify the permutation it applies only affects the SLP reduction.

unsigned a[1024];
unsigned b[1024];

void __attribute__((noipa))
foo (unsigned *q, unsigned *r)
{
  unsigned sum1 = 0, sum2 = 0;
  for (int i = 0; i < 512; ++i)
    {
      sum1 += a[2*i];
      sum2 += a[2*i+1];
      b[2*i] = a[2*i+1];
      b[2*i+1] = a[2*i];
    }
  *q = sum1;
  *r = sum2;
}

int main()
{
  unsigned sum1, sum2;
  a[0] = 0;
  a[1] = 1;
  foo (&sum1, &sum2);
  if (b[0] != 1 || b[1] != 0)
    __builtin_abort ();
  return 0;
}
Comment 1 Richard Biener 2020-02-21 11:24:29 UTC
Mine.
Comment 2 Martin Liška 2020-02-21 11:25:47 UTC
Just for the record, started with r10-4800-g10a73df76280e128.
Comment 3 Richard Biener 2020-02-21 12:26:27 UTC
What's more we cannot modify the SLP nodes stmt order since they are cached in
the SLP node cache.  So a "simple" fix might be to COW the whole SLP sub-tree
we re-arrange...
Comment 4 GCC Commits 2020-02-25 09:35:26 UTC
The master branch has been updated by Richard Guenther <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:81c833b311b16cfd87a947374d5ffbbd48facd03

commit r10-6838-g81c833b311b16cfd87a947374d5ffbbd48facd03
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Feb 25 10:31:16 2020 +0100

    tree-optimization/93868 copy SLP tree before re-arranging stmts
    
    This avoids altering possibly shared SLP subtrees when attempting
    to get rid of permutations in SLP reductions by copying the SLP
    subtree before re-arranging stmts in it.
    
    2020-02-25  Richard Biener  <rguenther@suse.de>
    
    	PR tree-optimization/93868
    	* tree-vect-slp.c (slp_copy_subtree): New function.
    	(vect_attempt_slp_rearrange_stmts): Copy the SLP tree before
    	re-arranging stmts in it.
    
    	* gcc.dg/torture/pr93868.c: New testcase.
Comment 5 Richard Biener 2020-02-25 09:41:51 UTC
Fixed.