[PATCH] D65376: Randomly outline code for cold regions

Aditya Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 22:11:40 PDT 2019


hiraditya updated this revision to Diff 212502.
hiraditya added a comment.

Updated to have a fixed sequence of 'random' numbers.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65376/new/

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,12 @@
                        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);
+
+static cl::opt<bool> EnableDeterministicRandomOutlining("hot-cold-deterministic-random-outline-code",
+                              cl::init(true), cl::Hidden);
+
 namespace {
 
 /// A sequence of basic blocks.
@@ -106,6 +112,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 +148,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.
@@ -694,6 +737,9 @@
 }
 
 bool HotColdSplittingLegacyPass::runOnModule(Module &M) {
+  // Initialize rng for deterministic aggressive outlining.
+  if (EnableDeterministicRandomOutlining)
+    std::srand(1);
   if (skipModule(M))
     return false;
   ProfileSummaryInfo *PSI =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65376.212502.patch
Type: text/x-patch
Size: 2789 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190731/171537d9/attachment.bin>


More information about the llvm-commits mailing list