[cfe-dev] [Lifetime, Temporary] Generate ExprWithCleanups for trivially destructible tempoararies

Tim Shen via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 28 11:13:29 PDT 2016


As discussed off the list, ExprWithCleanups should be used for dtors only.
So another solution can be creating a new LifetimeEndStack in
CodeGenFunction, which is handled separately.

The problem of this solution is that, since LifetimeEnd marks and dtor
calls are not in the same stack, they are not emitted interleaved. The
result will be like:
  call void dtor2();
  call void dtor1();
  llvm.lifetime.end(t2);
  llvm.lifetime.end(t1);

instead of, ideally:
  call void dtor2();
  llvm.lifetime.end(t2);
  call void dtor1();
  llvm.lifetime.end(t1);

This approach is good enough to reduce the stack frame size, though.

On Tue, Apr 26, 2016 at 9:51 PM Tim Shen <timshen at google.com> wrote:

> Hi, I'm working on bug 27451 (https://llvm.org/bugs/show_bug.cgi?id=27451
> ).
>
> If the lifetime-extended temporary object is not trivially destructible,
> there will be a ExprWithCleanups generated for the VarDecl, so calling
> CodeGenFunction::pushCleanupAfterFullExpr to push a llvm.lifetime.end()
> call as a cleanup (as clang currently does in non-temporary cases) just
> works.
>
> However, I learned that ExprWithCleanups will not be generated by Sema
> ::MaybeBindToTemporary, if the destructor is trivial, according to <
> https://github.com/llvm-mirror/clang/blob/4a65931dcba82b23856d654eb77d133d0a3c59f2/lib/Sema/SemaExprCXX.cpp#L5593>;
> and pushCleanupAfterFullExpr seems not working well without a
> ExprWithCleanups, due to the absence of RunCleanupsScope <
> https://github.com/llvm-mirror/clang/blob/717e5e20622b0f4ac3c16bb643ed6a36eef29603/lib/CodeGen/CGExpr.cpp#L1002
> >.
>
> If there is a ExprWithCleanups generated even for a trivially destructible
> temporary, generating llvm.lifetime.end() is easy by calling
> CodeGenFunction::pushCleanupAfterFullExpr.
>
> This requires moving L5593 to L5550 in SemaExprCXX.cpp. The two-lines
> change breaks 36 tests, most of which are checking for warnings, so I
> suppose I also need to fix some warning detections.
>
> Is this - by adding more ExprWithCleanups and fix tests - the correct way to
> fix the temporary lifetime bug? I feel weird about the existence of
> ExprWithCleanups in the first place, but that's off the topic.
>
> Just for illustration, the attached patch fixes the bug, but doesn't fix
> the tests though.
>
> Thanks!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160428/c7386682/attachment.html>


More information about the cfe-dev mailing list