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