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.