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::__anona9c23a880111::ChloLegalizeToHloPass34 explicit ChloLegalizeToHloPass(bool legalize_broadcasts,
35 bool expand_compositions)
36 : ChloLegalizeToHloPassBase<
37 ChloLegalizeToHloPass>::ChloLegalizeToHloPassBase() {
38 this->legalize_broadcasts_ = legalize_broadcasts;
39 this->expand_compositions_ = expand_compositions;
40 }
41
getDependentDialectsmlir::mhlo::__anona9c23a880111::ChloLegalizeToHloPass42 void getDependentDialects(DialectRegistry ®istry) const override {
43 registry.insert<mhlo::MhloDialect, shape::ShapeDialect, scf::SCFDialect>();
44 }
45
runOnFunctionmlir::mhlo::__anona9c23a880111::ChloLegalizeToHloPass46 void runOnFunction() override {
47 ConversionTarget conversionTarget(getContext());
48 OwningRewritePatternList conversionPatterns(&getContext());
49 conversionTarget.addIllegalDialect<chlo::HloClientDialect>();
50
51 // Consider the mhlo dialect legal for tests. Also add helper dialects
52 // that are needed by the patterns.
53 conversionTarget.addLegalDialect<
54 MhloDialect, mlir::StandardOpsDialect, mlir::tensor::TensorDialect,
55 mlir::shape::ShapeDialect, mlir::scf::SCFDialect>();
56 conversionTarget.addLegalOp<chlo::MinimumBroadcastShapesOp>();
57
58 if (legalize_broadcasts_) {
59 chlo::PopulateChloBroadcastingPatterns(&getContext(),
60 &conversionPatterns);
61 }
62
63 if (expand_compositions_) {
64 chlo::PopulateDecomposeChloPatterns(&getContext(), &conversionPatterns);
65 } else {
66 conversionTarget
67 .addLegalOp<chlo::NextAfterOp, chlo::PolygammaOp, chlo::ZetaOp>();
68 }
69
70 if (failed(applyPartialConversion(getOperation(), conversionTarget,
71 std::move(conversionPatterns)))) {
72 return signalPassFailure();
73 }
74 }
75 };
76
77 } // namespace
78
createChloLegalizeToHloPass(bool legalize_broadcasts,bool expand_compositions)79 std::unique_ptr<FunctionPass> createChloLegalizeToHloPass(
80 bool legalize_broadcasts, bool expand_compositions) {
81 return std::make_unique<ChloLegalizeToHloPass>(legalize_broadcasts,
82 expand_compositions);
83 }
84
85 } // namespace mhlo
86 } // namespace mlir
87