r206081 - CodeGen: Fix handling of C++11 lambdas in profiling

Justin Bogner mail at justinbogner.com
Fri Apr 11 16:06:36 PDT 2014


Author: bogner
Date: Fri Apr 11 18:06:35 2014
New Revision: 206081

URL: http://llvm.org/viewvc/llvm-project?rev=206081&view=rev
Log:
CodeGen: Fix handling of C++11 lambdas in profiling

Until now we were generating duplicate counters for lambdas: one set
in the function where the lambda was declared and another for the
lambda itself. Instead, we should skip over the bodies of lambdas in
their containing contexts.

Added:
    cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata
    cfe/trunk/test/Profile/cxx-lambda.cpp
Modified:
    cfe/trunk/lib/CodeGen/CodeGenPGO.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=206081&r1=206080&r2=206081&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Fri Apr 11 18:06:35 2014
@@ -331,9 +331,10 @@ namespace {
     MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)
         : NextCounter(0), CounterMap(CounterMap) {}
 
-    // Do not traverse the BlockDecl inside a BlockExpr since each BlockDecl
-    // is handled as a separate function.
-    bool TraverseBlockExpr(BlockExpr *block) { return true; }
+    // Blocks and lambdas are handled as separate functions, so we need not
+    // traverse them in the parent context.
+    bool TraverseBlockExpr(BlockExpr *BE) { return true; }
+    bool TraverseLambdaBody(LambdaExpr *LE) { return true; }
 
     bool VisitDecl(const Decl *D) {
       switch (D->getKind()) {
@@ -431,6 +432,11 @@ namespace {
       Visit(D->getBody());
     }
 
+    // Skip lambda expressions. We visit these as FunctionDecls when we're
+    // generating them and aren't interested in the body when generating a
+    // parent context.
+    void VisitLambdaExpr(const LambdaExpr *LE) {}
+
     void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
       // Counter tracks entry to the method body.
       RegionCounter Cnt(PGO, D->getBody());

Added: cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata?rev=206081&view=auto
==============================================================================
--- cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata (added)
+++ cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata Fri Apr 11 18:06:35 2014
@@ -0,0 +1,20 @@
+cxx-lambda.cpp:_ZZ7lambdasvENK3$_0clEi
+3
+3
+10
+9
+9
+
+main
+1
+1
+1
+
+_Z7lambdasv
+4
+4
+1
+1
+10
+1
+

Added: cfe/trunk/test/Profile/cxx-lambda.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/cxx-lambda.cpp?rev=206081&view=auto
==============================================================================
--- cfe/trunk/test/Profile/cxx-lambda.cpp (added)
+++ cfe/trunk/test/Profile/cxx-lambda.cpp Fri Apr 11 18:06:35 2014
@@ -0,0 +1,57 @@
+// Tests for instrumentation of C++11 lambdas
+
+// RUN: %clang_cc1 -x c++ %s -triple %itanium_abi_triple -main-file-name cxx-lambda.cpp -std=c++11 -o - -emit-llvm -fprofile-instr-generate > %tgen
+// RUN: FileCheck --input-file=%tgen -check-prefix=PGOGEN %s
+// RUN: FileCheck --input-file=%tgen -check-prefix=LMBGEN %s
+
+// RUN: %clang_cc1 -x c++ %s -triple %itanium_abi_triple -main-file-name cxx-lambda.cpp -std=c++11 -o - -emit-llvm -fprofile-instr-use=%S/Inputs/cxx-lambda.profdata > %tuse
+// RUN: FileCheck --input-file=%tuse -check-prefix=PGOUSE %s
+// RUN: FileCheck --input-file=%tuse -check-prefix=LMBUSE %s
+
+// PGOGEN: @[[LWC:__llvm_profile_counters__Z7lambdasv]] = global [4 x i64] zeroinitializer
+// PGOGEN: @[[MAC:__llvm_profile_counters_main]] = global [1 x i64] zeroinitializer
+// LMBGEN: @[[LFC:"__llvm_profile_counters__ZZ7lambdasvENK3\$_0clEi"]] = internal global [3 x i64] zeroinitializer
+
+// PGOGEN-LABEL: define void @_Z7lambdasv()
+// PGOUSE-LABEL: define void @_Z7lambdasv()
+// PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 0
+void lambdas() {
+  int i = 1;
+
+  // LMBGEN-LABEL: define internal zeroext i1 @"_ZZ7lambdasvENK3$_0clEi"(
+  // LMBUSE-LABEL: define internal zeroext i1 @"_ZZ7lambdasvENK3$_0clEi"(
+  // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 0
+  auto f = [&i](int k) {
+    // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 1
+    // LMBUSE: br {{.*}} !prof ![[LF1:[0-9]+]]
+    if (i > 0) {}
+    // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 2
+    // LMBUSE: br {{.*}} !prof ![[LF2:[0-9]+]]
+    return k && i;
+  };
+
+  // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 1
+  // PGOUSE: br {{.*}} !prof ![[LW1:[0-9]+]]
+  if (i) {}
+
+  // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 2
+  // PGOUSE: br {{.*}} !prof ![[LW2:[0-9]+]]
+  for (i = 0; i < 10; ++i)
+    f(9 - i);
+
+  // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 3
+  // PGOUSE: br {{.*}} !prof ![[LW3:[0-9]+]]
+  if (i) {}
+}
+
+// PGOUSE-DAG: ![[LW1]] = metadata !{metadata !"branch_weights", i32 2, i32 1}
+// PGOUSE-DAG: ![[LW2]] = metadata !{metadata !"branch_weights", i32 11, i32 2}
+// PGOUSE-DAG: ![[LW3]] = metadata !{metadata !"branch_weights", i32 2, i32 1}
+
+// LMBUSE-DAG: ![[LF1]] = metadata !{metadata !"branch_weights", i32 10, i32 2}
+// LMBUSE-DAG: ![[LF2]] = metadata !{metadata !"branch_weights", i32 10, i32 2}
+
+int main(int argc, const char *argv[]) {
+  lambdas();
+  return 0;
+}





More information about the cfe-commits mailing list