This commit generalizes the special tensor.extract_slice/tensor.insert_slice bufferization rules to tensor subset ops. Ops that insert a tensor into a tensor at a specified subset (e.g., tensor.insert_slice, tensor.scatter) can implement the `SubsetInsertionOpInterface`. Apart from adding a new op interface (extending the API), this change is NFC. The only ops that currently implement the new interface are tensor.insert_slice and tensor.parallel_insert_slice, and those ops were are supported by One-Shot Bufferize.
83 lines
3.0 KiB
C++
83 lines
3.0 KiB
C++
//===- SubsetInsertionOpInterfaceImpl.cpp - Tensor subsets ----------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Dialect/Tensor/Transforms/SubsetInsertionOpInterfaceImpl.h"
|
|
|
|
#include "mlir/Dialect/Bufferization/IR/SubsetInsertionOpInterface.h"
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::bufferization;
|
|
using namespace mlir::tensor;
|
|
|
|
namespace {
|
|
|
|
/// Return "true" if `insertSliceOp` inserts into a subset that is equivalent
|
|
/// to the subset defined by `candidate`. `equivalenceFn` is used to determine
|
|
/// equivalence of tensors.
|
|
template <typename OpTy>
|
|
bool isSubsetEquivalentToInsertSliceLikeOp(
|
|
OpTy insertSliceOp, Value candidate,
|
|
function_ref<bool(Value, Value)> equivalenceFn) {
|
|
// Look for a matching tensor.extract_slice op.
|
|
auto extractSliceOp = candidate.getDefiningOp<tensor::ExtractSliceOp>();
|
|
if (!extractSliceOp)
|
|
return false;
|
|
if (!equivalenceFn(extractSliceOp.getSource(), insertSliceOp.getDest()))
|
|
return false;
|
|
return sameOffsetsSizesAndStrides(extractSliceOp, insertSliceOp,
|
|
isEqualConstantIntOrValue);
|
|
}
|
|
|
|
struct InsertSliceOpInterface
|
|
: public SubsetInsertionOpInterface::ExternalModel<InsertSliceOpInterface,
|
|
tensor::InsertSliceOp> {
|
|
OpOperand &getSourceOperand(Operation *op) const {
|
|
return op->getOpOperand(0);
|
|
}
|
|
|
|
bool
|
|
isEquivalentSubset(Operation *op, Value candidate,
|
|
function_ref<bool(Value, Value)> equivalenceFn) const {
|
|
auto insertSliceOp = cast<tensor::InsertSliceOp>(op);
|
|
return isSubsetEquivalentToInsertSliceLikeOp(insertSliceOp, candidate,
|
|
equivalenceFn);
|
|
}
|
|
};
|
|
|
|
struct ParallelInsertSliceOpInterface
|
|
: public SubsetInsertionOpInterface::ExternalModel<
|
|
ParallelInsertSliceOpInterface, tensor::ParallelInsertSliceOp> {
|
|
OpOperand &getSourceOperand(Operation *op) const {
|
|
return op->getOpOperand(0);
|
|
}
|
|
|
|
OpOperand &getDestinationOperand(Operation *op) const {
|
|
return op->getOpOperand(1);
|
|
}
|
|
|
|
bool
|
|
isEquivalentSubset(Operation *op, Value candidate,
|
|
function_ref<bool(Value, Value)> equivalenceFn) const {
|
|
auto insertSliceOp = cast<tensor::ParallelInsertSliceOp>(op);
|
|
return isSubsetEquivalentToInsertSliceLikeOp(insertSliceOp, candidate,
|
|
equivalenceFn);
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
void mlir::tensor::registerSubsetInsertionOpInterfaceExternalModels(
|
|
DialectRegistry ®istry) {
|
|
registry.addExtension(+[](MLIRContext *ctx, tensor::TensorDialect *dialect) {
|
|
InsertSliceOp::attachInterface<InsertSliceOpInterface>(*ctx);
|
|
ParallelInsertSliceOp::attachInterface<ParallelInsertSliceOpInterface>(
|
|
*ctx);
|
|
});
|
|
}
|