[llvm-dev] [RFC] lifetime.end metadata

Akira Hatanaka via llvm-dev
Tue, 01 Mar 2016 21:39:12 -0000

I'd like to get a quick feedback on the lifetime.end metadata kind I'm
considering using. I'm planning to use it in cases where lifetime.end
intrinsics do not give lifetime bounds that are tight enough.

As an example of when lifetime.end cannot provide a tight lifetime bound,
consider the following program:

void test111(int n) {
  if (n < 10) {
    string    str = "One";
    cout << str;
  } else if (n < 100) {
    string    str = "Two";
    cout << str;
  } else if (n < 1000) {
    string    str = "Three";
    cout << str;
  }
}

In the program, the three local variables "str" have lifetimes that do
not
overlap with each other, so it should be possible to use the same stack
slot for all of them to save stack space. However, clang/llvm currently
allocates separate slots for each of the three variables because clang
doesn't insert lifetime.end markers after the objects are destructed (for
example, the destructor of the first string is called in block "lpad"
in
line 71 of the attached bitcode f1.ll, but there is no lifetime.end marker
inserted).

If we want to insert the missing lifetime.end markers for the strings,
we'll have to insert them at the beginning of lpad's successors,
%eh.resume
and %terminate.lpad, because the destructor call in lpad is an invoke
instruction. However, if we insert the lifetime.end markers for all of
them, the lifetimes of the strings will overlap clang generates only one
resume block and one terminate block per function.

To get tighter bounds for the strings' lifetimes, I'm considering
attaching
lifetime.end metadata to the destructor calls in clang:

invoke void @foo("basic_string"* %str) to label %eh.resume unwind
label
%terminate.lpad, !lifetime_end !10

!10 = !{i32 0}

SelectionDAGBuilder will then insert a lifetime.end node if there is a
lifetime.end attached to an invoke instruction.

Does this sound like a reasonable approach? Or are there better ways to
tell the backend that the lifetimes do not overlap?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20160301/27ef494d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test3.cpp
Type: text/x-c++src
Size: 267 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20160301/27ef494d/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: f1.ll
Type: application/octet-stream
Size: 32762 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20160301/27ef494d/attachment.obj>