[llvm] bdb173d - [llvm-remarkutil] Add an option to display DebugLoc when collecting counts for remarks.

Zain Jaffal via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 18 05:49:08 PDT 2023


Author: Zain Jaffal
Date: 2023-04-18T13:48:42+01:00
New Revision: bdb173d0dde81b60401e729433512aa76cd531d6

URL: https://github.com/llvm/llvm-project/commit/bdb173d0dde81b60401e729433512aa76cd531d6
DIFF: https://github.com/llvm/llvm-project/commit/bdb173d0dde81b60401e729433512aa76cd531d6.diff

LOG: [llvm-remarkutil] Add an option to display DebugLoc when collecting counts for remarks.

Reviewed By: paquette

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

Added: 
    llvm/test/tools/llvm-remarkutil/Inputs/annotation-count-with-dbg-loc.yaml
    llvm/test/tools/llvm-remarkutil/Inputs/instruction-count-with-dbg-loc.yaml
    llvm/test/tools/llvm-remarkutil/annotation-count-with-dbg-loc.test
    llvm/test/tools/llvm-remarkutil/instruction-count-with-dbg-loc.test
    llvm/test/tools/llvm-remarkutil/no-debug-log.test

Modified: 
    llvm/docs/CommandGuide/llvm-remarkutil.rst
    llvm/tools/llvm-remarkutil/RemarkUtil.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/docs/CommandGuide/llvm-remarkutil.rst b/llvm/docs/CommandGuide/llvm-remarkutil.rst
index 4dc5cefabf42f..96fb76798850b 100644
--- a/llvm/docs/CommandGuide/llvm-remarkutil.rst
+++ b/llvm/docs/CommandGuide/llvm-remarkutil.rst
@@ -58,7 +58,7 @@ instruction-count
 
 .. program:: llvm-remarkutil instruction-count
 
-USAGE: :program:`llvm-remarkutil` instruction-count <input file> --parser=<bitstream|yaml> -o <output file>
+USAGE: :program:`llvm-remarkutil` instruction-count <input file> --parser=<bitstream|yaml> [--use-debug-loc] -o <output file>
 
 Summary
 ^^^^^^^
@@ -74,6 +74,12 @@ CSV format is as follows:
   Function,InstructionCount
   foo,123
 
+if `--use-debug-loc` is passed then the CSV will include the source path, line number and column. 
+
+::
+  Source,Function,InstructionCount
+  path:line:column,foo,3
+
 .. _annotation-count_subcommand:
 
 annotation-count
@@ -81,7 +87,7 @@ annotation-count
 
 .. program:: llvm-remarkutil annotation-count
 
-USAGE: :program:`llvm-remarkutil` annotation-count <input file> --parser=<bitstream|yaml> --annotation-type=<type> -o <output file>
+USAGE: :program:`llvm-remarkutil` annotation-count <input file> --parser=<bitstream|yaml> --annotation-type=<type>  [--use-debug-loc] -o <output file>
 
 Summary
 ^^^^^^^
@@ -96,3 +102,9 @@ CSV format is as follows:
 ::
   Function,Count
   foo,123
+
+if `--use-debug-loc` is passed then the CSV will include the source path, line number and column. 
+
+::
+  Source,Function,Count
+  path:line:column,foo,3

diff  --git a/llvm/test/tools/llvm-remarkutil/Inputs/annotation-count-with-dbg-loc.yaml b/llvm/test/tools/llvm-remarkutil/Inputs/annotation-count-with-dbg-loc.yaml
new file mode 100644
index 0000000000000..6262ee262f499
--- /dev/null
+++ b/llvm/test/tools/llvm-remarkutil/Inputs/annotation-count-with-dbg-loc.yaml
@@ -0,0 +1,30 @@
+--- !Analysis
+Pass:            annotation-remarks
+Name:            AnnotationSummary
+DebugLoc:        { File: path/to/anno.c, Line: 1, Column: 2 }
+Function:        func1
+Args:
+  - String:          'Annotated '
+  - count:           '1'
+  - String:          ' instructions with '
+  - type:            remark
+--- !Analysis
+Pass:            annotation-remarks
+Name:            AnnotationSummary
+DebugLoc:        { File: path/to/anno2.c, Line: 1, Column: 2 }
+Function:        func2 
+Args:
+  - String:          'Annotated '
+  - count:           '2'
+  - String:          ' instructions with '
+  - type:            remark
+--- !Analysis
+Pass:            annotation-remarks
+Name:            AnnotationSummary
+DebugLoc:        { File: path/to/anno3.c, Line: 1, Column: 2 }
+Function:        func3
+Args:
+  - String:          'Annotated '
+  - count:           '3'
+  - String:          ' instructions with '
+  - type:            remark

diff  --git a/llvm/test/tools/llvm-remarkutil/Inputs/instruction-count-with-dbg-loc.yaml b/llvm/test/tools/llvm-remarkutil/Inputs/instruction-count-with-dbg-loc.yaml
new file mode 100644
index 0000000000000..3bde9f42405c9
--- /dev/null
+++ b/llvm/test/tools/llvm-remarkutil/Inputs/instruction-count-with-dbg-loc.yaml
@@ -0,0 +1,27 @@
+--- !Analysis
+Pass:            asm-printer
+Name:            InstructionCount
+DebugLoc:        { File: path/to/inst.c, Line: 1, Column: 2 }
+Function:        func1
+Args:
+  - NumInstructions: '1'
+  - String:          ' instructions in function'
+...
+--- !Analysis
+Pass:            asm-printer
+Name:            InstructionCount
+DebugLoc:        { File: path/to/inst2.c, Line: 1, Column: 2 }
+Function:        func2
+Args:
+  - NumInstructions: '2'
+  - String:          ' instructions in function'
+...
+--- !Analysis
+Pass:            asm-printer
+Name:            InstructionCount
+DebugLoc:        { File: path/to/inst3.c, Line: 1, Column: 2 }
+Function:        func3
+Args:
+  - NumInstructions: '3'
+  - String:          ' instructions in function'
+...

diff  --git a/llvm/test/tools/llvm-remarkutil/annotation-count-with-dbg-loc.test b/llvm/test/tools/llvm-remarkutil/annotation-count-with-dbg-loc.test
new file mode 100644
index 0000000000000..cdc731c003f6a
--- /dev/null
+++ b/llvm/test/tools/llvm-remarkutil/annotation-count-with-dbg-loc.test
@@ -0,0 +1,7 @@
+RUN: llvm-remarkutil annotation-count --use-debug-loc --parser=yaml --annotation-type=remark %p/Inputs/annotation-count-with-dbg-loc.yaml | FileCheck %s
+RUN: llvm-remarkutil yaml2bitstream %p/Inputs/annotation-count-with-dbg-loc.yaml | llvm-remarkutil annotation-count --use-debug-loc --parser=bitstream --annotation-type=remark | FileCheck %s
+
+; CHECK-LABEL: Source,Function,Count
+; CHECK: path/to/anno.c:1:2,func1,1
+; CHECK: path/to/anno2.c:1:2,func2,2
+; CHECK: path/to/anno3.c:1:2,func3,3

diff  --git a/llvm/test/tools/llvm-remarkutil/instruction-count-with-dbg-loc.test b/llvm/test/tools/llvm-remarkutil/instruction-count-with-dbg-loc.test
new file mode 100644
index 0000000000000..ac6c8256febe0
--- /dev/null
+++ b/llvm/test/tools/llvm-remarkutil/instruction-count-with-dbg-loc.test
@@ -0,0 +1,7 @@
+RUN: llvm-remarkutil instruction-count --use-debug-loc --parser=yaml %p/Inputs/instruction-count-with-dbg-loc.yaml | FileCheck %s
+RUN: llvm-remarkutil yaml2bitstream %p/Inputs/instruction-count-with-dbg-loc.yaml | llvm-remarkutil instruction-count --use-debug-loc --parser=bitstream | FileCheck %s
+
+; CHECK-LABEL: Source,Function,InstructionCount
+; CHECK: path/to/inst.c:1:2,func1,1
+; CHECK: path/to/inst2.c:1:2,func2,2
+; CHECK: path/to/inst3.c:1:2,func3,3

diff  --git a/llvm/test/tools/llvm-remarkutil/no-debug-log.test b/llvm/test/tools/llvm-remarkutil/no-debug-log.test
new file mode 100644
index 0000000000000..2f17e28031894
--- /dev/null
+++ b/llvm/test/tools/llvm-remarkutil/no-debug-log.test
@@ -0,0 +1,7 @@
+RUN: llvm-remarkutil instruction-count --use-debug-loc --parser=yaml %p/Inputs/instruction-count.yaml | FileCheck --check-prefix=INSTRUCTIONCOUNT %s
+RUN: llvm-remarkutil yaml2bitstream %p/Inputs/instruction-count.yaml | llvm-remarkutil instruction-count --use-debug-loc --parser=bitstream | FileCheck %s --check-prefix=INSTRUCTIONCOUNT
+RUN: llvm-remarkutil annotation-count --use-debug-loc --parser=yaml --annotation-type=remark %p/Inputs/annotation-count.yaml | FileCheck --check-prefix=ANNOTATIONCOUNT %s
+RUN: llvm-remarkutil yaml2bitstream %p/Inputs/instruction-count.yaml | llvm-remarkutil annotation-count --use-debug-loc --parser=bitstream --annotation-type=remark | FileCheck %s --check-prefix=ANNOTATIONCOUNT
+
+; ANNOTATIONCOUNT-LABEL: Source,Function,Count
+; INSTRUCTIONCOUNT-LABEL: Source,Function,InstructionCount

diff  --git a/llvm/tools/llvm-remarkutil/RemarkUtil.cpp b/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
index 004229c567dd2..14af5d2842cf8 100644
--- a/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
+++ b/llvm/tools/llvm-remarkutil/RemarkUtil.cpp
@@ -61,6 +61,15 @@ static cl::SubCommand
       cl::values(clEnumValN(Format::YAML, "yaml", "YAML"),                     \
                  clEnumValN(Format::Bitstream, "bitstream", "Bitstream")),     \
       cl::sub(SUBOPT));
+
+#define DEBUG_LOC_INFO_COMMAND_LINE_OPTIONS(SUBOPT)                            \
+  static cl::opt<bool> UseDebugLoc(                                            \
+      "use-debug-loc",                                                         \
+      cl::desc(                                                                \
+          "Add debug loc information when generating tables for "              \
+          "functions. The loc is represented as (path:line number:column "     \
+          "number)"),                                                          \
+      cl::init(false), cl::sub(SUBOPT));
 namespace yaml2bitstream {
 /// Remark format to parse.
 static constexpr Format InputFormat = Format::YAML;
@@ -80,6 +89,7 @@ INPUT_OUTPUT_COMMAND_LINE_OPTIONS(subopts::Bitstream2YAML)
 namespace instructioncount {
 INPUT_FORMAT_COMMAND_LINE_OPTIONS(subopts::InstructionCount)
 INPUT_OUTPUT_COMMAND_LINE_OPTIONS(subopts::InstructionCount)
+DEBUG_LOC_INFO_COMMAND_LINE_OPTIONS(subopts::InstructionCount)
 } // namespace instructioncount
 
 namespace annotationcount {
@@ -88,6 +98,7 @@ static cl::opt<std::string> AnnotationTypeToCollect(
     "annotation-type", cl::desc("annotation-type remark to collect count for"),
     cl::sub(subopts::AnnotationCount));
 INPUT_OUTPUT_COMMAND_LINE_OPTIONS(subopts::AnnotationCount)
+DEBUG_LOC_INFO_COMMAND_LINE_OPTIONS(subopts::AnnotationCount)
 } // namespace annotationcount
 
 /// \returns A MemoryBuffer for the input file on success, and an Error
@@ -131,6 +142,10 @@ getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat) {
                                                     : sys::fs::OF_None);
 }
 
+static bool shouldSkipRemark(bool UseDebugLoc, Remark &Remark) {
+  return UseDebugLoc && !Remark.Loc.has_value();
+}
+
 namespace yaml2bitstream {
 /// Parses all remarks in the input YAML file.
 /// \p [out] ParsedRemarks - Filled with remarks parsed from the input file.
@@ -245,6 +260,8 @@ static Error tryInstructionCount() {
   if (!MaybeParser)
     return MaybeParser.takeError();
   // Emit CSV header.
+  if (UseDebugLoc)
+    OF->os() << "Source,";
   OF->os() << "Function,InstructionCount\n";
   // Parse all remarks. Whenever we see an instruction count remark, output
   // the file name and the number of instructions.
@@ -254,11 +271,19 @@ static Error tryInstructionCount() {
     auto &Remark = **MaybeRemark;
     if (Remark.RemarkName != "InstructionCount")
       continue;
+    if (shouldSkipRemark(UseDebugLoc, Remark))
+      continue;
     auto *InstrCountArg = find_if(Remark.Args, [](const Argument &Arg) {
       return Arg.Key == "NumInstructions";
     });
     assert(InstrCountArg != Remark.Args.end() &&
            "Expected instruction count remarks to have a NumInstructions key?");
+    if (UseDebugLoc) {
+      std::string Loc = Remark.Loc->SourceFilePath.str() + ":" +
+                        std::to_string(Remark.Loc->SourceLine) + +":" +
+                        std::to_string(Remark.Loc->SourceColumn);
+      OF->os() << Loc << ",";
+    }
     OF->os() << Remark.FunctionName << "," << InstrCountArg->Val << "\n";
   }
   auto E = MaybeRemark.takeError();
@@ -286,6 +311,8 @@ static Error tryAnnotationCount() {
   if (!MaybeParser)
     return MaybeParser.takeError();
   // Emit CSV header.
+  if (UseDebugLoc)
+    OF->os() << "Source,";
   OF->os() << "Function,Count\n";
   // Parse all remarks. When we see the specified remark collect the count
   // information.
@@ -295,6 +322,8 @@ static Error tryAnnotationCount() {
     auto &Remark = **MaybeRemark;
     if (Remark.RemarkName != "AnnotationSummary")
       continue;
+    if (shouldSkipRemark(UseDebugLoc, Remark))
+      continue;
     auto *RemarkNameArg = find_if(Remark.Args, [](const Argument &Arg) {
       return Arg.Key == "type" && Arg.Val == AnnotationTypeToCollect;
     });
@@ -304,6 +333,12 @@ static Error tryAnnotationCount() {
         Remark.Args, [](const Argument &Arg) { return Arg.Key == "count"; });
     assert(CountArg != Remark.Args.end() &&
            "Expected annotation-type remark to have a count key?");
+    if (UseDebugLoc) {
+      std::string Loc = Remark.Loc->SourceFilePath.str() + ":" +
+                        std::to_string(Remark.Loc->SourceLine) + +":" +
+                        std::to_string(Remark.Loc->SourceColumn);
+      OF->os() << Loc << ",";
+    }
     OF->os() << Remark.FunctionName << "," << CountArg->Val << "\n";
   }
   auto E = MaybeRemark.takeError();


        


More information about the llvm-commits mailing list