public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Questions about __attribute((cold))
@ 2008-03-23  4:02 John Fine
  2008-03-24 18:08 ` John Fine
  2008-03-24 20:27 ` Brian Dessent
  0 siblings, 2 replies; 4+ messages in thread
From: John Fine @ 2008-03-23  4:02 UTC (permalink / raw)
  To: gcc-help

I want to understand some things about __attribute((cold)) that were 
unclear from the documentation. I have a few reasons including a 
specific possible use  that seems to not work.

I am using (GCC) 4.4.0 20080319 (experimental)
I constructed several sample cpp files and compiled with
g++  -O3 -S -fstrict-aliasing -fverbose-asm test.cpp
and looked at test.s to see the generated code.  I haven't yet managed 
to even construct a case in which __attribute((cold)) makes a difference.

For the first specific use I have in mind, consider code with the basic 
structure:

while (xxx1) {
   xxx2;
   if (xxx3) {
       xxx4; }
   xxx5; }

Where each xxxn is replaced by some useful code.  I want to invent a 
syntax to be used across my project in several such cases to get the 
compiler to push xxx4 out of line to avoid polluting the L1 cache.

One version of that which seems to work, but I dislike syntactically is:

#define rarely_true(x) __builtin_expect(x,0)
while (xxx1) {
   xxx2;
   if ( rarely_true(xxx3) ) {
       xxx4; }
   xxx5; }

The documentation of "cold" seems to say that declaring a function as 
cold will tag the block containing the call to that function as cold, so 
I hoped to be able to use something like this (which syntax I prefer):

inline void __attribute__((cold)) rarely_execute() {}
while (xxx1) {
   xxx2;
   if (xxx3) {
       rarely_execute();
       xxx4; }
   xxx5; }

That generates identical code as without the rarely_execute(), unlike 
the above rarely_true(...) version that generates code that looks like 
it would be better.

I don't know whether I misunderstood what cold is supposed to do, or  
whether the fact that the function does nothing causes the cold to be 
ignored or what.

I have also tried some examples where I want cold on a non empty 
function for other purposes (such as error reporting) and even there 
haven't constructed an example where it makes a difference.  Though for 
that use I have not tried as many different constructs to try to find a 
case where it makes a difference.

I understand that the usual advice for these situations is to use 
profile guided optimization instead of programmer guided optimization.  
I think my reasons for rejecting that are valid, but I'd prefer not to 
discuss them here.  I'd appreciate some help with programmer guided 
optimization methods without a lot of advice against the general idea.

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

end of thread, other threads:[~2008-03-24 21:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-23  4:02 Questions about __attribute((cold)) John Fine
2008-03-24 18:08 ` John Fine
2008-03-24 20:27 ` Brian Dessent
2008-03-24 21:25   ` John Fine

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