Skip to content

Commit 52cd997

Browse files
committed
[clang] Add flag to experiment with cold function attributes
To be removed and promoted to a proper driver flag if experiments turn out fruitful. Original LLVM patch for this functionality: llvm#69030
1 parent 1c5d547 commit 52cd997

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
104104
"sanitizer-early-opt-ep", cl::Optional,
105105
cl::desc("Insert sanitizers on OptimizerEarlyEP."));
106106

107+
// Experiment to mark cold functions as optsize/minsize/optnone.
108+
// TODO: remove once this is exposed as a proper driver flag.
109+
static cl::opt<PGOOptions::ColdFuncOpt> ClPGOColdFuncAttr(
110+
"pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), cl::Hidden,
111+
cl::desc(
112+
"Function attribute to apply to cold functions as determined by PGO"),
113+
cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default",
114+
"Default (no attribute)"),
115+
clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize",
116+
"Mark cold functions with optsize."),
117+
clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize",
118+
"Mark cold functions with minsize."),
119+
clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone",
120+
"Mark cold functions with optnone.")));
121+
107122
extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
108123

109124
// Re-link builtin bitcodes after optimization
@@ -768,42 +783,41 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
768783
CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
769784
: CodeGenOpts.InstrProfileOutput,
770785
"", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
771-
PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
786+
PGOOptions::NoCSAction, ClPGOColdFuncAttr,
772787
CodeGenOpts.DebugInfoForProfiling,
773788
/*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
774789
else if (CodeGenOpts.hasProfileIRUse()) {
775790
// -fprofile-use.
776791
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
777792
: PGOOptions::NoCSAction;
778-
PGOOpt = PGOOptions(
779-
CodeGenOpts.ProfileInstrumentUsePath, "",
780-
CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, VFS,
781-
PGOOptions::IRUse, CSAction, PGOOptions::ColdFuncOpt::Default,
782-
CodeGenOpts.DebugInfoForProfiling);
793+
PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
794+
CodeGenOpts.ProfileRemappingFile,
795+
CodeGenOpts.MemoryProfileUsePath, VFS,
796+
PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr,
797+
CodeGenOpts.DebugInfoForProfiling);
783798
} else if (!CodeGenOpts.SampleProfileFile.empty())
784799
// -fprofile-sample-use
785800
PGOOpt = PGOOptions(
786801
CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
787802
CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
788-
PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
803+
PGOOptions::NoCSAction, ClPGOColdFuncAttr,
789804
CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
790805
else if (!CodeGenOpts.MemoryProfileUsePath.empty())
791806
// -fmemory-profile-use (without any of the above options)
792807
PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
793808
PGOOptions::NoAction, PGOOptions::NoCSAction,
794-
PGOOptions::ColdFuncOpt::Default,
795-
CodeGenOpts.DebugInfoForProfiling);
809+
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
796810
else if (CodeGenOpts.PseudoProbeForProfiling)
797811
// -fpseudo-probe-for-profiling
798-
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
799-
PGOOptions::NoAction, PGOOptions::NoCSAction,
800-
PGOOptions::ColdFuncOpt::Default,
801-
CodeGenOpts.DebugInfoForProfiling, true);
812+
PGOOpt =
813+
PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
814+
PGOOptions::NoAction, PGOOptions::NoCSAction,
815+
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true);
802816
else if (CodeGenOpts.DebugInfoForProfiling)
803817
// -fdebug-info-for-profiling
804818
PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
805819
PGOOptions::NoAction, PGOOptions::NoCSAction,
806-
PGOOptions::ColdFuncOpt::Default, true);
820+
ClPGOColdFuncAttr, true);
807821

808822
// Check to see if we want to generate a CS profile.
809823
if (CodeGenOpts.hasProfileCSIRInstr()) {
@@ -820,14 +834,13 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
820834
: CodeGenOpts.InstrProfileOutput;
821835
PGOOpt->CSAction = PGOOptions::CSIRInstr;
822836
} else
823-
PGOOpt =
824-
PGOOptions("",
825-
CodeGenOpts.InstrProfileOutput.empty()
826-
? getDefaultProfileGenName()
827-
: CodeGenOpts.InstrProfileOutput,
828-
"", /*MemoryProfile=*/"", nullptr, PGOOptions::NoAction,
829-
PGOOptions::CSIRInstr, PGOOptions::ColdFuncOpt::Default,
830-
CodeGenOpts.DebugInfoForProfiling);
837+
PGOOpt = PGOOptions("",
838+
CodeGenOpts.InstrProfileOutput.empty()
839+
? getDefaultProfileGenName()
840+
: CodeGenOpts.InstrProfileOutput,
841+
"", /*MemoryProfile=*/"", nullptr,
842+
PGOOptions::NoAction, PGOOptions::CSIRInstr,
843+
ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
831844
}
832845
if (TM)
833846
TM->setPGOOption(PGOOpt);

0 commit comments

Comments
 (0)