xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/mlir_hlo/lib/Dialect/mhlo/transforms/lower_complex.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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 // Thsi file implements passes to convert complex operations to equivalent real
17 // value operations. This does not include removing complex values from function
18 // argument or return types.
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <iterator>
23 #include <numeric>
24 #include <utility>
25 
26 #include "llvm/ADT/STLExtras.h"
27 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
28 #include "mlir-hlo/Dialect/mhlo/transforms/PassDetail.h"
29 #include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
30 #include "mlir-hlo/Dialect/mhlo/transforms/rewriters.h"
31 #include "mlir-hlo/utils/hlo_utils.h"
32 #include "mlir/Dialect/Func/IR/FuncOps.h"
33 #include "mlir/IR/Attributes.h"
34 #include "mlir/IR/BuiltinOps.h"
35 #include "mlir/IR/MLIRContext.h"
36 #include "mlir/IR/Operation.h"
37 #include "mlir/IR/TypeUtilities.h"
38 #include "mlir/IR/Types.h"
39 #include "mlir/Pass/Pass.h"
40 #include "mlir/Pass/PassRegistry.h"
41 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
42 
43 namespace mlir {
44 namespace mhlo {
45 namespace {
46 class LowerComplexPass : public LowerComplexPassBase<LowerComplexPass> {
47  public:
48   /// Performs the lowering to MHLO dialect.
49   void runOnOperation() override;
50 };
51 
52 #include "generated_lower_complex.inc"
53 
54 // Lowers the complex operations that can be represented using other operations.
runOnOperation()55 void LowerComplexPass::runOnOperation() {
56   // Add lowering patterns to the list.
57   RewritePatternSet patterns(&getContext());
58   mlir::mhlo::populateComplexLoweringPatterns(&getContext(), &patterns);
59 
60   if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
61     return signalPassFailure();
62 }
63 
64 }  // end anonymous namespace
65 }  // end namespace mhlo
66 }  // end namespace mlir
67 
populateComplexLoweringPatterns(MLIRContext *,RewritePatternSet * patterns)68 void mlir::mhlo::populateComplexLoweringPatterns(MLIRContext* /*context*/,
69                                                  RewritePatternSet* patterns) {
70   populateWithGenerated(*patterns);
71 }
72 
73 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
createLowerComplexPass()74 mlir::mhlo::createLowerComplexPass() {
75   return std::make_unique<mlir::mhlo::LowerComplexPass>();
76 }
77