xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/mlir_hlo/lib/Transforms/generic_host_to_llvm.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 #include <memory>
16 #include <utility>
17 
18 #include "mlir-hlo/Transforms/PassDetail.h"
19 #include "mlir-hlo/Transforms/passes.h"
20 #include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
21 #include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h"
22 #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
23 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
24 #include "mlir/Conversion/LLVMCommon/TypeConverter.h"
25 #include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
26 #include "mlir/Conversion/MathToLibm/MathToLibm.h"
27 #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
28 #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
29 #include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h"
30 #include "mlir/Dialect/Arithmetic/Transforms/Passes.h"
31 #include "mlir/Dialect/Complex/IR/Complex.h"
32 #include "mlir/Dialect/Func/IR/FuncOps.h"
33 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
34 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
35 #include "mlir/Dialect/Math/IR/Math.h"
36 #include "mlir/Dialect/MemRef/Transforms/Passes.h"
37 
38 namespace mlir {
39 namespace {
40 
41 class GenericHostToLLVMPass
42     : public GenericHostToLLVMPassBase<GenericHostToLLVMPass> {
getDependentDialects(DialectRegistry & registry) const43   void getDependentDialects(DialectRegistry &registry) const override {
44     registry.insert<LLVM::LLVMDialect>();
45   }
46 
47  public:
48   explicit GenericHostToLLVMPass() = default;
49 
runOnOperation()50   void runOnOperation() override {
51     ModuleOp m = getOperation();
52 
53     // Populate type conversions.
54     MLIRContext *ctx = m.getContext();
55     LLVMTypeConverter typeConverter(ctx);
56 
57     // Populate patterns.
58     RewritePatternSet patterns(&getContext());
59     arith::populateArithmeticExpandOpsPatterns(patterns);
60     memref::populateExpandOpsPatterns(patterns);
61     arith::populateArithmeticToLLVMConversionPatterns(typeConverter, patterns);
62     populateMemRefToLLVMConversionPatterns(typeConverter, patterns);
63     populateMathToLLVMConversionPatterns(typeConverter, patterns);
64     populateFuncToLLVMConversionPatterns(typeConverter, patterns);
65     cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns);
66     populateSCFToControlFlowConversionPatterns(patterns);
67     populateComplexToLLVMConversionPatterns(typeConverter, patterns);
68     populateVectorToLLVMConversionPatterns(typeConverter, patterns);
69     // MathToLibm patterns are a last resort, so they have a 0 benefit.
70     populateMathToLibmConversionPatterns(patterns, 0);
71 
72     //  Set target.
73     ConversionTarget target(*ctx);
74     target.addLegalDialect<LLVM::LLVMDialect>();
75     target.addIllegalDialect<arith::ArithmeticDialect, func::FuncDialect,
76                              complex::ComplexDialect, math::MathDialect>();
77     // Mark modules as legal.
78     target.addLegalOp<ModuleOp>();
79     // Unrealized conversion casts are cleaned up by a separate pass.
80     target.addLegalOp<UnrealizedConversionCastOp>();
81 
82     if (failed(applyFullConversion(m, target, std::move(patterns)))) {
83       signalPassFailure();
84     }
85   }
86 };
87 
88 }  // namespace
89 
90 namespace hlo {
91 
createGenericHostToLLVMPass()92 std::unique_ptr<OperationPass<ModuleOp> > createGenericHostToLLVMPass() {
93   return std::make_unique<GenericHostToLLVMPass>();
94 }
95 
96 }  // namespace hlo
97 }  // namespace mlir
98