2021-10-08 10:07:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PassDetail.h"
|
|
|
|
|
2022-10-05 21:28:06 +08:00
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
2022-03-16 18:44:23 +08:00
|
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
2021-10-08 10:07:03 +08:00
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
|
|
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "torch-mlir/Dialect/TorchConversion/Transforms/Passes.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::TorchConversion;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VerifyTosaBackendContractPass
|
|
|
|
: public VerifyTosaBackendContractBase<VerifyTosaBackendContractPass> {
|
|
|
|
void runOnOperation() override {
|
|
|
|
MLIRContext *context = &getContext();
|
|
|
|
auto module = getOperation();
|
|
|
|
TypeConverter converter;
|
|
|
|
converter.addConversion([](TensorType type) -> Type {
|
|
|
|
if (BaseMemRefType::isValidElementType(type.getElementType()))
|
|
|
|
return type;
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto opHasLegalTypes = [&](Operation *op) { return converter.isLegal(op); };
|
|
|
|
|
|
|
|
ConversionTarget target(*context);
|
|
|
|
|
|
|
|
// Structural operations.
|
2022-04-27 03:27:51 +08:00
|
|
|
target.addDynamicallyLegalOp<ModuleOp, func::FuncOp, func::ReturnOp>(
|
2022-03-16 18:44:23 +08:00
|
|
|
opHasLegalTypes);
|
2021-10-08 10:07:03 +08:00
|
|
|
// Basic scalar operations.
|
|
|
|
target.addLegalDialect<tosa::TosaDialect>();
|
2021-12-16 03:01:01 +08:00
|
|
|
target.addDynamicallyLegalOp<tensor::CastOp>(opHasLegalTypes);
|
|
|
|
target.addDynamicallyLegalOp<arith::ExtSIOp>(opHasLegalTypes);
|
2022-01-12 04:49:17 +08:00
|
|
|
target.addDynamicallyLegalOp<arith::ConstantOp>(opHasLegalTypes);
|
2021-10-08 10:07:03 +08:00
|
|
|
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
if (failed(applyFullConversion(module, target, std::move(patterns)))) {
|
|
|
|
// We avoid `module.emitError()` so that mlir-print-op-on-diagnostics
|
|
|
|
// doesn't unnecessarily spew out the entire module.
|
|
|
|
emitError(module.getLoc())
|
|
|
|
<< "Module does not conform to the TOSA backend contract. "
|
|
|
|
"See dialect conversion legality information above.";
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
mlir::torch::TorchConversion::createVerifyTosaBackendContractPass() {
|
|
|
|
return std::make_unique<VerifyTosaBackendContractPass>();
|
|
|
|
}
|