I found something interesting the other day that might be of interest to those of you who are extremely performance-minded and are on the latest GCC12.2.0 toolchain, using C++20.
For those of you who don't know, the "[[likely]]" and "[[unlikely]]" attributes were added in C++20 and can be used to annotate code blocks for conditionals or loops to tell the compiler the likelihood of a branch occurring. With this information, it can theoretically tailor its code generation towards your suggestion to improve performance.
I've known about [[likely]] and [[unlikely]] for quite sometime and have even peppered a few around my codebase, but I never looked too much into them, because I figured 1) they are probably mostly ignored by the compiler, like the "inline" or "register" keywords 2) the Dreamcast doesn't have a branch predictor, so what good would it do?
Well, I watched a talk on the two attributes and their impact on code generation, which piqued my interest again, so I decided to do some experimenting in Compiler Explorer, and god dayum was I wrong. At least on our GCC toolchains, there's profound impact on branching and especially looping code generation with them! Holy shit!
Check out the impact it has here on this Compiler Explorer example by toggling on and off the #ifdef:
https://godbolt.org/z/fzj5TadK4
Looks like this is another benefit offered by the new toolchain that will easily let us optimize hot and cold code paths by giving the compiler additional hints. Just wanted to let everyone know.
If you are interested in more information about the two attributes in general, and how they work, good talk:
https://www.youtube.com/watch?v=RjPK3HKcouA
PSA: C++20 [[likely]] and [[unlikely]] attributes on Dreamcast
- GyroVorbis
- Elysian Shadows Developer
- Posts: 1874
- https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
- Joined: Mon Mar 22, 2004 4:55 pm
- Location: #%^&*!!!11one Super Sonic
- Has thanked: 81 times
- Been thanked: 64 times
- Contact:
PSA: C++20 [[likely]] and [[unlikely]] attributes on Dreamcast
- These users thanked the author GyroVorbis for the post (total 3):
- |darc| • BB Hood • ThePerfectK
- GyroVorbis
- Elysian Shadows Developer
- Posts: 1874
- Joined: Mon Mar 22, 2004 4:55 pm
- Location: #%^&*!!!11one Super Sonic
- Has thanked: 81 times
- Been thanked: 64 times
- Contact:
Re: PSA: C++20 [[likely]] and [[unlikely]] attributes on Dreamcast
OKAY, SO.
After the previous post, I felt a bunch of sympathy for C programmers, as I myself have code that I prefer to write in C17... and then I came to the realization that the functionality behind "[[likely]]" and "[[unlikely]]" is actually exposed behind compiler intrinsics which may be leveraged in C!
I have taken the liberty of recreating the above code example in C99, KOS's default, using a pair of macros written to do the exact same thing C++'s attributes are doing, except usable from within C:
https://godbolt.org/z/xc5f9K4xf
After the previous post, I felt a bunch of sympathy for C programmers, as I myself have code that I prefer to write in C17... and then I came to the realization that the functionality behind "[[likely]]" and "[[unlikely]]" is actually exposed behind compiler intrinsics which may be leveraged in C!
I have taken the liberty of recreating the above code example in C99, KOS's default, using a pair of macros written to do the exact same thing C++'s attributes are doing, except usable from within C:
https://godbolt.org/z/xc5f9K4xf
- These users thanked the author GyroVorbis for the post (total 3):
- maslevin • ThePerfectK • BB Hood