Skip to content

Commit

Permalink
NFC. Clean up memref utils library
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bondhugula committed Mar 9, 2022
1 parent 092601d commit af9f7d3
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 46 deletions.
5 changes: 5 additions & 0 deletions mlir/include/mlir/Dialect/MemRef/IR/MemRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions mlir/include/mlir/Dialect/MemRef/Utils/MemRefUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Bufferization/Transforms/BufferUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/MemRef/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ add_mlir_dialect_library(MLIRMemRef
MLIRDialectUtils
MLIRInferTypeOpInterface
MLIRIR
MLIRMemRefUtils
MLIRSideEffectInterfaces
MLIRViewLikeInterface
)
26 changes: 26 additions & 0 deletions mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Transforms/InliningUtils.h"

using namespace mlir;
Expand Down Expand Up @@ -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;
}
4 changes: 0 additions & 4 deletions mlir/lib/Dialect/MemRef/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ add_mlir_dialect_library(MLIRMemRefUtils

ADDITIONAL_HEADER_DIRS
${PROJECT_SOURCE_DIR}/inlude/mlir/Dialect/MemRefDialect

LINK_LIBS PUBLIC
MLIRIR
MLIRSideEffectInterfaces
)

28 changes: 0 additions & 28 deletions mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit af9f7d3

Please sign in to comment.