From c09e6935e047cd541d53bb197da562872565394b Mon Sep 17 00:00:00 2001 From: Dehao Chen Date: Fri, 16 Dec 2016 16:48:46 +0000 Subject: [PATCH] Pass sample pgo flags to thinlto. Summary: ThinLTO needs to invoke SampleProfileLoader pass during link time in order to annotate profile correctly after module importing. Reviewers: davidxl, mehdi_amini, tejohnson Subscribers: pcc, davide, llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D27790 llvm-svn: 289957 --- include/llvm/LTO/Config.h | 3 +++ lib/LTO/LTO.cpp | 6 ++++++ lib/LTO/LTOBackend.cpp | 1 + test/tools/gold/X86/Inputs/afdo.prof | 2 ++ test/tools/gold/X86/thinlto_afdo.ll | 25 +++++++++++++++++++++++++ tools/gold/gold-plugin.cpp | 7 +++++++ 6 files changed, 44 insertions(+) create mode 100644 test/tools/gold/X86/Inputs/afdo.prof create mode 100644 test/tools/gold/X86/thinlto_afdo.ll diff --git a/include/llvm/LTO/Config.h b/include/llvm/LTO/Config.h index e162ac3eaa4..3aa48c9f7c2 100644 --- a/include/llvm/LTO/Config.h +++ b/include/llvm/LTO/Config.h @@ -65,6 +65,9 @@ struct Config { /// with this triple. std::string DefaultTriple; + /// Sample PGO profile path. + std::string SampleProfile; + bool ShouldDiscardValueNames = true; DiagnosticHandlerFunction DiagHandler; diff --git a/lib/LTO/LTO.cpp b/lib/LTO/LTO.cpp index 74de6c18aef..09198591da9 100644 --- a/lib/LTO/LTO.cpp +++ b/lib/LTO/LTO.cpp @@ -129,6 +129,12 @@ static void computeCacheKey( ArrayRef((const uint8_t *)&Linkage, sizeof(Linkage))); } + if (!Conf.SampleProfile.empty()) { + auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile); + if (FileOrErr) + Hasher.update(FileOrErr.get()->getBuffer()); + } + Key = toHex(Hasher.result()); } diff --git a/lib/LTO/LTOBackend.cpp b/lib/LTO/LTOBackend.cpp index 87338260998..dbb54f34565 100644 --- a/lib/LTO/LTOBackend.cpp +++ b/lib/LTO/LTOBackend.cpp @@ -182,6 +182,7 @@ static void runOldPMPasses(Config &Conf, Module &Mod, TargetMachine *TM, PMB.LoopVectorize = true; PMB.SLPVectorize = true; PMB.OptLevel = Conf.OptLevel; + PMB.PGOSampleUse = Conf.SampleProfile; if (IsThinLTO) PMB.populateThinLTOPassManager(passes); else diff --git a/test/tools/gold/X86/Inputs/afdo.prof b/test/tools/gold/X86/Inputs/afdo.prof new file mode 100644 index 00000000000..8ca8b25d77d --- /dev/null +++ b/test/tools/gold/X86/Inputs/afdo.prof @@ -0,0 +1,2 @@ +f:100:3 + 1: 100 diff --git a/test/tools/gold/X86/thinlto_afdo.ll b/test/tools/gold/X86/thinlto_afdo.ll new file mode 100644 index 00000000000..083f89d7740 --- /dev/null +++ b/test/tools/gold/X86/thinlto_afdo.ll @@ -0,0 +1,25 @@ +; Generate summary sections +; RUN: opt -module-summary %s -o %t1.o +; RUN: opt -module-summary %p/Inputs/thinlto.ll -o %t2.o + +; RUN: rm -f %t1.o.4.opt.bc +; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: --plugin-opt=thinlto \ +; RUN: --plugin-opt=save-temps \ +; RUN: --plugin-opt=sample-profile=%p/Inputs/afdo.prof \ +; RUN: --plugin-opt=jobs=1 \ +; RUN: -shared %t1.o %t2.o -o %t3 +; RUN: opt -S %t1.o.4.opt.bc | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +; CHECK: ProfileSummary +declare void @g(...) +declare void @h(...) + +define void @f() { +entry: + call void (...) @g() + call void (...) @h() + ret void +} diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index ef021034401..50e102b0459 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -171,6 +171,8 @@ namespace options { // Note: This array will contain all plugin options which are not claimed // as plugin exclusive to pass to the code generator. static std::vector extra; + // Sample profile file path + static std::string sample_profile; static void process_plugin_option(const char *opt_) { @@ -220,6 +222,8 @@ namespace options { message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5); } else if (opt == "disable-verify") { DisableVerify = true; + } else if (opt.startswith("sample-profile=")) { + sample_profile= opt.substr(strlen("sample-profile=")); } else { // Save this option to pass to the code generator. // ParseCommandLineOptions() expects argv[0] to be program name. Lazily @@ -728,6 +732,9 @@ static std::unique_ptr createLTO() { break; } + if (!options::sample_profile.empty()) + Conf.SampleProfile = options::sample_profile; + return llvm::make_unique(std::move(Conf), Backend, options::ParallelCodeGenParallelismLevel); }