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/runtime/constraints.h"
17
18 #include <utility>
19
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "tensorflow/compiler/xla/runtime/errors.h"
23
24 namespace xla {
25 namespace runtime {
26
27 using llvm::ArrayRef;
28 using llvm::Expected;
29 using llvm::raw_ostream;
30 using llvm::StringRef;
31
operator <<(raw_ostream & os,const ArgumentConstraint & constraint)32 raw_ostream& operator<<(raw_ostream& os, const ArgumentConstraint& constraint) {
33 auto str = [](ArgumentConstraint constraint) {
34 switch (constraint) {
35 case ArgumentConstraint::kResolved:
36 return "resolved";
37 case ArgumentConstraint::kRank:
38 return "rank";
39 case ArgumentConstraint::kShape:
40 return "shape";
41 case ArgumentConstraint::kValue:
42 return "value";
43 default:
44 llvm_unreachable("unknown operand constraint");
45 }
46 };
47
48 os << str(constraint);
49 return os;
50 }
51
operator <<(raw_ostream & os,ArrayRef<ArgumentConstraint> constraints)52 raw_ostream& operator<<(raw_ostream& os,
53 ArrayRef<ArgumentConstraint> constraints) {
54 os << "[";
55 llvm::interleaveComma(constraints, os);
56 os << "]";
57 return os;
58 }
59
ParseArgumentConstraint(StringRef str)60 Expected<ArgumentConstraint> ParseArgumentConstraint(StringRef str) {
61 if (str == "rank") return ArgumentConstraint::kRank;
62 if (str == "shape") return ArgumentConstraint::kShape;
63 if (str == "value") return ArgumentConstraint::kValue;
64 return MakeStringError("unknown operand constraint: ", str);
65 }
66
67 } // namespace runtime
68 } // namespace xla
69