diff options
author | Uday Bondhugula <uday@polymagelabs.com> | 2022-03-09 09:25:49 +0300 |
---|---|---|
committer | Uday Bondhugula <uday@polymagelabs.com> | 2022-03-09 13:30:39 +0300 |
commit | af9f7d319b8c12063a9d105c9e13984141a51c62 (patch) | |
tree | f58292234cc49070636d9dd52b94594058967c86 /mlir | |
parent | 092601d4baab7c13c06b31eda2d5bed91d9a6b65 (diff) |
NFC. Clean up memref utils library
NFC. Clean up memref utils library. This library had a single function
that was completely misplaced. MemRefUtils is expected to be (also per
its comment) a library providing analysis/transforms utilities on memref
dialect ops or memref types. However, in reality it had a helper that
was depended upon by the MemRef dialect, i.e., it was a helper for the
dialect ops library and couldn't contain anything that itself depends on
the MemRef dialect. Move the single method to the memref dialect that
will now allow actual utilities depending on the memref dialect to be
placed in it.
Put findDealloc in the `memref` namespace. This is a pure move.
Differential Revision: https://reviews.llvm.org/D121273
Diffstat (limited to 'mlir')
-rw-r--r-- | mlir/include/mlir/Dialect/MemRef/IR/MemRef.h | 5 | ||||
-rw-r--r-- | mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h | 10 | ||||
-rw-r--r-- | mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp | 5 | ||||
-rw-r--r-- | mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp | 2 | ||||
-rw-r--r-- | mlir/lib/Dialect/MemRef/IR/CMakeLists.txt | 2 | ||||
-rw-r--r-- | mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp | 26 | ||||
-rw-r--r-- | mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt | 4 | ||||
-rw-r--r-- | mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp | 28 |
8 files changed, 36 insertions, 46 deletions
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h b/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h index bf472ac64104..5b7c59c4aa9e 100644 --- a/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h +++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRef.h @@ -43,6 +43,11 @@ LogicalResult foldMemRefCast(Operation *op, Value inner = nullptr); /// type. Type getTensorTypeFromMemRefType(Type type); +/// Finds a single dealloc operation for the given allocated value. If there +/// are > 1 deallocates for `allocValue`, returns None, else returns the single +/// deallocate if it exists or nullptr. +Optional<Operation *> findDealloc(Value allocValue); + } // namespace memref } // namespace mlir diff --git a/mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h b/mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h index 54b519bcb347..f7e525a7374f 100644 --- a/mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h +++ b/mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h @@ -16,14 +16,4 @@ #ifndef MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H #define MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H -#include "mlir/Dialect/MemRef/IR/MemRef.h" - -namespace mlir { - -/// Finds a single dealloc operation for the given allocated value. If there -/// are > 1 deallocates for `allocValue`, returns None, else returns the single -/// deallocate if it exists or nullptr. -llvm::Optional<Operation *> findDealloc(Value allocValue); -} // namespace mlir - #endif // MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp index 9b3342a6e801..8b07b1f97b83 100644 --- a/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp +++ b/mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp @@ -118,11 +118,12 @@ struct SimplifyClones : public OpRewritePattern<CloneOp> { // also consider aliases. That would also make the safety check below // redundant. llvm::Optional<Operation *> maybeCloneDeallocOp = - findDealloc(cloneOp.output()); + memref::findDealloc(cloneOp.output()); // Skip if either of them has > 1 deallocate operations. if (!maybeCloneDeallocOp.hasValue()) return failure(); - llvm::Optional<Operation *> maybeSourceDeallocOp = findDealloc(source); + llvm::Optional<Operation *> maybeSourceDeallocOp = + memref::findDealloc(source); if (!maybeSourceDeallocOp.hasValue()) return failure(); Operation *cloneDeallocOp = *maybeCloneDeallocOp; diff --git a/mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp b/mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp index e5ae569bf63d..5bd122890ceb 100644 --- a/mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp +++ b/mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp @@ -78,7 +78,7 @@ void BufferPlacementAllocs::build(Operation *op) { // Get allocation result. Value allocValue = allocateResultEffects[0].getValue(); // Find the associated dealloc value and register the allocation entry. - llvm::Optional<Operation *> dealloc = findDealloc(allocValue); + llvm::Optional<Operation *> dealloc = memref::findDealloc(allocValue); // If the allocation has > 1 dealloc associated with it, skip handling it. if (!dealloc.hasValue()) return; diff --git a/mlir/lib/Dialect/MemRef/IR/CMakeLists.txt b/mlir/lib/Dialect/MemRef/IR/CMakeLists.txt index ceaede262a23..4be8475f4549 100644 --- a/mlir/lib/Dialect/MemRef/IR/CMakeLists.txt +++ b/mlir/lib/Dialect/MemRef/IR/CMakeLists.txt @@ -19,6 +19,6 @@ add_mlir_dialect_library(MLIRMemRef MLIRDialectUtils MLIRInferTypeOpInterface MLIRIR - MLIRMemRefUtils + MLIRSideEffectInterfaces MLIRViewLikeInterface ) diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp index f27355835dfb..ab66214673b0 100644 --- a/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp +++ b/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "mlir/Dialect/MemRef/IR/MemRef.h" +#include "mlir/Interfaces/SideEffectInterfaces.h" #include "mlir/Transforms/InliningUtils.h" using namespace mlir; @@ -39,3 +40,28 @@ void mlir::memref::MemRefDialect::initialize() { >(); addInterfaces<MemRefInlinerInterface>(); } + +/// Finds a single dealloc operation for the given allocated value. +llvm::Optional<Operation *> mlir::memref::findDealloc(Value allocValue) { + Operation *dealloc = nullptr; + for (Operation *user : allocValue.getUsers()) { + auto effectInterface = dyn_cast<MemoryEffectOpInterface>(user); + if (!effectInterface) + continue; + // Try to find a free effect that is applied to one of our values + // that will be automatically freed by our pass. + SmallVector<MemoryEffects::EffectInstance, 2> effects; + effectInterface.getEffectsOnValue(allocValue, effects); + const bool isFree = + llvm::any_of(effects, [&](MemoryEffects::EffectInstance &it) { + return isa<MemoryEffects::Free>(it.getEffect()); + }); + if (!isFree) + continue; + // If we found > 1 dealloc, return None. + if (dealloc) + return llvm::None; + dealloc = user; + } + return dealloc; +} diff --git a/mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt b/mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt index 17a6ecba0910..770887217490 100644 --- a/mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt +++ b/mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt @@ -3,9 +3,5 @@ add_mlir_dialect_library(MLIRMemRefUtils ADDITIONAL_HEADER_DIRS ${PROJECT_SOURCE_DIR}/inlude/mlir/Dialect/MemRefDialect - - LINK_LIBS PUBLIC - MLIRIR - MLIRSideEffectInterfaces ) diff --git a/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp b/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp index edb7e46e7e9a..3fa3bab2f5ce 100644 --- a/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp +++ b/mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp @@ -11,31 +11,3 @@ //===----------------------------------------------------------------------===// #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" -#include "mlir/Interfaces/SideEffectInterfaces.h" - -using namespace mlir; - -/// Finds a single dealloc operation for the given allocated value. -llvm::Optional<Operation *> mlir::findDealloc(Value allocValue) { - Operation *dealloc = nullptr; - for (Operation *user : allocValue.getUsers()) { - auto effectInterface = dyn_cast<MemoryEffectOpInterface>(user); - if (!effectInterface) - continue; - // Try to find a free effect that is applied to one of our values - // that will be automatically freed by our pass. - SmallVector<MemoryEffects::EffectInstance, 2> effects; - effectInterface.getEffectsOnValue(allocValue, effects); - const bool isFree = - llvm::any_of(effects, [&](MemoryEffects::EffectInstance &it) { - return isa<MemoryEffects::Free>(it.getEffect()); - }); - if (!isFree) - continue; - // If we found > 1 dealloc, return None. - if (dealloc) - return llvm::None; - dealloc = user; - } - return dealloc; -} |