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/Arithmetic/IR/Arithmetic.h"
22 #include "mlir/Dialect/Func/IR/FuncOps.h"
23 #include "mlir/Dialect/SCF/IR/SCF.h"
24 #include "mlir/Dialect/Shape/IR/Shape.h"
25 #include "mlir/Dialect/Tensor/IR/Tensor.h"
26 #include "mlir/Pass/Pass.h"
27
28 namespace mlir {
29 namespace mhlo {
30
31 namespace {
32
33 struct ChloLegalizeToHloPass
34 : public ChloLegalizeToHloPassBase<ChloLegalizeToHloPass> {
ChloLegalizeToHloPassmlir::mhlo::__anon428c1b160111::ChloLegalizeToHloPass35 explicit ChloLegalizeToHloPass(bool legalizeBroadcasts,
36 bool expandCompositions)
37 : ChloLegalizeToHloPassBase<
38 ChloLegalizeToHloPass>::ChloLegalizeToHloPassBase() {
39 this->legalize_broadcasts_ = legalizeBroadcasts;
40 this->expand_compositions_ = expandCompositions;
41 }
42
getDependentDialectsmlir::mhlo::__anon428c1b160111::ChloLegalizeToHloPass43 void getDependentDialects(DialectRegistry ®istry) const override {
44 registry.insert<mhlo::MhloDialect, shape::ShapeDialect, scf::SCFDialect>();
45 }
46
runOnOperationmlir::mhlo::__anon428c1b160111::ChloLegalizeToHloPass47 void runOnOperation() override {
48 ConversionTarget conversionTarget(getContext());
49 RewritePatternSet conversionPatterns(&getContext());
50 conversionTarget.addIllegalDialect<chlo::ChloDialect>();
51
52 // Consider the mhlo dialect legal for tests. Also add helper dialects
53 // that are needed by the patterns.
54 conversionTarget
55 .addLegalDialect<MhloDialect, mlir::arith::ArithmeticDialect,
56 mlir::func::FuncDialect, mlir::tensor::TensorDialect,
57 mlir::shape::ShapeDialect, mlir::scf::SCFDialect>();
58 conversionTarget.addLegalOp<chlo::MinimumBroadcastShapesOp>();
59
60 if (legalize_broadcasts_) {
61 chlo::populateChloBroadcastingPatterns(&getContext(),
62 &conversionPatterns);
63 }
64
65 if (expand_compositions_) {
66 chlo::populateDecomposeChloPatterns(&getContext(), &conversionPatterns);
67 } else {
68 conversionTarget
69 .addLegalOp<chlo::NextAfterOp, chlo::PolygammaOp, chlo::ZetaOp>();
70 }
71
72 if (failed(applyPartialConversion(getOperation(), conversionTarget,
73 std::move(conversionPatterns)))) {
74 return signalPassFailure();
75 }
76 }
77 };
78
79 } // namespace
80
createChloLegalizeToHloPass(bool legalizeBroadcasts,bool expandCompositions)81 std::unique_ptr<OperationPass<func::FuncOp>> createChloLegalizeToHloPass(
82 bool legalizeBroadcasts, bool expandCompositions) {
83 return std::make_unique<ChloLegalizeToHloPass>(legalizeBroadcasts,
84 expandCompositions);
85 }
86
87 } // namespace mhlo
88 } // namespace mlir
89