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/Dialect/Ptr/IR/PtrOps.h"
15#include "mlir/IR/Attributes.h"
16#include "mlir/IR/PatternMatch.h"
17#include "mlir/Pass/Pass.h"
18#include "mlir/Transforms/DialectConversion.h"
19
20#define DEBUG_TYPE "aie-normalize-address-spaces"
21
22using namespace mlir;
23using namespace xilinx;
24using namespace xilinx::AIE;
25
27 if (auto memRefType = llvm::dyn_cast<MemRefType>(t);
28 memRefType && memRefType.getMemorySpace() != nullptr) {
29 // Preserve ptr::GenericSpaceAttr - it's needed for ptr dialect
30 // transformations
31 if (llvm::isa<ptr::GenericSpaceAttr>(memRefType.getMemorySpace()))
32 return t; // Keep as-is
33
34 // Remove other memory spaces
35 return MemRefType::get(memRefType.getShape(), memRefType.getElementType(),
36 memRefType.getLayout(), nullptr /* Address Space */);
37 }
38 return t;
39}
40
41#include "aie/Dialect/AIE/Transforms/AIENormalizeAddressSpaces.inc"
42
43namespace xilinx::AIE {
44#define GEN_PASS_DEF_AIENORMALIZEADDRESSSPACES
45#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
46} // namespace xilinx::AIE
47
50 AIENormalizeAddressSpacesPass> {
51 void getDependentDialects(DialectRegistry &registry) const override {
52 registry.insert<func::FuncDialect>();
53 }
54 void runOnOperation() override {
55 DeviceOp device = getOperation();
56
57 TypeConverter converter;
58 converter.addConversion([&](Type type) -> std::optional<Type> {
59 return memRefToDefaultAddressSpace(type);
60 });
61
62 ConversionTarget target(getContext());
63 target.addDynamicallyLegalOp<memref::GlobalOp>([](memref::GlobalOp op) {
64 return op.getType().getMemorySpace() == nullptr;
65 });
66
67 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
68 return converter.isSignatureLegal(op.getFunctionType());
69 });
70
71 RewritePatternSet patterns(&getContext());
72 populateWithGenerated(patterns);
73 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns,
74 converter);
75
76 if (failed(applyPartialConversion(device, target, std::move(patterns))))
77 signalPassFailure();
78
79 // Convert any output types to have the default address space
80 device.walk([&](Operation *op) {
81 for (Value r : op->getResults())
82 r.setType(memRefToDefaultAddressSpace(r.getType()));
83 });
84 }
85};
86
87std::unique_ptr<OperationPass<DeviceOp>>
89 return std::make_unique<AIENormalizeAddressSpacesPass>();
90}
Type memRefToDefaultAddressSpace(Type t)
Include the generated interface declarations.
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIENormalizeAddressSpacesPass()
void getDependentDialects(DialectRegistry &registry) const override