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