MLIR 24.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
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 addInterfaces<MemRefInlinerInterface>();
47 declarePromisedInterface<ConvertToEmitCPatternInterface, MemRefDialect>();
48 declarePromisedInterface<ConvertToLLVMPatternInterface, MemRefDialect>();
49 declarePromisedInterfaces<bufferization::AllocationOpInterface, AllocOp,
50 AllocaOp, ReallocOp>();
51 declarePromisedInterfaces<RuntimeVerifiableOpInterface, AssumeAlignmentOp,
52 AtomicRMWOp, CastOp, CopyOp, DimOp, ExpandShapeOp,
53 GenericAtomicRMWOp, LoadOp, StoreOp, SubViewOp>();
54 declarePromisedInterfaces<
55 ValueBoundsOpInterface, AllocOp, AllocaOp, AssumeAlignmentOp, CastOp,
56 DimOp, ExtractStridedMetadataOp, GetGlobalOp, RankOp, SubViewOp>();
57 declarePromisedInterface<DestructurableTypeInterface, MemRefType>();
58}
59
60/// Finds the unique dealloc operation (if one exists) for `allocValue`.
61std::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}
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.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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.