[PATCH] D131137: [clang][misexpect] Add support to clang for profitable annotation diagnostics

2023-01-25 Thread Paul Kirth via Phabricator via cfe-commits
paulkirth updated this revision to Diff 492269.
paulkirth added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131137/new/

https://reviews.llvm.org/D131137

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/test/Profile/Inputs/missing-annotation.proftext
  clang/test/Profile/missing-annotation.c

Index: clang/test/Profile/missing-annotation.c
===
--- /dev/null
+++ clang/test/Profile/missing-annotation.c
@@ -0,0 +1,37 @@
+// 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=exact -fdiagnostics-missing-annotations -debug-info-kind=line-tables-only -Rpass=missing-annotations
+
+
+// foo-no-diagnostics
+
+int foo(int);
+int baz(int);
+int buzz();
+
+const int inner_loop = 100;
+const int outer_loop = 2000;
+int bar() { 
+  int rando = buzz();
+  int x = 0;
+  if (rando % (outer_loop * inner_loop) == 0) { // exact-remark {{Extremely hot condition. Consider adding  llvm.expect intrinsic}}
+x = baz(rando);
+  } else {
+x = foo(50);
+  }
+  return x;
+}
+
+int fizz() {
+  int rando = buzz();
+  int x = 0;
+  if (rando % (outer_loop * inner_loop) == 0) { // exact-remark {{Extremely hot condition. Consider adding  llvm.expect intrinsic}}
+x = baz(rando);
+  } else {
+x = foo(50);
+  }
+  return x;
+}
+
Index: clang/test/Profile/Inputs/missing-annotation.proftext
===
--- /dev/null
+++ clang/test/Profile/Inputs/missing-annotation.proftext
@@ -0,0 +1,18 @@
+bar
+# Func Hash:
+11262309464
+# Num Counters:
+2
+# Counter Values:
+20
+1
+
+fizz
+# Func Hash:
+11262309464
+# Num Counters:
+2
+# Counter Values:
+20
+1
+
Index: clang/lib/CodeGen/CodeGenAction.cpp
===
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -347,6 +347,10 @@
 Ctx.setMisExpectWarningRequested(true);
   }
 
+  if (CodeGenOpts.MissingAnnotations) {
+Ctx.setAnnotationDiagsRequested(true);
+  }
+
   if (CodeGenOpts.DiagnosticsMisExpectTolerance) {
 Ctx.setDiagnosticsMisExpectTolerance(
 CodeGenOpts.DiagnosticsMisExpectTolerance);
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -492,6 +492,7 @@
   Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
   Options.MCOptions.AsSecureLogFile = CodeGenOpts.AsSecureLogFile;
   Options.MisExpect = CodeGenOpts.MisExpect;
+  Options.MissingAnnotations = CodeGenOpts.MissingAnnotations;
 
   return true;
 }
Index: clang/include/clang/Driver/Options.td
===
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1509,6 +1509,10 @@
 def fdiagnostics_misexpect_tolerance_EQ : Joined<["-"], "fdiagnostics-misexpect-tolerance=">,
 Group, Flags<[CC1Option]>, MetaVarName<"">,
 HelpText<"Prevent misexpect diagnostics from being output if the profile counts are within N% of the expected. ">;
+defm diagnostics_missing_annotations : BoolFOption<"diagnostics-missing-annotations",
+  CodeGenOpts<"MissingAnnotations">, DefaultFalse,
+  PosFlag,
+  NegFlag>;
 defm diagnostics_show_option : BoolFOption<"diagnostics-show-option",
 DiagnosticOpts<"ShowOptionNames">, DefaultTrue,
 NegFlag, PosFlag>;
Index: clang/include/clang/Basic/CodeGenOptions.def
===
--- clang/include/clang/Basic/CodeGenOptions.def
+++ clang/include/clang/Basic/CodeGenOptions.def
@@ -180,6 +180,7 @@
 CODEGENOPT(NoWarn, 1, 0) ///< Set when -Wa,--no-warn is enabled.
 CODEGENOPT(NoTypeCheck   , 1, 0) ///< Set when -Wa,--no-type-check 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.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D131137: [clang][misexpect] Add support to clang for profitable annotation diagnostics

2022-08-17 Thread Paul Kirth via Phabricator via cfe-commits
paulkirth created this revision.
Herald added a subscriber: ormris.
Herald added a project: All.
paulkirth updated this revision to Diff 450194.
paulkirth added a comment.
paulkirth added reviewers: tejohnson, nickdesaulniers, phosek.
paulkirth published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fix Clang tests and flags


Add basic plumbing to clang so that diagnostics can be surfaced to users


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131137

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/test/Profile/Inputs/missing-annotation.proftext
  clang/test/Profile/missing-annotation.c

Index: clang/test/Profile/missing-annotation.c
===
--- /dev/null
+++ clang/test/Profile/missing-annotation.c
@@ -0,0 +1,37 @@
+// 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=exact -fdiagnostics-missing-annotations -debug-info-kind=line-tables-only -Rpass=missing-annotations
+
+
+// foo-no-diagnostics
+
+int foo(int);
+int baz(int);
+int buzz();
+
+const int inner_loop = 100;
+const int outer_loop = 2000;
+int bar() { 
+  int rando = buzz();
+  int x = 0;
+  if (rando % (outer_loop * inner_loop) == 0) { // exact-remark {{Extremely hot condition. Consider adding  llvm.expect intrinsic}}
+x = baz(rando);
+  } else {
+x = foo(50);
+  }
+  return x;
+}
+
+int fizz() {
+  int rando = buzz();
+  int x = 0;
+  if (rando % (outer_loop * inner_loop) == 0) { // exact-remark {{Extremely hot condition. Consider adding  llvm.expect intrinsic}}
+x = baz(rando);
+  } else {
+x = foo(50);
+  }
+  return x;
+}
+
Index: clang/test/Profile/Inputs/missing-annotation.proftext
===
--- /dev/null
+++ clang/test/Profile/Inputs/missing-annotation.proftext
@@ -0,0 +1,18 @@
+bar
+# Func Hash:
+11262309464
+# Num Counters:
+2
+# Counter Values:
+20
+1
+
+fizz
+# Func Hash:
+11262309464
+# Num Counters:
+2
+# Counter Values:
+20
+1
+
Index: clang/lib/CodeGen/CodeGenAction.cpp
===
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -346,6 +346,10 @@
 Ctx.setMisExpectWarningRequested(true);
   }
 
+  if (CodeGenOpts.MissingAnnotations) {
+Ctx.setAnnotationDiagsRequested(true);
+  }
+
   if (CodeGenOpts.DiagnosticsMisExpectTolerance) {
 Ctx.setDiagnosticsMisExpectTolerance(
 CodeGenOpts.DiagnosticsMisExpectTolerance);
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ 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;
 }
Index: clang/include/clang/Driver/Options.td
===
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1481,6 +1481,10 @@
 def fdiagnostics_misexpect_tolerance_EQ : Joined<["-"], "fdiagnostics-misexpect-tolerance=">,
 Group, Flags<[CC1Option]>, MetaVarName<"">,
 HelpText<"Prevent misexpect diagnostics from being output if the profile counts are within N% of the expected. ">;
+defm diagnostics_missing_annotations : BoolFOption<"diagnostics-missing-annotations",
+  CodeGenOpts<"MissingAnnotations">, DefaultFalse,
+  PosFlag,
+  NegFlag>;
 defm diagnostics_show_option : BoolFOption<"diagnostics-show-option",
 DiagnosticOpts<"ShowOptionNames">, DefaultTrue,
 NegFlag, PosFlag>;
Index: clang/include/clang/Basic/CodeGenOptions.def
===
--- clang/include/clang/Basic/CodeGenOptions.def
+++ 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.