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 #include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h" // from @llvm-project
17 #include "mlir/Conversion/GPUToNVVM/GPUToNVVMPass.h" // from @llvm-project
18 #include "mlir/Conversion/GPUToROCDL/GPUToROCDLPass.h" // from @llvm-project
19 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h" // from @llvm-project
20 #include "mlir/Dialect/GPU/GPUDialect.h" // from @llvm-project
21 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" // from @llvm-project
22 #include "mlir/Dialect/LLVMIR/LLVMTypes.h" // from @llvm-project
23 #include "mlir/Dialect/LLVMIR/NVVMDialect.h" // from @llvm-project
24 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h" // from @llvm-project
25 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project
26 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
27
28 namespace mlir {
29 namespace kernel_gen {
30 namespace transforms {
31
32 using gpu::GPUModuleOp;
33
34 namespace {
35
36 #define GEN_PASS_CLASSES
37 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
38
39 /// A pass that does the final lowering to NVVM. It collects all the patterns
40 /// that are currently required, currently mixing std, linalg and gpu.
41 class GpuKernelToNVVMPass
42 : public GpuKernelToNVVMPassBase<GpuKernelToNVVMPass> {
getDependentDialects(mlir::DialectRegistry & registry) const43 void getDependentDialects(mlir::DialectRegistry& registry) const override {
44 registry.insert<mlir::NVVM::NVVMDialect, mlir::LLVM::LLVMDialect>();
45 }
46
47 public:
runOnOperation()48 void runOnOperation() override {
49 GPUModuleOp m = getOperation();
50
51 OwningRewritePatternList patterns;
52 mlir::LowerToLLVMOptions llvm_opts;
53 llvm_opts.indexBitwidth = 32;
54 LLVMTypeConverter converter(m.getContext(), llvm_opts);
55 populateStdToLLVMConversionPatterns(converter, patterns);
56 populateGpuToNVVMConversionPatterns(converter, patterns);
57 populateComplexToLLVMConversionPatterns(converter, patterns);
58 ConversionTarget target(getContext());
59 configureGpuToNVVMConversionLegality(target);
60 if (failed(mlir::applyFullConversion(m, target, std::move(patterns)))) {
61 signalPassFailure();
62 }
63 }
64 };
65
66 /// A pass that does the final lowering to ROCDL. It collects all the patterns
67 /// that are currently required, currently mixing std, linalg and gpu.
68 class GpuKernelToROCDLPass
69 : public GpuKernelToNVVMPassBase<GpuKernelToROCDLPass> {
getDependentDialects(mlir::DialectRegistry & registry) const70 void getDependentDialects(mlir::DialectRegistry& registry) const override {
71 registry.insert<mlir::ROCDL::ROCDLDialect, mlir::LLVM::LLVMDialect>();
72 }
73
74 public:
runOnOperation()75 void runOnOperation() override {
76 gpu::GPUModuleOp m = getOperation();
77
78 OwningRewritePatternList patterns;
79 LLVMTypeConverter converter(m.getContext());
80 populateStdToLLVMConversionPatterns(converter, patterns);
81 populateGpuToROCDLConversionPatterns(converter, patterns);
82 populateComplexToLLVMConversionPatterns(converter, patterns);
83 ConversionTarget target(getContext());
84 configureGpuToROCDLConversionLegality(target);
85 if (failed(mlir::applyFullConversion(m, target, std::move(patterns)))) {
86 signalPassFailure();
87 }
88 }
89 };
90
91 } // namespace
92
CreateGpuKernelToNvvmPass()93 std::unique_ptr<OperationPass<GPUModuleOp> > CreateGpuKernelToNvvmPass() {
94 return std::make_unique<GpuKernelToNVVMPass>();
95 }
96
CreateGpuKernelToRocdlPass()97 std::unique_ptr<OperationPass<GPUModuleOp> > CreateGpuKernelToRocdlPass() {
98 return std::make_unique<GpuKernelToROCDLPass>();
99 }
100
101 } // namespace transforms
102 } // namespace kernel_gen
103 } // namespace mlir
104