[misexpect] Re-implement MisExpect Diagnostics
authorPaul Kirth <paulkirth@google.com>
Tue, 29 Mar 2022 17:20:12 +0000 (17:20 +0000)
committerPaul Kirth <paulkirth@google.com>
Thu, 31 Mar 2022 17:38:21 +0000 (17:38 +0000)
commit46774df307159444d65083c2fd82f8574f0ab1d9
treef8ffbe79bc649e116207e735120faefb89fd9394
parent7641e42def02f228dc9e907094353c3ac55d5226
[misexpect] Re-implement MisExpect Diagnostics

Reimplements MisExpect diagnostics from D66324 to reconstruct its
original checking methodology only using MD_prof branch_weights
metadata.

New checks rely on 2 invariants:

1) For frontend instrumentation, MD_prof branch_weights will always be
   populated before llvm.expect intrinsics are lowered.

2) for IR and sample profiling, llvm.expect intrinsics will always be
   lowered before branch_weights are populated from the IR profiles.

These invariants allow the checking to assume how the existing branch
weights are populated depending on the profiling method used, and emit
the correct diagnostics. If these invariants are ever invalidated, the
MisExpect related checks would need to be updated, potentially by
re-introducing MD_misexpect metadata, and ensuring it always will be
transformed the same way as branch_weights in other optimization passes.

Frontend based profiling is now enabled without using LLVM Args, by
introducing a new CodeGen option, and checking if the -Wmisexpect flag
has been passed on the command line.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D115907
51 files changed:
clang/docs/MisExpect.rst [new file with mode: 0644]
clang/docs/ReleaseNotes.rst
clang/docs/index.rst
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Basic/DiagnosticFrontendKinds.td
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/Profile/Inputs/misexpect-branch-nonconst-expect-arg.proftext [new file with mode: 0644]
clang/test/Profile/Inputs/misexpect-branch.proftext [new file with mode: 0644]
clang/test/Profile/Inputs/misexpect-switch-default-only.proftext [new file with mode: 0644]
clang/test/Profile/Inputs/misexpect-switch-default.proftext [new file with mode: 0644]
clang/test/Profile/Inputs/misexpect-switch-nonconst.proftext [new file with mode: 0644]
clang/test/Profile/Inputs/misexpect-switch.proftext [new file with mode: 0644]
clang/test/Profile/misexpect-branch-cold.c [new file with mode: 0644]
clang/test/Profile/misexpect-branch-nonconst-expected-val.c [new file with mode: 0644]
clang/test/Profile/misexpect-branch-unpredictable.c [new file with mode: 0644]
clang/test/Profile/misexpect-branch.c [new file with mode: 0644]
clang/test/Profile/misexpect-switch-default.c [new file with mode: 0644]
clang/test/Profile/misexpect-switch-nonconst.c [new file with mode: 0644]
clang/test/Profile/misexpect-switch-only-default-case.c [new file with mode: 0644]
clang/test/Profile/misexpect-switch.c [new file with mode: 0644]
llvm/docs/MisExpect.rst [new file with mode: 0644]
llvm/docs/UserGuides.rst
llvm/include/llvm/IR/DiagnosticInfo.h
llvm/include/llvm/IR/LLVMContext.h
llvm/include/llvm/Target/TargetOptions.h
llvm/include/llvm/Transforms/Utils/MisExpect.h [new file with mode: 0644]
llvm/lib/IR/DiagnosticInfo.cpp
llvm/lib/IR/LLVMContext.cpp
llvm/lib/IR/LLVMContextImpl.h
llvm/lib/Transforms/IPO/SampleProfile.cpp
llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
llvm/lib/Transforms/Utils/CMakeLists.txt
llvm/lib/Transforms/Utils/MisExpect.cpp [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/misexpect-branch-correct.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/misexpect-branch-threshold.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/misexpect-branch.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/misexpect-switch-correct.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/Inputs/misexpect-switch.proftext [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-branch-correct.ll [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-branch-stripped.ll [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-branch-unpredictable.ll [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-branch.ll [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-switch-default.ll [new file with mode: 0644]
llvm/test/Transforms/PGOProfile/misexpect-switch.ll [new file with mode: 0644]