This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Simplify the way lifetime of block captures is extended
ClosedPublic

Authored by ahatanak on Jun 10 2020, 5:47 PM.

Details

Summary

Rather than pushing inactive cleanups for the block captures at the entry of a full expression and activating them during the creation of the block literal, just call pushLifetimeExtendedDestroy to ensure the cleanups are popped at the end of the scope enclosing the block expression.

I've made sure that the lifetime of block captures in a return statement doesn't get extended beyond the end of the full expression except when the BlockDecl isn't in the cleanup object list of the return statement's ExprWithCleanups (see test22 I added to arc-blocks.m).

This addresses the feedback I got in the following review: https://reviews.llvm.org/D64464#inline-690623

rdar://problem/63996471

Diff Detail

Event Timeline

ahatanak created this revision.Jun 10 2020, 5:47 PM

To clarify my comment, the lifetime of the block capture in test22 gets extended to the end of the enclosing scope, but of course not beyond the end of the return statement.

ahatanak edited the summary of this revision. (Show Details)Jun 10 2020, 6:14 PM
rjmccall accepted this revision.Jun 10 2020, 9:28 PM

This is a great improvement, thanks!

This revision is now accepted and ready to land.Jun 10 2020, 9:28 PM
This revision was automatically updated to reflect the committed changes.

Hi @ahatanak , @rjmccall, we found that this diff causes an issue:
When -fobjc-arc is enabled, if a block is passed as an argument to a function whose corresponding parameter type is && rvalue and attributed as noescape, the object captured by the block can get destructed before the block invocation.
More details and code example is in https://bugs.llvm.org/show_bug.cgi?id=50043 we filed.

We've tried locally reverted this patch, and the test cases mentioned in that bug report passed successfully. I am wondering could you please take a look at the bug report and see if you have a quick fix or something? Thanks!!