xref: /llvm-project/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp (revision 0a81ace0047a2de93e71c82cdf0977fc989660df)
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/Dialect/Arith/IR/Arith.h"
10 #include "mlir/Dialect/MemRef/IR/MemRef.h"
11 #include "mlir/Interfaces/SideEffectInterfaces.h"
12 #include "mlir/Transforms/InliningUtils.h"
13 #include <optional>
14 
15 using namespace mlir;
16 using namespace mlir::memref;
17 
18 #include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc"
19 
20 //===----------------------------------------------------------------------===//
21 // MemRefDialect Dialect Interfaces
22 //===----------------------------------------------------------------------===//
23 
24 namespace {
25 struct MemRefInlinerInterface : public DialectInlinerInterface {
26   using DialectInlinerInterface::DialectInlinerInterface;
27   bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
28                        IRMapping &valueMapping) const final {
29     return true;
30   }
31   bool isLegalToInline(Operation *, Region *, bool wouldBeCloned,
32                        IRMapping &) const final {
33     return true;
34   }
35 };
36 } // namespace
37 
38 void mlir::memref::MemRefDialect::initialize() {
39   addOperations<
40 #define GET_OP_LIST
41 #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
42       >();
43   addInterfaces<MemRefInlinerInterface>();
44 }
45 
46 /// Finds the unique dealloc operation (if one exists) for `allocValue`.
47 std::optional<Operation *> mlir::memref::findDealloc(Value allocValue) {
48   Operation *dealloc = nullptr;
49   for (Operation *user : allocValue.getUsers()) {
50     if (!hasEffect<MemoryEffects::Free>(user, allocValue))
51       continue;
52     // If we found > 1 dealloc, return std::nullopt.
53     if (dealloc)
54       return std::nullopt;
55     dealloc = user;
56   }
57   return dealloc;
58 }
59