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 "llvm/Support/raw_ostream.h"
17 #include "mlir/Analysis/Liveness.h" // from @llvm-project
18 #include "mlir/Dialect/Linalg/IR/LinalgOps.h" // from @llvm-project
19 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
20 #include "mlir/IR/AffineMap.h" // from @llvm-project
21 #include "mlir/IR/BuiltinOps.h" // from @llvm-project
22 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
23 #include "mlir/IR/Operation.h" // from @llvm-project
24 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project
25 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h"
26 #include "tensorflow/compiler/mlir/tools/kernel_gen/ir/tf_framework_ops.h"
27 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
28 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/rewriters.h"
29
30 namespace mlir {
31 namespace kernel_gen {
32 namespace transforms {
33 namespace {
34
35 #define GEN_PASS_CLASSES
36 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
37
38 using tf_framework::TFFrameworkDialect;
39
emitCallToPrint(Location loc,StringRef func_name,Value arg,OpBuilder * b)40 Operation* emitCallToPrint(Location loc, StringRef func_name, Value arg,
41 OpBuilder* b) {
42 auto caller_func =
43 b->getInsertionBlock()->getParent()->getParentOfType<FuncOp>();
44 auto callee_func =
45 SymbolTable::lookupNearestSymbolFrom<FuncOp>(caller_func, func_name);
46 if (!callee_func) {
47 OpBuilder::InsertionGuard insertGuard(*b);
48
49 auto module = caller_func->getParentOfType<ModuleOp>();
50 b->setInsertionPointToStart(module.getBody());
51 auto func_type = FunctionType::get(b->getContext(), arg.getType(),
52 /*results=*/llvm::None);
53 callee_func = b->create<FuncOp>(module.getLoc(), func_name, func_type);
54 callee_func.setPrivate();
55 }
56 return b->create<CallOp>(loc, callee_func, arg);
57 }
58
EmitPrint(Operation * op,Liveness & liveness,OpBuilder * b)59 void EmitPrint(Operation* op, Liveness& liveness, OpBuilder* b) {
60 Location loc = op->getLoc();
61 Value memref = op->getResult(0);
62 auto memref_type = memref.getType().cast<MemRefType>();
63 Type element_type = memref_type.getElementType();
64 if (!element_type.isF32() && !element_type.isF64() &&
65 !element_type.isIntOrIndex())
66 return;
67
68 Operation* end_op =
69 liveness.getLiveness(op->getBlock())->getEndOperation(memref, op);
70 b->setInsertionPoint(end_op);
71
72 if (element_type.isIndex()) {
73 element_type = b->getI64Type();
74 memref_type = MemRefType::get(memref_type.getShape(), element_type,
75 memref_type.getAffineMaps(),
76 memref_type.getMemorySpace());
77 memref = b->create<IndexCastOp>(loc, memref, memref_type);
78 }
79
80 auto unranked_type =
81 UnrankedMemRefType::get(element_type, memref_type.getMemorySpace());
82 Value unranked_memref = b->create<MemRefCastOp>(loc, memref, unranked_type);
83
84 if (element_type.isF32()) {
85 emitCallToPrint(loc, "print_memref_f32", unranked_memref, b);
86 return;
87 }
88 if (element_type.isF64()) {
89 emitCallToPrint(loc, "print_memref_f64", unranked_memref, b);
90 return;
91 }
92 if (element_type.isInteger(32)) {
93 emitCallToPrint(loc, "print_memref_i32", unranked_memref, b);
94 return;
95 }
96 if (element_type.isInteger(64) || element_type.isIndex()) {
97 emitCallToPrint(loc, "print_memref_i64", unranked_memref, b);
98 return;
99 }
100 }
101
102 // The pass the memrefs allocated in a `tf-entry` function and inserts printing
103 // at the end of their lifetime. Printing for buffers allocated with TFAllocOp
104 // is currently not supported because the data is not located on host.
105 struct EmbedMemRefPrintsPass
106 : public EmbedMemRefPrintsPassBase<EmbedMemRefPrintsPass> {
runOnFunctionmlir::kernel_gen::transforms::__anond5db4bbb0111::EmbedMemRefPrintsPass107 void runOnFunction() override {
108 FuncOp func = getFunction();
109 if (!func->getAttrOfType<UnitAttr>(TFFrameworkDialect::kTFEntryAttrName))
110 return;
111
112 Liveness liveness(func);
113 OpBuilder b(&getContext());
114 func.walk([&](AllocOp op) { EmitPrint(op, liveness, &b); });
115 func.walk([&](AllocaOp op) { EmitPrint(op, liveness, &b); });
116 func.walk([&](MemRefReinterpretCastOp op) { EmitPrint(op, liveness, &b); });
117 }
118 };
119
120 } // namespace
121
CreateEmbedMemRefPrintsPass()122 std::unique_ptr<FunctionPass> CreateEmbedMemRefPrintsPass() {
123 return std::make_unique<EmbedMemRefPrintsPass>();
124 }
125
126 } // namespace transforms
127 } // namespace kernel_gen
128 } // namespace mlir
129