1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
17 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
18 #include "mlir-hlo/Dialect/mhlo/transforms/PassDetail.h"
19 #include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
20 #include "mlir-hlo/Dialect/mhlo/transforms/rewriters.h"
21 #include "mlir/Dialect/SCF/SCF.h"
22 #include "mlir/Dialect/Shape/IR/Shape.h"
23 #include "mlir/Dialect/StandardOps/IR/Ops.h"
24 #include "mlir/Dialect/Tensor/IR/Tensor.h"
25 #include "mlir/Pass/Pass.h"
26
27 namespace mlir {
28 namespace mhlo {
29
30 namespace {
31
32 struct ChloLegalizeToHloPass
33 : public ChloLegalizeToHloPassBase<ChloLegalizeToHloPass> {
ChloLegalizeToHloPassmlir::mhlo::__anond384fddc0111::ChloLegalizeToHloPass34 explicit ChloLegalizeToHloPass(bool broadcast_only)
35 : ChloLegalizeToHloPassBase<
36 ChloLegalizeToHloPass>::ChloLegalizeToHloPassBase() {
37 this->broadcast_only_ = broadcast_only;
38 }
39
getDependentDialectsmlir::mhlo::__anond384fddc0111::ChloLegalizeToHloPass40 void getDependentDialects(DialectRegistry ®istry) const override {
41 registry.insert<mhlo::MhloDialect, shape::ShapeDialect, scf::SCFDialect>();
42 }
43
runOnFunctionmlir::mhlo::__anond384fddc0111::ChloLegalizeToHloPass44 void runOnFunction() override {
45 ConversionTarget conversionTarget(getContext());
46 OwningRewritePatternList conversionPatterns;
47 conversionTarget.addIllegalDialect<chlo::HloClientDialect>();
48
49 // Consider the mhlo dialect legal for tests. Also add helper dialects
50 // that are needed by the patterns.
51 conversionTarget.addLegalDialect<
52 MhloDialect, mlir::StandardOpsDialect, mlir::tensor::TensorDialect,
53 mlir::shape::ShapeDialect, mlir::scf::SCFDialect>();
54
55 if (broadcast_only_) {
56 chlo::PopulateChloBroadcastingPatterns(&getContext(),
57 &conversionPatterns);
58 conversionTarget.addLegalOp<chlo::ZetaOp, chlo::PolygammaOp>();
59 } else {
60 chlo::PopulateLegalizeChloToHloPatterns(&getContext(),
61 &conversionPatterns);
62 }
63
64 if (failed(applyPartialConversion(getOperation(), conversionTarget,
65 std::move(conversionPatterns)))) {
66 return signalPassFailure();
67 }
68 }
69 };
70
71 } // namespace
72
createChloLegalizeToHloPass(bool broadcast_only)73 std::unique_ptr<FunctionPass> createChloLegalizeToHloPass(bool broadcast_only) {
74 return std::make_unique<ChloLegalizeToHloPass>(broadcast_only);
75 }
76
77 } // namespace mhlo
78 } // namespace mlir
79