From 7fb400112f6c41fc98ee5d584cdc0dfe76393a25 Mon Sep 17 00:00:00 2001 From: Wei Mi Date: Sat, 27 Feb 2021 21:44:28 -0800 Subject: [PATCH] [SampleFDO] Add a cutoff flag to control how many symbols will be included 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 | 12 ++++++++++-- .../SampleProfile/Inputs/profile-symbol-list.text | 1 + .../Transforms/SampleProfile/profile-sample-accurate.ll | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp index d6acc00..bca5b45 100644 --- a/llvm/lib/ProfileData/SampleProf.cpp +++ b/llvm/lib/ProfileData/SampleProf.cpp @@ -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" @@ -29,6 +30,11 @@ using namespace llvm; using namespace sampleprof; +static cl::opt 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(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; } diff --git a/llvm/test/Transforms/SampleProfile/Inputs/profile-symbol-list.text b/llvm/test/Transforms/SampleProfile/Inputs/profile-symbol-list.text index 3763872..f7a5f22 100644 --- a/llvm/test/Transforms/SampleProfile/Inputs/profile-symbol-list.text +++ b/llvm/test/Transforms/SampleProfile/Inputs/profile-symbol-list.text @@ -1,5 +1,6 @@ _Z3goov _Z3sumii +_Z3toov __libc_csu_fini __libc_csu_init _dl_relocate_static_pie diff --git a/llvm/test/Transforms/SampleProfile/profile-sample-accurate.ll b/llvm/test/Transforms/SampleProfile/profile-sample-accurate.ll index dfd8fd9..0ac80fe 100644 --- a/llvm/test/Transforms/SampleProfile/profile-sample-accurate.ll +++ b/llvm/test/Transforms/SampleProfile/profile-sample-accurate.ll @@ -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 = !{} -- 2.7.4