[PATCH] D150466: profilie inference changes for stale profile matching

Sergey Pupyrev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 12 10:50:09 PDT 2023


spupyrev created this revision.
Herald added subscribers: hoy, wenlei, hiraditya.
Herald added a project: All.
spupyrev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150466

Files:
  llvm/lib/Transforms/Utils/SampleProfileInference.cpp


Index: llvm/lib/Transforms/Utils/SampleProfileInference.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SampleProfileInference.cpp
+++ llvm/lib/Transforms/Utils/SampleProfileInference.cpp
@@ -20,6 +20,7 @@
 #include <queue>
 #include <set>
 #include <stack>
+#include <unordered_set>
 
 using namespace llvm;
 #define DEBUG_TYPE "sample-profile-inference"
@@ -1218,10 +1219,24 @@
 #ifndef NDEBUG
 /// Verify that the provided block/jump weights are as expected.
 void verifyInput(const FlowFunction &Func) {
-  // Verify the entry block
+  // Verify entry and exit blocks
   assert(Func.Entry == 0 && Func.Blocks[0].isEntry());
+  size_t NumExitBlocks = 0;
   for (size_t I = 1; I < Func.Blocks.size(); I++) {
     assert(!Func.Blocks[I].isEntry() && "multiple entry blocks");
+    if (Func.Blocks[I].isExit())
+      NumExitBlocks++;
+  }
+  assert(NumExitBlocks > 0 && "cannot find exit blocks");
+
+  // Verify that there are no parallel edges
+  for (auto &Block : Func.Blocks) {
+    std::unordered_set<uint64_t> UniqueSuccs;
+    for (auto &Jump : Block.SuccJumps) {
+      assert(UniqueSuccs.find(Jump->Target) == UniqueSuccs.end() &&
+             "input CFG contains parallel edges");
+      UniqueSuccs.insert(Jump->Target);
+    }
   }
   // Verify CFG jumps
   for (auto &Block : Func.Blocks) {
@@ -1304,8 +1319,26 @@
 
 } // end of anonymous namespace
 
-/// Apply the profile inference algorithm for a given function
+/// Apply the profile inference algorithm for a given function and provided
+/// profi options
 void llvm::applyFlowInference(const ProfiParams &Params, FlowFunction &Func) {
+  // Check if the function has samples and assign initial flow values
+  bool HasSamples = false;
+  for (FlowBlock &Block : Func.Blocks) {
+    if (Block.Weight > 0)
+      HasSamples = true;
+    Block.Flow = Block.Weight;
+  }
+  for (FlowJump &Jump : Func.Jumps) {
+    if (Jump.Weight > 0)
+      HasSamples = true;
+    Jump.Flow = Jump.Weight;
+  }
+
+  // Quit early for functions with a single block or ones w/o samples
+  if (Func.Blocks.size() <= 1 || !HasSamples)
+    return;
+
 #ifndef NDEBUG
   // Verify the input data
   verifyInput(Func);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150466.521720.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230512/8a7be84d/attachment.bin>


More information about the llvm-commits mailing list