[clang] 3faab85 - [AIX][LTO] Enabling Context Sensitive PGO Options

Qiongsi Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 07:32:16 PST 2022


Author: Qiongsi Wu
Date: 2022-11-30T10:22:26-05:00
New Revision: 3faab85c12bc1d96e144dcb6d8f75bcafa120c79

URL: https://github.com/llvm/llvm-project/commit/3faab85c12bc1d96e144dcb6d8f75bcafa120c79
DIFF: https://github.com/llvm/llvm-project/commit/3faab85c12bc1d96e144dcb6d8f75bcafa120c79.diff

LOG: [AIX][LTO] Enabling Context Sensitive PGO Options

This patch enables context sensitive PGO (CSPGO) for LTO on AIX. Two parts are involved:

  # Frontend logic is added so libLTO can understand the CSPGO related options.
  # Two options are added to the backend so that the LTOCodeGenerator can understand the CSPGO related options and make use of them.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D138854

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/CommonArgs.cpp
    clang/test/Driver/lto-aix.c
    llvm/lib/LTO/LTOCodeGenerator.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 30fca3a4c0590..cbdb51fe9d688 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -519,8 +519,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
   }
 
   const char *PluginOptPrefix = IsOSAIX ? "-bplugin_opt:" : "-plugin-opt=";
-  const char *mcpuOptPrefix = IsOSAIX ? "-mcpu=" : "mcpu=";
-  const char *OptLevelPrefix = IsOSAIX ? "-O" : "O";
+  const char *ExtraDash = IsOSAIX ? "-" : "";
 
   // Note, this solution is far from perfect, better to encode it into IR
   // metadata, but this may not be worth it, since it looks like aranges is on
@@ -537,7 +536,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
   std::string CPU = getCPUName(D, Args, ToolChain.getTriple());
   if (!CPU.empty())
     CmdArgs.push_back(
-        Args.MakeArgString(Twine(PluginOptPrefix) + mcpuOptPrefix + CPU));
+        Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + "mcpu=" + CPU));
 
   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
     // The optimization level matches
@@ -556,7 +555,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
       OOpt = "0";
     if (!OOpt.empty())
       CmdArgs.push_back(
-          Args.MakeArgString(Twine(PluginOptPrefix) + OptLevelPrefix + OOpt));
+          Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + "O" + OOpt));
   }
 
   if (Args.hasArg(options::OPT_gsplit_dwarf))
@@ -650,24 +649,25 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
   auto *ProfileUseArg = getLastProfileUseArg(Args);
 
   if (CSPGOGenerateArg) {
-    CmdArgs.push_back(
-        Args.MakeArgString(Twine(PluginOptPrefix) + "cs-profile-generate"));
+    CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash +
+                                         "cs-profile-generate"));
     if (CSPGOGenerateArg->getOption().matches(
             options::OPT_fcs_profile_generate_EQ)) {
       SmallString<128> Path(CSPGOGenerateArg->getValue());
       llvm::sys::path::append(Path, "default_%m.profraw");
-      CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) +
+      CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash +
                                            "cs-profile-path=" + Path));
     } else
-      CmdArgs.push_back(Args.MakeArgString(
-          Twine(PluginOptPrefix) + "cs-profile-path=default_%m.profraw"));
+      CmdArgs.push_back(
+          Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash +
+                             "cs-profile-path=default_%m.profraw"));
   } else if (ProfileUseArg) {
     SmallString<128> Path(
         ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
     if (Path.empty() || llvm::sys::fs::is_directory(Path))
       llvm::sys::path::append(Path, "default.profdata");
-    CmdArgs.push_back(
-        Args.MakeArgString(Twine(PluginOptPrefix) + "cs-profile-path=" + Path));
+    CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash +
+                                         "cs-profile-path=" + Path));
   }
 
   // This controls whether or not we perform JustMyCode instrumentation.

diff  --git a/clang/test/Driver/lto-aix.c b/clang/test/Driver/lto-aix.c
index e12fa1c3afaee..c960d2f9a7775 100644
--- a/clang/test/Driver/lto-aix.c
+++ b/clang/test/Driver/lto-aix.c
@@ -67,3 +67,9 @@
 //
 // STRICT:       "-bplugin_opt:-strict-dwarf=true"
 // NOSTRICT-NOT: "-bplugin_opt:-strict-dwarf=true"
+//
+// Test cspgo options
+// RUN: %clang --target=powerpc-ibm-aix -### %s -flto -fuse-ld=ld \
+// RUN:   -fcs-profile-generate 2>&1 | FileCheck -check-prefix=CSPGO %s
+//
+// CSPGO: "-bplugin_opt:-cs-profile-generate" "-bplugin_opt:-cs-profile-path=default_%m.profraw"

diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 1f1c9bb5f8960..adc4eae61860a 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -118,7 +118,15 @@ cl::opt<std::string> AIXSystemAssemblerPath(
     "lto-aix-system-assembler",
     cl::desc("Path to a system assembler, picked up on AIX only"),
     cl::value_desc("path"));
-}
+
+cl::opt<bool>
+    LTORunCSIRInstr("cs-profile-generate",
+                    cl::desc("Perform context sensitive PGO instrumentation"));
+
+cl::opt<std::string>
+    LTOCSIRProfile("cs-profile-path",
+                   cl::desc("Context sensitive profile file path"));
+} // namespace llvm
 
 LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
@@ -131,6 +139,9 @@ LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
   Config.PreCodeGenPassesHook = [](legacy::PassManager &PM) {
     PM.add(createObjCARCContractPass());
   };
+
+  Config.RunCSIRInstr = LTORunCSIRInstr;
+  Config.CSIRProfile = LTOCSIRProfile;
 }
 
 LTOCodeGenerator::~LTOCodeGenerator() = default;


        


More information about the cfe-commits mailing list