Skip to content

Commit 57839e5

Browse files
committed
[llvm-dwarfdump][Statistics] Count more than one conrete out-of-line instances of a function
Here may be more than one out-of-line instance of the same function among different CUs. All of them should be accounted for to get an accurate total number of variables/parameters. Reviewed by: aprantl Differential Revision: https://reviews.llvm.org/D73002
1 parent 276a6b8 commit 57839e5

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
; RUN: llc -O0 %s -o - -filetype=obj \
2+
; RUN: | llvm-dwarfdump -statistics - | FileCheck %s
3+
4+
; Test checks that statistics accounts two and more out-of-line instances
5+
; of a function and reports the correct number of variables.
6+
7+
; $ cat test.h
8+
;
9+
; int foo(int a) { return a; }
10+
;
11+
; $ cat test1.cpp
12+
;
13+
; #include "test.h"
14+
; int bar() { return foo(42); }
15+
;
16+
; $ cat test2.cpp
17+
;
18+
; #include "test.h"
19+
; int far() { return foo(42); }
20+
21+
; CHECK: "source functions":3
22+
; CHECK-SAME: "source functions with location":3
23+
; CHECK-SAME: "inlined functions":0
24+
; CHECK-SAME: "unique source variables":1
25+
; CHECK-SAME: "source variables":2
26+
; CHECK-SAME: "variables with location":2
27+
28+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
29+
target triple = "x86_64-unknown-linux-gnu"
30+
31+
; Function Attrs: noinline nounwind optnone uwtable
32+
define dso_local i32 @_Z3fooi.1(i32 %a) !dbg !9 {
33+
entry:
34+
%a.addr = alloca i32, align 4
35+
store i32 %a, i32* %a.addr, align 4
36+
call void @llvm.dbg.declare(metadata i32* %a.addr, metadata !14, metadata !DIExpression()), !dbg !15
37+
%0 = load i32, i32* %a.addr, align 4, !dbg !15
38+
ret i32 %0, !dbg !15
39+
}
40+
; Function Attrs: nounwind readnone speculatable willreturn
41+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
42+
; Function Attrs: noinline nounwind optnone uwtable
43+
define dso_local i32 @_Z3barv() !dbg !18 {
44+
entry:
45+
%call = call i32 @_Z3fooi.1(i32 42), !dbg !21
46+
ret i32 %call, !dbg !21
47+
}
48+
; Function Attrs: noinline nounwind optnone uwtable
49+
define dso_local i32 @_Z3fooi(i32 %a) !dbg !23 {
50+
entry:
51+
%a.addr = alloca i32, align 4
52+
store i32 %a, i32* %a.addr, align 4
53+
call void @llvm.dbg.declare(metadata i32* %a.addr, metadata !24, metadata !DIExpression()), !dbg !25
54+
%0 = load i32, i32* %a.addr, align 4, !dbg !25
55+
ret i32 %0, !dbg !25
56+
}
57+
; Function Attrs: noinline nounwind optnone uwtable
58+
define dso_local i32 @_Z3farv() !dbg !28 {
59+
entry:
60+
%call = call i32 @_Z3fooi(i32 42), !dbg !29
61+
ret i32 %call, !dbg !29
62+
}
63+
64+
!llvm.dbg.cu = !{!0, !3}
65+
!llvm.ident = !{!5, !5}
66+
!llvm.module.flags = !{!6, !7, !8}
67+
68+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
69+
!1 = !DIFile(filename: "test1.cpp", directory: "/")
70+
!2 = !{}
71+
!3 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !4, producer: "clang version 11.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
72+
!4 = !DIFile(filename: "test2.cpp", directory: "/")
73+
!5 = !{!"clang version 11.0.0"}
74+
!6 = !{i32 7, !"Dwarf Version", i32 4}
75+
!7 = !{i32 2, !"Debug Info Version", i32 3}
76+
!8 = !{i32 1, !"wchar_size", i32 4}
77+
!9 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !10, file: !10, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
78+
!10 = !DIFile(filename: "./test.h", directory: "/")
79+
!11 = !DISubroutineType(types: !12)
80+
!12 = !{!13, !13}
81+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
82+
!14 = !DILocalVariable(name: "a", arg: 1, scope: !9, file: !10, line: 1, type: !13)
83+
!15 = !DILocation(line: 1, column: 13, scope: !9)
84+
!18 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !1, file: !1, line: 3, type: !19, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
85+
!19 = !DISubroutineType(types: !20)
86+
!20 = !{!13}
87+
!21 = !DILocation(line: 3, column: 20, scope: !18)
88+
!23 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !10, file: !10, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !3, retainedNodes: !2)
89+
!24 = !DILocalVariable(name: "a", arg: 1, scope: !23, file: !10, line: 1, type: !13)
90+
!25 = !DILocation(line: 1, column: 13, scope: !23)
91+
!28 = distinct !DISubprogram(name: "far", linkageName: "_Z3farv", scope: !4, file: !4, line: 3, type: !19, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !3, retainedNodes: !2)
92+
!29 = !DILocation(line: 3, column: 20, scope: !28)

llvm/tools/llvm-dwarfdump/Statistics.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ constexpr int NumOfCoverageCategories = 12;
2222
struct PerFunctionStats {
2323
/// Number of inlined instances of this function.
2424
unsigned NumFnInlined = 0;
25+
/// Number of out-of-line instances of this function.
26+
unsigned NumFnOutOfLine = 0;
2527
/// Number of inlined instances that have abstract origins.
2628
unsigned NumAbstractOrigins = 0;
2729
/// Number of variables and parameters with location across all inlined
@@ -33,9 +35,6 @@ struct PerFunctionStats {
3335
StringSet<> VarsInFunction;
3436
/// Compile units also cover a PC range, but have this flag set to false.
3537
bool IsFunction = false;
36-
/// Verify function definition has PC addresses (for detecting when
37-
/// a function has been inlined everywhere).
38-
bool HasPCAddresses = false;
3938
/// Function has source location information.
4039
bool HasSourceLocation = false;
4140
/// Number of function parameters.
@@ -399,16 +398,16 @@ static void collectStatsRecursive(DWARFDie Die, std::string FnPrefix,
399398
if (Die.find(dwarf::DW_AT_inline))
400399
return;
401400
std::string FnID = constructDieID(Die);
402-
// We've seen an (inlined) instance of this function.
401+
// We've seen an instance of this function.
403402
auto &FnStats = FnStatMap[FnID];
404403
FnStats.IsFunction = true;
405404
if (IsInlinedFunction) {
406405
FnStats.NumFnInlined++;
407406
if (Die.findRecursively(dwarf::DW_AT_abstract_origin))
408407
FnStats.NumAbstractOrigins++;
408+
} else {
409+
FnStats.NumFnOutOfLine++;
409410
}
410-
if (BytesInThisScope && !IsInlinedFunction)
411-
FnStats.HasPCAddresses = true;
412411
if (Die.findRecursively(dwarf::DW_AT_decl_file) &&
413412
Die.findRecursively(dwarf::DW_AT_decl_line))
414413
FnStats.HasSourceLocation = true;
@@ -521,9 +520,10 @@ bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
521520
unsigned VarWithLoc = 0;
522521
for (auto &Entry : Statistics) {
523522
PerFunctionStats &Stats = Entry.getValue();
524-
unsigned TotalVars = Stats.VarsInFunction.size() * Stats.NumFnInlined;
525-
// Count variables in concrete out-of-line functions and in global scope.
526-
if (Stats.HasPCAddresses || !Stats.IsFunction)
523+
unsigned TotalVars = Stats.VarsInFunction.size() *
524+
(Stats.NumFnInlined + Stats.NumFnOutOfLine);
525+
// Count variables in global scope.
526+
if (!Stats.IsFunction)
527527
TotalVars += Stats.VarsInFunction.size();
528528
unsigned Constants = Stats.ConstantMembers;
529529
VarParamWithLoc += Stats.TotalVarWithLoc + Constants;

0 commit comments

Comments
 (0)