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