MLIR-AIE 1.4.6.dev
AIESplitFlowVias.cpp
Go to the documentation of this file.
1//===- AIESplitFlowVias.cpp -------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2026 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
11
12#include "mlir/Pass/Pass.h"
13
14namespace xilinx::AIE {
15#define GEN_PASS_DEF_AIESPLITFLOWVIAS
16#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
17} // namespace xilinx::AIE
18
19using namespace mlir;
20using namespace xilinx;
21using namespace xilinx::AIE;
22
23namespace {
24
25// True when (srcTile, srcBundle:srcChannel) and (dstTile, dstBundle:dstChannel)
26// are the two ends of a single physical inter-switchbox wire. Such a segment is
27// realized by the wire itself and must not become a routable flow: doing so
28// would double-drive the port that the adjacent local via flow produces.
29static bool isDirectWire(Value srcTile, WireBundle srcBundle, int srcChannel,
30 Value dstTile, WireBundle dstBundle, int dstChannel) {
31 if (srcChannel != dstChannel) {
32 return false;
33 }
34 auto src = srcTile.getDefiningOp<TileOp>();
35 auto dst = dstTile.getDefiningOp<TileOp>();
36 if (!src || !dst) {
37 return false;
38 }
39 int sc = src.colIndex(), sr = src.rowIndex();
40 int dc = dst.colIndex(), dr = dst.rowIndex();
41 if (sc == dc) {
42 if (srcBundle == WireBundle::North && dstBundle == WireBundle::South &&
43 dr == sr + 1) {
44 return true;
45 }
46 if (srcBundle == WireBundle::South && dstBundle == WireBundle::North &&
47 dr == sr - 1) {
48 return true;
49 }
50 }
51 if (sr == dr) {
52 if (srcBundle == WireBundle::East && dstBundle == WireBundle::West &&
53 dc == sc + 1) {
54 return true;
55 }
56 if (srcBundle == WireBundle::West && dstBundle == WireBundle::East &&
57 dc == sc - 1) {
58 return true;
59 }
60 }
61 return false;
62}
63
64static bool isDirectShimMux(Value srcTile, WireBundle srcBundle, int srcChannel,
65 Value dstTile, WireBundle dstBundle,
66 int dstChannel) {
67 auto tile = srcTile.getDefiningOp<TileOp>();
68 if (srcTile != dstTile || !tile ||
69 !getTargetModel(tile).hasShimMux(tile.getCol(), tile.getRow()))
70 return false;
71 if (dstBundle == WireBundle::South)
72 return dstChannel == shimMuxChannelFrom({srcBundle, srcChannel});
73 if (srcBundle == WireBundle::South)
74 return srcChannel == shimMuxChannelTo({dstBundle, dstChannel});
75 return false;
76}
77
78static FailureOr<std::pair<Value, WireBundle>> wireEndpoint(DeviceOp device,
79 OpBuilder &builder,
80 Value tileValue,
81 WireBundle bundle) {
82 auto tile = cast<TileOp>(tileValue.getDefiningOp());
83 if (!llvm::is_contained({WireBundle::North, WireBundle::South,
84 WireBundle::East, WireBundle::West},
85 bundle))
86 return std::make_pair(tileValue, bundle);
87
88 auto endpoint = getConnectingWireEndpoint(device.getTargetModel(),
89 tile.getTileID(), Port{bundle, 0});
90 if (!endpoint)
91 return failure();
92 TileID neighbor = endpoint->first;
93
94 for (TileOp candidate : device.getOps<TileOp>())
95 if (candidate.getTileID() == neighbor)
96 return std::pair<Value, WireBundle>(candidate.getResult(),
97 endpoint->second.bundle);
98
99 OpBuilder::InsertionGuard guard(builder);
100 builder.setInsertionPoint(tile);
101 return std::pair<Value, WireBundle>(
102 TileOp::create(builder, tile.getLoc(), neighbor.col, neighbor.row)
103 .getResult(),
104 endpoint->second.bundle);
105}
106
107static void emitPacketFlow(OpBuilder &builder, Location loc, Operation *anchor,
108 Value srcTile, WireBundle srcBundle, int srcChannel,
109 Value dstTile, WireBundle dstBundle, int dstChannel,
110 int packetID, IntegerAttr maskAttr,
111 BoolAttr keepPktHeaderAttr,
112 BoolAttr priorityRouteAttr) {
113 OpBuilder::InsertionGuard guard(builder);
114 builder.setInsertionPoint(anchor);
115 auto pf = PacketFlowOp::create(builder, loc, static_cast<uint8_t>(packetID),
116 keepPktHeaderAttr, priorityRouteAttr);
117 if (maskAttr)
118 pf.setMaskAttr(maskAttr);
119 PacketFlowOp::ensureTerminator(pf.getPorts(), builder, loc);
120 builder.setInsertionPoint(pf.getPorts().front().getTerminator());
121 PacketSourceOp::create(builder, loc, srcTile, srcBundle, srcChannel);
122 PacketDestOp::create(builder, loc, dstTile, dstBundle, dstChannel);
123}
124
125// Emit the routable portion of a segment between two pinned ports, eliding the
126// parts the wires and vias already realize: a degenerate same-port segment or
127// a single inter-switchbox wire. Whatever remains is a gap the router must fill
128// and becomes a flow -- circuit when `packetID` < 0, otherwise a
129// packet_flow(packetID).
130static void emitRoutableSegment(OpBuilder &builder, Location loc,
131 Operation *anchor, Value srcTile,
132 WireBundle srcBundle, int srcChannel,
133 Value dstTile, WireBundle dstBundle,
134 int dstChannel, int packetID,
135 mlir::IntegerAttr maskAttr = {},
136 mlir::BoolAttr keepPktHeaderAttr = {},
137 mlir::BoolAttr priorityRouteAttr = {}) {
138 if (srcTile == dstTile && srcBundle == dstBundle &&
139 srcChannel == dstChannel) {
140 return;
141 }
142 if (isDirectWire(srcTile, srcBundle, srcChannel, dstTile, dstBundle,
143 dstChannel)) {
144 return;
145 }
146 OpBuilder::InsertionGuard guard(builder);
147 builder.setInsertionPoint(anchor);
148 if (packetID < 0) {
149 FlowOp::create(builder, loc, srcTile, srcBundle, srcChannel, dstTile,
150 dstBundle, dstChannel);
151 return;
152 }
153 // Preserve the mask across every section so the router derives the same
154 // packet claim for the gaps and local via flows.
155 emitPacketFlow(builder, loc, anchor, srcTile, srcBundle, srcChannel, dstTile,
156 dstBundle, dstChannel, packetID, maskAttr, keepPktHeaderAttr,
157 priorityRouteAttr);
158}
159
160struct AIESplitFlowViasPass
161 : public xilinx::AIE::impl::AIESplitFlowViasBase<AIESplitFlowViasPass> {
162 void runOnOperation() override {
163 DeviceOp device = getOperation();
164 OpBuilder builder(device.getContext());
165
166 SmallVector<FlowOp> flowsWithVias;
167 for (auto flow : device.getOps<FlowOp>()) {
168 if (!flow.getVias().empty()) {
169 flowsWithVias.push_back(flow);
170 }
171 }
172
173 for (FlowOp flow : flowsWithVias) {
174 ArrayRef<int32_t> ingressBundles =
175 flow.getViaIngressBundlesAttr().asArrayRef();
176 ArrayRef<int32_t> ingressChannels =
177 flow.getViaIngressChannelsAttr().asArrayRef();
178 ArrayRef<int32_t> egressBundles =
179 flow.getViaEgressBundlesAttr().asArrayRef();
180 ArrayRef<int32_t> egressChannels =
181 flow.getViaEgressChannelsAttr().asArrayRef();
182 Location loc = flow.getLoc();
183
184 // Running source of the next segment, starting at the flow's source.
185 Value srcTile = flow.getSource();
186 WireBundle srcBundle = flow.getSourceBundle();
187 int srcChannel = flow.getSourceChannel();
188
189 auto emitSegment = [&](Value dstTile, WireBundle dstBundle,
190 int dstChannel) {
191 emitRoutableSegment(builder, loc, flow, srcTile, srcBundle, srcChannel,
192 dstTile, dstBundle, dstChannel, /*packetID=*/-1);
193 };
194
195 for (size_t i = 0, e = flow.getVias().size(); i < e; i++) {
196 Value viaTile = flow.getVias()[i];
197 auto ingressBundle = static_cast<WireBundle>(ingressBundles[i]);
198 int ingressChannel = ingressChannels[i];
199 auto egressBundle = static_cast<WireBundle>(egressBundles[i]);
200 int egressChannel = egressChannels[i];
201
202 bool foldIngress =
203 isDirectShimMux(srcTile, srcBundle, srcChannel, viaTile,
204 ingressBundle, ingressChannel);
205 if (!foldIngress) {
206 bool directWire =
207 isDirectWire(srcTile, srcBundle, srcChannel, viaTile,
208 ingressBundle, ingressChannel);
209 auto segmentSource = std::make_pair(srcTile, srcBundle);
210 if (i > 0 && !directWire) {
211 auto endpoint = wireEndpoint(device, builder, srcTile, srcBundle);
212 if (failed(endpoint)) {
213 flow.emitOpError("via egress has no neighboring tile");
214 return signalPassFailure();
215 }
216 segmentSource = *endpoint;
217 }
218 auto wireSource =
219 wireEndpoint(device, builder, viaTile, ingressBundle);
220 if (failed(wireSource)) {
221 flow.emitOpError("via ingress has no neighboring tile");
222 return signalPassFailure();
223 }
224 auto [wireTile, wireBundle] = *wireSource;
225 srcTile = segmentSource.first;
226 srcBundle = segmentSource.second;
227 emitSegment(wireTile, wireBundle, ingressChannel);
228 }
229
230 Value localSrcTile = foldIngress ? srcTile : viaTile;
231 WireBundle localSrcBundle = foldIngress ? srcBundle : ingressBundle;
232 int localSrcChannel = foldIngress ? srcChannel : ingressChannel;
233
234 Value nextTile = i + 1 < e ? flow.getVias()[i + 1] : flow.getDest();
235 WireBundle nextBundle =
236 i + 1 < e ? static_cast<WireBundle>(ingressBundles[i + 1])
237 : flow.getDestBundle();
238 int nextChannel =
239 i + 1 < e ? ingressChannels[i + 1] : flow.getDestChannel();
240 bool foldEgress = isDirectShimMux(viaTile, egressBundle, egressChannel,
241 nextTile, nextBundle, nextChannel);
242
243 OpBuilder::InsertionGuard guard(builder);
244 builder.setInsertionPoint(flow);
245 FlowOp::create(builder, loc, localSrcTile, localSrcBundle,
246 localSrcChannel, foldEgress ? nextTile : viaTile,
247 foldEgress ? nextBundle : egressBundle,
248 foldEgress ? nextChannel : egressChannel);
249
250 srcTile = foldEgress ? nextTile : viaTile;
251 srcBundle = foldEgress ? nextBundle : egressBundle;
252 srcChannel = foldEgress ? nextChannel : egressChannel;
253 }
254
255 if (srcTile != flow.getDest() || srcBundle != flow.getDestBundle() ||
256 srcChannel != flow.getDestChannel()) {
257 auto endpoint = wireEndpoint(device, builder, srcTile, srcBundle);
258 if (failed(endpoint)) {
259 flow.emitOpError("via egress has no neighboring tile");
260 return signalPassFailure();
261 }
262 std::tie(srcTile, srcBundle) = *endpoint;
263 }
264 emitSegment(flow.getDest(), flow.getDestBundle(), flow.getDestChannel());
265 flow.erase();
266 }
267
268 // Packet sections pin their route with vias too. Each via becomes a local
269 // packet flow so the router can plan its rules, master sets, and arbiters
270 // with the other packet flows.
271 SmallVector<PacketFlowOp> pktFlowsWithVias;
272 for (auto pf : device.getOps<PacketFlowOp>()) {
273 if (!pf.getVias().empty()) {
274 pktFlowsWithVias.push_back(pf);
275 }
276 }
277
278 for (PacketFlowOp pf : pktFlowsWithVias) {
279 Location loc = pf.getLoc();
280 int id = pf.IDInt();
281 ArrayRef<int32_t> inB = pf.getViaIngressBundlesAttr().asArrayRef();
282 ArrayRef<int32_t> inC = pf.getViaIngressChannelsAttr().asArrayRef();
283 ArrayRef<int32_t> egB = pf.getViaEgressBundlesAttr().asArrayRef();
284 ArrayRef<int32_t> egC = pf.getViaEgressChannelsAttr().asArrayRef();
285
286 // A section has exactly one source and one dest; the gaps between the
287 // pinned vias (and before the first / after the last) are routed.
288 PacketSourceOp source;
289 PacketDestOp dest;
290 for (Operation &op : pf.getPorts().front()) {
291 if (auto s = dyn_cast<PacketSourceOp>(op)) {
292 source = s;
293 } else if (auto d = dyn_cast<PacketDestOp>(op)) {
294 dest = d;
295 }
296 }
297 Value srcTile = source.getTile();
298 WireBundle srcBundle = source.getBundle();
299 int srcChannel = source.getChannel();
300 auto emitSegment = [&](Value dstTile, WireBundle dstBundle,
301 int dstChannel, BoolAttr keepPktHeader = {}) {
302 emitRoutableSegment(builder, loc, pf, srcTile, srcBundle, srcChannel,
303 dstTile, dstBundle, dstChannel, id,
304 pf.getMaskAttr(), keepPktHeader,
305 pf.getPriorityRouteAttr());
306 };
307
308 for (size_t i = 0, e = pf.getVias().size(); i < e; i++) {
309 Value viaTile = pf.getVias()[i];
310 auto ingress = static_cast<WireBundle>(inB[i]);
311 int ingressChannel = inC[i];
312 auto egress = static_cast<WireBundle>(egB[i]);
313 int egressChannel = egC[i];
314
315 bool foldIngress = isDirectShimMux(srcTile, srcBundle, srcChannel,
316 viaTile, ingress, ingressChannel);
317 if (!foldIngress) {
318 bool directWire = isDirectWire(srcTile, srcBundle, srcChannel,
319 viaTile, ingress, ingressChannel);
320 auto segmentSource = std::make_pair(srcTile, srcBundle);
321 if (i > 0 && !directWire) {
322 auto endpoint = wireEndpoint(device, builder, srcTile, srcBundle);
323 if (failed(endpoint)) {
324 pf.emitOpError("via egress has no neighboring tile");
325 return signalPassFailure();
326 }
327 segmentSource = *endpoint;
328 }
329 auto wireSource = wireEndpoint(device, builder, viaTile, ingress);
330 if (failed(wireSource)) {
331 pf.emitOpError("via ingress has no neighboring tile");
332 return signalPassFailure();
333 }
334 auto [wireTile, wireBundle] = *wireSource;
335 srcTile = segmentSource.first;
336 srcBundle = segmentSource.second;
337 emitSegment(wireTile, wireBundle, ingressChannel);
338 }
339
340 Value localSrcTile = foldIngress ? srcTile : viaTile;
341 WireBundle localSrcBundle = foldIngress ? srcBundle : ingress;
342 int localSrcChannel = foldIngress ? srcChannel : ingressChannel;
343
344 Value nextTile = i + 1 < e ? pf.getVias()[i + 1] : dest.getTile();
345 WireBundle nextBundle =
346 i + 1 < e ? static_cast<WireBundle>(inB[i + 1]) : dest.getBundle();
347 int nextChannel = i + 1 < e ? inC[i + 1] : dest.getChannel();
348 bool foldEgress = isDirectShimMux(viaTile, egress, egressChannel,
349 nextTile, nextBundle, nextChannel);
350
351 BoolAttr keepPktHeader;
352 if ((foldEgress && nextTile == dest.getTile() &&
353 nextBundle == dest.getBundle() &&
354 nextChannel == dest.getChannel()) ||
355 (viaTile == dest.getTile() && egress == dest.getBundle() &&
356 egressChannel == dest.getChannel()))
357 keepPktHeader = pf.getKeepPktHeaderAttr();
358 emitPacketFlow(
359 builder, loc, pf, localSrcTile, localSrcBundle, localSrcChannel,
360 foldEgress ? nextTile : viaTile, foldEgress ? nextBundle : egress,
361 foldEgress ? nextChannel : egressChannel, id, pf.getMaskAttr(),
362 keepPktHeader, pf.getPriorityRouteAttr());
363
364 srcTile = foldEgress ? nextTile : viaTile;
365 srcBundle = foldEgress ? nextBundle : egress;
366 srcChannel = foldEgress ? nextChannel : egressChannel;
367 }
368
369 if (srcTile != dest.getTile() || srcBundle != dest.getBundle() ||
370 srcChannel != dest.getChannel()) {
371 auto endpoint = wireEndpoint(device, builder, srcTile, srcBundle);
372 if (failed(endpoint)) {
373 pf.emitOpError("via egress has no neighboring tile");
374 return signalPassFailure();
375 }
376 std::tie(srcTile, srcBundle) = *endpoint;
377 }
378 emitSegment(dest.getTile(), dest.getBundle(), dest.getChannel(),
379 pf.getKeepPktHeaderAttr());
380 pf.erase();
381 }
382 }
383};
384
385} // namespace
386
387std::unique_ptr<OperationPass<DeviceOp>> AIE::createAIESplitFlowViasPass() {
388 return std::make_unique<AIESplitFlowViasPass>();
389}
int shimMuxChannelTo(Port dst)
struct TileID { friend std::ostream &operator<<(std::ostream &os, const TileID &s) { os<< "TileID("<< s.col<< ", "<< s.row<< ")"; return os; } friend std::string to_string(const TileID &s) { std::ostringstream ss; ss<< s; return ss.str(); } friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const TileID &s) { os<< to_string(s); return os; } inline bool operator<(const TileID &rhs) const { return std::tie(col, row)< std::tie(rhs.col, rhs.row); } bool operator==(const TileID &rhs) const { return std::tie(col, row)==std::tie(rhs.col, rhs.row); } bool operator!=(const TileID &rhs) const { return !(*this==rhs);} int col, row;} TileID
const AIETargetModel & getTargetModel(mlir::Operation *op)
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIESplitFlowViasPass()
int shimMuxChannelFrom(Port src)
struct Port { WireBundle bundle; int channel; bool operator==(const Port &rhs) const { return std::tie(bundle, channel)==std::tie(rhs.bundle, rhs.channel); } bool operator!=(const Port &rhs) const { return !(*this==rhs);} bool operator<(const Port &rhs) const { return std::tie(bundle, channel)< std::tie(rhs.bundle, rhs.channel); } friend std::ostream &operator<<(std::ostream &os, const Port &port) { os<< "("; os<< stringifyWireBundle(port.bundle).str(); os<< ": "<< std::to_string(port.channel)<< ")"; return os; } friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Port &port) { os<< to_string(port); return os; } } Port
Definition AIEDialect.h:265
std::optional< std::pair< TileID, Port > > getConnectingWireEndpoint(const AIETargetModel &targetModel, TileID tile, Port port)
getLinkedInput, restricted to tiles on the device.