[PATCH] D136846: [Driver] Enable profi flag as user-facing flag

Zhang Haoyu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 27 07:09:00 PDT 2022


HaoyuZhang created this revision.
Herald added a subscriber: wenlei.
Herald added a project: All.
HaoyuZhang requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

This patch enable -sample-profile-use-profi in Clang frontend as user-facing
feature. By using this patch, we can use the cflag of "-fsample-profile-use-profi"
instead of "-mllvm -sample-profile-use-profi".


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136846

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/Inputs/pgo-sample-use-profi.prof
  clang/test/CodeGen/pgo-sample-use-profi.cpp


Index: clang/test/CodeGen/pgo-sample-use-profi.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/pgo-sample-use-profi.cpp
@@ -0,0 +1,38 @@
+//Test if profi flat is enabled in frontend as user-facing feature.
+//
+//RUN: %clang++ -c -O2 -g -fprofile-sample-use=%S/Inputs/pgo-sample-use-profi.prof %s -emit-llvm -o - | opt -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s --check-prefix=CHECK1
+//RUN: %clang++ -c -O2 -g -fprofile-sample-use=%S/Inputs/pgo-sample-use-profi.prof -mllvm -sample-profile-use-profi %s -emit-llvm -o - | opt -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s --check-prefix=CHECK2
+//RUN: %clang++ -c -O2 -g -fprofile-sample-use=%S/Inputs/pgo-sample-use-profi.prof -fsample-profile-use-profi %s -emit-llvm -o - | opt -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s --check-prefix=CHECK2
+
+// Check the profi algorithm can fix the block and with using the user-facing feature can achieve the same result of using "-mllvm"
+// CHECK1: for.body.preheader.preheader probability is 0x7ffe64db / 0x80000000 = 100.00% [HOT edge]
+// CHECK2: for.body.preheader.preheader probability is 0x7ffe64d6 / 0x80000000 = 100.00% [HOT edge]
+
+const int N = 30000;
+
+__attribute__ ((noinline))  void bubble_sort (int *a, int n) {
+    int i, t, s = 1;
+    while (s) {
+        s = 0;
+        for (i = 1; i < n; i++) {
+            if (a[i] < a[i - 1]) {
+                t = a[i];
+                a[i] = a[i - 1];
+                a[i - 1] = t;
+                s = 1;
+            }
+        }
+    }
+}
+
+int main() {
+  int data[N];
+  for (int i = 0; i < N; i ++) {
+    if (i%2 == 0)
+        data[i] = i;
+    else
+        data[i] = -i;
+  }
+    bubble_sort(data, N);
+    return 0;
+}
\ No newline at end of file
Index: clang/test/CodeGen/Inputs/pgo-sample-use-profi.prof
===================================================================
--- /dev/null
+++ clang/test/CodeGen/Inputs/pgo-sample-use-profi.prof
@@ -0,0 +1,8 @@
+_Z11bubble_sortPii:965211:0
+ 0: 0
+ 2: 4
+ 4: 19852
+ 5: 20401
+ 7: 4556
+ 8: 4556
+ 13: 0
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -5760,6 +5760,12 @@
 
   Args.AddLastArg(CmdArgs, options::OPT_fclang_abi_compat_EQ);
 
+  // Enable profi in frontend and forward to LLVM invocation
+  if (Args.hasArg(options::OPT_fsample_profile_use_profi)) {
+    CmdArgs.push_back("-mllvm");
+    CmdArgs.push_back("-sample-profile-use-profi");
+  }
+
   // Add runtime flag for PS4/PS5 when PGO, coverage, or sanitizers are enabled.
   if (RawTriple.isPS() &&
       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1248,6 +1248,9 @@
                as cold. Otherwise, treat callsites without profile samples as if
                we have no profile}]>,
    MarshallingInfoFlag<CodeGenOpts<"ProfileSampleAccurate">>;
+def fsample_profile_use_profi : Flag<["-"], "fsample-profile-use-profi">,
+    Flags<[NoXarchOption, CC1Option]>, Group<f_Group>,
+    HelpText<"Use profi to infer block and edge counts.">;
 def fno_profile_sample_accurate : Flag<["-"], "fno-profile-sample-accurate">,
   Group<f_Group>, Flags<[NoXarchOption]>;
 def fauto_profile : Flag<["-"], "fauto-profile">, Group<f_Group>,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136846.471158.patch
Type: text/x-patch
Size: 3620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221027/eede1cdd/attachment-0001.bin>


More information about the cfe-commits mailing list