#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
#include "mlir/Dialect/Vector/Utils/VectorUtils.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/PatternMatch.h"
#define DEBUG_TYPE "vector-bitcast-lowering"
using namespace mlir;
using namespace mlir::vector;
namespace {
class UnrollBitCastOp final : public OpRewritePattern<vector::BitCastOp> {
public:
UnrollBitCastOp(int64_t targetRank, MLIRContext *context,
PatternBenefit benefit = 1)
: OpRewritePattern(context, benefit), targetRank(targetRank) {};
LogicalResult matchAndRewrite(vector::BitCastOp op,
PatternRewriter &rewriter) const override {
VectorType resultType = op.getResultVectorType();
auto unrollIterator = vector::createUnrollIterator(resultType, targetRank);
if (!unrollIterator)
return failure();
auto unrollRank = unrollIterator->getRank();
ArrayRef<int64_t> shape = resultType.getShape().drop_front(unrollRank);
ArrayRef<bool> scalableDims =
resultType.getScalableDims().drop_front(unrollRank);
auto bitcastResType =
VectorType::get(shape, resultType.getElementType(), scalableDims);
Location loc = op.getLoc();
Value result = rewriter.create<arith::ConstantOp>(
loc, resultType, rewriter.getZeroAttr(resultType));
for (auto position : *unrollIterator) {
Value extract =
rewriter.create<vector::ExtractOp>(loc, op.getSource(), position);
Value bitcast =
rewriter.create<vector::BitCastOp>(loc, bitcastResType, extract);
result =
rewriter.create<vector::InsertOp>(loc, bitcast, result, position);
}
rewriter.replaceOp(op, result);
return success();
}
private:
int64_t targetRank = 1;
};
}
void mlir::vector::populateVectorBitCastLoweringPatterns(
RewritePatternSet &patterns, int64_t targetRank, PatternBenefit benefit) {
patterns.add<UnrollBitCastOp>(targetRank, patterns.getContext(), benefit);
}