24 using namespace llvm::msf;
25 using namespace llvm::support;
37 : Allocator(Allocator), IsGrowable(CanGrow),
40 FreeBlocks(MinBlockCount,
true) {
44 FreeBlocks[BlockMapAddr] =
false;
49 uint32_t MinBlockCount,
bool CanGrow) {
52 "The requested block size is unsupported");
60 if (Addr == BlockMapAddr)
63 if (Addr >= FreeBlocks.
size()) {
66 "Cannot grow the number of blocks");
67 FreeBlocks.
resize(Addr + 1,
true);
71 return make_error<MSFError>(
73 "Requested block map address is already in use");
74 FreeBlocks[BlockMapAddr] =
true;
75 FreeBlocks[Addr] =
false;
85 for (
auto B : DirectoryBlocks)
87 for (
auto B : DirBlocks) {
90 "Attempt to reuse an allocated block");
92 FreeBlocks[
B] =
false;
95 DirectoryBlocks = DirBlocks;
105 if (NumFreeBlocks < NumBlocks) {
108 "There are no free Blocks in the file");
109 uint32_t AllocBlocks = NumBlocks - NumFreeBlocks;
110 FreeBlocks.
resize(AllocBlocks + FreeBlocks.
size(),
true);
116 assert(Block != -1 &&
"We ran out of Blocks!");
119 Blocks[I++] = NextBlock;
120 FreeBlocks.
reset(NextBlock);
122 }
while (--NumBlocks > 0);
142 if (ReqBlocks != Blocks.
size())
143 return make_error<MSFError>(
145 "Incorrect number of blocks for requested stream size");
146 for (
auto Block : Blocks) {
147 if (Block >= FreeBlocks.
size())
148 FreeBlocks.
resize(Block + 1,
true);
150 if (!FreeBlocks.
test(Block))
151 return make_error<MSFError>(
153 "Attempt to re-use an already allocated block");
156 for (
auto Block : Blocks) {
157 FreeBlocks.
reset(Block);
159 StreamData.push_back(std::make_pair(Size, Blocks));
160 return StreamData.size() - 1;
165 std::vector<uint32_t> NewBlocks;
166 NewBlocks.resize(ReqBlocks);
167 if (
auto EC = allocateBlocks(ReqBlocks, NewBlocks))
168 return std::move(EC);
169 StreamData.push_back(std::make_pair(Size, NewBlocks));
170 return StreamData.size() - 1;
181 if (NewBlocks > OldBlocks) {
182 uint32_t AddedBlocks = NewBlocks - OldBlocks;
184 std::vector<uint32_t> AddedBlockList;
185 AddedBlockList.resize(AddedBlocks);
186 if (
auto EC = allocateBlocks(AddedBlocks, AddedBlockList))
188 auto &CurrentBlocks = StreamData[Idx].second;
189 CurrentBlocks.insert(CurrentBlocks.end(), AddedBlockList.begin(),
190 AddedBlockList.end());
191 }
else if (OldBlocks > NewBlocks) {
194 uint32_t RemovedBlocks = OldBlocks - NewBlocks;
196 auto RemovedBlockList = CurrentBlocks.
drop_front(NewBlocks);
197 for (
auto P : RemovedBlockList)
198 FreeBlocks[
P] =
true;
199 StreamData[Idx].second = CurrentBlocks.drop_back(RemovedBlocks);
202 StreamData[Idx].first =
Size;
209 return StreamData[StreamIdx].first;
213 return StreamData[StreamIdx].second;
216 uint32_t MSFBuilder::computeDirectoryByteSize()
const {
223 for (
const auto &
D : StreamData) {
225 assert(ExpectedNumBlocks ==
D.second.size() &&
226 "Unexpected number of blocks");
245 if (NumDirectoryBlocks > DirectoryBlocks.size()) {
248 std::vector<uint32_t> ExtraBlocks;
249 uint32_t NumExtraBlocks = NumDirectoryBlocks - DirectoryBlocks.size();
250 ExtraBlocks.resize(NumExtraBlocks);
251 if (
auto EC = allocateBlocks(NumExtraBlocks, ExtraBlocks))
252 return std::move(EC);
253 DirectoryBlocks.insert(DirectoryBlocks.end(), ExtraBlocks.begin(),
255 }
else if (NumDirectoryBlocks < DirectoryBlocks.size()) {
256 uint32_t NumUnnecessaryBlocks = DirectoryBlocks.size() - NumDirectoryBlocks;
259 FreeBlocks[
B] =
true;
260 DirectoryBlocks.resize(NumDirectoryBlocks);
269 std::uninitialized_copy_n(DirectoryBlocks.begin(), NumDirectoryBlocks,
275 if (!StreamData.empty()) {
278 L.StreamMap.resize(StreamData.size());
279 for (
uint32_t I = 0; I < StreamData.size(); ++
I) {
280 Sizes[
I] = StreamData[
I].first;
283 std::uninitialized_copy_n(StreamData[I].
second.begin(),
284 StreamData[
I].second.size(), BlockList);
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
char MagicBytes[sizeof(Magic)]
void setFreePageMap(uint32_t Fpm)
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
size_type size() const
size - Returns the number of bits in this bitvector.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
uint32_t getStreamSize(uint32_t StreamIdx) const
Get the size of a stream by index.
static const uint32_t kFreePageMap0Block
uint32_t getNumFreeBlocks() const
Get the total number of blocks that exist in the MSF file but are not allocated to any valid data...
Error setBlockMapAddr(uint32_t Addr)
Request the block map to be at a specific block address.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
uint32_t getMinimumBlockCount()
static const uint32_t kNumReservedPages
static const uint32_t kDefaultFreePageMap
Tagged union holding either a T or a Error.
static const uint32_t kSuperBlockBlock
uint32_t getTotalBlockCount() const
Get the total number of blocks in the MSF file.
uint32_t getNumStreams() const
Get the total number of streams in the MSF layout.
Error setDirectoryBlocksHint(ArrayRef< uint32_t > DirBlocks)
support::ulittle32_t BlockSize
size_t size() const
size - Get the array size.
support::ulittle32_t BlockMapAddr
INITIALIZE_PASS(HexagonEarlyIfConversion,"hexagon-eif","Hexagon early if conversion", false, false) bool HexagonEarlyIfConversion MachineBasicBlock * SB
size_type count() const
count - Returns the number of bits which are set.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Allocate memory in an ever growing pool, as if by bump-pointer.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
static const char Magic[]
Expected< MSFLayout > build()
Finalize the layout and build the headers and structures that describe the MSF layout and can be writ...
support::ulittle32_t Unknown1
Error setStreamSize(uint32_t Idx, uint32_t Size)
Update the size of an existing stream.
uint32_t getNumUsedBlocks() const
Get the total number of blocks that will be allocated to actual data in this MSF file.
static const int BlockSize
static const uint32_t kDefaultBlockMapAddr
static ErrorSuccess success()
Create a success value.
ArrayRef< uint32_t > getStreamBlocks(uint32_t StreamIdx) const
Get the list of blocks allocated to a particular stream.
bool test(unsigned Idx) const
Expected< uint32_t > addStream(uint32_t Size, ArrayRef< uint32_t > Blocks)
Add a stream to the MSF file with the given size, occupying the given list of blocks.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize)
static const uint32_t kFreePageMap1Block
void setUnknown1(uint32_t Unk1)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
support::ulittle32_t FreeBlockMapBlock
Lightweight error class with error context and mandatory checking.
bool isValidBlockSize(uint32_t Size)
bool isBlockFree(uint32_t Idx) const
Check whether a particular block is allocated or free.
support::ulittle32_t NumDirectoryBytes
support::ulittle32_t NumBlocks