diff --git a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h --- a/llvm/include/llvm/Analysis/ProfileSummaryInfo.h +++ b/llvm/include/llvm/Analysis/ProfileSummaryInfo.h @@ -88,6 +88,12 @@ Summary->getKind() == ProfileSummary::PSK_CSInstr; } + std::optional getProfileKind() const { + return hasProfileSummary() + ? std::make_optional(Summary->getKind()) + : std::nullopt; + } + /// Handle the invalidation of this information. /// /// When used as a result of \c ProfileSummaryAnalysis this method will be diff --git a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp --- a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp +++ b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp @@ -106,24 +106,36 @@ static bool isColdBlock(const MachineBasicBlock &MBB, const MachineBlockFrequencyInfo *MBFI, - ProfileSummaryInfo *PSI, bool HasAccurateProfile) { + ProfileSummaryInfo *PSI, + ProfileSummary::Kind ProfileKind) { std::optional Count = MBFI->getBlockProfileCount(&MBB); - // If using accurate profile, no count means cold. - // If no accurate profile, no count means "do not judge - // coldness". - if (!Count) - return HasAccurateProfile; - - if (PercentileCutoff > 0) - return PSI->isColdCountNthPercentile(PercentileCutoff, *Count); - return (*Count < ColdCountThreshold); + switch (ProfileKind) { + case ProfileSummary::PSK_Instr: + case ProfileSummary::PSK_CSInstr: + // If using instrument profile, which is deemed "accurate", no count means + // cold. + if (!Count) + return true; + if (PercentileCutoff > 0) + return PSI->isColdCountNthPercentile(PercentileCutoff, *Count); + return (*Count < ColdCountThreshold); + case llvm::ProfileSummary::PSK_Sample: + // For sample profile, no count means "do not judege coldness". + if (!Count) + return false; + return *Count < ColdCountThreshold; + } + return false; } bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) { // We target functions with profile data. Static information in the form // of exception handling code may be split to cold if user passes the // mfs-split-ehcode flag. - bool UseProfileData = MF.getFunction().hasProfileData(); + ProfileSummaryInfo *PSI = + &getAnalysis().getPSI(); + std::optional ProfileKind = PSI->getProfileKind(); + bool UseProfileData = ProfileKind && MF.getFunction().hasProfileData(); if (!UseProfileData && !SplitAllEHCode) return false; @@ -151,23 +163,14 @@ MF.setBBSectionsType(BasicBlockSection::Preset); MachineBlockFrequencyInfo *MBFI = nullptr; - ProfileSummaryInfo *PSI = nullptr; - // Whether this pass is using FSAFDO profile (not accurate) or IRPGO - // (accurate). HasAccurateProfile is only used when UseProfileData is true, - // but giving it a default value to silent any possible warning. - bool HasAccurateProfile = false; if (UseProfileData) { MBFI = &getAnalysis(); - PSI = &getAnalysis().getPSI(); - // "HasAccurateProfile" is false for FSAFDO, true when using IRPGO - // (traditional instrumented FDO) or CSPGO profiles. - HasAccurateProfile = - PSI->hasInstrumentationProfile() || PSI->hasCSInstrumentationProfile(); - // If HasAccurateProfile is false, we only trust hot functions, - // which have many samples, and consider them as split - // candidates. On the other hand, if HasAccurateProfile (likeIRPGO), we - // trust both cold and hot functions. - if (!HasAccurateProfile && !PSI->isFunctionHotInCallGraph(&MF, *MBFI)) { + // If we don't have a good profile (sample profile is not deemed + // as a "good profile") and the function is not hot, then early + // return. (Because we can only trust hot functions when profile + // quality is not good.) + if ((*ProfileKind == ProfileSummary::PSK_Sample && + !PSI->isFunctionHotInCallGraph(&MF, *MBFI))) { // Split all EH code and it's descendant statically by default. if (SplitAllEHCode) setDescendantEHBlocksCold(MF); @@ -183,8 +186,8 @@ if (MBB.isEHPad()) LandingPads.push_back(&MBB); - else if (UseProfileData && - isColdBlock(MBB, MBFI, PSI, HasAccurateProfile) && !SplitAllEHCode) + else if (UseProfileData && isColdBlock(MBB, MBFI, PSI, *ProfileKind) && + !SplitAllEHCode) MBB.setSectionID(MBBSectionID::ColdSectionID); } @@ -196,7 +199,7 @@ // Here we have UseProfileData == true. bool HasHotLandingPads = false; for (const MachineBasicBlock *LP : LandingPads) { - if (!isColdBlock(*LP, MBFI, PSI, HasAccurateProfile)) + if (!isColdBlock(*LP, MBFI, PSI, *ProfileKind)) HasHotLandingPads = true; } if (!HasHotLandingPads) {