MLIR 22.0.0git
MemRefDialect.cpp
Go to the documentation of this file.
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
19#include <optional>
20
21using namespace mlir;
22using namespace mlir::memref;
23
24#include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc"
25
26//===----------------------------------------------------------------------===//
27// MemRefDialect Dialect Interfaces
28//===----------------------------------------------------------------------===//
29
30namespace {
31struct MemRefInlinerInterface : public DialectInlinerInterface {
33 bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
34 IRMapping &valueMapping) const final {
35 return true;
36 }
37 bool isLegalToInline(Operation *, Region *, bool wouldBeCloned,
38 IRMapping &) const final {
39 return true;
40 }
41};
42} // namespace
43
44void 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<ConvertToEmitCPatternInterface, MemRefDialect>();
51 declarePromisedInterface<ConvertToLLVMPatternInterface, MemRefDialect>();
52 declarePromisedInterfaces<bufferization::AllocationOpInterface, AllocOp,
53 AllocaOp, ReallocOp>();
54 declarePromisedInterfaces<RuntimeVerifiableOpInterface, AssumeAlignmentOp,
55 AtomicRMWOp, CastOp, CopyOp, DimOp, ExpandShapeOp,
56 GenericAtomicRMWOp, LoadOp, StoreOp, SubViewOp>();
57 declarePromisedInterfaces<ValueBoundsOpInterface, AllocOp, AllocaOp, CastOp,
58 DimOp, GetGlobalOp, RankOp, SubViewOp>();
59 declarePromisedInterface<DestructurableTypeInterface, MemRefType>();
60}
61
62/// Finds the unique dealloc operation (if one exists) for `allocValue`.
63std::optional<Operation *> mlir::memref::findDealloc(Value allocValue) {
64 Operation *dealloc = nullptr;
65 for (Operation *user : allocValue.getUsers()) {
66 if (!hasEffect<MemoryEffects::Free>(user, allocValue))
67 continue;
68 // If we found a realloc instead of dealloc, return std::nullopt.
69 if (isa<memref::ReallocOp>(user))
70 return std::nullopt;
71 // If we found > 1 dealloc, return std::nullopt.
72 if (dealloc)
73 return std::nullopt;
74 dealloc = user;
75 }
76 return dealloc;
77}
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
This is the interface that must be implemented by the dialects of operations to be inlined.
DialectInlinerInterface(Dialect *dialect)
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
user_range getUsers() const
Definition Value.h:218
std::optional< Operation * > findDealloc(Value allocValue)
Finds a single dealloc operation for the given allocated value.
Include the generated interface declarations.
bool hasEffect(Operation *op)
Returns "true" if op has an effect of type EffectTy.