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 // This file implements logic for translating mixed IR to buffer form.
17
18 #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h" // from @llvm-project
19
20 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" // from @llvm-project
21 #include "mlir/Dialect/Complex/IR/Complex.h" // from @llvm-project
22 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
23 #include "mlir/Dialect/MemRef/IR/MemRef.h" // from @llvm-project
24 #include "mlir/Dialect/SCF/IR/SCF.h" // from @llvm-project
25 #include "mlir/IR/Attributes.h" // from @llvm-project
26 #include "mlir/IR/BlockAndValueMapping.h" // from @llvm-project
27 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
28 #include "mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project
29 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project
30 #include "tensorflow/compiler/mlir/tools/kernel_gen/ir/tf_framework_ops.h"
31 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/rewriters.h"
32 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
33
34 namespace mlir {
35 namespace kernel_gen {
36 namespace transforms {
37 namespace {
38
39 struct BufferizeJITExecuteOp
40 : public OpConversionPattern<tf_framework::JITExecuteOp> {
41 using OpConversionPattern::OpConversionPattern;
42
matchAndRewritemlir::kernel_gen::transforms::__anonbecf46750111::BufferizeJITExecuteOp43 LogicalResult matchAndRewrite(
44 tf_framework::JITExecuteOp op, OpAdaptor adaptor,
45 ConversionPatternRewriter &rewriter) const override {
46 Type result_ty = getTypeConverter()->convertType(op.getType());
47 rewriter.replaceOpWithNewOp<tf_framework::JITExecuteOp>(
48 op, result_ty, adaptor.getOperands(), op->getAttrs());
49 return success();
50 }
51 };
52
53 } // namespace
54
populateExtraBufferizePatterns(ConversionTarget & target,MLIRContext * context,bufferization::BufferizeTypeConverter * converter,RewritePatternSet * patterns)55 void populateExtraBufferizePatterns(
56 ConversionTarget &target, MLIRContext *context,
57 bufferization::BufferizeTypeConverter *converter,
58 RewritePatternSet *patterns) {
59 target.addLegalDialect<tf_framework::TFFrameworkDialect>();
60 auto typesAreLegal = [converter](Operation *op) {
61 return converter->isLegal(op->getOperandTypes()) &&
62 converter->isLegal(op->getResultTypes());
63 };
64 target.addDynamicallyLegalOp<tf_framework::JITExecuteOp>(typesAreLegal);
65 // clang-format off
66 patterns->add<
67 BufferizeJITExecuteOp
68 >(*converter, context);
69 // clang-format on
70 }
71
populateExtraBufferizeDialects(DialectRegistry & registry)72 void populateExtraBufferizeDialects(DialectRegistry ®istry) {
73 registry.insert<tf_framework::TFFrameworkDialect>();
74 }
75
76 } // namespace transforms
77 } // namespace kernel_gen
78 } // namespace mlir
79