diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -180,6 +180,7 @@ ///< enabled. CODEGENOPT(NoWarn , 1, 0) ///< Set when -Wa,--no-warn is enabled. CODEGENOPT(MisExpect , 1, 0) ///< Set when -Wmisexpect is enabled +CODEGENOPT(MissingAnnotations, 1, 0) ///< Set when suggesting missing perf annotations CODEGENOPT(EnableSegmentedStacks , 1, 0) ///< Set when -fsplit-stack is enabled. CODEGENOPT(NoInlineLineTables, 1, 0) ///< Whether debug info should contain ///< inline line tables. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -479,6 +479,7 @@ Options.MCOptions.Argv0 = CodeGenOpts.Argv0; Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs; Options.MisExpect = CodeGenOpts.MisExpect; + Options.MissingAnnotations = CodeGenOpts.MissingAnnotations; return true; } diff --git a/clang/test/Profile/Inputs/missing-annotation.proftext b/clang/test/Profile/Inputs/missing-annotation.proftext new file mode 100644 --- /dev/null +++ b/clang/test/Profile/Inputs/missing-annotation.proftext @@ -0,0 +1,18 @@ +bar +# Func Hash: +11262309464 +# Num Counters: +2 +# Counter Values: +200000 +18000 + +fizz +# Func Hash: +11262309464 +# Num Counters: +2 +# Counter Values: +200000 +18000 + diff --git a/clang/test/Profile/missing-annotation.c b/clang/test/Profile/missing-annotation.c new file mode 100644 --- /dev/null +++ b/clang/test/Profile/missing-annotation.c @@ -0,0 +1,39 @@ +// Test that missing annotation diagnostics are suggested for hot branches + +// test diagnostics are issued when profiling data mis-matches annotations +// RUN: llvm-profdata merge %S/Inputs/missing-annotation.proftext -o %t.profdata +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=imprecise -Rpass=missing-annotations +// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=exact -Rpass=missing-annotations -debug-info-kind=line-tables-only + + +// foo-no-diagnostics + +int foo(int); +int baz(int); +int buzz(); + +const int inner_loop = 100; +const int outer_loop = 2000; + +int bar() { // imprecise-warning-re {{remark: misexpect-branch.c:22:0: Extremely hot condition. Consider adding llvm.expect intrinsic }} + int rando = buzz(); + int x = 0; + if (rando % (outer_loop * inner_loop) == 0) { // exact-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} + x = baz(rando); + } else { + x = foo(50); + } + return x; +} + +int fizz() { // imprecise-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} + int rando = buzz(); + int x = 0; + if (rando % (outer_loop * inner_loop) == 0) { // exact-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} + x = baz(rando); + } else { + x = foo(50); + } + return x; +} +