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 <utility>
17 
18 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"  // from @llvm-project
19 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
20 #include "mlir/Dialect/MemRef/IR/MemRef.h"  // from @llvm-project
21 #include "mlir/Pass/Pass.h"  // from @llvm-project
22 #include "mlir/Transforms/DialectConversion.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/tools/kernel_gen/ir/tf_framework_ops.h"
24 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
25 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/rewriters.h"
26 
27 namespace mlir {
28 namespace kernel_gen {
29 namespace tf_framework {
30 namespace {
31 
32 #define GEN_PASS_CLASSES
33 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
34 
IsNotInsideTfEntryFunction(Operation * op)35 bool IsNotInsideTfEntryFunction(Operation* op) {
36   auto func = op->getParentOfType<func::FuncOp>();
37   return !func->hasAttrOfType<UnitAttr>(TFFrameworkDialect::kTFEntryAttrName);
38 }
39 
40 template <typename OpTy>
HasInitializedOpKernelContextOperand(OpTy op)41 bool HasInitializedOpKernelContextOperand(OpTy op) {
42   return op.ctx() != nullptr;
43 }
44 
45 // The pass rewrites the function marked with `tf_entry` attribute.
46 // * adds tf_framework::OpKernelContextType argument to the function,
47 // * std.alloc becomes tf_framework.alloc_raw,
48 // * std.dealloc becomes tf_framework.dealloc_raw.
49 class EmbedTFFrameworkPass
50     : public EmbedTFFrameworkPassBase<EmbedTFFrameworkPass> {
getDependentDialects(DialectRegistry & registry) const51   void getDependentDialects(DialectRegistry& registry) const override {
52     registry.insert<mlir::kernel_gen::tf_framework::TFFrameworkDialect>();
53   }
54 
55  public:
runOnOperation()56   void runOnOperation() override {
57     ModuleOp m = getOperation();
58 
59     // Populate patterns.
60     RewritePatternSet patterns(&getContext());
61     PopulateEmbedTFFrameworkPatterns(&patterns);
62 
63     // Set target.
64     ConversionTarget target(getContext());
65     target.addLegalDialect<tf_framework::TFFrameworkDialect>();
66 
67     target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
68       if (!op->hasAttrOfType<UnitAttr>(TFFrameworkDialect::kTFEntryAttrName)) {
69         return true;
70       }
71       FunctionType func_type = op.getFunctionType();
72       return func_type.getNumInputs() > 0 &&
73              func_type.getInput(0).isa<OpKernelContextType>();
74     });
75     target.addDynamicallyLegalOp<cf::AssertOp, memref::AllocOp,
76                                  memref::DeallocOp>(IsNotInsideTfEntryFunction);
77     target.addDynamicallyLegalOp<JITExecuteOp>(
78         &HasInitializedOpKernelContextOperand<JITExecuteOp>);
79     target.addDynamicallyLegalOp<JITCompileFromStrOp>(
80         &HasInitializedOpKernelContextOperand<JITCompileFromStrOp>);
81 
82     if (failed(applyPartialConversion(m, target, std::move(patterns)))) {
83       signalPassFailure();
84     }
85   }
86 };
87 
88 }  // namespace
89 
CreateEmbedTFFrameworkPass()90 std::unique_ptr<OperationPass<ModuleOp> > CreateEmbedTFFrameworkPass() {
91   return std::make_unique<EmbedTFFrameworkPass>();
92 }
93 
94 }  // namespace tf_framework
95 }  // namespace kernel_gen
96 }  // namespace mlir
97