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 #include "tensorflow/compiler/xla/types.h" 18 #include "tensorflow/compiler/xla/util.h" 19 #include "tensorflow/core/lib/gtl/flatmap.h" 20 21 namespace xla { 22 23 string HloOpcodeString(HloOpcode opcode) { 24 switch (opcode) { 25 #define CASE_OPCODE_STRING(enum_name, opcode_name, ...) \ 26 case HloOpcode::enum_name: \ 27 return opcode_name; 28 HLO_OPCODE_LIST(CASE_OPCODE_STRING) 29 #undef CASE_OPCODE_STRING 30 } 31 } 32 33 StatusOr<HloOpcode> StringToHloOpcode(const string& opcode_name) { 34 static auto* opcode_map = new tensorflow::gtl::FlatMap<string, HloOpcode>({ 35 #define STRING_TO_OPCODE_ENTRY(enum_name, opcode_name, ...) \ 36 {opcode_name, HloOpcode::enum_name}, 37 HLO_OPCODE_LIST(STRING_TO_OPCODE_ENTRY) 38 #undef STRING_TO_OPCODE_ENTRY 39 }); 40 auto it = opcode_map->find(opcode_name); 41 if (it == opcode_map->end()) { 42 return InvalidArgument("Unknown opcode: %s", opcode_name.c_str()); 43 } 44 return it->second; 45 } 46 47 #define CHECK_DEFAULT(property_name, opcode_name) false 48 #define CHECK_PROPERTY(property_name, opcode_name, value) \ 49 (value & property_name) 50 #define RESOLVE(_1, _2, target, ...) target 51 #define HAS_PROPERTY(property, ...) \ 52 RESOLVE(__VA_ARGS__, CHECK_PROPERTY, CHECK_DEFAULT)(property, __VA_ARGS__) 53 54 bool HloOpcodeIsComparison(HloOpcode opcode) { 55 switch (opcode) { 56 #define CASE_IS_COMPARISON(enum_name, ...) \ 57 case HloOpcode::enum_name: \ 58 return HAS_PROPERTY(kHloOpcodeIsComparison, __VA_ARGS__); 59 HLO_OPCODE_LIST(CASE_IS_COMPARISON) 60 #undef CASE_IS_COMPARISON 61 } 62 } 63 64 bool HloOpcodeIsVariadic(HloOpcode opcode) { 65 switch (opcode) { 66 #define CASE_IS_VARIADIC(enum_name, ...) \ 67 case HloOpcode::enum_name: \ 68 return HAS_PROPERTY(kHloOpcodeIsVariadic, __VA_ARGS__); 69 HLO_OPCODE_LIST(CASE_IS_VARIADIC) 70 #undef CASE_IS_VARIADIC 71 } 72 } 73 74 #undef HAS_PROPERTY 75 #undef RESOLVE 76 #undef CHECK_DEFAULT 77 #undef CHECK_PROPERTY 78 79 } // namespace xla 80