Bug 18400 - wrong unrolling after vectorization due to invalid loop->nb_iterations
Summary: wrong unrolling after vectorization due to invalid loop->nb_iterations
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: 4.0.0
Assignee: Dorit Naishlos
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2004-11-09 14:50 UTC by Dorit Naishlos
Modified: 2004-11-15 04:14 UTC (History)
1 user (show)

See Also:
Host: powerpc-apple-darwin7.0.0
Target: powerpc-apple-darwin7.0.0
Build: powerpc-apple-darwin7.0.0
Known to work:
Known to fail:
Last reconfirmed: 2004-11-09 14:56:05


Attachments
testcase (360 bytes, text/plain)
2004-11-09 14:54 UTC, Dorit Naishlos
Details
patch. (363 bytes, patch)
2004-11-09 14:59 UTC, Dorit Naishlos
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dorit Naishlos 2004-11-09 14:50:10 UTC
When a loop like the following:

=============================
#define N 8
int main ()
{
  int b[N] = {0,3,6,9,12,15,18,21};
  int a[N];
  int i;
  for (i = 0; i < N; i++){
      a[i] = b[i];
  }
  /* check results:  */
  for (i = 0; i < N; i++){
      if (a[i] != b[i])
        abort ();
  }
  return 0;
}
=============================

is compiled with:
-O2 -ftree-vectorize -maltivec -funroll-loops 

it gets vectorized and then completely unrolled. The complete unroller looks at 
loop->nb_iterations to get the number of iterations; the problem is that loop-
>nb_iterations is not always updated after vectorization, so the loop ends up 
being unrolled by 8 instead of by 2, and we get an execution failure.

The following patch will fix the problem:

Index: tree-vectorizer.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-vectorizer.c,v
retrieving revision 2.25
diff -c -3 -p -r2.25 tree-vectorizer.c
*** tree-vectorizer.c   8 Nov 2004 13:54:41 -0000       2.25
--- tree-vectorizer.c   9 Nov 2004 13:16:54 -0000
*************** make_loop_iterate_ntimes (struct loop *l
*** 635,640 ****
--- 635,642 ----

    if (vect_debug_stats (loop) || vect_debug_details (loop))
      print_generic_expr (dump_file, cond_stmt, TDF_SLIM);
+   
+   loop->nb_iterations = niters;
  }

  
*************** vect_transform_loop_bound (loop_vec_info
*** 2881,2886 ****
--- 2883,2890 ----

    if (vect_debug_details (NULL))
      print_generic_expr (dump_file, cond_stmt, TDF_SLIM);
+   
+   loop->nb_iterations = new_loop_bound;
  }
Comment 1 Dorit Naishlos 2004-11-09 14:54:41 UTC
Created attachment 7502 [details]
testcase
Comment 2 Andrew Pinski 2004-11-09 14:56:04 UTC
Confirmed.
Comment 3 Dorit Naishlos 2004-11-09 14:59:48 UTC
Created attachment 7503 [details]
patch.

	* tree-vectorizer.c (make_loop_iterate_ntimes): Set
loop->nb_iterations.
	(vect_transform_loop_bound): Set loop->nb_iterations.
Comment 4 GCC Commits 2004-11-14 18:30:42 UTC
Subject: Bug 18400

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	dorit@gcc.gnu.org	2004-11-14 18:30:36

Modified files:
	gcc            : ChangeLog tree-vectorizer.c 

Log message:
	PR tree-opt/18400
	* tree-vectorizer.c (make_loop_iterate_ntimes): Set loop->nb_iterations.
	(vect_transform_loop_bound): Set loop->nb_iterations.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.6334&r2=2.6335
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-vectorizer.c.diff?cvsroot=gcc&r1=2.29&r2=2.30

Comment 5 Andrew Pinski 2004-11-14 18:33:29 UTC
Fixed.
Comment 6 GCC Commits 2004-11-14 20:45:04 UTC
Subject: Bug 18400

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	dorit@gcc.gnu.org	2004-11-14 20:44:52

Modified files:
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg/vect: pr18400.c pr18425.c 

Log message:
	2004-11-14  Dorit Naishlos  <dorit@il.ibm.com>
	
	PR tree-opt/18400
	* gcc.dg/vect/pr18400.c: New test.
	
	2004-11-14  Dorit Naishlos  <dorit@il.ibm.com>
	Andrew Pinski  <pinskia@physics.uc.edu>
	
	PR tree-opt/18425
	* gcc.dg/vect/pr18425.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4599&r2=1.4600
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/vect/pr18400.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/vect/pr18425.c.diff?cvsroot=gcc&r1=NONE&r2=1.1