This patch introduces pattern rewrites for reducing the rank of named linalg contraction ops with unit spatial dim(s) to other named contraction ops. For example `linalg.batch_matmul` with batch size 1 -> `linalg.matmul` and `linalg.matmul` with unit LHS spatial dim -> `linalg.vecmat`, etc. These patterns don't support reducing the rank along reduction dimension as those don't convert to other named contraction ops.
68 lines
2.3 KiB
C++
68 lines
2.3 KiB
C++
//===- TestLinalgRankReduceContractionOps.cpp -----------------------------===//
|
|
//
|
|
// 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 a pass for testing rank reduing patterns for named
|
|
// contraction ops with unit dims.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
|
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Pass/PassManager.h"
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
|
|
struct TestLinalgRankReduceContractionOps
|
|
: public PassWrapper<TestLinalgRankReduceContractionOps,
|
|
OperationPass<func::FuncOp>> {
|
|
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
|
|
TestLinalgRankReduceContractionOps)
|
|
|
|
TestLinalgRankReduceContractionOps() = default;
|
|
TestLinalgRankReduceContractionOps(
|
|
const TestLinalgRankReduceContractionOps &pass)
|
|
: PassWrapper(pass) {}
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
|
registry.insert<affine::AffineDialect, linalg::LinalgDialect,
|
|
memref::MemRefDialect, tensor::TensorDialect>();
|
|
}
|
|
StringRef getArgument() const final {
|
|
return "test-linalg-rank-reduce-contraction-ops";
|
|
}
|
|
StringRef getDescription() const final {
|
|
return "Test Linalg rank reduce contraction ops with unit dims";
|
|
}
|
|
|
|
void runOnOperation() override {
|
|
MLIRContext *context = &this->getContext();
|
|
func::FuncOp funcOp = this->getOperation();
|
|
|
|
RewritePatternSet patterns(context);
|
|
linalg::populateContractionOpRankReducingPatterns(patterns);
|
|
if (failed(applyPatternsAndFoldGreedily(funcOp.getBody(),
|
|
std::move(patterns))))
|
|
return signalPassFailure();
|
|
return;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
namespace mlir {
|
|
namespace test {
|
|
void registerTestLinalgRankReduceContractionOps() {
|
|
PassRegistration<TestLinalgRankReduceContractionOps>();
|
|
}
|
|
} // namespace test
|
|
} // namespace mlir
|