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