This is an archive of the discontinued LLVM Phabricator instance.

[SemaCXX] _Pragma("clang optimize off") not affecting lambda.
ClosedPublic

Authored by CarlosAlbertoEnciso on Feb 27 2018, 6:38 AM.

Details

Summary

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.

Diff Detail

Repository
rL LLVM

Event Timeline

erichkeane added inline comments.Mar 6 2018, 6:51 AM
lib/Sema/SemaDecl.cpp
12598 ↗(On Diff #136062)

Should thi sonly happen if this is a definition? Additionally, does this negate the need for the other call to this in ActOnFunctionDeclarator?

lib/Sema/SemaDecl.cpp
12598 ↗(On Diff #136062)

First of all, thanks for your review.

Based on your comments, I found couple of issues with my patch:

For a normal function, the setting of the 'optnone' attribute was done twice:
ActOnFunctionDeclarator and ActOnFinishFunctionBody.

The setting of the 'optnone' attribute is not consistent for both lambda and
normal functions. For normal functions being done at the start of the definition
and for lambdas at the end of the function body.

Based on the previous findings, now I am setting the 'optnone' attribute in
ActOnStartOfLambdaDefinition.

I will update the patch to reflect the changes.

Correct the issues raised by the reviews.

erichkeane accepted this revision.Mar 22 2018, 5:36 AM
This revision is now accepted and ready to land.Mar 22 2018, 5:36 AM
This revision was automatically updated to reflect the committed changes.