--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/a7dbafd5-e5fd-48e2-82cd-6bbd938d77e4n%40chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAGFX3sH9j5XMkwDCNOVowJ0_gU2b4UreGjwdy%3DrHLkZC64t9DQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaR2XQn%2BVNvkX1bw-m4sFudPnnQT6MKO_tCLun52JeJ70A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAB%3D4xhrr1gEnjXss2gAM%3DXvrUhg9xUWoSN49v%3DViCYTaHZHSOw%40mail.gmail.com.
[Note: Excessive usage of either of these attributes is liable to result in performance degradation. — end note]
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAC_ixdzEBtFZAG61%2BtTJG4%2B4p0V3pxUyq_ZS_n3VRmiNTJX9Hw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHk1GDx98zYrv1U3Q80XzCceKLEj7ws3%2BKaxiJwwcf_xnJ_uCA%40mail.gmail.com.
I have heard rumours that overuse of __builtin_expect() can lead to suboptimal compiler output. So probably the same applies to [[likely]] and [[unlikely]] and we should not get carried away with them.
If this were a trivial find-and-replace-type rewrite, sure. But __builtin_expect and the existing LIKELY and UNLIKELY macros work differently than the [[likely]] and [[unlikely]] attributes. I don’t know if we can justify a more involved rewrite without also answering whether this is something that we want to keep doing.
On Mon, Mar 11, 2024 at 2:32 PM Mark Mentovai <ma...@chromium.org> wrote:If this were a trivial find-and-replace-type rewrite, sure. But __builtin_expect and the existing LIKELY and UNLIKELY macros work differently than the [[likely]] and [[unlikely]] attributes. I don’t know if we can justify a more involved rewrite without also answering whether this is something that we want to keep doing.I thought __builtin_expect is the same, but I see the LLVM output of clang differs, and is worse with [[unlikely]]: https://godbolt.org/z/4feEzxfjK
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAB%3D4xhpZq%3DgEi%3D%2Bzb39jANZDTsh69g4K12dD5Scz_NBfYeKieQ%40mail.gmail.com.
Given that the semantics of [[likely]] and [[unlikely]] are different than LIKELY and UNLIKELY, I suggest doing an auto-rename of LIKELY and UNLIKELY to something that doesn't conflict with the standard names (eg. LIKELY_RESULT and UNLIKELY_RESULT since it's used around expression results), and encouraging the standard attributes for any new usage but not migrating old usages (except in code that's being updated anyway).Since the bar for using these is high, the existing uses are probably in code that's performance-critical and subtle, so I don't think we can justify the risk of updating it just for consistency.
On Mon, Mar 11, 2024 at 3:18 PM Joe Mason <joenot...@google.com> wrote:Given that the semantics of [[likely]] and [[unlikely]] are different than LIKELY and UNLIKELY, I suggest doing an auto-rename of LIKELY and UNLIKELY to something that doesn't conflict with the standard names (eg. LIKELY_RESULT and UNLIKELY_RESULT since it's used around expression results), and encouraging the standard attributes for any new usage but not migrating old usages (except in code that's being updated anyway).Since the bar for using these is high, the existing uses are probably in code that's performance-critical and subtle, so I don't think we can justify the risk of updating it just for consistency.I think I am missing something. I don't see where we get that [[likely]] and [[unlikely]] are different (for the same compiler, they are different between clang and gcc). They should result in the same thing that __builtin_expect() does, which is normally nothing at all. It seems like what is new is that they can appear in more places (such as on switch cases), where before you'd need to put a __builtin_expect() above the switch instead to tell the compiler such things.
I think I am missing something. I don't see where we get that [[likely]] and [[unlikely]] are different (for the same compiler, they are different between clang and gcc). They should result in the same thing that __builtin_expect() does, which is normally nothing at all. It seems like what is new is that they can appear in more places (such as on switch cases), where before you'd need to put a __builtin_expect() above the switch instead to tell the compiler such things.
On Fri, Mar 8, 2024 at 3:19 PM Roland McGrath <mcgr...@chromium.org> wrote:The way these attributes are used is somewhat new and different from the old `__builtin_expect` that underlies past macro approaches. It doesn't go on an expression used in a conditional. Instead, it precedes a statement syntactically and says any way of reaching that path is likely/unlikely.
The other point is that, as you said, it SHOULD result in the same thing, but is it worth doing the work to verify that?
On Mon, Mar 11, 2024 at 2:44 PM danakj <dan...@chromium.org> wrote:On Mon, Mar 11, 2024 at 2:32 PM Mark Mentovai <ma...@chromium.org> wrote:If this were a trivial find-and-replace-type rewrite, sure. But __builtin_expect and the existing LIKELY and UNLIKELY macros work differently than the [[likely]] and [[unlikely]] attributes. I don’t know if we can justify a more involved rewrite without also answering whether this is something that we want to keep doing.I thought __builtin_expect is the same, but I see the LLVM output of clang differs, and is worse with [[unlikely]]: https://godbolt.org/z/4feEzxfjKWait no, sorry, I broke my example: https://godbolt.org/z/5PdnoxPMqI can't see what is different with __builtin_expect vs [[unlikely]], but I am also failing to get llvm.expect in the IR at all.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFA-TJibgbVDw%2BBZnxbGWRjOWVQ6UFqnwgXx7xEy5hZusQ%40mail.gmail.com.
I'll note that thanks to https://reviews.llvm.org/D134456 , PGO data wins over the annotation when both are present. (That used to not be the case!) So at least the annotations shouldn't hurt.
I'd love to see that rewriting done as a reusable clang-tidy fixit that's generic/configurable and upstreamed so as not to apply only to chromium.
likely and unlikely are useful for branches that we will never take without terminating (there's a NOTREACHED_NORETURN inside) or, inside terminating things like the CHECK macro. PGO does a better job, in that it's based on what we actually see happen in benchmarks, so the [[likely]] tool is not meant for "i believe we'll usually end up in this path" in a general sense. It really is for "we should ~never go here".Since all of our A/B testing and performance work is done _without_ PGO (for good reasons Leszek talks about as well), these attributes are not totally useless, they are worth using to skip over what would be ~dead code, but they really should be rare too
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaSOV3pad2WMb_qGw0FKUouDRSrfCEiGP0YqqYh01NvW_w%40mail.gmail.com.
I think this is orthogonal to the spelling question though. As long as it is sometimes appropriate to use likely/unlikely, even if rare, we'll want to have a spelling for it. The standard spelling seems preferable to the macro version, so let's migrate that.Separately, it may well be the case that some, or even most, of our existing uses are not appropriate. But I think we can change the syntax en masse and worry about that separately if someone feels inspired.I.e., I'm +1 to Peter's proposal.
I'm going to call the question.I think we should allow these and do a mechanical change that rewrites existing uses (to a form that still compiles, which will sometimes involve moving where in the statement they appear) and eliminates the macros. This is monotonically better than our current state. It will not change our current codegen (which seems to be one of the main concerns above), and it is standardized and readable.For cost/benefit reasons, I do not propose auditing existing uses to remove things that "don't seem necessary", unless someone is motivated to do that themselves.Please reply in the next week with your thoughts.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/bbd1da29-8d10-4003-af90-1bcaeda7facbn%40chromium.org.
Just based on what I've seen on #cxx on Slack, I'm wondering if we still want to do this.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAAHOzFAjzJpHWrJ_01K2d%3DNfLZSVKHxu%3DLs5-ctV_1OvUp4UPA%40mail.gmail.com.