diff --git a/llvm/docs/CommandGuide/llvm-remarkutil.rst b/llvm/docs/CommandGuide/llvm-remarkutil.rst --- a/llvm/docs/CommandGuide/llvm-remarkutil.rst +++ b/llvm/docs/CommandGuide/llvm-remarkutil.rst @@ -58,7 +58,7 @@ .. program:: llvm-remarkutil instruction-count -USAGE: :program:`llvm-remarkutil` instruction-count --parser= -o +USAGE: :program:`llvm-remarkutil` instruction-count --parser= [--use-debug-loc] -o Summary ^^^^^^^ @@ -74,6 +74,12 @@ 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 @@ .. program:: llvm-remarkutil annotation-count -USAGE: :program:`llvm-remarkutil` annotation-count --parser= --annotation-type= -o +USAGE: :program:`llvm-remarkutil` annotation-count --parser= --annotation-type= [--use-debug-loc] -o Summary ^^^^^^^ @@ -96,3 +102,9 @@ :: 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 --- /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 --- /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 --- /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 --- /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 --- /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 --- a/llvm/tools/llvm-remarkutil/RemarkUtil.cpp +++ b/llvm/tools/llvm-remarkutil/RemarkUtil.cpp @@ -61,6 +61,15 @@ 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 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 @@ 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 @@ "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 @@ : 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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();