#include "mlir/Dialect/Tensor/IR/TensorTilingInterfaceImpl.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/Utils.h"
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/Linalg/Utils/Utils.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/TilingInterface.h"
using namespace mlir;
using namespace mlir::tensor;
namespace {
struct PadOpTiling : public TilingInterface::ExternalModel<PadOpTiling, PadOp> {
SmallVector<utils::IteratorType> getLoopIteratorTypes(Operation *op) const {
auto padOp = cast<PadOp>(op);
SmallVector<utils::IteratorType> iteratorTypes(
padOp.getResultType().getRank(), utils::IteratorType::parallel);
return iteratorTypes;
}
SmallVector<Range> getIterationDomain(Operation *op, OpBuilder &b) const {
ReifiedRankedShapedTypeDims reifiedShapes;
(void)reifyResultShapes(b, op, reifiedShapes);
OpFoldResult zero = b.getIndexAttr(0);
OpFoldResult one = b.getIndexAttr(1);
SmallVector<Range> loopRanges(reifiedShapes[0].size(), {zero, one, one});
for (const auto &ub : enumerate(reifiedShapes[0]))
loopRanges[ub.index()].size = ub.value();
return loopRanges;
}
FailureOr<TilingResult>
getTiledImplementation(Operation *op, OpBuilder &b,
ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes) const {
FailureOr<TilingResult> result =
tensor::bubbleUpPadSlice(b, cast<PadOp>(op), offsets, sizes);
if (failed(result))
return failure();
return result.value();
}
LogicalResult
getResultTilePosition(Operation *op, OpBuilder &b, unsigned resultNumber,
ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes,
SmallVector<OpFoldResult> &resultOffsets,
SmallVector<OpFoldResult> &resultSizes) const {
resultOffsets.assign(offsets.begin(), offsets.end());
resultSizes.assign(sizes.begin(), sizes.end());
return success();
}
LogicalResult getIterationDomainTileFromResultTile(
Operation *op, OpBuilder &b, unsigned resultNumber,
ArrayRef<OpFoldResult> offsets, ArrayRef<OpFoldResult> sizes,
SmallVectorImpl<OpFoldResult> &iterDomainOffsets,
SmallVectorImpl<OpFoldResult> &iterDomainSizes) const {
iterDomainOffsets.assign(offsets.begin(), offsets.end());
iterDomainSizes.assign(sizes.begin(), sizes.end());
return success();
}
FailureOr<TilingResult>
generateResultTileValue(Operation *op, OpBuilder &b, unsigned resultNumber,
ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes) const {
return getTiledImplementation(op, b, offsets, sizes);
}
};
}
FailureOr<TilingResult> tensor::bubbleUpPadSlice(OpBuilder &b,
tensor::PadOp padOp,
ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes,
bool generateZeroSliceGuard) {
Value padValue = padOp.getConstantPaddingValue();
if (!padValue)
return failure();
Location loc = padOp->getLoc();
AffineExpr dim0, dim1;
bindDims(b.getContext(), dim0, dim1);
auto subMap = AffineMap::get(2, 0, {dim0 - dim1});
auto sub = [&](OpFoldResult v1, OpFoldResult v2) {
return affine::makeComposedFoldedAffineApply(b, loc, subMap, {v1, v2});
};
auto idMap = AffineMap::getMultiDimIdentityMap(2, b.getContext());
auto min = [&](OpFoldResult v1, OpFoldResult v2) {
return affine::makeComposedFoldedAffineMin(b, loc, idMap, {v1, v2});
};
auto max = [&](OpFoldResult v1, OpFoldResult v2) {
return affine::makeComposedFoldedAffineMax(b, loc, idMap, {v1, v2});
};
OpFoldResult zero = b.getIndexAttr(0);
SmallVector<OpFoldResult> newOffsets, newLengths;
SmallVector<OpFoldResult> newLows, newHighs;
bool hasZeroLen = false;
Value dynHasZeroLenCond;
int64_t rank = padOp.getSourceType().getRank();
SmallVector<OpFoldResult> newStrides(rank, b.getIndexAttr(1));
for (unsigned dim = 0; dim < rank; ++dim) {
auto low = padOp.getMixedLowPad()[dim];
bool hasLowPad = !isZeroInteger(low);
auto high = padOp.getMixedHighPad()[dim];
bool hasHighPad = !isZeroInteger(high);
auto offset = offsets[dim];
auto length = sizes[dim];
if (!hasLowPad && !hasHighPad) {
newOffsets.push_back(offset);
newLengths.push_back(length);
newLows.push_back(low);
newHighs.push_back(high);
continue;
}
auto srcSize = tensor::getMixedSize(b, loc, padOp.getSource(), dim);
OpFoldResult newLow = hasLowPad ? max(zero, sub(low, offset)) : zero;
newLows.push_back(newLow);
OpFoldResult newOffset = hasLowPad
? min(max(sub(offset, low), zero), srcSize)
: min(offset, srcSize);
newOffsets.push_back(newOffset);
OpFoldResult newLength = min(sub(srcSize, newOffset), sub(length, newLow));
if (hasLowPad)
newLength = max(newLength, zero);
newLengths.push_back(newLength);
if (isZeroInteger(newLength)) {
hasZeroLen = true;
} else if (!hasZeroLen) {
Value check = arith::CmpIOp::create(
b, loc, arith::CmpIPredicate::eq,
getValueOrCreateConstantIndexOp(b, loc, newLength),
getValueOrCreateConstantIndexOp(b, loc, zero));
dynHasZeroLenCond =
dynHasZeroLenCond
? arith::OrIOp::create(b, loc, check, dynHasZeroLenCond)
: check;
}
OpFoldResult newHigh =
hasHighPad ? sub(sub(length, newLength), newLow) : zero;
newHighs.push_back(newHigh);
}
SmallVector<Value> dynDims;
SmallVector<int64_t> shape;
dispatchIndexOpFoldResults(sizes, dynDims, shape);
RankedTensorType resultType =
RankedTensorType::get(shape, padOp.getResultType().getElementType());
auto castResult = [&](Value val) -> Value {
if (resultType == val.getType())
return val;
return tensor::CastOp::create(b, loc, resultType, val);
};
auto createGenerateOp = [&]() {
auto generateOp = tensor::GenerateOp::create(
b, loc, resultType, dynDims,
[&](OpBuilder &builder, Location gLoc, ValueRange indices) {
tensor::YieldOp::create(builder, gLoc, padValue);
});
return generateOp;
};
auto createPadOfExtractSlice = [&]() {
auto newSliceOp = tensor::ExtractSliceOp::create(
b, loc, padOp.getSource(), newOffsets, newLengths, newStrides);
auto newPadOp = PadOp::create(
b, loc, Type(), newSliceOp, newLows, newHighs,
padOp.getNofold(),
getPrunedAttributeList(padOp, PadOp::getAttributeNames()));
IRMapping bvm;
padOp.getRegion().cloneInto(&newPadOp.getRegion(), bvm);
return std::make_tuple(newPadOp, newSliceOp);
};
if (hasZeroLen) {
Operation *generateOp = createGenerateOp();
return TilingResult{{generateOp},
{castResult(generateOp->getResult(0))},
{}};
}
if (generateZeroSliceGuard && dynHasZeroLenCond) {
Operation *thenOp;
Operation *elseOp;
Operation *sliceOp;
auto result = scf::IfOp::create(
b, loc, dynHasZeroLenCond,
[&](OpBuilder &b, Location loc) {
thenOp = createGenerateOp();
scf::YieldOp::create(b, loc, castResult(thenOp->getResult(0)));
},
[&](OpBuilder &b, Location loc) {
std::tie(elseOp, sliceOp) = createPadOfExtractSlice();
scf::YieldOp::create(b, loc, castResult(elseOp->getResult(0)));
});
return TilingResult{
{elseOp}, SmallVector<Value>(result->getResults()), {sliceOp}};
}
auto [newPadOp, sliceOp] = createPadOfExtractSlice();
return TilingResult{
{newPadOp}, {castResult(newPadOp->getResult(0))}, {sliceOp}};
}
void mlir::tensor::registerTilingInterfaceExternalModels(
DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, TensorDialect *dialect) {
tensor::PadOp::attachInterface<PadOpTiling>(*ctx);
});
}