Re: Add uninitialized attribute?

2010-08-31 Thread Mark Mitchell
On 8/31/2010 1:19 AM, Andrew Haley wrote:
> On 08/30/2010 03:50 PM, Vincent Lefevre wrote:
>> On 2010-08-30 14:46:57 +0200, Michael Matz wrote:
>>> int x = x;
>>>
>>> is the way GCC offers this idiom since about forever, no need for an 
>>> attribute.  Downthread I see that people worry about this generating an 
>>> actual (uninitialized) access to x.  They are confused.
>>
>> This is not a good idea as "int x = x;" may really generate an
>> (uninitialized) access to x with other compilers.
> 
> Absolutely so.  I suspect it's even undefined behaviour in the standard
> language.  There's no way that this idiom should appear anywhere in
> GNU code.

I agree; an attribute, or the __unitialized__ keyword would be much cleaner.

On the other hand, I think GCC should continue to accept "int x = x;" as
a synonym for the forseeable future; whether or not it's good language
design it has been a de facto part of GNU C for a long time.

-- 
Mark Mitchell
CodeSourcery
m...@codesourcery.com
(650) 331-3385 x713


Re: Add uninitialized attribute?

2010-08-31 Thread Andrew Haley
On 08/30/2010 03:50 PM, Vincent Lefevre wrote:
> On 2010-08-30 14:46:57 +0200, Michael Matz wrote:
>> int x = x;
>>
>> is the way GCC offers this idiom since about forever, no need for an 
>> attribute.  Downthread I see that people worry about this generating an 
>> actual (uninitialized) access to x.  They are confused.
> 
> This is not a good idea as "int x = x;" may really generate an
> (uninitialized) access to x with other compilers.

Absolutely so.  I suspect it's even undefined behaviour in the standard
language.  There's no way that this idiom should appear anywhere in
GNU code.

Andrew.


Re: Add uninitialized attribute?

2010-08-30 Thread Vincent Lefevre
On 2010-08-30 14:46:57 +0200, Michael Matz wrote:
> int x = x;
> 
> is the way GCC offers this idiom since about forever, no need for an 
> attribute.  Downthread I see that people worry about this generating an 
> actual (uninitialized) access to x.  They are confused.

This is not a good idea as "int x = x;" may really generate an
(uninitialized) access to x with other compilers.

-- 
Vincent Lefèvre  - Web: 
100% accessible validated (X)HTML - Blog: 
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


Re: Add uninitialized attribute?

2010-08-30 Thread Michael Matz
Hi,

On Fri, 20 Aug 2010, H.J. Lu wrote:

> int x = 0;
> 
> to silence gcc from uninitialized warnings when I know it is
> unnecessary.  Is that a good idea to add
> 
> int x __attribute__ ((uninitialized));
> 
> to tell compiler that it is OK for "x" to be uninitialized?

int x = x;

is the way GCC offers this idiom since about forever, no need for an 
attribute.  Downthread I see that people worry about this generating an 
actual (uninitialized) access to x.  They are confused.


Ciao,
Michael.


Re: Add uninitialized attribute?

2010-08-23 Thread Richard Guenther
On Sat, Aug 21, 2010 at 11:43 AM, Florian Weimer  wrote:
> * H. J. Lu:
>
>> Sometime I have to do
>>
>> int x = 0;
>>
>> to silence gcc from uninitialized warnings when I know it is
>> unnecessary.
>
> I guess the official idiom is
>
>  int x = x;

That's what I thought as well, so I am confused.

> and it is somewhat used in the GNU project although it is not
> portable.

Neither is any of the other suggestions.

Richard.


Re: Add uninitialized attribute?

2010-08-21 Thread Mark Mitchell
Kreitzer, David L wrote:

> There are some situations where an __undefined__ keyword would be useful

Thanks for the example.

I suppose that in C the natural syntax is a pseudo-function that takes a
type, rather than an object, as an argument:

  __undefined__(int)
  __undefined__(vector float)
  ...

In GCC, at least, __undefined__ could be a macro, for the purposes of
defining its semantics:

  #define __undefined__(T) \
({ T t; t })

That is, create an new variable of type T, do not initialize it, and use
its value.

But, we would not issue warnings about it.

The macro definition is possibly also useful in that I think it says the
right thing for C++.  In particular, in C++, for a class that has
constructors, constructors should run.  You *should not* be able to say:

  __undefined__(std::vector)

and somehow bypass the constructor.  That violates key assumptions of
the programming model in C++.

-- 
Mark Mitchell
CodeSourcery
m...@codesourcery.com
(650) 331-3385 x713


Re: Add uninitialized attribute?

2010-08-21 Thread Florian Weimer
* Andrew Haley:

> On 08/21/2010 10:43 AM, Florian Weimer wrote:
>> * H. J. Lu:
>> 
>>> Sometime I have to do
>>>
>>> int x = 0;
>>>
>>> to silence gcc from uninitialized warnings when I know it is
>>> unnecessary.
>> 
>> I guess the official idiom is
>> 
>>   int x = x;
>> 
>> and it is somewhat used in the GNU project although it is not
>> portable.
>
> Ewww, yuck.  I think this'll get a read from uninitialized message from
> Valgrind.  It's undefined behaviour too.

GCC has specific code to deal with this construct.  I don't think the
undefined load will actually end up in machine code, so valgrind won't
see it.  And while it is undefined according to the standard (that's
why I called it not portable), it appears to be a supported GCC
extension.  (This is based on the existance of the -Wself-init option,
it's not explicitly documented as an extension AFAICT.)

That being said, I don't like it, either.


Re: Add uninitialized attribute?

2010-08-21 Thread Andrew Pinski
On Sat, Aug 21, 2010 at 8:21 AM, Andrew Haley  wrote:

> Ewww, yuck.  I think this'll get a read from uninitialized message from
> Valgrind.  It's undefined behaviour too.

Not to mention there is a patch to get rid of this idiom and warn
about it even without uninitialized warning.

-- Pinski


Re: Add uninitialized attribute?

2010-08-21 Thread Andrew Haley
On 08/21/2010 10:43 AM, Florian Weimer wrote:
> * H. J. Lu:
> 
>> Sometime I have to do
>>
>> int x = 0;
>>
>> to silence gcc from uninitialized warnings when I know it is
>> unnecessary.
> 
> I guess the official idiom is
> 
>   int x = x;
> 
> and it is somewhat used in the GNU project although it is not
> portable.

Ewww, yuck.  I think this'll get a read from uninitialized message from
Valgrind.  It's undefined behaviour too.

Andrew.



Re: Add uninitialized attribute?

2010-08-21 Thread David Brown

Mark Mitchell wrote:

Bernd Schmidt wrote:


int x __attribute__ ((uninitialized));

to tell compiler that it is OK for "x" to be uninitialized?

Better to call it "initialized", analogous to attribute used/unused.


I agree.


I think the general idea is reasonable.  I also think it might be worth
spending a few minutes thinking about whether we can implement some more
general diagnostic suppression mechanism.  E.g.,
   int x __attribute__ ((ignore ("-Wuninitialized")));

Or this.


FWIW, I think that's overly ambitious.



There is already an "optimise" function __attribute__ and matching 
#pragma's that temporarily change the optimisation flags for a function 
(and a similar set for target flags).  For warning messages, there are 
#pragma's (though no push/pop pragma, AFAIK).  Would it be practical to 
have a "diagnostic" function __attribute__ in the same style?  Then at 
least it would be possible to declare 
__attribute__((diagnostic("-Wno-uninitialized"))) on the function in 
question.





RE: Add uninitialized attribute?

2010-08-21 Thread Kreitzer, David L
There are some situations where an __undefined__ keyword would be useful, 
especially w.r.t. vector intrinsics.  Here is an example that came up recently.


t1 = _mm_movehl_ps(t0, t0);
t2 = _mm_add_ps(t1, t0);
_mm_storel_pi(mem, t2);

In this example, the programmer doesn't care about the upper elements of the 
result of the _mm_movehl_ps intrinsic.  They are never used.  The only purpose 
of the first argument to _mm_movehl_ps is to produce these values that are 
never used.  But the programmer has to pass *something* as that first argument. 
 And in this case, the choice of t0 causes the compiler to insert an extra 
copy.  Ideally, the programmer would like to write this:


t1 = _mm_movehl_ps(__undefined__, t0);
t2 = _mm_add_ps(t1, t0);
_mm_storel_pi(mem, t2);

For the Intel Compiler, we are adding new intrinsics to solve this problem, 
e.g. __m128 _mm_undefined_ps().  A keyword would be a nice alternative if we 
could find a convenient syntax.

Dave Kreitzer
IA32/Intel64 Code Generation
Intel Compiler Lab

-Original Message-
From: Mark Mitchell [mailto:m...@codesourcery.com] 
Sent: Friday, August 20, 2010 7:33 PM
To: H.J. Lu
Cc: Bernd Schmidt; Ian Lance Taylor; GCC Development; Kreitzer, David L; 
Girkar, Milind
Subject: Re: Add uninitialized attribute?

H.J. Lu wrote:

> void *undef __attribute__ ((uninitialized)); // Or something similar
> 
> foo (undef);
> 
> Compiler can pass some junk to foo.

I don't think that's a very good idea.  If we need this, which I doubt,
it should be something like a new __undefined__ keyword, that expands to
 an undefined value.  In C++, a syntax like __undefined__() would be
plausible; I'm not sure there's a good C syntax.  Perhaps you could do
what tgmath does.

I guess if you had this, you could use it in place of the attribute;

  int i = __undefined__();

would serve to indicate that you were knowingly not initializing i.

-- 
Mark Mitchell
CodeSourcery
m...@codesourcery.com
(650) 331-3385 x713


Re: Add uninitialized attribute?

2010-08-21 Thread Florian Weimer
* H. J. Lu:

> Sometime I have to do
>
> int x = 0;
>
> to silence gcc from uninitialized warnings when I know it is
> unnecessary.

I guess the official idiom is

  int x = x;

and it is somewhat used in the GNU project although it is not
portable.


Re: Add uninitialized attribute?

2010-08-20 Thread Mark Mitchell
H.J. Lu wrote:

> void *undef __attribute__ ((uninitialized)); // Or something similar
> 
> foo (undef);
> 
> Compiler can pass some junk to foo.

I don't think that's a very good idea.  If we need this, which I doubt,
it should be something like a new __undefined__ keyword, that expands to
 an undefined value.  In C++, a syntax like __undefined__() would be
plausible; I'm not sure there's a good C syntax.  Perhaps you could do
what tgmath does.

I guess if you had this, you could use it in place of the attribute;

  int i = __undefined__();

would serve to indicate that you were knowingly not initializing i.

-- 
Mark Mitchell
CodeSourcery
m...@codesourcery.com
(650) 331-3385 x713


Re: Add uninitialized attribute?

2010-08-20 Thread Mark Mitchell
Bernd Schmidt wrote:

>>> int x __attribute__ ((uninitialized));
>>>
>>> to tell compiler that it is OK for "x" to be uninitialized?
> 
> Better to call it "initialized", analogous to attribute used/unused.

I agree.

>> I think the general idea is reasonable.  I also think it might be worth
>> spending a few minutes thinking about whether we can implement some more
>> general diagnostic suppression mechanism.  E.g.,
>>int x __attribute__ ((ignore ("-Wuninitialized")));
> 
> Or this.

FWIW, I think that's overly ambitious.

-- 
Mark Mitchell
CodeSourcery
m...@codesourcery.com
(650) 331-3385 x713


Re: Add uninitialized attribute?

2010-08-20 Thread H.J. Lu
On Fri, Aug 20, 2010 at 1:57 PM, Bernd Schmidt  wrote:
> On 08/20/2010 10:51 PM, Ian Lance Taylor wrote:
>> "H.J. Lu"  writes:
>>
>>> Sometime I have to do
>>>
>>> int x = 0;
>>>
>>> to silence gcc from uninitialized warnings when I know it is
>>> unnecessary.  Is that a good idea to add
>>>
>>> int x __attribute__ ((uninitialized));
>>>
>>> to tell compiler that it is OK for "x" to be uninitialized?
>
> Better to call it "initialized", analogous to attribute used/unused.
>
>> I think the general idea is reasonable.  I also think it might be worth
>> spending a few minutes thinking about whether we can implement some more
>> general diagnostic suppression mechanism.  E.g.,
>>    int x __attribute__ ((ignore ("-Wuninitialized")));
>
> Or this.
>

Another usage for this it to specify a value which we don't care
and must provide when calling a function due to function prototype.
Currently I have to do

foo (NULL);

Instead I can do

void *undef __attribute__ ((uninitialized)); // Or something similar

foo (undef);

Compiler can pass some junk to foo.


-- 
H.J.


Re: Add uninitialized attribute?

2010-08-20 Thread Bernd Schmidt
On 08/20/2010 10:51 PM, Ian Lance Taylor wrote:
> "H.J. Lu"  writes:
> 
>> Sometime I have to do
>>
>> int x = 0;
>>
>> to silence gcc from uninitialized warnings when I know it is
>> unnecessary.  Is that a good idea to add
>>
>> int x __attribute__ ((uninitialized));
>>
>> to tell compiler that it is OK for "x" to be uninitialized?

Better to call it "initialized", analogous to attribute used/unused.

> I think the general idea is reasonable.  I also think it might be worth
> spending a few minutes thinking about whether we can implement some more
> general diagnostic suppression mechanism.  E.g.,
>int x __attribute__ ((ignore ("-Wuninitialized")));

Or this.


Bernd


Re: Add uninitialized attribute?

2010-08-20 Thread Ian Lance Taylor
"H.J. Lu"  writes:

> Sometime I have to do
>
> int x = 0;
>
> to silence gcc from uninitialized warnings when I know it is
> unnecessary.  Is that a good idea to add
>
> int x __attribute__ ((uninitialized));
>
> to tell compiler that it is OK for "x" to be uninitialized?

I think the general idea is reasonable.  I also think it might be worth
spending a few minutes thinking about whether we can implement some more
general diagnostic suppression mechanism.  E.g.,
   int x __attribute__ ((ignore ("-Wuninitialized")));

Ian


Re: Add uninitialized attribute?

2010-08-20 Thread Diego Novillo

On 10-08-20 15:42 , H.J. Lu wrote:

Hi,

Sometime I have to do

int x = 0;

to silence gcc from uninitialized warnings when I know it is
unnecessary.  Is that a good idea to add

int x __attribute__ ((uninitialized));

to tell compiler that it is OK for "x" to be uninitialized?


Seems to me that there is a lot less typing with the '= 0' variant.

However, there have been several instances (particularly in C++) where 
providing an initial value is somewhat more convoluted.



Diego.


Add uninitialized attribute?

2010-08-20 Thread H.J. Lu
Hi,

Sometime I have to do

int x = 0;

to silence gcc from uninitialized warnings when I know it is
unnecessary.  Is that a good idea to add

int x __attribute__ ((uninitialized));

to tell compiler that it is OK for "x" to be uninitialized?

-- 
H.J.