MLIR-AIE 1.4.6.dev
AIEPlaceTiles.cpp
Go to the documentation of this file.
1//===- AIEPlaceTiles.cpp ----------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2026 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
13
14#include "mlir/AsmParser/AsmParser.h"
15#include "mlir/IR/Diagnostics.h"
16#include "mlir/IR/Location.h"
17#include "mlir/IR/PatternMatch.h"
18#include "mlir/Transforms/DialectConversion.h"
19
20#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/StringSet.h"
22
23#include <cmath>
24
25namespace xilinx::AIE {
26#define GEN_PASS_DEF_AIEPLACETILES
27#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
28} // namespace xilinx::AIE
29
30#define DEBUG_TYPE "aie-place-tiles"
31
32using namespace mlir;
33using namespace xilinx;
34using namespace xilinx::AIE;
35
36namespace {
37
38struct ConvertLogicalTileToTile : OpConversionPattern<LogicalTileOp> {
39 ConvertLogicalTileToTile(MLIRContext *context, DeviceOp &d, Placer &p,
40 PatternBenefit benefit = 1)
41 : OpConversionPattern(context, benefit), device(d), placer(p) {}
42
43 LogicalResult
44 matchAndRewrite(LogicalTileOp logicalTile, OpAdaptor adaptor,
45 ConversionPatternRewriter &rewriter) const override {
46 auto placement = placer.getPlacement(logicalTile);
47 if (!placement)
48 return logicalTile.emitError("no placement found for logical tile");
49
50 // Handle merging multiple logical tiles to same physical tile
51 TileOp tileOp = TileOp::getOrCreate(rewriter, device, placement->col,
52 placement->row, logicalTile.getLoc());
53 rewriter.modifyOpInPlace(tileOp, [&] {
54 tileOp->setLoc(
55 rewriter.getFusedLoc({tileOp.getLoc(), logicalTile.getLoc()}));
56 });
57
58 if (auto controllerId = logicalTile->getAttr("controller_id"))
59 tileOp->setAttr("controller_id", controllerId);
60
61 rewriter.replaceOp(logicalTile, tileOp.getResult());
62 return success();
63 }
64
65private:
66 DeviceOp device;
67 Placer &placer;
68};
69
70struct AIEPlaceTilesPass
71 : xilinx::AIE::impl::AIEPlaceTilesBase<AIEPlaceTilesPass> {
72
73 AIEPlaceTilesPass() = default;
74
75 AIEPlaceTilesPass(const AIEPlaceTilesOptions &options)
76 : AIEPlaceTilesBase(options) {}
77
78 // Parses the SA's charges once, before any device is placed. Names are
79 // symbol references, so any name can be written.
80 LogicalResult initialize(MLIRContext *context) override {
81 walked.clear();
82 coreSizes.clear();
83 auto parseSymbol = [&](StringRef text) {
84 ScopedDiagnosticHandler quiet(context,
85 [](Diagnostic &) { return success(); });
86 return dyn_cast_or_null<SymbolRefAttr>(parseAttribute(text, context));
87 };
88 for (StringRef entry : clSAWalkedFifos) {
89 SymbolRefAttr fifo = parseSymbol(entry);
90 if (!fifo || fifo.getNestedReferences().size() != 1)
91 return emitError(UnknownLoc::get(context), "sa-walked-fifos entry '")
92 << entry << "' is not @<device>::@<fifo>";
93 walked.push_back(
94 {fifo.getRootReference().str(), fifo.getLeafReference().str()});
95 }
96 for (StringRef entry : clSACoreDataSizes) {
97 auto [core, charge] = entry.rsplit('=');
98 auto [name, ordinal] = core.rsplit(':');
99 auto [bytes, alignment] = charge.split('@');
100 auto device = dyn_cast_or_null<FlatSymbolRefAttr>(parseSymbol(name));
101 unsigned index;
102 int64_t size;
103 int64_t align = 1;
104 if (!device || ordinal.getAsInteger(10, index) ||
105 bytes.getAsInteger(10, size) || size < 0 ||
106 (charge.contains('@') && (alignment.getAsInteger(10, align) ||
107 align <= 0 || !llvm::isPowerOf2_64(align))))
108 return emitError(UnknownLoc::get(context), "sa-core-data-sizes entry '")
109 << entry << "' is not @<device>:<core>=<bytes>[@<align>]";
110 coreSizes.push_back({device.getValue().str(), index, size, align});
111 }
112 return success();
113 }
114
115 // One device after another, in structural order: a device's placement
116 // may read the devices placed before it.
117 void runOnOperation() override {
118 SmallVector<DeviceOp> devices(getOperation().getOps<DeviceOp>());
119 if (devices.size() > 1) {
120 DenseMap<Operation *, unsigned> rank =
121 StructuralGraph::of(getOperation()).rank();
122 llvm::stable_sort(devices, [&](DeviceOp a, DeviceOp b) {
123 return rank.lookup(a) < rank.lookup(b);
124 });
125 }
126 residents.clear();
127 placedCores.clear();
128 programKeys.clear();
129 // Configurations the host loads one right after the other.
130 consecutive.clear();
131 ModuleOp module = getOperation();
132 module.walk([&](RuntimeSequenceOp sequence) {
133 Operation *previous = nullptr;
134 sequence.walk([&](Operation *op) {
135 FlatSymbolRefAttr loaded;
136 if (auto configure = dyn_cast<AIEX::ConfigureOp>(op))
137 loaded = configure.getSymbolAttr();
138 else if (auto load = dyn_cast<AIEX::NpuLoadPdiOp>(op))
139 loaded = load.getDeviceRefAttr();
140 Operation *device =
141 loaded ? SymbolTable::lookupSymbolIn(module, loaded) : nullptr;
142 if (!isa_and_nonnull<DeviceOp>(device))
143 return;
144 if (previous && previous != device) {
145 consecutive[previous].insert(device);
146 consecutive[device].insert(previous);
147 }
148 previous = device;
149 });
150 });
151 for (DeviceOp device : devices)
152 if (failed(pinResidents(device)) || failed(placeDevice(device)) ||
153 failed(recordPlaced(device)))
154 return signalPassFailure();
155 }
156
157 // Pins each resident buffer to the tile and address an earlier device gave
158 // its global; an address only this buffer has is pinned on the earlier one.
159 LogicalResult pinResidents(DeviceOp device) {
160 for (BufferOp buffer : device.getOps<BufferOp>()) {
161 FlatSymbolRefAttr global = buffer.getResidentAttr();
162 auto first = global ? residents.find(global.getAttr()) : residents.end();
163 if (first == residents.end())
164 continue;
165 auto [twin, at] = first->second;
166 Operation *tile = buffer.getTile().getDefiningOp();
167 auto like = cast<TileLike>(tile);
168 AIETileType there = device.getTargetModel().getTileType(at.col, at.row);
169 if (like.getTileType() != there) {
170 InFlightDiagnostic diag =
171 buffer.emitError("buffer resident for ")
172 << global << " is on a " << stringifyAIETileType(like.getTileType())
173 << ", but the earlier buffer resident for it is on a "
174 << stringifyAIETileType(there) << " at (" << at.col << ", "
175 << at.row << ")";
176 diag.attachNote(twin.getLoc()) << "the earlier buffer";
177 return diag;
178 }
179 std::optional<int> col = like.tryGetCol(), row = like.tryGetRow();
180 if ((col && *col != at.col) || (row && *row != at.row)) {
181 InFlightDiagnostic diag =
182 buffer.emitError("buffer resident for ")
183 << global << " is on a tile pinned away from (" << at.col << ", "
184 << at.row << "), where the earlier buffer resident for it is";
185 diag.attachNote(twin.getLoc()) << "the earlier buffer";
186 return diag;
187 }
188 if (auto logical = dyn_cast<LogicalTileOp>(tile)) {
189 logical.setCol(at.col);
190 logical.setRow(at.row);
191 }
192 auto address = twin.getAddress(), own = buffer.getAddress();
193 if (address && own && *own != *address) {
194 InFlightDiagnostic diag =
195 buffer.emitError("buffer resident for ")
196 << global << " has address " << *own
197 << ", but the earlier buffer resident for it has " << *address;
198 diag.attachNote(twin.getLoc()) << "the earlier buffer";
199 return diag;
200 }
201 if (address)
202 buffer.setAddress(address);
203 else if (own)
204 twin.setAddress(own);
205 else
206 buffer.emitWarning("buffer resident for ")
207 << global
208 << " has no address, so its data may move between "
209 "configurations; give it and its earlier twin one";
210 }
211 return success();
212 }
213
214 // Records where `device`, now placed, keeps its resident buffers and its
215 // cores' programs.
216 LogicalResult recordPlaced(DeviceOp device) {
217 for (BufferOp buffer : device.getOps<BufferOp>())
218 if (FlatSymbolRefAttr global = buffer.getResidentAttr())
219 if (auto tile = dyn_cast<TileOp>(buffer.getTile().getDefiningOp()))
220 residents.try_emplace(global.getAttr(), buffer, tile.getTileID());
221 DenseMap<TileID, unsigned> &cores = placedCores[device];
222 for (auto [tile, key] : programKeysOf(device))
223 if (auto placed = dyn_cast<TileOp>(tile))
224 cores[placed.getTileID()] = key;
225 return success();
226 }
227
228 // Each tile op holding a core, keyed by its core's structure, names and
229 // symbols left out, so equal programs key alike across devices.
230 DenseMap<Operation *, unsigned> programKeysOf(DeviceOp device) {
231 StructuralGraph graph = StructuralGraph::of(device);
232 DenseMap<Operation *, unsigned> keys;
233 for (CoreOp core : device.getOps<CoreOp>()) {
234 auto [entry, added] = programKeys.try_emplace(graph.features.lookup(core),
235 programKeys.size() + 1);
236 keys[core.getTile().getDefiningOp()] = entry->second;
237 }
238 return keys;
239 }
240
241 LogicalResult placeDevice(DeviceOp device) {
242
243 // Create placer
244 std::shared_ptr<Placer> placer;
245 std::shared_ptr<SAPlacer> saPlacer;
246 switch (clPlacerType) {
248 std::optional<int> coresPerCol = std::nullopt;
249 if (clCoresPerCol >= 0)
250 coresPerCol = clCoresPerCol;
251 placer = std::make_shared<SequentialPlacer>(
252 coresPerCol, clMergeLogicalTiles, clSpreadUnanchoredTiles);
253 break;
254 }
256 double effort = clSAEffort.getValue();
257 if (!std::isfinite(effort) || effort <= 0) {
258 device.emitError("sa-effort must be positive and finite, got ")
259 << effort;
260 return failure();
261 }
262 int64_t budget = clPlacementBudget;
263 if (budget <= 0) {
264 device.emitError("placement-budget must be positive, got ") << budget;
265 return failure();
266 }
267 SAConfig config;
268 config.effort = effort;
269 config.placementBudget = budget;
270 // A device another device's entry names is reported once, by the
271 // module's first device; each device checks the entries naming it.
272 auto module = device->getParentOfType<ModuleOp>();
273 if (module && *module.getOps<DeviceOp>().begin() == device) {
274 llvm::StringSet<> devices;
275 for (DeviceOp other : module.getOps<DeviceOp>())
276 devices.insert(other.getSymName());
277 llvm::SmallVector<StringRef> named;
278 for (auto &[name, fifo] : walked)
279 named.push_back(name);
280 for (auto &[name, index, size, align] : coreSizes)
281 named.push_back(name);
282 for (StringRef name : named)
283 if (!devices.contains(name)) {
284 device.emitError(
285 "sa-walked-fifos or sa-core-data-sizes names no device @")
286 << name;
287 return failure();
288 }
289 }
290 for (auto &[name, fifo] : walked) {
291 if (name != device.getSymName())
292 continue;
293 if (!isa_and_nonnull<ObjectFifoCreateOp>(
294 SymbolTable::lookupSymbolIn(device, fifo))) {
295 device.emitError("sa-walked-fifos names no objectFIFO @")
296 << fifo << " in @" << name;
297 return failure();
298 }
299 config.walkedFifos.push_back(fifo);
300 }
301 size_t cores = llvm::range_size(device.getOps<CoreOp>());
302 for (auto &[name, index, size, align] : coreSizes) {
303 if (name != device.getSymName())
304 continue;
305 if (index >= cores) {
306 device.emitError("sa-core-data-sizes names core ")
307 << index << " of @" << name << ", which has " << cores
308 << " cores";
309 return failure();
310 }
311 config.coreDataCharges.insert_or_assign(
312 index, SAConfig::CoreDataCharge(size, align));
313 }
314 placer = saPlacer = std::make_shared<SAPlacer>(clSASeed, config);
315 break;
316 }
317 }
318
319 placer->initialize(
320 device, clCtrlPktOverlay && getsColumnControlOverlay(device, true),
321 clCtrlPktOverlayAdaptive, clPacketSwObjectFifos);
322 SmallVector<DenseMap<TileID, unsigned>> neighbours;
323 if (auto adjacent = consecutive.find(device); adjacent != consecutive.end())
324 for (Operation *other : adjacent->second)
325 if (auto cores = placedCores.find(other); cores != placedCores.end())
326 neighbours.push_back(cores->second);
327 if (!neighbours.empty())
328 placer->setNeighbourCores(programKeysOf(device), std::move(neighbours));
329 if (failed(placer->place(device)))
330 return failure();
331 if (saPlacer) {
332 saFinalCost += saPlacer->getFinalCost();
333 saLockGroups += saPlacer->getFinalLockGroups();
334 saLockPenalty += saPlacer->getFinalLockPenalty();
335 saLockStopped += saPlacer->getFinalLockStopped();
336 }
337
338 ConversionTarget target(getContext());
339 target.addLegalOp<TileOp>();
340 target.addIllegalOp<LogicalTileOp>();
341 target.addLegalDialect<AIEDialect>();
342
343 RewritePatternSet patterns(&getContext());
344 patterns.add<ConvertLogicalTileToTile>(device.getContext(), device,
345 *placer);
346
347 if (failed(applyPartialConversion(device, target, std::move(patterns))))
348 return failure();
349
350 // Generate objectfifo.allocate ops right after their respective objectfifo
351 auto &allocs = placer->getAllocates();
352 if (!allocs.empty()) {
353 // Deduplicate: only emit one allocate per fifo
354 llvm::DenseSet<Operation *> emitted;
355 for (auto &[fifoOp, delegateTileID] : allocs) {
356 if (emitted.count(fifoOp))
357 continue;
358 emitted.insert(fifoOp);
359
360 auto ofOp = dyn_cast<ObjectFifoCreateOp>(fifoOp);
361 if (!ofOp)
362 continue;
363
364 // Insert right after the objectfifo op
365 OpBuilder builder(ofOp->getContext());
366 builder.setInsertionPointAfter(ofOp);
367
368 TileOp delegateTile =
369 TileOp::getOrCreate(builder, device, delegateTileID.col,
370 delegateTileID.row, ofOp.getLoc());
371 ObjectFifoAllocateOp::create(
372 builder, ofOp.getLoc(),
373 SymbolRefAttr::get(builder.getContext(), ofOp.getSymName()),
374 delegateTile.getResult());
375 }
376 }
377 return success();
378 }
379
380private:
381 // The first buffer placed resident for each memref.global, and its tile.
382 DenseMap<StringAttr, std::pair<BufferOp, TileID>> residents;
383 // Each placed device's cores, by position, and the program key each runs.
384 DenseMap<Operation *, DenseMap<TileID, unsigned>> placedCores;
385 llvm::StringMap<unsigned> programKeys;
386 DenseMap<Operation *, llvm::SetVector<Operation *>> consecutive;
387 // sa-walked-fifos, as (device, fifo).
388 llvm::SmallVector<std::pair<std::string, std::string>> walked;
389 // sa-core-data-sizes, as (device, core, bytes, alignment).
390 llvm::SmallVector<std::tuple<std::string, unsigned, int64_t, int64_t>>
391 coreSizes;
392};
393
394} // namespace
395
396std::unique_ptr<OperationPass<ModuleOp>> AIE::createAIEPlaceTilesPass() {
397 return std::make_unique<AIEPlaceTilesPass>();
398}
399
400std::unique_ptr<OperationPass<ModuleOp>>
401AIE::createAIEPlaceTilesPass(const AIEPlaceTilesOptions &options) {
402 return std::make_unique<AIEPlaceTilesPass>(options);
403}
bool getsColumnControlOverlay(DeviceOp device, bool configuration)
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createAIEPlaceTilesPass()
llvm::DenseMap< unsigned, CoreDataCharge > coreDataCharges
Definition AIEPlacer.h:1085
llvm::SmallVector< std::string > walkedFifos
Definition AIEPlacer.h:1074
Ops and the labelled links between them, for ranking them by structure where a pass would otherwise f...
llvm::DenseMap< mlir::Operation *, unsigned > rank() const
Rank the nodes by their features and then, a round at a time until no class splits,...
llvm::DenseMap< mlir::Operation *, std::string > features
What each node is, before anything it links to is considered.
static StructuralGraph of(DeviceOp device)
The ops directly in device but its runtime sequences, each with the ops nested in it,...