* Add uninitialized attribute? @ 2010-08-20 20:36 H.J. Lu 2010-08-20 20:51 ` Diego Novillo ` (3 more replies) 0 siblings, 4 replies; 19+ messages in thread From: H.J. Lu @ 2010-08-20 20:36 UTC (permalink / raw) To: GCC Development, Kreitzer, David L, Girkar, Milind 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 20:36 Add uninitialized attribute? H.J. Lu @ 2010-08-20 20:51 ` Diego Novillo 2010-08-20 21:57 ` Ian Lance Taylor ` (2 subsequent siblings) 3 siblings, 0 replies; 19+ messages in thread From: Diego Novillo @ 2010-08-20 20:51 UTC (permalink / raw) To: H.J. Lu; +Cc: GCC Development, Kreitzer, David L, Girkar, Milind 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 20:36 Add uninitialized attribute? H.J. Lu 2010-08-20 20:51 ` Diego Novillo @ 2010-08-20 21:57 ` Ian Lance Taylor 2010-08-20 22:52 ` Bernd Schmidt 2010-08-21 11:46 ` Florian Weimer 2010-08-30 16:42 ` Michael Matz 3 siblings, 1 reply; 19+ messages in thread From: Ian Lance Taylor @ 2010-08-20 21:57 UTC (permalink / raw) To: H.J. Lu; +Cc: GCC Development, Kreitzer, David L, Girkar, Milind "H.J. Lu" <hjl.tools@gmail.com> 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 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 21:57 ` Ian Lance Taylor @ 2010-08-20 22:52 ` Bernd Schmidt 2010-08-20 23:29 ` H.J. Lu 2010-08-21 1:05 ` Mark Mitchell 0 siblings, 2 replies; 19+ messages in thread From: Bernd Schmidt @ 2010-08-20 22:52 UTC (permalink / raw) To: Ian Lance Taylor Cc: H.J. Lu, GCC Development, Kreitzer, David L, Girkar, Milind On 08/20/2010 10:51 PM, Ian Lance Taylor wrote: > "H.J. Lu" <hjl.tools@gmail.com> 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 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 22:52 ` Bernd Schmidt @ 2010-08-20 23:29 ` H.J. Lu 2010-08-21 9:43 ` Mark Mitchell 2010-08-21 1:05 ` Mark Mitchell 1 sibling, 1 reply; 19+ messages in thread From: H.J. Lu @ 2010-08-20 23:29 UTC (permalink / raw) To: Bernd Schmidt Cc: Ian Lance Taylor, GCC Development, Kreitzer, David L, Girkar, Milind On Fri, Aug 20, 2010 at 1:57 PM, Bernd Schmidt <bernds@codesourcery.com> wrote: > On 08/20/2010 10:51 PM, Ian Lance Taylor wrote: >> "H.J. Lu" <hjl.tools@gmail.com> 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 23:29 ` H.J. Lu @ 2010-08-21 9:43 ` Mark Mitchell 2010-08-21 15:21 ` Kreitzer, David L 0 siblings, 1 reply; 19+ messages in thread From: Mark Mitchell @ 2010-08-21 9:43 UTC (permalink / raw) To: H.J. Lu Cc: Bernd Schmidt, Ian Lance Taylor, GCC Development, Kreitzer, David L, Girkar, Milind 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__<X>() 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__<int>(); would serve to indicate that you were knowingly not initializing i. -- Mark Mitchell CodeSourcery mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: Add uninitialized attribute? 2010-08-21 9:43 ` Mark Mitchell @ 2010-08-21 15:21 ` Kreitzer, David L 2010-08-21 22:48 ` Mark Mitchell 0 siblings, 1 reply; 19+ messages in thread From: Kreitzer, David L @ 2010-08-21 15:21 UTC (permalink / raw) To: Mark Mitchell, H.J. Lu Cc: Bernd Schmidt, Ian Lance Taylor, GCC Development, Girkar, Milind 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. <a bunch of 4 wide vector computation> 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: <a bunch of 4 wide vector computation> 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:mark@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__<X>() 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__<int>(); would serve to indicate that you were knowingly not initializing i. -- Mark Mitchell CodeSourcery mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 15:21 ` Kreitzer, David L @ 2010-08-21 22:48 ` Mark Mitchell 0 siblings, 0 replies; 19+ messages in thread From: Mark Mitchell @ 2010-08-21 22:48 UTC (permalink / raw) To: Kreitzer, David L Cc: H.J. Lu, Bernd Schmidt, Ian Lance Taylor, GCC Development, Girkar, Milind 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<int>) and somehow bypass the constructor. That violates key assumptions of the programming model in C++. -- Mark Mitchell CodeSourcery mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 22:52 ` Bernd Schmidt 2010-08-20 23:29 ` H.J. Lu @ 2010-08-21 1:05 ` Mark Mitchell 2010-08-21 15:23 ` David Brown 1 sibling, 1 reply; 19+ messages in thread From: Mark Mitchell @ 2010-08-21 1:05 UTC (permalink / raw) To: Bernd Schmidt Cc: Ian Lance Taylor, H.J. Lu, GCC Development, Kreitzer, David L, Girkar, Milind 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 mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 1:05 ` Mark Mitchell @ 2010-08-21 15:23 ` David Brown 0 siblings, 0 replies; 19+ messages in thread From: David Brown @ 2010-08-21 15:23 UTC (permalink / raw) To: gcc 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 20:36 Add uninitialized attribute? H.J. Lu 2010-08-20 20:51 ` Diego Novillo 2010-08-20 21:57 ` Ian Lance Taylor @ 2010-08-21 11:46 ` Florian Weimer 2010-08-21 15:29 ` Andrew Haley 2010-08-23 17:26 ` Richard Guenther 2010-08-30 16:42 ` Michael Matz 3 siblings, 2 replies; 19+ messages in thread From: Florian Weimer @ 2010-08-21 11:46 UTC (permalink / raw) To: H.J. Lu; +Cc: GCC Development, Kreitzer, David L, Girkar, Milind * 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 11:46 ` Florian Weimer @ 2010-08-21 15:29 ` Andrew Haley 2010-08-21 16:07 ` Andrew Pinski 2010-08-21 18:50 ` Florian Weimer 2010-08-23 17:26 ` Richard Guenther 1 sibling, 2 replies; 19+ messages in thread From: Andrew Haley @ 2010-08-21 15:29 UTC (permalink / raw) To: gcc 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 15:29 ` Andrew Haley @ 2010-08-21 16:07 ` Andrew Pinski 2010-08-21 18:50 ` Florian Weimer 1 sibling, 0 replies; 19+ messages in thread From: Andrew Pinski @ 2010-08-21 16:07 UTC (permalink / raw) To: Andrew Haley; +Cc: gcc On Sat, Aug 21, 2010 at 8:21 AM, Andrew Haley <aph@redhat.com> 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 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 15:29 ` Andrew Haley 2010-08-21 16:07 ` Andrew Pinski @ 2010-08-21 18:50 ` Florian Weimer 1 sibling, 0 replies; 19+ messages in thread From: Florian Weimer @ 2010-08-21 18:50 UTC (permalink / raw) To: Andrew Haley; +Cc: gcc * 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-21 11:46 ` Florian Weimer 2010-08-21 15:29 ` Andrew Haley @ 2010-08-23 17:26 ` Richard Guenther 1 sibling, 0 replies; 19+ messages in thread From: Richard Guenther @ 2010-08-23 17:26 UTC (permalink / raw) To: Florian Weimer Cc: H.J. Lu, GCC Development, Kreitzer, David L, Girkar, Milind On Sat, Aug 21, 2010 at 11:43 AM, Florian Weimer <fw@deneb.enyo.de> 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-20 20:36 Add uninitialized attribute? H.J. Lu ` (2 preceding siblings ...) 2010-08-21 11:46 ` Florian Weimer @ 2010-08-30 16:42 ` Michael Matz 2010-08-30 16:46 ` Vincent Lefevre 3 siblings, 1 reply; 19+ messages in thread From: Michael Matz @ 2010-08-30 16:42 UTC (permalink / raw) To: H.J. Lu; +Cc: GCC Development, Kreitzer, David L, Girkar, Milind 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-30 16:42 ` Michael Matz @ 2010-08-30 16:46 ` Vincent Lefevre 2010-08-31 10:00 ` Andrew Haley 0 siblings, 1 reply; 19+ messages in thread From: Vincent Lefevre @ 2010-08-30 16:46 UTC (permalink / raw) To: gcc 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 <vincent@vinc17.net> - Web: <http://www.vinc17.net/> 100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/> Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon) ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-30 16:46 ` Vincent Lefevre @ 2010-08-31 10:00 ` Andrew Haley 2010-08-31 16:13 ` Mark Mitchell 0 siblings, 1 reply; 19+ messages in thread From: Andrew Haley @ 2010-08-31 10:00 UTC (permalink / raw) To: gcc 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. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: Add uninitialized attribute? 2010-08-31 10:00 ` Andrew Haley @ 2010-08-31 16:13 ` Mark Mitchell 0 siblings, 0 replies; 19+ messages in thread From: Mark Mitchell @ 2010-08-31 16:13 UTC (permalink / raw) To: gcc 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 mark@codesourcery.com (650) 331-3385 x713 ^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2010-08-31 14:57 UTC | newest] Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2010-08-20 20:36 Add uninitialized attribute? H.J. Lu 2010-08-20 20:51 ` Diego Novillo 2010-08-20 21:57 ` Ian Lance Taylor 2010-08-20 22:52 ` Bernd Schmidt 2010-08-20 23:29 ` H.J. Lu 2010-08-21 9:43 ` Mark Mitchell 2010-08-21 15:21 ` Kreitzer, David L 2010-08-21 22:48 ` Mark Mitchell 2010-08-21 1:05 ` Mark Mitchell 2010-08-21 15:23 ` David Brown 2010-08-21 11:46 ` Florian Weimer 2010-08-21 15:29 ` Andrew Haley 2010-08-21 16:07 ` Andrew Pinski 2010-08-21 18:50 ` Florian Weimer 2010-08-23 17:26 ` Richard Guenther 2010-08-30 16:42 ` Michael Matz 2010-08-30 16:46 ` Vincent Lefevre 2010-08-31 10:00 ` Andrew Haley 2010-08-31 16:13 ` Mark Mitchell
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).