[SampleFDO] Add a cutoff flag to control how many symbols will be included
authorWei Mi <wmi@google.com>
Sun, 28 Feb 2021 05:44:28 +0000 (21:44 -0800)
committerWei Mi <wmi@google.com>
Sun, 28 Feb 2021 07:15:31 +0000 (23:15 -0800)
into profile symbol list.

When test is unrepresentative to production behavior, sample profile
collected from production can cause unexpected performance behavior
in test. To triage such issue, it is useful to have a cutoff flag
to control how many symbols will be included into profile symbol list
in order to do binary search.

Differential Revision: https://reviews.llvm.org/D97623

llvm/lib/ProfileData/SampleProf.cpp
llvm/test/Transforms/SampleProfile/Inputs/profile-symbol-list.text
llvm/test/Transforms/SampleProfile/profile-sample-accurate.ll

index d6acc00..bca5b45 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/PseudoProbe.h"
 #include "llvm/ProfileData/SampleProfReader.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Error.h"
 using namespace llvm;
 using namespace sampleprof;
 
+static cl::opt<uint64_t> ProfileSymbolListCutOff(
+    "profile-symbol-list-cutoff", cl::Hidden, cl::init(-1), cl::ZeroOrMore,
+    cl::desc("Cutoff value about how many symbols in profile symbol list "
+             "will be used. This is very useful for performance debugging"));
+
 namespace llvm {
 namespace sampleprof {
 SampleProfileFormat FunctionSamples::Format;
@@ -274,12 +280,14 @@ std::error_code ProfileSymbolList::read(const uint8_t *Data,
                                         uint64_t ListSize) {
   const char *ListStart = reinterpret_cast<const char *>(Data);
   uint64_t Size = 0;
-  while (Size < ListSize) {
+  uint64_t StrNum = 0;
+  while (Size < ListSize && StrNum < ProfileSymbolListCutOff) {
     StringRef Str(ListStart + Size);
     add(Str);
     Size += Str.size() + 1;
+    StrNum++;
   }
-  if (Size != ListSize)
+  if (Size != ListSize && StrNum != ProfileSymbolListCutOff)
     return sampleprof_error::malformed;
   return sampleprof_error::success;
 }
index dfd8fd9..0ac80fe 100644 (file)
@@ -6,6 +6,8 @@
 ; RUN: llvm-profdata merge -sample -extbinary -prof-sym-list=%S/Inputs/profile-symbol-list.text %S/Inputs/profsampleacc.extbinary.afdo -o %t.symlist.afdo
 ; RUN: opt < %s -sample-profile -sample-profile-file=%t.symlist.afdo -profile-summary-cutoff-hot=600000 -profile-accurate-for-symsinlist -enable-new-pm=0 -S | FileCheck %s --check-prefix=PROFSYMLIST
 ; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-summary-cutoff-hot=600000 -profile-accurate-for-symsinlist -S | FileCheck %s --check-prefix=PROFSYMLIST
+; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-accurate-for-symsinlist -profile-symbol-list-cutoff=2 -S | FileCheck %s --check-prefix=PSLCUTOFF2
+; RUN: opt < %s -passes=sample-profile -sample-profile-file=%t.symlist.afdo -profile-accurate-for-symsinlist -profile-symbol-list-cutoff=3 -S | FileCheck %s --check-prefix=PSLCUTOFF3
 ;
 ; If -profile-accurate-for-symsinlist and -profile-sample-accurate both present,
 ; -profile-sample-accurate will override -profile-accurate-for-symsinlist.
@@ -60,6 +62,16 @@ entry:
   ret i32 %add, !dbg !11
 }
 
+; Check -profile-symbol-list-cutoff=3 will include _Z3toov into profile
+; symbol list and -profile-symbol-list-cutoff=2 will not.
+; PSLCUTOFF2: define i32 @_Z3toov{{.*}}!prof ![[TOO_ID:[0-9]+]]
+; PSLCUTOFF3: define i32 @_Z3toov{{.*}}!prof ![[TOO_ID:[0-9]+]]
+define i32 @_Z3toov(i32 %x, i32 %y) #0 {
+entry:
+  %add = add nsw i32 %x, %y
+  ret i32 %add
+}
+
 ; Function Attrs: uwtable
 define i32 @main() #0 !dbg !7 {
 entry:
@@ -132,6 +144,8 @@ attributes #0 = { "use-sample-profile" }
 ; CALL_SUM_IS_HOT: ![[ZERO_ID]] = !{!"function_entry_count", i64 0}
 ; CALL_SUM_IS_WARM: ![[NONZERO_ID]] = !{!"function_entry_count", i64 5179}
 ; PROFSYMLIST: ![[UNKNOWN_ID]] = !{!"function_entry_count", i64 -1}
+; PSLCUTOFF2: ![[TOO_ID]] = !{!"function_entry_count", i64 -1}
+; PSLCUTOFF3: ![[TOO_ID]] = !{!"function_entry_count", i64 0}
 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
 !1 = !DIFile(filename: "calls.cc", directory: ".")
 !2 = !{}