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
157 lines
5.8 KiB
C++
157 lines
5.8 KiB
C++
//===- LowerVectorBroadcast.cpp - Lower 'vector.broadcast' operation ------===//
|
|
//
|
|
// 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 target-independent rewrites and utilities to lower the
|
|
// 'vector.broadcast' operation.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
|
#include "mlir/Dialect/Arith/Utils/Utils.h"
|
|
#include "mlir/Dialect/Linalg/IR/Linalg.h"
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
|
#include "mlir/Dialect/SCF/IR/SCF.h"
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
#include "mlir/Dialect/Utils/IndexingUtils.h"
|
|
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
|
|
#include "mlir/Dialect/Vector/IR/VectorOps.h"
|
|
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
|
|
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h"
|
|
#include "mlir/Dialect/Vector/Utils/VectorUtils.h"
|
|
#include "mlir/IR/BuiltinAttributeInterfaces.h"
|
|
#include "mlir/IR/BuiltinTypes.h"
|
|
#include "mlir/IR/ImplicitLocOpBuilder.h"
|
|
#include "mlir/IR/Location.h"
|
|
#include "mlir/IR/Matchers.h"
|
|
#include "mlir/IR/PatternMatch.h"
|
|
#include "mlir/IR/TypeUtilities.h"
|
|
#include "mlir/Interfaces/VectorInterfaces.h"
|
|
#include "mlir/Support/LogicalResult.h"
|
|
|
|
#define DEBUG_TYPE "vector-broadcast-lowering"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::vector;
|
|
|
|
namespace {
|
|
/// Progressive lowering of BroadcastOp.
|
|
class BroadcastOpLowering : public OpRewritePattern<vector::BroadcastOp> {
|
|
public:
|
|
using OpRewritePattern::OpRewritePattern;
|
|
|
|
LogicalResult matchAndRewrite(vector::BroadcastOp op,
|
|
PatternRewriter &rewriter) const override {
|
|
auto loc = op.getLoc();
|
|
VectorType dstType = op.getResultVectorType();
|
|
VectorType srcType = dyn_cast<VectorType>(op.getSourceType());
|
|
Type eltType = dstType.getElementType();
|
|
|
|
// Scalar to any vector can use splat.
|
|
if (!srcType) {
|
|
rewriter.replaceOpWithNewOp<vector::SplatOp>(op, dstType, op.getSource());
|
|
return success();
|
|
}
|
|
|
|
// Determine rank of source and destination.
|
|
int64_t srcRank = srcType.getRank();
|
|
int64_t dstRank = dstType.getRank();
|
|
|
|
// Stretching scalar inside vector (e.g. vector<1xf32>) can use splat.
|
|
if (srcRank <= 1 && dstRank == 1) {
|
|
Value ext;
|
|
if (srcRank == 0)
|
|
ext = rewriter.create<vector::ExtractElementOp>(loc, op.getSource());
|
|
else
|
|
ext = rewriter.create<vector::ExtractOp>(loc, op.getSource(), 0);
|
|
rewriter.replaceOpWithNewOp<vector::SplatOp>(op, dstType, ext);
|
|
return success();
|
|
}
|
|
|
|
// Duplicate this rank.
|
|
// For example:
|
|
// %x = broadcast %y : k-D to n-D, k < n
|
|
// becomes:
|
|
// %b = broadcast %y : k-D to (n-1)-D
|
|
// %x = [%b,%b,%b,%b] : n-D
|
|
// becomes:
|
|
// %b = [%y,%y] : (n-1)-D
|
|
// %x = [%b,%b,%b,%b] : n-D
|
|
if (srcRank < dstRank) {
|
|
// Duplication.
|
|
VectorType resType =
|
|
VectorType::get(dstType.getShape().drop_front(), eltType);
|
|
Value bcst =
|
|
rewriter.create<vector::BroadcastOp>(loc, resType, op.getSource());
|
|
Value result = rewriter.create<arith::ConstantOp>(
|
|
loc, dstType, rewriter.getZeroAttr(dstType));
|
|
for (int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d)
|
|
result = rewriter.create<vector::InsertOp>(loc, bcst, result, d);
|
|
rewriter.replaceOp(op, result);
|
|
return success();
|
|
}
|
|
|
|
// Find non-matching dimension, if any.
|
|
assert(srcRank == dstRank);
|
|
int64_t m = -1;
|
|
for (int64_t r = 0; r < dstRank; r++)
|
|
if (srcType.getDimSize(r) != dstType.getDimSize(r)) {
|
|
m = r;
|
|
break;
|
|
}
|
|
|
|
// All trailing dimensions are the same. Simply pass through.
|
|
if (m == -1) {
|
|
rewriter.replaceOp(op, op.getSource());
|
|
return success();
|
|
}
|
|
|
|
// Any non-matching dimension forces a stretch along this rank.
|
|
// For example:
|
|
// %x = broadcast %y : vector<4x1x2xf32> to vector<4x2x2xf32>
|
|
// becomes:
|
|
// %a = broadcast %y[0] : vector<1x2xf32> to vector<2x2xf32>
|
|
// %b = broadcast %y[1] : vector<1x2xf32> to vector<2x2xf32>
|
|
// %c = broadcast %y[2] : vector<1x2xf32> to vector<2x2xf32>
|
|
// %d = broadcast %y[3] : vector<1x2xf32> to vector<2x2xf32>
|
|
// %x = [%a,%b,%c,%d]
|
|
// becomes:
|
|
// %u = broadcast %y[0][0] : vector<2xf32> to vector <2x2xf32>
|
|
// %v = broadcast %y[1][0] : vector<2xf32> to vector <2x2xf32>
|
|
// %a = [%u, %v]
|
|
// ..
|
|
// %x = [%a,%b,%c,%d]
|
|
VectorType resType =
|
|
VectorType::get(dstType.getShape().drop_front(), eltType);
|
|
Value result = rewriter.create<arith::ConstantOp>(
|
|
loc, dstType, rewriter.getZeroAttr(dstType));
|
|
if (m == 0) {
|
|
// Stetch at start.
|
|
Value ext = rewriter.create<vector::ExtractOp>(loc, op.getSource(), 0);
|
|
Value bcst = rewriter.create<vector::BroadcastOp>(loc, resType, ext);
|
|
for (int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d)
|
|
result = rewriter.create<vector::InsertOp>(loc, bcst, result, d);
|
|
} else {
|
|
// Stetch not at start.
|
|
for (int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d) {
|
|
Value ext = rewriter.create<vector::ExtractOp>(loc, op.getSource(), d);
|
|
Value bcst = rewriter.create<vector::BroadcastOp>(loc, resType, ext);
|
|
result = rewriter.create<vector::InsertOp>(loc, bcst, result, d);
|
|
}
|
|
}
|
|
rewriter.replaceOp(op, result);
|
|
return success();
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
void mlir::vector::populateVectorBroadcastLoweringPatterns(
|
|
RewritePatternSet &patterns, PatternBenefit benefit) {
|
|
patterns.add<BroadcastOpLowering>(patterns.getContext(), benefit);
|
|
}
|