1fe6060f1SDimitry Andric //===- SampleProfileLoaderBaseUtil.cpp - Profile loader Util func ---------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric // 9fe6060f1SDimitry Andric // This file implements the SampleProfileLoader base utility functions. 10fe6060f1SDimitry Andric // 11fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 12fe6060f1SDimitry Andric 13fe6060f1SDimitry Andric #include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h" 14fe6060f1SDimitry Andric 15fe6060f1SDimitry Andric namespace llvm { 16fe6060f1SDimitry Andric 17fe6060f1SDimitry Andric cl::opt<unsigned> SampleProfileMaxPropagateIterations( 18fe6060f1SDimitry Andric "sample-profile-max-propagate-iterations", cl::init(100), 19fe6060f1SDimitry Andric cl::desc("Maximum number of iterations to go through when propagating " 20fe6060f1SDimitry Andric "sample block/edge weights through the CFG.")); 21fe6060f1SDimitry Andric 22fe6060f1SDimitry Andric cl::opt<unsigned> SampleProfileRecordCoverage( 23fe6060f1SDimitry Andric "sample-profile-check-record-coverage", cl::init(0), cl::value_desc("N"), 24fe6060f1SDimitry Andric cl::desc("Emit a warning if less than N% of records in the input profile " 25fe6060f1SDimitry Andric "are matched to the IR.")); 26fe6060f1SDimitry Andric 27fe6060f1SDimitry Andric cl::opt<unsigned> SampleProfileSampleCoverage( 28fe6060f1SDimitry Andric "sample-profile-check-sample-coverage", cl::init(0), cl::value_desc("N"), 29fe6060f1SDimitry Andric cl::desc("Emit a warning if less than N% of samples in the input profile " 30fe6060f1SDimitry Andric "are matched to the IR.")); 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric cl::opt<bool> NoWarnSampleUnused( 33fe6060f1SDimitry Andric "no-warn-sample-unused", cl::init(false), cl::Hidden, 34fe6060f1SDimitry Andric cl::desc("Use this option to turn off/on warnings about function with " 35fe6060f1SDimitry Andric "samples but without debug information to use those samples. ")); 36fe6060f1SDimitry Andric 37*4824e7fdSDimitry Andric cl::opt<bool> SampleProfileUseProfi( 38*4824e7fdSDimitry Andric "sample-profile-use-profi", cl::init(false), cl::Hidden, cl::ZeroOrMore, 39*4824e7fdSDimitry Andric cl::desc("Use profi to infer block and edge counts.")); 40*4824e7fdSDimitry Andric 41fe6060f1SDimitry Andric namespace sampleprofutil { 42fe6060f1SDimitry Andric 43fe6060f1SDimitry Andric /// Return true if the given callsite is hot wrt to hot cutoff threshold. 44fe6060f1SDimitry Andric /// 45fe6060f1SDimitry Andric /// Functions that were inlined in the original binary will be represented 46fe6060f1SDimitry Andric /// in the inline stack in the sample profile. If the profile shows that 47fe6060f1SDimitry Andric /// the original inline decision was "good" (i.e., the callsite is executed 48fe6060f1SDimitry Andric /// frequently), then we will recreate the inline decision and apply the 49fe6060f1SDimitry Andric /// profile from the inlined callsite. 50fe6060f1SDimitry Andric /// 51fe6060f1SDimitry Andric /// To decide whether an inlined callsite is hot, we compare the callsite 52fe6060f1SDimitry Andric /// sample count with the hot cutoff computed by ProfileSummaryInfo, it is 53fe6060f1SDimitry Andric /// regarded as hot if the count is above the cutoff value. 54fe6060f1SDimitry Andric /// 55fe6060f1SDimitry Andric /// When ProfileAccurateForSymsInList is enabled and profile symbol list 56fe6060f1SDimitry Andric /// is present, functions in the profile symbol list but without profile will 57fe6060f1SDimitry Andric /// be regarded as cold and much less inlining will happen in CGSCC inlining 58fe6060f1SDimitry Andric /// pass, so we tend to lower the hot criteria here to allow more early 59fe6060f1SDimitry Andric /// inlining to happen for warm callsites and it is helpful for performance. 60fe6060f1SDimitry Andric bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, 61fe6060f1SDimitry Andric bool ProfAccForSymsInList) { 62fe6060f1SDimitry Andric if (!CallsiteFS) 63fe6060f1SDimitry Andric return false; // The callsite was not inlined in the original binary. 64fe6060f1SDimitry Andric 65fe6060f1SDimitry Andric assert(PSI && "PSI is expected to be non null"); 66fe6060f1SDimitry Andric uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples(); 67fe6060f1SDimitry Andric if (ProfAccForSymsInList) 68fe6060f1SDimitry Andric return !PSI->isColdCount(CallsiteTotalSamples); 69fe6060f1SDimitry Andric else 70fe6060f1SDimitry Andric return PSI->isHotCount(CallsiteTotalSamples); 71fe6060f1SDimitry Andric } 72fe6060f1SDimitry Andric 73fe6060f1SDimitry Andric /// Mark as used the sample record for the given function samples at 74fe6060f1SDimitry Andric /// (LineOffset, Discriminator). 75fe6060f1SDimitry Andric /// 76fe6060f1SDimitry Andric /// \returns true if this is the first time we mark the given record. 77fe6060f1SDimitry Andric bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS, 78fe6060f1SDimitry Andric uint32_t LineOffset, 79fe6060f1SDimitry Andric uint32_t Discriminator, 80fe6060f1SDimitry Andric uint64_t Samples) { 81fe6060f1SDimitry Andric LineLocation Loc(LineOffset, Discriminator); 82fe6060f1SDimitry Andric unsigned &Count = SampleCoverage[FS][Loc]; 83fe6060f1SDimitry Andric bool FirstTime = (++Count == 1); 84fe6060f1SDimitry Andric if (FirstTime) 85fe6060f1SDimitry Andric TotalUsedSamples += Samples; 86fe6060f1SDimitry Andric return FirstTime; 87fe6060f1SDimitry Andric } 88fe6060f1SDimitry Andric 89fe6060f1SDimitry Andric /// Return the number of sample records that were applied from this profile. 90fe6060f1SDimitry Andric /// 91fe6060f1SDimitry Andric /// This count does not include records from cold inlined callsites. 92fe6060f1SDimitry Andric unsigned 93fe6060f1SDimitry Andric SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS, 94fe6060f1SDimitry Andric ProfileSummaryInfo *PSI) const { 95fe6060f1SDimitry Andric auto I = SampleCoverage.find(FS); 96fe6060f1SDimitry Andric 97fe6060f1SDimitry Andric // The size of the coverage map for FS represents the number of records 98fe6060f1SDimitry Andric // that were marked used at least once. 99fe6060f1SDimitry Andric unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0; 100fe6060f1SDimitry Andric 101fe6060f1SDimitry Andric // If there are inlined callsites in this function, count the samples found 102fe6060f1SDimitry Andric // in the respective bodies. However, do not bother counting callees with 0 103fe6060f1SDimitry Andric // total samples, these are callees that were never invoked at runtime. 104fe6060f1SDimitry Andric for (const auto &I : FS->getCallsiteSamples()) 105fe6060f1SDimitry Andric for (const auto &J : I.second) { 106fe6060f1SDimitry Andric const FunctionSamples *CalleeSamples = &J.second; 107fe6060f1SDimitry Andric if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList)) 108fe6060f1SDimitry Andric Count += countUsedRecords(CalleeSamples, PSI); 109fe6060f1SDimitry Andric } 110fe6060f1SDimitry Andric 111fe6060f1SDimitry Andric return Count; 112fe6060f1SDimitry Andric } 113fe6060f1SDimitry Andric 114fe6060f1SDimitry Andric /// Return the number of sample records in the body of this profile. 115fe6060f1SDimitry Andric /// 116fe6060f1SDimitry Andric /// This count does not include records from cold inlined callsites. 117fe6060f1SDimitry Andric unsigned 118fe6060f1SDimitry Andric SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS, 119fe6060f1SDimitry Andric ProfileSummaryInfo *PSI) const { 120fe6060f1SDimitry Andric unsigned Count = FS->getBodySamples().size(); 121fe6060f1SDimitry Andric 122fe6060f1SDimitry Andric // Only count records in hot callsites. 123fe6060f1SDimitry Andric for (const auto &I : FS->getCallsiteSamples()) 124fe6060f1SDimitry Andric for (const auto &J : I.second) { 125fe6060f1SDimitry Andric const FunctionSamples *CalleeSamples = &J.second; 126fe6060f1SDimitry Andric if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList)) 127fe6060f1SDimitry Andric Count += countBodyRecords(CalleeSamples, PSI); 128fe6060f1SDimitry Andric } 129fe6060f1SDimitry Andric 130fe6060f1SDimitry Andric return Count; 131fe6060f1SDimitry Andric } 132fe6060f1SDimitry Andric 133fe6060f1SDimitry Andric /// Return the number of samples collected in the body of this profile. 134fe6060f1SDimitry Andric /// 135fe6060f1SDimitry Andric /// This count does not include samples from cold inlined callsites. 136fe6060f1SDimitry Andric uint64_t 137fe6060f1SDimitry Andric SampleCoverageTracker::countBodySamples(const FunctionSamples *FS, 138fe6060f1SDimitry Andric ProfileSummaryInfo *PSI) const { 139fe6060f1SDimitry Andric uint64_t Total = 0; 140fe6060f1SDimitry Andric for (const auto &I : FS->getBodySamples()) 141fe6060f1SDimitry Andric Total += I.second.getSamples(); 142fe6060f1SDimitry Andric 143fe6060f1SDimitry Andric // Only count samples in hot callsites. 144fe6060f1SDimitry Andric for (const auto &I : FS->getCallsiteSamples()) 145fe6060f1SDimitry Andric for (const auto &J : I.second) { 146fe6060f1SDimitry Andric const FunctionSamples *CalleeSamples = &J.second; 147fe6060f1SDimitry Andric if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList)) 148fe6060f1SDimitry Andric Total += countBodySamples(CalleeSamples, PSI); 149fe6060f1SDimitry Andric } 150fe6060f1SDimitry Andric 151fe6060f1SDimitry Andric return Total; 152fe6060f1SDimitry Andric } 153fe6060f1SDimitry Andric 154fe6060f1SDimitry Andric /// Return the fraction of sample records used in this profile. 155fe6060f1SDimitry Andric /// 156fe6060f1SDimitry Andric /// The returned value is an unsigned integer in the range 0-100 indicating 157fe6060f1SDimitry Andric /// the percentage of sample records that were used while applying this 158fe6060f1SDimitry Andric /// profile to the associated function. 159fe6060f1SDimitry Andric unsigned SampleCoverageTracker::computeCoverage(unsigned Used, 160fe6060f1SDimitry Andric unsigned Total) const { 161fe6060f1SDimitry Andric assert(Used <= Total && 162fe6060f1SDimitry Andric "number of used records cannot exceed the total number of records"); 163fe6060f1SDimitry Andric return Total > 0 ? Used * 100 / Total : 100; 164fe6060f1SDimitry Andric } 165fe6060f1SDimitry Andric 166fe6060f1SDimitry Andric /// Create a global variable to flag FSDiscriminators are used. 167fe6060f1SDimitry Andric void createFSDiscriminatorVariable(Module *M) { 168fe6060f1SDimitry Andric const char *FSDiscriminatorVar = "__llvm_fs_discriminator__"; 169fe6060f1SDimitry Andric if (M->getGlobalVariable(FSDiscriminatorVar)) 170fe6060f1SDimitry Andric return; 171fe6060f1SDimitry Andric 172fe6060f1SDimitry Andric auto &Context = M->getContext(); 173fe6060f1SDimitry Andric // Place this variable to llvm.used so it won't be GC'ed. 174fe6060f1SDimitry Andric appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true, 175fe6060f1SDimitry Andric GlobalValue::WeakODRLinkage, 176fe6060f1SDimitry Andric ConstantInt::getTrue(Context), 177fe6060f1SDimitry Andric FSDiscriminatorVar)}); 178fe6060f1SDimitry Andric } 179fe6060f1SDimitry Andric 180fe6060f1SDimitry Andric } // end of namespace sampleprofutil 181fe6060f1SDimitry Andric } // end of namespace llvm 182