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 #ifndef TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_TRANSFORMS_MAP_CHLO_TO_MHLO_OP_H_
17 #define TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_TRANSFORMS_MAP_CHLO_TO_MHLO_OP_H_
18 
19 #include <type_traits>
20 
21 #include "mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
22 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
23 #include "mlir/IR/PatternMatch.h"
24 
25 namespace mlir {
26 namespace chlo {
27 
28 struct HloComplexAdaptor {
CreateOpHloComplexAdaptor29   static mhlo::ComplexOp CreateOp(BroadcastComplexOp from_op, Type result_type,
30                                   Value broadcasted_lhs, Value broadcasted_rhs,
31                                   OpBuilder &builder) {
32     return builder.create<mhlo::ComplexOp>(from_op.getLoc(), result_type,
33                                            broadcasted_lhs, broadcasted_rhs);
34   }
35 };
36 template <typename FromOpTy, typename ToOpTy>
37 struct HloBinaryElementwiseAdaptor {
CreateOpHloBinaryElementwiseAdaptor38   static ToOpTy CreateOp(FromOpTy from_op, Type result_type,
39                          Value broadcasted_lhs, Value broadcasted_rhs,
40                          OpBuilder &builder) {
41     return builder.create<ToOpTy>(from_op.getLoc(), result_type,
42                                   broadcasted_lhs, broadcasted_rhs);
43   }
44 };
45 struct HloCompareAdaptor {
CreateOpHloCompareAdaptor46   static mhlo::CompareOp CreateOp(BroadcastCompareOp from_op, Type result_type,
47                                   Value broadcasted_lhs, Value broadcasted_rhs,
48                                   OpBuilder &builder) {
49     return builder.create<mhlo::CompareOp>(
50         from_op.getLoc(), result_type, broadcasted_lhs, broadcasted_rhs,
51         from_op.comparison_direction(), from_op.compare_typeAttr());
52   }
53 };
54 
55 // Populate a pattern for each Broadcasting CHlo op. This requires the pattern
56 // to take a ChloOpTy, NonBroadcastingOpTy, and an Adaptor as templated values.
57 template <template <typename, typename, typename> class Pattern,
58           typename... ConstructorArgs>
PopulateForBroadcastingBinaryOp(MLIRContext * context,OwningRewritePatternList * patterns,ConstructorArgs &&...args)59 void PopulateForBroadcastingBinaryOp(MLIRContext *context,
60                                      OwningRewritePatternList *patterns,
61                                      ConstructorArgs &&...args) {
62 #define POPULATE_BCAST(ChloOp, HloOp)                                      \
63   patterns->insert<                                                        \
64       Pattern<ChloOp, HloOp, HloBinaryElementwiseAdaptor<ChloOp, HloOp>>>( \
65       context, args...);
66 
67   POPULATE_BCAST(BroadcastAddOp, mhlo::AddOp);
68   POPULATE_BCAST(BroadcastAndOp, mhlo::AndOp);
69   POPULATE_BCAST(BroadcastAtan2Op, mhlo::Atan2Op);
70   POPULATE_BCAST(BroadcastDivOp, mhlo::DivOp);
71   POPULATE_BCAST(BroadcastMaxOp, mhlo::MaxOp);
72   POPULATE_BCAST(BroadcastMinOp, mhlo::MinOp);
73   POPULATE_BCAST(BroadcastMulOp, mhlo::MulOp);
74   POPULATE_BCAST(BroadcastOrOp, mhlo::OrOp);
75   POPULATE_BCAST(BroadcastPolygammaOp, PolygammaOp);
76   POPULATE_BCAST(BroadcastPowOp, mhlo::PowOp);
77   POPULATE_BCAST(BroadcastRemOp, mhlo::RemOp);
78   POPULATE_BCAST(BroadcastShiftLeftOp, mhlo::ShiftLeftOp);
79   POPULATE_BCAST(BroadcastShiftRightArithmeticOp, mhlo::ShiftRightArithmeticOp);
80   POPULATE_BCAST(BroadcastShiftRightLogicalOp, mhlo::ShiftRightLogicalOp);
81   POPULATE_BCAST(BroadcastSubOp, mhlo::SubOp);
82   POPULATE_BCAST(BroadcastXorOp, mhlo::XorOp);
83   POPULATE_BCAST(BroadcastZetaOp, ZetaOp);
84 
85   // Broadcasting ops requiring special construction.
86   patterns
87       ->insert<Pattern<BroadcastComplexOp, mhlo::ComplexOp, HloComplexAdaptor>>(
88           context, args...);
89   patterns
90       ->insert<Pattern<BroadcastCompareOp, mhlo::CompareOp, HloCompareAdaptor>>(
91           context, args...);
92 
93 #undef POPULATE_BCAST
94 }
95 
96 }  // namespace chlo
97 }  // namespace mlir
98 
99 #endif  // TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_DIALECT_MHLO_TRANSFORMS_MAP_CHLO_TO_HLO_OP_H_
100