Skip to content

Commit c845c00

Browse files
committed
PGO: Add support for Objective-C blocks.
llvm-svn: 203157
1 parent 6292a14 commit c845c00

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,8 +1200,14 @@ CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
12001200

12011201
if (IsLambdaConversionToBlock)
12021202
EmitLambdaBlockInvokeBody();
1203-
else
1203+
else {
1204+
PGO.assignRegionCounters(blockDecl, fn);
1205+
RegionCounter Cnt = getPGORegionCounter(blockDecl->getBody());
1206+
Cnt.beginRegion(Builder);
12041207
EmitStmt(blockDecl->getBody());
1208+
PGO.emitWriteoutFunction();
1209+
PGO.destroyRegionCounters();
1210+
}
12051211

12061212
// Remember where we were...
12071213
llvm::BasicBlock *resume = Builder.GetInsertBlock();

clang/lib/CodeGen/CodeGenPGO.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ namespace {
315315
(*CounterMap)[S->getBody()] = NextCounter++;
316316
Visit(S->getBody());
317317
}
318+
void VisitBlockDecl(const BlockDecl *S) {
319+
(*CounterMap)[S->getBody()] = NextCounter++;
320+
Visit(S->getBody());
321+
}
318322
/// Assign a counter to track the block following a label.
319323
void VisitLabelStmt(const LabelStmt *S) {
320324
(*CounterMap)[S] = NextCounter++;
@@ -481,6 +485,13 @@ namespace {
481485
Visit(S->getBody());
482486
}
483487

488+
void VisitBlockDecl(const BlockDecl *S) {
489+
RegionCounter Cnt(PGO, S->getBody());
490+
Cnt.beginRegion();
491+
(*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
492+
Visit(S->getBody());
493+
}
494+
484495
void VisitReturnStmt(const ReturnStmt *S) {
485496
RecordStmtCount(S);
486497
if (S->getRetValue())
@@ -802,6 +813,8 @@ void CodeGenPGO::mapRegionCounters(const Decl *D) {
802813
Walker.VisitFunctionDecl(FD);
803814
else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
804815
Walker.VisitObjCMethodDecl(MD);
816+
else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
817+
Walker.VisitBlockDecl(BD);
805818
NumRegionCounters = Walker.NextCounter;
806819
}
807820

@@ -812,6 +825,8 @@ void CodeGenPGO::computeRegionCounts(const Decl *D) {
812825
Walker.VisitFunctionDecl(FD);
813826
else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
814827
Walker.VisitObjCMethodDecl(MD);
828+
else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
829+
Walker.VisitBlockDecl(BD);
815830
}
816831

817832
void CodeGenPGO::emitCounterVariables() {

clang/test/CodeGenObjC/Inputs/instr-profile.profdata

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
instr-profile.m:__13+[A foreach:]_block_invoke 2
2+
2
3+
1
4+
15
instr-profile.m:+[A foreach:] 2
26
1
37
2

clang/test/CodeGenObjC/instr-profile.m

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// of running the program generated by the -fprofile-instr-generate case. As
55
// such, main() should call every function in this test.
66

7-
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name instr-profile.m %s -o - -emit-llvm -fprofile-instr-generate | FileCheck -check-prefix=PGOGEN %s
8-
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name instr-profile.m %s -o - -emit-llvm -fprofile-instr-use=%S/Inputs/instr-profile.profdata | FileCheck -check-prefix=PGOUSE %s
7+
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name instr-profile.m %s -o - -emit-llvm -fblocks -fprofile-instr-generate | FileCheck -check-prefix=PGOGEN %s
8+
// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name instr-profile.m %s -o - -emit-llvm -fblocks -fprofile-instr-use=%S/Inputs/instr-profile.profdata | FileCheck -check-prefix=PGOUSE %s
99

1010
#ifdef HAVE_FOUNDATION
1111

@@ -33,7 +33,9 @@ +(NSArray*) arrayWithObjects: (id) first, ...;
3333
@end;
3434
#endif
3535

36-
// PGOGEN: @[[FOR:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
36+
// PGOGEN: @[[FRC:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
37+
// PGOGEN: @[[BLC:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
38+
// PGOGEN: @[[MAC:__llvm_pgo_ctr[0-9]*]] = private global [1 x i64] zeroinitializer
3739

3840
@interface A : NSObject
3941
+ (void)foreach: (NSArray *)array;
@@ -42,16 +44,28 @@ + (void)foreach: (NSArray *)array;
4244
@implementation A
4345
// PGOGEN: define {{.*}}+[A foreach:]
4446
// PGOUSE: define {{.*}}+[A foreach:]
45-
// PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 0
47+
// PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 0
4648
+ (void)foreach: (NSArray *)array
4749
{
48-
// PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 1
50+
__block id result;
51+
// PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 1
4952
// FIXME: We don't emit branch weights for this yet.
5053
for (id x in array) {
54+
// PGOGEN: define {{.*}}_block_invoke
55+
// PGOUSE: define {{.*}}_block_invoke
56+
// PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 0
57+
^{ static int init = 0;
58+
// PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 1
59+
// PGOUSE: br {{.*}} !prof ![[BL1:[0-9]+]]
60+
if (init)
61+
result = x;
62+
init = 1; }();
5163
}
5264
}
5365
@end
5466

67+
// PGOUSE-DAG: ![[BL1]] = metadata !{metadata !"branch_weights", i32 2, i32 2}
68+
5569
int main(int argc, const char *argv[]) {
5670
A *a = [[A alloc] init];
5771
NSArray *array = [NSArray arrayWithObjects: @"0", @"1", (void*)0];

0 commit comments

Comments
 (0)