2020-05-15 12:22:21 +08:00
|
|
|
//===- LinalgToStandard.cpp - conversion from Linalg to Standard dialect --===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Conversion/LinalgToStandard/LinalgToStandard.h"
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
|
|
|
#include "mlir/Dialect/Linalg/IR/LinalgOps.h"
|
2020-10-09 22:31:52 +08:00
|
|
|
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
|
2021-02-10 20:53:11 +08:00
|
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
2020-05-15 12:22:21 +08:00
|
|
|
#include "mlir/Dialect/SCF/SCF.h"
|
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::linalg;
|
|
|
|
|
|
|
|
/// Helper function to extract the operand types that are passed to the
|
|
|
|
/// generated CallOp. MemRefTypes have their layout canonicalized since the
|
|
|
|
/// information is not used in signature generation.
|
|
|
|
/// Note that static size information is not modified.
|
|
|
|
static SmallVector<Type, 4> extractOperandTypes(Operation *op) {
|
|
|
|
SmallVector<Type, 4> result;
|
|
|
|
result.reserve(op->getNumOperands());
|
|
|
|
for (auto type : op->getOperandTypes()) {
|
|
|
|
// The underlying descriptor type (e.g. LLVM) does not have layout
|
|
|
|
// information. Canonicalizing the type at the level of std when going into
|
|
|
|
// a library call avoids needing to introduce DialectCastOp.
|
|
|
|
if (auto memrefType = type.dyn_cast<MemRefType>())
|
|
|
|
result.push_back(eraseStridedLayout(memrefType));
|
|
|
|
else
|
|
|
|
result.push_back(type);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a SymbolRefAttr containing the library function name for the LinalgOp.
|
|
|
|
// If the library function does not exist, insert a declaration.
|
|
|
|
static FlatSymbolRefAttr getLibraryCallSymbolRef(Operation *op,
|
|
|
|
PatternRewriter &rewriter) {
|
|
|
|
auto linalgOp = cast<LinalgOp>(op);
|
|
|
|
auto fnName = linalgOp.getLibraryCallName();
|
|
|
|
if (fnName.empty()) {
|
|
|
|
op->emitWarning("No library call defined for: ") << *op;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// fnName is a dynamic std::string, unique it via a SymbolRefAttr.
|
|
|
|
FlatSymbolRefAttr fnNameAttr = rewriter.getSymbolRefAttr(fnName);
|
|
|
|
auto module = op->getParentOfType<ModuleOp>();
|
|
|
|
if (module.lookupSymbol(fnName)) {
|
|
|
|
return fnNameAttr;
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:31:52 +08:00
|
|
|
SmallVector<Type, 4> inputTypes(extractOperandTypes(op));
|
2020-05-15 12:22:21 +08:00
|
|
|
assert(op->getNumResults() == 0 &&
|
|
|
|
"Library call for linalg operation can be generated only for ops that "
|
|
|
|
"have void return types");
|
2020-12-18 04:24:45 +08:00
|
|
|
auto libFnType = rewriter.getFunctionType(inputTypes, {});
|
2020-05-15 12:22:21 +08:00
|
|
|
|
|
|
|
OpBuilder::InsertionGuard guard(rewriter);
|
|
|
|
// Insert before module terminator.
|
|
|
|
rewriter.setInsertionPoint(module.getBody(),
|
|
|
|
std::prev(module.getBody()->end()));
|
|
|
|
FuncOp funcOp =
|
2020-07-08 07:15:44 +08:00
|
|
|
rewriter.create<FuncOp>(op->getLoc(), fnNameAttr.getValue(), libFnType);
|
2020-05-15 12:22:21 +08:00
|
|
|
// Insert a function attribute that will trigger the emission of the
|
|
|
|
// corresponding `_mlir_ciface_xxx` interface so that external libraries see
|
|
|
|
// a normalized ABI. This interface is added during std to llvm conversion.
|
2020-12-12 17:50:41 +08:00
|
|
|
funcOp->setAttr("llvm.emit_c_interface", UnitAttr::get(op->getContext()));
|
2020-11-14 05:04:53 +08:00
|
|
|
funcOp.setPrivate();
|
2020-05-15 12:22:21 +08:00
|
|
|
return fnNameAttr;
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:31:52 +08:00
|
|
|
static SmallVector<Value, 4>
|
2020-05-15 12:22:21 +08:00
|
|
|
createTypeCanonicalizedMemRefOperands(OpBuilder &b, Location loc,
|
|
|
|
ValueRange operands) {
|
|
|
|
SmallVector<Value, 4> res;
|
|
|
|
res.reserve(operands.size());
|
|
|
|
for (auto op : operands) {
|
|
|
|
auto memrefType = op.getType().dyn_cast<MemRefType>();
|
|
|
|
if (!memrefType) {
|
|
|
|
res.push_back(op);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Value cast =
|
2021-02-10 20:53:11 +08:00
|
|
|
b.create<memref::CastOp>(loc, eraseStridedLayout(memrefType), op);
|
2020-05-15 12:22:21 +08:00
|
|
|
res.push_back(cast);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:31:52 +08:00
|
|
|
LogicalResult mlir::linalg::LinalgOpToLibraryCallRewrite::matchAndRewrite(
|
2021-03-24 04:44:14 +08:00
|
|
|
LinalgOp op, PatternRewriter &rewriter) const {
|
2020-10-09 22:31:52 +08:00
|
|
|
// Only LinalgOp for which there is no specialized pattern go through this.
|
2021-05-12 19:34:13 +08:00
|
|
|
if (isa<CopyOp>(op))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Canonicalize indexed generic operations before library call conversion.
|
|
|
|
if (isa<IndexedGenericOp>(op))
|
2020-10-09 22:31:52 +08:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
auto libraryCallName = getLibraryCallSymbolRef(op, rewriter);
|
|
|
|
if (!libraryCallName)
|
|
|
|
return failure();
|
|
|
|
|
2021-04-19 20:23:11 +08:00
|
|
|
// TODO: Add support for more complex library call signatures that include
|
|
|
|
// indices or captured values.
|
2020-10-09 22:31:52 +08:00
|
|
|
rewriter.replaceOpWithNewOp<mlir::CallOp>(
|
|
|
|
op, libraryCallName.getValue(), TypeRange(),
|
|
|
|
createTypeCanonicalizedMemRefOperands(rewriter, op->getLoc(),
|
|
|
|
op->getOperands()));
|
|
|
|
return success();
|
|
|
|
}
|
2020-05-15 12:22:21 +08:00
|
|
|
|
2020-10-09 22:31:52 +08:00
|
|
|
LogicalResult mlir::linalg::CopyOpToLibraryCallRewrite::matchAndRewrite(
|
|
|
|
CopyOp op, PatternRewriter &rewriter) const {
|
|
|
|
auto inputPerm = op.inputPermutation();
|
|
|
|
if (inputPerm.hasValue() && !inputPerm->isIdentity())
|
|
|
|
return failure();
|
|
|
|
auto outputPerm = op.outputPermutation();
|
|
|
|
if (outputPerm.hasValue() && !outputPerm->isIdentity())
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
auto libraryCallName = getLibraryCallSymbolRef(op, rewriter);
|
|
|
|
if (!libraryCallName)
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<mlir::CallOp>(
|
|
|
|
op, libraryCallName.getValue(), TypeRange(),
|
|
|
|
createTypeCanonicalizedMemRefOperands(rewriter, op.getLoc(),
|
|
|
|
op.getOperands()));
|
|
|
|
return success();
|
|
|
|
}
|
2020-05-15 12:22:21 +08:00
|
|
|
|
2020-10-09 22:31:52 +08:00
|
|
|
LogicalResult mlir::linalg::CopyTransposeRewrite::matchAndRewrite(
|
|
|
|
CopyOp op, PatternRewriter &rewriter) const {
|
|
|
|
Value in = op.input(), out = op.output();
|
|
|
|
|
|
|
|
// If either inputPerm or outputPerm are non-identities, insert transposes.
|
|
|
|
auto inputPerm = op.inputPermutation();
|
|
|
|
if (inputPerm.hasValue() && !inputPerm->isIdentity())
|
2021-02-10 20:53:11 +08:00
|
|
|
in = rewriter.create<memref::TransposeOp>(op.getLoc(), in,
|
|
|
|
AffineMapAttr::get(*inputPerm));
|
2020-10-09 22:31:52 +08:00
|
|
|
auto outputPerm = op.outputPermutation();
|
|
|
|
if (outputPerm.hasValue() && !outputPerm->isIdentity())
|
2021-02-10 20:53:11 +08:00
|
|
|
out = rewriter.create<memref::TransposeOp>(op.getLoc(), out,
|
|
|
|
AffineMapAttr::get(*outputPerm));
|
2020-10-09 22:31:52 +08:00
|
|
|
|
|
|
|
// If nothing was transposed, fail and let the conversion kick in.
|
|
|
|
if (in == op.input() && out == op.output())
|
|
|
|
return failure();
|
|
|
|
|
2021-02-13 02:24:39 +08:00
|
|
|
auto libraryCallName = getLibraryCallSymbolRef(op, rewriter);
|
|
|
|
if (!libraryCallName)
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
rewriter.replaceOpWithNewOp<mlir::CallOp>(
|
|
|
|
op, libraryCallName.getValue(), TypeRange(),
|
|
|
|
createTypeCanonicalizedMemRefOperands(rewriter, op.getLoc(), {in, out}));
|
2020-10-09 22:31:52 +08:00
|
|
|
return success();
|
|
|
|
}
|
2020-05-15 12:22:21 +08:00
|
|
|
|
|
|
|
/// Populate the given list with patterns that convert from Linalg to Standard.
|
2020-10-09 22:31:52 +08:00
|
|
|
void mlir::linalg::populateLinalgToStandardConversionPatterns(
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet &patterns) {
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: ConvOp conversion needs to export a descriptor with relevant
|
2020-05-15 12:22:21 +08:00
|
|
|
// attribute values such as kernel striding and dilation.
|
|
|
|
// clang-format off
|
2021-03-23 07:58:34 +08:00
|
|
|
patterns.add<
|
2020-10-09 22:31:52 +08:00
|
|
|
CopyOpToLibraryCallRewrite,
|
|
|
|
CopyTransposeRewrite,
|
2021-03-24 04:44:14 +08:00
|
|
|
LinalgOpToLibraryCallRewrite>(patterns.getContext());
|
2020-05-15 12:22:21 +08:00
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ConvertLinalgToStandardPass
|
|
|
|
: public ConvertLinalgToStandardBase<ConvertLinalgToStandardPass> {
|
|
|
|
void runOnOperation() override;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void ConvertLinalgToStandardPass::runOnOperation() {
|
|
|
|
auto module = getOperation();
|
|
|
|
ConversionTarget target(getContext());
|
2021-02-10 20:53:11 +08:00
|
|
|
target.addLegalDialect<AffineDialect, memref::MemRefDialect, scf::SCFDialect,
|
|
|
|
StandardOpsDialect>();
|
2021-03-12 07:58:02 +08:00
|
|
|
target.addLegalOp<ModuleOp, FuncOp, ReturnOp>();
|
2020-10-01 01:19:04 +08:00
|
|
|
target.addLegalOp<linalg::ReshapeOp, linalg::RangeOp>();
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet patterns(&getContext());
|
2021-03-21 07:29:41 +08:00
|
|
|
populateLinalgToStandardConversionPatterns(patterns);
|
2020-10-27 08:25:01 +08:00
|
|
|
if (failed(applyFullConversion(module, target, std::move(patterns))))
|
2020-05-15 12:22:21 +08:00
|
|
|
signalPassFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
mlir::createConvertLinalgToStandardPass() {
|
|
|
|
return std::make_unique<ConvertLinalgToStandardPass>();
|
|
|
|
}
|