llvm.org GIT mirror llvm / 91ffe00
[InlineCost] Refactor the checks for different analyses to be a bit more localized to the code that uses those analyses. Technically, this can change behavior as we no longer require the existence of the ProfileSummaryInfo analysis to use local profile information via BFI. We didn't actually require the PSI to have an interesting profile though, so this only really impacts the behavior in non-default pass pipelines. IMO, this makes it substantially less surprising how everything works -- before an analysis that wasn't actually used had to exist to trigger *any* profile aware inlining. I think the new organization makes it more obvious where various checks for profile signals happen. Differential Revision: https://reviews.llvm.org/D36710 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310888 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
2 changed file(s) with 65 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
658658 bool CallAnalyzer::isColdCallSite(CallSite CS, BlockFrequencyInfo *CallerBFI) {
659659 // If global profile summary is available, then callsite's coldness is
660660 // determined based on that.
661 if (PSI->hasProfileSummary())
661 if (PSI && PSI->hasProfileSummary())
662662 return PSI->isColdCallSite(CS, CallerBFI);
663
664 // Otherwise we need BFI to be available.
663665 if (!CallerBFI)
664666 return false;
665667
666 // In the absence of global profile summary, determine if the callsite is cold
667 // relative to caller's entry. We could potentially cache the computation of
668 // scaled entry frequency, but the added complexity is not worth it unless
669 // this scaling shows up high in the profiles.
668 // Determine if the callsite is cold relative to caller's entry. We could
669 // potentially cache the computation of scaled entry frequency, but the added
670 // complexity is not worth it unless this scaling shows up high in the
671 // profiles.
670672 const BranchProbability ColdProb(ColdCallSiteRelFreq, 100);
671673 auto CallSiteBB = CS.getInstruction()->getParent();
672674 auto CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB);
678680 Optional
679681 CallAnalyzer::getHotCallSiteThreshold(CallSite CS,
680682 BlockFrequencyInfo *CallerBFI) {
683
681684 // If global profile summary is available, then callsite's hotness is
682685 // determined based on that.
683
684 auto HotCallSiteThreshold = Params.HotCallSiteThreshold;
685 if (PSI->hasProfileSummary() && PSI->isHotCallSite(CS, CallerBFI))
686 return HotCallSiteThreshold;
687 if (!CallerBFI)
686 if (PSI && PSI->hasProfileSummary() && PSI->isHotCallSite(CS, CallerBFI))
687 return Params.HotCallSiteThreshold;
688
689 // Otherwise we need BFI to be available and to have a locally hot callsite
690 // threshold.
691 if (!CallerBFI || !Params.LocallyHotCallSiteThreshold)
688692 return None;
689693
690 HotCallSiteThreshold = Params.LocallyHotCallSiteThreshold;
691 if (!HotCallSiteThreshold)
692 return None;
693
694 // In the absence of global profile summary, determine if the callsite is hot
695 // relative to caller's entry. We could potentially cache the computation of
696 // scaled entry frequency, but the added complexity is not worth it unless
697 // this scaling shows up high in the profiles.
694 // Determine if the callsite is hot relative to caller's entry. We could
695 // potentially cache the computation of scaled entry frequency, but the added
696 // complexity is not worth it unless this scaling shows up high in the
697 // profiles.
698698 auto CallSiteBB = CS.getInstruction()->getParent();
699699 auto CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB).getFrequency();
700700 auto CallerEntryFreq = CallerBFI->getEntryFreq();
701701 if (CallSiteFreq >= CallerEntryFreq * HotCallSiteRelFreq)
702 return HotCallSiteThreshold;
702 return Params.LocallyHotCallSiteThreshold;
703
704 // Otherwise treat it normally.
703705 return None;
704706 }
705707
772774 if (!Caller->optForMinSize()) {
773775 if (Callee.hasFnAttribute(Attribute::InlineHint))
774776 Threshold = MaxIfValid(Threshold, Params.HintThreshold);
775 if (PSI) {
776 BlockFrequencyInfo *CallerBFI = GetBFI ? &((*GetBFI)(*Caller)) : nullptr;
777 // FIXME: After switching to the new passmanager, simplify the logic below
778 // by checking only the callsite hotness/coldness. The check for CallerBFI
779 // exists only because we do not have BFI available with the old PM.
780 //
781 // Use callee's hotness information only if we have no way of determining
782 // callsite's hotness information. Callsite hotness can be determined if
783 // sample profile is used (which adds hotness metadata to calls) or if
784 // caller's BlockFrequencyInfo is available.
785 if (CallerBFI || PSI->hasSampleProfile()) {
786 auto HotCallSiteThreshold = getHotCallSiteThreshold(CS, CallerBFI);
787 if (!Caller->optForSize() && HotCallSiteThreshold) {
788 DEBUG(dbgs() << "Hot callsite.\n");
789 // FIXME: This should update the threshold only if it exceeds the
790 // current threshold, but AutoFDO + ThinLTO currently relies on this
791 // behavior to prevent inlining of hot callsites during ThinLTO
792 // compile phase.
793 Threshold = HotCallSiteThreshold.getValue();
794 } else if (isColdCallSite(CS, CallerBFI)) {
795 DEBUG(dbgs() << "Cold callsite.\n");
796 // Do not apply bonuses for a cold callsite including the
797 // LastCallToStatic bonus. While this bonus might result in code size
798 // reduction, it can cause the size of a non-cold caller to increase
799 // preventing it from being inlined.
800 DisallowAllBonuses();
801 Threshold = MinIfValid(Threshold, Params.ColdCallSiteThreshold);
802 }
803 } else {
804 if (PSI->isFunctionEntryHot(&Callee)) {
805 DEBUG(dbgs() << "Hot callee.\n");
806 // If callsite hotness can not be determined, we may still know
807 // that the callee is hot and treat it as a weaker hint for threshold
808 // increase.
809 Threshold = MaxIfValid(Threshold, Params.HintThreshold);
810 } else if (PSI->isFunctionEntryCold(&Callee)) {
811 DEBUG(dbgs() << "Cold callee.\n");
812 // Do not apply bonuses for a cold callee including the
813 // LastCallToStatic bonus. While this bonus might result in code size
814 // reduction, it can cause the size of a non-cold caller to increase
815 // preventing it from being inlined.
816 DisallowAllBonuses();
817 Threshold = MinIfValid(Threshold, Params.ColdThreshold);
818 }
777
778 // FIXME: After switching to the new passmanager, simplify the logic below
779 // by checking only the callsite hotness/coldness as we will reliably
780 // have local profile information.
781 //
782 // Callsite hotness and coldness can be determined if sample profile is
783 // used (which adds hotness metadata to calls) or if caller's
784 // BlockFrequencyInfo is available.
785 BlockFrequencyInfo *CallerBFI = GetBFI ? &((*GetBFI)(*Caller)) : nullptr;
786 auto HotCallSiteThreshold = getHotCallSiteThreshold(CS, CallerBFI);
787 if (!Caller->optForSize() && HotCallSiteThreshold) {
788 DEBUG(dbgs() << "Hot callsite.\n");
789 // FIXME: This should update the threshold only if it exceeds the
790 // current threshold, but AutoFDO + ThinLTO currently relies on this
791 // behavior to prevent inlining of hot callsites during ThinLTO
792 // compile phase.
793 Threshold = HotCallSiteThreshold.getValue();
794 } else if (isColdCallSite(CS, CallerBFI)) {
795 DEBUG(dbgs() << "Cold callsite.\n");
796 // Do not apply bonuses for a cold callsite including the
797 // LastCallToStatic bonus. While this bonus might result in code size
798 // reduction, it can cause the size of a non-cold caller to increase
799 // preventing it from being inlined.
800 DisallowAllBonuses();
801 Threshold = MinIfValid(Threshold, Params.ColdCallSiteThreshold);
802 } else if (PSI) {
803 // Use callee's global profile information only if we have no way of
804 // determining this via callsite information.
805 if (PSI->isFunctionEntryHot(&Callee)) {
806 DEBUG(dbgs() << "Hot callee.\n");
807 // If callsite hotness can not be determined, we may still know
808 // that the callee is hot and treat it as a weaker hint for threshold
809 // increase.
810 Threshold = MaxIfValid(Threshold, Params.HintThreshold);
811 } else if (PSI->isFunctionEntryCold(&Callee)) {
812 DEBUG(dbgs() << "Cold callee.\n");
813 // Do not apply bonuses for a cold callee including the
814 // LastCallToStatic bonus. While this bonus might result in code size
815 // reduction, it can cause the size of a non-cold caller to increase
816 // preventing it from being inlined.
817 DisallowAllBonuses();
818 Threshold = MinIfValid(Threshold, Params.ColdThreshold);
819819 }
820820 }
821821 }
None ; RUN: opt -S -inline < %s | FileCheck %s
1 ; RUN: opt -S -passes='cgscc(inline)' < %s | FileCheck %s
0 ; RUN: opt -S -inline -inline-threshold=100 -inline-cold-callsite-threshold=100 < %s | FileCheck %s
1 ; RUN: opt -S -passes='cgscc(inline)' -inline-threshold=100 -inline-cold-callsite-threshold=100 < %s | FileCheck %s
22 target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
33 target triple = "i686-pc-windows-msvc18.0.0"
44