10

IN SHORT: Is there a place to put the [[(un)likely]] attribute so that the control flow at cond2 is considered likely to take the false branch, without affecting the possibility of branches at cond1?

if (cond1) {
  do {
    foo();
  } while (cond2);
}

If I put [[unlikely]] in do [[unlikely]] { or do { [[unlikely]],does it affect cond1? Since the true-branch of cond1 is the ONLY path that goes into the loop and is the path that ALWAYS goes into the loop, according to cppreference.com:

Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are less likely than any alternative path of execution that does not include such a statement.

it seems that cond1 is affected.

If I put [[likely]] after the loop, e.g. do { foo(); } while(cond2); [[likely]];, the attribute is applied to the empty statement. This code is unintuitive and become unclearer whether cond2 and/or cond1 are affected.

Btw, the question is infact asking about the semantics of [[(un)likely]] attribute, not about implementations neither alternatives such as __builtin_expect or breaking the do-while loop to foo(); while(cond2) [[unlikely]] foo();.

VainMan
  • 2,025
  • 8
  • 23
  • What is wrong with `do { foo(); } while(cond2) [[likely]];`? – Goswin von Brederlow May 25 '22 at 13:54
  • @GoswinvonBrederlow doesn't compile. – ixSci May 25 '22 at 14:08
  • 1
    It seems that the only work around is to define a function similar to `__builtin_expect`, e.g. `inline bool unlikely_cond(bool cond) { if (cond) [[unlikely]] { return true; } else { return false; } }`, then use `unlikely_cond(cond2)`. But I still don't understand the *exact* semantics of `[[(un)likely]]` attributes. – VainMan May 31 '22 at 15:11

2 Answers2

0

Not a solution, but as an alternative one could try this:

while (1){
    foo();
    if (cond2) [[unlikely]]
        break;
}
Azmisov
  • 6,493
  • 7
  • 53
  • 70
-1

I believe this is the correct placement of the attributes:

if (cond1) [[likely]] {
    do [[unlikely]] {
        foo();
    } while (cond2);
}

The attributes are just examples to show where to place each. The likely affects the if while the unlikely affects the do/while. Confusing that the attribute is so far from the condition but that seems to be the place where it compiles.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42