<div dir="ltr">We would like to propose adding a new clang based tool, clang-mispredict, for identifying potentially incorrect uses of __builtin_expect().  <br> <br>--- TLDR ---<br>What are we proposing?<br>* Adding a new tool, clang-mispredict, to the LLVM project.<br> <br>What is its purpose?<br>* Identifying potentially problematic uses of __builtin_expect(). <br> <br>Why is this important?<br>* __builtin_expect() has a high performance cost when it is used incorrectly.<br> <br>Where will the code live?<br>* clang-mispredict will live in clang-tools-extra, and there will be some additions to clang.<br> <br>Open questions<br>* What is the correct heuristic for determining a mismatch?<br> <br>Potential shortcomings<br>* The approach is sensitive to both profiling input and configuration.<br> <br> <br>--- Details ---<br> <br>We plant to build a new tool, clang-mispredict, on top of Clang’s PGO infrastructure. This tool will issue warnings about the use of _builtin_expect() when collected PGO profile data shows a mismatch with the use of __builtin_expect(). Existing solutions for validating these annotations generally follow this approach: leverage dynamic profiling to validate existing annotations. This is the approach used in the Linux kernel, for example. Unfortunately, existing solutions seem to be custom efforts specific to a particular code base. Supporting this in the LLVM ecosystem gives developers a general way to check the use of _builtin_expect() without creating custom instrumentation.<br> <br>Additionally, if _builtin_expect() is used incorrectly, then developers may notice a performance regression when switching to the new PM. This happens because the New PM follows performance annotations more closely than the legacy PM, and is therefore more likely to try and aggressively optimize these cases. Our proposed tool can help developers narrow their focus to potentially problematic areas during the transition.<br> <br>Finally, it may also be beneficial to suggest annotations when profiling suggests it would be beneficial. We consider this a desirable property not only from a performance standpoint, but also for identifying potential bugs (both in the profiling corpus and within the codebase). <br> <br>We think having clang-mispredict behave similar to clang-tidy is good approach. We can use a compile commands database in conjunction with profiling data to drive our validation tool. More concretely, we plan to use Clang’s existing PGO infrastructure to emit warnings where branch weights are assigned, but stop compilation before the IR is passed to the backend. This largely amounts to checking if the profile counters are outside of certain thresholds for 'hot' and 'cold' code. Similar to clang tidy, we plan to support suppressing warnings within the codebase through use of comment strings. <br> <br>The most obvious question about this approach is how to quantify what is ‘hot’ or ‘cold’. Initially, we plan to use the thresholds already present in LLVM; however, we are happy to use any suitable heuristic or empirically derived threshold. Input from the community about the correct heuristic are most welcome.<br> <br>Our strategy does have some shortcomings. The usefulness of the warnings are directly related to how representative the profiling input actually was when compared to its normal use. If the profile collected is not representative of typical use, then the warnings may not reflect the ground truth. Program and build configuration can also dramatically change which paths will be executed, thus affecting the quality of the profile data and the generated warnings.<br> <br>Ultimately, we are proposing adding a new standalone tool that takes a compile commands database and an instrumentation profile to emit warnings in a clang-tidy fashion.<br> <br>Please let us know if you have comments or concerns about this proposal.<div><br>Thanks!<br><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Paul Kirth</div></div></div></div>