brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · a9e6f8c Raw
74 lines · plain
1===================2Misexpect3===================4.. contents::5 6.. toctree::7   :maxdepth: 18 9When developers use ``llvm.expect`` intrinsics, i.e., through use of10``__builtin_expect(...)``, they are trying to communicate how their code is11expected to behave at runtime to the optimizer. These annotations, however, can12be incorrect for a variety of reasons: changes to the code base invalidate them13silently, the developer mis-annotated them (e.g., using ``LIKELY`` instead of14``UNLIKELY``), or perhaps they assumed something incorrectly when they wrote15the annotation. Regardless of why, it is useful to detect these situations so16that the optimizer can make more useful decisions about the code. MisExpect17diagnostics are intended to help developers identify and address these18situations, by comparing the use of the ``llvm.expect`` intrinsic to the ground19truth provided by a profiling input.20 21The MisExpect checks in the LLVM backend follow a simple procedure: if there is22a mismatch between the branch weights collected during profiling and those23supplied by an ``llvm.expect`` intrinsic, then it will emit a diagnostic24message to the user.25 26The most natural place to perform the verification is just prior to when27branch weights are assigned to the target instruction in the form of28branch weight metadata.29 30There are 3 key places in the LLVM backend where branch weights are31created and assigned based on profiling information or the use of the32``llvm.expect`` intrinsic, and our implementation focuses on these33places to perform the verification.34 35We calculate the threshold for emitting MisExpect related diagnostics36based on the values the compiler assigns to ``llvm.expect`` intrinsics,37which can be set through the ``-likely-branch-weight`` and38``-unlikely-branch-weight`` LLVM options. During verification, if the39profile weights mismatch the calculated threshold, then we will emit a40remark or warning detailing a potential performance regression. The41diagnostic also reports the percentage of the time the annotation was42correct during profiling to help developers reason about how to proceed.43 44The diagnostics are also available in the form of optimization remarks,45which can be serialized and processed through the ``opt-viewer.py``46scripts in LLVM.47 48.. option:: -pass-remarks=misexpect49 50  Enables optimization remarks for misexpect when profiling data conflicts with51  use of ``llvm.expect`` intrinsics.52 53 54.. option:: -pgo-warn-misexpect55 56  Enables misexpect warnings when profiling data conflicts with use of57  ``llvm.expect`` intrinsics.58 59LLVM supports 4 types of profile formats: Frontend, IR, CS-IR, and60Sampling. MisExpect Diagnostics are compatible with all Profiling formats.61 62+----------------+--------------------------------------------------------------------------------------+63| Profile Type   | Description                                                                          |64+================+======================================================================================+65| Frontend       | Profiling instrumentation added during compilation by the frontend, i.e. ``clang``   |66+----------------+--------------------------------------------------------------------------------------+67| IR             | Profiling instrumentation added during by the LLVM backend                           |68+----------------+--------------------------------------------------------------------------------------+69| CS-IR          | Context Sensitive IR based profiles                                                  |70+----------------+--------------------------------------------------------------------------------------+71| Sampling       | Profiles collected through sampling with external tools, such as ``perf`` on Linux   |72+----------------+--------------------------------------------------------------------------------------+73 74