The MLIR classes Type/Attribute/Operation/Op/Value support cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast functionality in addition to defining methods with the same name. This change begins the migration of uses of the method to the corresponding function call as has been decided as more consistent. Note that there still exist classes that only define methods directly, such as AffineExpr, and this does not include work currently to support a functional cast/isa call. Caveats include: - This clang-tidy script probably has more problems. - This only touches C++ code, so nothing that is being generated. Context: - https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…" - Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443 Implementation: This first patch was created with the following steps. The intention is to only do automated changes at first, so I waste less time if it's reverted, and so the first mass change is more clear as an example to other teams that will need to follow similar steps. Steps are described per line, as comments are removed by git: 0. Retrieve the change from the following to build clang-tidy with an additional check: https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check 1. Build clang-tidy 2. Run clang-tidy over your entire codebase while disabling all checks and enabling the one relevant one. Run on all header files also. 3. Delete .inc files that were also modified, so the next build rebuilds them to a pure state. 4. Some changes have been deleted for the following reasons: - Some files had a variable also named cast - Some files had not included a header file that defines the cast functions - Some files are definitions of the classes that have the casting methods, so the code still refers to the method instead of the function without adding a prefix or removing the method declaration at the same time. ``` ninja -C $BUILD_DIR clang-tidy run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\ -header-filter=mlir/ mlir/* -fix rm -rf $BUILD_DIR/tools/mlir/**/*.inc git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\ mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\ mlir/lib/**/IR/\ mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\ mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\ mlir/test/lib/Dialect/Test/TestTypes.cpp\ mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\ mlir/test/lib/Dialect/Test/TestAttributes.cpp\ mlir/unittests/TableGen/EnumsGenTest.cpp\ mlir/test/python/lib/PythonTestCAPI.cpp\ mlir/include/mlir/IR/ ``` Differential Revision: https://reviews.llvm.org/D150123
214 lines
9.0 KiB
C++
214 lines
9.0 KiB
C++
//===- ExtractSliceFromReshapeUtils.cpp - Slice reshape rewrites ----------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements rewrites that replace slices of reshape results with
|
|
// aggregated slices of the reshape source.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
|
#include "mlir/Dialect/Arith/Utils/Utils.h"
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
#include "mlir/Dialect/Tensor/Transforms/TransformUtils.h"
|
|
#include "mlir/Dialect/Tensor/Transforms/Transforms.h"
|
|
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"
|
|
#include "mlir/Dialect/Utils/StaticValueUtils.h"
|
|
#include "mlir/IR/BuiltinTypes.h"
|
|
#include "mlir/IR/OpDefinition.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::affine;
|
|
using namespace mlir::tensor;
|
|
|
|
/// Get the dimension size of a value of RankedTensor type at the
|
|
static OpFoldResult getShapeDimSize(OpBuilder &b, Location loc,
|
|
Value rankedTensor, int64_t dimIdx) {
|
|
RankedTensorType tensorType = cast<RankedTensorType>(rankedTensor.getType());
|
|
if (!tensorType.isDynamicDim(dimIdx)) {
|
|
return b.getIndexAttr(tensorType.getDimSize(dimIdx));
|
|
}
|
|
Value idxValue = b.create<arith::ConstantIndexOp>(loc, dimIdx);
|
|
return b.createOrFold<tensor::DimOp>(loc, rankedTensor, idxValue);
|
|
}
|
|
|
|
/// Get all the dimension sizes of a value of RankedTensor type.
|
|
static SmallVector<OpFoldResult> getShapeDimSizes(OpBuilder &b, Location loc,
|
|
Value rankedTensor) {
|
|
SmallVector<OpFoldResult> dimSizes;
|
|
RankedTensorType tensorType = cast<RankedTensorType>(rankedTensor.getType());
|
|
for (unsigned i = 0; i < tensorType.getRank(); i++)
|
|
dimSizes.push_back(getShapeDimSize(b, loc, rankedTensor, i));
|
|
return dimSizes;
|
|
}
|
|
|
|
/// A tuple that represents (dimension number, dimension value).
|
|
using DimAndIndex = std::tuple<unsigned, Value>;
|
|
|
|
/// Transform `dimAndIndex` from the output index space of a (non-rank-reducing)
|
|
/// slice described by `sliceParams` into the input index space.
|
|
static DimAndIndex invertSliceIndexing(OpBuilder &b, Location loc,
|
|
ArrayRef<Range> sliceParams,
|
|
const DimAndIndex &dimAndIndex) {
|
|
AffineExpr d0, s0, s1;
|
|
bindDims(b.getContext(), d0);
|
|
bindSymbols(b.getContext(), s0, s1);
|
|
auto [dim, indexValue] = dimAndIndex;
|
|
assert(dim < sliceParams.size() && "slice should be non rank-reducing");
|
|
return std::make_pair(
|
|
dim,
|
|
affine::makeComposedAffineApply(
|
|
b, loc, s0 + d0 * s1,
|
|
{indexValue,
|
|
getValueOrCreateConstantIndexOp(b, loc, sliceParams[dim].offset),
|
|
getValueOrCreateConstantIndexOp(b, loc, sliceParams[dim].stride)}));
|
|
}
|
|
|
|
/// Transform `dimAndIndex` from the result tensor index space of a
|
|
/// CollapseShapeOp to the source tensor index space.
|
|
static ValueRange invertCollapseShapeIndexing(
|
|
OpBuilder &b, Location loc, ArrayRef<ReassociationIndices> reassociation,
|
|
ArrayRef<OpFoldResult> reshapeSourceShape, const DimAndIndex &dimAndIndex) {
|
|
const auto &[dim, indexValue] = dimAndIndex;
|
|
SmallVector<OpFoldResult> basis;
|
|
for (int64_t i : reassociation[dim])
|
|
basis.push_back(reshapeSourceShape[i]);
|
|
auto delinearized =
|
|
b.create<AffineDelinearizeIndexOp>(loc, indexValue, basis);
|
|
return delinearized->getResults();
|
|
}
|
|
|
|
FailureOr<ExtractSliceFromCollapseHelper>
|
|
tensor::ExtractSliceFromCollapseHelper::create(
|
|
OpBuilder &b, tensor::CollapseShapeOp collapseOp,
|
|
tensor::ExtractSliceOp extractOp) {
|
|
if (extractOp.getSource().getDefiningOp<tensor::CollapseShapeOp>() !=
|
|
collapseOp)
|
|
return failure();
|
|
SmallVector<Range> ranges;
|
|
ranges.reserve(extractOp.getSourceType().getRank());
|
|
for (const auto &[o, s, st] :
|
|
llvm::zip(extractOp.getMixedOffsets(), extractOp.getMixedSizes(),
|
|
extractOp.getMixedStrides())) {
|
|
ranges.push_back({o, s, st});
|
|
}
|
|
return ExtractSliceFromCollapseHelper::create(b, collapseOp, ranges);
|
|
}
|
|
|
|
FailureOr<ExtractSliceFromCollapseHelper>
|
|
tensor::ExtractSliceFromCollapseHelper::create(OpBuilder &b,
|
|
tensor::CollapseShapeOp op,
|
|
ArrayRef<Range> sliceParams) {
|
|
// Don't perform this pattern if the collapse op can be simplified by
|
|
// a rank-reducing extract slice.
|
|
if (succeeded(mlir::getSimplifyCollapseShapeWithRankReducingSliceInfo(
|
|
op.getSrcType(), op.getReassociationIndices())))
|
|
return failure();
|
|
|
|
// Materialize the output shape of the collapse_shape operation. This will
|
|
// create IR describing the output shape in terms of the input shape.
|
|
ReifiedRankedShapedTypeDims reifiedShapes;
|
|
if (failed(reifyResultShapes(b, op, reifiedShapes)))
|
|
return failure();
|
|
SmallVector<OpFoldResult> &collapseShapeOutputShape = reifiedShapes[0];
|
|
SmallVector<ReassociationIndices> reassociationIndices =
|
|
op.getReassociationIndices();
|
|
|
|
// Determine which of the CollapseShapeOp's result dimensions are sliced
|
|
// and/or linearized.
|
|
llvm::SmallBitVector linearizedDimensions =
|
|
getLinearizedDimensions(reassociationIndices);
|
|
llvm::SmallBitVector slicedDimensions =
|
|
getSlicedDimensions(collapseShapeOutputShape, sliceParams);
|
|
|
|
auto collapseShapeInputShape = getShapeDimSizes(b, op.getLoc(), op.getSrc());
|
|
|
|
SmallVector<Value> tileSizes;
|
|
for (unsigned i = 0; i < sliceParams.size(); i++) {
|
|
if (slicedDimensions[i] && linearizedDimensions[i])
|
|
tileSizes.push_back(
|
|
getValueOrCreateConstantIndexOp(b, op.getLoc(), sliceParams[i].size));
|
|
}
|
|
|
|
return ExtractSliceFromCollapseHelper(
|
|
op, collapseShapeInputShape, collapseShapeOutputShape, sliceParams,
|
|
linearizedDimensions, slicedDimensions, tileSizes);
|
|
}
|
|
|
|
std::pair<Value, SmallVector<Range>>
|
|
tensor::ExtractSliceFromCollapseHelper::emitLoopNestBody(
|
|
OpBuilder &builder, Location loc, ValueRange tileInductionVars) {
|
|
// Create the helper class for forming the slice parameters.
|
|
const SmallVector<ReassociationIndices> reassociationIndices =
|
|
collapseShapeOp.getReassociationIndices();
|
|
SliceFromCollapseHelper helper(reassociationIndices, collapseShapeInputShape,
|
|
collapseShapeOutputShape, sliceParams);
|
|
|
|
// Get the indices of the tiled dims (linearized by the collapse_shape
|
|
// and sliced by the extract_slice) invert the index spaces
|
|
// transformations.
|
|
SmallVector<ValueRange> multiIndices;
|
|
unsigned loopIdx = 0;
|
|
for (unsigned i = 0, e = linearizedDimensions.size(); i < e; i++) {
|
|
if (linearizedDimensions[i] && slicedDimensions[i]) {
|
|
DimAndIndex tb =
|
|
invertSliceIndexing(builder, loc, sliceParams,
|
|
std::make_tuple(i, tileInductionVars[loopIdx++]));
|
|
multiIndices.push_back(invertCollapseShapeIndexing(
|
|
builder, loc, reassociationIndices, collapseShapeInputShape, tb));
|
|
}
|
|
}
|
|
|
|
SmallVector<Range> extractParams =
|
|
helper.getExtractSliceParams(builder.getContext(), multiIndices);
|
|
|
|
Value subTileResult = builder.create<tensor::ExtractSliceOp>(
|
|
loc, collapseShapeOp.getSrc(), extractParams);
|
|
|
|
SmallVector<Range> insertParams =
|
|
helper.getInsertSliceParams(builder.getContext(), tileInductionVars);
|
|
|
|
// Collapse the dimensions of the source slice back down.
|
|
Value collapsedResult = builder.create<tensor::CollapseShapeOp>(
|
|
loc, subTileResult, reassociationIndices);
|
|
return std::make_pair(collapsedResult, insertParams);
|
|
}
|
|
|
|
FailureOr<Operation *>
|
|
tensor::simplifyCollapseShapeWithRankReducingExtractSlice(
|
|
tensor::CollapseShapeOp op, RewriterBase &rewriter) {
|
|
SmallVector<ReassociationIndices> reassociationIndices =
|
|
op.getReassociationIndices();
|
|
RankedTensorType sourceType = op.getSrcType();
|
|
FailureOr<CollapseShapeRankReducingSliceSimplificationInfo> info =
|
|
getSimplifyCollapseShapeWithRankReducingSliceInfo(sourceType,
|
|
reassociationIndices);
|
|
if (failed(info))
|
|
return failure();
|
|
|
|
// Create the rank-reducing extract slice op.
|
|
auto zero = rewriter.getIndexAttr(0);
|
|
auto one = rewriter.getIndexAttr(1);
|
|
SmallVector<OpFoldResult> offsets(sourceType.getRank(), zero);
|
|
SmallVector<OpFoldResult> sizes =
|
|
getShapeDimSizes(rewriter, op.getLoc(), op.getSrc());
|
|
SmallVector<OpFoldResult> strides(sourceType.getRank(), one);
|
|
auto sliceOp = rewriter.create<tensor::ExtractSliceOp>(
|
|
op.getLoc(), info->sliceResultType, op.getSrc(), offsets, sizes, strides);
|
|
|
|
if (!info->newReassociationIndices.has_value()) {
|
|
rewriter.replaceOp(op, sliceOp.getResult());
|
|
return sliceOp.getOperation();
|
|
}
|
|
|
|
return rewriter
|
|
.replaceOpWithNewOp<tensor::CollapseShapeOp>(
|
|
op, sliceOp.getResult(), *info->newReassociationIndices)
|
|
.getOperation();
|
|
}
|