Questions about __attribute((cold))
\ John Fine (23 Mar 2008)
. \ John Fine (24 Mar 2008)
. \ Brian Dessent (24 Mar 2008)
. . \ John Fine (24 Mar 2008)

3 msghow to initialize union?
4 msgOn the C99 Status
1 msgProblem with GCC 4.2 and above
5 msgHelp!!! build failed for GCC-3.2.3
1 msgPassing array variable as Input Constraint to l...
7 msgException handling on AIX5.3 with gcc 3.4.6
1 msgINT64
1 msgPassing array variable address as Input Constra...
3 msgDynamic compilation using gcc
3 msgWhat does the version number of gcc mean ?
1 msgCome join me on ssbbw4u...
3 msg64-bit write() system call
23 msgtry, finally
3 msgstack variables and loops
2 msgHow can I get the dump file which is in SSA form ?
1 msgSie wurden ins RotlichtVZ eingeladen
2 msgModifying the std C libraries
1 msgRe: An error occured when building gcc4.3.0
5 msgStrange syntax error problem with mmorpg-framework
Subject:Questions about __attribute((cold))
Group:Gcc-help
From:John Fine
Date:23 Mar 2008


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.



© 2004-2008 readlist.com