Enable the use of pwrite in the object writers.

Rafael EspĂ­ndola rafael.espindola at gmail.com
Fri Apr 10 14:13:52 PDT 2015


The attached patch makes it possible to use pwrite in the object writers.

This is extremely convenient, as most of the file can be streamed and
then just a few entries patched once the size is known.

In the case of ELF, only one number has to be patched: the location of
the section table.

The feature is also useful in other places. Justin says it would be
useful for profile writing and while working on this I noticed a FIXME
in clang about ptth.

raw_fd_osteam already support seeking, but we cannot unfortunately
just use that as it would fail on pipes. That was the FIXME in clang.

What is needed is to detect at runtime if a fd supports seeking and if
not keep a buffer until the end.

This patch introduces a new abstract type pwrite_stream which is used
in the object writers.

It is still a stream, since many places expect to pass around a stream
that eventually makes to  LLVMTargetMachine::addPassesToEmitFile
(which may also pass the stream for printing assembly).

In any case, having a dedicated type makes it easier for someone to
remove the " : public raw_ostream" if they really want to.

There a lot of possible variations:

* Using isa/dyn_cast/cast. That is what I first implemented. There
were objections to it, and I do prefer the strongly typed version a
bit more.

* Implementing seek instead of pwrite in raw_svector_stream. Doable,
but not as simple. Since the class is very performance sensitive I
went with pwrite which is an excellent fit.

* Have buffer_ostream delete the inner stream. Nice for clang, doesn't
work with tool_output_file.

* Have buffer_ostream own a buffer that is passed in. It works, but
looks odd from the user perspective.

* Have buffer_ostream contain a buffer. That is probably the best for
the users, but requires changing raw_svector_ostream to have a
protected init method since it would be constructed before the buffer.
I might still give this one a try.

Cheers,
Rafael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm.patch
Type: text/x-patch
Size: 83934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150410/00e255c9/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang.patch
Type: text/x-patch
Size: 18706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150410/00e255c9/attachment-0001.bin>


More information about the llvm-commits mailing list