Go to the documentation of this file.
28 template <
typename Base>
class MappedBlockStreamImpl :
public Base {
30 template <
typename...
Args>
31 MappedBlockStreamImpl(
Args &&... Params)
37 using Interval = std::pair<uint64_t, uint64_t>;
40 return std::make_pair(
std::max(I1.first, I2.first),
54 return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
61 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
65 return std::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
69 std::unique_ptr<MappedBlockStream>
79 std::unique_ptr<MappedBlockStream>
93 if (tryReadContiguously(Offset, Size, Buffer))
96 auto CacheIter = CacheMap.find(Offset);
97 if (CacheIter != CacheMap.end()) {
99 for (
auto &Entry : CacheIter->second) {
100 if (Entry.size() >= Size) {
101 Buffer = Entry.slice(0, Size);
110 for (
auto &CacheItem : CacheMap) {
111 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
114 if (CacheItem.first == Offset)
118 if (CacheItem.first >= Offset + Size)
123 if (CacheItem.second.empty())
126 auto CachedAlloc = CacheItem.second.back();
130 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
131 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
137 if (Intersection != RequestExtent)
142 Buffer = CachedAlloc.
slice(CacheRangeOffset, Size);
150 uint8_t *WriteBuffer =
static_cast<uint8_t *
>(Allocator.
Allocate(Size, 8));
154 if (CacheIter != CacheMap.end()) {
155 CacheIter->second.emplace_back(WriteBuffer, Size);
157 std::vector<CacheEntry>
List;
158 List.emplace_back(WriteBuffer, Size);
159 CacheMap.insert(std::make_pair(Offset,
List));
180 uint64_t OffsetInFirstBlock = Offset % BlockSize;
181 uint64_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
183 uint64_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
187 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData))
190 BlockData = BlockData.drop_front(OffsetInFirstBlock);
208 uint64_t BlockNum = Offset / BlockSize;
209 uint64_t OffsetInBlock = Offset % BlockSize;
212 alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
214 uint64_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
216 for (
uint64_t I = 0;
I < RequiredContiguousBlocks; ++
I, ++
E) {
217 if (StreamLayout.
Blocks[
I + BlockNum] !=
E)
229 if (
auto EC = MsfData.
readBytes(MsfOffset, BlockSize, BlockData)) {
233 BlockData = BlockData.
drop_front(OffsetInBlock);
249 uint8_t *WriteBuffer = Buffer.
data();
250 while (BytesLeft > 0) {
255 if (
auto EC = MsfData.
readBytes(Offset, BlockSize, BlockData))
258 const uint8_t *ChunkStart = BlockData.
data() + OffsetInBlock;
260 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
262 BytesWritten += BytesInChunk;
263 BytesLeft -= BytesInChunk;
273 void MappedBlockStream::fixCacheAfterWrite(
uint64_t Offset,
279 for (
const auto &MapEntry : CacheMap) {
282 if (Offset +
Data.size() < MapEntry.first)
284 for (
const auto &Alloc : MapEntry.second) {
287 if (MapEntry.first + Alloc.size() < Offset)
291 Interval WriteInterval = std::make_pair(Offset, Offset +
Data.size());
293 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
296 auto Intersection =
intersect(WriteInterval, CachedInterval);
297 assert(Intersection.first <= Intersection.second);
313 WriteInterface(MsfData) {}
315 std::unique_ptr<WritableMappedBlockStream>
320 return std::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
324 std::unique_ptr<WritableMappedBlockStream>
329 assert(StreamIndex < Layout.
StreamMap.size() &&
"Invalid stream index");
336 std::unique_ptr<WritableMappedBlockStream>
346 std::unique_ptr<WritableMappedBlockStream>
365 std::vector<uint8_t> InitData(Layout.
SB->
BlockSize, 0xFF);
374 return ReadInterface.
readBytes(Offset, Size, Buffer);
397 while (BytesLeft > 0) {
402 const uint8_t *Chunk = Buffer.
data() + BytesWritten;
405 MsfOffset += OffsetInBlock;
406 if (
auto EC = WriteInterface.
writeBytes(MsfOffset, ChunkData))
409 BytesLeft -= BytesToWriteInChunk;
410 BytesWritten += BytesToWriteInChunk;
415 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
WritableMappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
std::vector< support::ulittle32_t > Blocks
This is an optimization pass for GlobalISel generic memory operations.
static const int BlockSize
static std::unique_ptr< MappedBlockStream > createDirectoryStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
ArrayRef< support::ulittle32_t > DirectoryBlocks
uint64_t getLength() override
Return the number of bytes of data in this stream.
const MSFStreamLayout & getStreamLayout() const
Provides write only access to a subclass of WritableBinaryStream.
static ErrorSuccess success()
Create a success value.
support::ulittle32_t NumDirectoryBytes
uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize)
Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize)
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
MSFStreamLayout getFpmStreamLayout(const MSFLayout &Msf, bool IncludeUnusedFpmData=false, bool AltFpm=false)
Determine the layout of the FPM stream, given the MSF layout.
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
Error commit()
For buffered streams, commits changes to the backing store.
void consumeError(Error Err)
Consume a Error without doing anything.
Error commit() override
For buffered streams, commits changes to the backing store.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static std::unique_ptr< WritableMappedBlockStream > createFpmStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator, bool AltFpm=false)
into llvm powi allowing the code generator to produce balanced multiplication trees First
static std::unique_ptr< WritableMappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
ArrayRef< support::ulittle32_t > StreamSizes
uint64_t bytesRemaining() const
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
static std::unique_ptr< WritableMappedBlockStream > createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
Allocate memory in an ever growing pool, as if by bump-pointer.
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Buffer) override
Attempt to write the given bytes into the stream at the desired offset.
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
uint32_t getNumBlocks() const
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Error writeBytes(ArrayRef< uint8_t > Buffer)
Write the bytes specified in Buffer to the underlying stream.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
support::ulittle32_t BlockSize
Lightweight error class with error context and mandatory checking.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
std::vector< ArrayRef< support::ulittle32_t > > StreamMap
MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
static Interval intersect(const Interval &I1, const Interval &I2)
Error writeBytes(uint64_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this StreamRef and a Size, return a reference to a buffer owned by the stream.
static std::unique_ptr< MappedBlockStream > createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData, uint32_t StreamIndex, BumpPtrAllocator &Allocator)
static std::unique_ptr< MappedBlockStream > createStream(uint32_t BlockSize, const MSFStreamLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize)
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) override
Given an offset into the stream, read as much as possible without copying any data.
static std::unique_ptr< WritableMappedBlockStream > createDirectoryStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
size_t size() const
size - Get the array size.
static std::unique_ptr< MappedBlockStream > createFpmStream(const MSFLayout &Layout, BinaryStreamRef MsfData, BumpPtrAllocator &Allocator)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
uint64_t getLength() override
Return the number of bytes of data in this stream.
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
std::enable_if_t< std::is_unsigned< T >::value, T > AbsoluteDifference(T X, T Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
uint32_t getBlockSize() const
Describes the layout of a stream in an MSF layout.