[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-19 Thread Arthur Eubanks via cfe-commits

https://github.com/aeubanks closed 
https://github.com/llvm/llvm-project/pull/89298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-18 Thread Mircea Trofin via cfe-commits


@@ -768,42 +783,41 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
 CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
: 
CodeGenOpts.InstrProfileOutput,
 "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
-PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
+PGOOptions::NoCSAction, ClPGOColdFuncAttr,
 CodeGenOpts.DebugInfoForProfiling,
 /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
   else if (CodeGenOpts.hasProfileIRUse()) {
 // -fprofile-use.
 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
 : PGOOptions::NoCSAction;
-PGOOpt = PGOOptions(
-CodeGenOpts.ProfileInstrumentUsePath, "",
-CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, 
VFS,
-PGOOptions::IRUse, CSAction, PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling);
+PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",

mtrofin wrote:

could you do a clang-format, commit and rebase? This and the stuff at line 812 
seems mostly that. Or does adding the `ClPGOCouldFuncAttr` mess up formatting?

https://github.com/llvm/llvm-project/pull/89298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-18 Thread Mircea Trofin via cfe-commits

https://github.com/mtrofin edited 
https://github.com/llvm/llvm-project/pull/89298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-18 Thread Mircea Trofin via cfe-commits

https://github.com/mtrofin approved this pull request.


https://github.com/llvm/llvm-project/pull/89298
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-18 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Arthur Eubanks (aeubanks)


Changes

To be removed and promoted to a proper driver flag if experiments turn out 
fruitful.

For now, this can be experimented with `-mllvm 
-pgo-cold-func-opt=[optsize|minsize|optnone|default] -mllvm 
-enable-pgo-force-function-attrs`.

Original LLVM patch for this functionality: #69030

---
Full diff: https://github.com/llvm/llvm-project/pull/89298.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/BackendUtil.cpp (+35-22) 
- (added) clang/test/CodeGen/pgo-force-function-attrs.ll (+12) 


``diff
diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 6cc00b85664f41..22c3f8642ad8eb 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -104,6 +104,21 @@ static cl::opt ClSanitizeOnOptimizerEarlyEP(
 "sanitizer-early-opt-ep", cl::Optional,
 cl::desc("Insert sanitizers on OptimizerEarlyEP."));
 
+// Experiment to mark cold functions as optsize/minsize/optnone.
+// TODO: remove once this is exposed as a proper driver flag.
+static cl::opt ClPGOColdFuncAttr(
+"pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), 
cl::Hidden,
+cl::desc(
+"Function attribute to apply to cold functions as determined by PGO"),
+cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default",
+  "Default (no attribute)"),
+   clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize",
+  "Mark cold functions with optsize."),
+   clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize",
+  "Mark cold functions with minsize."),
+   clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone",
+  "Mark cold functions with optnone.")));
+
 extern cl::opt ProfileCorrelate;
 
 // Re-link builtin bitcodes after optimization
@@ -768,42 +783,41 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
 CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
: 
CodeGenOpts.InstrProfileOutput,
 "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
-PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
+PGOOptions::NoCSAction, ClPGOColdFuncAttr,
 CodeGenOpts.DebugInfoForProfiling,
 /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
   else if (CodeGenOpts.hasProfileIRUse()) {
 // -fprofile-use.
 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
 : PGOOptions::NoCSAction;
-PGOOpt = PGOOptions(
-CodeGenOpts.ProfileInstrumentUsePath, "",
-CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, 
VFS,
-PGOOptions::IRUse, CSAction, PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling);
+PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
+CodeGenOpts.ProfileRemappingFile,
+CodeGenOpts.MemoryProfileUsePath, VFS,
+PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr,
+CodeGenOpts.DebugInfoForProfiling);
   } else if (!CodeGenOpts.SampleProfileFile.empty())
 // -fprofile-sample-use
 PGOOpt = PGOOptions(
 CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
 CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
-PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
+PGOOptions::NoCSAction, ClPGOColdFuncAttr,
 CodeGenOpts.DebugInfoForProfiling, 
CodeGenOpts.PseudoProbeForProfiling);
   else if (!CodeGenOpts.MemoryProfileUsePath.empty())
 // -fmemory-profile-use (without any of the above options)
 PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
 PGOOptions::NoAction, PGOOptions::NoCSAction,
-PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling);
+ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
   else if (CodeGenOpts.PseudoProbeForProfiling)
 // -fpseudo-probe-for-profiling
-PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
-PGOOptions::NoAction, PGOOptions::NoCSAction,
-PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling, true);
+PGOOpt =
+PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
+   PGOOptions::NoAction, PGOOptions::NoCSAction,
+   ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling, true);
   else if (CodeGenOpts.DebugInfoForProfiling)
 // -fdebug-info-for-profiling
 PGOOpt = PGOOptions("", "", "", 

[clang] [clang] Add flag to experiment with cold function attributes (PR #89298)

2024-04-18 Thread Arthur Eubanks via cfe-commits

https://github.com/aeubanks created 
https://github.com/llvm/llvm-project/pull/89298

To be removed and promoted to a proper driver flag if experiments turn out 
fruitful.

For now, this can be experimented with `-mllvm 
-pgo-cold-func-opt=[optsize|minsize|optnone|default] -mllvm 
-enable-pgo-force-function-attrs`.

Original LLVM patch for this functionality: #69030

>From 1c69510ab5a92998cc443100ecfb551776fc03a0 Mon Sep 17 00:00:00 2001
From: Arthur Eubanks 
Date: Mon, 15 Apr 2024 20:40:43 +
Subject: [PATCH] [clang] Add flag to experiment with cold function attributes

To be removed and promoted to a proper driver flag if experiments turn out 
fruitful.

For now, this can be experimented with `-mllvm 
-pgo-cold-func-opt=[optsize|minsize|optnone|default] -mllvm 
-enable-pgo-force-function-attrs`.

Original LLVM patch for this functionality: #69030
---
 clang/lib/CodeGen/BackendUtil.cpp | 57 ---
 .../test/CodeGen/pgo-force-function-attrs.ll  | 12 
 2 files changed, 47 insertions(+), 22 deletions(-)
 create mode 100644 clang/test/CodeGen/pgo-force-function-attrs.ll

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 6cc00b85664f41..22c3f8642ad8eb 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -104,6 +104,21 @@ static cl::opt ClSanitizeOnOptimizerEarlyEP(
 "sanitizer-early-opt-ep", cl::Optional,
 cl::desc("Insert sanitizers on OptimizerEarlyEP."));
 
+// Experiment to mark cold functions as optsize/minsize/optnone.
+// TODO: remove once this is exposed as a proper driver flag.
+static cl::opt ClPGOColdFuncAttr(
+"pgo-cold-func-opt", cl::init(PGOOptions::ColdFuncOpt::Default), 
cl::Hidden,
+cl::desc(
+"Function attribute to apply to cold functions as determined by PGO"),
+cl::values(clEnumValN(PGOOptions::ColdFuncOpt::Default, "default",
+  "Default (no attribute)"),
+   clEnumValN(PGOOptions::ColdFuncOpt::OptSize, "optsize",
+  "Mark cold functions with optsize."),
+   clEnumValN(PGOOptions::ColdFuncOpt::MinSize, "minsize",
+  "Mark cold functions with minsize."),
+   clEnumValN(PGOOptions::ColdFuncOpt::OptNone, "optnone",
+  "Mark cold functions with optnone.")));
+
 extern cl::opt ProfileCorrelate;
 
 // Re-link builtin bitcodes after optimization
@@ -768,42 +783,41 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
 CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
: 
CodeGenOpts.InstrProfileOutput,
 "", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
-PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
+PGOOptions::NoCSAction, ClPGOColdFuncAttr,
 CodeGenOpts.DebugInfoForProfiling,
 /*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
   else if (CodeGenOpts.hasProfileIRUse()) {
 // -fprofile-use.
 auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
 : PGOOptions::NoCSAction;
-PGOOpt = PGOOptions(
-CodeGenOpts.ProfileInstrumentUsePath, "",
-CodeGenOpts.ProfileRemappingFile, CodeGenOpts.MemoryProfileUsePath, 
VFS,
-PGOOptions::IRUse, CSAction, PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling);
+PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
+CodeGenOpts.ProfileRemappingFile,
+CodeGenOpts.MemoryProfileUsePath, VFS,
+PGOOptions::IRUse, CSAction, ClPGOColdFuncAttr,
+CodeGenOpts.DebugInfoForProfiling);
   } else if (!CodeGenOpts.SampleProfileFile.empty())
 // -fprofile-sample-use
 PGOOpt = PGOOptions(
 CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
 CodeGenOpts.MemoryProfileUsePath, VFS, PGOOptions::SampleUse,
-PGOOptions::NoCSAction, PGOOptions::ColdFuncOpt::Default,
+PGOOptions::NoCSAction, ClPGOColdFuncAttr,
 CodeGenOpts.DebugInfoForProfiling, 
CodeGenOpts.PseudoProbeForProfiling);
   else if (!CodeGenOpts.MemoryProfileUsePath.empty())
 // -fmemory-profile-use (without any of the above options)
 PGOOpt = PGOOptions("", "", "", CodeGenOpts.MemoryProfileUsePath, VFS,
 PGOOptions::NoAction, PGOOptions::NoCSAction,
-PGOOptions::ColdFuncOpt::Default,
-CodeGenOpts.DebugInfoForProfiling);
+ClPGOColdFuncAttr, CodeGenOpts.DebugInfoForProfiling);
   else if (CodeGenOpts.PseudoProbeForProfiling)
 // -fpseudo-probe-for-profiling
-PGOOpt = PGOOptions("", "", "", /*MemoryProfile=*/"", nullptr,
-PGOOptions::NoAction, PGOOptions::NoCSAction,
-