aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/MisExpect.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:04 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:11 +0000
commite3b557809604d036af6e00c60f012c2025b59a5e (patch)
tree8a11ba2269a3b669601e2fd41145b174008f4da8 /llvm/lib/Transforms/Utils/MisExpect.cpp
parent08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff)
Diffstat (limited to 'llvm/lib/Transforms/Utils/MisExpect.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/MisExpect.cpp63
1 files changed, 14 insertions, 49 deletions
diff --git a/llvm/lib/Transforms/Utils/MisExpect.cpp b/llvm/lib/Transforms/Utils/MisExpect.cpp
index 4414b04c7264..6f5a25a26821 100644
--- a/llvm/lib/Transforms/Utils/MisExpect.cpp
+++ b/llvm/lib/Transforms/Utils/MisExpect.cpp
@@ -35,10 +35,12 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.h"
+#include <algorithm>
#include <cstdint>
#include <functional>
#include <numeric>
@@ -57,7 +59,7 @@ static cl::opt<bool> PGOWarnMisExpect(
cl::desc("Use this option to turn on/off "
"warnings about incorrect usage of llvm.expect intrinsics."));
-static cl::opt<unsigned> MisExpectTolerance(
+static cl::opt<uint32_t> MisExpectTolerance(
"misexpect-tolerance", cl::init(0),
cl::desc("Prevents emiting diagnostics when profile counts are "
"within N% of the threshold.."));
@@ -70,8 +72,8 @@ bool isMisExpectDiagEnabled(LLVMContext &Ctx) {
return PGOWarnMisExpect || Ctx.getMisExpectWarningRequested();
}
-uint64_t getMisExpectTolerance(LLVMContext &Ctx) {
- return std::max(static_cast<uint64_t>(MisExpectTolerance),
+uint32_t getMisExpectTolerance(LLVMContext &Ctx) {
+ return std::max(static_cast<uint32_t>(MisExpectTolerance),
Ctx.getDiagnosticsMisExpectTolerance());
}
@@ -118,43 +120,6 @@ void emitMisexpectDiagnostic(Instruction *I, LLVMContext &Ctx,
namespace llvm {
namespace misexpect {
-// Helper function to extract branch weights into a vector
-Optional<SmallVector<uint32_t, 4>> extractWeights(Instruction *I,
- LLVMContext &Ctx) {
- assert(I && "MisExpect::extractWeights given invalid pointer");
-
- auto *ProfileData = I->getMetadata(LLVMContext::MD_prof);
- if (!ProfileData)
- return None;
-
- unsigned NOps = ProfileData->getNumOperands();
- if (NOps < 3)
- return None;
-
- auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
- if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
- return None;
-
- SmallVector<uint32_t, 4> Weights(NOps - 1);
- for (unsigned Idx = 1; Idx < NOps; Idx++) {
- ConstantInt *Value =
- mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
- uint32_t V = Value->getZExtValue();
- Weights[Idx - 1] = V;
- }
-
- return Weights;
-}
-
-// TODO: when clang allows c++17, use std::clamp instead
-uint32_t clamp(uint64_t value, uint32_t low, uint32_t hi) {
- if (value > hi)
- return hi;
- if (value < low)
- return low;
- return value;
-}
-
void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
ArrayRef<uint32_t> ExpectedWeights) {
// To determine if we emit a diagnostic, we need to compare the branch weights
@@ -190,6 +155,8 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
// We cannot calculate branch probability if either of these invariants aren't
// met. However, MisExpect diagnostics should not prevent code from compiling,
// so we simply forgo emitting diagnostics here, and return early.
+ // assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0)
+ // && "TotalBranchWeight is less than the Likely branch weight");
if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight))
return;
@@ -203,7 +170,7 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
// clamp tolerance range to [0, 100)
auto Tolerance = getMisExpectTolerance(I.getContext());
- Tolerance = clamp(Tolerance, 0, 99);
+ Tolerance = std::clamp(Tolerance, 0u, 99u);
// Allow users to relax checking by N% i.e., if they use a 5% tolerance,
// then we check against 0.95*ScaledThreshold
@@ -218,26 +185,24 @@ void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
void checkBackendInstrumentation(Instruction &I,
const ArrayRef<uint32_t> RealWeights) {
- auto ExpectedWeightsOpt = extractWeights(&I, I.getContext());
- if (!ExpectedWeightsOpt)
+ SmallVector<uint32_t> ExpectedWeights;
+ if (!extractBranchWeights(I, ExpectedWeights))
return;
- auto ExpectedWeights = ExpectedWeightsOpt.value();
verifyMisExpect(I, RealWeights, ExpectedWeights);
}
void checkFrontendInstrumentation(Instruction &I,
const ArrayRef<uint32_t> ExpectedWeights) {
- auto RealWeightsOpt = extractWeights(&I, I.getContext());
- if (!RealWeightsOpt)
+ SmallVector<uint32_t> RealWeights;
+ if (!extractBranchWeights(I, RealWeights))
return;
- auto RealWeights = RealWeightsOpt.value();
verifyMisExpect(I, RealWeights, ExpectedWeights);
}
void checkExpectAnnotations(Instruction &I,
const ArrayRef<uint32_t> ExistingWeights,
- bool IsFrontendInstr) {
- if (IsFrontendInstr) {
+ bool IsFrontend) {
+ if (IsFrontend) {
checkFrontendInstrumentation(I, ExistingWeights);
} else {
checkBackendInstrumentation(I, ExistingWeights);