From 022ca8be2887cd1c46be566822d88850d7e11ff5 Mon Sep 17 00:00:00 2001 From: Rong Xu Date: Tue, 15 Jun 2021 21:31:27 -0700 Subject: [PATCH] [SampleFDO] Place the discriminator flag variable into the used list. We create flag variable "__llvm_fs_discriminator__" in the binary to indicate that FSAFDO hierarchical discriminators are used. This variable might be GC'ed by the linker since it is not explicitly reference. I initially added the var to the use list in pass MIRFSDiscriminator but it did not work. It turned out the used global list is collected in lowering (before MIR pass) and then emitted in the end of pass pipeline. Here I add the variable to the use list in IR level's AddDiscriminators pass. The machine level code is still keep in the case IR's AddDiscriminators is not invoked. If this is the case, this just use -Wl,--export-dynamic-symbol=__llvm_fs_discriminator__ to force the emit. Differential Revision: https://reviews.llvm.org/D103988 --- .../Transforms/Utils/SampleProfileLoaderBaseUtil.h | 6 ++++++ lib/CodeGen/MIRFSDiscriminator.cpp | 13 +++---------- lib/Transforms/Utils/AddDiscriminators.cpp | 6 ++++++ .../Utils/SampleProfileLoaderBaseUtil.cpp | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/include/llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h b/include/llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h index 91186cb8cdc..a621cb3078c 100644 --- a/include/llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h +++ b/include/llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h @@ -19,10 +19,12 @@ #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" +#include "llvm/IR/Constants.h" #include "llvm/IR/DebugLoc.h" #include "llvm/IR/Function.h" #include "llvm/ProfileData/SampleProf.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" namespace llvm { using namespace sampleprof; @@ -92,6 +94,10 @@ private: /// Return true if the given callsite is hot wrt to hot cutoff threshold. bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, bool ProfAccForSymsInList); + +/// Create a global variable to flag FSDiscriminators are used. +void createFSDiscriminatorVariable(Module *M); + } // end of namespace sampleprofutil } // end of namespace llvm diff --git a/lib/CodeGen/MIRFSDiscriminator.cpp b/lib/CodeGen/MIRFSDiscriminator.cpp index b8d7d300e1d..bf78594e9b2 100644 --- a/lib/CodeGen/MIRFSDiscriminator.cpp +++ b/lib/CodeGen/MIRFSDiscriminator.cpp @@ -19,10 +19,12 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h" #include using namespace llvm; using namespace sampleprof; +using namespace sampleprofutil; #define DEBUG_TYPE "mirfs-discriminators" @@ -127,16 +129,7 @@ bool MIRAddFSDiscriminators::runOnMachineFunction(MachineFunction &MF) { } if (Changed) { - Module *M = MF.getFunction().getParent(); - const char *FSDiscriminatorVar = "__llvm_fs_discriminator__"; - if (!M->getGlobalVariable(FSDiscriminatorVar)) { - auto &Context = M->getContext(); - // Create a global variable to flag that FSDiscriminators are used. - new GlobalVariable(*M, Type::getInt1Ty(Context), true, - GlobalValue::WeakAnyLinkage, - ConstantInt::getTrue(Context), FSDiscriminatorVar); - } - + createFSDiscriminatorVariable(MF.getFunction().getParent()); LLVM_DEBUG(dbgs() << "Num of FS Discriminators: " << NumNewD << "\n"); } diff --git a/lib/Transforms/Utils/AddDiscriminators.cpp b/lib/Transforms/Utils/AddDiscriminators.cpp index 0908b361a4d..e789194eb3a 100644 --- a/lib/Transforms/Utils/AddDiscriminators.cpp +++ b/lib/Transforms/Utils/AddDiscriminators.cpp @@ -70,9 +70,11 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils.h" +#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h" #include using namespace llvm; +using namespace sampleprofutil; #define DEBUG_TYPE "add-discriminators" @@ -172,6 +174,10 @@ static bool addDiscriminators(Function &F) { if (NoDiscriminators || !F.getSubprogram()) return false; + // Create FSDiscriminatorVariable if flow sensitive discriminators are used. + if (EnableFSDiscriminator) + createFSDiscriminatorVariable(F.getParent()); + bool Changed = false; using Location = std::pair; diff --git a/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp b/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp index d2cd0deb349..6d995cf4c04 100644 --- a/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp +++ b/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp @@ -159,5 +159,19 @@ unsigned SampleCoverageTracker::computeCoverage(unsigned Used, return Total > 0 ? Used * 100 / Total : 100; } +/// Create a global variable to flag FSDiscriminators are used. +void createFSDiscriminatorVariable(Module *M) { + const char *FSDiscriminatorVar = "__llvm_fs_discriminator__"; + if (M->getGlobalVariable(FSDiscriminatorVar)) + return; + + auto &Context = M->getContext(); + // Place this variable to llvm.used so it won't be GC'ed. + appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true, + GlobalValue::WeakODRLinkage, + ConstantInt::getTrue(Context), + FSDiscriminatorVar)}); +} + } // end of namespace sampleprofutil } // end of namespace llvm