]> granicus.if.org Git - clang/commit
clang-misexpect: Profile Guided Validation of Performance Annotations in LLVM
authorPetr Hosek <phosek@chromium.org>
Wed, 11 Sep 2019 01:09:16 +0000 (01:09 +0000)
committerPetr Hosek <phosek@chromium.org>
Wed, 11 Sep 2019 01:09:16 +0000 (01:09 +0000)
commitb8a052bb7bf7bfe159d8885739cd906421d33d40
tree47f9f32228d6b633c25d07e0c417f381c8dfeedc
parent20c2e19346d420611e0f67037a827c5d83ce4c0c
clang-misexpect: Profile Guided Validation of Performance Annotations in LLVM

This patch contains the basic functionality for reporting potentially
incorrect usage of __builtin_expect() by comparing the developer's
annotation against a collected PGO profile. A more detailed proposal and
discussion appears on the CFE-dev mailing list
(http://lists.llvm.org/pipermail/cfe-dev/2019-July/062971.html) and a
prototype of the initial frontend changes appear here in D65300

We revised the work in D65300 by moving the misexpect check into the
LLVM backend, and adding support for IR and sampling based profiles, in
addition to frontend instrumentation.

We add new misexpect metadata tags to those instructions directly
influenced by the llvm.expect intrinsic (branch, switch, and select)
when lowering the intrinsics. The misexpect metadata contains
information about the expected target of the intrinsic so that we can
check against the correct PGO counter when emitting diagnostics, and the
compiler's values for the LikelyBranchWeight and UnlikelyBranchWeight.
We use these branch weight values to determine when to emit the
diagnostic to the user.

A future patch should address the comment at the top of
LowerExpectIntrisic.cpp to hoist the LikelyBranchWeight and
UnlikelyBranchWeight values into a shared space that can be accessed
outside of the LowerExpectIntrinsic pass. Once that is done, the
misexpect metadata can be updated to be smaller.

In the long term, it is possible to reconstruct portions of the
misexpect metadata from the existing profile data. However, we have
avoided this to keep the code simple, and because some kind of metadata
tag will be required to identify which branch/switch/select instructions
are influenced by the use of llvm.expect

Patch By: paulkirth
Differential Revision: https://reviews.llvm.org/D66324

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371584 91177308-0d34-0410-b5e6-96231b3b80d8
18 files changed:
include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Basic/DiagnosticGroups.td
lib/CodeGen/CodeGenAction.cpp
lib/Frontend/CompilerInvocation.cpp
test/Profile/Inputs/misexpect-branch-nonconst-expect-arg.proftext [new file with mode: 0644]
test/Profile/Inputs/misexpect-branch.proftext [new file with mode: 0644]
test/Profile/Inputs/misexpect-switch-default-only.proftext [new file with mode: 0644]
test/Profile/Inputs/misexpect-switch-default.proftext [new file with mode: 0644]
test/Profile/Inputs/misexpect-switch-nonconst.proftext [new file with mode: 0644]
test/Profile/Inputs/misexpect-switch.proftext [new file with mode: 0644]
test/Profile/misexpect-branch-cold.c [new file with mode: 0644]
test/Profile/misexpect-branch-nonconst-expected-val.c [new file with mode: 0644]
test/Profile/misexpect-branch-unpredictable.c [new file with mode: 0644]
test/Profile/misexpect-branch.c [new file with mode: 0644]
test/Profile/misexpect-switch-default.c [new file with mode: 0644]
test/Profile/misexpect-switch-nonconst.c [new file with mode: 0644]
test/Profile/misexpect-switch-only-default-case.c [new file with mode: 0644]
test/Profile/misexpect-switch.c [new file with mode: 0644]