public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c/2462: "restrict" implementation bug
@ 2003-03-25  2:07 Dan Nicolaescu
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Nicolaescu @ 2003-03-25  2:07 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/2462; it has been noted by GNATS.

From: Dan Nicolaescu <dann@ics.uci.edu>
To: bangerth@dealii.org
Cc: gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org,
   gcc-gnats@gcc.gnu.org
Subject: Re: c/2462: "restrict" implementation bug
Date: Mon, 24 Mar 2003 17:53:26 -0800

 bangerth@dealii.org writes:
 
   > Synopsis: "restrict" implementation bug
   > 
   > State-Changed-From-To: open->feedback
   > State-Changed-By: bangerth
   > State-Changed-When: Tue Mar 25 01:28:38 2003
   > State-Changed-Why:
   >     Dan, I don't have this platform, so have to ask: this report
   >     is now almost 2 years old, can you say anything about whether
   >     the problem still persists with present versions of gcc?
 
 Yes it does, CVS gcc still has the same problem on SPARC. 
 
 
 
   >     Thanks
   >       Wolfgang
   > 
   > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=2462


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: c/2462: "restrict" implementation bug
@ 2003-05-03 23:26 Dan Nicolaescu
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Nicolaescu @ 2003-05-03 23:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/2462; it has been noted by GNATS.

From: Dan Nicolaescu <dann@ics.uci.edu>
To: bangerth@dealii.org
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c/2462: "restrict" implementation bug
Date: Sat, 03 May 2003 16:16:52 -0700

 With the tweaks below the code in this PR can be added to the
 GCC testsuite in case somebody wants to do that.
 
 /* { dg-do link } */
 
 #include <stdlib.h>
 
 int *  __restrict__ d;
 int *  __restrict__ g;
 int *  __restrict__ h;
 
 struct two_intp {
   int *first;
   int *second;
 };
 
 extern void link_error(void);
 
 /* a malloced pointer, local restricted vars and a global restricted var */
 int*
 foo (int *a, int *b, int *c, int n)
 {
   int i;
   int *f;
   f = (int*) malloc (n * sizeof (int));
 
 
   {
     int * __restrict__ p = a;
     int * __restrict__ q = b;
     int * __restrict__ r = c;
    
     for (i = 0;i < n; i++)
       {
         p[i] = 1;
         q[i] = 1;
         r[i] = 1;
         f[i] = p[i] + q[i];
 
         if (f[i] != 2)
           link_error ();
 
         d[i] = p[i] + r[i];
 
         if (d[i] != 2)
           link_error ();
 
         f[i] += p[i] + q[i];
 
         if (f[i] != 4)
           link_error ();
       }
 
   }
   return f;
 }
 
 
 /* a malloced pointer and a restricted global var */
 int*
 bar (int *  __restrict__ b,
      int *  __restrict__ c, int n)
 {
   int i;
 
   int * f = (int*) malloc (n * sizeof (int));
  
   for (i = 0;i < n; i++)
     {
       b[i] = 1;
       c[i] = 1;
       
       f[i] = b[i] + c[i];
 
       if (f[i] != 2)
         link_error ();
 
       if ((b[i] != 1) || (c[i] != 1))
           link_error ();
       
       d[i] = b[i] + c[i];
       
       if (d[i] != 2)
         link_error ();
       
       f[i] += b[i] + c[i];
 
       if (f[i] != 4)
         link_error ();
       
     }
   return f;
 }
 
 
 /* 2 malloced pointers */
 struct two_intp
 foobar (int *  __restrict__ b,
         int *  __restrict__ c, int n)
 {
   int i;
 
   struct two_intp retval;
 
   int * f = (int*) malloc (n * sizeof (int));
   int * ff = (int*) malloc (n * sizeof (int));
  
   for (i = 0;i < n; i++)
     {
       b[i] = 1;
       c[i] = 1;
 
       f[i] = b[i] + c[i];
 
       if (f[i] != 2)
         link_error ();
       
       ff[i] = b[i] + c[i];
 
       if (ff[i] != 2)
         link_error ();
 
       f[i] += b[i] + c[i];
 
       if (f[i] != 4)
         link_error ();
 
     }
   retval.first = f;
   retval.second = ff;
   return retval;
 }
 
 /* 2 restricted global vars */
 int*
 baz (int *  __restrict__ b,
      int *  __restrict__ c, int n)
 {
   int i;
   for (i = 0;i < n; i++)
     {
       b[i] = 1;
       c[i] = 1;
       g[i] = b[i] + c[i];
       
       if (g[i] != 2)
         link_error ();
       
       d[i] = b[i] + c[i];
 
       if (d[i] != 2)
         link_error ();
 
       g[i] += b[i] + c[i];
 
       if (g[i] != 4)
         link_error ();
     }
   return g;
 }
 
 int main (void)
 {
 
   int *a,  *b,  *c, *f, n;
   struct two_intp twoints;
   f = foo (a, b, c, n);
   f = bar (b, c, n);
   twoints = foobar (b, c, n);
   f = baz (b, c, n);
   return 0;
 }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: c/2462: "restrict" implementation bug
@ 2003-03-25  3:16 bangerth
  0 siblings, 0 replies; 5+ messages in thread
From: bangerth @ 2003-03-25  3:16 UTC (permalink / raw)
  To: dann, gcc-bugs, gcc-prs, nobody

Synopsis: "restrict" implementation bug

State-Changed-From-To: feedback->analyzed
State-Changed-By: bangerth
State-Changed-When: Tue Mar 25 02:35:35 2003
State-Changed-Why:
    Still a problem. Can at least be moved into "analyzed" state
    this way.
    
    Thanks for the quick feedback! W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=2462


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: c/2462: "restrict" implementation bug
@ 2003-03-25  1:39 bangerth
  0 siblings, 0 replies; 5+ messages in thread
From: bangerth @ 2003-03-25  1:39 UTC (permalink / raw)
  To: dann, gcc-bugs, gcc-prs, nobody

Synopsis: "restrict" implementation bug

State-Changed-From-To: open->feedback
State-Changed-By: bangerth
State-Changed-When: Tue Mar 25 01:28:38 2003
State-Changed-Why:
    Dan, I don't have this platform, so have to ask: this report
    is now almost 2 years old, can you say anything about whether
    the problem still persists with present versions of gcc?
    
    Thanks
      Wolfgang

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=2462


^ permalink raw reply	[flat|nested] 5+ messages in thread

* c/2462: "restrict" implementation bug
@ 2001-04-02 14:36 Dan Nicolaescu
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Nicolaescu @ 2001-04-02 14:36 UTC (permalink / raw)
  To: gcc-gnats

>Number:         2462
>Category:       c
>Synopsis:       "restrict" implementation bug
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Mon Apr 02 14:36:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Dan Nicolaescu <dann@godzilla.ics.uci.edu>
>Release:        gcc version 3.1 20010402 (experimental)
>Organization:
>Environment:
sparc-sun-solaris2.7
But the problem is arhitecture independent.
The problem is also present in gcc-2.95.2
>Description:
According to http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/n897.pdf  
(pointed to by a link from http://gcc.gnu.org/readings.html ) Chapter 
6.7.3.1 paragraph 9  global variables with a restrict qualifier should
act "as if it were declared as an array". 
An in Chapter 6.7.3.1 paragraph 4: a pointer returned from a call to
"malloc" is the initial single mean to access an array. 
The conclusion from these 2 is that a pointer from malloc cannot alias
a global restricted var. 

It seems that there is a bug when using both a restricted global var
and a pointer obtained from "malloc" call. 
As shown in the example bellow when using either of them individualy
"restrict" works correctly. 

Here is an example:

float *  __restrict__ d;
float *  __restrict__ g;
float *  __restrict__ h;

struct two_floatp {
  float *first;
  float *second;
};

/* a malloced pointer, local restricted vars and a global restricted var */
float* 
foo (float * a, float * b, float * c, int n)
{
  int i;
  float *f;
  f = (float*) malloc (n * sizeof (float));


  {
    float * __restrict__ p = a;
    float * __restrict__ q = b;
    float * __restrict__ r = c;
    
    for (i = 0;i < n; i++)
      {
        f[i] = p[i] + q[i];
        d[i] = p[i] + r[i]; 
        f[i] += p[i] + q[i];
      }

  }
  return f;
}


/* a malloced pointer and a restricted global var */
float* 
bar (float *  __restrict__ b,
     float *  __restrict__ c, int n)
{
  int i;

  float * f = (float*) malloc (n * sizeof (float));
  
  for (i = 0;i < n; i++)
    {
      f[i] = b[i] + c[i];
      d[i] = b[i] + c[i]; 
      f[i] += b[i] + c[i];
    }
  return f;
}


/* 2 malloced pointers */
struct two_floatp
foobar (float *  __restrict__ b,
        float *  __restrict__ c, int n)
{
  int i;

  struct two_floatp retval;

  float * f = (float*) malloc (n * sizeof (float));
  float * ff = (float*) malloc (n * sizeof (float));
  
  for (i = 0;i < n; i++)
    {
      f[i] = b[i] + c[i];
      ff[i] = b[i] + c[i]; 
      f[i] += b[i] + c[i];
    }
  retval.first = f;
  retval.second = ff;
  return retval;
}

/* 2 restricted global vars */
float* 
baz (float *  __restrict__ b,
     float *  __restrict__ c, int n)
{
  int i;
  for (i = 0;i < n; i++)
    {
      g[i] = b[i] + c[i];
      d[i] = b[i] + c[i]; 
      g[i] += b[i] + c[i];
    }
  return g;
}

Following is the SPARC assembly just for the loops from all the functions.
	

foo:
[snip]	
.LL5:
	sll	%o2, 2, %o0
	ld	[%i0+%o0], %f3
	add	%o2, 1, %o2
	ld	[%i1+%o0], %f4
	cmp	%o2, %i3
	fadds	%f3, %f4, %f4
	ld	[%i2+%o0], %f2
	fadds	%f3, %f2, %f3
	st	%f4, [%o1+%o0]
	st	%f3, [%o3+%o0]
	ld	[%o1+%o0], %f2
	fadds	%f2, %f4, %f2
	bl	.LL5
	st	%f2, [%o1+%o0]
	
Note there are 2 extra loads

		
bar:
[snip]	
.LL12:
	sll	%o2, 2, %o0
	ld	[%i0+%o0], %f3
	add	%o2, 1, %o2
	ld	[%i1+%o0], %f2
	cmp	%o2, %i2
	fadds	%f3, %f2, %f3
	st	%f3, [%o1+%o0]
	st	%f3, [%o3+%o0]
	ld	[%o1+%o0], %f2
	fadds	%f2, %f3, %f2
	bl	.LL12
	st	%f2, [%o1+%o0]
	
Note one extra load
		
foobar:
[snip]	
.LL19:
	sll	%o1, 2, %o0
	ld	[%i0+%o0], %f2
	add	%o1, 1, %o1
	ld	[%i1+%o0], %f3
	cmp	%o1, %i2
	fadds	%f2, %f3, %f2
	fadds	%f2, %f2, %f4
	st	%f2, [%o2+%o0]
	bl	.LL19
	st	%f4, [%l1+%o0]

This one is fine.
		
baz:
[snip]	
.LL26:
	sll	%i3, 2, %i0
	ld	[%o7+%i0], %f2
	add	%i3, 1, %i3
	ld	[%i1+%i0], %f3
	cmp	%i3, %i2
	fadds	%f2, %f3, %f2
	fadds	%f2, %f2, %f4
	st	%f2, [%i5+%i0]
	bl	.LL26
	st	%f4, [%i4+%i0]
	b	.LL30
	ld	[%g1+%lo(g)], %i0
	
As is this one. 


>How-To-Repeat:
Compile with gcc -O2 -fstrict-aliasing -S 
and look at the assembly for the foo and bar functions. 
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-05-03 23:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-25  2:07 c/2462: "restrict" implementation bug Dan Nicolaescu
  -- strict thread matches above, loose matches on Subject: below --
2003-05-03 23:26 Dan Nicolaescu
2003-03-25  3:16 bangerth
2003-03-25  1:39 bangerth
2001-04-02 14:36 Dan Nicolaescu

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).