[patch][rfc] Add pwrite to raw_stream

Justin Bogner mail at justinbogner.com
Wed Apr 8 16:59:54 PDT 2015


Rafael EspĂ­ndola <rafael.espindola at gmail.com> writes:
> I noticed that object writer are somewhat convoluted because they have
> to write to streams with to seek or pwrite support. For that to work
> they have to do a pass over the data computing the size and only then
> write it out.
>
> That is a bit silly, as most of the time (and the ones we care about
> performance) we are writing to an actual file with seek support.
>
> When that is not the case, we can accumulate the data and write it on the end.
>
> For accumulating the data, raw_svector_ostream works just fine, except
> that it doesn't support seeking. Adding that is doable, but
> complicates a performance critical structure.
>
> Fortunately, adding pwrite to raw_svector_ostream is trivial.
>
> The attached patch changes ELFObjectWriter.cpp to use pwrite to update
> the one forward reference that is left at that point. Similar cleanups
> should be possible for the other object writers.
>
> The patch also adds a virtual pwrite to raw_stream and a helper class
> for creating a raw_svector_ostream when a given FD does'n support
> seeking (like a pipe).
>
> It passes check-llvm with the exception of
> CodeGen/R600/cf-stack-bug.ll which uses "2>&1" and depends on the
> order the data from stdout and stderr is mixed.
>
> The test could be fixed, but another issue is that clang is structured
> to pass a plain raw_ostream around until the last moment when it is
> wrapped in a formatted_raw_ostream. That makes it harder to detect if
> a stream supports seeking or not.
>
> So, if you guys are OK with this direction, I think the proper way to
> split/commit this is

I like this direction. I've needed this sort of functionality before (in
InstrProfWriter, I think) and worked around it at the expense of
readability.

> * Implement classof for raw_ostream.
> * Stop using formatted_raw_ostream when producing binaries.
> * This patch, but SeekWrapper can then take a plain raw_stream and use
> dyn_cast to see if it a fd stream. It would also not add pwrite to
> formatted_raw_ostream.

SGTM.




More information about the llvm-commits mailing list