12#include "mlir/Pass/Pass.h"
15#define GEN_PASS_DEF_AIESPLITFLOWVIAS
16#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
29static bool isDirectWire(Value srcTile, WireBundle srcBundle,
int srcChannel,
30 Value dstTile, WireBundle dstBundle,
int dstChannel) {
31 if (srcChannel != dstChannel) {
34 auto src = srcTile.getDefiningOp<TileOp>();
35 auto dst = dstTile.getDefiningOp<TileOp>();
39 int sc = src.colIndex(), sr = src.rowIndex();
40 int dc = dst.colIndex(), dr = dst.rowIndex();
42 if (srcBundle == WireBundle::North && dstBundle == WireBundle::South &&
46 if (srcBundle == WireBundle::South && dstBundle == WireBundle::North &&
52 if (srcBundle == WireBundle::East && dstBundle == WireBundle::West &&
56 if (srcBundle == WireBundle::West && dstBundle == WireBundle::East &&
64static bool isDirectShimMux(Value srcTile, WireBundle srcBundle,
int srcChannel,
65 Value dstTile, WireBundle dstBundle,
67 auto tile = srcTile.getDefiningOp<TileOp>();
68 if (srcTile != dstTile || !tile ||
71 if (dstBundle == WireBundle::South)
73 if (srcBundle == WireBundle::South)
78static FailureOr<std::pair<Value, WireBundle>> wireEndpoint(DeviceOp device,
82 auto tile = cast<TileOp>(tileValue.getDefiningOp());
83 if (!llvm::is_contained({WireBundle::North, WireBundle::South,
84 WireBundle::East, WireBundle::West},
86 return std::make_pair(tileValue, bundle);
89 tile.getTileID(),
Port{bundle, 0});
92 TileID neighbor = endpoint->first;
94 for (TileOp candidate : device.getOps<TileOp>())
95 if (candidate.getTileID() == neighbor)
96 return std::pair<Value, WireBundle>(candidate.getResult(),
97 endpoint->second.bundle);
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)
104 endpoint->second.bundle);
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);
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);
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) {
142 if (isDirectWire(srcTile, srcBundle, srcChannel, dstTile, dstBundle,
146 OpBuilder::InsertionGuard guard(builder);
147 builder.setInsertionPoint(anchor);
149 FlowOp::create(builder, loc, srcTile, srcBundle, srcChannel, dstTile,
150 dstBundle, dstChannel);
155 emitPacketFlow(builder, loc, anchor, srcTile, srcBundle, srcChannel, dstTile,
156 dstBundle, dstChannel, packetID, maskAttr, keepPktHeaderAttr,
160struct AIESplitFlowViasPass
161 :
public xilinx::AIE::impl::AIESplitFlowViasBase<AIESplitFlowViasPass> {
162 void runOnOperation()
override {
163 DeviceOp device = getOperation();
164 OpBuilder builder(device.getContext());
166 SmallVector<FlowOp> flowsWithVias;
167 for (
auto flow : device.getOps<FlowOp>()) {
168 if (!flow.getVias().empty()) {
169 flowsWithVias.push_back(flow);
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();
185 Value srcTile = flow.getSource();
186 WireBundle srcBundle = flow.getSourceBundle();
187 int srcChannel = flow.getSourceChannel();
189 auto emitSegment = [&](Value dstTile, WireBundle dstBundle,
191 emitRoutableSegment(builder, loc, flow, srcTile, srcBundle, srcChannel,
192 dstTile, dstBundle, dstChannel, -1);
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];
203 isDirectShimMux(srcTile, srcBundle, srcChannel, viaTile,
204 ingressBundle, ingressChannel);
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();
216 segmentSource = *endpoint;
219 wireEndpoint(device, builder, viaTile, ingressBundle);
220 if (failed(wireSource)) {
221 flow.emitOpError(
"via ingress has no neighboring tile");
222 return signalPassFailure();
224 auto [wireTile, wireBundle] = *wireSource;
225 srcTile = segmentSource.first;
226 srcBundle = segmentSource.second;
227 emitSegment(wireTile, wireBundle, ingressChannel);
230 Value localSrcTile = foldIngress ? srcTile : viaTile;
231 WireBundle localSrcBundle = foldIngress ? srcBundle : ingressBundle;
232 int localSrcChannel = foldIngress ? srcChannel : ingressChannel;
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();
239 i + 1 < e ? ingressChannels[i + 1] : flow.getDestChannel();
240 bool foldEgress = isDirectShimMux(viaTile, egressBundle, egressChannel,
241 nextTile, nextBundle, nextChannel);
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);
250 srcTile = foldEgress ? nextTile : viaTile;
251 srcBundle = foldEgress ? nextBundle : egressBundle;
252 srcChannel = foldEgress ? nextChannel : egressChannel;
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();
262 std::tie(srcTile, srcBundle) = *endpoint;
264 emitSegment(flow.getDest(), flow.getDestBundle(), flow.getDestChannel());
271 SmallVector<PacketFlowOp> pktFlowsWithVias;
272 for (
auto pf : device.getOps<PacketFlowOp>()) {
273 if (!pf.getVias().empty()) {
274 pktFlowsWithVias.push_back(pf);
278 for (PacketFlowOp pf : pktFlowsWithVias) {
279 Location loc = pf.getLoc();
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();
288 PacketSourceOp source;
290 for (Operation &op : pf.getPorts().front()) {
291 if (
auto s = dyn_cast<PacketSourceOp>(op)) {
293 }
else if (
auto d = dyn_cast<PacketDestOp>(op)) {
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());
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];
315 bool foldIngress = isDirectShimMux(srcTile, srcBundle, srcChannel,
316 viaTile, ingress, ingressChannel);
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();
327 segmentSource = *endpoint;
329 auto wireSource = wireEndpoint(device, builder, viaTile, ingress);
330 if (failed(wireSource)) {
331 pf.emitOpError(
"via ingress has no neighboring tile");
332 return signalPassFailure();
334 auto [wireTile, wireBundle] = *wireSource;
335 srcTile = segmentSource.first;
336 srcBundle = segmentSource.second;
337 emitSegment(wireTile, wireBundle, ingressChannel);
340 Value localSrcTile = foldIngress ? srcTile : viaTile;
341 WireBundle localSrcBundle = foldIngress ? srcBundle : ingress;
342 int localSrcChannel = foldIngress ? srcChannel : ingressChannel;
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);
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();
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());
364 srcTile = foldEgress ? nextTile : viaTile;
365 srcBundle = foldEgress ? nextBundle : egress;
366 srcChannel = foldEgress ? nextChannel : egressChannel;
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();
376 std::tie(srcTile, srcBundle) = *endpoint;
378 emitSegment(dest.getTile(), dest.getBundle(), dest.getChannel(),
379 pf.getKeepPktHeaderAttr());
388 return std::make_unique<AIESplitFlowViasPass>();
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
std::optional< std::pair< TileID, Port > > getConnectingWireEndpoint(const AIETargetModel &targetModel, TileID tile, Port port)
getLinkedInput, restricted to tiles on the device.