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
24namespace xilinx::AIEX {
25#define GEN_PASS_DEF_AIESUBSTITUTESHIMDMAALLOCATIONS
26#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
27} // namespace xilinx::AIEX
28
29using namespace mlir;
30using namespace xilinx;
31using namespace xilinx::AIEX;
32
33namespace {
34
35struct DMAConfigureTaskForOpPattern
36 : public mlir::OpRewritePattern<DMAConfigureTaskForOp> {
37
38 using OpRewritePattern::OpRewritePattern;
39
40 LogicalResult matchAndRewrite(DMAConfigureTaskForOp op,
41 PatternRewriter &rewriter) const override {
42 AIE::DeviceOp device = op->getParentOfType<AIE::DeviceOp>();
43
44 AIE::ShimDMAAllocationOp alloc_op = AIE::ShimDMAAllocationOp::getForSymbol(
45 device, op.getAlloc().getRootReference());
46 if (!alloc_op) {
47 return op.emitOpError("no shim DMA allocation found for symbol");
48 }
49
50 AIE::TileOp tile = alloc_op.getTileOp();
51 if (!tile) {
52 return op.emitOpError(
53 "shim DMA allocation must reference a valid TileOp");
54 }
55
56 DMAConfigureTaskOp new_op = DMAConfigureTaskOp::create(
57 rewriter, op.getLoc(), rewriter.getIndexType(), tile.getResult(),
58 alloc_op.getChannelDir(), (int32_t)alloc_op.getChannelIndex(),
59 op.getIssueToken(), op.getRepeatCount(),
60 alloc_op.getPacket().value_or(nullptr));
61 rewriter.replaceAllUsesWith(op.getResult(), new_op.getResult());
62 rewriter.inlineRegionBefore(op.getBody(), new_op.getBody(),
63 new_op.getBody().begin());
64 rewriter.eraseOp(op);
65 return success();
66 }
67};
68
69struct AIESubstituteShimDMAAllocationsPass
70 : xilinx::AIEX::impl::AIESubstituteShimDMAAllocationsBase<
71 AIESubstituteShimDMAAllocationsPass> {
72
73 void runOnOperation() override {
74 AIE::DeviceOp device = getOperation();
75
76 // Convert DMAConfigureTaskForOps that reference shim DMA allocations
77 // to regular DMAConfigureTaskOps
78 RewritePatternSet patterns(&getContext());
79 patterns.insert<DMAConfigureTaskForOpPattern>(&getContext());
80
81 (void)applyPatternsGreedily(device, std::move(patterns));
82 }
83};
84
85} // namespace
86
87std::unique_ptr<OperationPass<AIE::DeviceOp>>
89 return std::make_unique<AIESubstituteShimDMAAllocationsPass>();
90}
std::unique_ptr< mlir::OperationPass< AIE::DeviceOp > > createAIESubstituteShimDMAAllocationsPass()