2021-01-06 06:26:48 +08:00
|
|
|
//===- TosaToLinalgPass.cpp - Lowering Tosa to Linalg 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This transformation pass legalizes Tosa operations to the Linalg dialect.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
|
|
|
#include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h"
|
|
|
|
#include "mlir/Dialect/Linalg/IR/LinalgOps.h"
|
2021-02-12 17:34:42 +08:00
|
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
2021-03-18 04:41:53 +08:00
|
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
2021-01-06 06:26:48 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
|
|
|
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
|
|
|
#include "mlir/Dialect/Tosa/Transforms/PassDetail.h"
|
|
|
|
#include "mlir/Dialect/Tosa/Transforms/Passes.h"
|
|
|
|
#include "mlir/Dialect/Tosa/Utils/QuantUtils.h"
|
|
|
|
#include "mlir/IR/PatternMatch.h"
|
|
|
|
#include "mlir/Pass/PassManager.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct TosaToLinalgOnTensors
|
|
|
|
: public TosaToLinalgOnTensorsBase<TosaToLinalgOnTensors> {
|
|
|
|
public:
|
|
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
2021-03-18 04:41:53 +08:00
|
|
|
registry.insert<linalg::LinalgDialect, math::MathDialect,
|
|
|
|
memref::MemRefDialect, StandardOpsDialect>();
|
2021-01-06 06:26:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runOnFunction() override {
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet patterns(&getContext());
|
2021-01-06 06:26:48 +08:00
|
|
|
ConversionTarget target(getContext());
|
2021-03-18 04:41:53 +08:00
|
|
|
target.addLegalDialect<linalg::LinalgDialect, memref::MemRefDialect,
|
|
|
|
StandardOpsDialect>();
|
2021-01-06 06:26:48 +08:00
|
|
|
target.addIllegalDialect<tosa::TosaDialect>();
|
2021-03-19 07:14:05 +08:00
|
|
|
|
|
|
|
// Not every TOSA op can be legalized to linalg.
|
|
|
|
target.addLegalOp<tosa::ApplyScaleOp>();
|
|
|
|
target.addLegalOp<tosa::IfOp>();
|
|
|
|
target.addLegalOp<tosa::ConstOp>();
|
|
|
|
target.addLegalOp<tosa::WhileOp>();
|
|
|
|
|
2021-01-06 06:26:48 +08:00
|
|
|
target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
|
|
|
|
|
|
|
|
FuncOp func = getFunction();
|
2021-03-21 07:29:41 +08:00
|
|
|
mlir::tosa::populateTosaToLinalgOnTensorsConversionPatterns(&patterns);
|
2021-01-06 06:26:48 +08:00
|
|
|
if (failed(applyFullConversion(func, target, std::move(patterns))))
|
|
|
|
signalPassFailure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<Pass> mlir::tosa::createTosaToLinalgOnTensors() {
|
|
|
|
return std::make_unique<TosaToLinalgOnTensors>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlir::tosa::addTosaToLinalgOnTensorsPasses(OpPassManager &pm) {
|
|
|
|
pm.addNestedPass<FuncOp>(createTosaMakeBroadcastablePass());
|
|
|
|
pm.addNestedPass<FuncOp>(createTosaToLinalgOnTensors());
|
|
|
|
}
|