From: Qiongsi Wu Date: Wed, 30 Nov 2022 14:57:55 +0000 (-0500) Subject: [AIX][LTO] Enabling Context Sensitive PGO Options X-Git-Tag: upstream/17.0.6~25862 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3faab85c12bc1d96e144dcb6d8f75bcafa120c79;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 30fca3a..cbdb51f 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 e12fa1c..c960d2f 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 1f1c9bb..adc4eae 100644 --- a/llvm/lib/LTO/LTOCodeGenerator.cpp +++ b/llvm/lib/LTO/LTOCodeGenerator.cpp @@ -118,7 +118,15 @@ cl::opt AIXSystemAssemblerPath( "lto-aix-system-assembler", cl::desc("Path to a system assembler, picked up on AIX only"), cl::value_desc("path")); -} + +cl::opt + LTORunCSIRInstr("cs-profile-generate", + cl::desc("Perform context sensitive PGO instrumentation")); + +cl::opt + 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;