MLIR-AIE 1.4.6.dev
AIEDMAChannelAnalysis.h
Go to the documentation of this file.
1//===- AIEDMAChannelAnalysis.h ----------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2021-2022 Xilinx, Inc.
4// Copyright (C) 2022-2026 Advanced Micro Devices, Inc.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef AIE_DIALECT_AIE_TRANSFORMS_AIEDMACHANNELANALYSIS_H
10#define AIE_DIALECT_AIE_TRANSFORMS_AIEDMACHANNELANALYSIS_H
11
13
14#include "llvm/ADT/BitVector.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/MapVector.h"
17#include "llvm/ADT/STLFunctionalExtras.h"
18#include "llvm/ADT/SmallVector.h"
19
20#include <limits>
21#include <map>
22#include <optional>
23#include <utility>
24
25namespace xilinx::AIE {
26
27/// Which DMA channels of each tile are already spoken for, so that a channel is
28/// handed out at most once across everything that programs one.
30 /// Fully resolved tile aliases name the same hardware resources. Unresolved
31 /// tiles retain their SSA identity until placement establishes co-location.
32 mlir::DenseMap<std::pair<int, int>, mlir::Value> tilesByCoordinate;
33 mlir::Value getTileKey(mlir::Value tile);
34
35 /// Keep the reserving operation so diagnostics retain its MLIR location.
36 mlir::DenseMap<std::tuple<mlir::Value, DMAChannelDir, int>, mlir::Operation *>
37 usedChannels;
38 /// DMA channels an explicit flow routes. First-free assignment skips them;
39 /// a pinned request may still claim one.
40 mlir::DenseMap<std::tuple<mlir::Value, DMAChannelDir, int>, mlir::Operation *>
41 streamedChannels;
42 /// Channels whose claimants all share them. Several packet-switched sources
43 /// coexist on one; a circuit claim is exclusive.
44 mlir::DenseSet<std::tuple<mlir::Value, DMAChannelDir, int>> sharedChannels;
45 mlir::DenseMap<std::tuple<mlir::Value, DMAChannelDir, int>, mlir::Operation *>
46 shareableChannels;
47 /// The first claimant of a core stream port, and whether only packets use
48 /// it; circuit occupancy is exclusive.
49 struct StreamClaim {
50 mlir::Operation *owner;
51 bool packet;
52 };
53 mlir::DenseMap<std::tuple<mlir::Value, DMAChannelDir, int>, StreamClaim>
54 usedStreams;
55
56public:
57 /// A channel claimed by index: by a DMA program start, a runtime task or a
58 /// shim allocation (`owner`), on `tile`.
59 struct ChannelClaim {
60 ChannelClaim(mlir::Value tile, DMAChannelDir dir, int channel,
61 mlir::Operation *owner)
63 mlir::Value tile;
64 DMAChannelDir dir;
66 mlir::Operation *owner;
67 };
68
69private:
70 /// Every claim that took a channel, in the order they were made.
71 llvm::SmallVector<ChannelClaim> claims;
72 void claim(mlir::Value tile, DMAChannelDir dir, int channel,
73 mlir::Operation *owner);
74
75public:
76 DMAChannelAnalysis(DeviceOp &device);
77
78 /// The channels taken by index rather than through a route endpoint, in
79 /// the order the claims were made. A shareable one may appear more than
80 /// once.
81 llvm::ArrayRef<ChannelClaim> getChannelClaims() const { return claims; }
82
83 /// Exclusive upper bound on channel indices eligible for this transfer.
84 static int getDMAChannelLimit(TileLike tile, DMAChannelDir dir,
85 bool requiresAdjacentTileAccessChannels);
86
87 /// Next free channel of `tile` in `dir`, or -1 when the tile has none left.
88 /// A channel an explicit flow routes is not free.
89 /// A channel reaching an adjacent MemTile's memory must come from the
90 /// target's restricted range at every compatible physical position.
91 int getDMAChannelIndex(TileLike tile, DMAChannelDir dir,
92 bool requiresAdjacentTileAccessChannels,
93 mlir::Operation *owner = nullptr);
94
95 /// Whether first-free assignment could hand out `channel`: nothing reserves
96 /// it and no explicit flow routes it.
97 bool isChannelFree(TileLike tile, DMAChannelDir dir, int channel);
98
99 /// Claim `channel` for (`tile`, `dir`) so first-free assignment cannot take
100 /// it. Returns the channel, or -1 when it is out of range or already
101 /// claimed; the caller reports, since it knows which endpoint asked.
102 /// `shareable` claimants join a channel other shareable ones hold.
103 int reservePinnedChannel(TileLike tile, DMAChannelDir dir, int channel,
104 mlir::Operation *owner = nullptr,
105 bool shareable = false);
106
107 /// Operation reserving or routing this channel; null if none.
108 mlir::Operation *getDMAChannelOwner(TileLike tile, DMAChannelDir dir,
109 int channel);
110
111 /// A DMA end of one tile: its direction, the BD ids it takes, the
112 /// exclusive upper bound on the channels it may take, and the channel it
113 /// pins, if any, which every end pinning it shares.
114 struct BDPlanEnd {
115 BDPlanEnd(DMAChannelDir dir, int64_t bds, int limit,
116 std::optional<int> pinned)
117 : dir(dir), bds(bds), limit(limit), pinned(pinned) {}
118 DMAChannelDir dir;
119 int64_t bds;
120 int limit;
121 std::optional<int> pinned;
122 };
123
124 using BDUser = std::pair<DMAChannelDir, int>;
125 /// The channels among `0..numChannels-1` at (`col`, `row`), per direction,
126 /// grouped by BD pool; `size` is the pool's number of BDs.
127 struct BDGroup {
128 int64_t size = 0;
129 llvm::SmallVector<BDUser> users;
130 };
131 static llvm::SmallVector<BDGroup> bdGroups(const AIETargetModel &target,
132 int col, int row, int numChannels);
133
134 /// A channel of the tile at (`col`, `row`) for each of `ends`: its pinned
135 /// one, else one `free` leaves that no other end of its direction takes;
136 /// and, for every BD pool, the ends on its channels plus the ids `spent` on
137 /// each of them fit the pool. Nothing when no assignment fits.
138 static std::optional<llvm::SmallVector<int>>
139 planBDChannels(const AIETargetModel &target, int col, int row,
140 llvm::ArrayRef<BDPlanEnd> ends,
141 llvm::function_ref<bool(DMAChannelDir, int)> free,
142 llvm::function_ref<int64_t(DMAChannelDir, int)> spent);
143 /// The same, given the tile's bdGroups.
144 static std::optional<llvm::SmallVector<int>>
145 planBDChannels(llvm::ArrayRef<BDGroup> tileGroups,
146 llvm::ArrayRef<BDPlanEnd> ends,
147 llvm::function_ref<bool(DMAChannelDir, int)> free,
148 llvm::function_ref<int64_t(DMAChannelDir, int)> spent);
149
150 /// A pool whose ends a BD plan weighs: how many objects it holds, the bytes
151 /// of one, and whether it can be walked whole, its ends then taking one BD
152 /// id each.
153 struct BDWalkPool {
154 int64_t depth;
155 int64_t objectBytes;
157 };
158 /// walkSetsThatFit's result; `channelShort` when the ends do not fit the
159 /// tile's channels even taking no BD ids, `fits` false when no set does.
160 struct BDWalkSets {
161 llvm::SmallVector<llvm::SmallVector<unsigned>> sets;
162 /// The bytes each set walks whole.
163 llvm::SmallVector<int64_t> bytes;
164 bool channelShort = false;
165 bool fits = true;
166 };
167
168 /// The walkable pool not yet `whole` to walk next: the deepest, then the one
169 /// with the smallest objects, then the one whose ends take the most BD ids;
170 /// `pools` order decides only between pools alike in all three. Nothing
171 /// when none is left.
172 static std::optional<unsigned>
173 nextPoolToWalk(llvm::ArrayRef<BDPlanEnd> ends,
174 llvm::ArrayRef<unsigned> poolOf,
175 llvm::ArrayRef<BDWalkPool> pools, llvm::ArrayRef<bool> whole);
176
177 /// Every set of walkable `pools` whose walk lets planBDChannels fit `ends`
178 /// (`poolOf` gives each end's pool), ordered by the bytes the set walks
179 /// whole, then its size, then its members in nextPoolToWalk's order; at
180 /// most `maxSets` of them, cheapest first (unbounded by default). Empty
181 /// when the ends fit unwalked. At most 12 pools are walkable: each draws a
182 /// channel of its own, and more ends than channels make `channelShort`.
183 /// aie-objectfifo-allocate and the SA placer both walk by this; the SA only
184 /// ever takes the first set, so it should pass `maxSets=1`.
185 static BDWalkSets
186 walkSetsThatFit(const AIETargetModel &target, int col, int row,
187 llvm::ArrayRef<BDPlanEnd> ends,
188 llvm::ArrayRef<unsigned> poolOf,
189 llvm::ArrayRef<BDWalkPool> pools,
190 llvm::function_ref<bool(DMAChannelDir, int)> free,
191 llvm::function_ref<int64_t(DMAChannelDir, int)> spent,
192 unsigned maxSets = std::numeric_limits<unsigned>::max());
193
194 /// One walkSetsThatFit set per mem tile of a spill component
195 /// (memTileSpillComponents), by index; `placed` when its buffers place,
196 /// `stopped` when the search gave up at `maxTries` tries.
197 struct WalkChoice {
198 llvm::SmallVector<unsigned> sets;
199 bool placed = false;
200 bool stopped = false;
201 };
202 /// Tries the choices of one set per tile, `bytes[tile]` giving each of its
203 /// sets' bytes walked whole, in order of total bytes walked, then the
204 /// tiles' set indices in turn, until `places` (the caller's buffer search
205 /// over the component) accepts one; the first choice when none does.
206 static WalkChoice
207 searchWalkProduct(llvm::ArrayRef<llvm::SmallVector<int64_t>> bytes,
208 llvm::function_ref<bool(llvm::ArrayRef<unsigned>)> places,
209 unsigned maxTries);
210 static constexpr unsigned kMaxWalkPlacements = 1024;
211
212 /// Blocks of the BD chain `start` begins, following next_bd.
213 static llvm::SmallVector<mlir::Block *> chainOf(DMAStartOp start);
214 static int64_t countBDs(DMAStartOp start);
215
216 /// The BD ids tiles' own programs hold before objectFIFOs take theirs: the
217 /// chains DMA programs start by index, on their channels, and each id a
218 /// runtime sequence pins (AIEX::forEachRuntimePinnedBdId), once per tile and
219 /// BD pool, on the lowest channel up to its own whose pool holds it
220 /// (aie-reserve-runtime-bd-ids keeps static BDs off those).
221 class TakenBDs {
222 public:
223 TakenBDs() = default;
224 explicit TakenBDs(DeviceOp device);
225 /// By channel and direction, the ids held at (`col`, `row`) by `tiles`
226 /// and by every tile with those coordinates.
227 std::map<BDUser, int64_t>
228 at(const AIETargetModel &target, int col, int row,
229 llvm::ArrayRef<mlir::Operation *> tiles = {}) const;
230 /// Coordinates of the placed tiles that hold BD ids.
231 llvm::SmallVector<std::pair<int, int>> positions() const;
232
233 private:
234 struct Pin {
235 unsigned order;
236 int32_t id;
237 std::optional<int> channel;
238 std::optional<DMAChannelDir> dir;
239 };
240 llvm::MapVector<mlir::Operation *, std::map<BDUser, int64_t>> fixed;
241 llvm::MapVector<mlir::Operation *, llvm::SmallVector<Pin>> pinned;
242 };
243
244 /// Claim a raw stream port for `user`, reporting on it when the port is
245 /// taken; packet users may share it with other packets.
246 mlir::LogicalResult checkAIEStreamIndex(TileLike tile, DMAChannel chan,
247 mlir::Operation *user,
248 bool packet = false);
249};
250
251} // namespace xilinx::AIE
252
253#endif // AIE_DIALECT_AIE_TRANSFORMS_AIEDMACHANNELANALYSIS_H
llvm::SmallVector< std::pair< int, int > > positions() const
Coordinates of the placed tiles that hold BD ids.
std::map< BDUser, int64_t > at(const AIETargetModel &target, int col, int row, llvm::ArrayRef< mlir::Operation * > tiles={}) const
By channel and direction, the ids held at (col, row) by tiles and by every tile with those coordinate...
int getDMAChannelIndex(TileLike tile, DMAChannelDir dir, bool requiresAdjacentTileAccessChannels, mlir::Operation *owner=nullptr)
Next free channel of tile in dir, or -1 when the tile has none left.
static std::optional< unsigned > nextPoolToWalk(llvm::ArrayRef< BDPlanEnd > ends, llvm::ArrayRef< unsigned > poolOf, llvm::ArrayRef< BDWalkPool > pools, llvm::ArrayRef< bool > whole)
The walkable pool not yet whole to walk next: the deepest, then the one with the smallest objects,...
static std::optional< llvm::SmallVector< int > > planBDChannels(const AIETargetModel &target, int col, int row, llvm::ArrayRef< BDPlanEnd > ends, llvm::function_ref< bool(DMAChannelDir, int)> free, llvm::function_ref< int64_t(DMAChannelDir, int)> spent)
A channel of the tile at (col, row) for each of ends: its pinned one, else one free leaves that no ot...
mlir::Operation * getDMAChannelOwner(TileLike tile, DMAChannelDir dir, int channel)
Operation reserving or routing this channel; null if none.
bool isChannelFree(TileLike tile, DMAChannelDir dir, int channel)
Whether first-free assignment could hand out channel: nothing reserves it and no explicit flow routes...
static constexpr unsigned kMaxWalkPlacements
static int64_t countBDs(DMAStartOp start)
static llvm::SmallVector< BDGroup > bdGroups(const AIETargetModel &target, int col, int row, int numChannels)
static WalkChoice searchWalkProduct(llvm::ArrayRef< llvm::SmallVector< int64_t > > bytes, llvm::function_ref< bool(llvm::ArrayRef< unsigned >)> places, unsigned maxTries)
Tries the choices of one set per tile, bytes[tile] giving each of its sets' bytes walked whole,...
static llvm::SmallVector< mlir::Block * > chainOf(DMAStartOp start)
Blocks of the BD chain start begins, following next_bd.
static BDWalkSets walkSetsThatFit(const AIETargetModel &target, int col, int row, llvm::ArrayRef< BDPlanEnd > ends, llvm::ArrayRef< unsigned > poolOf, llvm::ArrayRef< BDWalkPool > pools, llvm::function_ref< bool(DMAChannelDir, int)> free, llvm::function_ref< int64_t(DMAChannelDir, int)> spent, unsigned maxSets=std::numeric_limits< unsigned >::max())
Every set of walkable pools whose walk lets planBDChannels fit ends (poolOf gives each end's pool),...
static int getDMAChannelLimit(TileLike tile, DMAChannelDir dir, bool requiresAdjacentTileAccessChannels)
Exclusive upper bound on channel indices eligible for this transfer.
std::pair< DMAChannelDir, int > BDUser
mlir::LogicalResult checkAIEStreamIndex(TileLike tile, DMAChannel chan, mlir::Operation *user, bool packet=false)
Claim a raw stream port for user, reporting on it when the port is taken; packet users may share it w...
int reservePinnedChannel(TileLike tile, DMAChannelDir dir, int channel, mlir::Operation *owner=nullptr, bool shareable=false)
Claim channel for (tile, dir) so first-free assignment cannot take it.
llvm::ArrayRef< ChannelClaim > getChannelClaims() const
The channels taken by index rather than through a route endpoint, in the order the claims were made.
static std::optional< llvm::SmallVector< int > > planBDChannels(llvm::ArrayRef< BDGroup > tileGroups, llvm::ArrayRef< BDPlanEnd > ends, llvm::function_ref< bool(DMAChannelDir, int)> free, llvm::function_ref< int64_t(DMAChannelDir, int)> spent)
The same, given the tile's bdGroups.
struct DMAChannel { DMAChannelDir direction; int channel; bool operator==(const DMAChannel &rhs) const { return std::tie(direction, channel)==std::tie(rhs.direction, rhs.channel); } } DMAChannel
Definition AIEDialect.h:320
The channels among 0..numChannels-1 at (col, row), per direction, grouped by BD pool; size is the poo...
BDPlanEnd(DMAChannelDir dir, int64_t bds, int limit, std::optional< int > pinned)
A pool whose ends a BD plan weighs: how many objects it holds, the bytes of one, and whether it can b...
walkSetsThatFit's result; channelShort when the ends do not fit the tile's channels even taking no BD...
llvm::SmallVector< int64_t > bytes
The bytes each set walks whole.
llvm::SmallVector< llvm::SmallVector< unsigned > > sets
ChannelClaim(mlir::Value tile, DMAChannelDir dir, int channel, mlir::Operation *owner)
One walkSetsThatFit set per mem tile of a spill component (memTileSpillComponents),...