MLIR-AIE
AIENormalizeAddressSpaces.cpp
Go to the documentation of this file.
1//===- AIENormalizeAddressSpaces.cpp ----------------------------*- C++ -*-===//
2//
3// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7// (c) Copyright 2021 Xilinx Inc.
8//
9//===----------------------------------------------------------------------===//
10
13
14#include "mlir/IR/Attributes.h"
15#include "mlir/IR/PatternMatch.h"
16#include "mlir/Pass/Pass.h"
17#include "mlir/Transforms/DialectConversion.h"
18
19#define DEBUG_TYPE "aie-normalize-address-spaces"
20
21using namespace mlir;
22using namespace xilinx;
23using namespace xilinx::AIE;
24
26 if (auto memRefType = llvm::dyn_cast<MemRefType>(t);
27 memRefType && memRefType.getMemorySpace() != nullptr)
28 return MemRefType::get(memRefType.getShape(), memRefType.getElementType(),
29 memRefType.getLayout(), nullptr /* Address Space */);
30 return t;
31}
32
33#include "aie/Dialect/AIE/Transforms/AIENormalizeAddressSpaces.inc"
34
36 : AIENormalizeAddressSpacesBase<AIENormalizeAddressSpacesPass> {
37 void getDependentDialects(DialectRegistry &registry) const override {
38 registry.insert<func::FuncDialect>();
39 }
40 void runOnOperation() override {
41 DeviceOp device = getOperation();
42
43 TypeConverter converter;
44 converter.addConversion([&](Type type) -> std::optional<Type> {
45 return memRefToDefaultAddressSpace(type);
46 });
47
48 ConversionTarget target(getContext());
49 target.addDynamicallyLegalOp<memref::GlobalOp>([](memref::GlobalOp op) {
50 return op.getType().getMemorySpace() == nullptr;
51 });
52
53 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
54 return converter.isSignatureLegal(op.getFunctionType());
55 });
56
57 RewritePatternSet patterns(&getContext());
58 populateWithGenerated(patterns);
59 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns,
60 converter);
61
62 if (failed(applyPartialConversion(device, target, std::move(patterns))))
63 signalPassFailure();
64
65 // Convert any output types to have the default address space
66 device.walk([&](Operation *op) {
67 for (Value r : op->getResults())
68 r.setType(memRefToDefaultAddressSpace(r.getType()));
69 });
70 }
71};
72
73std::unique_ptr<OperationPass<DeviceOp>>
75 return std::make_unique<AIENormalizeAddressSpacesPass>();
76}
Type memRefToDefaultAddressSpace(Type t)
Include the generated interface declarations.
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIENormalizeAddressSpacesPass()
void getDependentDialects(DialectRegistry &registry) const override