[PATCH] D21405: [PGO] IRPGO pre-cleanup pass changes

Rong Xu via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 15 13:31:50 PDT 2016


xur created this revision.
xur added reviewers: silvas, vsk, davidxl, mehdi_amini, tejohnson.
xur added a subscriber: llvm-commits.
Herald added a subscriber: mehdi_amini.

This patch adds a selected set of cleanup passes including a pre-inline pass before LLVM IR PGO instrumentation. The inline is only intended to apply those obvious/trivial ones before instrumentation so that much less instrumentation is needed to get better profiling information. This will drastically improve the instrumented code performance for large C++ applications. Another benefit was the context sensitive counts that can potentially improve the PGO optimization. 

This is previously discussed here:
http://lists.llvm.org/pipermail/llvm-dev/2015-August/089425.html

Here I recollected some the data to show the impact using the followings:
(1) spec2006 C and C++ programs 
(2) some Google internal benchmarks (which are of different size of C++ applications).
(3) instrumented LLVM compiler 

The metrics collected are:
(1) instrumented code runtime performance.
https://docs.google.com/document/d/1DCP7KPjtitHMotX2KgTO8PADd9zjLzPJgSXZ8egd6Mk/edit?usp=sharing

(2) program text size (i.e. sum of sections (having address and name) sizes minus
writable section sizes.)
https://docs.google.com/document/d/1ItOetOEc1vTpU54EBjQK4O0LfulFIv3gs9XXnmzwqdo/edit?usp=sharing


(3) program binary size
https://docs.google.com/document/d/1nmiR6He0cxMFqMzWE-X7FdhL5jSzdS9h7WLxDrfmSxQ/edit?usp=sharing


http://reviews.llvm.org/D21405

Files:
  lib/Transforms/IPO/PassManagerBuilder.cpp
  test/Transforms/PGOProfile/preinline.ll

Index: test/Transforms/PGOProfile/preinline.ll
===================================================================
--- /dev/null
+++ test/Transforms/PGOProfile/preinline.ll
@@ -0,0 +1,20 @@
+; RUN: opt < %s -O2 -profile-generate=default.profraw -S | FileCheck %s --check-prefix=GEN
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define i32 @foo(i32 %i) {
+entry:
+; GEN: %pgocount = load i64, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc_foo
+; GEN-NOT: %pgocount.i = load i64, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__stdin__bar
+  %call = call i32 @bar()
+  %add = add nsw i32 %i, %call
+  ret i32 %add
+}
+
+define internal i32 @bar() {
+entry:
+  %call = call i32 (...) @bar1()
+  ret i32 %call
+}
+
+declare i32 @bar1(...)
Index: lib/Transforms/IPO/PassManagerBuilder.cpp
===================================================================
--- lib/Transforms/IPO/PassManagerBuilder.cpp
+++ lib/Transforms/IPO/PassManagerBuilder.cpp
@@ -118,6 +118,14 @@
     "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
     cl::desc("Enable the experimental Loop Versioning LICM pass"));
 
+static cl::opt<bool>
+    RunPreInliner("preinline", cl::init(true), cl::Hidden, cl::ZeroOrMore,
+                  cl::desc("Enable pre-instrumentation inliner"));
+
+static cl::opt<bool>
+    DisablePreInliner("disable-preinline", cl::init(false), cl::Hidden,
+                      cl::desc("Disable pre-instrumentation inliner"));
+
 PassManagerBuilder::PassManagerBuilder() {
     OptLevel = 2;
     SizeLevel = 0;
@@ -209,8 +217,32 @@
   FPM.add(createLowerExpectIntrinsicPass());
 }
 
+// Get the inline threshold for pre-instrumentation inline.
+static int computePreInlineThresholdFromOptLevels(unsigned OptLevel,
+                                                  unsigned SizeOptLevel) {
+  if (SizeOptLevel == 1) // -Os
+    return 75;
+  if (SizeOptLevel == 2) // -Oz
+    return 25;
+  return 75;
+}
+
 // Do PGO instrumentation generation or use pass as the option specified.
 void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
+  if (PGOInstrGen.empty() && PGOInstrUse.empty())
+    return;
+  if (OptLevel > 0 && RunPreInliner && !DisablePreInliner) {
+    // Create preinline pass.
+    MPM.add(createFunctionInliningPass(
+        computePreInlineThresholdFromOptLevels(OptLevel, SizeLevel)));
+    MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
+    MPM.add(createJumpThreadingPass()); // Thread jumps.
+    MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
+    MPM.add(createCFGSimplificationPass());          // Merge & remove BBs
+    MPM.add(createInstructionCombiningPass());       // Combine silly seq's
+    MPM.add(createGVNPass(DisableGVNLoadPRE));       // Remove redundancies
+    addExtensionsToPM(EP_Peephole, MPM);
+  }
   if (!PGOInstrGen.empty()) {
     MPM.add(createPGOInstrumentationGenLegacyPass());
     // Add the profile lowering pass.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21405.60891.patch
Type: text/x-patch
Size: 3039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160615/838e8e8c/attachment.bin>


More information about the llvm-commits mailing list