MLIR-AIE 1.4.6.dev
AIELowerCascadeFlows.cpp
Go to the documentation of this file.
1//===- AIELowerCascadeFlows.cpp ---------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2022 Xilinx, Inc.
4// Copyright (C) 2022-2026 Advanced Micro Devices, Inc.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11
12#include "mlir/IR/Attributes.h"
13#include "mlir/Pass/Pass.h"
14
15#include "llvm/ADT/Twine.h"
16
17namespace xilinx::AIE {
18#define GEN_PASS_DEF_AIELOWERCASCADEFLOWS
19#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
20} // namespace xilinx::AIE
21
22#define DEBUG_TYPE "aie-lower-cascade-flows"
23
24using namespace mlir;
25using namespace xilinx;
26using namespace xilinx::AIE;
27
29 : xilinx::AIE::impl::AIELowerCascadeFlowsBase<AIELowerCascadeFlowsPass> {
30 void getDependentDialects(DialectRegistry &registry) const override {
31 registry.insert<AIEDialect>();
32 }
33 void runOnOperation() override {
34 DeviceOp device = getOperation();
35 const auto &targetModel = device.getTargetModel();
36 OpBuilder builder = OpBuilder::atBlockTerminator(device.getBody());
37
38 DenseSet<TileOp> tilesWithCascadeFlow;
39 DenseMap<TileOp, WireBundle> cascadeInputsPerTile;
40 DenseMap<TileOp, WireBundle> cascadeOutputsPerTile;
41
42 // identify cascade_flows and what ports they use on each tile
43 for (auto cascadeFlow : device.getOps<CascadeFlowOp>()) {
44 // for each cascade flow
45 TileOp src = cascadeFlow.getSourceTileOp();
46 TileOp dst = cascadeFlow.getDestTileOp();
47 tilesWithCascadeFlow.insert(src);
48 tilesWithCascadeFlow.insert(dst);
49
50 // CascadeFlowOp::verify already refuses a step isLegalCascade rejects,
51 // so every flow the pass sees here is one of the two it handles below.
52 if (targetModel.isSouth(src.getCol(), src.getRow(), dst.getCol(),
53 dst.getRow())) {
54 cascadeInputsPerTile[dst] = WireBundle::North;
55 cascadeOutputsPerTile[src] = WireBundle::South;
56 } else {
57 cascadeInputsPerTile[dst] = WireBundle::West;
58 cascadeOutputsPerTile[src] = WireBundle::East;
59 }
60 }
61
62 // generate configure cascade ops
63 for (TileOp tile : device.getOps<TileOp>()) {
64 if (!tilesWithCascadeFlow.contains(tile))
65 continue;
66 WireBundle inputDir;
67 if (cascadeInputsPerTile.find(tile) != cascadeInputsPerTile.end()) {
68 inputDir = cascadeInputsPerTile[tile];
69 } else {
70 inputDir = WireBundle::North;
71 }
72 WireBundle outputDir;
73 if (cascadeOutputsPerTile.find(tile) != cascadeOutputsPerTile.end()) {
74 outputDir = cascadeOutputsPerTile[tile];
75 } else {
76 outputDir = WireBundle::South;
77 }
78 ConfigureCascadeOp::create(builder, tile.getLoc(), tile,
79 static_cast<CascadeDir>(inputDir),
80 static_cast<CascadeDir>(outputDir));
81 }
82
83 // erase CascadeFlowOps
84 SetVector<Operation *> opsToErase;
85 device.walk([&](Operation *op) {
86 if (isa<CascadeFlowOp>(op))
87 opsToErase.insert(op);
88 });
89 IRRewriter rewriter(&getContext());
90 for (auto it = opsToErase.rbegin(); it != opsToErase.rend(); ++it)
91 (*it)->erase();
92 }
93};
94
95std::unique_ptr<OperationPass<DeviceOp>> AIE::createAIELowerCascadeFlowsPass() {
96 return std::make_unique<AIELowerCascadeFlowsPass>();
97}
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIELowerCascadeFlowsPass()
void getDependentDialects(DialectRegistry &registry) const override