myhsu created this revision.
myhsu added reviewers: rsmith, bogner, xur.
Herald added subscribers: cfe-commits, wenlei, dang, dexonsmith, steven_wu, 
hiraditya.
Herald added a project: clang.
myhsu requested review of this revision.

Following up https://reviews.llvm.org/D87337 . This patch add the driver 
support to propogate `-fprofile-deopt-cold` and 
`-fprofile-deopt-cold-percent=<percentage>` flags to codegen and the LTO plugin.

authors: myhsu, probinson, and edd


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87338

Files:
  clang/docs/ClangCommandLineReference.rst
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/test/Driver/deopt-cold-funcs.c
  clang/test/Driver/gold-lto-deopt-cold-funcs.c

Index: clang/test/Driver/gold-lto-deopt-cold-funcs.c
===================================================================
--- /dev/null
+++ clang/test/Driver/gold-lto-deopt-cold-funcs.c
@@ -0,0 +1,11 @@
+// RUN: touch %t.o
+//
+// RUN: %clang -### -fprofile-instr-use -fprofile-deopt-cold %t.o -flto 2>&1 \
+// RUN:     | FileCheck --check-prefix=CHECK-BASE %s
+
+// RUN: %clang -### -fprofile-instr-use -fprofile-deopt-cold -fprofile-deopt-cold-percent=87 \
+// RUN:     %t.o -flto 2>&1 \
+// RUN:     | FileCheck --check-prefixes=CHECK-BASE,CHECK-PERCENT %s
+
+// CHECK-BASE: "-plugin-opt=-profile-deopt-cold=true"
+// CHECK-PERCENT: "-plugin-opt=-profile-deopt-cold-percent=87"
Index: clang/test/Driver/deopt-cold-funcs.c
===================================================================
--- /dev/null
+++ clang/test/Driver/deopt-cold-funcs.c
@@ -0,0 +1,15 @@
+// RUN: %clang -### -fprofile-instr-use -fprofile-deopt-cold -c %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-BASE %s
+
+// RUN: %clang -### -fprofile-instr-use \
+// RUN:             -fprofile-deopt-cold -fprofile-deopt-cold-percent=87 \
+// RUN:             -c %s 2>&1 \
+// RUN: | FileCheck --check-prefixes=CHECK-BASE,CHECK-PERCENT %s
+
+// Shouldn't do anything if it's not using PGO profile
+// RUN: %clang -### -fprofile-deopt-cold -c %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-NO-PGO %s
+
+// CHECK-BASE: "-mllvm" "-profile-deopt-cold=true"
+// CHECK-PERCENT: "-mllvm" "-profile-deopt-cold-percent=87"
+// CHECK-NO-PGO-NOT: "-profile-deopt-cold=true"
Index: clang/lib/Driver/ToolChains/CommonArgs.cpp
===================================================================
--- clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -516,6 +516,16 @@
       llvm::sys::path::append(Path, "default.profdata");
     CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=cs-profile-path=") +
                                          Path));
+    // Propagate -profile-deopt-cold options
+    if (Args.hasFlag(options::OPT_fprofile_deopt_cold,
+                     options::OPT_fno_profile_deopt_cold, false)) {
+      CmdArgs.push_back("-plugin-opt=-profile-deopt-cold=true");
+      Arg *A = Args.getLastArg(options::OPT_fprofile_deopt_cold_percent_EQ);
+      if (A) {
+        CmdArgs.push_back(Args.MakeArgString(
+            Twine("-plugin-opt=-profile-deopt-cold-percent=") + A->getValue()));
+      }
+    }
   }
 
   // Need this flag to turn on new pass manager via Gold plugin.
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -824,6 +824,19 @@
       CmdArgs.push_back(
           Args.MakeArgString(Twine("-fprofile-instrument-use-path=") + Path));
     }
+    // Tell profile passes to deopt cold functions.
+    if (Args.hasFlag(options::OPT_fprofile_deopt_cold,
+                     options::OPT_fno_profile_deopt_cold)) {
+      CmdArgs.push_back("-mllvm");
+      CmdArgs.push_back("-profile-deopt-cold=true");
+
+      if (Arg *A =
+              Args.getLastArg(options::OPT_fprofile_deopt_cold_percent_EQ)) {
+        CmdArgs.push_back("-mllvm");
+        CmdArgs.push_back(Args.MakeArgString(
+            Twine("-profile-deopt-cold-percent=") + A->getValue()));
+      }
+    }
   }
 
   bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage,
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -793,6 +793,17 @@
     Alias<fdebug_compilation_dir>;
 defm debug_info_for_profiling : OptInFFlag<"debug-info-for-profiling",
   "Emit extra debug info to make sample profile more accurate">;
+
+// New option to deopt cold code.
+def fprofile_deopt_cold : Flag<["-"], "fprofile-deopt-cold">,
+    Group<f_Group>, Flags<[CoreOption]>,
+    HelpText<"Do not optimize functions that profiling indicates are cold">;
+def fno_profile_deopt_cold : Flag<["-"], "fno-profile-deopt-cold">,
+    Group<f_Group>, Flags<[CoreOption]>;
+def fprofile_deopt_cold_percent_EQ : Joined<["-"], "fprofile-deopt-cold-percent=">,
+    Group<f_Group>, Flags<[CoreOption]>,
+    HelpText<"Do not optimize functions whose profiling count is lower than this percentage">;
+
 def fprofile_instr_generate : Flag<["-"], "fprofile-instr-generate">,
     Group<f_Group>, Flags<[CoreOption]>,
     HelpText<"Generate instrumented code to collect execution counts into default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var)">;
Index: clang/docs/ClangCommandLineReference.rst
===================================================================
--- clang/docs/ClangCommandLineReference.rst
+++ clang/docs/ClangCommandLineReference.rst
@@ -2016,6 +2016,16 @@
 
 Use instrumentation data for profile-guided optimization. If pathname is a directory, it reads from <pathname>/default.profdata. Otherwise, it reads from file <pathname>.
 
+.. option:: -fprofile-deopt-cold, -fno-profile-deopt-cold
+
+Adds ``optnone`` attributes to functions whose instrumentation-based PGO profiling counts are equal to zero (i.e. "cold").
+
+.. program:: clang1
+.. option:: -fprofile-deopt-cold-percent=<percentage>
+.. program:: clang
+
+If ``-fprofile-deopt-cold`` is specified, this allows you to choose the lower <percentage>% of functions, sorted by their profiling counts, as the cold functions to be de-optimized.
+
 .. option:: -freciprocal-math, -fno-reciprocal-math
 
 Allow division operations to be reassociated
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to