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"
22#define GEN_PASS_DEF_AIEMATERIALIZEBDCHAINS
23#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
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();
48 for (
unsigned i = 0, n = source_region.getNumArguments(); i < n; i++) {
49 BlockArgument arg = source_region.getArgument(i);
51 assert(arg.getType() == val.getType());
52 arg_map.map(arg, val);
54 source_region.cloneInto(&target_region, arg_map);
56 rewriter.replaceAllUsesWith(start->getResult(0), configure_op.getResult());
57 DMAStartTaskOp::create(rewriter, start->getLoc(), configure_op.getResult());
58 rewriter.eraseOp(start);
68 : RewritePattern(DMAStartBdChainForOp::getOperationName(),
69 PatternBenefit(1), ctx),
73 PatternRewriter &rewriter)
const override {
74 DMAStartBdChainForOp op = llvm::dyn_cast<DMAStartBdChainForOp>(op_any);
79 AIE::ShimDMAAllocationOp alloc_op =
80 symbolTable.lookup<AIE::ShimDMAAllocationOp>(op.getAlloc());
82 return op.emitOpError(
"no shim DMA allocation found for symbol");
85 AIE::TileOp tile = alloc_op.getTileOp();
87 return op.emitOpError(
88 "shim DMA allocation must reference a valid TileOp");
91 auto chain =
symbolTable.lookup<AIE::BDChainOp>(op.getSymbol());
93 return op.emitOpError(
"symbol does not reference valid BD chain");
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());
106 : RewritePattern(DMAStartBdChainOp::getOperationName(), PatternBenefit(1),
110 PatternRewriter &rewriter)
const override {
111 DMAStartBdChainOp start_op = llvm::dyn_cast<DMAStartBdChainOp>(op);
115 AIE::BDChainOp chain_def = start_op.getBDChainOp();
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());
127 AIEMaterializeBDChainsPass> {
130 MLIRContext *ctx = &getContext();
131 AIE::DeviceOp device = getOperation();
132 GreedyRewriteConfig rewriter_config = GreedyRewriteConfig();
133 rewriter_config.setRegionSimplificationLevel(
134 GreedySimplifyRegionLevel::Disabled);
136 mlir::SymbolTable symbolTable(device);
138 RewritePatternSet patterns_0(ctx);
140 DMAConfigureTaskOp::getCanonicalizationPatterns(patterns_0, ctx);
141 if (failed(applyPatternsGreedily(device, std::move(patterns_0),
146 RewritePatternSet patterns_1(ctx);
148 rewriter_config.setRegionSimplificationLevel(
149 GreedySimplifyRegionLevel::Disabled);
150 DMAConfigureTaskOp::getCanonicalizationPatterns(patterns_1, ctx);
151 if (failed(applyPatternsGreedily(device, std::move(patterns_1),
158std::unique_ptr<OperationPass<AIE::DeviceOp>>
160 return std::make_unique<AIEMaterializeBDChainsPass>();
std::unique_ptr< mlir::OperationPass< AIE::DeviceOp > > createAIEMaterializeBDChainsPass()
void runOnOperation() override
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