[PATCH] D114319: [InstrProf][NFC] Save profile bias to function map

Ellis Hoag via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 22 09:18:22 PST 2021


ellis created this revision.
Herald added a subscriber: hiraditya.
ellis updated this revision to Diff 388714.
ellis added a comment.
ellis added reviewers: phosek, MaskRay.
ellis published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Run `clang-format`.


Add a map from functions to load instructions that compute the profile bias. Previously we assumed that if the first instruction in the function was a load instruction, then it must be computing the bias. This was likely to work out because functions usually start with the `llvm.instrprof.increment` instruction, but optimizations could change this. For example, inlining into a non-profiled function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114319

Files:
  llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
  llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp


Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -699,10 +699,9 @@
     Type *Int64Ty = Type::getInt64Ty(M->getContext());
     Type *Int64PtrTy = Type::getInt64PtrTy(M->getContext());
     Function *Fn = Inc->getParent()->getParent();
-    Instruction &I = Fn->getEntryBlock().front();
-    LoadInst *LI = dyn_cast<LoadInst>(&I);
-    if (!LI) {
-      IRBuilder<> Builder(&I);
+    auto *&BiasLI = FunctionToProfileBiasLI[Fn];
+    if (!BiasLI) {
+      IRBuilder<> Builder(&Fn->getEntryBlock().front());
       GlobalVariable *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName());
       if (!Bias) {
         // Compiler must define this variable when runtime counter relocation
@@ -719,9 +718,10 @@
         if (TT.supportsCOMDAT())
           Bias->setComdat(M->getOrInsertComdat(Bias->getName()));
       }
-      LI = Builder.CreateLoad(Int64Ty, Bias);
+      BiasLI = Builder.CreateLoad(Int64Ty, Bias);
     }
-    auto *Add = Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), LI);
+    auto *Add =
+        Builder.CreateAdd(Builder.CreatePtrToInt(Addr, Int64Ty), BiasLI);
     Addr = Builder.CreateIntToPtr(Add, Int64PtrTy);
   }
 
Index: llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
===================================================================
--- llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
+++ llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
@@ -57,6 +57,9 @@
     }
   };
   DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
+  /// If runtime relocation is enabled, this maps functions to the load
+  /// instruction that produces the profile relocation bias.
+  DenseMap<const Function *, LoadInst *> FunctionToProfileBiasLI;
   std::vector<GlobalValue *> CompilerUsedVars;
   std::vector<GlobalValue *> UsedVars;
   std::vector<GlobalVariable *> ReferencedNames;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114319.388714.patch
Type: text/x-patch
Size: 2071 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211122/fcfb924f/attachment.bin>


More information about the llvm-commits mailing list