MLIR-AIE 1.4.6.dev
AIEShimSharing.h
Go to the documentation of this file.
1//===- AIEShimSharing.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_AIESHIMSHARING_H
9#define AIE_DIALECT_AIE_TRANSFORMS_AIESHIMSHARING_H
10
12
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/DenseSet.h"
15#include "llvm/ADT/STLFunctionalExtras.h"
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/SmallVector.h"
18
19#include <optional>
20#include <tuple>
21
22namespace xilinx::AIE {
23
24/// The shim MM2S channels `device`'s overlay weighs, for
25/// aie-generate-column-control-overlay. A route asking for no packet header
26/// counts as packet switched with `packetSwitched`. Defined in
27/// AIEGenerateColumnControlOverlay.cpp.
29 /// Channels a circuit-switched route end, flow, allocation or shim mux
30 /// holds, per shim tile op.
31 llvm::DenseMap<mlir::Operation *, llvm::SmallSet<int, 2>> circuits;
32 /// Packet-switched route ends, by name, per (tile, channel).
33 llvm::DenseMap<std::pair<mlir::Operation *, int>,
34 llvm::SmallVector<mlir::StringAttr>>
36 /// Packet flows per (tile, channel).
37 llvm::DenseMap<std::pair<mlir::Operation *, int>, unsigned> packetFlows;
38};
39ShimMM2SUses shimMM2SUses(DeviceOp device, bool packetSwitched);
40
41/// Shim endpoint lifetimes, including calls to self-contained runtime
42/// sequences. MM2S transfers conservatively remain live until their sequence
43/// returns; a callee's return and a dispatch boundary are themselves taken as
44/// the end of its sends, by the aiex.run and hardware-context contracts, not
45/// by anything this analysis computes. A receiving channel writes whatever
46/// arrives into the task it runs, so a receiving end shares only by explicit
47/// permission or where the array cannot send its words outside its turn: its
48/// senders wait on a gate the host fills only after the end before it is
49/// awaited, and each sequence drains all that gate lets them send.
51public:
52 /// A shim end a runtime sequence transfers through.
53 struct End {
54 mlir::StringAttr fifo;
55 /// An MM2S end, sending into the array.
56 bool sends;
57 };
58
59 /// A shim input gating a receiving end's senders, per its
60 /// `#aie.consumed_by`.
61 struct Gate {
62 Gate(mlir::StringAttr input, ConsumedByAttr proof)
63 : input(input), proof(proof) {}
64 mlir::StringAttr input;
65 ConsumedByAttr proof;
66 };
67
68 using Gates = llvm::DenseMap<mlir::StringAttr, llvm::SmallVector<Gate>>;
69
70 /// How ends and gates name a shim route end: by its objectFIFO, else by its
71 /// own symbol.
72 static mlir::StringAttr keyOf(RouteEndpointOp endpoint);
73 /// The end a runtime sequence of `device` transfers through when it names
74 /// `symbol`: a shim DMA route end, keyed by keyOf, or, before
75 /// aie-objectfifo-split, an objectFIFO, sending when a shim tile produces
76 /// it.
77 static std::optional<End> endOf(DeviceOp device, mlir::StringAttr symbol);
78
79 /// `endOf` maps a symbol a runtime sequence transfers through to the
80 /// objectFIFO end it belongs to, or nothing when it belongs to none.
81 /// `gates` holds each receiving end's gates, keyed as `endOf` keys ends;
82 /// without them, a receiving end shares only by explicit permission, which
83 /// is all a caller running before the proofs can know. With
84 /// `plannedOverlay`, the control overlay's channel on each column's shim
85 /// tile is an end too, overlayEnd(), before
86 /// aie-generate-column-control-overlay makes it: it carries the control
87 /// packets to its column and every configuration load, as the overlay's
88 /// own channel does once made.
90 DeviceOp device,
91 llvm::function_ref<std::optional<End>(mlir::StringAttr)> endOf,
92 std::optional<Gates> gates = std::nullopt, bool plannedOverlay = false);
93
95 mlir::ModuleOp module,
96 llvm::function_ref<std::optional<End>(DeviceOp, mlir::StringAttr)> endOf,
97 std::optional<Gates> gates = std::nullopt);
98
99 /// Each receiving end's gates, keyed by `keyOf`: the shim inputs whose
100 /// consumption its progress proves, from the `#aie.consumed_by` proofs
101 /// aie-objectfifo-verify leaves on split route endpoints. Endpoints `keyOf`
102 /// maps to null are left out.
103 static Gates
104 provenGates(DeviceOp device,
105 llvm::function_ref<mlir::StringAttr(RouteEndpointOp)> keyOf);
106
107 /// Whether both ends permit sharing: explicitly, by MM2S lifetime analysis,
108 /// or, for a receiving end, by its gates.
109 bool apart(mlir::StringAttr a, mlir::StringAttr b) const;
110
111 bool isUsed(mlir::StringAttr end) const;
112
113 /// The planned control overlay channel's end on column `column`'s shim tile.
114 mlir::StringAttr overlayEnd(int column) const;
115
116 /// The number of runtime sequences `bytes` indexes into.
117 unsigned sequences() const { return spans.size(); }
118 /// The bytes runtime sequence `seq` moves through `end`, nullopt when not
119 /// static or when `end` is not one of its ends.
120 std::optional<int64_t> bytes(unsigned seq, mlir::StringAttr end) const {
121 auto it = spans[seq].find(end);
122 return it == spans[seq].end() ? std::nullopt : it->second.bytes;
123 }
124 /// Whether runtime sequence `seq` transfers through `end` at all, static or
125 /// not; `bytes` alone cannot tell a dynamic end from an absent one.
126 bool names(unsigned seq, mlir::StringAttr end) const {
127 return spans[seq].contains(end);
128 }
129
130 /// Splits `ends` into groups whose members are pairwise apart, each group
131 /// taking one channel: first fit, ordered by the ends' spans in flight.
132 /// Returns indices into `ends`.
133 llvm::SmallVector<llvm::SmallVector<unsigned>>
134 groups(llvm::ArrayRef<mlir::StringAttr> ends) const;
135
136 /// A shim allocation's channel the ends its `aie.shareable_with` lists may
137 /// take turns on.
139 SharedChannel(llvm::SmallVector<mlir::StringAttr> listed, bool pinned)
140 : listed(std::move(listed)), pinned(pinned) {}
141 llvm::SmallVector<mlir::StringAttr> listed;
142 /// Whether a route end pins the channel, which then takes no other end.
143 bool pinned;
144 };
145
146 /// How one shim tile's unpinned ends in one direction share channels, as
147 /// objectFIFO allocation shares them.
148 struct Plan {
149 /// Per end, the index of the shared channel it takes, if any.
150 llvm::SmallVector<std::optional<unsigned>> sharedChannel;
151 /// The other ends, in groups taking one channel each.
152 llvm::SmallVector<llvm::SmallVector<unsigned>> groups;
153 };
154
155 /// Plans `ends` (keyed as apart() takes keys); `names[i]` is how `shared`'s
156 /// lists name `ends[i]`. Each channel in `shared`, in order, takes the ends
157 /// its list names that are apart from those it already took; the rest
158 /// group as groups() groups them. Indices are into `ends`.
159 Plan plan(llvm::ArrayRef<mlir::StringAttr> ends,
160 llvm::ArrayRef<mlir::StringAttr> names,
161 llvm::ArrayRef<SharedChannel> shared) const;
162
163private:
164 /// An end's transfers in one runtime sequence, as the gates are checked.
165 struct Span {
166 int64_t first = std::numeric_limits<int64_t>::max();
167 int64_t last = -1;
168 int64_t lastIssue = -1;
169 int64_t lastDone = -1;
170 /// As lastDone, by an await alone: a free does not wait for the task.
171 int64_t lastAwaited = -1;
172 bool sends = false;
173 /// Bytes the sequence moves through the end; nullopt when not static.
174 std::optional<int64_t> bytes = 0;
175 };
176
177 void analyze(
178 mlir::Operation *root,
179 llvm::function_ref<std::optional<End>(DeviceOp, mlir::StringAttr)> endOf);
180 void analyzeGates(
181 mlir::Operation *root,
182 llvm::function_ref<std::optional<End>(DeviceOp, mlir::StringAttr)> endOf);
183
184 /// Whether the receiving ends `a` and `b` take turns by their gates.
185 bool gated(mlir::StringAttr a, mlir::StringAttr b) const;
186 /// The gates `end` may share by: all of a receiving end's that `contained`
187 /// holds for, or one null gate for an end that needs none.
188 llvm::SmallVector<const Gate *> usableGates(mlir::StringAttr end) const;
189 /// Whether, in one sequence, `x` is done before `y` starts and, when `gate`
190 /// is set, awaited before `gate` is first filled.
191 bool before(const llvm::DenseMap<mlir::StringAttr, Span> &byFifo,
192 const Span &x, const Span &y, const Gate *gate) const;
193 /// Whether `gate` is filled only in sequences that drain the receiving
194 /// `end`, each awaiting every byte the gate lets its senders send.
195 bool contained(mlir::StringAttr end, const Gate &gate) const;
196
197 llvm::DenseMap<mlir::StringAttr,
198 llvm::SmallVector<std::tuple<unsigned, int64_t, int64_t>>>
199 lifetimes;
200 llvm::DenseSet<mlir::StringAttr> used;
201 llvm::DenseMap<mlir::StringAttr, llvm::SmallVector<mlir::StringAttr>>
202 permissions;
203 /// Receiving ends no explicit list covers.
204 llvm::DenseSet<mlir::StringAttr> receivers;
205 llvm::DenseSet<std::pair<mlir::StringAttr, mlir::StringAttr>> overlaps;
206 bool unknown = false;
207 llvm::SmallVector<llvm::DenseMap<mlir::StringAttr, Span>> spans;
208 std::optional<Gates> gates;
209 mlir::MLIRContext *context = nullptr;
210 /// usableGates' answer, by end: depends only on the immutable analysis.
211 mutable llvm::DenseMap<mlir::StringAttr, llvm::SmallVector<const Gate *>>
212 usableGatesCache;
213 /// Whether some sequence names an end, by end.
214 mutable llvm::DenseMap<mlir::StringAttr, bool> namedCache;
215 /// The columns with a planned overlay end, none without `plannedOverlay`.
216 int overlayColumns = 0;
217};
218
219} // namespace xilinx::AIE
220
221#endif // AIE_DIALECT_AIE_TRANSFORMS_AIESHIMSHARING_H
std::optional< int64_t > bytes(unsigned seq, mlir::StringAttr end) const
The bytes runtime sequence seq moves through end, nullopt when not static or when end is not one of i...
ShimTransferSpans(mlir::ModuleOp module, llvm::function_ref< std::optional< End >(DeviceOp, mlir::StringAttr)> endOf, std::optional< Gates > gates=std::nullopt)
llvm::DenseMap< mlir::StringAttr, llvm::SmallVector< Gate > > Gates
Plan plan(llvm::ArrayRef< mlir::StringAttr > ends, llvm::ArrayRef< mlir::StringAttr > names, llvm::ArrayRef< SharedChannel > shared) const
Plans ends (keyed as apart() takes keys); names[i] is how shared's lists name ends[i].
bool isUsed(mlir::StringAttr end) const
bool names(unsigned seq, mlir::StringAttr end) const
Whether runtime sequence seq transfers through end at all, static or not; bytes alone cannot tell a d...
unsigned sequences() const
The number of runtime sequences bytes indexes into.
bool apart(mlir::StringAttr a, mlir::StringAttr b) const
Whether both ends permit sharing: explicitly, by MM2S lifetime analysis, or, for a receiving end,...
static std::optional< End > endOf(DeviceOp device, mlir::StringAttr symbol)
The end a runtime sequence of device transfers through when it names symbol: a shim DMA route end,...
static mlir::StringAttr keyOf(RouteEndpointOp endpoint)
How ends and gates name a shim route end: by its objectFIFO, else by its own symbol.
mlir::StringAttr overlayEnd(int column) const
The planned control overlay channel's end on column column's shim tile.
llvm::SmallVector< llvm::SmallVector< unsigned > > groups(llvm::ArrayRef< mlir::StringAttr > ends) const
Splits ends into groups whose members are pairwise apart, each group taking one channel: first fit,...
ShimTransferSpans(DeviceOp device, llvm::function_ref< std::optional< End >(mlir::StringAttr)> endOf, std::optional< Gates > gates=std::nullopt, bool plannedOverlay=false)
endOf maps a symbol a runtime sequence transfers through to the objectFIFO end it belongs to,...
static Gates provenGates(DeviceOp device, llvm::function_ref< mlir::StringAttr(RouteEndpointOp)> keyOf)
Each receiving end's gates, keyed by keyOf: the shim inputs whose consumption its progress proves,...
ShimMM2SUses shimMM2SUses(DeviceOp device, bool packetSwitched)
The shim MM2S channels device's overlay weighs, for aie-generate-column-control-overlay.
llvm::DenseMap< std::pair< mlir::Operation *, int >, unsigned > packetFlows
Packet flows per (tile, channel).
llvm::DenseMap< mlir::Operation *, llvm::SmallSet< int, 2 > > circuits
Channels a circuit-switched route end, flow, allocation or shim mux holds, per shim tile op.
llvm::DenseMap< std::pair< mlir::Operation *, int >, llvm::SmallVector< mlir::StringAttr > > packetEnds
Packet-switched route ends, by name, per (tile, channel).
A shim end a runtime sequence transfers through.
bool sends
An MM2S end, sending into the array.
Gate(mlir::StringAttr input, ConsumedByAttr proof)
How one shim tile's unpinned ends in one direction share channels, as objectFIFO allocation shares th...
llvm::SmallVector< llvm::SmallVector< unsigned > > groups
The other ends, in groups taking one channel each.
llvm::SmallVector< std::optional< unsigned > > sharedChannel
Per end, the index of the shared channel it takes, if any.
SharedChannel(llvm::SmallVector< mlir::StringAttr > listed, bool pinned)
bool pinned
Whether a route end pins the channel, which then takes no other end.
llvm::SmallVector< mlir::StringAttr > listed