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"
20#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/StringSet.h"
26#define GEN_PASS_DEF_AIEPLACETILES
27#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
30#define DEBUG_TYPE "aie-place-tiles"
38struct ConvertLogicalTileToTile : OpConversionPattern<LogicalTileOp> {
39 ConvertLogicalTileToTile(MLIRContext *context, DeviceOp &d,
Placer &p,
40 PatternBenefit benefit = 1)
41 : OpConversionPattern(context, benefit), device(d), placer(p) {}
44 matchAndRewrite(LogicalTileOp logicalTile, OpAdaptor adaptor,
45 ConversionPatternRewriter &rewriter)
const override {
46 auto placement = placer.getPlacement(logicalTile);
48 return logicalTile.emitError(
"no placement found for logical tile");
51 TileOp tileOp = TileOp::getOrCreate(rewriter, device, placement->col,
52 placement->row, logicalTile.getLoc());
53 rewriter.modifyOpInPlace(tileOp, [&] {
55 rewriter.getFusedLoc({tileOp.getLoc(), logicalTile.getLoc()}));
58 if (
auto controllerId = logicalTile->getAttr(
"controller_id"))
59 tileOp->setAttr(
"controller_id", controllerId);
61 rewriter.replaceOp(logicalTile, tileOp.getResult());
70struct AIEPlaceTilesPass
71 : xilinx::AIE::impl::AIEPlaceTilesBase<AIEPlaceTilesPass> {
73 AIEPlaceTilesPass() =
default;
75 AIEPlaceTilesPass(
const AIEPlaceTilesOptions &options)
76 : AIEPlaceTilesBase(options) {}
80 LogicalResult initialize(MLIRContext *context)
override {
83 auto parseSymbol = [&](StringRef text) {
84 ScopedDiagnosticHandler quiet(context,
85 [](Diagnostic &) {
return success(); });
86 return dyn_cast_or_null<SymbolRefAttr>(parseAttribute(text, context));
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>";
94 {fifo.getRootReference().str(), fifo.getLeafReference().str()});
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));
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});
117 void runOnOperation()
override {
118 SmallVector<DeviceOp> devices(getOperation().getOps<DeviceOp>());
119 if (devices.size() > 1) {
120 DenseMap<Operation *, unsigned> rank =
122 llvm::stable_sort(devices, [&](DeviceOp a, DeviceOp b) {
123 return rank.lookup(a) < rank.lookup(b);
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();
141 loaded ? SymbolTable::lookupSymbolIn(module, loaded) :
nullptr;
142 if (!isa_and_nonnull<DeviceOp>(device))
144 if (previous && previous != device) {
145 consecutive[previous].insert(device);
146 consecutive[device].insert(previous);
151 for (DeviceOp device : devices)
152 if (failed(pinResidents(device)) || failed(placeDevice(device)) ||
153 failed(recordPlaced(device)))
154 return signalPassFailure();
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())
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 <<
", "
176 diag.attachNote(twin.getLoc()) <<
"the earlier buffer";
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";
188 if (
auto logical = dyn_cast<LogicalTileOp>(tile)) {
189 logical.setCol(at.col);
190 logical.setRow(at.row);
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";
202 buffer.setAddress(address);
204 twin.setAddress(own);
206 buffer.emitWarning(
"buffer resident for ")
208 <<
" has no address, so its data may move between "
209 "configurations; give it and its earlier twin one";
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;
230 DenseMap<Operation *, unsigned> programKeysOf(DeviceOp 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;
241 LogicalResult placeDevice(DeviceOp device) {
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);
256 double effort = clSAEffort.getValue();
257 if (!std::isfinite(effort) || effort <= 0) {
258 device.emitError(
"sa-effort must be positive and finite, got ")
262 int64_t budget = clPlacementBudget;
264 device.emitError(
"placement-budget must be positive, got ") << budget;
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)) {
285 "sa-walked-fifos or sa-core-data-sizes names no device @")
290 for (
auto &[name, fifo] : walked) {
291 if (name != device.getSymName())
293 if (!isa_and_nonnull<ObjectFifoCreateOp>(
294 SymbolTable::lookupSymbolIn(device, fifo))) {
295 device.emitError(
"sa-walked-fifos names no objectFIFO @")
296 << fifo <<
" in @" << name;
301 size_t cores = llvm::range_size(device.getOps<CoreOp>());
302 for (
auto &[name, index, size, align] : coreSizes) {
303 if (name != device.getSymName())
305 if (index >= cores) {
306 device.emitError(
"sa-core-data-sizes names core ")
307 << index <<
" of @" << name <<
", which has " << cores
314 placer = saPlacer = std::make_shared<SAPlacer>(clSASeed, config);
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)))
332 saFinalCost += saPlacer->getFinalCost();
333 saLockGroups += saPlacer->getFinalLockGroups();
334 saLockPenalty += saPlacer->getFinalLockPenalty();
335 saLockStopped += saPlacer->getFinalLockStopped();
338 ConversionTarget target(getContext());
339 target.addLegalOp<TileOp>();
340 target.addIllegalOp<LogicalTileOp>();
341 target.addLegalDialect<AIEDialect>();
343 RewritePatternSet patterns(&getContext());
344 patterns.add<ConvertLogicalTileToTile>(device.getContext(), device,
347 if (failed(applyPartialConversion(device, target, std::move(patterns))))
351 auto &allocs = placer->getAllocates();
352 if (!allocs.empty()) {
354 llvm::DenseSet<Operation *> emitted;
355 for (
auto &[fifoOp, delegateTileID] : allocs) {
356 if (emitted.count(fifoOp))
358 emitted.insert(fifoOp);
360 auto ofOp = dyn_cast<ObjectFifoCreateOp>(fifoOp);
365 OpBuilder builder(ofOp->getContext());
366 builder.setInsertionPointAfter(ofOp);
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());
382 DenseMap<StringAttr, std::pair<BufferOp, TileID>> residents;
384 DenseMap<Operation *, DenseMap<TileID, unsigned>> placedCores;
385 llvm::StringMap<unsigned> programKeys;
386 DenseMap<Operation *, llvm::SetVector<Operation *>> consecutive;
388 llvm::SmallVector<std::pair<std::string, std::string>> walked;
390 llvm::SmallVector<std::tuple<std::string, unsigned, int64_t, int64_t>>
397 return std::make_unique<AIEPlaceTilesPass>();
400std::unique_ptr<OperationPass<ModuleOp>>
402 return std::make_unique<AIEPlaceTilesPass>(options);
bool getsColumnControlOverlay(DeviceOp device, bool configuration)
std::unique_ptr< mlir::OperationPass< mlir::ModuleOp > > createAIEPlaceTilesPass()
llvm::DenseMap< unsigned, CoreDataCharge > coreDataCharges
llvm::SmallVector< std::string > walkedFifos
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,...