diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h --- a/llvm/include/llvm/Target/TargetOptions.h +++ b/llvm/include/llvm/Target/TargetOptions.h @@ -363,6 +363,7 @@ /// When set to true, enable MisExpect Diagnostics /// By default, it is set to false unsigned MisExpect : 1; + unsigned MissingAnnotations : 1; /// Name of the stack usage file (i.e., .su file) if user passes /// -fstack-usage. If empty, it can be implied that -fstack-usage is not diff --git a/llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp b/llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp --- a/llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp +++ b/llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp @@ -21,6 +21,7 @@ #include "llvm/IR/Intrinsics.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/MDBuilder.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" @@ -343,8 +344,13 @@ static bool handleBranchExpect(BranchInst &BI) { if (BI.isUnconditional()) return false; - - return handleBrSelExpect(BI); + auto ret = handleBrSelExpect(BI); + if (!ret) { + SmallVector Weights; + if (extractBranchWeights(BI, Weights)) + misexpect::checkMissingAnnotations(BI, Weights, /*isFrontend=*/true); + } + return ret; } static bool lowerExpectIntrinsic(Function &F) { @@ -358,6 +364,11 @@ } else if (SwitchInst *SI = dyn_cast(BB.getTerminator())) { if (handleSwitchExpect(*SI)) ExpectIntrinsicsHandled++; + else { + SmallVector Weights; + if (extractBranchWeights(*SI, Weights)) + misexpect::checkMissingAnnotations(*SI, Weights, /*isFrontend=*/true); + } } // Remove llvm.expect intrinsics. Iterate backwards in order diff --git a/llvm/lib/Transforms/Utils/MisExpect.cpp b/llvm/lib/Transforms/Utils/MisExpect.cpp --- a/llvm/lib/Transforms/Utils/MisExpect.cpp +++ b/llvm/lib/Transforms/Utils/MisExpect.cpp @@ -63,7 +63,7 @@ cl::desc("Prevents emiting diagnostics when profile counts are " "within N% of the threshold..")); -// Command line option to enable/disable the Remparks when profile data suggests +// Command line option to enable/disable the Remarks when profile data suggests // that the llvm.expect intrinsic may be profitable static cl::opt PGOMissingAnnotations("pgo-missing-annotations", cl::init(false), @@ -238,7 +238,7 @@ // To determine if we emit a diagnostic, we need to compare the branch weights // from the profile to those added by the llvm.expect intrinsic. // So first, we extract the "likely" and "unlikely" weights from - // ExpectedWeights And determine the correct weight in the profile to compare + // ExpectedWeights and determine the correct weight in the profile to compare // against. uint64_t LikelyBranchWeight = 0, UnlikelyBranchWeight = std::numeric_limits::max(); @@ -288,7 +288,7 @@ void verifyMissingAnnotations(Instruction &I, ArrayRef RealWeights) { // To determine if we emit a diagnostic, we need to compare the branch weights // from the profile to those that would be added by the llvm.expect intrinsic. - // And compare it to the real profile to see if it would be profitable. + // to see if adding an annotation would be profitable. uint32_t ProfiledWeight = *std::max_element(RealWeights.begin(), RealWeights.end()); @@ -303,14 +303,12 @@ void checkMissingAnnotations(Instruction &I, const ArrayRef ExistingWeights, bool IsFrontendInstr) { - - // TODO: Ironically, this is probably a branch that should be marked UNLIKELY // exit early if these diagnostics weren't requested - if (!isAnnotationDiagEnabled(I.getContext())) + if (LLVM_LIKELY(!isAnnotationDiagEnabled(I.getContext()))) return; if (IsFrontendInstr) { - // TODO: Fronend checking will have to be thought through, since we need + // TODO: Frontend checking will have to be thought through, since we need // to do the check on branches that don't have expect intrinsics // auto RealWeightsOpt = extractWeights(&I, I.getContext());