MLIR-AIE
AIESubstituteShimDMAAllocations.cpp
Go to the documentation of this file.
1//===- AIESubstituteShimDMAAllocations.cpp -----------------------*- C++
2//-*-===//
3//
4// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8// (c) Copyright 2024 Advanced Micro Devices, Inc.
9//
10//===----------------------------------------------------------------------===//
11
12#include <algorithm>
13#include <iterator>
14
18
19#include "mlir/Pass/Pass.h"
20#include "mlir/Transforms/DialectConversion.h"
21#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
22#include "llvm/ADT/TypeSwitch.h"
23
24using namespace mlir;
25using namespace xilinx;
26using namespace xilinx::AIEX;
27
28namespace {
29
30struct DMAConfigureTaskForOpPattern
31 : public mlir::OpRewritePattern<DMAConfigureTaskForOp> {
32
33 using OpRewritePattern::OpRewritePattern;
34
35 LogicalResult matchAndRewrite(DMAConfigureTaskForOp op,
36 PatternRewriter &rewriter) const override {
37 AIE::DeviceOp device = op->getParentOfType<AIE::DeviceOp>();
38
39 AIE::ShimDMAAllocationOp alloc_op =
40 AIE::ShimDMAAllocationOp::getForSymbol(device, op.getAlloc());
41 if (!alloc_op) {
42 return op.emitOpError("no shim DMA allocation found for symbol");
43 }
44
45 const int col = alloc_op.getCol();
46 AIE::TileOp tile = AIE::TileOp::getOrCreate(rewriter, device, col, 0);
47 DMAConfigureTaskOp new_op = rewriter.create<DMAConfigureTaskOp>(
48 op.getLoc(), rewriter.getIndexType(), tile.getResult(),
49 alloc_op.getChannelDir(), (int32_t)alloc_op.getChannelIndex(),
50 op.getIssueToken(), op.getRepeatCount());
51 rewriter.replaceAllUsesWith(op.getResult(), new_op.getResult());
52 rewriter.inlineRegionBefore(op.getBody(), new_op.getBody(),
53 new_op.getBody().begin());
54 rewriter.eraseOp(op);
55 return success();
56 }
57};
58
59struct AIESubstituteShimDMAAllocationsPass
60 : AIESubstituteShimDMAAllocationsBase<AIESubstituteShimDMAAllocationsPass> {
61
62 void runOnOperation() override {
63 AIE::DeviceOp device = getOperation();
64
65 // Convert DMAConfigureTaskForOps that reference shim DMA allocations
66 // to regular DMAConfigureTaskOps
67 RewritePatternSet patterns(&getContext());
68 patterns.insert<DMAConfigureTaskForOpPattern>(&getContext());
69
70 (void)applyPatternsGreedily(device, std::move(patterns));
71 }
72};
73
74} // namespace
75
76std::unique_ptr<OperationPass<AIE::DeviceOp>>
78 return std::make_unique<AIESubstituteShimDMAAllocationsPass>();
79}
std::unique_ptr< mlir::OperationPass< AIE::DeviceOp > > createAIESubstituteShimDMAAllocationsPass()