[SampleFDO] Place the discriminator flag variable into the used list.
authorRong Xu <xur@google.com>
Wed, 16 Jun 2021 04:31:27 +0000 (21:31 -0700)
committerRong Xu <xur@google.com>
Wed, 16 Jun 2021 04:51:04 +0000 (21:51 -0700)
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

llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h
llvm/lib/CodeGen/MIRFSDiscriminator.cpp
llvm/lib/Transforms/Utils/AddDiscriminators.cpp
llvm/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp

index 91186cb..a621cb3 100644 (file)
 #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
 
index b8d7d30..bf78594 100644 (file)
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
 #include <unordered_map>
 
 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");
   }
 
index 0908b36..e789194 100644 (file)
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils.h"
+#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
 #include <utility>
 
 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<StringRef, unsigned>;
index d2cd0de..6d995cf 100644 (file)
@@ -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