CarlosAlbertoEnciso created this revision. CarlosAlbertoEnciso added reviewers: rsmith, erichkeane, andreadb. CarlosAlbertoEnciso added a project: clang.
Declaring "_Pragma("clang optimize off")" before the body of a function with a lambda leads to the lambda functions in the body not being affected. For the below test: _Pragma("clang optimize off") void foo(int p) { auto lambda = [&p]() { ++p; }; lambda(); } _Pragma("clang optimize on") At -O1 the attributes for the 'foo' function are: attributes #0 = { noinline nounwind optnone uwtable ... } At -O1 the attributes for the lambda function are: attributes #1 = { inlinehint norecurse nounwind uwtable ... } The incorrect attribute causes the lambda function to be considered for inlining ignoring the given _Pragma. Repository: rC Clang https://reviews.llvm.org/D43821 Files: lib/Sema/SemaDecl.cpp test/CodeGenCXX/optnone-pragma-optimize-off.cpp Index: test/CodeGenCXX/optnone-pragma-optimize-off.cpp =================================================================== --- test/CodeGenCXX/optnone-pragma-optimize-off.cpp +++ test/CodeGenCXX/optnone-pragma-optimize-off.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 %s -triple %itanium_abi_triple -O1 -disable-llvm-passes -emit-llvm -o - | FileCheck %s + +// Test the attributes for the lambda function contains 'optnone' as result of +// the _Pragma("clang optimize off"). + +_Pragma("clang optimize off") + +void foo(int p) { + auto lambda = [&p]() { ++p; }; + lambda(); + // CHECK: define {{.*}} @"_ZZ3fooiENK3$_0clEv"({{.*}}) #[[LAMBDA_ATR:[0-9]+]] +} + +_Pragma("clang optimize on") + +// CHECK: attributes #[[LAMBDA_ATR]] = { {{.*}} optnone {{.*}} } Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -12593,6 +12593,10 @@ FD->setBody(Body); FD->setWillHaveBody(false); + // This represents the function body for the lambda function, check if we + // have to apply optnone due to a pragma. + AddRangeBasedOptnone(FD); + if (getLangOpts().CPlusPlus14) { if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() && FD->getReturnType()->isUndeducedType()) {
Index: test/CodeGenCXX/optnone-pragma-optimize-off.cpp =================================================================== --- test/CodeGenCXX/optnone-pragma-optimize-off.cpp +++ test/CodeGenCXX/optnone-pragma-optimize-off.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 %s -triple %itanium_abi_triple -O1 -disable-llvm-passes -emit-llvm -o - | FileCheck %s + +// Test the attributes for the lambda function contains 'optnone' as result of +// the _Pragma("clang optimize off"). + +_Pragma("clang optimize off") + +void foo(int p) { + auto lambda = [&p]() { ++p; }; + lambda(); + // CHECK: define {{.*}} @"_ZZ3fooiENK3$_0clEv"({{.*}}) #[[LAMBDA_ATR:[0-9]+]] +} + +_Pragma("clang optimize on") + +// CHECK: attributes #[[LAMBDA_ATR]] = { {{.*}} optnone {{.*}} } Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -12593,6 +12593,10 @@ FD->setBody(Body); FD->setWillHaveBody(false); + // This represents the function body for the lambda function, check if we + // have to apply optnone due to a pragma. + AddRangeBasedOptnone(FD); + if (getLangOpts().CPlusPlus14) { if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() && FD->getReturnType()->isUndeducedType()) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits