Index: llvm/trunk/CMakeLists.txt =================================================================== --- llvm/trunk/CMakeLists.txt +++ llvm/trunk/CMakeLists.txt @@ -1097,3 +1097,7 @@ add_subdirectory(utils/benchmark) add_subdirectory(benchmarks) endif() + +if (LLVM_INCLUDE_UTILS AND LLVM_INCLUDE_TOOLS) + add_subdirectory(utils/llvm-locstats) +endif() Index: llvm/trunk/docs/CommandGuide/index.rst =================================================================== --- llvm/trunk/docs/CommandGuide/index.rst +++ llvm/trunk/docs/CommandGuide/index.rst @@ -74,3 +74,4 @@ llvm-build llvm-exegesis llvm-pdbutil + llvm-locstats Index: llvm/trunk/docs/CommandGuide/llvm-locstats.rst =================================================================== --- llvm/trunk/docs/CommandGuide/llvm-locstats.rst +++ llvm/trunk/docs/CommandGuide/llvm-locstats.rst @@ -0,0 +1,79 @@ +llvm-locstats - calculate statistics on DWARF debug location +============================================================ + +.. program:: llvm-locstats + +SYNOPSIS +-------- + +:program:`llvm-locstats` [*options*] [*filename*] + +DESCRIPTION +----------- + +:program:`llvm-locstats` works like a wrapper around :program:`llvm-dwarfdump`. +It parses :program:`llvm-dwarfdump` statistics regarding debug location by +pretty printing it in a more human readable way. + +The line 0% shows the number and the percentage of DIEs with no location +information, but the line 100% shows the information for DIEs where there is +location information in all code section bytes (where the variable or parameter +is in the scope). The line 50-59% shows the number and the percentage of DIEs +where the location information is between 50 and 59 percentage of its scope +covered. + +OPTIONS +------- + +.. option:: -only-variables + + Calculate the location statistics only for local variables. + +.. option:: -only-formal-parameters + + Calculate the location statistics only for formal parameters. + +.. option:: -ignore-debug-entry-values + + Ignore the location statistics on locations containing the + debug entry values DWARF operation. + +EXIT STATUS +----------- + +:program:`llvm-locstats` returns 0 if the input file were parsed +successfully. Otherwise, it returns 1. + +OUTPUT EXAMPLE +-------------- + +.. code-block:: none + + ================================================= + Debug Location Statistics + ================================================= + cov% samples percentage(~) + ------------------------------------------------- + 0% 1 16% + 1-9% 0 0% + 10-19% 0 0% + 20-29% 0 0% + 30-39% 0 0% + 40-49% 0 0% + 50-99% 1 16% + 60-69% 0 0% + 70-79% 0 0% + 80-89% 1 16% + 90-99% 0 0% + 100% 3 50% + ================================================= + -the number of debug variables processed: 6 + -PC ranges covered: 81% + ------------------------------------------------- + -total availability: 83% + ================================================= + +SEE ALSO +-------- + +:manpage:`llvm-dwarfdump(1)` Index: llvm/trunk/test/lit.cfg.py =================================================================== --- llvm/trunk/test/lit.cfg.py +++ llvm/trunk/test/lit.cfg.py @@ -124,6 +124,11 @@ opt_viewer_cmd = '%s %s/tools/opt-viewer/opt-viewer.py' % (sys.executable, config.llvm_src_root) +llvm_locstats_tool = os.path.join(config.llvm_tools_dir, 'llvm-locstats') +config.substitutions.append( + ('%llvm-locstats', "'%s' %s" % (config.python_executable, llvm_locstats_tool))) +config.llvm_locstats_used = os.path.exists(llvm_locstats_tool) + tools = [ ToolSubst('%lli', FindTool('lli'), post='.', extra_args=lli_args), ToolSubst('%llc_dwarf', FindTool('llc'), extra_args=llc_args), Index: llvm/trunk/test/tools/llvm-locstats/lit.local.cfg =================================================================== --- llvm/trunk/test/tools/llvm-locstats/lit.local.cfg +++ llvm/trunk/test/tools/llvm-locstats/lit.local.cfg @@ -0,0 +1,2 @@ +if not config.llvm_locstats_used: + config.unsupported = True Index: llvm/trunk/test/tools/llvm-locstats/locstats.ll =================================================================== --- llvm/trunk/test/tools/llvm-locstats/locstats.ll +++ llvm/trunk/test/tools/llvm-locstats/locstats.ll @@ -0,0 +1,173 @@ +; RUN: llc %s -o %t0.o -filetype=obj \ +; RUN: | %llvm-locstats %t0.o | FileCheck %s --check-prefix=LOCSTATS +; +; Test the llvm-locstats output. +; LOCSTATS: 0% 0 0% +; LOCSTATS: 1-9% 0 0% +; LOCSTATS: 10-19% 0 0% +; LOCSTATS: 20-29% 1 11% +; LOCSTATS: 30-39% 0 0% +; LOCSTATS: 40-49% 1 11% +; LOCSTATS: 50-59% 1 11% +; LOCSTATS: 60-69% 1 11% +; LOCSTATS: 70-79% 0 0% +; LOCSTATS: 80-89% 2 22% +; LOCSTATS: 90-99% 1 11% +; LOCSTATS: 100% 2 22% +; +; The source code of the test case: +;extern int fn2 (int); +; +;__attribute__((noinline)) +;int +;fn1 (int *x, int *y) +;{ +; int a = *x; +; int b = *y; +; int local = a + b; +; if (a > 1) { +; local += 2; +; ++local; +; if (local > 200) +; local -= fn2(a); +; } else { +; local += 3; +; ++local; +; local += fn2(a); +; } +; if (b > 4) +; local += a; +; int local2 = 7; +; local -= fn2 (local2); +; return local; +;} +; +;__attribute__((noinline)) +;int f() +;{ +; int l, k; +; int res = 0; +; res += fn1 (&l, &k); +; return res; +;} +; +; ModuleID = 'locstats.c' +source_filename = "locstats.c" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Function Attrs: noinline nounwind uwtable +define dso_local i32 @fn1(i32* nocapture readonly %0, i32* nocapture readonly %1) local_unnamed_addr !dbg !7 { + call void @llvm.dbg.value(metadata i32* %0, metadata !13, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32* %1, metadata !14, metadata !DIExpression()), !dbg !19 + %3 = load i32, i32* %0, align 4, !dbg !20 + call void @llvm.dbg.value(metadata i32 %3, metadata !15, metadata !DIExpression()), !dbg !19 + %4 = load i32, i32* %1, align 4, !dbg !20 + call void @llvm.dbg.value(metadata i32 %4, metadata !16, metadata !DIExpression()), !dbg !19 + %5 = add nsw i32 %4, %3, !dbg !20 + call void @llvm.dbg.value(metadata i32 %5, metadata !17, metadata !DIExpression()), !dbg !19 + %6 = icmp sgt i32 %3, 1, !dbg !20 + br i1 %6, label %7, label %13, !dbg !22 + +7: ; preds = %2 + call void @llvm.dbg.value(metadata i32 %5, metadata !17, metadata !DIExpression(DW_OP_plus_uconst, 2, DW_OP_stack_value)), !dbg !19 + %8 = add nsw i32 %5, 3, !dbg !23 + call void @llvm.dbg.value(metadata i32 %8, metadata !17, metadata !DIExpression()), !dbg !19 + %9 = icmp sgt i32 %8, 200, !dbg !25 + br i1 %9, label %10, label %17, !dbg !27 + +10: ; preds = %7 + %11 = tail call i32 @fn2(i32 %3), !dbg !27 + %12 = sub nsw i32 %8, %11, !dbg !27 + call void @llvm.dbg.value(metadata i32 %12, metadata !17, metadata !DIExpression()), !dbg !19 + br label %17, !dbg !27 + +13: ; preds = %2 + call void @llvm.dbg.value(metadata i32 %5, metadata !17, metadata !DIExpression(DW_OP_plus_uconst, 3, DW_OP_stack_value)), !dbg !19 + %14 = add nsw i32 %5, 4, !dbg !28 + call void @llvm.dbg.value(metadata i32 %14, metadata !17, metadata !DIExpression()), !dbg !19 + %15 = tail call i32 @fn2(i32 %3), !dbg !30 + %16 = add nsw i32 %14, %15, !dbg !30 + call void @llvm.dbg.value(metadata i32 %16, metadata !17, metadata !DIExpression()), !dbg !19 + br label %17 + +17: ; preds = %7, %10, %13 + %18 = phi i32 [ %12, %10 ], [ %8, %7 ], [ %16, %13 ], !dbg !31 + call void @llvm.dbg.value(metadata i32 %18, metadata !17, metadata !DIExpression()), !dbg !19 + %19 = icmp sgt i32 %4, 4, !dbg !32 + %20 = select i1 %19, i32 %3, i32 0, !dbg !34 + %21 = add nsw i32 %18, %20, !dbg !34 + call void @llvm.dbg.value(metadata i32 %21, metadata !17, metadata !DIExpression()), !dbg !19 + call void @llvm.dbg.value(metadata i32 7, metadata !18, metadata !DIExpression()), !dbg !19 + %22 = tail call i32 @fn2(i32 7), !dbg !34 + %23 = sub i32 %21, %22, !dbg !34 + call void @llvm.dbg.value(metadata i32 %23, metadata !17, metadata !DIExpression()), !dbg !19 + ret i32 %23, !dbg !34 +} + +declare dso_local i32 @fn2(i32) local_unnamed_addr + +; Function Attrs: noinline nounwind uwtable +define dso_local i32 @f() local_unnamed_addr !dbg !35 { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = bitcast i32* %1 to i8*, !dbg !42 + %4 = bitcast i32* %2 to i8*, !dbg !42 + call void @llvm.dbg.value(metadata i32 0, metadata !41, metadata !DIExpression()), !dbg !42 + call void @llvm.dbg.value(metadata i32* %1, metadata !39, metadata !DIExpression(DW_OP_deref)), !dbg !42 + call void @llvm.dbg.value(metadata i32* %2, metadata !40, metadata !DIExpression(DW_OP_deref)), !dbg !42 + %5 = call i32 @fn1(i32* nonnull %1, i32* nonnull %2), !dbg !42 + call void @llvm.dbg.value(metadata i32 %5, metadata !41, metadata !DIExpression()), !dbg !42 + ret i32 %5, !dbg !42 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "locstats.c", directory: "/dir") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 10.0.0"} +!7 = distinct !DISubprogram(name: "fn1", scope: !1, file: !1, line: 5, type: !8, scopeLine: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !11, !11} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64) +!12 = !{!13, !14, !15, !16, !17, !18} +!13 = !DILocalVariable(name: "x", arg: 1, scope: !7, file: !1, line: 5, type: !11) +!14 = !DILocalVariable(name: "y", arg: 2, scope: !7, file: !1, line: 5, type: !11) +!15 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 7, type: !10) +!16 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 8, type: !10) +!17 = !DILocalVariable(name: "local", scope: !7, file: !1, line: 9, type: !10) +!18 = !DILocalVariable(name: "local2", scope: !7, file: !1, line: 22, type: !10) +!19 = !DILocation(line: 0, scope: !7) +!20 = !DILocation(line: 7, column: 11, scope: !7) +!21 = distinct !DILexicalBlock(scope: !7, file: !1, line: 10, column: 7) +!22 = !DILocation(line: 10, column: 7, scope: !7) +!23 = !DILocation(line: 12, column: 5, scope: !24) +!24 = distinct !DILexicalBlock(scope: !21, file: !1, line: 10, column: 14) +!25 = !DILocation(line: 13, column: 15, scope: !26) +!26 = distinct !DILexicalBlock(scope: !24, file: !1, line: 13, column: 9) +!27 = !DILocation(line: 13, column: 9, scope: !24) +!28 = !DILocation(line: 17, column: 5, scope: !26) +!29 = distinct !DILexicalBlock(scope: !21, file: !1, line: 15, column: 10) +!30 = !DILocation(line: 18, column: 14, scope: !29) +!31 = !DILocation(line: 0, scope: !21) +!32 = !DILocation(line: 20, column: 9, scope: !33) +!33 = distinct !DILexicalBlock(scope: !7, file: !1, line: 20, column: 7) +!34 = !DILocation(line: 20, column: 7, scope: !7) +!35 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 28, type: !36, scopeLine: 29, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !38) +!36 = !DISubroutineType(types: !37) +!37 = !{!10} +!38 = !{!39, !40, !41} +!39 = !DILocalVariable(name: "l", scope: !35, file: !1, line: 30, type: !10) +!40 = !DILocalVariable(name: "k", scope: !35, file: !1, line: 30, type: !10) +!41 = !DILocalVariable(name: "res", scope: !35, file: !1, line: 31, type: !10) +!42 = !DILocation(line: 30, column: 3, scope: !35) Index: llvm/trunk/utils/llvm-locstats/CMakeLists.txt =================================================================== --- llvm/trunk/utils/llvm-locstats/CMakeLists.txt +++ llvm/trunk/utils/llvm-locstats/CMakeLists.txt @@ -0,0 +1,7 @@ +if (LLVM_BUILD_UTILS AND LLVM_BUILD_TOOLS) + add_custom_target(llvm-locstats ALL + COMMAND ${CMAKE_COMMAND} -E copy ${LLVM_MAIN_SRC_DIR}/utils/llvm-locstats/llvm-locstats.py ${LLVM_TOOLS_BINARY_DIR}/llvm-locstats + COMMENT "Copying llvm-locstats into ${LLVM_TOOLS_BINARY_DIR}" + ) + set_target_properties(llvm-locstats PROPERTIES FOLDER "Tools") +endif() Index: llvm/trunk/utils/llvm-locstats/llvm-locstats.py =================================================================== --- llvm/trunk/utils/llvm-locstats/llvm-locstats.py +++ llvm/trunk/utils/llvm-locstats/llvm-locstats.py @@ -0,0 +1,208 @@ +#!/usr/bin/env python +# +# This is a tool that works like debug location coverage calculator. +# It parses the llvm-dwarfdump --statistics output by reporting it +# in a more human readable way. +# + +from __future__ import print_function +import argparse +import os +import sys +from json import loads +from math import ceil +from subprocess import Popen, PIPE + +def coverage_buckets(): + yield '0%' + yield '1-9%' + for start in range(10, 91, 10): + yield '{0}-{1}%'.format(start, start + 9) + yield '100%' + +def locstats_output( + variables_total, + variables_total_locstats, + variables_with_loc, + scope_bytes_covered, + scope_bytes_from_first_def, + variables_coverage_map + ): + + pc_ranges_covered = int(ceil(scope_bytes_covered * 100.0) + / scope_bytes_from_first_def) + variables_coverage_per_map = {} + for cov_bucket in coverage_buckets(): + variables_coverage_per_map[cov_bucket] = \ + int(ceil(variables_coverage_map[cov_bucket] * 100.0) \ + / variables_total_locstats) + + print (' =================================================') + print (' Debug Location Statistics ') + print (' =================================================') + print (' cov% samples percentage(~) ') + print (' -------------------------------------------------') + for cov_bucket in coverage_buckets(): + print (' {0:6} {1:8d} {2:3d}%'. \ + format(cov_bucket, variables_coverage_map[cov_bucket], \ + variables_coverage_per_map[cov_bucket])) + print (' =================================================') + print (' -the number of debug variables processed: ' \ + + str(variables_total_locstats)) + print (' -PC ranges covered: ' + str(pc_ranges_covered) + '%') + + # Only if we are processing all the variables output the total + # availability. + if variables_total and variables_with_loc: + total_availability = int(ceil(variables_with_loc * 100.0) \ + / variables_total) + print (' -------------------------------------------------') + print (' -total availability: ' + str(total_availability) + '%') + print (' =================================================') + +def parse_program_args(parser): + parser.add_argument('-only-variables', action='store_true', + default=False, + help='calculate the location statistics only for ' + 'local variables' + ) + parser.add_argument('-only-formal-parameters', action='store_true', + default=False, + help='calculate the location statistics only for ' + 'formal parameters' + ) + parser.add_argument('-ignore-debug-entry-values', action='store_true', + default=False, + help='ignore the location statistics on locations with ' + 'entry values' + ) + parser.add_argument('file_name', type=str, help='file to process') + return parser.parse_args() + + +def Main(): + parser = argparse.ArgumentParser() + results = parse_program_args(parser) + + if len(sys.argv) < 2: + print ('error: Too few arguments.') + parser.print_help() + sys.exit(1) + + if results.only_variables and results.only_formal_parameters: + print ('error: Please use just one only* option.') + parser.print_help() + sys.exit(1) + + # These will be different due to different options enabled. + variables_total = None + variables_total_locstats = None + variables_with_loc = None + variables_scope_bytes_covered = None + variables_scope_bytes_from_first_def = None + variables_scope_bytes_entry_values = None + variables_coverage_map = {} + binary = results.file_name + + # Get the directory of the LLVM tools. + llvm_dwarfdump_cmd = os.path.join(os.path.dirname(__file__), \ + "llvm-dwarfdump") + # The statistics llvm-dwarfdump option. + llvm_dwarfdump_stats_opt = "--statistics" + + subproc = Popen([llvm_dwarfdump_cmd, llvm_dwarfdump_stats_opt, binary], \ + stdin=PIPE, stdout=PIPE, stderr=PIPE, \ + universal_newlines = True) + cmd_stdout, cmd_stderr = subproc.communicate() + + # Get the JSON and parse it. + json_parsed = None + + try: + json_parsed = loads(cmd_stdout) + except: + print ('error: No valid llvm-dwarfdump statistics found.') + sys.exit(1) + + if results.only_variables: + # Read the JSON only for local variables. + variables_total_locstats = \ + json_parsed['total vars procesed by location statistics'] + variables_scope_bytes_covered = \ + json_parsed['vars scope bytes covered'] + variables_scope_bytes_from_first_def = \ + json_parsed['vars scope bytes total'] + if not results.ignore_debug_entry_values: + for cov_bucket in coverage_buckets(): + cov_category = "vars with {} of its scope covered".format(cov_bucket) + variables_coverage_map[cov_bucket] = json_parsed[cov_category] + else: + variables_scope_bytes_entry_values = \ + json_parsed['vars entry value scope bytes covered'] + variables_scope_bytes_covered = variables_scope_bytes_covered \ + - variables_scope_bytes_entry_values + for cov_bucket in coverage_buckets(): + cov_category = \ + "vars (excluding the debug entry values) " \ + "with {} of its scope covered".format(cov_bucket) + variables_coverage_map[cov_bucket] = json_parsed[cov_category] + elif results.only_formal_parameters: + # Read the JSON only for formal parameters. + variables_total_locstats = \ + json_parsed['total params procesed by location statistics'] + variables_scope_bytes_covered = \ + json_parsed['formal params scope bytes covered'] + variables_scope_bytes_from_first_def = \ + json_parsed['formal params scope bytes total'] + if not results.ignore_debug_entry_values: + for cov_bucket in coverage_buckets(): + cov_category = "params with {} of its scope covered".format(cov_bucket) + variables_coverage_map[cov_bucket] = json_parsed[cov_category] + else: + variables_scope_bytes_entry_values = \ + json_parsed['formal params entry value scope bytes covered'] + variables_scope_bytes_covered = variables_scope_bytes_covered \ + - variables_scope_bytes_entry_values + for cov_bucket in coverage_buckets(): + cov_category = \ + "params (excluding the debug entry values) " \ + "with {} of its scope covered".format(cov_bucket) + else: + # Read the JSON for both local variables and formal parameters. + variables_total = \ + json_parsed['source variables'] + variables_with_loc = json_parsed['variables with location'] + variables_total_locstats = \ + json_parsed['total variables procesed by location statistics'] + variables_scope_bytes_covered = \ + json_parsed['scope bytes covered'] + variables_scope_bytes_from_first_def = \ + json_parsed['scope bytes total'] + if not results.ignore_debug_entry_values: + for cov_bucket in coverage_buckets(): + cov_category = "variables with {} of its scope covered". \ + format(cov_bucket) + variables_coverage_map[cov_bucket] = json_parsed[cov_category] + else: + variables_scope_bytes_entry_values = \ + json_parsed['entry value scope bytes covered'] + variables_scope_bytes_covered = variables_scope_bytes_covered \ + - variables_scope_bytes_entry_values + for cov_bucket in coverage_buckets(): + cov_category = "variables (excluding the debug entry values) " \ + "with {} of its scope covered". format(cov_bucket) + variables_coverage_map[cov_bucket] = json_parsed[cov_category] + + # Pretty print collected info. + locstats_output( + variables_total, + variables_total_locstats, + variables_with_loc, + variables_scope_bytes_covered, + variables_scope_bytes_from_first_def, + variables_coverage_map + ) + +if __name__ == '__main__': + Main() + sys.exit(0)