1 /* Copyright 2022 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 "tensorflow/core/transforms/func_to_graph/func_to_graph.h"
17
18 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
19 #include "tensorflow/core/ir/ops.h"
20 #include "tensorflow/core/platform/errors.h"
21 #include "tensorflow/core/platform/status.h"
22
23 namespace mlir {
24 namespace tfg {
25
FuncToGraph(GraphFuncOp func)26 tensorflow::Status FuncToGraph(GraphFuncOp func) {
27 MLIRContext *context = func->getContext();
28 auto version = func->getAttrOfType<VersionAttr>("tfg.lifted_graph_version");
29 if (!version) {
30 return tensorflow::errors::InvalidArgument(
31 "lifted graph func is missing version attribute");
32 }
33
34 auto lifted_value_attr_name =
35 StringAttr::get(context, "tfg.lifted_value_attr");
36
37 DenseMap<StringRef, Operation *> referred_ops;
38
39 if (ArrayAttr all_arg_attrs = func.getAllArgAttrs()) {
40 for (auto arg_attr : all_arg_attrs.getAsRange<DictionaryAttr>()) {
41 auto lifted_value_attr =
42 arg_attr.getAs<ArrayAttr>(lifted_value_attr_name);
43 // Control arg doesn't have lifted_value_attr, just skip it here. For
44 // non-control arg, this attribute is required. This invariant will be
45 // checked below.
46 if (!lifted_value_attr) continue;
47
48 // Init the entry with nullptr and it'll be updated with associated op
49 // later.
50 referred_ops.insert({lifted_value_attr[0].cast<StringAttr>().getValue(),
51 /*Operation=*/nullptr});
52 }
53 }
54
55 for (Operation &op : func.getBody()->without_terminator()) {
56 StringRef op_name = TFOp(op).name();
57 auto it = referred_ops.find(op_name);
58 if (it != referred_ops.end()) it->second = &op;
59 }
60
61 for (const auto &it : llvm::enumerate(func.getArguments())) {
62 if (it.value().getType().isa<ControlType>()) continue;
63
64 auto lifted_value_attr =
65 func.getArgAttrOfType<ArrayAttr>(it.index(), lifted_value_attr_name);
66 if (!lifted_value_attr) {
67 return tensorflow::errors::InvalidArgument(
68 "arg #", it.index(),
69 " is missing tfg.lifted_value_attr, can't be lowered");
70 }
71
72 StringRef value_defining_op_name =
73 lifted_value_attr[0].cast<StringAttr>().getValue();
74 Operation *op = referred_ops[value_defining_op_name];
75 if (!op) {
76 return tensorflow::errors::InvalidArgument(
77 "lifted arg can't find the associated operation: ",
78 value_defining_op_name.data());
79 }
80
81 uint64_t result_index =
82 lifted_value_attr[1].cast<IntegerAttr>().getValue().getZExtValue();
83 if (result_index >= op->getNumResults()) {
84 return tensorflow::errors::InvalidArgument(
85 "result index out of bound: seeing index ", result_index,
86 " from lifted_value_attr of arg #", it.index(), ", but op only has ",
87 op->getNumResults(), " results");
88 }
89
90 it.value().replaceAllUsesWith(op->getResult(result_index));
91 }
92
93 OpBuilder builder(func);
94 auto graph = builder.create<GraphOp>(func.getLoc(), version);
95
96 // Remove the terminator.
97 func.getBody()->getTerminator()->erase();
98 graph.getRegion().takeBody(func.getRegion());
99 func.erase();
100
101 return tensorflow::Status::OK();
102 }
103
104 } // namespace tfg
105 } // namespace mlir
106