MLIR-AIE 1.4.6.dev
AIEGenerateColumnControlOverlay.cpp
Go to the documentation of this file.
1//===- AIEGenerateColumnControlOverlay.cpp ----------------------*- C++ -*-===//
2//
3// Copyright (C) 2024 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
14
15#include "mlir/IR/Attributes.h"
16#include "mlir/Pass/PassManager.h"
17
18#include "llvm/ADT/SmallSet.h"
19
20#include <map>
21#include <optional>
22#include <set>
23#include <tuple>
24
25namespace xilinx::AIE {
26#define GEN_PASS_DEF_AIEGENERATECOLUMNCONTROLOVERLAY
27#define GEN_PASS_DEF_AIEASSIGNTILECTRLIDS
28#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
29} // namespace xilinx::AIE
30
31#define DEBUG_TYPE "aie-generate-column-control-overlay"
32
33using namespace mlir;
34using namespace xilinx;
35using namespace xilinx::AIE;
36
37static std::string getCtrlPktAllocName(int col, int chan) {
38 return "ctrlpkt_col" + std::to_string(col) + "_mm2s_chan" +
39 std::to_string(chan);
40}
41
43 : xilinx::AIE::impl::AIEAssignTileCtrlIDsBase<AIEAssignTileCtrlIDsPass> {
44 void runOnOperation() override {
45 DeviceOp device = getOperation();
46 const auto &targetModel = device.getTargetModel();
47
48 if (!targetModel.hasProperty(AIETargetModel::SupportsControlPackets))
49 return;
50
51 auto tileIDMap =
52 targetModel.getTileToControllerIdMap(clColumnWiseUniqueIDs);
53 for (auto tile : device.getOps<TileOp>()) {
54 if (!tile->hasAttr("controller_id"))
55 tile->setAttr("controller_id",
56 PacketInfoAttr::get(tile.getContext(), 0,
57 tileIDMap[tile.getTileID()]));
58 }
59 }
60};
61
64 AIEGenerateColumnControlOverlayPass> {
65 using Base::Base;
66 DenseMap<std::pair<int, int>, unsigned> packetDemand;
67 DenseMap<int, llvm::SmallSet<int, 2>> fixedCircuits;
68
69 void runOnOperation() override {
70 llvm::MapVector<AIEDevice, SmallVector<DeviceOp>> groups;
71 for (DeviceOp device : getOperation().getOps<DeviceOp>())
72 if (getsColumnControlOverlay(device, configuration))
73 groups[device.getDevice()].push_back(device);
74 for (auto &[target, users] : groups)
75 if (failed(generate(users)))
76 return signalPassFailure();
77 }
78
79 LogicalResult generate(ArrayRef<DeviceOp> users) {
80 ModuleOp module = getOperation();
81 AIEDialect::IsCtrlPktOverlayAttrHelper marker(&getContext());
82 DeviceOp first = users.front();
83 auto target = first.getDevice();
84 auto tokenTiles = collectTokenIssuingTiles(users);
85 std::set<int> columns;
86 if (configuration)
87 for (DeviceOp device : users)
88 for (TileOp tile : device.getOps<TileOp>())
89 columns.insert(tile.colIndex());
90 for (TileID tile : tokenTiles)
91 columns.insert(tile.col);
92 if (configuration) {
93 for (DeviceOp device : users)
94 device.walk([&](AIEX::NpuControlPacketOp packet) {
95 columns.insert(packet.getColumnFromAddr());
96 });
97 columns.insert(coverColumns.begin(), coverColumns.end());
99 }
100 SymbolTable symbols(module);
101 OpBuilder builder(module.getContext());
102 builder.setInsertionPointToEnd(module.getBody());
103 DeviceOp overlay =
104 DeviceOp::create(builder, module.getLoc(), target,
105 builder.getStringAttr("ctrl_pkt_overlay"), nullptr);
106 symbols.insert(overlay);
107 overlay.getRegion().emplaceBlock();
108 builder.setInsertionPointToEnd(overlay.getBody());
109 EndOp::create(builder, overlay.getLoc());
110 marker.setAttr(overlay, builder.getUnitAttr());
111 builder.setInsertionPointToStart(overlay.getBody());
112 for (DeviceOp device : users)
113 for (TileOp tile : device.getOps<TileOp>()) {
114 TileOp dest = TileOp::getOrCreate(builder, overlay, tile.colIndex(),
115 tile.rowIndex());
116 if (auto id = tile->getAttr("controller_id")) {
117 if (auto other = dest->getAttr("controller_id");
118 other && other != id) {
119 tile.emitOpError(
120 "has a different controller ID in another configuration");
121 return failure();
122 }
123 dest->setAttr("controller_id", id);
124 }
125 }
126 const auto &model = overlay.getTargetModel();
127 auto ids = model.getTileToControllerIdMap(true);
128 auto flow = [&](TileOp source, Port src, TileOp dest, Port dst,
129 TileOp controller) {
130 builder.setInsertionPoint(overlay.getBody()->getTerminator());
131 int id = ids[controller.getTileID()];
132 if (auto attr =
133 controller->getAttrOfType<PacketInfoAttr>("controller_id"))
134 id = attr.assignedId();
135 auto packet = PacketFlowOp::create(builder, controller.getLoc(), id,
136 builder.getBoolAttr(true),
137 builder.getBoolAttr(true));
138 OpBuilder::InsertionGuard guard(builder);
139 builder.createBlock(&packet.getPorts());
140 PacketSourceOp::create(builder, packet.getLoc(), source, src.bundle,
141 src.channel);
142 PacketDestOp::create(builder, packet.getLoc(), dest, dst.bundle,
143 dst.channel);
144 EndOp::create(builder, packet.getLoc());
145 };
146 for (int col : columns) {
147 if (col < 0 || col >= model.columns()) {
148 overlay.emitOpError("control coverage column is outside the target");
149 return failure();
150 }
151 builder.setInsertionPointToStart(overlay.getBody());
152 TileOp shim = TileOp::getOrCreate(builder, overlay, col, 0);
153 flow(shim, {WireBundle::TileControl, 0}, shim, {WireBundle::South, 0},
154 shim);
155 for (TileID tile : tokenTiles)
156 if (tile.col == col && tile.row != 0) {
157 builder.setInsertionPointToStart(overlay.getBody());
158 TileOp source = TileOp::getOrCreate(builder, overlay, col, tile.row);
159 flow(source, {WireBundle::TileControl, 0}, shim,
160 {WireBundle::South, 0}, source);
161 }
162 if (!configuration)
163 continue;
164 int channel = -1;
165 unsigned best = std::numeric_limits<unsigned>::max();
166 int count =
167 adaptive
168 ? model.getNumSourceShimMuxConnections(col, 0, WireBundle::DMA)
169 : 1;
170 for (int ch = 0; ch < count; ++ch)
171 if (!fixedCircuits.lookup(col).contains(ch) &&
172 packetDemand.lookup({col, ch}) < best) {
173 channel = ch;
174 best = packetDemand.lookup({col, ch});
175 }
176 if (channel < 0) {
177 overlay.emitOpError("failed to generate control overlay: no available "
178 "shim MM2S channel in column ")
179 << col;
180 return failure();
181 }
182 builder.setInsertionPoint(overlay.getBody()->getTerminator());
183 auto alloc = ShimDMAAllocationOp::create(
184 builder, shim.getLoc(), getCtrlPktAllocName(col, channel), shim,
185 DMAChannelDir::MM2S, channel, false, nullptr, nullptr);
186 marker.setAttr(alloc, builder.getUnitAttr());
187 for (int row = 0; row < model.rows(); ++row) {
188 builder.setInsertionPointToStart(overlay.getBody());
189 TileOp dest = TileOp::getOrCreate(builder, overlay, col, row);
190 flow(shim, {WireBundle::DMA, channel}, dest,
191 {WireBundle::TileControl, 0}, dest);
192 }
193 }
194 for (DeviceOp device : users) {
195 device.setOverlayAttr(FlatSymbolRefAttr::get(overlay.getSymNameAttr()));
196 if (configuration)
197 AIEDialect::ReserveControllerPacketIdsAttrHelper(&getContext())
198 .setAttr(device, builder.getUnitAttr());
199 }
200 if (!configuration)
201 return success();
202 std::vector<std::pair<TileID, Port>> avoided;
203 if (adaptive && failed(applicationPorts(module, avoided)))
204 return failure();
205 OpPassManager routing(DeviceOp::getOperationName());
206 routing.addPass(createAIEAssignTileCtrlIDsPass());
207 routing.addPass(createAIEPathfinderPass({}, avoided));
208 if (failed(runPipeline(routing, overlay)))
209 return failure();
210 // Reprogramming another master set of an active arbiter can stall control
211 // packets.
212 for (auto box : overlay.getOps<SwitchboxOp>()) {
213 auto amsels = llvm::to_vector(box.getConnections().getOps<AMSelOp>());
214 std::set<std::pair<int, int>> slots;
215 for (auto amsel : amsels)
216 slots.emplace(amsel.arbiterIndex(), amsel.getMselValue());
217 builder.setInsertionPoint(box.getConnections().front().getTerminator());
218 for (auto amsel : amsels)
219 for (unsigned msel = 0; msel < model.getNumMselsPerArbiter(); ++msel)
220 if (slots.emplace(amsel.arbiterIndex(), msel).second)
221 AMSelOp::create(builder, amsel.getLoc(), amsel.arbiterIndex(),
222 msel);
223 }
224 overlay.walk([&](Operation *op) {
225 op->removeAttr("aie.priority_route");
226 if (!isa<DeviceOp, EndOp, PacketRulesOp>(op))
227 marker.setAttr(op, builder.getUnitAttr());
228 });
229 return success();
230 }
231 void collectShimMM2SUses(ArrayRef<DeviceOp> devices) {
232 packetDemand.clear();
233 fixedCircuits.clear();
234 for (DeviceOp device : devices) {
235 ShimMM2SUses uses = shimMM2SUses(device, packetSwitched);
236 for (auto &[tile, channels] : uses.circuits)
237 if (std::optional<int> col = cast<TileLike>(tile).tryGetCol())
238 fixedCircuits[*col].insert(channels.begin(), channels.end());
239 for (auto &[at, ends] : uses.packetEnds)
240 if (std::optional<int> col = cast<TileLike>(at.first).tryGetCol())
241 packetDemand[{*col, at.second}] += ends.size();
242 for (auto &[at, flows] : uses.packetFlows)
243 if (std::optional<int> col = cast<TileLike>(at.first).tryGetCol())
244 packetDemand[{*col, at.second}] += flows;
245 }
246 }
247
248 static llvm::SmallSet<AIE::TileID, 4>
249 collectTokenIssuingTiles(ArrayRef<DeviceOp> devices) {
250 llvm::SmallSet<AIE::TileID, 4> tiles;
251 for (DeviceOp device : devices) {
252 for (TileOp tile : device.getOps<TileOp>())
253 if (tile.isShimNOCorPLTile())
254 tiles.insert(tile.getTileID());
255 SymbolTable symbols(device);
256 device.walk([&](Operation *op) {
257 bool issuesToken = false;
258 if (auto task = dyn_cast<AIEX::DMAConfigureTaskOp>(op))
259 issuesToken = bool(task.getIssueToken());
260 else if (auto task = dyn_cast<AIEX::DMAConfigureTaskForOp>(op))
261 issuesToken = bool(task.getIssueToken());
262 if (issuesToken)
263 if (auto tile = dyn_cast_or_null<TileOp>(
264 AIEX::getRuntimeDmaTile(op, symbols)))
265 tiles.insert(tile.getTileID());
266 if (auto push = dyn_cast<AIEX::NpuPushQueueOp>(op))
267 if (push.getIssueToken())
268 tiles.insert({static_cast<int>(push.getColumn()),
269 static_cast<int>(push.getRow())});
270 });
271 }
272 return tiles;
273 }
274
275 LogicalResult
276 applicationPorts(ModuleOp module,
277 std::vector<std::pair<TileID, Port>> &avoided) {
278 OwningOpRef<ModuleOp> trial = cast<ModuleOp>(module->clone());
279 for (DeviceOp device :
280 llvm::make_early_inc_range(trial->getOps<DeviceOp>())) {
281 if (device->hasAttr("aie.is_ctrl_pkt_overlay"))
282 device.erase();
283 else
284 device.removeOverlayAttr();
285 }
286 PassManager pm(&getContext());
287 auto &dpm = pm.nest<DeviceOp>();
288 dpm.addPass(createAIEAssignPacketIdsPass());
289 dpm.addPass(createAIEObjectFifoAllocatePass());
291 if (failed(pm.run(*trial))) {
292 module.emitError("failed to analyze application resources for adaptive "
293 "control overlay");
294 return failure();
295 }
296 std::set<std::pair<TileID, Port>> ports;
297 for (DeviceOp device : trial->getOps<DeviceOp>()) {
298 for (SwitchboxOp box : device.getOps<SwitchboxOp>()) {
299 for (ConnectOp connect : box.getOps<ConnectOp>())
300 ports.insert({box.getTileOp().getTileID(), connect.destPort()});
301 for (MasterSetOp master : box.getOps<MasterSetOp>())
302 ports.insert({box.getTileOp().getTileID(), master.destPort()});
303 }
304 DynamicTileAnalysis analysis;
305 llvm::Error error = analysis.runAnalysis(device);
306 if (error) {
307 device.emitOpError("cannot plan adaptive control overlay: ")
308 << llvm::toString(std::move(error));
309 return failure();
310 }
311 auto collect = [&](const SwitchSettings &settings) {
312 for (const auto &[tile, setting] : settings)
313 for (Port output : setting.dsts)
314 ports.insert({tile, output});
315 };
316 for (const auto &[_, settings] : analysis.routing.settings)
317 collect(settings);
318 for (const auto &[_, settings] : analysis.routing.idSettings)
319 collect(settings);
320 }
321 avoided.assign(ports.begin(), ports.end());
322 return success();
323 }
324};
325
326ShimMM2SUses AIE::shimMM2SUses(DeviceOp device, bool packetSwitched) {
327 ShimMM2SUses uses;
328 auto shim = [](Value tile) -> Operation * {
329 auto like = dyn_cast_or_null<TileLike>(tile.getDefiningOp());
330 return like && (like.getTileType() == AIETileType::ShimNOCTile ||
331 like.getTileType() == AIETileType::ShimPLTile)
332 ? like.getOperation()
333 : nullptr;
334 };
335 for (auto endpoint : device.getOps<RouteEndpointOp>()) {
336 Operation *tile = shim(endpoint.getTile());
337 std::optional<int> channel = endpoint.getChannelIndex();
338 if (!tile || endpoint.getBundle() != WireBundle::DMA ||
339 endpoint.getRouteDirection() != DMAChannelDir::MM2S || !channel)
340 continue;
341 bool packet = llvm::any_of(device.getOps<RouteOp>(), [&](RouteOp route) {
342 std::optional<PacketInfoAttr> header = route.getPacket();
343 return route.namesAsSource(endpoint.getSymName()) &&
344 isPacketSwitched(header.value_or(nullptr), packetSwitched);
345 });
346 if (packet)
347 uses.packetEnds[{tile, *channel}].push_back(endpoint.getSymNameAttr());
348 else
349 uses.circuits[tile].insert(*channel);
350 }
351 for (auto alloc : device.getOps<ShimDMAAllocationOp>())
352 if (alloc.getChannelDir() == DMAChannelDir::MM2S)
353 uses.circuits[alloc.getTile().getDefiningOp()].insert(
354 static_cast<int>(alloc.getChannelIndex()));
355 for (auto flow : device.getOps<FlowOp>())
356 if (flow.getSourceBundle() == WireBundle::DMA)
357 if (Operation *tile = shim(flow.getSource()))
358 uses.circuits[tile].insert(flow.getSourceChannel());
359 for (auto flow : device.getOps<PacketFlowOp>())
360 for (auto src : flow.getOps<PacketSourceOp>())
361 if (src.getBundle() == WireBundle::DMA)
362 if (Operation *tile = shim(src.getTile()))
363 ++uses.packetFlows[{tile, src.channelIndex()}];
364 for (auto mux : device.getOps<ShimMuxOp>())
365 for (auto connect : mux.getConnections().front().getOps<ConnectOp>())
366 if (connect.getSourceBundle() == WireBundle::DMA)
367 uses.circuits[mux.getTile().getDefiningOp()].insert(
368 connect.sourceIndex());
369 return uses;
370}
371
372bool AIE::getsColumnControlOverlay(DeviceOp device, bool configuration) {
373 return !AIEDialect::IsCtrlPktOverlayAttrHelper(device.getContext())
374 .isAttrPresent(device) &&
375 !device.getOverlayAttr() &&
376 device.getTargetModel().hasProperty(
378 (configuration ||
380 {device})
381 .empty());
382}
383
384std::unique_ptr<OperationPass<DeviceOp>> AIE::createAIEAssignTileCtrlIDsPass() {
385 return std::make_unique<AIEAssignTileCtrlIDsPass>();
386}
387
388std::unique_ptr<OperationPass<mlir::ModuleOp>>
390 return std::make_unique<AIEGenerateColumnControlOverlayPass>();
391}
392
393std::unique_ptr<OperationPass<mlir::ModuleOp>>
395 const AIEGenerateColumnControlOverlayOptions &options) {
396 return std::make_unique<AIEGenerateColumnControlOverlayPass>(options);
397}
llvm::Error runAnalysis(DeviceOp &device)
Operation * getRuntimeDmaTile(Operation *op, SymbolTable &symbols)
Definition AIEUtils.cpp:578
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEAssignTileCtrlIDsPass()
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEObjectFifoAllocatePass()
bool getsColumnControlOverlay(DeviceOp device, bool configuration)
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
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createAIEGenerateColumnControlOverlayPass()
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEAssignPacketIdsPass()
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::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEObjectFifoLowerDMAsPass()
std::map< TileID, SwitchSetting > SwitchSettings
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIEPathfinderPass()
ShimMM2SUses shimMM2SUses(DeviceOp device, bool packetSwitched)
DenseMap< std::pair< int, int >, unsigned > packetDemand
void collectShimMM2SUses(ArrayRef< DeviceOp > devices)
LogicalResult applicationPorts(ModuleOp module, std::vector< std::pair< TileID, Port > > &avoided)
LogicalResult generate(ArrayRef< DeviceOp > users)
static llvm::SmallSet< AIE::TileID, 4 > collectTokenIssuingTiles(ArrayRef< DeviceOp > devices)
DenseMap< int, llvm::SmallSet< int, 2 > > fixedCircuits
std::map< PathEndPoint, SwitchSettings > settings
std::map< std::pair< PathEndPoint, int >, SwitchSettings > idSettings
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).