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/MemRef/IR/MemRef.h" 10 #include "mlir/Interfaces/SideEffectInterfaces.h" 11 #include "mlir/Transforms/InliningUtils.h" 12 13 using namespace mlir; 14 using namespace mlir::memref; 15 16 #include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc" 17 18 //===----------------------------------------------------------------------===// 19 // MemRefDialect Dialect Interfaces 20 //===----------------------------------------------------------------------===// 21 22 namespace { 23 struct MemRefInlinerInterface : public DialectInlinerInterface { 24 using DialectInlinerInterface::DialectInlinerInterface; 25 bool isLegalToInline(Region *dest, Region *src, bool wouldBeCloned, 26 BlockAndValueMapping &valueMapping) const final { 27 return true; 28 } 29 bool isLegalToInline(Operation *, Region *, bool wouldBeCloned, 30 BlockAndValueMapping &) const final { 31 return true; 32 } 33 }; 34 } // namespace 35 36 void mlir::memref::MemRefDialect::initialize() { 37 addOperations< 38 #define GET_OP_LIST 39 #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc" 40 >(); 41 addInterfaces<MemRefInlinerInterface>(); 42 } 43 44 /// Finds a single dealloc operation for the given allocated value. 45 llvm::Optional<Operation *> mlir::memref::findDealloc(Value allocValue) { 46 Operation *dealloc = nullptr; 47 for (Operation *user : allocValue.getUsers()) { 48 auto effectInterface = dyn_cast<MemoryEffectOpInterface>(user); 49 if (!effectInterface) 50 continue; 51 // Try to find a free effect that is applied to one of our values 52 // that will be automatically freed by our pass. 53 SmallVector<MemoryEffects::EffectInstance, 2> effects; 54 effectInterface.getEffectsOnValue(allocValue, effects); 55 const bool isFree = 56 llvm::any_of(effects, [&](MemoryEffects::EffectInstance &it) { 57 return isa<MemoryEffects::Free>(it.getEffect()); 58 }); 59 if (!isFree) 60 continue; 61 // If we found > 1 dealloc, return None. 62 if (dealloc) 63 return llvm::None; 64 dealloc = user; 65 } 66 return dealloc; 67 } 68