@@ -28,6 +28,10 @@ static cl::opt<bool>
2828 AnnotateSelect (" profcheck-annotate-select" , cl::init(true ),
2929 cl::desc(" Also inject (if missing) and verify MD_prof for "
3030 " `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+
3135static cl::opt<uint32_t > SelectTrueWeight (
3236 " profcheck-default-select-true-weight" , cl::init(2U ),
3337 cl::desc(" When annotating `select` instructions, this value will be used "
@@ -91,6 +95,10 @@ bool ProfileInjector::inject() {
9195 if (F.getEntryCount (/* AllowSynthetic=*/ true )->getCount () == 0 )
9296 return false ;
9397 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 ;
94102 for (auto &BB : F) {
95103 if (AnnotateSelect) {
96104 for (auto &I : BB) {
@@ -103,38 +111,48 @@ bool ProfileInjector::inject() {
103111 if (!Term || Term->getMetadata (LLVMContext::MD_prof))
104112 continue ;
105113 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)));
109114
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- }
132115 SmallVector<uint32_t > Weights;
133116 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+ }
138156 }
139157 setBranchWeights (*Term, Weights, /* IsExpected=*/ false );
140158 Changed = true ;
0 commit comments