[PATCH] D65376: Randomly outline code for cold regions

Aditya Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 06:33:20 PDT 2019


hiraditya created this revision.
hiraditya added reviewers: vsk, tejohnson.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In absence of profile information, it is possible to take advantage of hot-cold splitting
if the code base is mostly code e.g. hot/cold<0.2/0.8 (80:20 rule). It is a non-deterministic
outlining but can be beneficial in several instances where profile data is not available and determinism is not important.

Putting the diff here in case there is interest in having aggressive hot-cold-splitting.


Repository:
  rL LLVM

https://reviews.llvm.org/D65376

Files:
  llvm/lib/Transforms/IPO/HotColdSplitting.cpp


Index: llvm/lib/Transforms/IPO/HotColdSplitting.cpp
===================================================================
--- llvm/lib/Transforms/IPO/HotColdSplitting.cpp
+++ llvm/lib/Transforms/IPO/HotColdSplitting.cpp
@@ -84,6 +84,9 @@
                        cl::desc("Base penalty for splitting cold code (as a "
                                 "multiple of TCC_Basic)"));
 
+static cl::opt<bool> EnableRandomOutlining("hot-cold-randomly-outline-cold-code",
+                              cl::init(false), cl::Hidden);
+
 namespace {
 
 /// A sequence of basic blocks.
@@ -106,6 +109,20 @@
   return !(isa<ReturnInst>(I) || isa<IndirectBrInst>(I));
 }
 
+static Optional<std::pair<const BasicBlock *, const BasicBlock *>>
+hasSingleSuccAndPred(const BasicBlock &BB) {
+  auto FirstSucc = succ_begin(&BB);
+  auto SuccEnd = succ_end(&BB);
+  if (FirstSucc == SuccEnd || ++FirstSucc != SuccEnd)
+    return None;
+  auto FirstPred = pred_begin(&BB);
+  auto PredEnd = pred_end(&BB);
+  if (FirstPred == PredEnd || ++FirstPred != PredEnd)
+    return None;
+  return Optional<std::pair<const BasicBlock *, const BasicBlock *>>(
+      {*succ_begin(&BB), *pred_begin(&BB)});
+}
+
 bool unlikelyExecuted(BasicBlock &BB) {
   // Exception handling blocks are unlikely executed.
   if (BB.isEHPad() || isa<ResumeInst>(BB.getTerminator()))
@@ -128,7 +145,30 @@
     return true;
   }
 
-  return false;
+	if (!EnableRandomOutlining)
+		return false;
+
+  // Game of chance hypothesis: when most (80:20 rule) of the code is cold,
+	// a randomly selected basic block has a higher chance of being cold. Do this
+  // if the basic block is part of a diamond structure (if-else).
+	// NB: This causes non-deterministic outlining.
+  if (auto SuccPred = hasSingleSuccAndPred(BB)) {
+    auto Succ = SuccPred->first;
+    int SuccPredCount = 0;
+    for (auto X : predecessors(Succ))
+      ++SuccPredCount;
+    if (SuccPredCount < 2)
+      return false;
+    auto Pred = SuccPred->second;
+    int PredSuccCount = 0;
+    for (auto X : successors(Pred))
+      ++PredSuccCount;
+    if (PredSuccCount < 2)
+      return false;
+    double Chance = (std::rand() % PredSuccCount) / double(PredSuccCount);
+		// Threshold can be decreased to enable aggressive outlining.
+    return (Chance < 0.5);
+  }
 }
 
 /// Check whether it's safe to outline \p BB.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65376.212092.patch
Type: text/x-patch
Size: 2345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190728/ba024f46/attachment.bin>


More information about the llvm-commits mailing list