xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/hlo_opcode.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 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/service/hlo_opcode.h"
17 
18 #include "absl/container/flat_hash_map.h"
19 #include "tensorflow/compiler/xla/types.h"
20 #include "tensorflow/compiler/xla/util.h"
21 
22 namespace xla {
23 
HloOpcodeString(HloOpcode opcode)24 std::string HloOpcodeString(HloOpcode opcode) {
25   switch (opcode) {
26 #define CASE_OPCODE_STRING(enum_name, opcode_name, ...) \
27   case HloOpcode::enum_name:                            \
28     return opcode_name;
29     HLO_OPCODE_LIST(CASE_OPCODE_STRING)
30 #undef CASE_OPCODE_STRING
31   }
32 }
33 
StringToHloOpcode(const std::string & opcode_name)34 StatusOr<HloOpcode> StringToHloOpcode(const std::string& opcode_name) {
35   static auto* opcode_map = new absl::flat_hash_map<std::string, HloOpcode>({
36 #define STRING_TO_OPCODE_ENTRY(enum_name, opcode_name, ...) \
37   {opcode_name, HloOpcode::enum_name},
38       HLO_OPCODE_LIST(STRING_TO_OPCODE_ENTRY)
39 #undef STRING_TO_OPCODE_ENTRY
40   });
41   auto it = opcode_map->find(opcode_name);
42   if (it == opcode_map->end()) {
43     return InvalidArgument("Unknown opcode: %s", opcode_name);
44   }
45   return it->second;
46 }
47 
HloOpcodeIsComparison(HloOpcode opcode)48 bool HloOpcodeIsComparison(HloOpcode opcode) {
49   return opcode == HloOpcode::kCompare;
50 }
51 
HloOpcodeIsVariadic(HloOpcode opcode)52 bool HloOpcodeIsVariadic(HloOpcode opcode) {
53   switch (opcode) {
54 #define CASE_IS_VARIADIC(enum_name, opcode_name, arity, ...) \
55   case HloOpcode::enum_name:                                 \
56     return arity == kHloOpcodeIsVariadic;
57     HLO_OPCODE_LIST(CASE_IS_VARIADIC)
58 #undef CASE_IS_VARIADIC
59   }
60 }
61 
HloOpcodeArity(HloOpcode opcode)62 std::optional<int> HloOpcodeArity(HloOpcode opcode) {
63   switch (opcode) {
64 #define CASE_ARITY(enum_name, opcode_name, arity, ...)  \
65   case HloOpcode::enum_name:                            \
66     return arity == kHloOpcodeIsVariadic ? std::nullopt \
67                                          : std::make_optional(arity);
68     HLO_OPCODE_LIST(CASE_ARITY)
69 #undef CASE_ARITY
70   }
71 }
72 
HloOpcodeIsAsync(HloOpcode opcode)73 bool HloOpcodeIsAsync(HloOpcode opcode) {
74   return opcode == HloOpcode::kAsyncStart ||
75          opcode == HloOpcode::kAsyncUpdate || opcode == HloOpcode::kAsyncDone;
76 }
77 
78 }  // namespace xla
79