<p dir="ltr">Should we do the same for CapturedStmt?</p>
<div class="gmail_quote">On 11 Apr 2014 16:14, "Justin Bogner" <<a href="mailto:mail@justinbogner.com">mail@justinbogner.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: bogner<br>
Date: Fri Apr 11 18:06:35 2014<br>
New Revision: 206081<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=206081&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=206081&view=rev</a><br>
Log:<br>
CodeGen: Fix handling of C++11 lambdas in profiling<br>
<br>
Until now we were generating duplicate counters for lambdas: one set<br>
in the function where the lambda was declared and another for the<br>
lambda itself. Instead, we should skip over the bodies of lambdas in<br>
their containing contexts.<br>
<br>
Added:<br>
cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata<br>
cfe/trunk/test/Profile/cxx-lambda.cpp<br>
Modified:<br>
cfe/trunk/lib/CodeGen/CodeGenPGO.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=206081&r1=206080&r2=206081&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=206081&r1=206080&r2=206081&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Fri Apr 11 18:06:35 2014<br>
@@ -331,9 +331,10 @@ namespace {<br>
MapRegionCounters(llvm::DenseMap<const Stmt *, unsigned> &CounterMap)<br>
: NextCounter(0), CounterMap(CounterMap) {}<br>
<br>
- // Do not traverse the BlockDecl inside a BlockExpr since each BlockDecl<br>
- // is handled as a separate function.<br>
- bool TraverseBlockExpr(BlockExpr *block) { return true; }<br>
+ // Blocks and lambdas are handled as separate functions, so we need not<br>
+ // traverse them in the parent context.<br>
+ bool TraverseBlockExpr(BlockExpr *BE) { return true; }<br>
+ bool TraverseLambdaBody(LambdaExpr *LE) { return true; }<br>
<br>
bool VisitDecl(const Decl *D) {<br>
switch (D->getKind()) {<br>
@@ -431,6 +432,11 @@ namespace {<br>
Visit(D->getBody());<br>
}<br>
<br>
+ // Skip lambda expressions. We visit these as FunctionDecls when we're<br>
+ // generating them and aren't interested in the body when generating a<br>
+ // parent context.<br>
+ void VisitLambdaExpr(const LambdaExpr *LE) {}<br>
+<br>
void VisitObjCMethodDecl(const ObjCMethodDecl *D) {<br>
// Counter tracks entry to the method body.<br>
RegionCounter Cnt(PGO, D->getBody());<br>
<br>
Added: cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata?rev=206081&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata?rev=206081&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata (added)<br>
+++ cfe/trunk/test/Profile/Inputs/cxx-lambda.profdata Fri Apr 11 18:06:35 2014<br>
@@ -0,0 +1,20 @@<br>
+cxx-lambda.cpp:_ZZ7lambdasvENK3$_0clEi<br>
+3<br>
+3<br>
+10<br>
+9<br>
+9<br>
+<br>
+main<br>
+1<br>
+1<br>
+1<br>
+<br>
+_Z7lambdasv<br>
+4<br>
+4<br>
+1<br>
+1<br>
+10<br>
+1<br>
+<br>
<br>
Added: cfe/trunk/test/Profile/cxx-lambda.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/cxx-lambda.cpp?rev=206081&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/cxx-lambda.cpp?rev=206081&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/Profile/cxx-lambda.cpp (added)<br>
+++ cfe/trunk/test/Profile/cxx-lambda.cpp Fri Apr 11 18:06:35 2014<br>
@@ -0,0 +1,57 @@<br>
+// Tests for instrumentation of C++11 lambdas<br>
+<br>
+// 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<br>
+// RUN: FileCheck --input-file=%tgen -check-prefix=PGOGEN %s<br>
+// RUN: FileCheck --input-file=%tgen -check-prefix=LMBGEN %s<br>
+<br>
+// 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<br>
+// RUN: FileCheck --input-file=%tuse -check-prefix=PGOUSE %s<br>
+// RUN: FileCheck --input-file=%tuse -check-prefix=LMBUSE %s<br>
+<br>
+// PGOGEN: @[[LWC:__llvm_profile_counters__Z7lambdasv]] = global [4 x i64] zeroinitializer<br>
+// PGOGEN: @[[MAC:__llvm_profile_counters_main]] = global [1 x i64] zeroinitializer<br>
+// LMBGEN: @[[LFC:"__llvm_profile_counters__ZZ7lambdasvENK3\$_0clEi"]] = internal global [3 x i64] zeroinitializer<br>
+<br>
+// PGOGEN-LABEL: define void @_Z7lambdasv()<br>
+// PGOUSE-LABEL: define void @_Z7lambdasv()<br>
+// PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 0<br>
+void lambdas() {<br>
+ int i = 1;<br>
+<br>
+ // LMBGEN-LABEL: define internal zeroext i1 @"_ZZ7lambdasvENK3$_0clEi"(<br>
+ // LMBUSE-LABEL: define internal zeroext i1 @"_ZZ7lambdasvENK3$_0clEi"(<br>
+ // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 0<br>
+ auto f = [&i](int k) {<br>
+ // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 1<br>
+ // LMBUSE: br {{.*}} !prof ![[LF1:[0-9]+]]<br>
+ if (i > 0) {}<br>
+ // LMBGEN: store {{.*}} @[[LFC]], i64 0, i64 2<br>
+ // LMBUSE: br {{.*}} !prof ![[LF2:[0-9]+]]<br>
+ return k && i;<br>
+ };<br>
+<br>
+ // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 1<br>
+ // PGOUSE: br {{.*}} !prof ![[LW1:[0-9]+]]<br>
+ if (i) {}<br>
+<br>
+ // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 2<br>
+ // PGOUSE: br {{.*}} !prof ![[LW2:[0-9]+]]<br>
+ for (i = 0; i < 10; ++i)<br>
+ f(9 - i);<br>
+<br>
+ // PGOGEN: store {{.*}} @[[LWC]], i64 0, i64 3<br>
+ // PGOUSE: br {{.*}} !prof ![[LW3:[0-9]+]]<br>
+ if (i) {}<br>
+}<br>
+<br>
+// PGOUSE-DAG: ![[LW1]] = metadata !{metadata !"branch_weights", i32 2, i32 1}<br>
+// PGOUSE-DAG: ![[LW2]] = metadata !{metadata !"branch_weights", i32 11, i32 2}<br>
+// PGOUSE-DAG: ![[LW3]] = metadata !{metadata !"branch_weights", i32 2, i32 1}<br>
+<br>
+// LMBUSE-DAG: ![[LF1]] = metadata !{metadata !"branch_weights", i32 10, i32 2}<br>
+// LMBUSE-DAG: ![[LF2]] = metadata !{metadata !"branch_weights", i32 10, i32 2}<br>
+<br>
+int main(int argc, const char *argv[]) {<br>
+ lambdas();<br>
+ return 0;<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>