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 
25 #include "llvm/ADT/STLExtras.h"
26 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
27 #include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
28 #include "mlir-hlo/utils/hlo_utils.h"
29 #include "mlir/IR/Attributes.h"
30 #include "mlir/IR/MLIRContext.h"
31 #include "mlir/IR/Operation.h"
32 #include "mlir/IR/TypeUtilities.h"
33 #include "mlir/IR/Types.h"
34 #include "mlir/Pass/Pass.h"
35 #include "mlir/Pass/PassRegistry.h"
36 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
37 
38 using mlir::FunctionPass;
39 using mlir::OwningRewritePatternList;
40 using mlir::PassWrapper;
41 
42 namespace {
43 class LowerComplexPass : public PassWrapper<LowerComplexPass, FunctionPass> {
44  public:
LowerComplexPass()45   explicit LowerComplexPass() : PassWrapper<LowerComplexPass, FunctionPass>() {}
46 
47   /// Performs the lowering to MHLO dialect.
48   void runOnFunction() override;
49 };
50 }  // end anonymous namespace
51 
52 namespace mlir {
53 namespace mhlo {
54 namespace {
55 
56 #include "generated_lower_complex.inc"
57 
58 }  // end anonymous namespace
59 
PopulateComplexLoweringPatterns(MLIRContext * context,OwningRewritePatternList * patterns)60 void PopulateComplexLoweringPatterns(MLIRContext* context,
61                                      OwningRewritePatternList* patterns) {
62   populateWithGenerated(context, *patterns);
63 }
64 }  // end namespace mhlo
65 }  // end namespace mlir
66 
67 // Lowers the complex operations that can be represented using other operations.
runOnFunction()68 void LowerComplexPass::runOnFunction() {
69   // Add lowering patterns to the list.
70   OwningRewritePatternList patterns;
71   mlir::mhlo::PopulateComplexLoweringPatterns(&getContext(), &patterns);
72 
73   (void)applyPatternsAndFoldGreedily(getFunction(), std::move(patterns));
74 }
75 
createLowerComplexPass()76 std::unique_ptr<FunctionPass> mlir::mhlo::createLowerComplexPass() {
77   return std::make_unique<LowerComplexPass>();
78 }
79