xref: /llvm-project/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp (revision 35d55f2894a2a2cdca5db494f519aa5ec7273678)
1e2310704SJulian Gross //===----------------------------------------------------------------------===//
2e2310704SJulian Gross //
3e2310704SJulian Gross // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e2310704SJulian Gross // See https://llvm.org/LICENSE.txt for license information.
5e2310704SJulian Gross // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e2310704SJulian Gross //
7e2310704SJulian Gross //===----------------------------------------------------------------------===//
8e2310704SJulian Gross 
9b43c5049SJustin Fargnoli #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
10abc362a1SJakub Kuderski #include "mlir/Dialect/Arith/IR/Arith.h"
11513cdb82SJustin Fargnoli #include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
12e2310704SJulian Gross #include "mlir/Dialect/MemRef/IR/MemRef.h"
13513cdb82SJustin Fargnoli #include "mlir/IR/BuiltinTypes.h"
14513cdb82SJustin Fargnoli #include "mlir/Interfaces/MemorySlotInterfaces.h"
15513cdb82SJustin Fargnoli #include "mlir/Interfaces/RuntimeVerifiableOpInterface.h"
16af9f7d31SUday Bondhugula #include "mlir/Interfaces/SideEffectInterfaces.h"
17513cdb82SJustin Fargnoli #include "mlir/Interfaces/ValueBoundsOpInterface.h"
18e2310704SJulian Gross #include "mlir/Transforms/InliningUtils.h"
19a1fe1f5fSKazu Hirata #include <optional>
20e2310704SJulian Gross 
21e2310704SJulian Gross using namespace mlir;
22e2310704SJulian Gross using namespace mlir::memref;
23e2310704SJulian Gross 
24485cc55eSStella Laurenzo #include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc"
25485cc55eSStella Laurenzo 
26e2310704SJulian Gross //===----------------------------------------------------------------------===//
27e2310704SJulian Gross // MemRefDialect Dialect Interfaces
28e2310704SJulian Gross //===----------------------------------------------------------------------===//
29e2310704SJulian Gross 
30e2310704SJulian Gross namespace {
31e2310704SJulian Gross struct MemRefInlinerInterface : public DialectInlinerInterface {
32e2310704SJulian Gross   using DialectInlinerInterface::DialectInlinerInterface;
isLegalToInline__anon5bf416480111::MemRefInlinerInterface33e2310704SJulian Gross   bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned,
344d67b278SJeff Niu                        IRMapping &valueMapping) const final {
35e2310704SJulian Gross     return true;
36e2310704SJulian Gross   }
isLegalToInline__anon5bf416480111::MemRefInlinerInterface37e2310704SJulian Gross   bool isLegalToInline(Operation *, Region *, bool wouldBeCloned,
384d67b278SJeff Niu                        IRMapping &) const final {
39e2310704SJulian Gross     return true;
40e2310704SJulian Gross   }
41e2310704SJulian Gross };
42be0a7e9fSMehdi Amini } // namespace
43e2310704SJulian Gross 
initialize()44e2310704SJulian Gross void mlir::memref::MemRefDialect::initialize() {
45aca9bea1SRiver Riddle   addOperations<
46e2310704SJulian Gross #define GET_OP_LIST
47e2310704SJulian Gross #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
48e2310704SJulian Gross       >();
49e2310704SJulian Gross   addInterfaces<MemRefInlinerInterface>();
50*35d55f28SJustin Fargnoli   declarePromisedInterface<ConvertToLLVMPatternInterface, MemRefDialect>();
51513cdb82SJustin Fargnoli   declarePromisedInterfaces<bufferization::AllocationOpInterface, AllocOp,
52513cdb82SJustin Fargnoli                             AllocaOp, ReallocOp>();
53513cdb82SJustin Fargnoli   declarePromisedInterfaces<RuntimeVerifiableOpInterface, CastOp, ExpandShapeOp,
54513cdb82SJustin Fargnoli                             LoadOp, ReinterpretCastOp, StoreOp, SubViewOp>();
55513cdb82SJustin Fargnoli   declarePromisedInterfaces<ValueBoundsOpInterface, AllocOp, AllocaOp, CastOp,
56513cdb82SJustin Fargnoli                             DimOp, GetGlobalOp, RankOp, SubViewOp>();
57*35d55f28SJustin Fargnoli   declarePromisedInterface<DestructurableTypeInterface, MemRefType>();
58e2310704SJulian Gross }
59af9f7d31SUday Bondhugula 
608d7f2701SUday Bondhugula /// Finds the unique dealloc operation (if one exists) for `allocValue`.
findDealloc(Value allocValue)610a81ace0SKazu Hirata std::optional<Operation *> mlir::memref::findDealloc(Value allocValue) {
62af9f7d31SUday Bondhugula   Operation *dealloc = nullptr;
63af9f7d31SUday Bondhugula   for (Operation *user : allocValue.getUsers()) {
648d7f2701SUday Bondhugula     if (!hasEffect<MemoryEffects::Free>(user, allocValue))
65af9f7d31SUday Bondhugula       continue;
6657609fb6SAlexander Shaposhnikov     // If we found a realloc instead of dealloc, return std::nullopt.
6757609fb6SAlexander Shaposhnikov     if (isa<memref::ReallocOp>(user))
6857609fb6SAlexander Shaposhnikov       return std::nullopt;
6970c73d1bSKazu Hirata     // If we found > 1 dealloc, return std::nullopt.
70af9f7d31SUday Bondhugula     if (dealloc)
711a36588eSKazu Hirata       return std::nullopt;
72af9f7d31SUday Bondhugula     dealloc = user;
73af9f7d31SUday Bondhugula   }
74af9f7d31SUday Bondhugula   return dealloc;
75af9f7d31SUday Bondhugula }
76