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
This commit is contained in:
Uday Bondhugula 2022-03-09 11:55:49 +05:30
parent 092601d4ba
commit af9f7d319b
8 changed files with 36 additions and 46 deletions

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -19,6 +19,6 @@ add_mlir_dialect_library(MLIRMemRef
MLIRDialectUtils
MLIRInferTypeOpInterface
MLIRIR
MLIRMemRefUtils
MLIRSideEffectInterfaces
MLIRViewLikeInterface
)

View File

@ -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;
}

View File

@ -3,9 +3,5 @@ add_mlir_dialect_library(MLIRMemRefUtils
ADDITIONAL_HEADER_DIRS
${PROJECT_SOURCE_DIR}/inlude/mlir/Dialect/MemRefDialect
LINK_LIBS PUBLIC
MLIRIR
MLIRSideEffectInterfaces
)

View File

@ -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;
}