xref: /llvm-project/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp (revision 35d55f2894a2a2cdca5db494f519aa5ec7273678)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, 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 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
10 #include "mlir/Dialect/Arith/IR/Arith.h"
11 #include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
12 #include "mlir/Dialect/MemRef/IR/MemRef.h"
13 #include "mlir/IR/BuiltinTypes.h"
14 #include "mlir/Interfaces/MemorySlotInterfaces.h"
15 #include "mlir/Interfaces/RuntimeVerifiableOpInterface.h"
16 #include "mlir/Interfaces/SideEffectInterfaces.h"
17 #include "mlir/Interfaces/ValueBoundsOpInterface.h"
18 #include "mlir/Transforms/InliningUtils.h"
19 #include <optional>
20 
21 using namespace mlir;
22 using namespace mlir::memref;
23 
24 #include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc"
25 
26 //===----------------------------------------------------------------------===//
27 // MemRefDialect Dialect Interfaces
28 //===----------------------------------------------------------------------===//
29 
30 namespace {
31 struct MemRefInlinerInterface : public DialectInlinerInterface {
32   using DialectInlinerInterface::DialectInlinerInterface;
isLegalToInline__anon5bf416480111::MemRefInlinerInterface33   bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
34                        IRMapping &valueMapping) const final {
35     return true;
36   }
isLegalToInline__anon5bf416480111::MemRefInlinerInterface37   bool isLegalToInline(Operation *, Region *, bool wouldBeCloned,
38                        IRMapping &) const final {
39     return true;
40   }
41 };
42 } // namespace
43 
initialize()44 void mlir::memref::MemRefDialect::initialize() {
45   addOperations<
46 #define GET_OP_LIST
47 #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
48       >();
49   addInterfaces<MemRefInlinerInterface>();
50   declarePromisedInterface<ConvertToLLVMPatternInterface, MemRefDialect>();
51   declarePromisedInterfaces<bufferization::AllocationOpInterface, AllocOp,
52                             AllocaOp, ReallocOp>();
53   declarePromisedInterfaces<RuntimeVerifiableOpInterface, CastOp, ExpandShapeOp,
54                             LoadOp, ReinterpretCastOp, StoreOp, SubViewOp>();
55   declarePromisedInterfaces<ValueBoundsOpInterface, AllocOp, AllocaOp, CastOp,
56                             DimOp, GetGlobalOp, RankOp, SubViewOp>();
57   declarePromisedInterface<DestructurableTypeInterface, MemRefType>();
58 }
59 
60 /// Finds the unique dealloc operation (if one exists) for `allocValue`.
findDealloc(Value allocValue)61 std::optional<Operation *> mlir::memref::findDealloc(Value allocValue) {
62   Operation *dealloc = nullptr;
63   for (Operation *user : allocValue.getUsers()) {
64     if (!hasEffect<MemoryEffects::Free>(user, allocValue))
65       continue;
66     // If we found a realloc instead of dealloc, return std::nullopt.
67     if (isa<memref::ReallocOp>(user))
68       return std::nullopt;
69     // If we found > 1 dealloc, return std::nullopt.
70     if (dealloc)
71       return std::nullopt;
72     dealloc = user;
73   }
74   return dealloc;
75 }
76