Skip to content

Commit

Permalink
[CodeGen] Separate MachineFunctionSplitter logic for different profil…
Browse files Browse the repository at this point in the history
…e types.

In D152577 @xur has a post-submit comment regarding to an awkward usage
of MFS for Autofdo - instead of just using -fsplit-machine-function, the
user needs to add "-mllvm -mfs-psi-cutoff=0" to choose the right logic
for AutoFDO. The compiler should choose the right default values for
such case.

This CL separate MFS logic for different profile types.

Reviewed By: xur, wenlei

Differential Revision: https://reviews.llvm.org/D155253
  • Loading branch information
shenhanc78 committed Jul 18, 2023
1 parent 3f65f71 commit f7f744a
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions llvm/lib/CodeGen/MachineFunctionSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,24 @@ static void finishAdjustingBasicBlocksAndLandingPads(MachineFunction &MF) {

static bool isColdBlock(const MachineBasicBlock &MBB,
const MachineBlockFrequencyInfo *MBFI,
ProfileSummaryInfo *PSI, bool HasAccurateProfile) {
ProfileSummaryInfo *PSI) {
std::optional<uint64_t> 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);
// For instrumentation profiles and sample profiles, we use different ways
// to judge whether a block is cold and should be split.
if (PSI->hasInstrumentationProfile() || PSI->hasCSInstrumentationProfile()) {
// If using instrument profile, which is deemed "accurate", no count means
// cold.
if (!Count)
return true;
if (PercentileCutoff > 0)
return PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
// Fallthrough to end of function.
} else if (PSI->hasSampleProfile()) {
// For sample profile, no count means "do not judege coldness".
if (!Count)
return false;
}

return (*Count < ColdCountThreshold);
}

Expand Down Expand Up @@ -152,22 +160,14 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {

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<MachineBlockFrequencyInfo>();
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().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 (PSI->hasSampleProfile() && !PSI->isFunctionHotInCallGraph(&MF, *MBFI)) {
// Split all EH code and it's descendant statically by default.
if (SplitAllEHCode)
setDescendantEHBlocksCold(MF);
Expand All @@ -183,8 +183,7 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {

if (MBB.isEHPad())
LandingPads.push_back(&MBB);
else if (UseProfileData &&
isColdBlock(MBB, MBFI, PSI, HasAccurateProfile) && !SplitAllEHCode)
else if (UseProfileData && isColdBlock(MBB, MBFI, PSI) && !SplitAllEHCode)
MBB.setSectionID(MBBSectionID::ColdSectionID);
}

Expand All @@ -196,7 +195,7 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
// Here we have UseProfileData == true.
bool HasHotLandingPads = false;
for (const MachineBasicBlock *LP : LandingPads) {
if (!isColdBlock(*LP, MBFI, PSI, HasAccurateProfile))
if (!isColdBlock(*LP, MBFI, PSI))
HasHotLandingPads = true;
}
if (!HasHotLandingPads) {
Expand Down

0 comments on commit f7f744a

Please sign in to comment.