r203157 - PGO: Add support for Objective-C blocks.

Bob Wilson bob.wilson at apple.com
Thu Mar 6 12:24:28 PST 2014


Author: bwilson
Date: Thu Mar  6 14:24:27 2014
New Revision: 203157

URL: http://llvm.org/viewvc/llvm-project?rev=203157&view=rev
Log:
PGO: Add support for Objective-C blocks.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
    cfe/trunk/test/CodeGenObjC/Inputs/instr-profile.profdata
    cfe/trunk/test/CodeGenObjC/instr-profile.m

Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=203157&r1=203156&r2=203157&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Thu Mar  6 14:24:27 2014
@@ -1200,8 +1200,14 @@ CodeGenFunction::GenerateBlockFunction(G
 
   if (IsLambdaConversionToBlock)
     EmitLambdaBlockInvokeBody();
-  else
+  else {
+    PGO.assignRegionCounters(blockDecl, fn);
+    RegionCounter Cnt = getPGORegionCounter(blockDecl->getBody());
+    Cnt.beginRegion(Builder);
     EmitStmt(blockDecl->getBody());
+    PGO.emitWriteoutFunction();
+    PGO.destroyRegionCounters();
+  }
 
   // Remember where we were...
   llvm::BasicBlock *resume = Builder.GetInsertBlock();

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=203157&r1=203156&r2=203157&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Thu Mar  6 14:24:27 2014
@@ -315,6 +315,10 @@ namespace {
       (*CounterMap)[S->getBody()] = NextCounter++;
       Visit(S->getBody());
     }
+    void VisitBlockDecl(const BlockDecl *S) {
+      (*CounterMap)[S->getBody()] = NextCounter++;
+      Visit(S->getBody());
+    }
     /// Assign a counter to track the block following a label.
     void VisitLabelStmt(const LabelStmt *S) {
       (*CounterMap)[S] = NextCounter++;
@@ -481,6 +485,13 @@ namespace {
       Visit(S->getBody());
     }
 
+    void VisitBlockDecl(const BlockDecl *S) {
+      RegionCounter Cnt(PGO, S->getBody());
+      Cnt.beginRegion();
+      (*CountMap)[S->getBody()] = PGO.getCurrentRegionCount();
+      Visit(S->getBody());
+    }
+
     void VisitReturnStmt(const ReturnStmt *S) {
       RecordStmtCount(S);
       if (S->getRetValue())
@@ -802,6 +813,8 @@ void CodeGenPGO::mapRegionCounters(const
     Walker.VisitFunctionDecl(FD);
   else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
     Walker.VisitObjCMethodDecl(MD);
+  else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
+    Walker.VisitBlockDecl(BD);
   NumRegionCounters = Walker.NextCounter;
 }
 
@@ -812,6 +825,8 @@ void CodeGenPGO::computeRegionCounts(con
     Walker.VisitFunctionDecl(FD);
   else if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
     Walker.VisitObjCMethodDecl(MD);
+  else if (const BlockDecl *BD = dyn_cast_or_null<BlockDecl>(D))
+    Walker.VisitBlockDecl(BD);
 }
 
 void CodeGenPGO::emitCounterVariables() {

Modified: cfe/trunk/test/CodeGenObjC/Inputs/instr-profile.profdata
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/Inputs/instr-profile.profdata?rev=203157&r1=203156&r2=203157&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjC/Inputs/instr-profile.profdata (original)
+++ cfe/trunk/test/CodeGenObjC/Inputs/instr-profile.profdata Thu Mar  6 14:24:27 2014
@@ -1,3 +1,7 @@
+instr-profile.m:__13+[A foreach:]_block_invoke 2
+2
+1
+
 instr-profile.m:+[A foreach:] 2
 1
 2

Modified: cfe/trunk/test/CodeGenObjC/instr-profile.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/instr-profile.m?rev=203157&r1=203156&r2=203157&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjC/instr-profile.m (original)
+++ cfe/trunk/test/CodeGenObjC/instr-profile.m Thu Mar  6 14:24:27 2014
@@ -4,8 +4,8 @@
 // of running the program generated by the -fprofile-instr-generate case. As
 // such, main() should call every function in this test.
 
-// 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
-// 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
+// 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
+// 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
 
 #ifdef HAVE_FOUNDATION
 
@@ -33,7 +33,9 @@ struct NSFastEnumerationState;
 @end;
 #endif
 
-// PGOGEN: @[[FOR:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
+// PGOGEN: @[[FRC:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
+// PGOGEN: @[[BLC:__llvm_pgo_ctr[0-9]*]] = private global [2 x i64] zeroinitializer
+// PGOGEN: @[[MAC:__llvm_pgo_ctr[0-9]*]] = private global [1 x i64] zeroinitializer
 
 @interface A : NSObject
 + (void)foreach: (NSArray *)array;
@@ -42,16 +44,28 @@ struct NSFastEnumerationState;
 @implementation A
 // PGOGEN: define {{.*}}+[A foreach:]
 // PGOUSE: define {{.*}}+[A foreach:]
-// PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 0
+// PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 0
 + (void)foreach: (NSArray *)array
 {
-  // PGOGEN: store {{.*}} @[[FOR]], i64 0, i64 1
+  __block id result;
+  // PGOGEN: store {{.*}} @[[FRC]], i64 0, i64 1
   // FIXME: We don't emit branch weights for this yet.
   for (id x in array) {
+    // PGOGEN: define {{.*}}_block_invoke
+    // PGOUSE: define {{.*}}_block_invoke
+    // PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 0
+    ^{ static int init = 0;
+      // PGOGEN: store {{.*}} @[[BLC]], i64 0, i64 1
+      // PGOUSE: br {{.*}} !prof ![[BL1:[0-9]+]]
+       if (init)
+         result = x;
+       init = 1; }();
   }
 }
 @end
 
+// PGOUSE-DAG: ![[BL1]] = metadata !{metadata !"branch_weights", i32 2, i32 2}
+
 int main(int argc, const char *argv[]) {
   A *a = [[A alloc] init];
   NSArray *array = [NSArray arrayWithObjects: @"0", @"1", (void*)0];





More information about the cfe-commits mailing list