@@ -28,6 +28,10 @@ static cl::opt<bool>
28
28
AnnotateSelect (" profcheck-annotate-select" , cl::init(true ),
29
29
cl::desc(" Also inject (if missing) and verify MD_prof for "
30
30
" `select` instructions" ));
31
+ static cl::opt<bool >
32
+ WeightsForTest (" profcheck-weights-for-test" , cl::init(false ),
33
+ cl::desc(" Generate weights with small values for tests." ));
34
+
31
35
static cl::opt<uint32_t > SelectTrueWeight (
32
36
" profcheck-default-select-true-weight" , cl::init(2U ),
33
37
cl::desc(" When annotating `select` instructions, this value will be used "
@@ -91,6 +95,10 @@ bool ProfileInjector::inject() {
91
95
if (F.getEntryCount (/* AllowSynthetic=*/ true )->getCount () == 0 )
92
96
return false ;
93
97
bool Changed = false ;
98
+ // Cycle through the weights list. If we didn't, tests with more than (say)
99
+ // one conditional branch would have the same !prof metadata on all of them,
100
+ // and numerically that may make for a poor unit test.
101
+ uint32_t WeightsForTestOffset = 0 ;
94
102
for (auto &BB : F) {
95
103
if (AnnotateSelect) {
96
104
for (auto &I : BB) {
@@ -103,38 +111,48 @@ bool ProfileInjector::inject() {
103
111
if (!Term || Term->getMetadata (LLVMContext::MD_prof))
104
112
continue ;
105
113
SmallVector<BranchProbability> Probs;
106
- Probs.reserve (Term->getNumSuccessors ());
107
- for (auto I = 0U , E = Term->getNumSuccessors (); I < E; ++I)
108
- Probs.emplace_back (BPI.getEdgeProbability (&BB, Term->getSuccessor (I)));
109
114
110
- assert (llvm::find_if (Probs,
111
- [](const BranchProbability &P) {
112
- return P.isUnknown ();
113
- }) == Probs.end () &&
114
- " All branch probabilities should be valid" );
115
- const auto *FirstZeroDenominator =
116
- find_if (Probs, [](const BranchProbability &P) {
117
- return P.getDenominator () == 0 ;
118
- });
119
- (void )FirstZeroDenominator;
120
- assert (FirstZeroDenominator == Probs.end ());
121
- const auto *FirstNonZeroNumerator =
122
- find_if (Probs, [](const BranchProbability &P) { return !P.isZero (); });
123
- assert (FirstNonZeroNumerator != Probs.end ());
124
- DynamicAPInt LCM (Probs[0 ].getDenominator ());
125
- DynamicAPInt GCD (FirstNonZeroNumerator->getNumerator ());
126
- for (const auto &Prob : drop_begin (Probs)) {
127
- if (!Prob.getNumerator ())
128
- continue ;
129
- LCM = llvm::lcm (LCM, DynamicAPInt (Prob.getDenominator ()));
130
- GCD = llvm::gcd (GCD, DynamicAPInt (Prob.getNumerator ()));
131
- }
132
115
SmallVector<uint32_t > Weights;
133
116
Weights.reserve (Term->getNumSuccessors ());
134
- for (const auto &Prob : Probs) {
135
- DynamicAPInt W =
136
- (Prob.getNumerator () * LCM / GCD) / Prob.getDenominator ();
137
- Weights.emplace_back (static_cast <uint32_t >((int64_t )W));
117
+ if (WeightsForTest) {
118
+ static const std::array Primes{3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 ,
119
+ 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 };
120
+ for (uint32_t I = 0 , E = Term->getNumSuccessors (); I < E; ++I)
121
+ Weights.emplace_back (
122
+ Primes[(WeightsForTestOffset + I) % Primes.size ()]);
123
+ ++WeightsForTestOffset;
124
+ } else {
125
+ Probs.reserve (Term->getNumSuccessors ());
126
+ for (auto I = 0U , E = Term->getNumSuccessors (); I < E; ++I)
127
+ Probs.emplace_back (BPI.getEdgeProbability (&BB, Term->getSuccessor (I)));
128
+
129
+ assert (llvm::find_if (Probs,
130
+ [](const BranchProbability &P) {
131
+ return P.isUnknown ();
132
+ }) == Probs.end () &&
133
+ " All branch probabilities should be valid" );
134
+ const auto *FirstZeroDenominator =
135
+ find_if (Probs, [](const BranchProbability &P) {
136
+ return P.getDenominator () == 0 ;
137
+ });
138
+ (void )FirstZeroDenominator;
139
+ assert (FirstZeroDenominator == Probs.end ());
140
+ const auto *FirstNonZeroNumerator = find_if (
141
+ Probs, [](const BranchProbability &P) { return !P.isZero (); });
142
+ assert (FirstNonZeroNumerator != Probs.end ());
143
+ DynamicAPInt LCM (Probs[0 ].getDenominator ());
144
+ DynamicAPInt GCD (FirstNonZeroNumerator->getNumerator ());
145
+ for (const auto &Prob : drop_begin (Probs)) {
146
+ if (!Prob.getNumerator ())
147
+ continue ;
148
+ LCM = llvm::lcm (LCM, DynamicAPInt (Prob.getDenominator ()));
149
+ GCD = llvm::gcd (GCD, DynamicAPInt (Prob.getNumerator ()));
150
+ }
151
+ for (const auto &Prob : Probs) {
152
+ DynamicAPInt W =
153
+ (Prob.getNumerator () * LCM / GCD) / Prob.getDenominator ();
154
+ Weights.emplace_back (static_cast <uint32_t >((int64_t )W));
155
+ }
138
156
}
139
157
setBranchWeights (*Term, Weights, /* IsExpected=*/ false );
140
158
Changed = true ;
0 commit comments