Questions tagged [likely-unlikely]

likely and unlikely are hints to a compiler that a conditional is likely (or not) to run, informing its decision about how to lay out branches in a way that's good for the pipeline and/or I-cache locality, or whether or not to make branchless asm.

likely and unlikely are hints to a compiler that a block of code controlled by a run-time condition is likely (or not) to run. This will influence its decisions about how to lay out branches in a way that's good for the pipeline and/or I-cache locality (prefer not-taken branches in the common case).

Also, either hint will discourage compilers from making branchless asm, since predictable branches can be better than cmov. (e.g. in a case like gcc optimization flag -O3 makes code slower than -O2 without profile-guided optimization, gcc -O3 guesses the branch will be hard to predict and makes branchless asm, but since it is predictable in practice -O2's branchy asm is faster. A likely or unlikely hint might have had the same effect, even if the actual data is evenly split between taken and not-taken.)

The GCC compiler provides __builtin_expect, and likely() and unlikely() are macros that wrap it builtin. ISO C++20 introduces [[likely]] and [[unlikely]] attributes that attach to blocks (actually labels or statements) instead of conditions.

These hints do not affect run-time dynamic branch prediction by typical modern high-end CPUs; this is a common misconception.

A few superscalar / out-of-order CPUs have supported runtime branch hints embedded into machine code, for example PowerPC. Pentium 4 also supported hints, but earlier and later x86 CPUs ignore them. likely/unlikely hints may or may not result in a compiler using run-time hints on the rare targets that do support them.

Static prediction based on the branch being forward or backward does still happen in some CPUs (when the dynamic predictors don't have a prediction for a branch). See Why did Intel change the static branch prediction mechanism over these years? for more info on how x86 microarchitectures have evolved. However, laying out blocks of code to minimize taken branches on the fast (likely) path usually optimizes for this anyway, whether it's relevant for the target CPU or not (e.g. modern x86).

Resources / examples:

See also the tag; in some ways runtime branch prediction is a different subject from compile-time branch hints.

24 questions
3
votes
1 answer

Can I improve branch prediction with my code?

This is a naive general question open to any platform, language, or compiler. Though I am most curious about Aarch64, C++, GCC. When coding an unavoidable branch in program flow dependent on I/O state (compiler cannot predict), and I know that one…
3
votes
0 answers

MSVC C++ compiler giving inconsistent performance on different builds of the same code when using [[unlikely/likely]] attributes

I am simply experimenting with the [[unlikely/likely]] branch hints available when compiling with /std:c++latest. To do this I am using a modified version of the code provided by http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0479r0.html…
name
  • 181
  • 1
  • 12
3
votes
0 answers

c++20 Likely and Unlikely performance optimization

I read about the attributes likely and unlikely of c++20, and i want to ask if there are some reasonable and official data of performance advantages that this new attributes give to the execution. I mean there are examples execution test that give…
Zig Razor
  • 3,381
  • 2
  • 15
  • 35
2
votes
1 answer

What does .text.unlikely mean in ELF object files?

In my objdump -t output, I see the following two lines: 00000000000004d2 l F .text.unlikely 00000000000000ec function-signature-goes-here [clone .cold.427] and 00000000000018e0 g F .text 0000000000000690 function-signature-goes-here I…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
1 answer

Why doesn't likely and unlikely macros have any effect on ARM assembly code?

I took below example from https://kernelnewbies.org/FAQ/LikelyUnlikely #include #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) int main(char *argv[], int argc) { int a; /* Get the…
bornfree
  • 2,308
  • 1
  • 23
  • 33
1
vote
0 answers

The correct usage of unlikely macro in multiple conditions

I see many examples using the unlikely macro (not the C++20 attribute) as if( unlikely( server == 0 ) ), but never combining them both as if( unlikely( server == 0 ) || unlikely( client == 0 ) ) or if( unlikely( server == 0 || client == 0 ) ). Can I…
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
1
vote
1 answer

likely()/unlikely() macros in the Linux kernel with a segmentation fault

I have an understanding of how the likely()/unlikely() macros work and I also have an understanding of branch prediction. Unfortunately, I did not learn branch prediction in the context of high level programming. What I want to know is if the…
0
votes
1 answer

Linux Kernel: likely() vs unlikely()

These two methods seem to be extensively used inside linux kernel code. I know the foundations of branch prediction, but I would like to know how these two functions affect the operation of if() statements. Also do they work at the level of CPU…
Ace
  • 1,501
  • 4
  • 30
  • 49
0
votes
0 answers

What's the point of "unlikely()"?

Possible Duplicate: likely/unlikely macros in the Linux kernel In the Linux kernel, I came across the line in ./linux/include/shed.h: if (unlikely(atomic_dec_and_test(&mm->mm_count))) What exactly is the point of unlikely? I've found a…
Ivan
  • 10,052
  • 12
  • 47
  • 78
1
2