2022-03-10 08:44:22 +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"
|
|
|
|
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/Transforms/Passes.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
|
|
|
|
|
|
|
namespace {
|
2022-12-14 00:25:41 +08:00
|
|
|
template <typename CalculateOp>
|
|
|
|
class DropCalculateOp : public OpConversionPattern<CalculateOp> {
|
2022-03-10 08:44:22 +08:00
|
|
|
public:
|
2022-12-14 00:25:41 +08:00
|
|
|
using OpConversionPattern<CalculateOp>::OpConversionPattern;
|
2022-03-10 08:44:22 +08:00
|
|
|
LogicalResult
|
2022-12-14 00:25:41 +08:00
|
|
|
matchAndRewrite(CalculateOp op, typename CalculateOp::Adaptor adaptor,
|
2022-03-10 08:44:22 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Block *block = &op.getBody().front();
|
2022-03-10 08:44:22 +08:00
|
|
|
Operation *terminator = block->getTerminator();
|
|
|
|
ValueRange results = terminator->getOperands();
|
2023-03-21 01:31:05 +08:00
|
|
|
rewriter.inlineBlockBefore(block, op);
|
2022-03-10 08:44:22 +08:00
|
|
|
rewriter.replaceOp(op, results);
|
|
|
|
rewriter.eraseOp(terminator);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2022-12-14 00:25:41 +08:00
|
|
|
class DropAbstractInterpCalculationsPass
|
|
|
|
: public DropAbstractInterpCalculationsBase<
|
|
|
|
DropAbstractInterpCalculationsPass> {
|
2022-03-10 08:44:22 +08:00
|
|
|
void runOnOperation() override {
|
|
|
|
MLIRContext *context = &getContext();
|
|
|
|
|
|
|
|
RewritePatternSet patterns(context);
|
2022-12-14 00:25:41 +08:00
|
|
|
patterns.insert<DropCalculateOp<DtypeCalculateOp>>(context);
|
|
|
|
patterns.insert<DropCalculateOp<ShapeCalculateOp>>(context);
|
2022-03-10 08:44:22 +08:00
|
|
|
ConversionTarget target(*context);
|
2022-06-25 22:27:47 +08:00
|
|
|
target.addLegalDialect<Torch::TorchDialect>();
|
2022-12-14 00:25:41 +08:00
|
|
|
target.addIllegalOp<DtypeCalculateOp, ShapeCalculateOp>();
|
2022-04-27 03:27:51 +08:00
|
|
|
target.addLegalOp<func::FuncOp>();
|
2022-03-10 08:44:22 +08:00
|
|
|
|
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
|
|
std::move(patterns)))) {
|
|
|
|
signalPassFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-04-27 03:27:51 +08:00
|
|
|
std::unique_ptr<OperationPass<func::FuncOp>>
|
2022-12-14 00:25:41 +08:00
|
|
|
mlir::torch::Torch::createDropAbstractInterpCalculationsPass() {
|
|
|
|
return std::make_unique<DropAbstractInterpCalculationsPass>();
|
2022-03-10 08:44:22 +08:00
|
|
|
}
|