This is an archive of the discontinued LLVM Phabricator instance.

Make BinaryStreamWriter::padToAlignment write blocks vs bytes.
ClosedPublic

Authored by stellaraccident on May 1 2022, 7:27 PM.

Details

Summary

While I think this is a performance improvement over the original, this actually fixes a correctness issue: For an appendable underlying stream, padToAlignment would fail if the additional padding would have caused the stream to grow since it was doing its own check on bounds. By deferring to the regular writeArray method this takes the same path as everything else, which does the correct bounds check in WritableBinaryStreamRef::checkOffsetForWrite (i.e. skips the extension check if BSF_Append is set). I had started to fix the existing bounds check in BinaryStreamWriter but deferred to this because it layered better and is more efficient/consistent.

It didn't look like this method was tested at all, so I added a unit test.

Diff Detail

Event Timeline

stellaraccident created this revision.May 1 2022, 7:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 7:27 PM
stellaraccident requested review of this revision.May 1 2022, 7:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 1 2022, 7:27 PM

Add unit test.

stellaraccident edited the summary of this revision. (Show Details)May 1 2022, 7:58 PM
thakis accepted this revision.May 3 2022, 3:53 PM

Seems ok.

llvm/lib/Support/BinaryStreamWriter.cpp
98

nit: If you do

constexpr uint64_t ZerosSize = 64;
constexpr char Zeros[ZerosSize] = {};

then I think you don't need the memset call (and it's all constant zeroed data).

This revision is now accepted and ready to land.May 3 2022, 3:53 PM
thakis added a subscriber: hans.May 4 2022, 6:20 AM
thakis added inline comments.
llvm/lib/Support/BinaryStreamWriter.cpp
98

@hans reminded me elsewhere that this is only true if you also make the array static, unless you build with -fmerge-all-constants: https://godbolt.org/z/fMcKjvfjo

So static constexpr Zeros[ZerosSize] = {} :)

stellaraccident added inline comments.May 7 2022, 1:28 PM
llvm/lib/Support/BinaryStreamWriter.cpp
98

@hans reminded me elsewhere that this is only true if you also make the array static, unless you build with -fmerge-all-constants: https://godbolt.org/z/fMcKjvfjo

So static constexpr Zeros[ZerosSize] = {} :)

Thank you - TIL today. Made the change as suggested.

Address comments.

This revision was landed with ongoing or failed builds.May 7 2022, 5:39 PM
This revision was automatically updated to reflect the committed changes.