Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 9970e5f

Browse files
committed
[profiling] PR31992: Don't skip interesting non-base constructors
Fix the fact that we don't assign profile counters to constructors in classes with virtual bases, or constructors with variadic parameters. Differential Revision: https://reviews.llvm.org/D30131 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296062 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9049fa8 commit 9970e5f

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

Diff for: lib/CodeGen/CGClass.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS,
689689
/// complete-to-base constructor delegation optimization, i.e.
690690
/// emitting the complete constructor as a simple call to the base
691691
/// constructor.
692-
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
692+
bool CodeGenFunction::IsConstructorDelegationValid(
693+
const CXXConstructorDecl *Ctor) {
693694

694695
// Currently we disable the optimization for classes with virtual
695696
// bases because (1) the addresses of parameter variables need to be

Diff for: lib/CodeGen/CodeGenFunction.h

+2
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,8 @@ class CodeGenFunction : public CodeGenTypeCache {
15641564
SourceLocation Loc = SourceLocation(),
15651565
SourceLocation StartLoc = SourceLocation());
15661566

1567+
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor);
1568+
15671569
void EmitConstructorBody(FunctionArgList &Args);
15681570
void EmitDestructorBody(FunctionArgList &Args);
15691571
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);

Diff for: lib/CodeGen/CodeGenPGO.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,14 @@ void CodeGenPGO::assignRegionCounters(GlobalDecl GD, llvm::Function *Fn) {
626626
// Constructors and destructors may be represented by several functions in IR.
627627
// If so, instrument only base variant, others are implemented by delegation
628628
// to the base one, it would be counted twice otherwise.
629-
if (CGM.getTarget().getCXXABI().hasConstructorVariants() &&
630-
((isa<CXXConstructorDecl>(D) && GD.getCtorType() != Ctor_Base) ||
631-
(isa<CXXDestructorDecl>(D) && GD.getDtorType() != Dtor_Base))) {
632-
return;
629+
if (CGM.getTarget().getCXXABI().hasConstructorVariants()) {
630+
if (isa<CXXDestructorDecl>(D) && GD.getDtorType() != Dtor_Base)
631+
return;
632+
633+
if (const auto *CCD = dyn_cast<CXXConstructorDecl>(D))
634+
if (GD.getCtorType() != Ctor_Base &&
635+
CodeGenFunction::IsConstructorDelegationValid(CCD))
636+
return;
633637
}
634638
CGM.ClearUnusedCoverageMapping(D);
635639
setFuncName(Fn);

Diff for: test/Profile/Inputs/cxx-class.proftext

+11
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ _ZN6SimpleC2Ei
3939
100
4040
99
4141

42+
_ZN7DerivedC1Ev
43+
10
44+
2
45+
100
46+
99
47+
48+
_ZN7DerivedD2Ev
49+
10
50+
2
51+
100
52+
99

Diff for: test/Profile/cxx-class.cpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
// RUN: FileCheck --input-file=%tgen -check-prefix=DTRGEN %s
66
// RUN: FileCheck --input-file=%tgen -check-prefix=MTHGEN %s
77
// RUN: FileCheck --input-file=%tgen -check-prefix=WRPGEN %s
8+
// RUN: FileCheck --input-file=%tgen -check-prefix=VCTRGEN %s
9+
// RUN: FileCheck --input-file=%tgen -check-prefix=VDTRGEN %s
810

911
// RUN: llvm-profdata merge %S/Inputs/cxx-class.proftext -o %t.profdata
1012
// RUN: %clang_cc1 %s -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -triple %itanium_abi_triple > %tuse
1113
// RUN: FileCheck --input-file=%tuse -check-prefix=CTRUSE %s
1214
// RUN: FileCheck --input-file=%tuse -check-prefix=DTRUSE %s
1315
// RUN: FileCheck --input-file=%tuse -check-prefix=MTHUSE %s
1416
// RUN: FileCheck --input-file=%tuse -check-prefix=WRPUSE %s
17+
// RUN: FileCheck --input-file=%tuse -check-prefix=VCTRUSE %s
18+
// RUN: FileCheck --input-file=%tuse -check-prefix=VDTRUSE %s
1519

1620
class Simple {
17-
int Member;
1821
public:
22+
int Member;
1923
// CTRGEN-LABEL: define {{.*}} @_ZN6SimpleC2Ei(
2024
// CTRUSE-LABEL: define {{.*}} @_ZN6SimpleC2Ei(
2125
// CTRGEN: store {{.*}} @[[SCC:__profc__ZN6SimpleC2Ei]], i64 0, i64 0
@@ -56,13 +60,43 @@ class Simple {
5660
// MTHUSE: ![[SM1]] = !{!"branch_weights", i32 100, i32 2}
5761
};
5862

63+
class Derived : virtual public Simple {
64+
public:
65+
// VCTRGEN-LABEL: define {{.*}} @_ZN7DerivedC1Ev(
66+
// VCTRUSE-LABEL: define {{.*}} @_ZN7DerivedC1Ev(
67+
// VCTRGEN: store {{.*}} @[[SCC:__profc__ZN7DerivedC1Ev]], i64 0, i64 0
68+
Derived() : Simple(0) {
69+
// VCTRGEN: store {{.*}} @[[SCC]], i64 0, i64 1
70+
// VCTRUSE: br {{.*}} !prof ![[SC1:[0-9]+]]
71+
if (Member) {}
72+
// VCTRGEN-NOT: store {{.*}} @[[SCC]],
73+
// VCTRUSE-NOT: br {{.*}} !prof ![0-9]+
74+
// VCTRUSE: ret
75+
}
76+
// VCTRUSE: ![[SC1]] = !{!"branch_weights", i32 100, i32 2}
77+
78+
// VDTRGEN-LABEL: define {{.*}} @_ZN7DerivedD2Ev(
79+
// VDTRUSE-LABEL: define {{.*}} @_ZN7DerivedD2Ev(
80+
// VDTRGEN: store {{.*}} @[[SDC:__profc__ZN7DerivedD2Ev]], i64 0, i64 0
81+
~Derived() {
82+
// VDTRGEN: store {{.*}} @[[SDC]], i64 0, i64 1
83+
// VDTRUSE: br {{.*}} !prof ![[SD1:[0-9]+]]
84+
if (Member) {}
85+
// VDTRGEN-NOT: store {{.*}} @[[SDC]],
86+
// VDTRUSE-NOT: br {{.*}} !prof ![0-9]+
87+
// VDTRUSE: ret
88+
}
89+
// VDTRUSE: ![[SD1]] = !{!"branch_weights", i32 100, i32 2}
90+
};
91+
5992
// WRPGEN-LABEL: define {{.*}} @_Z14simple_wrapperv(
6093
// WRPUSE-LABEL: define {{.*}} @_Z14simple_wrapperv(
6194
// WRPGEN: store {{.*}} @[[SWC:__profc__Z14simple_wrapperv]], i64 0, i64 0
6295
void simple_wrapper() {
6396
// WRPGEN: store {{.*}} @[[SWC]], i64 0, i64 1
6497
// WRPUSE: br {{.*}} !prof ![[SW1:[0-9]+]]
6598
for (int I = 0; I < 100; ++I) {
99+
Derived d;
66100
Simple S(I);
67101
S.method();
68102
}

Diff for: test/Profile/cxx-structors.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ struct Bar : public Foo {
1616
~Bar();
1717
};
1818

19+
struct Baz : virtual public Foo {
20+
Baz() {}
21+
Baz(int x) : Foo(x) {}
22+
~Baz();
23+
};
24+
25+
struct Quux : public Foo {
26+
Quux(const char *y, ...) : Foo(0) {}
27+
};
28+
1929
Foo foo;
2030
Foo foo2(1);
2131
Bar bar;
32+
Baz baz;
33+
Baz baz2(1);
34+
Quux qux("fi", "fo", "fum");
2235

2336
// Profile data for complete constructors and destructors must absent.
2437

38+
// INSTR: @__profc__ZN3BazC1Ev =
39+
// INSTR: @__profc__ZN3BazC1Ei =
40+
// INSTR: @__profc__ZN4QuuxC1EPKcz =
2541
// INSTR: @__profc_main =
2642
// INSTR: @__profc__ZN3FooC2Ev =
2743
// INSTR: @__profc__ZN3FooD2Ev =

0 commit comments

Comments
 (0)