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/compiler/xla/mlir/utils/runtime/constraints.h"
17
18 #include <utility>
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/Error.h"
22 #include "tfrt/support/error_util.h" // from @tf_runtime
23
24 namespace xla {
25 namespace runtime {
26
27 using namespace mlir; // NOLINT
28
29 using llvm::Expected;
30 using llvm::SmallVector;
31
32 using tfrt::MakeStringError;
33
GetArgumentsConstraints(func::FuncOp func)34 Expected<SmallVector<ArgumentConstraint>> GetArgumentsConstraints(
35 func::FuncOp func) {
36 llvm::SmallVector<ArgumentConstraint> constraints;
37 constraints.reserve(func.getNumArguments());
38
39 auto parse = [](Attribute attr) -> Expected<ArgumentConstraint> {
40 // If attribute is not defined it means that there is no constraint.
41 if (!attr) return ArgumentConstraint::kResolved;
42
43 // Otherwise try to parse constraint from the string attribute.
44 auto str = attr.dyn_cast_or_null<StringAttr>();
45 if (!str)
46 return MakeStringError("unexpected ", kArgumentConstraintAttrName,
47 " attribute");
48 return ParseArgumentConstraint(str.getValue());
49 };
50
51 for (int i = 0; i < func.getNumArguments(); ++i) {
52 auto arg_type = func.getFunctionType().getInput(i);
53
54 auto constraint = parse(func.getArgAttr(i, kArgumentConstraintAttrName));
55 if (auto err = constraint.takeError()) return std::move(err);
56
57 auto resolved = ResolveArgumentConstraint(*constraint, arg_type);
58 if (auto err = resolved.takeError()) return std::move(err);
59
60 constraints.push_back(*resolved);
61 }
62
63 return constraints;
64 }
65
ResolveArgumentConstraint(ArgumentConstraint constraint,Type type)66 Expected<ArgumentConstraint> ResolveArgumentConstraint(
67 ArgumentConstraint constraint, Type type) {
68 // Skip already resolved constraints.
69 if (constraint == ArgumentConstraint::kResolved) return constraint;
70
71 // Operand must be a shaped type: memref or tensor.
72 auto shaped = type.dyn_cast<ShapedType>();
73 if (!shaped) return MakeStringError("unsupported operand type: ", type);
74
75 // Resolve `rank` constraint if rank is known at compile time.
76 if (constraint == ArgumentConstraint::kRank && shaped.hasRank())
77 return ArgumentConstraint::kResolved;
78
79 // Resolve `shape` constraint if shape is known at compile time.
80 if (constraint == ArgumentConstraint::kShape && shaped.hasStaticShape())
81 return ArgumentConstraint::kResolved;
82
83 // Leave the `value` constraint unmodified if the operand is sinkable.
84 if (constraint == ArgumentConstraint::kValue) {
85 if (SupportsValueSpecialization(shaped)) return constraint;
86 return MakeStringError("cannot sink operand type: ", type);
87 }
88
89 return constraint;
90 }
91
92 } // namespace runtime
93 } // namespace xla
94