MLIR-AIE 1.4.5.dev
AIEBufferAllocation.h
Go to the documentation of this file.
1//===- AIEBufferAllocation.h ------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2026 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef AIE_DIALECT_AIE_TRANSFORMS_AIEBUFFERALLOCATION_H
9#define AIE_DIALECT_AIE_TRANSFORMS_AIEBUFFERALLOCATION_H
10
12
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/Support/MathExtras.h"
17
18#include <optional>
19#include <utility>
20
21namespace xilinx::AIE {
22
23/// A memtile is reached by DMA rather than by core vector load and store, so
24/// its bus width also covers the vector-alignment requirement.
31 AIETileType tileType) {
32 if (tileType == AIETileType::MemTile) {
33 return {targetModel.getMemTileSize(),
34 targetModel.getMemTileLoadStoreBusWidth(),
35 targetModel.getMemTileLoadStoreBusWidth()};
36 }
37 return {targetModel.getLocalMemorySize(),
40}
41
42/// Return the alignment (in bits) a buffer of `sizeBytes` must satisfy.
43///
44/// Bus width alone is insufficient: from AIE2P on, a full-width vector access
45/// needs 512-bit alignment while the bus is 256 bits wide. An externally
46/// compiled kernel may perform such an access, so a buffer large enough to hold
47/// a full-width vector gets the stricter alignment. A smaller buffer keeps the
48/// bus width and costs no padding.
49inline uint32_t requiredBufferAlignBits(uint32_t busAlignBits,
50 uint32_t maxVecAlignBits,
51 int64_t sizeBytes) {
52 if (maxVecAlignBits <= busAlignBits)
53 return busAlignBits;
54 return sizeBytes * 8 >= static_cast<int64_t>(maxVecAlignBits)
55 ? maxVecAlignBits
56 : busAlignBits;
57}
58
60 int64_t size;
61 int64_t alignment;
62 std::optional<int64_t> address;
63};
64
65/// The basic address allocator and resource planner share this layout: retain
66/// address pins, then place unpinned buffers largest-first, aligning each start
67/// and skipping occupied ranges. Return the high-water mark, without padding
68/// after the final buffer. Entries retain their input order.
69inline int64_t
70assignSequentialBufferAddresses(llvm::MutableArrayRef<BufferAllocation> buffers,
71 int64_t start = 0) {
72 llvm::SmallVector<std::pair<int64_t, int64_t>> pinned;
73 llvm::SmallVector<BufferAllocation *> pending;
74 int64_t highWater = start;
75 for (auto &buffer : buffers) {
76 if (buffer.address) {
77 pinned.emplace_back(*buffer.address, buffer.size);
78 highWater = std::max(highWater, *buffer.address + buffer.size);
79 } else {
80 pending.push_back(&buffer);
81 }
82 }
83 llvm::stable_sort(pinned, [](auto a, auto b) { return a.first < b.first; });
84 llvm::stable_sort(pending,
85 [](auto *a, auto *b) { return a->size > b->size; });
86 auto *current = pinned.begin();
87 int64_t address = start;
88 for (auto *buffer : pending) {
89 address = llvm::alignTo(address, buffer->alignment);
90 while (current != pinned.end() && address + buffer->size > current->first) {
91 address =
92 llvm::alignTo(current->first + current->second, buffer->alignment);
93 ++current;
94 }
95 buffer->address = address;
96 address += buffer->size;
97 }
98 return std::max(highWater, address);
99}
100
101} // namespace xilinx::AIE
102
103#endif // AIE_DIALECT_AIE_TRANSFORMS_AIEBUFFERALLOCATION_H
virtual uint32_t getLocalMemorySize() const =0
Return the size (in bytes) of the local data memory of a core.
virtual uint32_t getComputeTileLoadStoreBusWidth() const =0
Return the data bus width (in bits) for load/store operations of a compute core.
virtual uint32_t getComputeTileMaxVectorAlignBits() const
Return the alignment (in bits) required by the widest vector load/store a compute core can issue.
virtual uint32_t getMemTileLoadStoreBusWidth() const
Return the data bus width (in bits) for load/store operations of a memory tile.
virtual uint32_t getMemTileSize() const =0
Return the size (in bytes) of a MemTile.
uint32_t requiredBufferAlignBits(uint32_t busAlignBits, uint32_t maxVecAlignBits, int64_t sizeBytes)
Return the alignment (in bits) a buffer of sizeBytes must satisfy.
int64_t assignSequentialBufferAddresses(llvm::MutableArrayRef< BufferAllocation > buffers, int64_t start=0)
The basic address allocator and resource planner share this layout: retain address pins,...
TileMemoryLimits tileMemoryLimits(const AIETargetModel &targetModel, AIETileType tileType)
std::optional< int64_t > address
A memtile is reached by DMA rather than by core vector load and store, so its bus width also covers t...