2022-09-01 17:08:17 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2023-02-02 21:29:47 +08:00
|
|
|
#ifdef TORCH_MLIR_ENABLE_STABLEHLO
|
2022-09-01 17:08:17 +08:00
|
|
|
#include "PassDetail.h"
|
|
|
|
|
2022-10-05 21:28:06 +08:00
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
2022-09-01 17:08:17 +08:00
|
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
2024-05-16 11:03:43 +08:00
|
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
2022-09-23 20:39:15 +08:00
|
|
|
#include "mlir/Dialect/Shape/IR/Shape.h"
|
2022-09-01 17:08:17 +08:00
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "stablehlo/dialect/ChloOps.h"
|
2023-02-02 21:29:47 +08:00
|
|
|
#include "stablehlo/dialect/StablehloOps.h"
|
2022-09-01 17:08:17 +08:00
|
|
|
#include "torch-mlir/Dialect/TorchConversion/Transforms/Passes.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::TorchConversion;
|
|
|
|
|
|
|
|
namespace {
|
2023-02-02 21:29:47 +08:00
|
|
|
class VerifyStablehloBackendContractPass
|
|
|
|
: public VerifyStablehloBackendContractBase<
|
|
|
|
VerifyStablehloBackendContractPass> {
|
2022-09-01 17:08:17 +08:00
|
|
|
void runOnOperation() override {
|
|
|
|
TypeConverter converter;
|
|
|
|
converter.addConversion([](Type type) -> Type {
|
|
|
|
auto elemTy = type;
|
2023-02-02 21:29:47 +08:00
|
|
|
if (isa<TensorType>(type))
|
2024-04-11 21:47:35 +08:00
|
|
|
elemTy = cast<TensorType>(type).getElementType();
|
2022-09-01 17:08:17 +08:00
|
|
|
if (BaseMemRefType::isValidElementType(elemTy))
|
|
|
|
return type;
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto opHasLegalTypes = [&](Operation *op) { return converter.isLegal(op); };
|
|
|
|
|
2023-02-02 21:29:47 +08:00
|
|
|
MLIRContext *context = &getContext();
|
2022-09-01 17:08:17 +08:00
|
|
|
ConversionTarget target(*context);
|
|
|
|
|
|
|
|
// Structural operations.
|
2024-01-30 01:59:33 +08:00
|
|
|
target.addDynamicallyLegalOp<ModuleOp, func::FuncOp, func::ReturnOp>(
|
|
|
|
opHasLegalTypes);
|
2022-09-23 20:39:15 +08:00
|
|
|
|
2022-09-01 17:08:17 +08:00
|
|
|
target.addLegalDialect<chlo::ChloDialect>();
|
2023-02-02 21:29:47 +08:00
|
|
|
target.addLegalDialect<stablehlo::StablehloDialect>();
|
2022-09-01 17:08:17 +08:00
|
|
|
target.addLegalDialect<tensor::TensorDialect>();
|
2022-10-05 21:28:06 +08:00
|
|
|
target.addLegalDialect<arith::ArithDialect>();
|
2024-05-16 11:03:43 +08:00
|
|
|
target.addLegalDialect<math::MathDialect>();
|
|
|
|
target.addLegalDialect<shape::ShapeDialect>();
|
|
|
|
auto moduleOp = getOperation();
|
|
|
|
RewritePatternSet patterns(context);
|
|
|
|
if (failed(applyFullConversion(moduleOp, target, std::move(patterns)))) {
|
|
|
|
emitError(moduleOp.getLoc())
|
|
|
|
<< "Module does not conform to the Stablehlo backend contract. "
|
|
|
|
"See dialect conversion legality information above.";
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
2022-09-01 17:08:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
2023-02-02 21:29:47 +08:00
|
|
|
mlir::torch::TorchConversion::createVerifyStablehloBackendContractPass() {
|
|
|
|
return std::make_unique<VerifyStablehloBackendContractPass>();
|
2022-09-01 17:08:17 +08:00
|
|
|
}
|
2023-02-02 21:29:47 +08:00
|
|
|
#endif // TORCH_MLIR_ENABLE_STABLEHLO
|