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