88 LogicalResult
generate(ArrayRef<DeviceOp> users) {
89 ModuleOp module = getOperation();
90 AIEDialect::IsCtrlPktOverlayAttrHelper marker(&getContext());
91 DeviceOp first = users.front();
92 auto target = first.getDevice();
94 std::set<int> columns;
96 for (DeviceOp device : users)
97 for (TileOp tile : device.getOps<TileOp>())
98 columns.insert(tile.colIndex());
99 for (
TileID tile : tokenTiles)
100 columns.insert(tile.col);
102 for (DeviceOp device : users)
103 device.walk([&](AIEX::NpuControlPacketOp packet) {
104 columns.insert(packet.getColumnFromAddr());
106 columns.insert(coverColumns.begin(), coverColumns.end());
109 SymbolTable symbols(module);
110 OpBuilder builder(module.getContext());
111 builder.setInsertionPointToEnd(module.getBody());
113 DeviceOp::create(builder, module.getLoc(), target,
114 builder.getStringAttr(
"ctrl_pkt_overlay"),
nullptr);
115 symbols.insert(overlay);
116 overlay.getRegion().emplaceBlock();
117 builder.setInsertionPointToEnd(overlay.getBody());
118 EndOp::create(builder, overlay.getLoc());
119 marker.setAttr(overlay, builder.getUnitAttr());
120 builder.setInsertionPointToStart(overlay.getBody());
121 for (DeviceOp device : users)
122 for (TileOp tile : device.getOps<TileOp>()) {
123 TileOp dest = TileOp::getOrCreate(builder, overlay, tile.colIndex(),
125 if (
auto id = tile->getAttr(
"controller_id")) {
126 if (
auto other = dest->getAttr(
"controller_id");
127 other && other !=
id) {
129 "has a different controller ID in another configuration");
132 dest->setAttr(
"controller_id",
id);
135 const auto &model = overlay.getTargetModel();
136 auto ids = model.getTileToControllerIdMap(
true);
137 auto flow = [&](TileOp source,
Port src, TileOp dest,
Port dst,
139 builder.setInsertionPoint(overlay.getBody()->getTerminator());
140 int id = ids[controller.getTileID()];
142 controller->getAttrOfType<PacketInfoAttr>(
"controller_id"))
143 id = attr.assignedId();
144 auto packet = PacketFlowOp::create(builder, controller.getLoc(),
id,
145 builder.getBoolAttr(
true),
146 builder.getBoolAttr(
true));
147 OpBuilder::InsertionGuard guard(builder);
148 builder.createBlock(&packet.getPorts());
149 PacketSourceOp::create(builder, packet.getLoc(), source, src.bundle,
151 PacketDestOp::create(builder, packet.getLoc(), dest, dst.bundle,
153 EndOp::create(builder, packet.getLoc());
155 std::map<int, int> controlChannel;
156 for (
int col : columns) {
157 if (col < 0 || col >= model.columns()) {
158 overlay.emitOpError(
"control coverage column is outside the target");
161 builder.setInsertionPointToStart(overlay.getBody());
162 TileOp shim = TileOp::getOrCreate(builder, overlay, col, 0);
163 flow(shim, {WireBundle::TileControl, 0}, shim, {WireBundle::South, 0},
165 for (
TileID tile : tokenTiles)
166 if (tile.col == col && tile.row != 0) {
167 builder.setInsertionPointToStart(overlay.getBody());
168 TileOp source = TileOp::getOrCreate(builder, overlay, col, tile.row);
169 flow(source, {WireBundle::TileControl, 0}, shim,
170 {WireBundle::South, 0}, source);
175 unsigned best = std::numeric_limits<unsigned>::max();
177 adaptive ? model.getNumDMAChannels(col, 0, DMAChannelDir::MM2S) : 1;
178 for (
int ch = 0; ch < count; ++ch)
185 overlay.emitOpError(
"failed to generate control overlay: no available "
186 "shim MM2S channel in column ")
190 controlChannel[col] = channel;
191 builder.setInsertionPoint(overlay.getBody()->getTerminator());
192 auto alloc = ShimDMAAllocationOp::create(
193 builder, shim.getLoc(), getCtrlPktAllocName(col, channel), shim,
194 DMAChannelDir::MM2S, channel,
false,
nullptr,
nullptr);
195 marker.setAttr(alloc, builder.getUnitAttr());
196 for (
int row = 0; row < model.rows(); ++row) {
197 builder.setInsertionPointToStart(overlay.getBody());
198 TileOp dest = TileOp::getOrCreate(builder, overlay, col, row);
199 flow(shim, {WireBundle::DMA, channel}, dest,
200 {WireBundle::TileControl, 0}, dest);
203 if (configuration && multicastSpan &&
204 failed(
addMulticasts(builder, overlay, users, controlChannel)))
206 for (DeviceOp device : users) {
207 device.setOverlayAttr(FlatSymbolRefAttr::get(overlay.getSymNameAttr()));
209 AIEDialect::ReserveControllerPacketIdsAttrHelper(&getContext())
210 .setAttr(device, builder.getUnitAttr());
214 std::vector<std::pair<TileID, Port>> avoided;
220 OwningOpRef<Operation *> unrouted;
221 if (!avoided.empty())
222 unrouted = overlay->clone();
223 if (failed(
route(overlay, avoided)))
226 overlay.getRegion().takeBody(unrouted.get()->getRegion(0));
227 if (failed(
route(overlay, {})))
232 for (
auto box : overlay.getOps<SwitchboxOp>()) {
233 auto amsels = llvm::to_vector(box.getConnections().getOps<AMSelOp>());
234 std::set<std::pair<int, int>> slots;
235 for (
auto amsel : amsels)
236 slots.emplace(amsel.arbiterIndex(), amsel.getMselValue());
237 builder.setInsertionPoint(box.getConnections().front().getTerminator());
238 for (
auto amsel : amsels)
239 for (
unsigned msel = 0; msel < model.getNumMselsPerArbiter(); ++msel)
240 if (slots.emplace(amsel.arbiterIndex(), msel).second)
241 AMSelOp::create(builder, amsel.getLoc(), amsel.arbiterIndex(),
244 overlay.walk([&](Operation *op) {
245 op->removeAttr(
"aie.priority_route");
246 if (!isa<DeviceOp, EndOp, PacketRulesOp>(op))
247 marker.setAttr(op, builder.getUnitAttr());
291 ArrayRef<DeviceOp> users,
292 const std::map<int, int> &controlChannel) {
293 std::set<TileID> cores;
294 for (DeviceOp device : users)
295 for (CoreOp core : device.getOps<CoreOp>())
296 cores.insert(core.getTileOp().getTileID());
299 int base = cores.begin()->col;
300 std::map<int, SmallVector<TileID>> groups;
302 groups[(t.col - base) /
static_cast<int>(multicastSpan)].push_back(t);
303 for (
auto it = groups.begin(); it != groups.end();)
304 it = it->second.size() < 2 ? groups.erase(it) : std::next(it);
308 SmallVector<PacketIdSpace> spaces;
309 for (DeviceOp device : users)
310 spaces.emplace_back(device,
true);
314 int id = overlay.getTargetModel().getMaxPacketId();
315 for (;
id >= 0; --id)
323 MLIRContext *ctx = overlay.getContext();
324 SmallVector<Attribute> record;
325 for (
auto &[g, members] : groups) {
326 int srcCol = members.front().col;
327 auto channel = controlChannel.find(srcCol);
328 if (channel == controlChannel.end())
329 return overlay.emitOpError(
"has no control channel in column ")
330 << srcCol <<
" for a control multicast";
332 builder.setInsertionPointToStart(overlay.getBody());
333 TileOp shim = TileOp::getOrCreate(builder, overlay, srcCol, 0);
334 SmallVector<TileOp> tiles;
335 SmallVector<Attribute> listed;
336 for (
TileID t : members) {
337 tiles.push_back(TileOp::getOrCreate(builder, overlay, t.col, t.row));
338 listed.push_back(TileAttr::get(ctx, t.col, t.row));
340 builder.setInsertionPoint(overlay.getBody()->getTerminator());
341 auto flow = PacketFlowOp::create(builder, shim.getLoc(),
id,
342 builder.getBoolAttr(
true),
343 builder.getBoolAttr(
true));
344 OpBuilder::InsertionGuard guard(builder);
345 builder.createBlock(&flow.getPorts());
346 PacketSourceOp::create(builder, flow.getLoc(), shim, WireBundle::DMA,
348 for (TileOp t : tiles)
349 PacketDestOp::create(builder, flow.getLoc(), t, WireBundle::TileControl,
351 EndOp::create(builder, flow.getLoc());
352 record.push_back(DictionaryAttr::get(
354 {builder.getNamedAttr(
356 FlatSymbolRefAttr::get(
357 ctx, getCtrlPktAllocName(srcCol, channel->second))),
358 builder.getNamedAttr(
"packet_id", builder.getI32IntegerAttr(
id)),
359 builder.getNamedAttr(
"tiles", ArrayAttr::get(ctx, listed))}));
361 AIEDialect::CtrlPktMulticastAttrHelper(ctx).setAttr(
362 overlay, ArrayAttr::get(ctx, record));
412 std::vector<std::pair<TileID, Port>> &avoided) {
413 OwningOpRef<ModuleOp> trial = cast<ModuleOp>(module->clone());
414 for (DeviceOp device :
415 llvm::make_early_inc_range(trial->getOps<DeviceOp>())) {
416 if (device->hasAttr(
"aie.is_ctrl_pkt_overlay"))
419 device.removeOverlayAttr();
421 PassManager pm(&getContext());
422 auto &dpm = pm.nest<DeviceOp>();
426 if (failed(pm.run(*trial))) {
427 module.emitError("failed to analyze application resources for adaptive "
431 std::set<std::pair<TileID, Port>> ports;
432 for (DeviceOp device : trial->getOps<DeviceOp>()) {
433 for (SwitchboxOp box : device.getOps<SwitchboxOp>()) {
434 for (ConnectOp connect : box.getOps<ConnectOp>())
435 ports.insert({box.getTileOp().getTileID(), connect.destPort()});
436 for (MasterSetOp master : box.getOps<MasterSetOp>())
437 ports.insert({box.getTileOp().getTileID(), master.destPort()});
442 device.emitOpError(
"cannot plan adaptive control overlay: ")
443 << llvm::toString(std::move(error));
447 for (
const auto &[tile, setting] : settings)
448 for (
Port output : setting.dsts)
449 ports.insert({tile, output});
456 avoided.assign(ports.begin(), ports.end());