@@ -184,21 +184,48 @@ static cl::opt<unsigned> SampledInstrPeriod(
184184 cl::desc (" Set the profile instrumentation sample period. For each sample "
185185 " period, a fixed number of consecutive samples will be recorded. "
186186 " The number is controlled by 'sampled-instr-burst-duration' flag. "
187- " The default sample period of 65535 is optimized for generating "
187+ " The default sample period of 65536 is optimized for generating "
188188 " efficient code that leverages unsigned integer wrapping in "
189189 " overflow." ),
190- cl::init(65535 ));
190+ cl::init(USHRT_MAX + 1 ));
191191
192192static cl::opt<unsigned > SampledInstrBurstDuration (
193193 " sampled-instr-burst-duration" ,
194194 cl::desc (" Set the profile instrumentation burst duration, which can range "
195- " from 0 to one less than the value of 'sampled-instr-period'. "
196- " This number of samples will be recorded for each "
197- " 'sampled-instr-period' count update. Setting to 1 enables "
198- " simple sampling, in which case it is recommended to set "
199- " 'sampled-instr-period' to a prime number." ),
195+ " from 1 to the value of 'sampled-instr-period'. This number of "
196+ " samples will be recorded for each 'sampled-instr-period' count "
197+ " update. Setting to 1 enables simple sampling, in which case it "
198+ " is recommended to set 'sampled-instr-period' to a prime "
199+ " number." ),
200200 cl::init(200 ));
201201
202+ struct SampledInstrumentationConfig {
203+ unsigned BurstDuration;
204+ unsigned Period;
205+ bool UseShort;
206+ bool IsSimpleSampling;
207+ bool IsFastSampling;
208+ };
209+
210+ static SampledInstrumentationConfig getSampledInstrumentationConfig () {
211+ SampledInstrumentationConfig config;
212+ config.BurstDuration = SampledInstrBurstDuration.getValue ();
213+ config.Period = SampledInstrPeriod.getValue ();
214+ if (config.BurstDuration > config.Period )
215+ report_fatal_error (
216+ " SampledBurstDuration must be less than or equal to SampledPeriod" );
217+ if (config.Period == 0 || config.BurstDuration == 0 )
218+ report_fatal_error (
219+ " SampledPeriod and SampledBurstDuration must be greater than 0" );
220+ config.IsSimpleSampling = (config.BurstDuration == 1 );
221+ // If (BurstDuration == 1 && Period == 65536), generate the simple sampling
222+ // style code.
223+ config.IsFastSampling =
224+ (!config.IsSimpleSampling && config.Period == USHRT_MAX + 1 );
225+ config.UseShort = (config.Period <= USHRT_MAX) || config.IsFastSampling ;
226+ return config;
227+ }
228+
202229using LoadStorePair = std::pair<Instruction *, Instruction *>;
203230
204231static uint64_t getIntModuleFlagOrZero (const Module &M, StringRef Flag) {
@@ -665,7 +692,7 @@ PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
665692// (1) Full burst sampling: We transform:
666693// Increment_Instruction;
667694// to:
668- // if (__llvm_profile_sampling__ < SampledInstrBurstDuration) {
695+ // if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1 ) {
669696// Increment_Instruction;
670697// }
671698// __llvm_profile_sampling__ += 1;
@@ -680,14 +707,14 @@ PreservedAnalyses InstrProfilingLoweringPass::run(Module &M,
680707// "__llvm_profile_sampling__" variable is an unsigned type, meaning it will
681708// wrap around to zero when overflows. In this case, the second check is
682709// unnecessary, so we won't generate check2 when the SampledInstrPeriod is
683- // set to 65535 (64K - 1 ). The code after:
684- // if (__llvm_profile_sampling__ < SampledInstrBurstDuration) {
710+ // set to 65536 (64K). The code after:
711+ // if (__llvm_profile_sampling__ <= SampledInstrBurstDuration - 1 ) {
685712// Increment_Instruction;
686713// }
687714// __llvm_profile_sampling__ += 1;
688715//
689716// (3) Simple sampling:
690- // When SampledInstrBurstDuration sets to 1, we do a simple sampling:
717+ // When SampledInstrBurstDuration is set to 1, we do a simple sampling:
691718// __llvm_profile_sampling__ += 1;
692719// if (__llvm_profile_sampling__ >= SampledInstrPeriod) {
693720// __llvm_profile_sampling__ = 0;
@@ -706,27 +733,16 @@ void InstrLowerer::doSampling(Instruction *I) {
706733 if (!isSamplingEnabled ())
707734 return ;
708735
709- unsigned SampledBurstDuration = SampledInstrBurstDuration.getValue ();
710- unsigned SampledPeriod = SampledInstrPeriod.getValue ();
711- if (SampledBurstDuration >= SampledPeriod) {
712- report_fatal_error (
713- " SampledPeriod needs to be greater than SampledBurstDuration" );
714- }
715- bool UseShort = (SampledPeriod <= USHRT_MAX);
716- bool IsSimpleSampling = (SampledBurstDuration == 1 );
717- // If (SampledBurstDuration == 1 && SampledPeriod == 65535), generate
718- // the simple sampling style code.
719- bool IsFastSampling = (!IsSimpleSampling && SampledPeriod == 65535 );
720-
721- auto GetConstant = [UseShort](IRBuilder<> &Builder, uint32_t C) {
722- if (UseShort)
736+ SampledInstrumentationConfig config = getSampledInstrumentationConfig ();
737+ auto GetConstant = [&config](IRBuilder<> &Builder, uint32_t C) {
738+ if (config.UseShort )
723739 return Builder.getInt16 (C);
724740 else
725741 return Builder.getInt32 (C);
726742 };
727743
728744 IntegerType *SamplingVarTy;
729- if (UseShort)
745+ if (config. UseShort )
730746 SamplingVarTy = Type::getInt16Ty (M.getContext ());
731747 else
732748 SamplingVarTy = Type::getInt32Ty (M.getContext ());
@@ -741,18 +757,18 @@ void InstrLowerer::doSampling(Instruction *I) {
741757 MDNode *BranchWeight;
742758 IRBuilder<> CondBuilder (I);
743759 auto *LoadSamplingVar = CondBuilder.CreateLoad (SamplingVarTy, SamplingVar);
744- if (IsSimpleSampling) {
760+ if (config. IsSimpleSampling ) {
745761 // For the simple sampling, just create the load and increments.
746762 IRBuilder<> IncBuilder (I);
747763 NewSamplingVarVal =
748764 IncBuilder.CreateAdd (LoadSamplingVar, GetConstant (IncBuilder, 1 ));
749765 SamplingVarIncr = IncBuilder.CreateStore (NewSamplingVarVal, SamplingVar);
750766 } else {
751- // For the bust -sampling, create the conditonal update.
767+ // For the burst -sampling, create the conditional update.
752768 auto *DurationCond = CondBuilder.CreateICmpULE (
753- LoadSamplingVar, GetConstant (CondBuilder, SampledBurstDuration ));
769+ LoadSamplingVar, GetConstant (CondBuilder, config. BurstDuration - 1 ));
754770 BranchWeight = MDB.createBranchWeights (
755- SampledBurstDuration, SampledPeriod + 1 - SampledBurstDuration );
771+ config. BurstDuration , config. Period - config. BurstDuration );
756772 Instruction *ThenTerm = SplitBlockAndInsertIfThen (
757773 DurationCond, I, /* Unreachable */ false , BranchWeight);
758774 IRBuilder<> IncBuilder (I);
@@ -762,20 +778,20 @@ void InstrLowerer::doSampling(Instruction *I) {
762778 I->moveBefore (ThenTerm);
763779 }
764780
765- if (IsFastSampling)
781+ if (config. IsFastSampling )
766782 return ;
767783
768- // Create the condtion for checking the period.
784+ // Create the condition for checking the period.
769785 Instruction *ThenTerm, *ElseTerm;
770786 IRBuilder<> PeriodCondBuilder (SamplingVarIncr);
771787 auto *PeriodCond = PeriodCondBuilder.CreateICmpUGE (
772- NewSamplingVarVal, GetConstant (PeriodCondBuilder, SampledPeriod ));
773- BranchWeight = MDB.createBranchWeights (1 , SampledPeriod );
788+ NewSamplingVarVal, GetConstant (PeriodCondBuilder, config. Period ));
789+ BranchWeight = MDB.createBranchWeights (1 , config. Period - 1 );
774790 SplitBlockAndInsertIfThenElse (PeriodCond, SamplingVarIncr, &ThenTerm,
775791 &ElseTerm, BranchWeight);
776792
777793 // For the simple sampling, the counter update happens in sampling var reset.
778- if (IsSimpleSampling)
794+ if (config. IsSimpleSampling )
779795 I->moveBefore (ThenTerm);
780796
781797 IRBuilder<> ResetBuilder (ThenTerm);
@@ -2138,7 +2154,7 @@ void createProfileSamplingVar(Module &M) {
21382154 const StringRef VarName (INSTR_PROF_QUOTE (INSTR_PROF_PROFILE_SAMPLING_VAR));
21392155 IntegerType *SamplingVarTy;
21402156 Constant *ValueZero;
2141- if (SampledInstrPeriod. getValue () <= USHRT_MAX ) {
2157+ if (getSampledInstrumentationConfig (). UseShort ) {
21422158 SamplingVarTy = Type::getInt16Ty (M.getContext ());
21432159 ValueZero = Constant::getIntegerValue (SamplingVarTy, APInt (16 , 0 ));
21442160 } else {
0 commit comments