LLVM: lib/Transforms/Utils/MisExpect.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

41#include

42#include

43#include

44#include

45

46#define DEBUG_TYPE "misexpect"

47

48using namespace llvm;

50

51

52

55 cl::desc("Use this option to turn on/off "

56 "warnings about incorrect usage of llvm.expect intrinsics."));

57

58

60 "misexpect-tolerance", cl::init(0),

61 cl::desc("Prevents emitting diagnostics when profile counts are "

62 "within N% of the threshold.."));

63

67

70 Ctx.getDiagnosticsMisExpectTolerance());

71}

72

74 assert(I != nullptr && "MisExpect target Instruction cannot be nullptr");

78 }

79

80

81

82

83

84

85

86

87

90 }

91 return Ret ? Ret : I;

92}

93

96 double PercentageCorrect = (double)ProfCount / TotalCount;

97 auto PerString =

98 formatv("{0:P} ({1} / {2})", PercentageCorrect, ProfCount, TotalCount);

100 "Potential performance regression from use of the llvm.expect intrinsic: "

101 "Annotation was correct on {0} of profiled executions.",

102 PerString);

108}

109

113

114

115

116

117

120 size_t MaxIndex = 0;

121 for (const auto &[Idx, V] : enumerate(ExpectedWeights)) {

124 MaxIndex = Idx;

125 }

128 }

129

130 const uint64_t ProfiledWeight = RealWeights[MaxIndex];

131 const uint64_t RealWeightsTotal =

132 std::accumulate(RealWeights.begin(), RealWeights.end(), (uint64_t)0,

133 std::plus<uint64_t>());

134 const uint64_t NumUnlikelyTargets = RealWeights.size() - 1;

135

138

139

141 "TotalBranchWeight is less than the Likely branch weight");

142

143

144

145

148

149 uint64_t ScaledThreshold = LikelyProbablilty.scale(RealWeightsTotal);

150

151

153 Tolerance = std::clamp(Tolerance, 0u, 99u);

154

155

156

157 if (Tolerance > 0)

158 ScaledThreshold *= (1.0 - Tolerance / 100.0);

159

160

161 if (ProfiledWeight < ScaledThreshold)

163 RealWeightsTotal);

164}

165

168

169

170

171

172

174 return;

177 return;

179}

180

188

191 bool IsFrontend) {

192 if (IsFrontend)

194 else

196}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

static cl::opt< uint32_t > UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(1), cl::desc("Weight of the branch unlikely to be taken (default = 1)"))

static cl::opt< uint32_t > LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(2000), cl::desc("Weight of the branch likely to be taken (default = 2000)"))

static const Instruction * getInstCondition(const Instruction *I)

Definition MisExpect.cpp:73

static void emitMisexpectDiagnostic(const Instruction *I, LLVMContext &Ctx, uint64_t ProfCount, uint64_t TotalCount)

Definition MisExpect.cpp:94

static uint32_t getMisExpectTolerance(const LLVMContext &Ctx)

Definition MisExpect.cpp:68

static cl::opt< bool > PGOWarnMisExpect("pgo-warn-misexpect", cl::init(false), cl::Hidden, cl::desc("Use this option to turn on/off " "warnings about incorrect usage of llvm.expect intrinsics."))

static cl::opt< uint32_t > MisExpectTolerance("misexpect-tolerance", cl::init(0), cl::desc("Prevents emitting diagnostics when profile counts are " "within N% of the threshold.."))

static bool isMisExpectDiagEnabled(const LLVMContext &Ctx)

Definition MisExpect.cpp:64

This file contains the declarations for profiling metadata utility functions.

const SmallVectorImpl< MachineOperand > & Cond

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

size_t size() const

size - Get the array size.

static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)

Diagnostic information for MisExpect analysis.

This is an important class for using LLVM in a threaded context.

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

initializer< Ty > init(const Ty &Val)

void checkBackendInstrumentation(const Instruction &I, ArrayRef< uint32_t > RealWeights)

checkBackendInstrumentation - compares PGO counters to the thresholds used for llvm....

Definition MisExpect.cpp:166

void verifyMisExpect(const Instruction &I, ArrayRef< uint32_t > RealWeights, ArrayRef< uint32_t > ExpectedWeights)

veryifyMisExpect - compares RealWeights to the thresholds used for llvm.expect and warns if the PGO c...

Definition MisExpect.cpp:110

void checkExpectAnnotations(const Instruction &I, ArrayRef< uint32_t > ExistingWeights, bool IsFrontend)

checkExpectAnnotations - compares PGO counters to the thresholds used for llvm.expect and warns if th...

Definition MisExpect.cpp:189

void checkFrontendInstrumentation(const Instruction &I, ArrayRef< uint32_t > ExpectedWeights)

checkFrontendInstrumentation - compares PGO counters to the thresholds used for llvm....

Definition MisExpect.cpp:181

This is an optimization pass for GlobalISel generic memory operations.

auto enumerate(FirstRange &&First, RestRanges &&...Rest)

Given two or more input ranges, returns a new range whose values are tuples (A, B,...

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

LLVM_ABI bool hasBranchWeightOrigin(const Instruction &I)

Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.

auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)

LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)

Extract branch weights from MD_prof metadata.