1 /* Copyright 2021 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 // This file implements logic for lowering LHLO GPU dialect to TFRT CUDA
17 // dialect.
18 
19 #include "tensorflow/compiler/mlir/tfrt/transforms/lmhlo_to_gpu/lmhlo_to_gpu_binary.h"
20 
21 #include <memory>
22 #include <utility>
23 
24 #include "mlir/IR/BuiltinOps.h"
25 #include "mlir/IR/MLIRContext.h"
26 #include "mlir/IR/PatternMatch.h"
27 #include "mlir/Pass/Pass.h"
28 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
29 #include "tensorflow/compiler/xla/service/gpu/ir_emitter_unnested.h"
30 
31 namespace tensorflow {
32 
33 void populateKernelOpsPattern(mlir::RewritePatternSet&, GpuBinaryOptions);
34 
35 namespace {
36 
37 #define GEN_PASS_CLASSES
38 #include "tensorflow/compiler/mlir/tfrt/transforms/lmhlo_to_gpu/gpu_passes.h.inc"
39 
40 struct ConvertLmhloToGpuBinaryPass
41     : public ConvertLmhloToGpuBinaryPassBase<ConvertLmhloToGpuBinaryPass> {
42  public:
ConvertLmhloToGpuBinaryPasstensorflow::__anon9201607e0111::ConvertLmhloToGpuBinaryPass43   explicit ConvertLmhloToGpuBinaryPass(GpuBinaryOptions options)
44       : options(options) {}
45 
46  private:
runOnOperationtensorflow::__anon9201607e0111::ConvertLmhloToGpuBinaryPass47   void runOnOperation() override {
48     mlir::RewritePatternSet patterns(&getContext());
49     populateKernelOpsPattern(patterns, options);
50     if (failed(applyOpPatternsAndFold(getOperation(), std::move(patterns))))
51       return signalPassFailure();
52   }
53 
getDependentDialectstensorflow::__anon9201607e0111::ConvertLmhloToGpuBinaryPass54   void getDependentDialects(mlir::DialectRegistry& registry) const override {
55     xla::gpu::IrEmitterUnnested::GetDependentDialects(registry);
56   }
57 
58   GpuBinaryOptions options;
59 };
60 
61 }  // namespace
62 
createConvertLmhloToGpuBinaryPass(GpuBinaryOptions options)63 std::unique_ptr<mlir::Pass> createConvertLmhloToGpuBinaryPass(
64     GpuBinaryOptions options) {
65   return std::make_unique<ConvertLmhloToGpuBinaryPass>(options);
66 }
67 
registerConvertLmhloToGpuBinaryPass()68 void registerConvertLmhloToGpuBinaryPass() {
69   ::mlir::registerPass([] { return createConvertLmhloToGpuBinaryPass(); });
70 }
71 
72 }  // namespace tensorflow
73