MLIR-AIE 1.4.6.dev
AIEMaterializeBDChains.cpp
Go to the documentation of this file.
1//===- AIEMaterializeBDChains.cpp -------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2024 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
11
12#include "mlir/Analysis/CallGraph.h"
13#include "mlir/IR/IRMapping.h"
14#include "mlir/IR/PatternMatch.h"
15#include "mlir/IR/SymbolTable.h"
16#include "mlir/Pass/Pass.h"
17#include "mlir/Pass/PassManager.h"
18#include "mlir/Transforms/DialectConversion.h"
19#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
20
21namespace xilinx::AIEX {
22#define GEN_PASS_DEF_AIEMATERIALIZEBDCHAINS
23#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
24} // namespace xilinx::AIEX
25
26using namespace mlir;
27using namespace xilinx;
28using namespace xilinx::AIEX;
29
30// Concretizes `chain` with `args` as a dma_configure_task on `tile`'s
31// channel, starts it, and replaces `start` with the task.
32static void inlineBDChain(PatternRewriter &rewriter, Operation *start,
33 AIE::BDChainOp chain, ValueRange args, Value tile,
34 AIE::DMAChannelDir direction, int32_t channel,
35 bool issueToken, int32_t repeatCount,
36 FlatSymbolRefAttr alloc) {
37 rewriter.setInsertionPointAfter(start);
38 DMAConfigureTaskOp configure_op = DMAConfigureTaskOp::create(
39 rewriter, start->getLoc(), rewriter.getIndexType(), tile, direction,
40 channel, issueToken, repeatCount);
41 configure_op.setAllocAttr(alloc);
42 Region &source_region = chain.getBody();
43 Region &target_region = configure_op.getBody();
44
45 // Clone BD definition into usage site, replacing abstract SSA values with
46 // concrete ones
47 IRMapping arg_map;
48 for (unsigned i = 0, n = source_region.getNumArguments(); i < n; i++) {
49 BlockArgument arg = source_region.getArgument(i);
50 Value val = args[i];
51 assert(arg.getType() == val.getType());
52 arg_map.map(arg, val);
53 }
54 source_region.cloneInto(&target_region, arg_map);
55
56 rewriter.replaceAllUsesWith(start->getResult(0), configure_op.getResult());
57 DMAStartTaskOp::create(rewriter, start->getLoc(), configure_op.getResult());
58 rewriter.eraseOp(start);
59}
60
61struct DMAStartBdChainForOpPattern : RewritePattern {
62
63 // Symbol-lookup cache used for speedier `ShimDMAAllocationOp` lookups
64 const mlir::SymbolTable &symbolTable;
65
67 const mlir::SymbolTable &symbolTable)
68 : RewritePattern(DMAStartBdChainForOp::getOperationName(),
69 PatternBenefit(1), ctx),
71
72 LogicalResult matchAndRewrite(Operation *op_any,
73 PatternRewriter &rewriter) const override {
74 DMAStartBdChainForOp op = llvm::dyn_cast<DMAStartBdChainForOp>(op_any);
75 if (!op) {
76 return failure();
77 }
78
79 AIE::ShimDMAAllocationOp alloc_op =
80 symbolTable.lookup<AIE::ShimDMAAllocationOp>(op.getAlloc());
81 if (!alloc_op) {
82 return op.emitOpError("no shim DMA allocation found for symbol");
83 }
84
85 AIE::TileOp tile = alloc_op.getTileOp();
86 if (!tile) {
87 return op.emitOpError(
88 "shim DMA allocation must reference a valid TileOp");
89 }
90
91 auto chain = symbolTable.lookup<AIE::BDChainOp>(op.getSymbol());
92 if (!chain) {
93 return op.emitOpError("symbol does not reference valid BD chain");
94 }
95
96 inlineBDChain(rewriter, op, chain, op.getArgs(), tile.getResult(),
97 alloc_op.getChannelDir(), (int32_t)alloc_op.getChannelIndex(),
98 op.getIssueToken(), op.getRepeatCount(), op.getAllocAttr());
99 return success();
100 }
101};
102
103struct DMAInlineBDChainPattern : RewritePattern {
104
105 DMAInlineBDChainPattern(MLIRContext *ctx)
106 : RewritePattern(DMAStartBdChainOp::getOperationName(), PatternBenefit(1),
107 ctx) {}
108
109 LogicalResult matchAndRewrite(Operation *op,
110 PatternRewriter &rewriter) const override {
111 DMAStartBdChainOp start_op = llvm::dyn_cast<DMAStartBdChainOp>(op);
112 if (!start_op) { // Not a match.
113 return failure();
114 }
115 AIE::BDChainOp chain_def = start_op.getBDChainOp();
116 assert(chain_def);
117 inlineBDChain(rewriter, start_op, chain_def, start_op.getArgs(),
118 start_op.getTile(), start_op.getDirection(),
119 start_op.getChannel(), start_op.getIssueToken(),
120 start_op.getRepeatCount(), FlatSymbolRefAttr());
121 return success();
122 }
123};
124
127 AIEMaterializeBDChainsPass> {
128
129 void runOnOperation() override {
130 MLIRContext *ctx = &getContext();
131 AIE::DeviceOp device = getOperation();
132 GreedyRewriteConfig rewriter_config = GreedyRewriteConfig();
133 rewriter_config.setRegionSimplificationLevel(
134 GreedySimplifyRegionLevel::Disabled);
135
136 mlir::SymbolTable symbolTable(device);
137
138 RewritePatternSet patterns_0(ctx);
139 patterns_0.insert<DMAStartBdChainForOpPattern>(ctx, symbolTable);
140 DMAConfigureTaskOp::getCanonicalizationPatterns(patterns_0, ctx);
141 if (failed(applyPatternsGreedily(device, std::move(patterns_0),
142 rewriter_config))) {
143 signalPassFailure();
144 }
145
146 RewritePatternSet patterns_1(ctx);
147 patterns_1.insert<DMAInlineBDChainPattern>(ctx);
148 rewriter_config.setRegionSimplificationLevel(
149 GreedySimplifyRegionLevel::Disabled);
150 DMAConfigureTaskOp::getCanonicalizationPatterns(patterns_1, ctx);
151 if (failed(applyPatternsGreedily(device, std::move(patterns_1),
152 rewriter_config))) {
153 signalPassFailure();
154 }
155 }
156};
157
158std::unique_ptr<OperationPass<AIE::DeviceOp>>
160 return std::make_unique<AIEMaterializeBDChainsPass>();
161}
std::unique_ptr< mlir::OperationPass< AIE::DeviceOp > > createAIEMaterializeBDChainsPass()
LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const override
DMAInlineBDChainPattern(MLIRContext *ctx)
LogicalResult matchAndRewrite(Operation *op_any, PatternRewriter &rewriter) const override
DMAStartBdChainForOpPattern(MLIRContext *ctx, const mlir::SymbolTable &symbolTable)
const mlir::SymbolTable & symbolTable